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