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