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