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