lam.c revision 1.2 1 1.2 jtc /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.1 jtc #ifndef lint
37 1.1 jtc static char copyright[] =
38 1.1 jtc "@(#) Copyright (c) 1993\n\
39 1.1 jtc The Regents of the University of California. All rights reserved.\n";
40 1.1 jtc #endif /* not lint */
41 1.1 jtc
42 1.1 jtc #ifndef lint
43 1.2 jtc #if 0
44 1.1 jtc static char sccsid[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93";
45 1.2 jtc #endif
46 1.2 jtc static char rcsid[] = "$NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $";
47 1.1 jtc #endif /* not lint */
48 1.1 jtc
49 1.1 jtc /*
50 1.1 jtc * lam - laminate files
51 1.1 jtc * Author: John Kunze, UCB
52 1.1 jtc */
53 1.1 jtc
54 1.1 jtc #include <stdio.h>
55 1.1 jtc #include <stdlib.h>
56 1.1 jtc #include <string.h>
57 1.1 jtc
58 1.1 jtc #define MAXOFILES 20
59 1.1 jtc #define BIGBUFSIZ 5 * BUFSIZ
60 1.1 jtc
61 1.1 jtc struct openfile { /* open file structure */
62 1.1 jtc FILE *fp; /* file pointer */
63 1.1 jtc short eof; /* eof flag */
64 1.1 jtc short pad; /* pad flag for missing columns */
65 1.1 jtc char eol; /* end of line character */
66 1.1 jtc char *sepstring; /* string to print before each line */
67 1.1 jtc char *format; /* printf(3) style string spec. */
68 1.1 jtc } input[MAXOFILES];
69 1.1 jtc
70 1.1 jtc int morefiles; /* set by getargs(), changed by gatherline() */
71 1.1 jtc int nofinalnl; /* normally append \n to each output line */
72 1.1 jtc char line[BIGBUFSIZ];
73 1.1 jtc char *linep;
74 1.1 jtc
75 1.1 jtc void error __P((char *, char *));
76 1.1 jtc char *gatherline __P((struct openfile *));
77 1.1 jtc void getargs __P((char *[]));
78 1.1 jtc char *pad __P((struct openfile *));
79 1.1 jtc
80 1.1 jtc int
81 1.1 jtc main(argc, argv)
82 1.1 jtc int argc;
83 1.1 jtc char *argv[];
84 1.1 jtc {
85 1.1 jtc register struct openfile *ip;
86 1.1 jtc
87 1.1 jtc getargs(argv);
88 1.1 jtc if (!morefiles)
89 1.1 jtc error("lam - laminate files", "");
90 1.1 jtc for (;;) {
91 1.1 jtc linep = line;
92 1.1 jtc for (ip = input; ip->fp != NULL; ip++)
93 1.1 jtc linep = gatherline(ip);
94 1.1 jtc if (!morefiles)
95 1.1 jtc exit(0);
96 1.1 jtc fputs(line, stdout);
97 1.1 jtc fputs(ip->sepstring, stdout);
98 1.1 jtc if (!nofinalnl)
99 1.1 jtc putchar('\n');
100 1.1 jtc }
101 1.1 jtc }
102 1.1 jtc
103 1.1 jtc void
104 1.1 jtc getargs(av)
105 1.1 jtc char *av[];
106 1.1 jtc {
107 1.1 jtc register struct openfile *ip = input;
108 1.1 jtc register char *p;
109 1.1 jtc register char *c;
110 1.1 jtc static char fmtbuf[BUFSIZ];
111 1.1 jtc char *fmtp = fmtbuf;
112 1.1 jtc int P, S, F, T;
113 1.1 jtc
114 1.1 jtc P = S = F = T = 0; /* capitalized options */
115 1.1 jtc while ((p = *++av) != NULL) {
116 1.1 jtc if (*p != '-' || !p[1]) {
117 1.1 jtc morefiles++;
118 1.1 jtc if (*p == '-')
119 1.1 jtc ip->fp = stdin;
120 1.1 jtc else if ((ip->fp = fopen(p, "r")) == NULL) {
121 1.1 jtc perror(p);
122 1.1 jtc exit(1);
123 1.1 jtc }
124 1.1 jtc ip->pad = P;
125 1.1 jtc if (!ip->sepstring)
126 1.1 jtc ip->sepstring = (S ? (ip-1)->sepstring : "");
127 1.1 jtc if (!ip->format)
128 1.1 jtc ip->format = ((P || F) ? (ip-1)->format : "%s");
129 1.1 jtc if (!ip->eol)
130 1.1 jtc ip->eol = (T ? (ip-1)->eol : '\n');
131 1.1 jtc ip++;
132 1.1 jtc continue;
133 1.1 jtc }
134 1.1 jtc switch (*(c = ++p) | 040) {
135 1.1 jtc case 's':
136 1.1 jtc if (*++p || (p = *++av))
137 1.1 jtc ip->sepstring = p;
138 1.1 jtc else
139 1.1 jtc error("Need string after -%s", c);
140 1.1 jtc S = (*c == 'S' ? 1 : 0);
141 1.1 jtc break;
142 1.1 jtc case 't':
143 1.1 jtc if (*++p || (p = *++av))
144 1.1 jtc ip->eol = *p;
145 1.1 jtc else
146 1.1 jtc error("Need character after -%s", c);
147 1.1 jtc T = (*c == 'T' ? 1 : 0);
148 1.1 jtc nofinalnl = 1;
149 1.1 jtc break;
150 1.1 jtc case 'p':
151 1.1 jtc ip->pad = 1;
152 1.1 jtc P = (*c == 'P' ? 1 : 0);
153 1.1 jtc case 'f':
154 1.1 jtc F = (*c == 'F' ? 1 : 0);
155 1.1 jtc if (*++p || (p = *++av)) {
156 1.1 jtc fmtp += strlen(fmtp) + 1;
157 1.1 jtc if (fmtp > fmtbuf + BUFSIZ)
158 1.1 jtc error("No more format space", "");
159 1.1 jtc sprintf(fmtp, "%%%ss", p);
160 1.1 jtc ip->format = fmtp;
161 1.1 jtc }
162 1.1 jtc else
163 1.1 jtc error("Need string after -%s", c);
164 1.1 jtc break;
165 1.1 jtc default:
166 1.1 jtc error("What do you mean by -%s?", c);
167 1.1 jtc break;
168 1.1 jtc }
169 1.1 jtc }
170 1.1 jtc ip->fp = NULL;
171 1.1 jtc if (!ip->sepstring)
172 1.1 jtc ip->sepstring = "";
173 1.1 jtc }
174 1.1 jtc
175 1.1 jtc char *
176 1.1 jtc pad(ip)
177 1.1 jtc struct openfile *ip;
178 1.1 jtc {
179 1.1 jtc register char *p = ip->sepstring;
180 1.1 jtc register char *lp = linep;
181 1.1 jtc
182 1.1 jtc while (*p)
183 1.1 jtc *lp++ = *p++;
184 1.1 jtc if (ip->pad) {
185 1.1 jtc sprintf(lp, ip->format, "");
186 1.1 jtc lp += strlen(lp);
187 1.1 jtc }
188 1.1 jtc return (lp);
189 1.1 jtc }
190 1.1 jtc
191 1.1 jtc char *
192 1.1 jtc gatherline(ip)
193 1.1 jtc struct openfile *ip;
194 1.1 jtc {
195 1.1 jtc char s[BUFSIZ];
196 1.1 jtc register int c;
197 1.1 jtc register char *p;
198 1.1 jtc register char *lp = linep;
199 1.1 jtc char *end = s + BUFSIZ;
200 1.1 jtc
201 1.1 jtc if (ip->eof)
202 1.1 jtc return (pad(ip));
203 1.1 jtc for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
204 1.1 jtc if ((*p = c) == ip->eol)
205 1.1 jtc break;
206 1.1 jtc *p = '\0';
207 1.1 jtc if (c == EOF) {
208 1.1 jtc ip->eof = 1;
209 1.1 jtc if (ip->fp == stdin)
210 1.1 jtc fclose(stdin);
211 1.1 jtc morefiles--;
212 1.1 jtc return (pad(ip));
213 1.1 jtc }
214 1.1 jtc p = ip->sepstring;
215 1.1 jtc while (*p)
216 1.1 jtc *lp++ = *p++;
217 1.1 jtc sprintf(lp, ip->format, s);
218 1.1 jtc lp += strlen(lp);
219 1.1 jtc return (lp);
220 1.1 jtc }
221 1.1 jtc
222 1.1 jtc void
223 1.1 jtc error(msg, s)
224 1.1 jtc char *msg, *s;
225 1.1 jtc {
226 1.1 jtc fprintf(stderr, "lam: ");
227 1.1 jtc fprintf(stderr, msg, s);
228 1.1 jtc fprintf(stderr,
229 1.1 jtc "\nUsage: lam [ -[fp] min.max ] [ -s sepstring ] [ -t c ] file ...\n");
230 1.1 jtc if (strncmp("lam - ", msg, 6) == 0)
231 1.1 jtc fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s",
232 1.1 jtc "-f min.max field widths for file fragments\n",
233 1.1 jtc "-p min.max like -f, but pad missing fragments\n",
234 1.1 jtc "-s sepstring fragment separator\n",
235 1.1 jtc "-t c input line terminator is c, no \\n after output lines\n",
236 1.1 jtc "Capitalized options affect more than one file.\n");
237 1.1 jtc exit(1);
238 1.1 jtc }
239