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