syck_.c

filter: Statementstmt 78.4% 218 / 278 Branchbran 70.5% 55 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.6% 273 / 356 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« handler.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 18752269 syck_strndup( char *buf, long len )
34 {
35 18752269 char *new = S_ALLOC_N( char, len + 1 );
36 18752269 S_MEMZERO( new, char, len + 1 );
37 18752269 S_MEMCPY( new, buf, char, len );
38 18752269 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 249902 syck_io_str_read( char *buf, SyckIoStr *str, long max_size, long skip )
64 {
65 char *beg;
66 249902 long len = 0;
67  
68 ASSERT( str != NULL );
69 249902 beg = str->ptr;
70 249902 ▶ if ( max_size >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( max_size >= 0 ) 249902 0 249902
71 {
72 249902 max_size -= skip;
73 249902 ▶ if ( max_size <= 0 ) max_size = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ( max_size <= 0 ) max_size = 0; 0 249902 249902
74 249902 else str->ptr += max_size;
75  
76 249902 ▶ if ( str->ptr > str->end )
Branch: 1 branch
BranchTrueFalseTotal
if ( str->ptr > str->end ) 10731 239171 249902
77 {
78 10731 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 249902 ▶ if ( beg < str->ptr )
Branch: 1 branch
BranchTrueFalseTotal
if ( beg < str->ptr ) 241422 8480 249902
89 {
90 241422 len = ( str->ptr - beg );
91 241422 S_MEMCPY( buf + skip, beg, char, len );
92 }
93 249902 len += skip;
94 249902 buf[len] = '\0';
95  
96 249902 return len;
97 }
98  
99 void
100 6992 syck_parser_reset_levels( SyckParser *p )
101 {
102 8062 ▶ while ( p->lvl_idx > 1 )
Branch: 1 branch
BranchTrueFalseTotal
while ( p->lvl_idx > 1 ) 1070 6992 8062
103 {
104 1070 syck_parser_pop_level( p );
105 }
106  
107 6992 ▶ if ( p->lvl_idx < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx < 1 ) 2252 4740 6992
108 {
109 2252 p->lvl_idx = 1;
110 2252 p->levels[0].spaces = -1;
111 2252 p->levels[0].ncount = 0;
112 2252 p->levels[0].domain = syck_strndup( "", 0 );
113 }
114 6992 p->levels[0].status = syck_lvl_header;
115 6992 }
116  
117 void
118 2252 syck_parser_reset_cursor( SyckParser *p )
119 {
120 2252 ▶ if ( p->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->buffer == NULL ) 2252 0 2252
121 {
122 2252 p->buffer = S_ALLOC_N( char, p->bufsize );
123 2252 S_MEMZERO( p->buffer, char, p->bufsize );
124 }
125 2252 p->buffer[0] = '\0';
126  
127 2252 p->cursor = NULL;
128 2252 p->lineptr = NULL;
129 2252 p->linectptr = NULL;
130 2252 p->token = NULL;
131 2252 p->toktmp = NULL;
132 2252 p->marker = NULL;
133 2252 p->limit = NULL;
134  
135 2252 p->root = 0;
136 2252 p->root_on_error = 0;
137 2252 p->linect = 0;
138 2252 p->eof = 0;
139 2252 p->last_token = 0;
140 2252 p->force_token = 0;
141 2252 }
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 2252 syck_new_parser(void)
157 {
158 SyckParser *p;
159 2252 p = S_ALLOC( SyckParser );
160 2252 S_MEMZERO( p, SyckParser, 1 );
161 2252 p->lvl_capa = ALLOC_CT;
162 2252 p->levels = S_ALLOC_N( SyckLevel, p->lvl_capa );
163 2252 p->input_type = syck_yaml_utf8;
164 2252 p->io_type = syck_io_str;
165 2252 p->io.str = NULL;
166 2252 p->syms = NULL;
167 2252 p->anchors = NULL;
168 2252 p->bad_anchors = NULL;
169 2252 p->retired = NULL;
170 2252 p->implicit_typing = 1;
171 2252 p->taguri_expansion = 0;
172 2252 p->bufsize = SYCK_BUFFERSIZE;
173 2252 p->buffer = NULL;
174 2252 p->lvl_idx = 0;
175 2252 p->max_depth = 0;
176 2252 syck_parser_reset_levels( p );
177 2252 return p;
178 }
179  
180 int
181 18726921 syck_add_sym( SyckParser *p, char *data )
182 {
183 18726921 SYMID id = 0;
184 18726921 ▶ if ( p->syms == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms == NULL ) 2233 18724688 18726921
185 {
186 2233 p->syms = st_init_numtable();
187 }
188 18726921 id = p->syms->num_entries + 1;
189 18726921 st_insert( p->syms, id, (st_data_t)data );
190 18726921 return id;
191 }
192  
193 int
194 18726938 syck_lookup_sym( SyckParser *p, SYMID id, char **data )
195 {
196 18726938 ▶ if ( p->syms == NULL ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms == NULL ) return 0; 0 18726938 18726938
197 18726938 return st_lookup( p->syms, id, (st_data_t *)data );
198 }
199  
200 enum st_retval
201 40 syck_st_free_nodes( st_data_t key, st_data_t value, st_data_t arg )
202 {
203 40 SyckNode *n = (SyckNode *)value;
204 40 char *k = (char *)key;
205 40 ▶ if ( n != (void *)1 ) syck_free_node( n );
Branch: 1 branch
BranchTrueFalseTotal
if ( n != (void *)1 ) syck_free_node( n ); 38 2 40
206 40 S_FREE( k ); /* the anchor tables own their key strings */
207 40 return ST_CONTINUE;
208 }
209  
210 enum st_retval
211 1 syck_st_free_retired( st_data_t key, st_data_t value, st_data_t arg )
212 {
213 1 syck_free_node( (SyckNode *)value );
214 1 return ST_CONTINUE;
215 }
216  
217 void
218 4740 syck_st_free( SyckParser *p )
219 {
220 /*
221 * Free the anchor tables
222 */
223 4740 ▶ if ( p->anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->anchors != NULL ) 24 4716 4740
224 {
225 24 st_foreach( p->anchors, syck_st_free_nodes, 0 );
226 24 st_free_table( p->anchors );
227 24 p->anchors = NULL;
228 }
229  
230 4740 ▶ if ( p->bad_anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->bad_anchors != NULL ) 6 4734 4740
231 {
232 6 st_foreach( p->bad_anchors, syck_st_free_nodes, 0 );
233 6 st_free_table( p->bad_anchors );
234 6 p->bad_anchors = NULL;
235 }
236  
237 4740 ▶ if ( p->retired != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->retired != NULL ) 1 4739 4740
238 {
239 1 st_foreach( p->retired, syck_st_free_retired, 0 );
240 1 st_free_table( p->retired );
241 1 p->retired = NULL;
242 }
243 4740 }
244  
245 void
246 2252 syck_free_parser( SyckParser *p )
247 {
248 /*
249 * Free the adhoc symbol table
250 */
251 2252 ▶ if ( p->syms != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms != NULL ) 2233 19 2252
252 {
253 2233 st_free_table( p->syms );
254 2233 p->syms = NULL;
255 }
256  
257 /*
258 * Free tables, levels
259 */
260 2252 syck_st_free( p );
261 2252 syck_parser_reset_levels( p );
262 2252 S_FREE( p->levels[0].domain );
263 2252 S_FREE( p->levels );
264  
265 2252 ▶ if ( p->buffer != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->buffer != NULL ) 2252 0 2252
266 {
267 2252 S_FREE( p->buffer );
268 }
269 2252 free_any_io( p );
270 2252 S_FREE( p );
271 2252 }
272  
273 void
274 2252 syck_parser_handler( SyckParser *p, SyckNodeHandler hdlr )
275 {
276 ASSERT( p != NULL );
277 2252 p->handler = hdlr;
278 2252 }
279  
280 void
281 2252 syck_parser_implicit_typing( SyckParser *p, int flag )
282 {
283 2252 p->implicit_typing = ( flag == 0 ? 0 : 1 );
284 2252 }
285  
286 void
287 2252 syck_parser_taguri_expansion( SyckParser *p, int flag )
288 {
289 2252 p->taguri_expansion = ( flag == 0 ? 0 : 1 );
290 2252 }
291  
292 void
293 2252 syck_parser_error_handler( SyckParser *p, SyckErrorHandler hdlr )
294 {
295 ASSERT( p != NULL );
296 2252 p->error_handler = hdlr;
297 2252 }
298  
299 void
300 2252 syck_parser_bad_anchor_handler( SyckParser *p, SyckBadAnchorHandler hdlr )
301 {
302 ASSERT( p != NULL );
303 2252 p->bad_anchor_handler = hdlr;
304 2252 }
305  
306 void
307 0 syck_parser_set_input_type( SyckParser *p, enum syck_parser_input input_type )
308 {
309 ASSERT( p != NULL );
310 0 p->input_type = input_type;
311 0 }
312  
313 void
314 0 syck_parser_file( SyckParser *p, FILE *fp, SyckIoFileRead read )
315 {
316 ASSERT( p != NULL );
317 0 free_any_io( p );
318 0 syck_parser_reset_cursor( p );
319 0 p->io_type = syck_io_file;
320 0 p->io.file = S_ALLOC( SyckIoFile );
321 0 p->io.file->ptr = fp;
322 0 ▶ if ( read != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( read != NULL ) 0 0 0
323 {
324 0 p->io.file->read = read;
325 }
326 else
327 {
328 0 p->io.file->read = syck_io_file_read;
329 }
330 0 }
331  
332 void
333 2252 syck_parser_str( SyckParser *p, char *ptr, long len, SyckIoStrRead read )
334 {
335 ASSERT( p != NULL );
336 2252 free_any_io( p );
337 2252 syck_parser_reset_cursor( p );
338 2252 p->io_type = syck_io_str;
339 2252 p->io.str = S_ALLOC( SyckIoStr );
340 2252 p->io.str->beg = ptr;
341 2252 p->io.str->ptr = ptr;
342 2252 p->io.str->end = ptr + len;
343 2252 ▶ if ( read != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( read != NULL ) 0 2252 2252
344 {
345 0 p->io.str->read = read;
346 }
347 else
348 {
349 2252 p->io.str->read = syck_io_str_read;
350 }
351 2252 }
352  
353 void
354 2252 syck_parser_str_auto( SyckParser *p, char *ptr, SyckIoStrRead read )
355 {
356 2252 syck_parser_str( p, ptr, strlen( ptr ), read );
357 2252 }
358  
359 SyckLevel *
360 74927284 syck_parser_current_level( SyckParser *p )
361 {
362 74927284 return &p->levels[p->lvl_idx-1];
363 }
364  
365 void
366 5022 syck_parser_pop_level( SyckParser *p )
367 {
368 ASSERT( p != NULL );
369  
370 /* The root level should never be popped */
371 5022 ▶ if ( p->lvl_idx <= 1 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx <= 1 ) return; 0 5022 5022
372  
373 5022 p->lvl_idx -= 1;
374 5022 free( p->levels[p->lvl_idx].domain );
375 }
376  
377 void
378 5030 syck_parser_add_level( SyckParser *p, int len, enum syck_level_status status )
379 {
380 ASSERT( p != NULL );
381 5030 ▶ if ( p->max_depth > 0 && p->lvl_idx >= p->max_depth )
Branch: 2 branches
BranchTrueFalseTotal
if ( p->max_depth > 0 && p->lvl_idx >= p->max_depth ) 5030 0 5030
if ( p->max_depth > 0 && p->lvl_idx >= p->max_depth ) 8 5022 5030
382 {
383 8 (p->error_handler)(p, "document is nested too deeply");
384 0 return;
385 }
386 5022 ▶ if ( p->lvl_idx + 1 > p->lvl_capa )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx + 1 > p->lvl_capa ) 291 4731 5022
387 {
388 291 p->lvl_capa += ALLOC_CT;
389 291 S_REALLOC_N( p->levels, SyckLevel, p->lvl_capa );
390 }
391  
392 ASSERT( len > p->levels[p->lvl_idx-1].spaces );
393 5022 p->levels[p->lvl_idx].spaces = len;
394 5022 p->levels[p->lvl_idx].ncount = 0;
395 5022 p->levels[p->lvl_idx].domain = syck_strndup( p->levels[p->lvl_idx-1].domain, strlen( p->levels[p->lvl_idx-1].domain ) );
396 5022 p->levels[p->lvl_idx].status = status;
397 5022 p->lvl_idx += 1;
398 }
399  
400 void
401 4504 free_any_io( SyckParser *p )
402 {
403 ASSERT( p != NULL );
404 4504 switch ( p->io_type )
405 {
406 4504 case syck_io_str:
407 4504 ▶ if ( p->io.str != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->io.str != NULL ) 2252 2252 4504
408 {
409 2252 S_FREE( p->io.str );
410 2252 p->io.str = NULL;
411 }
412 4504 break;
413  
414 0 case syck_io_file:
415 0 ▶ if ( p->io.file != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->io.file != NULL ) 0 0 0
416 {
417 0 S_FREE( p->io.file );
418 0 p->io.file = NULL;
419 }
420 0 break;
421 }
422 4504 }
423  
424 long
425 249902 syck_move_tokens( SyckParser *p )
426 {
427 long count, skip;
428 ASSERT( p->buffer != NULL );
429  
430 249902 ▶ if ( p->token == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->token == NULL ) 2252 247650 249902
431 2252 return 0;
432  
433 247650 skip = p->limit - p->token;
434 247650 ▶ if ( skip < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( skip < 0 ) 0 247650 247650
435 0 return 0;
436  
437 247650 ▶ if ( ( count = p->token - p->buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( count = p->token - p->buffer ) ) 243390 4260 247650
438 {
439 243390 S_MEMMOVE( p->buffer, p->token, char, skip );
440 243390 p->token = p->buffer;
441 243390 p->marker -= count;
442 243390 p->cursor -= count;
443 243390 p->toktmp -= count;
444 243390 p->limit -= count;
445 243390 p->lineptr -= count;
446 243390 p->linectptr -= count;
447 }
448 247650 return skip;
449 }
450  
451 void
452 249902 syck_check_limit( SyckParser *p, long len )
453 {
454 249902 ▶ if ( p->cursor == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->cursor == NULL ) 2252 247650 249902
455 {
456 2252 p->cursor = p->buffer;
457 2252 p->lineptr = p->buffer;
458 2252 p->linectptr = p->buffer;
459 2252 p->marker = p->buffer;
460 }
461 249902 p->limit = p->buffer + len;
462 249902 }
463  
464 long
465 249902 syck_parser_read( SyckParser *p )
466 {
467 249902 long len = 0;
468 249902 long skip = 0;
469 ASSERT( p != NULL );
470 249902 switch ( p->io_type )
471 {
472 249902 case syck_io_str:
473 249902 skip = syck_move_tokens( p );
474 249902 len = (p->io.str->read)( p->buffer, p->io.str, SYCK_BUFFERSIZE - 1, skip );
475 249902 break;
476  
477 0 case syck_io_file:
478 0 skip = syck_move_tokens( p );
479 0 len = (p->io.file->read)( p->buffer, p->io.file, SYCK_BUFFERSIZE - 1, skip );
480 0 break;
481 }
482 249902 syck_check_limit( p, len );
483 249902 return len;
484 }
485  
486 long
487 0 syck_parser_readlen( SyckParser *p, long max_size )
488 {
489 0 long len = 0;
490 0 long skip = 0;
491 ASSERT( p != NULL );
492 0 switch ( p->io_type )
493 {
494 0 case syck_io_str:
495 0 skip = syck_move_tokens( p );
496 0 len = (p->io.str->read)( p->buffer, p->io.str, max_size, skip );
497 0 break;
498  
499 0 case syck_io_file:
500 0 skip = syck_move_tokens( p );
501 0 len = (p->io.file->read)( p->buffer, p->io.file, max_size, skip );
502 0 break;
503 }
504 0 syck_check_limit( p, len );
505 0 return len;
506 }
507  
508 SYMID
509 2488 syck_parse( SyckParser *p )
510 {
511 ASSERT( p != NULL );
512  
513 2488 syck_st_free( p );
514 2488 syck_parser_reset_levels( p );
515 2488 syckparse( p );
516 2456 return p->root;
517 }
518  
519 void
520 0 syck_default_error_handler( SyckParser *p, char *msg )
521 {
522 0 printf( "Error at [Line %d, Col %ld]: %s\n",
523 p->linect,
524 0 p->cursor - p->lineptr,
525 msg );
526 0 }
527  
528 267 int syck_str_is_unquotable_integer(char* str, long len) {
529 int idx;
530  
531 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
532 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
533 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
534  
535 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
536 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
537 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
538 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
539  
540 /* Look for illegal characters */
541 305 ▶ for ( idx = 1; idx < len; idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( idx = 1; idx < len; idx++ ) { 45 260 305
542 45 ▶ if(!isdigit(str[idx])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!isdigit(str[idx])) return 0; 0 45 45
543 }
544  
545 260 return 1;
546 }
« handler.c ↑ Summary perl_syck.h »