xref: /freebsd/usr.bin/lam/lam.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 static const char copyright[] =
34 "@(#) 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 #endif /* not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 /*
47  *	lam - laminate files
48  *	Author:  John Kunze, UCB
49  */
50 
51 #include <sys/capsicum.h>
52 
53 #include <capsicum_helpers.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #define	MAXOFILES	20
63 #define	BIGBUFSIZ	5 * BUFSIZ
64 
65 static struct openfile {	/* open file structure */
66 	FILE	*fp;		/* file pointer */
67 	short	eof;		/* eof flag */
68 	short	pad;		/* pad flag for missing columns */
69 	char	eol;		/* end of line character */
70 	const char *sepstring;	/* string to print before each line */
71 	const char *format;	/* printf(3) style string spec. */
72 }	input[MAXOFILES];
73 
74 static int	morefiles;	/* set by getargs(), changed by gatherline() */
75 static int	nofinalnl;	/* normally append \n to each output line */
76 static char	line[BIGBUFSIZ];
77 static char	*linep;
78 
79 static char    *gatherline(struct openfile *);
80 static void	getargs(char *[]);
81 static char    *pad(struct openfile *);
82 static void	usage(void);
83 
84 int
85 main(int argc, char *argv[])
86 {
87 	struct	openfile *ip;
88 
89 	if (argc == 1)
90 		usage();
91 	if (caph_limit_stdio() == -1)
92 		err(1, "unable to limit stdio");
93 	getargs(argv);
94 	if (!morefiles)
95 		usage();
96 
97 	/*
98 	 * Cache NLS data, for strerror, for err(3), before entering capability
99 	 * mode.
100 	 */
101 	caph_cache_catpages();
102 	if (cap_enter() < 0 && errno != ENOSYS)
103 		err(1, "unable to enter capability mode");
104 
105 	for (;;) {
106 		linep = line;
107 		for (ip = input; ip->fp != NULL; ip++)
108 			linep = gatherline(ip);
109 		if (!morefiles)
110 			exit(0);
111 		fputs(line, stdout);
112 		fputs(ip->sepstring, stdout);
113 		if (!nofinalnl)
114 			putchar('\n');
115 	}
116 }
117 
118 static void
119 getargs(char *av[])
120 {
121 	struct	openfile *ip = input;
122 	char *p, *c;
123 	static char fmtbuf[BUFSIZ];
124 	char *fmtp = fmtbuf;
125 	int P, S, F, T;
126 	cap_rights_t rights_ro;
127 
128 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT);
129 	P = S = F = T = 0;		/* capitalized options */
130 	while ((p = *++av) != NULL) {
131 		if (*p != '-' || !p[1]) {
132 			if (++morefiles >= MAXOFILES)
133 				errx(1, "too many input files");
134 			if (*p == '-')
135 				ip->fp = stdin;
136 			else if ((ip->fp = fopen(p, "r")) == NULL) {
137 				err(1, "%s", p);
138 			}
139 			if (cap_rights_limit(fileno(ip->fp), &rights_ro) < 0
140 			    && errno != ENOSYS)
141 				err(1, "unable to limit rights on: %s", p);
142 			ip->pad = P;
143 			if (!ip->sepstring)
144 				ip->sepstring = (S ? (ip-1)->sepstring : "");
145 			if (!ip->format)
146 				ip->format = ((P || F) ? (ip-1)->format : "%s");
147 			if (!ip->eol)
148 				ip->eol = (T ? (ip-1)->eol : '\n');
149 			ip++;
150 			continue;
151 		}
152 		c = ++p;
153 		switch (tolower((unsigned char)*c)) {
154 		case 's':
155 			if (*++p || (p = *++av))
156 				ip->sepstring = p;
157 			else
158 				usage();
159 			S = (*c == 'S' ? 1 : 0);
160 			break;
161 		case 't':
162 			if (*++p || (p = *++av))
163 				ip->eol = *p;
164 			else
165 				usage();
166 			T = (*c == 'T' ? 1 : 0);
167 			nofinalnl = 1;
168 			break;
169 		case 'p':
170 			ip->pad = 1;
171 			P = (*c == 'P' ? 1 : 0);
172 			/* FALLTHROUGH */
173 		case 'f':
174 			F = (*c == 'F' ? 1 : 0);
175 			if (*++p || (p = *++av)) {
176 				fmtp += strlen(fmtp) + 1;
177 				if (fmtp >= fmtbuf + sizeof(fmtbuf))
178 					errx(1, "no more format space");
179 				/* restrict format string to only valid width formatters */
180 				if (strspn(p, "-.0123456789") != strlen(p))
181 					errx(1, "invalid format string `%s'", p);
182 				if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
183 				    >= fmtbuf + sizeof(fmtbuf) - fmtp)
184 					errx(1, "no more format space");
185 				ip->format = fmtp;
186 			}
187 			else
188 				usage();
189 			break;
190 		default:
191 			usage();
192 		}
193 	}
194 	ip->fp = NULL;
195 	if (!ip->sepstring)
196 		ip->sepstring = "";
197 }
198 
199 static char *
200 pad(struct openfile *ip)
201 {
202 	char *lp = linep;
203 
204 	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
205 	lp += strlen(lp);
206 	if (ip->pad) {
207 		snprintf(lp, line + sizeof(line) - lp, ip->format, "");
208 		lp += strlen(lp);
209 	}
210 	return (lp);
211 }
212 
213 static char *
214 gatherline(struct openfile *ip)
215 {
216 	char s[BUFSIZ];
217 	int c;
218 	char *p;
219 	char *lp = linep;
220 	char *end = s + sizeof(s) - 1;
221 
222 	if (ip->eof)
223 		return (pad(ip));
224 	for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
225 		if ((*p = c) == ip->eol)
226 			break;
227 	*p = '\0';
228 	if (c == EOF) {
229 		ip->eof = 1;
230 		if (ip->fp == stdin)
231 			fclose(stdin);
232 		morefiles--;
233 		return (pad(ip));
234 	}
235 	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
236 	lp += strlen(lp);
237 	snprintf(lp, line + sizeof(line) - lp, ip->format, s);
238 	lp += strlen(lp);
239 	return (lp);
240 }
241 
242 static void
243 usage(void)
244 {
245 	fprintf(stderr, "%s\n%s\n",
246 "usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...",
247 "       lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ...");
248 	exit(1);
249 }
250