* function to get the next word from the command line or from stdin * until stdin exhausted. stdin is read if the special token '-' is * found on the command line. * * returns EOF when words exhausted * returns <0 is error number * returns >=0 when successful */
| 610 | * returns >=0 when successful |
| 611 | */ |
| 612 | static int next_word(char *buffer, int argc, char *argv[], int newline) |
| 613 | { |
| 614 | int ret; |
| 615 | char c; |
| 616 | static int reading_stdin; |
| 617 | |
| 618 | if (!reading_stdin) |
| 619 | { |
| 620 | if (optind >= argc) |
| 621 | { |
| 622 | return EOF; |
| 623 | } |
| 624 | else if (newline && '-' == argv[optind][0] && 1 == strlen(argv[optind])) |
| 625 | { |
| 626 | ++optind; |
| 627 | reading_stdin = 1; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (reading_stdin) |
| 632 | { |
| 633 | do |
| 634 | { |
| 635 | do |
| 636 | { |
| 637 | ret = scanf(" %c%" STR(MAXARGSZ) "[^ \t\n#]", &c, &buffer[1]); |
| 638 | } |
| 639 | while (EINTR == ret); |
| 640 | |
| 641 | if (ret > 0 && '#' == c) |
| 642 | { |
| 643 | do |
| 644 | { |
| 645 | ret = scanf("%*[^\n]"); |
| 646 | } |
| 647 | while (EINTR == ret); /* consume comments */ |
| 648 | |
| 649 | ret = 0; |
| 650 | } |
| 651 | } |
| 652 | while (!ret); |
| 653 | |
| 654 | if (EOF == ret) |
| 655 | { |
| 656 | reading_stdin = 0; |
| 657 | } |
| 658 | else if (ret < 0) |
| 659 | { |
| 660 | rig_debug(RIG_DEBUG_ERR, "scanf: %s\n", strerror(errno)); |
| 661 | reading_stdin = 0; |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | buffer[0] = c; |
| 666 | buffer[1 == ret ? 1 : MAXARGSZ] = '\0'; |
| 667 | |
| 668 | if (newline) |
| 669 | { |
no test coverage detected