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