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