syck_.c

filter: Statementstmt 77.7% 206 / 265 Branchbran 68.0% 49 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.6% 255 / 337 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« emitter.c ↑ Summary perl_syck.h »
1 /*
2 * syck.c
3 *
4 * $Author: why $
5 * $Date: 2005-01-01 10:06:25 +0800 (六, 01 1 2005) $
6 *
7 * Copyright (C) 2003 why the lucky stiff
8 */
9 #include <stdio.h>
10 #include <string.h>
11  
12 #include "syck.h"
13  
14 void syck_parser_pop_level( SyckParser * );
15  
16 /*
17 * Custom assert
18 */
19 void
20 0 syck_assert( char *file_name, unsigned line_num )
21 {
22 0 fflush( NULL );
23 0 fprintf( stderr, "\nAssertion failed: %s, line %u\n",
24 file_name, line_num );
25 0 fflush( stderr );
26 0 abort();
27 }
28  
29 /*
30 * Allocates and copies a string
31 */
32 char *
33 18748579 syck_strndup( char *buf, long len )
34 {
35 18748579 char *new = S_ALLOC_N( char, len + 1 );
36 18748579 S_MEMZERO( new, char, len + 1 );
37 18748579 S_MEMCPY( new, buf, char, len );
38 18748579 return new;
39 }
40  
41 /*
42 * Default FILE IO function
43 */
44 long
45 0 syck_io_file_read( char *buf, SyckIoFile *file, long max_size, long skip )
46 {
47 0 long len = 0;
48  
49 ASSERT( file != NULL );
50  
51 0 max_size -= skip;
52 0 len = fread( buf + skip, sizeof( char ), max_size, file->ptr );
53 0 len += skip;
54 0 buf[len] = '\0';
55  
56 0 return len;
57 }
58  
59 /*
60 * Default string IO function
61 */
62 long
63 248976 syck_io_str_read( char *buf, SyckIoStr *str, long max_size, long skip )
64 {
65 char *beg;
66 248976 long len = 0;
67  
68 ASSERT( str != NULL );
69 248976 beg = str->ptr;
70 248976 ▶ if ( max_size >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( max_size >= 0 ) 248976 0 248976
71 {
72 248976 max_size -= skip;
73 248976 ▶ if ( max_size <= 0 ) max_size = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ( max_size <= 0 ) max_size = 0; 0 248976 248976
74 248976 else str->ptr += max_size;
75  
76 248976 ▶ if ( str->ptr > str->end )
Branch: 1 branch
BranchTrueFalseTotal
if ( str->ptr > str->end ) 9976 239000 248976
77 {
78 9976 str->ptr = str->end;
79 }
80 }
81 else
82 {
83 /* Use exact string length */
84 0 ▶ while ( str->ptr < str->end ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( str->ptr < str->end ) { 0 0 0
85 0 ▶ if (*(str->ptr++) == '\n') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*(str->ptr++) == '\n') break; 0 0 0
86 }
87 }
88 248976 ▶ if ( beg < str->ptr )
Branch: 1 branch
BranchTrueFalseTotal
if ( beg < str->ptr ) 241225 7751 248976
89 {
90 241225 len = ( str->ptr - beg );
91 241225 S_MEMCPY( buf + skip, beg, char, len );
92 }
93 248976 len += skip;
94 248976 buf[len] = '\0';
95  
96 248976 return len;
97 }
98  
99 void
100 6911 syck_parser_reset_levels( SyckParser *p )
101 {
102 6922 ▶ while ( p->lvl_idx > 1 )
Branch: 1 branch
BranchTrueFalseTotal
while ( p->lvl_idx > 1 ) 11 6911 6922
103 {
104 11 syck_parser_pop_level( p );
105 }
106  
107 6911 ▶ if ( p->lvl_idx < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx < 1 ) 2225 4686 6911
108 {
109 2225 p->lvl_idx = 1;
110 2225 p->levels[0].spaces = -1;
111 2225 p->levels[0].ncount = 0;
112 2225 p->levels[0].domain = syck_strndup( "", 0 );
113 }
114 6911 p->levels[0].status = syck_lvl_header;
115 6911 }
116  
117 void
118 2225 syck_parser_reset_cursor( SyckParser *p )
119 {
120 2225 ▶ if ( p->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->buffer == NULL ) 2225 0 2225
121 {
122 2225 p->buffer = S_ALLOC_N( char, p->bufsize );
123 2225 S_MEMZERO( p->buffer, char, p->bufsize );
124 }
125 2225 p->buffer[0] = '\0';
126  
127 2225 p->cursor = NULL;
128 2225 p->lineptr = NULL;
129 2225 p->linectptr = NULL;
130 2225 p->token = NULL;
131 2225 p->toktmp = NULL;
132 2225 p->marker = NULL;
133 2225 p->limit = NULL;
134  
135 2225 p->root = 0;
136 2225 p->root_on_error = 0;
137 2225 p->linect = 0;
138 2225 p->eof = 0;
139 2225 p->last_token = 0;
140 2225 p->force_token = 0;
141 2225 }
142  
143 /*
144 * Value to return on a parse error
145 */
146 void
147 0 syck_parser_set_root_on_error( SyckParser *p, SYMID roer )
148 {
149 0 p->root_on_error = roer;
150 0 }
151  
152 /*
153 * Allocate the parser
154 */
155 SyckParser *
156 2225 syck_new_parser(void)
157 {
158 SyckParser *p;
159 2225 p = S_ALLOC( SyckParser );
160 2225 S_MEMZERO( p, SyckParser, 1 );
161 2225 p->lvl_capa = ALLOC_CT;
162 2225 p->levels = S_ALLOC_N( SyckLevel, p->lvl_capa );
163 2225 p->input_type = syck_yaml_utf8;
164 2225 p->io_type = syck_io_str;
165 2225 p->io.str = NULL;
166 2225 p->syms = NULL;
167 2225 p->anchors = NULL;
168 2225 p->bad_anchors = NULL;
169 2225 p->implicit_typing = 1;
170 2225 p->taguri_expansion = 0;
171 2225 p->bufsize = SYCK_BUFFERSIZE;
172 2225 p->buffer = NULL;
173 2225 p->lvl_idx = 0;
174 2225 syck_parser_reset_levels( p );
175 2225 return p;
176 }
177  
178 int
179 18724263 syck_add_sym( SyckParser *p, char *data )
180 {
181 18724263 SYMID id = 0;
182 18724263 ▶ if ( p->syms == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms == NULL ) 2214 18722049 18724263
183 {
184 2214 p->syms = st_init_numtable();
185 }
186 18724263 id = p->syms->num_entries + 1;
187 18724263 st_insert( p->syms, id, (st_data_t)data );
188 18724263 return id;
189 }
190  
191 int
192 18724282 syck_lookup_sym( SyckParser *p, SYMID id, char **data )
193 {
194 18724282 ▶ if ( p->syms == NULL ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms == NULL ) return 0; 0 18724282 18724282
195 18724282 return st_lookup( p->syms, id, (st_data_t *)data );
196 }
197  
198 enum st_retval
199 37 syck_st_free_nodes( st_data_t key, st_data_t value, st_data_t arg )
200 {
201 37 SyckNode *n = (SyckNode *)value;
202 37 ▶ if ( n != (void *)1 ) syck_free_node( n );
Branch: 1 branch
BranchTrueFalseTotal
if ( n != (void *)1 ) syck_free_node( n ); 37 0 37
203 37 return ST_CONTINUE;
204 }
205  
206 void
207 4686 syck_st_free( SyckParser *p )
208 {
209 /*
210 * Free the anchor tables
211 */
212 4686 ▶ if ( p->anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->anchors != NULL ) 22 4664 4686
213 {
214 22 st_foreach( p->anchors, syck_st_free_nodes, 0 );
215 22 st_free_table( p->anchors );
216 22 p->anchors = NULL;
217 }
218  
219 4686 ▶ if ( p->bad_anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->bad_anchors != NULL ) 6 4680 4686
220 {
221 6 st_foreach( p->bad_anchors, syck_st_free_nodes, 0 );
222 6 st_free_table( p->bad_anchors );
223 6 p->bad_anchors = NULL;
224 }
225 4686 }
226  
227 void
228 2225 syck_free_parser( SyckParser *p )
229 {
230 /*
231 * Free the adhoc symbol table
232 */
233 2225 ▶ if ( p->syms != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms != NULL ) 2214 11 2225
234 {
235 2214 st_free_table( p->syms );
236 2214 p->syms = NULL;
237 }
238  
239 /*
240 * Free tables, levels
241 */
242 2225 syck_st_free( p );
243 2225 syck_parser_reset_levels( p );
244 2225 S_FREE( p->levels[0].domain );
245 2225 S_FREE( p->levels );
246  
247 2225 ▶ if ( p->buffer != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->buffer != NULL ) 2225 0 2225
248 {
249 2225 S_FREE( p->buffer );
250 }
251 2225 free_any_io( p );
252 2225 S_FREE( p );
253 2225 }
254  
255 void
256 2225 syck_parser_handler( SyckParser *p, SyckNodeHandler hdlr )
257 {
258 ASSERT( p != NULL );
259 2225 p->handler = hdlr;
260 2225 }
261  
262 void
263 2225 syck_parser_implicit_typing( SyckParser *p, int flag )
264 {
265 2225 p->implicit_typing = ( flag == 0 ? 0 : 1 );
266 2225 }
267  
268 void
269 2225 syck_parser_taguri_expansion( SyckParser *p, int flag )
270 {
271 2225 p->taguri_expansion = ( flag == 0 ? 0 : 1 );
272 2225 }
273  
274 void
275 2225 syck_parser_error_handler( SyckParser *p, SyckErrorHandler hdlr )
276 {
277 ASSERT( p != NULL );
278 2225 p->error_handler = hdlr;
279 2225 }
280  
281 void
282 2225 syck_parser_bad_anchor_handler( SyckParser *p, SyckBadAnchorHandler hdlr )
283 {
284 ASSERT( p != NULL );
285 2225 p->bad_anchor_handler = hdlr;
286 2225 }
287  
288 void
289 0 syck_parser_set_input_type( SyckParser *p, enum syck_parser_input input_type )
290 {
291 ASSERT( p != NULL );
292 0 p->input_type = input_type;
293 0 }
294  
295 void
296 0 syck_parser_file( SyckParser *p, FILE *fp, SyckIoFileRead read )
297 {
298 ASSERT( p != NULL );
299 0 free_any_io( p );
300 0 syck_parser_reset_cursor( p );
301 0 p->io_type = syck_io_file;
302 0 p->io.file = S_ALLOC( SyckIoFile );
303 0 p->io.file->ptr = fp;
304 0 ▶ if ( read != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( read != NULL ) 0 0 0
305 {
306 0 p->io.file->read = read;
307 }
308 else
309 {
310 0 p->io.file->read = syck_io_file_read;
311 }
312 0 }
313  
314 void
315 2225 syck_parser_str( SyckParser *p, char *ptr, long len, SyckIoStrRead read )
316 {
317 ASSERT( p != NULL );
318 2225 free_any_io( p );
319 2225 syck_parser_reset_cursor( p );
320 2225 p->io_type = syck_io_str;
321 2225 p->io.str = S_ALLOC( SyckIoStr );
322 2225 p->io.str->beg = ptr;
323 2225 p->io.str->ptr = ptr;
324 2225 p->io.str->end = ptr + len;
325 2225 ▶ if ( read != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( read != NULL ) 0 2225 2225
326 {
327 0 p->io.str->read = read;
328 }
329 else
330 {
331 2225 p->io.str->read = syck_io_str_read;
332 }
333 2225 }
334  
335 void
336 2225 syck_parser_str_auto( SyckParser *p, char *ptr, SyckIoStrRead read )
337 {
338 2225 syck_parser_str( p, ptr, strlen( ptr ), read );
339 2225 }
340  
341 SyckLevel *
342 74909681 syck_parser_current_level( SyckParser *p )
343 {
344 74909681 return &p->levels[p->lvl_idx-1];
345 }
346  
347 void
348 2628 syck_parser_pop_level( SyckParser *p )
349 {
350 ASSERT( p != NULL );
351  
352 /* The root level should never be popped */
353 2628 ▶ if ( p->lvl_idx <= 1 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx <= 1 ) return; 0 2628 2628
354  
355 2628 p->lvl_idx -= 1;
356 2628 free( p->levels[p->lvl_idx].domain );
357 }
358  
359 void
360 2628 syck_parser_add_level( SyckParser *p, int len, enum syck_level_status status )
361 {
362 ASSERT( p != NULL );
363 2628 ▶ if ( p->lvl_idx + 1 > p->lvl_capa )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx + 1 > p->lvl_capa ) 1 2627 2628
364 {
365 1 p->lvl_capa += ALLOC_CT;
366 1 S_REALLOC_N( p->levels, SyckLevel, p->lvl_capa );
367 }
368  
369 ASSERT( len > p->levels[p->lvl_idx-1].spaces );
370 2628 p->levels[p->lvl_idx].spaces = len;
371 2628 p->levels[p->lvl_idx].ncount = 0;
372 2628 p->levels[p->lvl_idx].domain = syck_strndup( p->levels[p->lvl_idx-1].domain, strlen( p->levels[p->lvl_idx-1].domain ) );
373 2628 p->levels[p->lvl_idx].status = status;
374 2628 p->lvl_idx += 1;
375 2628 }
376  
377 void
378 4450 free_any_io( SyckParser *p )
379 {
380 ASSERT( p != NULL );
381 4450 switch ( p->io_type )
382 {
383 4450 case syck_io_str:
384 4450 ▶ if ( p->io.str != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->io.str != NULL ) 2225 2225 4450
385 {
386 2225 S_FREE( p->io.str );
387 2225 p->io.str = NULL;
388 }
389 4450 break;
390  
391 0 case syck_io_file:
392 0 ▶ if ( p->io.file != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->io.file != NULL ) 0 0 0
393 {
394 0 S_FREE( p->io.file );
395 0 p->io.file = NULL;
396 }
397 0 break;
398 }
399 4450 }
400  
401 long
402 248976 syck_move_tokens( SyckParser *p )
403 {
404 long count, skip;
405 ASSERT( p->buffer != NULL );
406  
407 248976 ▶ if ( p->token == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->token == NULL ) 2225 246751 248976
408 2225 return 0;
409  
410 246751 skip = p->limit - p->token;
411 246751 ▶ if ( skip < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( skip < 0 ) 0 246751 246751
412 0 return 0;
413  
414 246751 ▶ if ( ( count = p->token - p->buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( count = p->token - p->buffer ) ) 243179 3572 246751
415 {
416 243179 S_MEMMOVE( p->buffer, p->token, char, skip );
417 243179 p->token = p->buffer;
418 243179 p->marker -= count;
419 243179 p->cursor -= count;
420 243179 p->toktmp -= count;
421 243179 p->limit -= count;
422 243179 p->lineptr -= count;
423 243179 p->linectptr -= count;
424 }
425 246751 return skip;
426 }
427  
428 void
429 248976 syck_check_limit( SyckParser *p, long len )
430 {
431 248976 ▶ if ( p->cursor == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->cursor == NULL ) 2225 246751 248976
432 {
433 2225 p->cursor = p->buffer;
434 2225 p->lineptr = p->buffer;
435 2225 p->linectptr = p->buffer;
436 2225 p->marker = p->buffer;
437 }
438 248976 p->limit = p->buffer + len;
439 248976 }
440  
441 long
442 248976 syck_parser_read( SyckParser *p )
443 {
444 248976 long len = 0;
445 248976 long skip = 0;
446 ASSERT( p != NULL );
447 248976 switch ( p->io_type )
448 {
449 248976 case syck_io_str:
450 248976 skip = syck_move_tokens( p );
451 248976 len = (p->io.str->read)( p->buffer, p->io.str, SYCK_BUFFERSIZE - 1, skip );
452 248976 break;
453  
454 0 case syck_io_file:
455 0 skip = syck_move_tokens( p );
456 0 len = (p->io.file->read)( p->buffer, p->io.file, SYCK_BUFFERSIZE - 1, skip );
457 0 break;
458 }
459 248976 syck_check_limit( p, len );
460 248976 return len;
461 }
462  
463 long
464 0 syck_parser_readlen( SyckParser *p, long max_size )
465 {
466 0 long len = 0;
467 0 long skip = 0;
468 ASSERT( p != NULL );
469 0 switch ( p->io_type )
470 {
471 0 case syck_io_str:
472 0 skip = syck_move_tokens( p );
473 0 len = (p->io.str->read)( p->buffer, p->io.str, max_size, skip );
474 0 break;
475  
476 0 case syck_io_file:
477 0 skip = syck_move_tokens( p );
478 0 len = (p->io.file->read)( p->buffer, p->io.file, max_size, skip );
479 0 break;
480 }
481 0 syck_check_limit( p, len );
482 0 return len;
483 }
484  
485 SYMID
486 2461 syck_parse( SyckParser *p )
487 {
488 ASSERT( p != NULL );
489  
490 2461 syck_st_free( p );
491 2461 syck_parser_reset_levels( p );
492 2461 syckparse( p );
493 2439 return p->root;
494 }
495  
496 void
497 0 syck_default_error_handler( SyckParser *p, char *msg )
498 {
499 0 printf( "Error at [Line %d, Col %ld]: %s\n",
500 p->linect,
501 0 p->cursor - p->lineptr,
502 msg );
503 0 }
504  
505 267 int syck_str_is_unquotable_integer(char* str, long len) {
506 int idx;
507  
508 267 ▶ if(!str) return 0; /* Don't parse null strings */
Branch: 1 branch
BranchTrueFalseTotal
if(!str) return 0; /* Don't parse null strings */ 0 267 267
509 267 ▶ if(len < 1) return 0; /* empty strings can't be numbers */
Branch: 1 branch
BranchTrueFalseTotal
if(len < 1) return 0; /* empty strings can't be numbers */ 0 267 267
510 267 ▶ if(len > 9) return 0; /* Ints larger than 9 digits (32bit) might not portable. Force a string. */
Branch: 1 branch
BranchTrueFalseTotal
if(len > 9) return 0; /* Ints larger than 9 digits (32bit) might not portable. Force a string. */ 2 265 267
511  
512 265 ▶ if(str[0] == '0' && len == 1) return 1; /* 0 is unquoted. */
Branch: 2 branches
BranchTrueFalseTotal
if(str[0] == '0' && len == 1) return 1; /* 0 is unquoted. */ 5 260 265
if(str[0] == '0' && len == 1) return 1; /* 0 is unquoted. */ 5 0 5
513 260 ▶ if(str[0] == '+') return 0; /* +N must be quoted or the '+' is lost on roundtrip. */
Branch: 1 branch
BranchTrueFalseTotal
if(str[0] == '+') return 0; /* +N must be quoted or the '+' is lost on roundtrip. */ 0 260 260
514 260 ▶ if(str[0] == '-') {str++; len --;} /* supress the leading '-' sign if detected for testing purposes only. */
Branch: 1 branch
BranchTrueFalseTotal
if(str[0] == '-') {str++; len --;} /* supress the leading '-' sign if detected for testing purposes only. */ 14 246 260
515 260 ▶ if(str[0] == '0') return 0; /* Octals need to be quoted or you lose data converting them to an integer. This also accidentally blocks -0 which probably needs to be quoted. */
Branch: 1 branch
BranchTrueFalseTotal
if(str[0] == '0') return 0; /* Octals need to be quoted or you lose data converting them to an integer. This also accidentally blocks -0 which probably needs to be quoted. */ 0 260 260
516  
517 /* Look for illegal characters */
518 305 ▶ for ( idx = 1; idx < len; idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( idx = 1; idx < len; idx++ ) { 45 260 305
519 45 ▶ if(!isdigit(str[idx])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!isdigit(str[idx])) return 0; 0 45 45
520 }
521  
522 260 return 1;
523 }
« emitter.c ↑ Summary perl_syck.h »