1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: lam.c,v 1.9 2006/05/07 15:44:28 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 'i': |
57 |
curbytes = sizeof(int); |
58 |
break; |
59 |
case 'w': |
60 |
curbytes = 2; |
61 |
break; |
62 |
case 'b': |
63 |
curbytes = 1; |
64 |
break; |
65 |
case 'a': |
66 |
curbytes = argv[i][3] ? 1 : 0; |
67 |
break; |
68 |
default: |
69 |
goto badopt; |
70 |
} |
71 |
if (isdigit(argv[i][3])) |
72 |
curbytes *= atoi(argv[i]+3); |
73 |
if (curbytes < 0 || curbytes > MAXLINE) { |
74 |
fputs(argv[0], stderr); |
75 |
fputs(": illegal input size\n", stderr); |
76 |
exit(1); |
77 |
} |
78 |
if (curbytes) |
79 |
curtab = ""; |
80 |
break; |
81 |
case '\0': |
82 |
tabc[nfiles] = curtab; |
83 |
bytsiz[nfiles] = curbytes; |
84 |
input[nfiles++] = stdin; |
85 |
break; |
86 |
badopt:; |
87 |
default: |
88 |
fputs(argv[0], stderr); |
89 |
fputs(": bad option\n", stderr); |
90 |
exit(1); |
91 |
} |
92 |
} else if (argv[i][0] == '!') { |
93 |
tabc[nfiles] = curtab; |
94 |
bytsiz[nfiles] = curbytes; |
95 |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
96 |
fputs(argv[i], stderr); |
97 |
fputs(": cannot start command\n", stderr); |
98 |
exit(1); |
99 |
} |
100 |
if (bytsiz[nfiles]) |
101 |
SET_FILE_BINARY(input[nfiles]); |
102 |
++nfiles; |
103 |
} else { |
104 |
tabc[nfiles] = curtab; |
105 |
bytsiz[nfiles] = curbytes; |
106 |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
107 |
fputs(argv[i], stderr); |
108 |
fputs(": cannot open file\n", stderr); |
109 |
exit(1); |
110 |
} |
111 |
if (bytsiz[nfiles]) |
112 |
SET_FILE_BINARY(input[nfiles]); |
113 |
++nfiles; |
114 |
} |
115 |
if (nfiles >= MAXFILE) { |
116 |
fputs(argv[0], stderr); |
117 |
fputs(": too many input streams\n", stderr); |
118 |
exit(1); |
119 |
} |
120 |
} |
121 |
puteol = 0; /* check for tab character */ |
122 |
for (i = nfiles; i--; ) |
123 |
if (isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
124 |
puteol++; |
125 |
break; |
126 |
} |
127 |
do { |
128 |
running = 0; |
129 |
for (i = 0; i < nfiles; i++) { |
130 |
if (bytsiz[i]) { /* binary file */ |
131 |
if (fread(buf, bytsiz[i], 1, input[i]) == 1) { |
132 |
if (i) |
133 |
fputs(tabc[i], stdout); |
134 |
fwrite(buf, bytsiz[i], 1, stdout); |
135 |
running++; |
136 |
} |
137 |
} else if (fgets(buf, MAXLINE, input[i]) != NULL) { |
138 |
if (i) |
139 |
fputs(tabc[i], stdout); |
140 |
buf[strlen(buf)-1] = '\0'; |
141 |
fputs(buf, stdout); |
142 |
puteol++; |
143 |
running++; |
144 |
} |
145 |
} |
146 |
if (running && puteol) |
147 |
putchar('\n'); |
148 |
} while (running); |
149 |
|
150 |
exit(0); |
151 |
} |