77%{
88
99#include < cmetrics/cmt_decode_prometheus.h>
10- #include < stdio.h>
1110
1211#define STRBUF_RET \
1312 yylval->str = context->strbuf; \
1413 context->strbuf = NULL
1514
16- static void set_allocation_error (struct cmt_decode_prometheus_context *context)
17- {
18- context->errcode = CMT_DECODE_PROMETHEUS_ALLOCATION_ERROR ;
19-
20- if (context->opts .errbuf != NULL && context->opts .errbuf_size > 0 ) {
21- snprintf (context->opts .errbuf ,
22- context->opts .errbuf_size - 1 ,
23- " memory allocation failed" );
24- }
25- }
26-
27- static int reset_strbuf (struct cmt_decode_prometheus_context *context)
28- {
29- if (context->strbuf != NULL ) {
30- cfl_sds_destroy (context->strbuf );
31- }
32-
33- context->strbuf = cfl_sds_create_size (256 );
34- if (context->strbuf == NULL ) {
35- set_allocation_error (context);
36-
37- return -1 ;
38- }
39-
40- return 0 ;
41- }
42-
43- static int append_strbuf (struct cmt_decode_prometheus_context *context,
44- const char *text, int length)
45- {
46- cfl_sds_t result;
47-
48- result = cfl_sds_cat (context->strbuf , text, length);
49- if (result == NULL ) {
50- set_allocation_error (context);
51-
52- return -1 ;
53- }
54-
55- context->strbuf = result;
56-
57- return 0 ;
58- }
59-
60- #define STRBUF_CREATE () \
61- do { \
62- if (reset_strbuf (context) != 0 ) { \
63- return 0 ; \
64- } \
65- } while (0 )
66-
67- #define STRBUF_APPEND (text, length ) \
68- do { \
69- if (append_strbuf (context, (text), (length)) != 0 ) { \
70- return 0 ; \
71- } \
72- } while (0 )
73-
74- #define SET_STR_TOKEN () \
75- do { \
76- yylval->str = cfl_sds_create (yytext); \
77- if (yylval->str == NULL ) { \
78- set_allocation_error (context); \
79- return 0 ; \
80- } \
81- } while (0 )
82-
8315%}
8416
8517/* here we define some states that allow us to create rules only
@@ -142,7 +74,7 @@ static int append_strbuf(struct cmt_decode_prometheus_context *context,
14274
14375<HELPTAG ,TYPETAG >[^ \t ]+ {
14476 // The next token will be the metric name
145- SET_STR_TOKEN ( );
77+ yylval-> str = cfl_sds_create (yytext );
14678 return YYSTATE == HELPTAG ? HELP : TYPE ;
14779}
14880
@@ -154,7 +86,7 @@ static int append_strbuf(struct cmt_decode_prometheus_context *context,
15486 // separate start condition for this to handle "\\" and "\n" escapes
15587 // more easily.
15688 BEGIN (INHELPTAG );
157- STRBUF_CREATE ( );
89+ context-> strbuf = cfl_sds_create_size ( 256 );
15890 }
15991 else {
16092 // For TYPETAG we enter INTYPETAG start condition to check only valid
@@ -175,17 +107,17 @@ static int append_strbuf(struct cmt_decode_prometheus_context *context,
175107
176108<INHELPTAG >\\ n {
177109 // Process linefeed escape sequence
178- STRBUF_APPEND ( " \n " , 1 );
110+ context-> strbuf = cfl_sds_cat (context-> strbuf , " \n " , 1 );
179111}
180112
181113<INHELPTAG >\\\\ {
182114 // Process backslack escape sequence
183- STRBUF_APPEND ( " \\ " , 1 );
115+ context-> strbuf = cfl_sds_cat (context-> strbuf , " \\ " , 1 );
184116}
185117
186118<INHELPTAG >[^ \r\n\\ ]+ {
187119 // Put everything that is not a backslash or a line feed into strbuf
188- STRBUF_APPEND ( yytext, yyleng);
120+ context-> strbuf = cfl_sds_cat (context-> strbuf , yytext, yyleng);
189121}
190122
191123<INTYPETAG >counter {
@@ -214,23 +146,26 @@ static int append_strbuf(struct cmt_decode_prometheus_context *context,
214146
215147[" ] {
216148 BEGIN (INQUOTE );
217- STRBUF_CREATE ();
149+ if (context->strbuf != NULL ) {
150+ cfl_sds_destroy (context->strbuf );
151+ }
152+ context->strbuf = cfl_sds_create_size (256 );
218153}
219154
220155<INQUOTE >[\\ ][" ] {
221- STRBUF_APPEND ( " \" " , 1 );
156+ context-> strbuf = cfl_sds_cat (context-> strbuf , " \" " , 1 );
222157}
223158
224159<INQUOTE >\\ n {
225- STRBUF_APPEND ( " \n " , 1 );
160+ context-> strbuf = cfl_sds_cat (context-> strbuf , " \n " , 1 );
226161}
227162
228163<INQUOTE >\\\\ {
229- STRBUF_APPEND ( " \\ " , 1 );
164+ context-> strbuf = cfl_sds_cat (context-> strbuf , " \\ " , 1 );
230165}
231166
232167<INQUOTE >[^ \r\n\\ " ]+ {
233- STRBUF_APPEND ( yytext, yyleng);
168+ context-> strbuf = cfl_sds_cat (context-> strbuf , yytext, yyleng);
234169}
235170
236171<INQUOTE >[" ] {
@@ -245,7 +180,7 @@ static int append_strbuf(struct cmt_decode_prometheus_context *context,
245180}
246181
247182[a -zA -Z_ ][a -zA -Z_0 -9 ]* {
248- SET_STR_TOKEN ( );
183+ yylval-> str = cfl_sds_create (yytext );
249184 return IDENTIFIER ;
250185}
251186
0 commit comments