| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: lam.c,v 1.8 2005/10/05 05:20:50 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* lam.c - simple program to laminate files. |
| 6 |
* |
| 7 |
* 7/14/88 Greg Ward |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <ctype.h> |
| 14 |
|
| 15 |
#include "platform.h" |
| 16 |
#include "rtprocess.h" |
| 17 |
|
| 18 |
#define MAXFILE 512 /* maximum number of files */ |
| 19 |
|
| 20 |
#define MAXLINE 4096 /* maximum input line */ |
| 21 |
|
| 22 |
FILE *input[MAXFILE]; |
| 23 |
int bytsiz[MAXFILE]; |
| 24 |
char *tabc[MAXFILE]; |
| 25 |
int nfiles; |
| 26 |
|
| 27 |
char buf[MAXLINE]; |
| 28 |
|
| 29 |
int |
| 30 |
main(argc, argv) |
| 31 |
int argc; |
| 32 |
char *argv[]; |
| 33 |
{ |
| 34 |
register int i; |
| 35 |
char *curtab; |
| 36 |
int curbytes; |
| 37 |
int running, puteol; |
| 38 |
|
| 39 |
curtab = "\t"; |
| 40 |
curbytes = 0; |
| 41 |
nfiles = 0; |
| 42 |
for (i = 1; i < argc; i++) { |
| 43 |
if (argv[i][0] == '-') { |
| 44 |
switch (argv[i][1]) { |
| 45 |
case 't': |
| 46 |
curtab = argv[i]+2; |
| 47 |
break; |
| 48 |
case 'i': |
| 49 |
switch (argv[i][2]) { |
| 50 |
case 'f': |
| 51 |
curbytes = sizeof(float); |
| 52 |
break; |
| 53 |
case 'd': |
| 54 |
curbytes = sizeof(double); |
| 55 |
break; |
| 56 |
case 'w': |
| 57 |
curbytes = sizeof(int); |
| 58 |
break; |
| 59 |
case 'a': |
| 60 |
curbytes = argv[i][3] ? 1 : 0; |
| 61 |
break; |
| 62 |
default: |
| 63 |
goto badopt; |
| 64 |
} |
| 65 |
if (isdigit(argv[i][3])) |
| 66 |
curbytes *= atoi(argv[i]+3); |
| 67 |
if (curbytes < 0 || curbytes > MAXLINE) { |
| 68 |
fputs(argv[0], stderr); |
| 69 |
fputs(": illegal input size\n", stderr); |
| 70 |
exit(1); |
| 71 |
} |
| 72 |
if (curbytes) |
| 73 |
curtab = ""; |
| 74 |
break; |
| 75 |
case '\0': |
| 76 |
tabc[nfiles] = curtab; |
| 77 |
bytsiz[nfiles] = curbytes; |
| 78 |
input[nfiles++] = stdin; |
| 79 |
break; |
| 80 |
badopt:; |
| 81 |
default: |
| 82 |
fputs(argv[0], stderr); |
| 83 |
fputs(": bad option\n", stderr); |
| 84 |
exit(1); |
| 85 |
} |
| 86 |
} else if (argv[i][0] == '!') { |
| 87 |
tabc[nfiles] = curtab; |
| 88 |
bytsiz[nfiles] = curbytes; |
| 89 |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
| 90 |
fputs(argv[i], stderr); |
| 91 |
fputs(": cannot start command\n", stderr); |
| 92 |
exit(1); |
| 93 |
} |
| 94 |
if (bytsiz[nfiles]) |
| 95 |
SET_FILE_BINARY(input[nfiles]); |
| 96 |
++nfiles; |
| 97 |
} else { |
| 98 |
tabc[nfiles] = curtab; |
| 99 |
bytsiz[nfiles] = curbytes; |
| 100 |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
| 101 |
fputs(argv[i], stderr); |
| 102 |
fputs(": cannot open file\n", stderr); |
| 103 |
exit(1); |
| 104 |
} |
| 105 |
if (bytsiz[nfiles]) |
| 106 |
SET_FILE_BINARY(input[nfiles]); |
| 107 |
++nfiles; |
| 108 |
} |
| 109 |
if (nfiles >= MAXFILE) { |
| 110 |
fputs(argv[0], stderr); |
| 111 |
fputs(": too many input streams\n", stderr); |
| 112 |
exit(1); |
| 113 |
} |
| 114 |
} |
| 115 |
puteol = 0; /* check for tab character */ |
| 116 |
for (i = nfiles; i--; ) |
| 117 |
if (isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
| 118 |
puteol++; |
| 119 |
break; |
| 120 |
} |
| 121 |
do { |
| 122 |
running = 0; |
| 123 |
for (i = 0; i < nfiles; i++) { |
| 124 |
if (bytsiz[i]) { /* binary file */ |
| 125 |
if (fread(buf, bytsiz[i], 1, input[i]) == 1) { |
| 126 |
if (i) |
| 127 |
fputs(tabc[i], stdout); |
| 128 |
fwrite(buf, bytsiz[i], 1, stdout); |
| 129 |
running++; |
| 130 |
} |
| 131 |
} else if (fgets(buf, MAXLINE, input[i]) != NULL) { |
| 132 |
if (i) |
| 133 |
fputs(tabc[i], stdout); |
| 134 |
buf[strlen(buf)-1] = '\0'; |
| 135 |
fputs(buf, stdout); |
| 136 |
puteol++; |
| 137 |
running++; |
| 138 |
} |
| 139 |
} |
| 140 |
if (running && puteol) |
| 141 |
putchar('\n'); |
| 142 |
} while (running); |
| 143 |
|
| 144 |
exit(0); |
| 145 |
} |