diff options
Diffstat (limited to 'pw.c')
-rw-r--r-- | pw.c | 41 |
1 files changed, 39 insertions, 2 deletions
@@ -269,6 +269,18 @@ static char *addchesc(char *line, int ch) return line; } +static char *getln(FILE *stream) +{ + for (char *line = 0;;) { + int ch = getc(stream); + if (ch == EOF) + return line; + if (ch == '\n') + return dsensure(line); + line = addchesc(line, ch); + } +} + static void usage(void) { fprintf(stderr, @@ -283,7 +295,8 @@ static void usage(void) "-g [!]pattern add pattern to grep stack; ! inverts\n" "-m integer specify maixmum line length\n" "-p values set display parameters\n" - "-e command execute :, / or ? command\n\n" + "-e command execute : / ? command\n" + "-f file execute : / ? commands from file\n\n" "<command> represents an arbitrary command that generates the\n" "output to be monitored by %s.\n\n" "Standard input must be redirected; it cannot be the same device\n" @@ -994,7 +1007,7 @@ int main(int argc, char **argv) } } - while ((opt = getopt(argc, argv, "n:i:l:dEBg:q:m:p:e:")) != -1) { + while ((opt = getopt(argc, argv, "n:i:l:dEBg:q:m:p:e:f:")) != -1) { switch (opt) { case 'n': { @@ -1121,7 +1134,31 @@ int main(int argc, char **argv) error("-%c option: %s: %s\n", opt, optarg, pw.cmdbuf); return EXIT_FAILURE; } + } + break; + case 'f': + { + FILE *f = fopen(optarg, "r"); + long lino = 1; + int errors = 0; + char *line; + if (f == 0) { + error("-%c option: unable to open %s\n", opt, optarg); + return EXIT_FAILURE; + } + for (; (line = getln(f)) != 0; lino++) { + if (line[0] == '#') + continue; + if (batchexe(&pw, line, pw.cmdbuf, cmdsize) == exec_failed) { + error("%s:%ld: %s\n", optarg, lino, pw.cmdbuf); + errors = 1; + } + dsdrop(line); + } + fclose(f); + if (errors) { return EXIT_FAILURE; + dsdrop(line); } } break; |