From 3b19d1d1ad88ba63ae821b2aedd4005466ed52fa Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 2 Jan 2013 16:55:45 +0000 Subject: Add epilogue output Add header guard generation --- src/jsapi-libdom.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/jsapi-libdom.h | 2 ++ src/nsgenbind-ast.h | 2 ++ 3 files changed, 69 insertions(+) diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 4fbe67b..39225ea 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "options.h" #include "nsgenbind-ast.h" @@ -68,6 +69,15 @@ static int webidl_preamble_cb(struct genbind_node *node, void *ctx) return 0; } +static int webidl_epilogue_cb(struct genbind_node *node, void *ctx) +{ + struct binding *binding = ctx; + + fprintf(binding->outfile, "%s", genbind_node_gettext(node)); + + return 0; +} + static int webidl_hdrcomments_cb(struct genbind_node *node, void *ctx) { @@ -597,6 +607,42 @@ output_preamble(struct binding *binding) fprintf(binding->outfile,"\n\n"); + if (binding->hdrfile) { + binding->outfile = binding->hdrfile; + + fprintf(binding->outfile, + "#ifndef _%s_\n" + "#define _%s_\n\n", + binding->hdrguard, + binding->hdrguard); + + binding->outfile = binding->srcfile; + } + + + return 0; +} + +static int +output_epilogue(struct binding *binding) +{ + genbind_node_for_each_type(binding->gb_ast, + GENBIND_NODE_TYPE_EPILOGUE, + webidl_epilogue_cb, + binding); + + fprintf(binding->outfile,"\n\n"); + + if (binding->hdrfile) { + binding->outfile = binding->hdrfile; + + fprintf(binding->outfile, + "\n\n#endif /* _%s_ */\n", + binding->hdrguard); + + binding->outfile = binding->srcfile; + } + return 0; } @@ -679,6 +725,7 @@ binding_new(char *outfilename, struct genbind_node *interface_node; FILE *outfile = NULL; /* output source file */ FILE *hdrfile = NULL; /* output header file */ + char *hdrguard = NULL; struct webidl_node *webidl_ast = NULL; int res; @@ -730,6 +777,9 @@ binding_new(char *outfilename, /* output header file if required */ if (hdrfilename != NULL) { + int guardlen; + int pos; + hdrfile = fopen(hdrfilename, "w"); if (hdrfile == NULL) { fprintf(stderr, "Error opening header output %s: %s\n", @@ -738,6 +788,15 @@ binding_new(char *outfilename, fclose(outfile); return NULL; } + guardlen = strlen(hdrfilename); + hdrguard = calloc(1, guardlen + 1); + for (pos = 0; pos < guardlen; pos++) { + if (isalpha(hdrfilename[pos])) { + hdrguard[pos] = toupper(hdrfilename[pos]); + } else { + hdrguard[pos] = '_'; + } + } } nb = calloc(1, sizeof(struct binding)); @@ -749,6 +808,7 @@ binding_new(char *outfilename, nb->outfile = outfile; nb->srcfile = outfile; nb->hdrfile = hdrfile; + nb->hdrguard = hdrguard; nb->has_private = binding_has_private(binding_list); nb->has_global = binding_has_global(nb); nb->binding_list = binding_list; @@ -849,6 +909,11 @@ jsapi_libdom_output(char *outfilename, return 150; } + res = output_epilogue(binding); + if (res) { + return 160; + } + fclose(binding->outfile); return 0; diff --git a/src/jsapi-libdom.h b/src/jsapi-libdom.h index bdc941d..a263efa 100644 --- a/src/jsapi-libdom.h +++ b/src/jsapi-libdom.h @@ -24,6 +24,8 @@ struct binding { struct genbind_node *finalise; /* binding api finalise node or NULL */ struct genbind_node *mark; /* binding api mark node or NULL */ + const char *hdrguard; /* header file guard name */ + FILE *outfile ; /* file handle output should be written to, * allows reuse of callback routines to output * to headers and source files diff --git a/src/nsgenbind-ast.h b/src/nsgenbind-ast.h index 6368519..4e2ffee 100644 --- a/src/nsgenbind-ast.h +++ b/src/nsgenbind-ast.h @@ -20,6 +20,8 @@ enum genbind_node_type { GENBIND_NODE_TYPE_HDRCOMMENT, GENBIND_NODE_TYPE_STRING, GENBIND_NODE_TYPE_PREAMBLE, + GENBIND_NODE_TYPE_PROLOGUE, + GENBIND_NODE_TYPE_EPILOGUE, GENBIND_NODE_TYPE_BINDING, GENBIND_NODE_TYPE_BINDING_PRIVATE, GENBIND_NODE_TYPE_BINDING_INTERNAL, -- cgit v1.2.3