11#include "test-tool.h"
22#include "json-writer.h"
3+ #include "string-list.h"
34
45static const char * expect_obj1 = "{\"a\":\"abc\",\"b\":42,\"c\":true}" ;
56static const char * expect_obj2 = "{\"a\":-1,\"b\":2147483647,\"c\":0}" ;
@@ -394,35 +395,41 @@ static int unit_tests(void)
394395 return 0 ;
395396}
396397
397- static void get_s (int line_nr , char * * s_in )
398+ struct line {
399+ struct string_list * parts ;
400+ size_t consumed_nr ;
401+ int nr ;
402+ };
403+
404+ static void get_s (struct line * line , char * * s_in )
398405{
399- * s_in = strtok ( NULL , " " );
400- if (! * s_in )
401- die ( "line[%d]: expected: <s>" , line_nr ) ;
406+ if ( line -> consumed_nr > line -> parts -> nr )
407+ die ( "line[%d]: expected: <s>" , line -> nr );
408+ * s_in = line -> parts -> items [ line -> consumed_nr ++ ]. string ;
402409}
403410
404- static void get_i (int line_nr , intmax_t * s_in )
411+ static void get_i (struct line * line , intmax_t * s_in )
405412{
406413 char * s ;
407414 char * endptr ;
408415
409- get_s (line_nr , & s );
416+ get_s (line , & s );
410417
411418 * s_in = strtol (s , & endptr , 10 );
412419 if (* endptr || errno == ERANGE )
413- die ("line[%d]: invalid integer value" , line_nr );
420+ die ("line[%d]: invalid integer value" , line -> nr );
414421}
415422
416- static void get_d (int line_nr , double * s_in )
423+ static void get_d (struct line * line , double * s_in )
417424{
418425 char * s ;
419426 char * endptr ;
420427
421- get_s (line_nr , & s );
428+ get_s (line , & s );
422429
423430 * s_in = strtod (s , & endptr );
424431 if (* endptr || errno == ERANGE )
425- die ("line[%d]: invalid float value" , line_nr );
432+ die ("line[%d]: invalid float value" , line -> nr );
426433}
427434
428435static int pretty ;
@@ -453,6 +460,7 @@ static char *get_trimmed_line(char *buf, int buf_size)
453460
454461static int scripted (void )
455462{
463+ struct string_list parts = STRING_LIST_INIT_NODUP ;
456464 struct json_writer jw = JSON_WRITER_INIT ;
457465 char buf [MAX_LINE_LENGTH ];
458466 char * line ;
@@ -470,66 +478,77 @@ static int scripted(void)
470478 die ("expected first line to be 'object' or 'array'" );
471479
472480 while ((line = get_trimmed_line (buf , MAX_LINE_LENGTH )) != NULL ) {
481+ struct line state = { 0 };
473482 char * verb ;
474483 char * key ;
475484 char * s_value ;
476485 intmax_t i_value ;
477486 double d_value ;
478487
479- line_nr ++ ;
488+ state .parts = & parts ;
489+ state .nr = ++ line_nr ;
490+
491+ /* break line into command and zero or more tokens */
492+ string_list_setlen (& parts , 0 );
493+ string_list_split_in_place (& parts , line , " " , -1 );
494+ string_list_remove_empty_items (& parts , 0 );
495+
496+ /* ignore empty lines */
497+ if (!parts .nr || !* parts .items [0 ].string )
498+ continue ;
480499
481- verb = strtok ( line , " " ) ;
500+ verb = parts . items [ state . consumed_nr ++ ]. string ;
482501
483502 if (!strcmp (verb , "end" )) {
484503 jw_end (& jw );
485504 }
486505 else if (!strcmp (verb , "object-string" )) {
487- get_s (line_nr , & key );
488- get_s (line_nr , & s_value );
506+ get_s (& state , & key );
507+ get_s (& state , & s_value );
489508 jw_object_string (& jw , key , s_value );
490509 }
491510 else if (!strcmp (verb , "object-int" )) {
492- get_s (line_nr , & key );
493- get_i (line_nr , & i_value );
511+ get_s (& state , & key );
512+ get_i (& state , & i_value );
494513 jw_object_intmax (& jw , key , i_value );
495514 }
496515 else if (!strcmp (verb , "object-double" )) {
497- get_s (line_nr , & key );
498- get_i (line_nr , & i_value );
499- get_d (line_nr , & d_value );
516+ get_s (& state , & key );
517+ get_i (& state , & i_value );
518+ get_d (& state , & d_value );
500519 jw_object_double (& jw , key , i_value , d_value );
501520 }
502521 else if (!strcmp (verb , "object-true" )) {
503- get_s (line_nr , & key );
522+ get_s (& state , & key );
504523 jw_object_true (& jw , key );
505524 }
506525 else if (!strcmp (verb , "object-false" )) {
507- get_s (line_nr , & key );
526+ get_s (& state , & key );
508527 jw_object_false (& jw , key );
509528 }
510529 else if (!strcmp (verb , "object-null" )) {
511- get_s (line_nr , & key );
530+ get_s (& state , & key );
512531 jw_object_null (& jw , key );
513532 }
514533 else if (!strcmp (verb , "object-object" )) {
515- get_s (line_nr , & key );
534+ get_s (& state , & key );
516535 jw_object_inline_begin_object (& jw , key );
517536 }
518537 else if (!strcmp (verb , "object-array" )) {
519- get_s (line_nr , & key );
538+ get_s (& state , & key );
520539 jw_object_inline_begin_array (& jw , key );
521540 }
522541 else if (!strcmp (verb , "array-string" )) {
523- get_s (line_nr , & s_value );
542+ get_s (& state , & s_value );
524543 jw_array_string (& jw , s_value );
525544 }
526545 else if (!strcmp (verb , "array-int" )) {
527- get_i (line_nr , & i_value );
546+ get_i (& state , & i_value );
528547 jw_array_intmax (& jw , i_value );
529548 }
530549 else if (!strcmp (verb , "array-double" )) {
531- get_i (line_nr , & i_value );
532- get_d (line_nr , & d_value );
550+ get_i (& state , & i_value );
551+ get_d (& state , & d_value );
533552 jw_array_double (& jw , i_value , d_value );
534553 }
535554 else if (!strcmp (verb , "array-true" ))
@@ -552,6 +571,7 @@ static int scripted(void)
552571 printf ("%s\n" , jw .json .buf );
553572
554573 jw_release (& jw );
574+ string_list_clear (& parts , 0 );
555575 return 0 ;
556576}
557577
0 commit comments