| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
|
|
static const char RCSid[] = "$Id$"; |
| 3 |
|
|
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* lam.c - simple program to laminate files. |
| 6 |
|
|
* |
| 7 |
|
|
* 7/14/88 Greg Ward |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include <stdio.h> |
| 11 |
|
|
|
| 12 |
|
|
#define MAXFILE 16 /* maximum number of files */ |
| 13 |
|
|
|
| 14 |
|
|
#define MAXLINE 512 /* maximum input line */ |
| 15 |
|
|
|
| 16 |
|
|
FILE *input[MAXFILE]; |
| 17 |
|
|
int tabc[MAXFILE]; |
| 18 |
|
|
int nfiles; |
| 19 |
|
|
|
| 20 |
|
|
char buf[MAXLINE]; |
| 21 |
|
|
|
| 22 |
|
|
main(argc, argv) |
| 23 |
|
|
int argc; |
| 24 |
|
|
char *argv[]; |
| 25 |
|
|
{ |
| 26 |
|
|
register int i; |
| 27 |
|
|
int curtab; |
| 28 |
|
|
int running; |
| 29 |
|
|
|
| 30 |
|
|
curtab = '\t'; |
| 31 |
|
|
nfiles = 0; |
| 32 |
|
|
for (i = 1; i < argc; i++) { |
| 33 |
|
|
if (argv[i][0] == '-') { |
| 34 |
|
|
switch (argv[i][1]) { |
| 35 |
|
|
case 't': |
| 36 |
|
|
curtab = argv[i][2]; |
| 37 |
|
|
break; |
| 38 |
|
|
case '\0': |
| 39 |
|
|
tabc[nfiles] = curtab; |
| 40 |
|
|
input[nfiles++] = stdin; |
| 41 |
|
|
break; |
| 42 |
|
|
default: |
| 43 |
|
|
fputs(argv[0], stderr); |
| 44 |
|
|
fputs(": bad option\n", stderr); |
| 45 |
|
|
exit(1); |
| 46 |
|
|
} |
| 47 |
|
|
} else if (argv[i][0] == '!') { |
| 48 |
|
|
tabc[nfiles] = curtab; |
| 49 |
|
|
if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) { |
| 50 |
|
|
fputs(argv[i], stderr); |
| 51 |
|
|
fputs(": cannot start command\n", stderr); |
| 52 |
|
|
exit(1); |
| 53 |
|
|
} |
| 54 |
|
|
} else { |
| 55 |
|
|
tabc[nfiles] = curtab; |
| 56 |
|
|
if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) { |
| 57 |
|
|
fputs(argv[i], stderr); |
| 58 |
|
|
fputs(": cannot open file\n", stderr); |
| 59 |
|
|
exit(1); |
| 60 |
|
|
} |
| 61 |
|
|
} |
| 62 |
|
|
if (nfiles >= MAXFILE) { |
| 63 |
|
|
fputs(argv[0], stderr); |
| 64 |
|
|
fputs(": too many input streams\n", stderr); |
| 65 |
|
|
exit(1); |
| 66 |
|
|
} |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
|
|
do { |
| 70 |
|
|
running = 0; |
| 71 |
|
|
for (i = 0; i < nfiles; i++) { |
| 72 |
|
|
if (fgets(buf, MAXLINE, input[i]) != NULL) { |
| 73 |
|
|
if (i) |
| 74 |
|
|
putchar(tabc[i]); |
| 75 |
|
|
buf[strlen(buf)-1] = '\0'; |
| 76 |
|
|
fputs(buf, stdout); |
| 77 |
|
|
running++; |
| 78 |
|
|
} |
| 79 |
|
|
} |
| 80 |
|
|
if (running) |
| 81 |
|
|
putchar('\n'); |
| 82 |
|
|
} while (running); |
| 83 |
|
|
|
| 84 |
|
|
exit(0); |
| 85 |
|
|
} |