X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=vppapigen%2Flex.c;h=e807d46b9a0ece7ce8d8def8e4004c557964ed5c;hb=c4d1cf76072b54f630b0263cd86e01df82249568;hp=f1d49a877e8abe2c9a6e64081432747e4e99fb0a;hpb=6855f6cdfee8c479f1e0ae440ce87a91ff41a708;p=vpp.git diff --git a/vppapigen/lex.c b/vppapigen/lex.c index f1d49a877e8..e807d46b9a0 100644 --- a/vppapigen/lex.c +++ b/vppapigen/lex.c @@ -28,8 +28,7 @@ #include "node.h" #include "gram.h" -FILE *ifp, *ofp, *javafp, *jnifp, *pythonfp; -char *java_class = "vppApi"; +FILE *ifp, *ofp, *pythonfp, *jsonfp; char *vlib_app_name = "vpp"; int dump_tree; time_t starttime; @@ -37,6 +36,7 @@ char *input_filename; char *current_filename; int current_filename_allocated; unsigned long input_crc; +unsigned long message_crc; int yydebug; /* @@ -258,9 +258,8 @@ int main (int argc, char **argv) { int curarg = 1; char *ofile=0; - char *jofile=0; - char *jnifile=0; char *pythonfile=0; + char *jsonfile=0; char *show_name=0; while (curarg < argc) { @@ -331,53 +330,44 @@ int main (int argc, char **argv) } continue; } - if (!strncmp (argv [curarg], "--java", 4)) { + if (!strncmp (argv [curarg], "--python", 8)) { curarg++; if (curarg < argc) { - javafp = fopen (argv[curarg], "w"); - if (javafp == NULL) { - fprintf (stderr, "Couldn't open java output file %s\n", + if (!strcmp(argv[curarg], "-")) { + pythonfp = stdout; + } else { + pythonfp = fopen(argv[curarg], "w"); + pythonfile = argv[curarg]; + } + if (pythonfp == NULL) { + fprintf (stderr, "Couldn't open python output file %s\n", argv[curarg]); exit (1); } - jofile = argv[curarg]; curarg++; } else { - fprintf(stderr, "Missing filename after --java\n"); + fprintf(stderr, "Missing filename after --python\n"); exit(1); } continue; } - if (!strncmp (argv [curarg], "--jni", 4)) { + if (!strncmp (argv [curarg], "--json", 6)) { curarg++; if (curarg < argc) { - jnifp = fopen (argv[curarg], "w"); - if (jnifp == NULL) { - fprintf (stderr, "Couldn't open jni output file %s\n", - argv[curarg]); - exit (1); - } - jnifile = argv[curarg]; - curarg++; - } else { - fprintf(stderr, "Missing filename after --jni\n"); - exit(1); - } - continue; - } - if (!strncmp (argv [curarg], "--python", 8)) { - curarg++; - if (curarg < argc) { - pythonfp = fopen (argv[curarg], "w"); - if (pythonfp == NULL) { - fprintf (stderr, "Couldn't open python output file %s\n", + if (!strcmp(argv[curarg], "-")) { + jsonfp = stdout; + } else { + jsonfp = fopen(argv[curarg], "w"); + jsonfile = argv[curarg]; + } + if (jsonfp == NULL) { + fprintf (stderr, "Couldn't open JSON output file %s\n", argv[curarg]); exit (1); } - pythonfile = argv[curarg]; curarg++; } else { - fprintf(stderr, "Missing filename after --python\n"); + fprintf(stderr, "Missing filename after --json\n"); exit(1); } continue; @@ -393,17 +383,6 @@ int main (int argc, char **argv) } continue; } - if (!strncmp (argv [curarg], "--class", 3)) { - curarg++; - if (curarg < argc) { - java_class = argv[curarg]; - curarg++; - } else { - fprintf(stderr, "Missing class name after --class\n"); - exit(1); - } - continue; - } usage(argv[0]); exit (1); @@ -411,15 +390,12 @@ int main (int argc, char **argv) if (ofp == NULL) { ofile = 0; } - if (javafp == NULL) { - jofile = 0; - } - if (jnifp == NULL) { - jnifile = 0; - } if (pythonfp == NULL) { pythonfile = 0; } + if (jsonfp == NULL) { + jsonfile = 0; + } if (ifp == NULL) { fprintf(stderr, "No input file specified...\n"); exit(1); @@ -437,39 +413,31 @@ int main (int argc, char **argv) printf ("Output written to %s\n", ofile); fclose (ofp); } - if (jofile) { - printf ("Java class defn written to %s\n", jofile); - fclose (javafp); - } - if (jnifile) { - printf ("Java native bindings written to %s\n", jnifile); - fclose (jnifp); - } if (pythonfile) { printf ("Python bindings written to %s\n", pythonfile); fclose (pythonfp); } + if (jsonfile) { + printf ("JSON bindings written to %s\n", jsonfile); + fclose (jsonfp); + } } else { fclose (ifp); - fclose (ofp); + if (ofp) + fclose (ofp); if (ofile) { printf ("Removing %s\n", ofile); unlink (ofile); } - fclose (javafp); - if (jofile) { - printf ("Removing %s\n", jofile); - unlink (jofile); - } - if (jnifile) { - printf ("Removing %s\n", jnifile); - unlink (jnifile); - } if (pythonfile) { printf ("Removing %s\n", pythonfile); unlink (pythonfile); } + if (jsonfile) { + printf ("Removing %s\n", jsonfile); + unlink (jsonfile); + } exit (1); } exit (0); @@ -481,7 +449,8 @@ int main (int argc, char **argv) static void usage (char *progname) { fprintf (stderr, - "usage: %s --input [--output ] [--python ]\n%s", + "usage: %s --input [--output ] " + "[--json ] [--python ]\n%s", progname, " [--yydebug] [--dump-tree]\n"); exit (1); @@ -497,6 +466,12 @@ void yyerror (char *s) static char namebuf [MAXNAME]; +static inline char +getc_char (FILE *ifp) +{ + return ((char)(getc(ifp) & 0x7f)); +} + /* * yylex (well, yylex_1: The real yylex below does crc-hackery) */ @@ -504,7 +479,6 @@ static int yylex_1 (void) { int nameidx=0; char c; - int at_bol=1; enum { LP_INITIAL_WHITESPACE, LP_LINE_NUMBER, LP_PRE_FILENAME_WHITESPACE, LP_FILENAME, LP_POST_FILENAME, @@ -517,23 +491,16 @@ static int yylex_1 (void) * START state -- looking for something interesting */ case START_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); switch (c) { case '\n': the_lexer_linenumber++; - at_bol=1; goto again; case '#': - if (!at_bol) { - fprintf (stderr, "unknown token /%c at line %d\n", - c, the_lexer_linenumber); - return (BARF); - } - the_lexer_state = LINE_PRAGMA_STATE; lp_substate = LP_INITIAL_WHITESPACE; goto again; @@ -578,7 +545,7 @@ static int yylex_1 (void) goto again; case '/': - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); @@ -595,7 +562,7 @@ static int yylex_1 (void) } case '\\': - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); @@ -623,7 +590,7 @@ static int yylex_1 (void) * NAME state -- eat the rest of a name */ case NAME_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); @@ -644,7 +611,7 @@ static int yylex_1 (void) * NUMBER state -- eat the rest of a number */ case NUMBER_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); @@ -666,11 +633,11 @@ static int yylex_1 (void) * C_COMMENT state -- eat a peach */ case C_COMMENT_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); if (c == '*') { - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); if (c == '/') { @@ -687,7 +654,7 @@ static int yylex_1 (void) */ case CPP_COMMENT_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); if (c == '\n') { @@ -698,12 +665,12 @@ static int yylex_1 (void) goto again; case STRING_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); switch (c) { case '\\': - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); namebuf[nameidx++] = c; @@ -729,12 +696,12 @@ static int yylex_1 (void) break; case HELPER_STATE: - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); switch (c) { case '\\': - c = getc (ifp); + c = getc_char (ifp); if (feof (ifp)) return (EOF); namebuf[nameidx] = c; @@ -774,7 +741,7 @@ static int yylex_1 (void) switch (lp_substate) { case LP_INITIAL_WHITESPACE: /* no number seen yet */ - c = getc(ifp); + c = getc_char(ifp); if (feof(ifp)) return(EOF); if (c >= '0' && c <= '9') { @@ -790,7 +757,7 @@ static int yylex_1 (void) goto again; case LP_LINE_NUMBER: /* eating linenumber */ - c = getc(ifp); + c = getc_char(ifp); if (feof(ifp)) return(EOF); if (c >= '0' && c <= '9') { @@ -807,7 +774,7 @@ static int yylex_1 (void) goto again; case LP_PRE_FILENAME_WHITESPACE: /* awaiting filename */ - c = getc(ifp); + c = getc_char(ifp); if (feof(ifp)) return(EOF); @@ -824,7 +791,7 @@ static int yylex_1 (void) goto again; case LP_FILENAME: /* eating filename */ - c = getc(ifp); + c = getc_char(ifp); if (feof(ifp)) return(EOF); @@ -840,7 +807,7 @@ static int yylex_1 (void) case LP_POST_FILENAME: /* ignoring rest of line */ case LP_OTHER: - c = getc(ifp); + c = getc_char(ifp); if (feof(ifp)) return(EOF); @@ -860,7 +827,6 @@ static int yylex_1 (void) } lp_end_of_line: the_lexer_state = START_STATE; - at_bol = 1; nameidx = 0; } goto again; @@ -887,18 +853,18 @@ int yylex (void) */ unsigned long crc = input_crc; int node_type = yylex_1 (); + unsigned long crc2 = message_crc; + int use_helper_string = 0; + unsigned short code; switch (node_type) { case PRIMTYPE: case NAME: case NUMBER: case STRING: - case HELPER_STRING: { - /* We know these types accumulated token text into namebuf */ - /* HELPER_STRING may still contain C comments. Argh. */ - crc = crc_eliding_c_comments (namebuf, crc); + case HELPER_STRING: + use_helper_string = 1; break; - } /* Other node types have no "substate" */ /* This code is written in this curious fashion because we @@ -906,31 +872,25 @@ int yylex (void) * values a particular version of lex/bison assigned to various states. */ - /* case NAME: crc = CRC16 (crc, 257); break; */ - case RPAR: crc = CRC16 (crc, 258); break; - case LPAR: crc = CRC16 (crc, 259); break; - case SEMI: crc = CRC16 (crc, 260); break; - case LBRACK: crc = CRC16 (crc, 261); break; - case RBRACK: crc = CRC16 (crc, 262); break; - /* case NUMBER: crc = CRC16 (crc, 263); break; */ - /* case PRIMTYPE: crc = CRC16 (crc, 264); break; */ - case BARF: crc = CRC16 (crc, 265); break; - case TPACKED: crc = CRC16 (crc, 266); break; - case DEFINE: crc = CRC16 (crc, 267); break; - case LCURLY: crc = CRC16 (crc, 268); break; - case RCURLY: crc = CRC16 (crc, 269); break; - /* case STRING: crc = CRC16 (crc, 270); break; */ - case UNION: crc = CRC16 (crc, 271); break; - /* case HELPER_STRING: crc = CRC16 (crc, 272); break; */ - case COMMA: crc = CRC16 (crc, 273); break; - case NOVERSION: crc = CRC16 (crc, 274); break; - case MANUAL_PRINT: crc = CRC16 (crc, 275); break; - case MANUAL_ENDIAN: crc = CRC16 (crc, 276); break; - case MANUAL_JAVA: crc = CRC16 (crc, 277); break; - case TYPEONLY: crc = CRC16 (crc, 278); break; - case DONT_TRACE: crc = CRC16 (crc, 279); break; + case RPAR: code = 258; break; + case LPAR: code = 259; break; + case SEMI: code = 260; break; + case LBRACK: code = 261; break; + case RBRACK: code = 262; break; + case BARF: code = 265; break; + case TPACKED: code = 266; break; + case DEFINE: code = 267; break; + case LCURLY: code = 268; break; + case RCURLY: code = 269; break; + case UNION: code = 271; break; + case COMMA: code = 273; break; + case NOVERSION: code = 274; break; + case MANUAL_PRINT: code = 275; break; + case MANUAL_ENDIAN: code = 276; break; + case TYPEONLY: code = 278; break; + case DONT_TRACE: code = 279; break; - case EOF: crc = CRC16 (crc, ~0); break; /* hysterical compatibility */ + case EOF: code = ~0; break; /* hysterical compatibility */ default: fprintf(stderr, "yylex: node_type %d missing state CRC cookie\n", @@ -938,11 +898,23 @@ int yylex (void) exit(1); } + if (use_helper_string) + { + /* We know these types accumulated token text into namebuf */ + /* HELPER_STRING may still contain C comments. Argh. */ + crc = crc_eliding_c_comments (namebuf, crc); + crc2 = crc_eliding_c_comments (namebuf, crc2); + } else + { + crc = CRC16 (crc, code); + crc2 = CRC16 (crc2, code); + } + input_crc = crc; + message_crc = crc2; return (node_type); } - /* * name_check -- see if the name we just ate * matches a known keyword. If so, set yylval @@ -965,7 +937,6 @@ static struct keytab { {"i64", NODE_I64}, {"i8", NODE_I8}, {"manual_endian", NODE_MANUAL_ENDIAN}, - {"manual_java", NODE_MANUAL_JAVA}, {"manual_print", NODE_MANUAL_PRINT}, {"noversion", NODE_NOVERSION}, {"packed", NODE_PACKED}, @@ -1014,6 +985,7 @@ static int name_check (const char *s, YYSTYPE *token_value) return (TPACKED); case NODE_DEFINE: + message_crc = 0; *token_value = make_node(subclass_id); return(DEFINE); @@ -1025,10 +997,6 @@ static int name_check (const char *s, YYSTYPE *token_value) *token_value = (YYSTYPE) NODE_FLAG_MANUAL_ENDIAN; return (MANUAL_ENDIAN); - case NODE_MANUAL_JAVA: - *token_value = (YYSTYPE) NODE_FLAG_MANUAL_JAVA; - return (MANUAL_JAVA); - case NODE_TYPEONLY: *token_value = (YYSTYPE) NODE_FLAG_TYPEONLY; return(TYPEONLY); @@ -1063,6 +1031,11 @@ char *sxerox (const char *s) char *rv; rv = (char *) malloc (len+1); + if (rv == 0) { + fprintf(stderr, "Out of memory..."); + exit (1); + } + strcpy (rv, s); return (rv); }