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