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