xref: /freebsd/usr.bin/lam/lam.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
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 (caph_enter() < 0)
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 (caph_rights_limit(fileno(ip->fp), &rights_ro) < 0)
140 				err(1, "unable to limit rights on: %s", p);
141 			ip->pad = P;
142 			if (!ip->sepstring)
143 				ip->sepstring = (S ? (ip-1)->sepstring : "");
144 			if (!ip->format)
145 				ip->format = ((P || F) ? (ip-1)->format : "%s");
146 			if (!ip->eol)
147 				ip->eol = (T ? (ip-1)->eol : '\n');
148 			ip++;
149 			continue;
150 		}
151 		c = ++p;
152 		switch (tolower((unsigned char)*c)) {
153 		case 's':
154 			if (*++p || (p = *++av))
155 				ip->sepstring = p;
156 			else
157 				usage();
158 			S = (*c == 'S' ? 1 : 0);
159 			break;
160 		case 't':
161 			if (*++p || (p = *++av))
162 				ip->eol = *p;
163 			else
164 				usage();
165 			T = (*c == 'T' ? 1 : 0);
166 			nofinalnl = 1;
167 			break;
168 		case 'p':
169 			ip->pad = 1;
170 			P = (*c == 'P' ? 1 : 0);
171 			/* FALLTHROUGH */
172 		case 'f':
173 			F = (*c == 'F' ? 1 : 0);
174 			if (*++p || (p = *++av)) {
175 				fmtp += strlen(fmtp) + 1;
176 				if (fmtp >= fmtbuf + sizeof(fmtbuf))
177 					errx(1, "no more format space");
178 				/* restrict format string to only valid width formatters */
179 				if (strspn(p, "-.0123456789") != strlen(p))
180 					errx(1, "invalid format string `%s'", p);
181 				if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
182 				    >= fmtbuf + sizeof(fmtbuf) - fmtp)
183 					errx(1, "no more format space");
184 				ip->format = fmtp;
185 			}
186 			else
187 				usage();
188 			break;
189 		default:
190 			usage();
191 		}
192 	}
193 	ip->fp = NULL;
194 	if (!ip->sepstring)
195 		ip->sepstring = "";
196 }
197 
198 static char *
199 pad(struct openfile *ip)
200 {
201 	char *lp = linep;
202 
203 	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
204 	lp += strlen(lp);
205 	if (ip->pad) {
206 		snprintf(lp, line + sizeof(line) - lp, ip->format, "");
207 		lp += strlen(lp);
208 	}
209 	return (lp);
210 }
211 
212 static char *
213 gatherline(struct openfile *ip)
214 {
215 	char s[BUFSIZ];
216 	int c;
217 	char *p;
218 	char *lp = linep;
219 	char *end = s + sizeof(s) - 1;
220 
221 	if (ip->eof)
222 		return (pad(ip));
223 	for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
224 		if ((*p = c) == ip->eol)
225 			break;
226 	*p = '\0';
227 	if (c == EOF) {
228 		ip->eof = 1;
229 		if (ip->fp == stdin)
230 			fclose(stdin);
231 		morefiles--;
232 		return (pad(ip));
233 	}
234 	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
235 	lp += strlen(lp);
236 	snprintf(lp, line + sizeof(line) - lp, ip->format, s);
237 	lp += strlen(lp);
238 	return (lp);
239 }
240 
241 static void
242 usage(void)
243 {
244 	fprintf(stderr, "%s\n%s\n",
245 "usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...",
246 "       lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ...");
247 	exit(1);
248 }
249