Skip to content

Commit

Permalink
java: add the skeleton of a java vm program
Browse files Browse the repository at this point in the history
This program loads the given class and determines whether a main method is
present.

Signed-off-by: Vegard Nossum <[email protected]>
  • Loading branch information
Vegard Nossum committed Jun 5, 2008
1 parent 7c2b828 commit f9ca163
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ add_library (cafebabe SHARED
)

add_executable (dump src/dump.c)

target_link_libraries (dump cafebabe)

add_executable (java src/java.c)
target_link_libraries (java cafebabe)

set (LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib
CACHE PATH "Installation prefix for user executables" FORCE)
set (INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include
Expand Down
87 changes: 87 additions & 0 deletions src/java.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* cafebabe - the class loader library in C
* Copyright (C) 2008 Vegard Nossum <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#define _GNU_SOURCE

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cafebabe/access.h"
#include "cafebabe/attribute_info.h"
#include "cafebabe/class.h"
#include "cafebabe/constant_pool.h"
#include "cafebabe/error.h"
#include "cafebabe/field_info.h"
#include "cafebabe/method_info.h"
#include "cafebabe/stream.h"

int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s CLASS\n", argv[0]);
exit(EXIT_FAILURE);
}

const char *classname = argv[1];
char *filename;
if (asprintf(&filename, "%s.class", classname) == -1) {
fprintf(stderr, "error: %s\n", strerror(errno));
goto out;
}

struct cafebabe_stream stream;
if (cafebabe_stream_open(&stream, filename)) {
fprintf(stderr, "error: %s: %s\n", filename,
cafebabe_stream_error(&stream));
goto out_filename;
}

struct cafebabe_class class;
if (cafebabe_class_init(&class, &stream)) {
fprintf(stderr, "error: %s:%d/%d: %s\n", filename,
stream.virtual_i, stream.virtual_n,
cafebabe_stream_error(&stream));
goto out_stream;
}

struct cafebabe_method_info *main_method;
if (cafebabe_class_get_method(&class,
"main", "([Ljava/lang/String;)V", &main_method))
{
fprintf(stderr, "error: %s: no main method\n", classname);
goto out_class;
}

cafebabe_class_deinit(&class);
cafebabe_stream_close(&stream);

return EXIT_SUCCESS;

out_class:
cafebabe_class_deinit(&class);
out_stream:
cafebabe_stream_close(&stream);
out_filename:
free(filename);
out:
return EXIT_FAILURE;
}

0 comments on commit f9ca163

Please sign in to comment.