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