summaryrefslogtreecommitdiff
path: root/src/nsgenbind.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nsgenbind.c')
-rw-r--r--src/nsgenbind.c324
1 files changed, 148 insertions, 176 deletions
diff --git a/src/nsgenbind.c b/src/nsgenbind.c
index d81f30f..3174fa0 100644
--- a/src/nsgenbind.c
+++ b/src/nsgenbind.c
@@ -22,201 +22,173 @@ struct options *options;
static struct options* process_cmdline(int argc, char **argv)
{
- int opt;
+ int opt;
- options = calloc(1,sizeof(struct options));
- if (options == NULL) {
- fprintf(stderr, "Allocation error\n");
- return NULL;
- }
+ options = calloc(1,sizeof(struct options));
+ if (options == NULL) {
+ fprintf(stderr, "Allocation error\n");
+ return NULL;
+ }
- while ((opt = getopt(argc, argv, "vgDW::d:I:o:h:")) != -1) {
- switch (opt) {
- case 'I':
- options->idlpath = strdup(optarg);
- break;
+ while ((opt = getopt(argc, argv, "vgDW::I:")) != -1) {
+ switch (opt) {
+ case 'I':
+ options->idlpath = strdup(optarg);
+ break;
- case 'o':
- options->outfilename = strdup(optarg);
- break;
+ case 'v':
+ options->verbose = true;
+ break;
- case 'h':
- options->hdrfilename = strdup(optarg);
- break;
+ case 'D':
+ options->debug = true;
+ break;
- case 'd':
- options->depfilename = strdup(optarg);
- break;
+ case 'g':
+ options->dbglog = true;
+ break;
- case 'v':
- options->verbose = true;
- break;
+ case 'W':
+ options->warnings = 1; /* warning flags */
+ break;
- case 'D':
- options->debug = true;
- break;
+ default: /* '?' */
+ fprintf(stderr,
+ "Usage: %s [-v] [-g] [-D] [-W] [-I idlpath] inputfile outputdir\n",
+ argv[0]);
+ free(options);
+ return NULL;
- case 'g':
- options->dbglog = true;
- break;
+ }
+ }
- case 'W':
- options->warnings = 1; /* warning flags */
- break;
+ if (optind > (argc - 2)) {
+ fprintf(stderr,
+ "Error: expected input filename and output directory\n");
+ free(options);
+ return NULL;
+ }
- default: /* '?' */
- fprintf(stderr,
- "Usage: %s [-v] [-g] [-D] [-W] [-d depfilename] [-I idlpath] [-o filename] [-h headerfile] inputfile\n",
- argv[0]);
- free(options);
- return NULL;
+ options->infilename = strdup(argv[optind]);
- }
- }
+ options->outdirname = strdup(argv[optind + 1]);
- if (optind >= argc) {
- fprintf(stderr, "Error: expected input filename\n");
- free(options);
- return NULL;
- }
-
- options->infilename = strdup(argv[optind]);
-
- return options;
+ return options;
}
static int generate_binding(struct genbind_node *binding_node, void *ctx)
{
- struct genbind_node *genbind_root = ctx;
- char *type;
- int res = 10;
-
- type = genbind_node_gettext(
- genbind_node_find_type(
- genbind_node_getnode(binding_node),
- NULL,
- GENBIND_NODE_TYPE_TYPE));
-
- if (strcmp(type, "jsapi_libdom") == 0) {
- res = jsapi_libdom_output(options, genbind_root, binding_node);
- } else {
- fprintf(stderr, "Error: unsupported binding type \"%s\"\n", type);
- }
-
- return res;
+ struct genbind_node *genbind_root = ctx;
+ char *type;
+ int res = 10;
+
+ type = genbind_node_gettext(
+ genbind_node_find_type(
+ genbind_node_getnode(binding_node),
+ NULL,
+ GENBIND_NODE_TYPE_TYPE));
+
+ if (strcmp(type, "jsapi_libdom") == 0) {
+ res = jsapi_libdom_output(options, genbind_root, binding_node);
+ } else {
+ fprintf(stderr, "Error: unsupported binding type \"%s\"\n", type);
+ }
+
+ return res;
+}
+
+enum bindingtype_e {
+ BINDINGTYPE_UNKNOWN,
+ BINDINGTYPE_JSAPI_LIBDOM,
+ BINDINGTYPE_DUK_LIBDOM,
+};
+
+/**
+ * get the type of binding
+ */
+static enum bindingtype_e genbind_get_type(struct genbind_node *node)
+{
+ struct genbind_node *binding_node;
+ const char *binding_type;
+
+ binding_node = genbind_node_find_type(node,
+ NULL,
+ GENBIND_NODE_TYPE_BINDING);
+ if (binding_node == NULL) {
+ /* binding entry is missing which is invalid */
+ return BINDINGTYPE_UNKNOWN;
+ }
+
+ binding_type = genbind_node_gettext(
+ genbind_node_find_type(
+ genbind_node_getnode(binding_node),
+ NULL,
+ GENBIND_NODE_TYPE_TYPE));
+ if (binding_type == NULL) {
+ fprintf(stderr, "Error: missing binding type\n");
+ return BINDINGTYPE_UNKNOWN;
+ }
+
+ if (strcmp(binding_type, "jsapi_libdom") == 0) {
+ return BINDINGTYPE_JSAPI_LIBDOM;
+ }
+
+ if (strcmp(binding_type, "duk_libdom") == 0) {
+ return BINDINGTYPE_DUK_LIBDOM;
+ }
+
+ fprintf(stderr, "Error: unsupported binding type \"%s\"\n", binding_type);
+
+ return BINDINGTYPE_UNKNOWN;
}
int main(int argc, char **argv)
{
- int res;
- struct genbind_node *genbind_root;
-
- options = process_cmdline(argc, argv);
- if (options == NULL) {
- return 1; /* bad commandline */
- }
-
- if (options->verbose &&
- (options->outfilename == NULL)) {
- fprintf(stderr,
- "Error: output to stdout with verbose logging would fail\n");
- return 2;
- }
-
- if (options->depfilename != NULL &&
- options->outfilename == NULL) {
- fprintf(stderr,
- "Error: output to stdout with dep generation would fail\n");
- return 3;
- }
-
- if (options->depfilename != NULL &&
- options->infilename == NULL) {
- fprintf(stderr,
- "Error: input from stdin with dep generation would fail\n");
- return 3;
- }
-
- /* open dependancy file */
- if (options->depfilename != NULL) {
- options->depfilehandle = fopen(options->depfilename, "w");
- if (options->depfilehandle == NULL) {
- fprintf(stderr,
- "Error: unable to open dep file\n");
- return 4;
- }
- fprintf(options->depfilehandle,
- "%s %s :", options->depfilename,
- options->outfilename);
- }
-
- /* parse input and generate dependancy */
- res = genbind_parsefile(options->infilename, &genbind_root);
- if (res != 0) {
- fprintf(stderr, "Error: parse failed with code %d\n", res);
- return res;
- }
-
- /* dependancy generation complete */
- if (options->depfilehandle != NULL) {
- fputc('\n', options->depfilehandle);
- fclose(options->depfilehandle);
- }
-
-
- /* open output file */
- if (options->outfilename == NULL) {
- options->outfilehandle = stdout;
- } else {
- options->outfilehandle = fopen(options->outfilename, "w");
- }
- if (options->outfilehandle == NULL) {
- fprintf(stderr, "Error opening source output %s: %s\n",
- options->outfilename,
- strerror(errno));
- return 5;
- }
-
- /* open output header file if required */
- if (options->hdrfilename != NULL) {
- options->hdrfilehandle = fopen(options->hdrfilename, "w");
- if (options->hdrfilehandle == NULL) {
- fprintf(stderr, "Error opening header output %s: %s\n",
- options->hdrfilename,
- strerror(errno));
- /* close and unlink output file */
- fclose(options->outfilehandle);
- if (options->outfilename != NULL) {
- unlink(options->outfilename);
- }
- return 6;
- }
- } else {
- options->hdrfilehandle = NULL;
- }
-
- /* dump the AST */
- if (options->verbose) {
- genbind_ast_dump(genbind_root, 0);
- }
-
- /* generate output for each binding */
- res = genbind_node_foreach_type(genbind_root,
- GENBIND_NODE_TYPE_BINDING,
- generate_binding,
- genbind_root);
- if (res != 0) {
- fprintf(stderr, "Error: output failed with code %d\n", res);
- if (options->outfilename != NULL) {
- unlink(options->outfilename);
- }
- if (options->hdrfilename != NULL) {
- unlink(options->hdrfilename);
- }
- return res;
- }
-
-
- return 0;
+ int res;
+ struct genbind_node *genbind_root;
+ enum bindingtype_e bindingtype;
+
+ options = process_cmdline(argc, argv);
+ if (options == NULL) {
+ return 1; /* bad commandline */
+ }
+
+ /* parse input and generate dependancy */
+ res = genbind_parsefile(options->infilename, &genbind_root);
+ if (res != 0) {
+ fprintf(stderr, "Error: parse failed with code %d\n", res);
+ return res;
+ }
+
+ /* dump the AST */
+ genbind_dump_ast(genbind_root);
+
+ /* get bindingtype */
+ bindingtype = genbind_get_type(genbind_root);
+ if (bindingtype == BINDINGTYPE_UNKNOWN) {
+ return 3;
+ }
+
+#if 0
+ genbind_load_idl(genbind_root);
+
+ /* generate output for each binding */
+ res = genbind_node_foreach_type(genbind_root,
+ GENBIND_NODE_TYPE_BINDING,
+ generate_binding,
+ genbind_root);
+ if (res != 0) {
+ fprintf(stderr, "Error: output failed with code %d\n", res);
+ if (options->outfilename != NULL) {
+ unlink(options->outfilename);
+ }
+ if (options->hdrfilename != NULL) {
+ unlink(options->hdrfilename);
+ }
+ return res;
+ }
+
+#endif
+ return 0;
}