1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
1.7 |
static const char RCSid[] = "$Id: lam.c,v 1.6 2005/06/13 22:40:47 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* lam.c - simple program to laminate files. |
6 |
|
|
* |
7 |
|
|
* 7/14/88 Greg Ward |
8 |
|
|
*/ |
9 |
|
|
|
10 |
schorsch |
1.2 |
#include <stdlib.h> |
11 |
|
|
#include <string.h> |
12 |
greg |
1.1 |
#include <stdio.h> |
13 |
greg |
1.7 |
#include <ctype.h> |
14 |
schorsch |
1.2 |
|
15 |
|
|
#include "platform.h" |
16 |
schorsch |
1.3 |
#include "rtprocess.h" |
17 |
greg |
1.1 |
|
18 |
greg |
1.7 |
#define MAXFILE 512 /* maximum number of files */ |
19 |
greg |
1.1 |
|
20 |
greg |
1.7 |
#define MAXLINE 4096 /* maximum input line */ |
21 |
greg |
1.1 |
|
22 |
|
|
FILE *input[MAXFILE]; |
23 |
greg |
1.7 |
int bytsiz[MAXFILE]; |
24 |
greg |
1.6 |
char *tabc[MAXFILE]; |
25 |
greg |
1.1 |
int nfiles; |
26 |
|
|
|
27 |
|
|
char buf[MAXLINE]; |
28 |
|
|
|
29 |
schorsch |
1.4 |
int |
30 |
greg |
1.1 |
main(argc, argv) |
31 |
|
|
int argc; |
32 |
|
|
char *argv[]; |
33 |
|
|
{ |
34 |
|
|
register int i; |
35 |
greg |
1.6 |
char *curtab; |
36 |
greg |
1.7 |
int curbytes; |
37 |
|
|
int running, puteol; |
38 |
greg |
1.1 |
|
39 |
greg |
1.6 |
curtab = "\t"; |
40 |
greg |
1.7 |
curbytes = 0; |
41 |
greg |
1.1 |
nfiles = 0; |
42 |
|
|
for (i = 1; i < argc; i++) { |
43 |
|
|
if (argv[i][0] == '-') { |
44 |
|
|
switch (argv[i][1]) { |
45 |
|
|
case 't': |
46 |
greg |
1.6 |
curtab = argv[i]+2; |
47 |
greg |
1.1 |
break; |
48 |
greg |
1.7 |
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 |
|
|
break; |
73 |
greg |
1.1 |
case '\0': |
74 |
|
|
tabc[nfiles] = curtab; |
75 |
greg |
1.7 |
bytsiz[nfiles] = curbytes; |
76 |
greg |
1.1 |
input[nfiles++] = stdin; |
77 |
|
|
break; |
78 |
greg |
1.7 |
badopt:; |
79 |
greg |
1.1 |
default: |
80 |
|
|
fputs(argv[0], stderr); |
81 |
|
|
fputs(": bad option\n", stderr); |
82 |
|
|
exit(1); |
83 |
|
|
} |
84 |
|
|
} else if (argv[i][0] == '!') { |
85 |
|
|
tabc[nfiles] = curtab; |
86 |
greg |
1.7 |
bytsiz[nfiles] = curbytes; |
87 |
greg |
1.1 |
if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) { |
88 |
|
|
fputs(argv[i], stderr); |
89 |
|
|
fputs(": cannot start command\n", stderr); |
90 |
|
|
exit(1); |
91 |
|
|
} |
92 |
|
|
} else { |
93 |
|
|
tabc[nfiles] = curtab; |
94 |
greg |
1.7 |
bytsiz[nfiles] = curbytes; |
95 |
greg |
1.1 |
if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) { |
96 |
|
|
fputs(argv[i], stderr); |
97 |
|
|
fputs(": cannot open file\n", stderr); |
98 |
|
|
exit(1); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
if (nfiles >= MAXFILE) { |
102 |
|
|
fputs(argv[0], stderr); |
103 |
|
|
fputs(": too many input streams\n", stderr); |
104 |
|
|
exit(1); |
105 |
|
|
} |
106 |
|
|
} |
107 |
greg |
1.7 |
puteol = 0; /* check for tab character */ |
108 |
|
|
for (i = nfiles; i--; ) |
109 |
|
|
if (isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
110 |
|
|
puteol++; |
111 |
|
|
break; |
112 |
|
|
} |
113 |
greg |
1.1 |
do { |
114 |
|
|
running = 0; |
115 |
|
|
for (i = 0; i < nfiles; i++) { |
116 |
greg |
1.7 |
if (bytsiz[i]) { /* binary file */ |
117 |
|
|
if (fread(buf, bytsiz[i], 1, input[i]) == 1) { |
118 |
|
|
if (i) |
119 |
|
|
fputs(tabc[i], stdout); |
120 |
|
|
fwrite(buf, bytsiz[i], 1, stdout); |
121 |
|
|
running++; |
122 |
|
|
} |
123 |
|
|
} else if (fgets(buf, MAXLINE, input[i]) != NULL) { |
124 |
greg |
1.1 |
if (i) |
125 |
greg |
1.6 |
fputs(tabc[i], stdout); |
126 |
greg |
1.1 |
buf[strlen(buf)-1] = '\0'; |
127 |
|
|
fputs(buf, stdout); |
128 |
greg |
1.7 |
puteol++; |
129 |
greg |
1.1 |
running++; |
130 |
|
|
} |
131 |
|
|
} |
132 |
greg |
1.7 |
if (running && puteol) |
133 |
greg |
1.1 |
putchar('\n'); |
134 |
|
|
} while (running); |
135 |
|
|
|
136 |
|
|
exit(0); |
137 |
|
|
} |