* 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 */
| 362 | * returns >=0 when successful |
| 363 | */ |
| 364 | static int next_word(char *buffer, int argc, char *argv[], int newline) |
| 365 | { |
| 366 | int ret; |
| 367 | char c; |
| 368 | static int reading_stdin; |
| 369 | |
| 370 | if (!reading_stdin) |
| 371 | { |
| 372 | if (optind >= argc) |
| 373 | { |
| 374 | return EOF; |
| 375 | } |
| 376 | else if (newline |
| 377 | && '-' == argv[optind][0] |
| 378 | && 1 == strlen(argv[optind])) |
| 379 | { |
| 380 | |
| 381 | ++optind; |
| 382 | reading_stdin = 1; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if (reading_stdin) |
| 387 | { |
| 388 | do |
| 389 | { |
| 390 | do |
| 391 | { |
| 392 | ret = scanf(" %c%" STR(MAXARGSZ) "[^ \t\n#]", &c, &buffer[1]); |
| 393 | } |
| 394 | while (EINTR == ret); |
| 395 | |
| 396 | if (ret > 0 && '#' == c) |
| 397 | { |
| 398 | do |
| 399 | { |
| 400 | ret = scanf("%*[^\n]"); |
| 401 | } |
| 402 | while (EINTR == ret); /* consume comments */ |
| 403 | |
| 404 | ret = 0; |
| 405 | } |
| 406 | } |
| 407 | while (!ret); |
| 408 | |
| 409 | if (EOF == ret) |
| 410 | { |
| 411 | reading_stdin = 0; |
| 412 | } |
| 413 | else if (ret < 0) |
| 414 | { |
| 415 | rig_debug(RIG_DEBUG_ERR, "scanf: %s\n", strerror(errno)); |
| 416 | reading_stdin = 0; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | buffer[0] = c; |
| 421 | buffer[1 == ret ? 1 : MAXARGSZ] = '\0'; |
no test coverage detected