Home | History | Annotate | Line # | Download | only in cut
cut.c revision 1.11
      1  1.11    lukem /*	$NetBSD: cut.c,v 1.11 1997/10/18 13:21:39 lukem Exp $	*/
      2   1.8    glass 
      3   1.1      cgd /*
      4   1.8    glass  * Copyright (c) 1989, 1993
      5   1.8    glass  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * This code is derived from software contributed to Berkeley by
      8   1.1      cgd  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
      9   1.1      cgd  *
     10   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1      cgd  * modification, are permitted provided that the following conditions
     12   1.1      cgd  * are met:
     13   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1      cgd  *    must display the following acknowledgement:
     20   1.1      cgd  *	This product includes software developed by the University of
     21   1.1      cgd  *	California, Berkeley and its contributors.
     22   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1      cgd  *    may be used to endorse or promote products derived from this software
     24   1.1      cgd  *    without specific prior written permission.
     25   1.1      cgd  *
     26   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1      cgd  * SUCH DAMAGE.
     37   1.1      cgd  */
     38   1.1      cgd 
     39  1.11    lukem #include <sys/cdefs.h>
     40   1.1      cgd #ifndef lint
     41  1.11    lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     42  1.11    lukem 	The Regents of the University of California.  All rights reserved.\n");
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45   1.1      cgd #ifndef lint
     46   1.8    glass #if 0
     47   1.9      jtc static char sccsid[] = "@(#)cut.c	8.3 (Berkeley) 5/4/95";
     48   1.8    glass #endif
     49  1.11    lukem __RCSID("$NetBSD: cut.c,v 1.11 1997/10/18 13:21:39 lukem Exp $");
     50   1.1      cgd #endif /* not lint */
     51   1.1      cgd 
     52   1.8    glass #include <ctype.h>
     53   1.8    glass #include <err.h>
     54   1.8    glass #include <errno.h>
     55   1.8    glass #include <limits.h>
     56   1.8    glass #include <locale.h>
     57   1.5      jtc #include <stdio.h>
     58   1.5      jtc #include <stdlib.h>
     59   1.5      jtc #include <string.h>
     60   1.9      jtc #include <unistd.h>
     61   1.1      cgd 
     62   1.1      cgd int	cflag;
     63   1.1      cgd char	dchar;
     64   1.1      cgd int	dflag;
     65   1.1      cgd int	fflag;
     66   1.1      cgd int	sflag;
     67   1.1      cgd 
     68   1.8    glass void	c_cut __P((FILE *, char *));
     69   1.8    glass void	f_cut __P((FILE *, char *));
     70   1.8    glass void	get_list __P((char *));
     71  1.11    lukem int	main __P((int, char **));
     72   1.8    glass void	usage __P((void));
     73   1.8    glass 
     74   1.5      jtc int
     75   1.1      cgd main(argc, argv)
     76   1.1      cgd 	int argc;
     77   1.8    glass 	char *argv[];
     78   1.1      cgd {
     79   1.1      cgd 	FILE *fp;
     80   1.8    glass 	void (*fcn) __P((FILE *, char *));
     81   1.8    glass 	int ch;
     82   1.6      jtc 
     83  1.11    lukem 	fcn = NULL;
     84   1.6      jtc 	setlocale (LC_ALL, "");
     85   1.1      cgd 
     86   1.1      cgd 	dchar = '\t';			/* default delimiter is \t */
     87   1.1      cgd 
     88   1.4      jtc 	/* Since we don't support multi-byte characters, the -c and -b
     89   1.4      jtc 	   options are equivalent, and the -n option is meaningless. */
     90  1.10      mrg 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
     91   1.1      cgd 		switch(ch) {
     92   1.4      jtc 		case 'b':
     93   1.1      cgd 		case 'c':
     94   1.1      cgd 			fcn = c_cut;
     95   1.1      cgd 			get_list(optarg);
     96   1.1      cgd 			cflag = 1;
     97   1.1      cgd 			break;
     98   1.1      cgd 		case 'd':
     99   1.1      cgd 			dchar = *optarg;
    100   1.1      cgd 			dflag = 1;
    101   1.1      cgd 			break;
    102   1.1      cgd 		case 'f':
    103   1.1      cgd 			get_list(optarg);
    104   1.1      cgd 			fcn = f_cut;
    105   1.1      cgd 			fflag = 1;
    106   1.1      cgd 			break;
    107   1.1      cgd 		case 's':
    108   1.1      cgd 			sflag = 1;
    109   1.4      jtc 			break;
    110   1.4      jtc 		case 'n':
    111   1.1      cgd 			break;
    112   1.1      cgd 		case '?':
    113   1.1      cgd 		default:
    114   1.1      cgd 			usage();
    115   1.1      cgd 		}
    116   1.1      cgd 	argc -= optind;
    117   1.1      cgd 	argv += optind;
    118   1.1      cgd 
    119   1.1      cgd 	if (fflag) {
    120   1.1      cgd 		if (cflag)
    121   1.1      cgd 			usage();
    122   1.1      cgd 	} else if (!cflag || dflag || sflag)
    123   1.1      cgd 		usage();
    124   1.1      cgd 
    125   1.1      cgd 	if (*argv)
    126   1.1      cgd 		for (; *argv; ++argv) {
    127   1.8    glass 			if (!(fp = fopen(*argv, "r")))
    128   1.6      jtc 				err(1, "%s", *argv);
    129   1.1      cgd 			fcn(fp, *argv);
    130   1.8    glass 			(void)fclose(fp);
    131   1.1      cgd 		}
    132   1.1      cgd 	else
    133   1.1      cgd 		fcn(stdin, "stdin");
    134   1.1      cgd 	exit(0);
    135   1.1      cgd }
    136   1.1      cgd 
    137   1.1      cgd int autostart, autostop, maxval;
    138   1.1      cgd 
    139   1.1      cgd char positions[_POSIX2_LINE_MAX + 1];
    140   1.1      cgd 
    141   1.8    glass void
    142   1.1      cgd get_list(list)
    143   1.1      cgd 	char *list;
    144   1.1      cgd {
    145  1.11    lukem 	int setautostart, start, stop;
    146  1.11    lukem 	char *pos;
    147   1.6      jtc 	char *p;
    148   1.1      cgd 
    149   1.1      cgd 	/*
    150   1.1      cgd 	 * set a byte in the positions array to indicate if a field or
    151   1.1      cgd 	 * column is to be selected; use +1, it's 1-based, not 0-based.
    152   1.1      cgd 	 * This parser is less restrictive than the Draft 9 POSIX spec.
    153   1.1      cgd 	 * POSIX doesn't allow lists that aren't in increasing order or
    154   1.1      cgd 	 * overlapping lists.  We also handle "-3-5" although there's no
    155   1.1      cgd 	 * real reason too.
    156   1.1      cgd 	 */
    157  1.11    lukem 	for (; (p = strtok(list, ", \t")) != NULL; list = NULL) {
    158   1.1      cgd 		setautostart = start = stop = 0;
    159   1.1      cgd 		if (*p == '-') {
    160   1.1      cgd 			++p;
    161   1.1      cgd 			setautostart = 1;
    162   1.1      cgd 		}
    163   1.1      cgd 		if (isdigit(*p)) {
    164   1.1      cgd 			start = stop = strtol(p, &p, 10);
    165   1.1      cgd 			if (setautostart && start > autostart)
    166   1.1      cgd 				autostart = start;
    167   1.1      cgd 		}
    168   1.1      cgd 		if (*p == '-') {
    169   1.1      cgd 			if (isdigit(p[1]))
    170   1.1      cgd 				stop = strtol(p + 1, &p, 10);
    171   1.1      cgd 			if (*p == '-') {
    172   1.1      cgd 				++p;
    173   1.1      cgd 				if (!autostop || autostop > stop)
    174   1.1      cgd 					autostop = stop;
    175   1.1      cgd 			}
    176   1.1      cgd 		}
    177   1.1      cgd 		if (*p)
    178   1.8    glass 			errx(1, "[-cf] list: illegal list value\n");
    179   1.1      cgd 		if (!stop || !start)
    180   1.8    glass 			errx(1, "[-cf] list: values may not include zero\n");
    181   1.8    glass 		if (stop > _POSIX2_LINE_MAX)
    182   1.8    glass 			errx(1, "[-cf] list: %d too large (max %d)\n",
    183   1.1      cgd 			    stop, _POSIX2_LINE_MAX);
    184   1.1      cgd 		if (maxval < stop)
    185   1.1      cgd 			maxval = stop;
    186   1.1      cgd 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
    187   1.1      cgd 	}
    188   1.1      cgd 
    189   1.1      cgd 	/* overlapping ranges */
    190   1.1      cgd 	if (autostop && maxval > autostop)
    191   1.1      cgd 		maxval = autostop;
    192   1.1      cgd 
    193   1.1      cgd 	/* set autostart */
    194   1.1      cgd 	if (autostart)
    195   1.1      cgd 		memset(positions + 1, '1', autostart);
    196   1.1      cgd }
    197   1.1      cgd 
    198   1.1      cgd /* ARGSUSED */
    199   1.8    glass void
    200   1.1      cgd c_cut(fp, fname)
    201   1.1      cgd 	FILE *fp;
    202   1.1      cgd 	char *fname;
    203   1.1      cgd {
    204  1.11    lukem 	int ch, col;
    205  1.11    lukem 	char *pos;
    206   1.1      cgd 
    207  1.11    lukem 	ch = 0;
    208   1.1      cgd 	for (;;) {
    209   1.1      cgd 		pos = positions + 1;
    210   1.1      cgd 		for (col = maxval; col; --col) {
    211   1.1      cgd 			if ((ch = getc(fp)) == EOF)
    212   1.1      cgd 				return;
    213   1.1      cgd 			if (ch == '\n')
    214   1.1      cgd 				break;
    215   1.1      cgd 			if (*pos++)
    216   1.8    glass 				(void)putchar(ch);
    217   1.1      cgd 		}
    218   1.1      cgd 		if (ch != '\n')
    219   1.1      cgd 			if (autostop)
    220   1.1      cgd 				while ((ch = getc(fp)) != EOF && ch != '\n')
    221   1.8    glass 					(void)putchar(ch);
    222   1.1      cgd 			else
    223   1.1      cgd 				while ((ch = getc(fp)) != EOF && ch != '\n');
    224   1.8    glass 		(void)putchar('\n');
    225   1.1      cgd 	}
    226   1.1      cgd }
    227   1.1      cgd 
    228   1.8    glass void
    229   1.1      cgd f_cut(fp, fname)
    230   1.1      cgd 	FILE *fp;
    231   1.1      cgd 	char *fname;
    232   1.1      cgd {
    233  1.11    lukem 	int ch, field, isdelim;
    234  1.11    lukem 	char *pos, *p, sep;
    235   1.1      cgd 	int output;
    236   1.1      cgd 	char lbuf[_POSIX2_LINE_MAX + 1];
    237   1.1      cgd 
    238   1.7  mycroft 	for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) {
    239   1.7  mycroft 		output = 0;
    240   1.1      cgd 		for (isdelim = 0, p = lbuf;; ++p) {
    241   1.8    glass 			if (!(ch = *p))
    242   1.8    glass 				errx(1, "%s: line too long.\n", fname);
    243   1.1      cgd 			/* this should work if newline is delimiter */
    244   1.1      cgd 			if (ch == sep)
    245   1.1      cgd 				isdelim = 1;
    246   1.1      cgd 			if (ch == '\n') {
    247   1.1      cgd 				if (!isdelim && !sflag)
    248   1.1      cgd 					(void)printf("%s", lbuf);
    249   1.1      cgd 				break;
    250   1.1      cgd 			}
    251   1.1      cgd 		}
    252   1.1      cgd 		if (!isdelim)
    253   1.1      cgd 			continue;
    254   1.1      cgd 
    255   1.1      cgd 		pos = positions + 1;
    256   1.1      cgd 		for (field = maxval, p = lbuf; field; --field, ++pos) {
    257   1.1      cgd 			if (*pos) {
    258   1.1      cgd 				if (output++)
    259   1.8    glass 					(void)putchar(sep);
    260   1.1      cgd 				while ((ch = *p++) != '\n' && ch != sep)
    261   1.8    glass 					(void)putchar(ch);
    262   1.1      cgd 			} else
    263   1.1      cgd 				while ((ch = *p++) != '\n' && ch != sep);
    264   1.1      cgd 			if (ch == '\n')
    265   1.1      cgd 				break;
    266   1.1      cgd 		}
    267   1.1      cgd 		if (ch != '\n')
    268   1.1      cgd 			if (autostop) {
    269   1.1      cgd 				if (output)
    270   1.8    glass 					(void)putchar(sep);
    271   1.1      cgd 				for (; (ch = *p) != '\n'; ++p)
    272   1.8    glass 					(void)putchar(ch);
    273   1.1      cgd 			} else
    274   1.1      cgd 				for (; (ch = *p) != '\n'; ++p);
    275   1.8    glass 		(void)putchar('\n');
    276   1.1      cgd 	}
    277   1.1      cgd }
    278   1.1      cgd 
    279   1.8    glass void
    280   1.1      cgd usage()
    281   1.1      cgd {
    282   1.1      cgd 	(void)fprintf(stderr,
    283   1.1      cgd "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
    284   1.1      cgd 	exit(1);
    285   1.1      cgd }
    286