void cook_args(char **argv) { FILE *fp; fp = stdin; filename = "stdin"; do { if (*argv) { if (!strcmp(*argv, "-")) fp = stdin; else if ((fp = fopen(*argv, "r")) == NULL) { warn("%s", *argv); rval = 1; ++argv; continue; } filename = *argv++; } cook_buf(fp); if (fp == stdin) clearerr(fp); else (void)fclose(fp); } while (*argv); }
cook_args(char *argv[]) #endif { register FILE *fp; fp = stdin; filename = "stdin"; do { if (*argv) { if (!strcmp(*argv, "-")) fp = stdin; else if ((fp = fopen(*argv, "r")) == NULL) { warn("%s", *argv); rval = 1; ++argv; continue; } filename = *argv++; } cook_buf(fp); if (fp != stdin) (void)fclose(fp); } while (*argv); }
/* main program loop */ void mux_loop(int pf) { fd_set ready; /* used for select */ int i = 0; /* used in the multiplex loop */ int done = 0; char buf[BUFSIZE]; struct timeval tv; tv.tv_sec = SCRIPT_DELAY; tv.tv_usec = 0; if (script) { script_init(scr_name); } do { /* forever */ FD_ZERO(&ready); FD_SET(STDIN_FILENO, &ready); FD_SET(pf, &ready); if (script) { if (!select(pf+1, &ready, NULL, NULL, &tv)) { i = script_process(S_TIMEOUT, buf, BUFSIZE); if (i > 0) { cook_buf(pf, buf, i); } /* restart timer */ tv.tv_sec = SCRIPT_DELAY; tv.tv_usec = 0; } } /* if */ else { select(pf+1, &ready, NULL, NULL, NULL); } if (FD_ISSET(pf, &ready)) { /* pf has characters for us */ i = read(pf, buf, BUFSIZE); if (i > 0) { if (options & OPTION_LOG_FILTER) { /* only printable characters */ size_t size = 0; char *printable = PrintableBuffer(buf,i,&size); DEBUG_MSG("received printable buffer = %s",printable); if (flog != NULL) { printable[size] = '\n'; printable[size+1] = '\0'; const size_t written = fwrite(printable, 1, size, flog); if (written != size) { const int error = errno; DEBUG_MSG("error writing log file, only %u characters written, errno = %d",written,error); } } } else { /* raw memory dump */ DEBUG_DUMP_MEMORY(buf,i); if (flog != 0) { fwrite(buf, 1, i, flog); } } write(STDOUT_FILENO, buf, i); if (script) { i = script_process(S_DCE, buf, i); if (i > 0) { cook_buf(pf, buf, i); } } } else { done = 1; } } /* if */ if (FD_ISSET(STDIN_FILENO, &ready)) { /* standard input has characters for us */ i = read(STDIN_FILENO, buf, BUFSIZE); if (i > 0) { cook_buf(pf, buf, i); } else { done = 1; } } /* if */ } while (!done); /* do */ }