From 640ed1da81d909bb3c2f01a481e7e8d3336f336c Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 9 Sep 2012 12:23:28 +0100 Subject: split out output geenration --- src/Makefile | 2 +- src/genjsbind-ast.c | 67 ++++++++++++++++---------------------------------- src/genjsbind-ast.h | 17 +++++++++---- src/genjsbind-parser.y | 10 ++++---- src/genjsbind.c | 5 ++-- src/jsapi-libdom.c | 42 +++++++++++++++++++++++++++++++ src/jsapi-libdom.h | 1 + 7 files changed, 85 insertions(+), 59 deletions(-) create mode 100644 src/jsapi-libdom.c create mode 100644 src/jsapi-libdom.h (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 1116400..530dd4b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CFLAGS := $(CFLAGS) -I$(BUILDDIR) -Isrc/ -g # Sources in this directory -DIR_SOURCES := genjsbind.c webidl-ast.c genjsbind-ast.c +DIR_SOURCES := genjsbind.c webidl-ast.c genjsbind-ast.c jsapi-libdom.c SOURCES := $(SOURCES) $(BUILDDIR)/genjsbind-parser.c $(BUILDDIR)/genjsbind-lexer.c $(BUILDDIR)/webidl-parser.c $(BUILDDIR)/webidl-lexer.c diff --git a/src/genjsbind-ast.c b/src/genjsbind-ast.c index ccadc7f..a2e17df 100644 --- a/src/genjsbind-ast.c +++ b/src/genjsbind-ast.c @@ -31,56 +31,58 @@ extern int genjsbind_parse(void); #define HDR_COMMENT_PREABLE "Generated by nsgenjsapi"HDR_COMMENT_SEP /* current state */ -static char *hdr_comments = NULL; -static char *preamble = NULL; -static char *ifname; +struct genbind_ast *genbind_ast; -int genjsbind_header_comment(char *comment) +int genbind_header_comment(char *comment) { char *fullstr; int fulllen; - fulllen = strlen(hdr_comments) + strlen(comment) + HDR_COMMENT_SEP_LEN + 1; + fulllen = strlen(genbind_ast->hdr_comments) + + strlen(comment) + HDR_COMMENT_SEP_LEN + 1; fullstr = malloc(fulllen); - snprintf(fullstr, fulllen, "%s"HDR_COMMENT_SEP"%s", hdr_comments , comment); - free(hdr_comments); + snprintf(fullstr, fulllen, "%s"HDR_COMMENT_SEP"%s", genbind_ast->hdr_comments , comment); + free(genbind_ast->hdr_comments); free(comment); - hdr_comments = fullstr; + genbind_ast->hdr_comments = fullstr; return 0; } -int genjsbind_preamble(char *ccode) +int genbind_preamble(char *ccode) { char *fullstr; int fulllen; - fulllen = strlen(preamble) + strlen(ccode) + 1; + fulllen = strlen(genbind_ast->preamble) + strlen(ccode) + 1; fullstr = malloc(fulllen); - snprintf(fullstr, fulllen, "%s%s", preamble , ccode); - free(preamble); + snprintf(fullstr, fulllen, "%s%s", genbind_ast->preamble , ccode); + free(genbind_ast->preamble); free(ccode); - preamble = fullstr; + genbind_ast->preamble = fullstr; return 0; } -int genjsbind_interface(char *interface) +int genbind_interface(char *interface) { - ifname = interface; + genbind_ast->ifname = interface; return 0; } static void init_state(void) { - /* initialise root node */ + /* allocate state */ + genbind_ast = calloc(1, sizeof(struct genbind_ast)); + + /* initialise root IDL node */ webidl_root = webidl_new_node(WEBIDL_NODE_TYPE_ROOT); /* set default comment header text */ - hdr_comments = strdup(HDR_COMMENT_PREABLE); + genbind_ast->hdr_comments = strdup(HDR_COMMENT_PREABLE); - preamble = strdup(""); + genbind_ast->preamble = strdup(""); } -int genjsbind_parsefile(char *infilename) +int genbind_parsefile(char *infilename) { FILE *infile; @@ -121,31 +123,4 @@ int genjsbind_parsefile(char *infilename) } -int genjsbind_output(char *outfilename) -{ - FILE *outfile = NULL; - /* open output file */ - if (outfilename == NULL) { - outfile = stdout; - } else { - outfile = fopen(outfilename, "w"); - } - - if (!outfile) { - fprintf(stderr, "Error opening output %s: %s\n", - outfilename, - strerror(errno)); - return 4; - } - - fprintf(outfile, "/* %s\n */\n\n", hdr_comments); - - fprintf(outfile, "%s", preamble); - - fprintf(outfile, "/* interface %s */\n\n", ifname); - - fclose(outfile); - - return 0; -} diff --git a/src/genjsbind-ast.h b/src/genjsbind-ast.h index d94eda6..0f7c2d2 100644 --- a/src/genjsbind-ast.h +++ b/src/genjsbind-ast.h @@ -9,10 +9,17 @@ #ifndef genjsbind_genjsbind_ast_h #define genjsbind_genjsbind_ast_h -int genjsbind_parsefile(char *infilename); -int genjsbind_output(char *outfilename); -int genjsbind_header_comment(char *); -int genjsbind_interface(char *); -int genjsbind_preamble(char *ccode); +int genbind_parsefile(char *infilename); +int genbind_header_comment(char *); +int genbind_interface(char *); +int genbind_preamble(char *ccode); + +struct genbind_ast { + char *hdr_comments; + char *preamble; + char *ifname; +}; + +extern struct genbind_ast *genbind_ast; #endif diff --git a/src/genjsbind-parser.y b/src/genjsbind-parser.y index 9ed4f21..0e68a47 100644 --- a/src/genjsbind-parser.y +++ b/src/genjsbind-parser.y @@ -104,12 +104,12 @@ HdrStrings : TOK_STRING_LITERAL { - genjsbind_header_comment($1); + genbind_header_comment($1); } | HdrStrings TOK_STRING_LITERAL { - genjsbind_header_comment($2); + genbind_header_comment($2); } ; @@ -122,12 +122,12 @@ CBlock : TOK_CCODE_LITERAL { - genjsbind_preamble($1); + genbind_preamble($1); } | CBlock TOK_CCODE_LITERAL { - genjsbind_preamble($2); + genbind_preamble($2); } ; @@ -171,7 +171,7 @@ Interface : TOK_INTERFACE TOK_IDENTIFIER ';' { - genjsbind_interface($2); + genbind_interface($2); } ; diff --git a/src/genjsbind.c b/src/genjsbind.c index dd5b242..21bf8b1 100644 --- a/src/genjsbind.c +++ b/src/genjsbind.c @@ -15,6 +15,7 @@ #include #include "genjsbind-ast.h" +#include "jsapi-libdom.h" #include "options.h" struct options *options; @@ -85,13 +86,13 @@ int main(int argc, char **argv) return 2; } - res = genjsbind_parsefile(options->infilename); + res = genbind_parsefile(options->infilename); if (res != 0) { fprintf(stderr, "Error: parse failed with code %d\n", res); return res; } - res = genjsbind_output(options->outfilename); + res = jsapi_libdom_output(options->outfilename); if (res != 0) { fprintf(stderr, "Error: output failed with code %d\n", res); unlink(options->outfilename); diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c new file mode 100644 index 0000000..02033e1 --- /dev/null +++ b/src/jsapi-libdom.c @@ -0,0 +1,42 @@ +/* binding output generator for jsapi(spidermonkey) to libdom + * + * This file is part of nsgenjsbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +#include +#include +#include + +#include "genjsbind-ast.h" +#include "jsapi-libdom.h" + +int jsapi_libdom_output(char *outfilename) +{ + FILE *outfile = NULL; + /* open output file */ + if (outfilename == NULL) { + outfile = stdout; + } else { + outfile = fopen(outfilename, "w"); + } + + if (!outfile) { + fprintf(stderr, "Error opening output %s: %s\n", + outfilename, + strerror(errno)); + return 4; + } + + fprintf(outfile, "/* %s\n */\n\n", genbind_ast->hdr_comments); + + fprintf(outfile, "%s", genbind_ast->preamble); + + fprintf(outfile, "/* interface %s */\n\n", genbind_ast->ifname); + + fclose(outfile); + + return 0; +} diff --git a/src/jsapi-libdom.h b/src/jsapi-libdom.h new file mode 100644 index 0000000..6ee8db7 --- /dev/null +++ b/src/jsapi-libdom.h @@ -0,0 +1 @@ +int jsapi_libdom_output(char *outfile); -- cgit v1.2.3