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