| 461 | } |
| 462 | |
| 463 | static int scripted(void) |
| 464 | { |
| 465 | struct string_list parts = STRING_LIST_INIT_NODUP; |
| 466 | struct json_writer jw = JSON_WRITER_INIT; |
| 467 | char buf[MAX_LINE_LENGTH]; |
| 468 | char *line; |
| 469 | int line_nr = 0; |
| 470 | |
| 471 | line = get_trimmed_line(buf, MAX_LINE_LENGTH); |
| 472 | if (!line) |
| 473 | return 0; |
| 474 | |
| 475 | if (!strcmp(line, "object")) |
| 476 | jw_object_begin(&jw, pretty); |
| 477 | else if (!strcmp(line, "array")) |
| 478 | jw_array_begin(&jw, pretty); |
| 479 | else |
| 480 | die("expected first line to be 'object' or 'array'"); |
| 481 | |
| 482 | while ((line = get_trimmed_line(buf, MAX_LINE_LENGTH)) != NULL) { |
| 483 | struct line state = { 0 }; |
| 484 | char *verb; |
| 485 | char *key; |
| 486 | char *s_value; |
| 487 | intmax_t i_value; |
| 488 | double d_value; |
| 489 | |
| 490 | state.parts = &parts; |
| 491 | state.nr = ++line_nr; |
| 492 | |
| 493 | /* break line into command and zero or more tokens */ |
| 494 | string_list_setlen(&parts, 0); |
| 495 | string_list_split_in_place_f(&parts, line, " ", -1, |
| 496 | STRING_LIST_SPLIT_NONEMPTY); |
| 497 | |
| 498 | /* ignore empty lines */ |
| 499 | if (!parts.nr || !*parts.items[0].string) |
| 500 | continue; |
| 501 | |
| 502 | verb = parts.items[state.consumed_nr++].string; |
| 503 | |
| 504 | if (!strcmp(verb, "end")) { |
| 505 | jw_end(&jw); |
| 506 | } |
| 507 | else if (!strcmp(verb, "object-string")) { |
| 508 | get_s(&state, &key); |
| 509 | get_s(&state, &s_value); |
| 510 | jw_object_string(&jw, key, s_value); |
| 511 | } |
| 512 | else if (!strcmp(verb, "object-int")) { |
| 513 | get_s(&state, &key); |
| 514 | get_i(&state, &i_value); |
| 515 | jw_object_intmax(&jw, key, i_value); |
| 516 | } |
| 517 | else if (!strcmp(verb, "object-double")) { |
| 518 | get_s(&state, &key); |
| 519 | get_i(&state, &i_value); |
| 520 | get_d(&state, &d_value); |
no test coverage detected