diff --git a/lib/src/ovis_json/ovis_json.h b/lib/src/ovis_json/ovis_json.h index d0e3b59ec..e77574a29 100644 --- a/lib/src/ovis_json/ovis_json.h +++ b/lib/src/ovis_json/ovis_json.h @@ -164,5 +164,4 @@ extern jbuf_t jbuf_append_str(jbuf_t jb, const char *fmt, ...); extern jbuf_t jbuf_append_va(jbuf_t jb, const char *fmt, va_list ap); extern void jbuf_free(jbuf_t jb); -#define YYSTYPE json_entity_t #endif diff --git a/lib/src/ovis_json/ovis_json_lexer.l b/lib/src/ovis_json/ovis_json_lexer.l index 230f6c4cb..9780e2885 100644 --- a/lib/src/ovis_json/ovis_json_lexer.l +++ b/lib/src/ovis_json/ovis_json_lexer.l @@ -5,6 +5,8 @@ #include #include "ovis_json_parser.h" +#define YYSTYPE json_entity_t + struct yyguts_t; void update_yylloc(YYLTYPE *, struct yyguts_t *); void init_yylloc(YYLTYPE *, struct yyguts_t *); @@ -125,3 +127,23 @@ void init_yylloc(YYLTYPE *yyl, struct yyguts_t *yyg) yyl->first_column = yyl->last_column = 0; yylineno = 0; } + +int json_parse_buffer(json_parser_t p, char *buf, size_t buf_len, json_entity_t *pentity) +{ + int rc; + *pentity = NULL; + char *nbuf = malloc(buf_len + 2); + if (!nbuf) + return ENOMEM; + memcpy(nbuf, buf, buf_len); + nbuf[buf_len] = YY_END_OF_BUFFER_CHAR; + nbuf[buf_len+1] = YY_END_OF_BUFFER_CHAR; + if (NULL == yy_scan_buffer(buf, buf_len + 2, p->scanner)) { + rc = EINVAL; + goto out; + } + rc = yyparse(p, nbuf, buf_len + 2, pentity); +out: + free(nbuf); + return rc; +} diff --git a/lib/src/ovis_json/ovis_json_parser.y b/lib/src/ovis_json/ovis_json_parser.y index 026740697..b76872735 100644 --- a/lib/src/ovis_json/ovis_json_parser.y +++ b/lib/src/ovis_json/ovis_json_parser.y @@ -5,7 +5,9 @@ #include #include #include "ovis_json.h" + #define YYLTYPE struct json_loc_s +#define YYSTYPE json_entity_t void yyerror(YYLTYPE *yylloc, json_parser_t parser, char *input, size_t input_len, json_entity_t *pentity, const char *str) @@ -127,7 +129,7 @@ item_list: /* empty */ { $$ = new_list_val(); } ; %% -#include + json_parser_t json_parser_new(size_t user_data) { json_parser_t p = calloc(1, sizeof *p + user_data); if (p) @@ -141,17 +143,3 @@ void json_parser_free(json_parser_t parser) free(parser); } -int json_parse_buffer(json_parser_t p, char *buf, size_t buf_len, json_entity_t *pentity) -{ - int rc; - *pentity = NULL; - /* NB: Force two 0x00 on the end of the buffer for the scanner. If - * the user didn't allocate enough memory, this could corrupt - * memory */ - buf[buf_len] = 0x00; - buf[buf_len+1] = 0x00; - assert(NULL != yy_scan_buffer(buf, buf_len+2, p->scanner)); - rc = yyparse(p, buf, buf_len, pentity); - return rc; -} -