Home | History | Annotate | Line # | Download | only in lam
lam.c revision 1.6
      1  1.6   lukem /*	$NetBSD: lam.c,v 1.6 2008/07/21 14:19:23 lukem 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.6   lukem __RCSID("$NetBSD: lam.c,v 1.6 2008/07/21 14:19:23 lukem 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.1     jtc 	char	*sepstring;	/* string to print before each line */
     65  1.1     jtc 	char	*format;	/* printf(3) style string spec. */
     66  1.1     jtc }	input[MAXOFILES];
     67  1.1     jtc 
     68  1.1     jtc int	morefiles;		/* set by getargs(), changed by gatherline() */
     69  1.1     jtc int	nofinalnl;		/* normally append \n to each output line */
     70  1.1     jtc char	line[BIGBUFSIZ];
     71  1.1     jtc char	*linep;
     72  1.1     jtc 
     73  1.1     jtc void	 error __P((char *, char *));
     74  1.1     jtc char	*gatherline __P((struct openfile *));
     75  1.1     jtc void	 getargs __P((char *[]));
     76  1.3   lukem int	 main __P((int, char **));
     77  1.1     jtc char	*pad __P((struct openfile *));
     78  1.1     jtc 
     79  1.1     jtc int
     80  1.1     jtc main(argc, argv)
     81  1.1     jtc 	int argc;
     82  1.1     jtc 	char *argv[];
     83  1.1     jtc {
     84  1.3   lukem 	struct	openfile *ip;
     85  1.1     jtc 
     86  1.1     jtc 	getargs(argv);
     87  1.1     jtc 	if (!morefiles)
     88  1.1     jtc 		error("lam - laminate files", "");
     89  1.1     jtc 	for (;;) {
     90  1.1     jtc 		linep = line;
     91  1.1     jtc 		for (ip = input; ip->fp != NULL; ip++)
     92  1.1     jtc 			linep = gatherline(ip);
     93  1.1     jtc 		if (!morefiles)
     94  1.1     jtc 			exit(0);
     95  1.1     jtc 		fputs(line, stdout);
     96  1.1     jtc 		fputs(ip->sepstring, stdout);
     97  1.1     jtc 		if (!nofinalnl)
     98  1.1     jtc 			putchar('\n');
     99  1.1     jtc 	}
    100  1.1     jtc }
    101  1.1     jtc 
    102  1.1     jtc void
    103  1.1     jtc getargs(av)
    104  1.1     jtc 	char *av[];
    105  1.1     jtc {
    106  1.5  daniel 	struct openfile *ip = input;
    107  1.5  daniel 	char *p, *c;
    108  1.1     jtc 	static char fmtbuf[BUFSIZ];
    109  1.1     jtc 	char *fmtp = fmtbuf;
    110  1.1     jtc 	int P, S, F, T;
    111  1.1     jtc 
    112  1.1     jtc 	P = S = F = T = 0;		/* capitalized options */
    113  1.1     jtc 	while ((p = *++av) != NULL) {
    114  1.1     jtc 		if (*p != '-' || !p[1]) {
    115  1.5  daniel 			if (++morefiles >= MAXOFILES)
    116  1.5  daniel 				errx(1, "too many input files");
    117  1.1     jtc 			if (*p == '-')
    118  1.1     jtc 				ip->fp = stdin;
    119  1.3   lukem 			else if ((ip->fp = fopen(p, "r")) == NULL)
    120  1.3   lukem 				errx(1, "open %s", p);
    121  1.1     jtc 			ip->pad = P;
    122  1.1     jtc 			if (!ip->sepstring)
    123  1.1     jtc 				ip->sepstring = (S ? (ip-1)->sepstring : "");
    124  1.1     jtc 			if (!ip->format)
    125  1.1     jtc 				ip->format = ((P || F) ? (ip-1)->format : "%s");
    126  1.1     jtc 			if (!ip->eol)
    127  1.1     jtc 				ip->eol = (T ? (ip-1)->eol : '\n');
    128  1.1     jtc 			ip++;
    129  1.1     jtc 			continue;
    130  1.1     jtc 		}
    131  1.5  daniel 		c = ++p;
    132  1.5  daniel 		switch (tolower((unsigned char) *c)) {
    133  1.1     jtc 		case 's':
    134  1.1     jtc 			if (*++p || (p = *++av))
    135  1.1     jtc 				ip->sepstring = p;
    136  1.1     jtc 			else
    137  1.1     jtc 				error("Need string after -%s", c);
    138  1.1     jtc 			S = (*c == 'S' ? 1 : 0);
    139  1.1     jtc 			break;
    140  1.1     jtc 		case 't':
    141  1.1     jtc 			if (*++p || (p = *++av))
    142  1.1     jtc 				ip->eol = *p;
    143  1.1     jtc 			else
    144  1.1     jtc 				error("Need character after -%s", c);
    145  1.1     jtc 			T = (*c == 'T' ? 1 : 0);
    146  1.1     jtc 			nofinalnl = 1;
    147  1.1     jtc 			break;
    148  1.1     jtc 		case 'p':
    149  1.1     jtc 			ip->pad = 1;
    150  1.1     jtc 			P = (*c == 'P' ? 1 : 0);
    151  1.5  daniel 			/* FALLTHROUGH */
    152  1.1     jtc 		case 'f':
    153  1.1     jtc 			F = (*c == 'F' ? 1 : 0);
    154  1.1     jtc 			if (*++p || (p = *++av)) {
    155  1.1     jtc 				fmtp += strlen(fmtp) + 1;
    156  1.5  daniel 				if (fmtp >= fmtbuf + sizeof(fmtbuf))
    157  1.5  daniel 					errx(1, "no more format space");
    158  1.5  daniel 				/* restrict format string to only valid width formatters */
    159  1.5  daniel 				if (strspn(p, "-.0123456789") != strlen(p))
    160  1.5  daniel 					errx(1, "invalid format string `%s'", p);
    161  1.5  daniel 				if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
    162  1.5  daniel 					>= fmtbuf + sizeof(fmtbuf) - fmtp)
    163  1.5  daniel 						errx(1, "no more format space");
    164  1.1     jtc 				sprintf(fmtp, "%%%ss", p);
    165  1.1     jtc 				ip->format = fmtp;
    166  1.1     jtc 			}
    167  1.1     jtc 			else
    168  1.1     jtc 				error("Need string after -%s", c);
    169  1.1     jtc 			break;
    170  1.1     jtc 		default:
    171  1.1     jtc 			error("What do you mean by -%s?", c);
    172  1.1     jtc 			break;
    173  1.1     jtc 		}
    174  1.1     jtc 	}
    175  1.1     jtc 	ip->fp = NULL;
    176  1.1     jtc 	if (!ip->sepstring)
    177  1.1     jtc 		ip->sepstring = "";
    178  1.1     jtc }
    179  1.1     jtc 
    180  1.1     jtc char *
    181  1.1     jtc pad(ip)
    182  1.1     jtc 	struct openfile *ip;
    183  1.1     jtc {
    184  1.3   lukem 	char *lp = linep;
    185  1.1     jtc 
    186  1.5  daniel 	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
    187  1.5  daniel 	lp += strlen(lp);
    188  1.1     jtc 	if (ip->pad) {
    189  1.5  daniel 		snprintf(lp, line + sizeof(line) - lp, ip->format, "");
    190  1.1     jtc 		lp += strlen(lp);
    191  1.1     jtc 	}
    192  1.1     jtc 	return (lp);
    193  1.1     jtc }
    194  1.1     jtc 
    195  1.1     jtc char *
    196  1.1     jtc gatherline(ip)
    197  1.1     jtc 	struct openfile *ip;
    198  1.1     jtc {
    199  1.1     jtc 	char s[BUFSIZ];
    200  1.3   lukem 	int c;
    201  1.3   lukem 	char *p;
    202  1.3   lukem 	char *lp = linep;
    203  1.5  daniel 	char *end = s + sizeof(s) - 1;
    204  1.1     jtc 
    205  1.1     jtc 	if (ip->eof)
    206  1.1     jtc 		return (pad(ip));
    207  1.1     jtc 	for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
    208  1.1     jtc 		if ((*p = c) == ip->eol)
    209  1.1     jtc 			break;
    210  1.1     jtc 	*p = '\0';
    211  1.1     jtc 	if (c == EOF) {
    212  1.1     jtc 		ip->eof = 1;
    213  1.1     jtc 		if (ip->fp == stdin)
    214  1.1     jtc 			fclose(stdin);
    215  1.1     jtc 		morefiles--;
    216  1.1     jtc 		return (pad(ip));
    217  1.1     jtc 	}
    218  1.5  daniel 	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
    219  1.5  daniel 	lp += strlen(lp);
    220  1.5  daniel 	snprintf(lp, line + sizeof(line) - lp, ip->format, s);
    221  1.1     jtc 	lp += strlen(lp);
    222  1.1     jtc 	return (lp);
    223  1.1     jtc }
    224  1.1     jtc 
    225  1.1     jtc void
    226  1.1     jtc error(msg, s)
    227  1.1     jtc 	char *msg, *s;
    228  1.1     jtc {
    229  1.3   lukem 	warnx(msg, s);
    230  1.1     jtc 	fprintf(stderr,
    231  1.1     jtc "\nUsage:  lam [ -[fp] min.max ] [ -s sepstring ] [ -t c ] file ...\n");
    232  1.1     jtc 	if (strncmp("lam - ", msg, 6) == 0)
    233  1.1     jtc 		fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s",
    234  1.1     jtc 		    "-f min.max	field widths for file fragments\n",
    235  1.1     jtc 		    "-p min.max	like -f, but pad missing fragments\n",
    236  1.1     jtc 		    "-s sepstring	fragment separator\n",
    237  1.1     jtc "-t c		input line terminator is c, no \\n after output lines\n",
    238  1.1     jtc 		    "Capitalized options affect more than one file.\n");
    239  1.1     jtc 	exit(1);
    240  1.1     jtc }
    241