[9070] | 1 | /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc 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. |
---|
[23441] | 15 | * 3. Neither the name of the University nor the names of its contributors |
---|
[9070] | 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 | #ifndef lint |
---|
| 33 | static char copyright[] = |
---|
| 34 | "@(#) Copyright (c) 1993\n\ |
---|
| 35 | The Regents of the University of California. All rights reserved.\n"; |
---|
| 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 | static char rcsid[] = "$NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $"; |
---|
| 43 | #endif /* not lint */ |
---|
| 44 | |
---|
| 45 | /* |
---|
| 46 | * lam - laminate files |
---|
| 47 | * Author: John Kunze, UCB |
---|
| 48 | */ |
---|
| 49 | |
---|
| 50 | #include <stdio.h> |
---|
| 51 | #include <stdlib.h> |
---|
| 52 | #include <string.h> |
---|
| 53 | |
---|
| 54 | #define MAXOFILES 20 |
---|
| 55 | #define BIGBUFSIZ 5 * BUFSIZ |
---|
| 56 | |
---|
| 57 | struct openfile { /* open file structure */ |
---|
| 58 | FILE *fp; /* file pointer */ |
---|
| 59 | short eof; /* eof flag */ |
---|
| 60 | short pad; /* pad flag for missing columns */ |
---|
| 61 | char eol; /* end of line character */ |
---|
| 62 | char *sepstring; /* string to print before each line */ |
---|
| 63 | char *format; /* printf(3) style string spec. */ |
---|
| 64 | } input[MAXOFILES]; |
---|
| 65 | |
---|
| 66 | int morefiles; /* set by getargs(), changed by gatherline() */ |
---|
| 67 | int nofinalnl; /* normally append \n to each output line */ |
---|
| 68 | char line[BIGBUFSIZ]; |
---|
| 69 | char *linep; |
---|
| 70 | |
---|
[9113] | 71 | void error(char *, char *); |
---|
| 72 | char *gatherline(struct openfile *); |
---|
| 73 | void getargs(char *[]); |
---|
| 74 | char *pad(struct openfile *); |
---|
[9070] | 75 | |
---|
| 76 | int |
---|
| 77 | main(argc, argv) |
---|
| 78 | int argc; |
---|
| 79 | char *argv[]; |
---|
| 80 | { |
---|
| 81 | register struct openfile *ip; |
---|
| 82 | |
---|
| 83 | getargs(argv); |
---|
| 84 | if (!morefiles) |
---|
| 85 | error("lam - laminate files", ""); |
---|
| 86 | for (;;) { |
---|
| 87 | linep = line; |
---|
| 88 | for (ip = input; ip->fp != NULL; ip++) |
---|
| 89 | linep = gatherline(ip); |
---|
| 90 | if (!morefiles) |
---|
| 91 | exit(0); |
---|
| 92 | fputs(line, stdout); |
---|
| 93 | fputs(ip->sepstring, stdout); |
---|
| 94 | if (!nofinalnl) |
---|
| 95 | putchar('\n'); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void |
---|
| 100 | getargs(av) |
---|
| 101 | char *av[]; |
---|
| 102 | { |
---|
| 103 | register struct openfile *ip = input; |
---|
| 104 | register char *p; |
---|
| 105 | register char *c; |
---|
| 106 | static char fmtbuf[BUFSIZ]; |
---|
| 107 | char *fmtp = fmtbuf; |
---|
| 108 | int P, S, F, T; |
---|
| 109 | |
---|
| 110 | P = S = F = T = 0; /* capitalized options */ |
---|
| 111 | while ((p = *++av) != NULL) { |
---|
| 112 | if (*p != '-' || !p[1]) { |
---|
| 113 | morefiles++; |
---|
| 114 | if (*p == '-') |
---|
| 115 | ip->fp = stdin; |
---|
| 116 | else if ((ip->fp = fopen(p, "r")) == NULL) { |
---|
| 117 | perror(p); |
---|
| 118 | exit(1); |
---|
| 119 | } |
---|
| 120 | ip->pad = P; |
---|
| 121 | if (!ip->sepstring) |
---|
| 122 | ip->sepstring = (S ? (ip-1)->sepstring : ""); |
---|
| 123 | if (!ip->format) |
---|
| 124 | ip->format = ((P || F) ? (ip-1)->format : "%s"); |
---|
| 125 | if (!ip->eol) |
---|
| 126 | ip->eol = (T ? (ip-1)->eol : '\n'); |
---|
| 127 | ip++; |
---|
| 128 | continue; |
---|
| 129 | } |
---|
| 130 | switch (*(c = ++p) | 040) { |
---|
| 131 | case 's': |
---|
| 132 | if (*++p || (p = *++av)) |
---|
| 133 | ip->sepstring = p; |
---|
| 134 | else |
---|
| 135 | error("Need string after -%s", c); |
---|
| 136 | S = (*c == 'S' ? 1 : 0); |
---|
| 137 | break; |
---|
| 138 | case 't': |
---|
| 139 | if (*++p || (p = *++av)) |
---|
| 140 | ip->eol = *p; |
---|
| 141 | else |
---|
| 142 | error("Need character after -%s", c); |
---|
| 143 | T = (*c == 'T' ? 1 : 0); |
---|
| 144 | nofinalnl = 1; |
---|
| 145 | break; |
---|
| 146 | case 'p': |
---|
| 147 | ip->pad = 1; |
---|
| 148 | P = (*c == 'P' ? 1 : 0); |
---|
| 149 | case 'f': |
---|
| 150 | F = (*c == 'F' ? 1 : 0); |
---|
| 151 | if (*++p || (p = *++av)) { |
---|
| 152 | fmtp += strlen(fmtp) + 1; |
---|
| 153 | if (fmtp > fmtbuf + BUFSIZ) |
---|
| 154 | error("No more format space", ""); |
---|
| 155 | sprintf(fmtp, "%%%ss", p); |
---|
| 156 | ip->format = fmtp; |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | error("Need string after -%s", c); |
---|
| 160 | break; |
---|
| 161 | default: |
---|
| 162 | error("What do you mean by -%s?", c); |
---|
| 163 | break; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | ip->fp = NULL; |
---|
| 167 | if (!ip->sepstring) |
---|
| 168 | ip->sepstring = ""; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | char * |
---|
| 172 | pad(ip) |
---|
| 173 | struct openfile *ip; |
---|
| 174 | { |
---|
| 175 | register char *p = ip->sepstring; |
---|
| 176 | register char *lp = linep; |
---|
| 177 | |
---|
| 178 | while (*p) |
---|
| 179 | *lp++ = *p++; |
---|
| 180 | if (ip->pad) { |
---|
| 181 | sprintf(lp, ip->format, ""); |
---|
| 182 | lp += strlen(lp); |
---|
| 183 | } |
---|
| 184 | return (lp); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | char * |
---|
| 188 | gatherline(ip) |
---|
| 189 | struct openfile *ip; |
---|
| 190 | { |
---|
| 191 | char s[BUFSIZ]; |
---|
| 192 | register int c; |
---|
| 193 | register char *p; |
---|
| 194 | register char *lp = linep; |
---|
| 195 | char *end = s + BUFSIZ; |
---|
| 196 | |
---|
| 197 | if (ip->eof) |
---|
| 198 | return (pad(ip)); |
---|
| 199 | for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++) |
---|
| 200 | if ((*p = c) == ip->eol) |
---|
| 201 | break; |
---|
| 202 | *p = '\0'; |
---|
| 203 | if (c == EOF) { |
---|
| 204 | ip->eof = 1; |
---|
| 205 | if (ip->fp == stdin) |
---|
| 206 | fclose(stdin); |
---|
| 207 | morefiles--; |
---|
| 208 | return (pad(ip)); |
---|
| 209 | } |
---|
| 210 | p = ip->sepstring; |
---|
| 211 | while (*p) |
---|
| 212 | *lp++ = *p++; |
---|
| 213 | sprintf(lp, ip->format, s); |
---|
| 214 | lp += strlen(lp); |
---|
| 215 | return (lp); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | void |
---|
| 219 | error(msg, s) |
---|
| 220 | char *msg, *s; |
---|
| 221 | { |
---|
| 222 | fprintf(stderr, "lam: "); |
---|
| 223 | fprintf(stderr, msg, s); |
---|
| 224 | fprintf(stderr, |
---|
| 225 | "\nUsage: lam [ -[fp] min.max ] [ -s sepstring ] [ -t c ] file ...\n"); |
---|
| 226 | if (strncmp("lam - ", msg, 6) == 0) |
---|
| 227 | fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s", |
---|
| 228 | "-f min.max field widths for file fragments\n", |
---|
| 229 | "-p min.max like -f, but pad missing fragments\n", |
---|
| 230 | "-s sepstring fragment separator\n", |
---|
| 231 | "-t c input line terminator is c, no \\n after output lines\n", |
---|
| 232 | "Capitalized options affect more than one file.\n"); |
---|
| 233 | exit(1); |
---|
| 234 | } |
---|