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