Home | History | Annotate | Line # | Download | only in cut
cut.c revision 1.8
      1  1.8    glass /*	$NetBSD: cut.c,v 1.8 1995/03/26 20:51:27 glass 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.1      cgd #ifndef lint
     40  1.8    glass static char copyright[] =
     41  1.8    glass "@(#) Copyright (c) 1989, 1993\n\
     42  1.8    glass 	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.8    glass static char sccsid[] = "@(#)cut.c	8.1 (Berkeley) 6/6/93";
     48  1.8    glass #else
     49  1.8    glass static char rcsid[] = "$NetBSD: cut.c,v 1.8 1995/03/26 20:51:27 glass Exp $";
     50  1.8    glass #endif
     51  1.1      cgd #endif /* not lint */
     52  1.1      cgd 
     53  1.8    glass #include <ctype.h>
     54  1.8    glass #include <err.h>
     55  1.8    glass #include <errno.h>
     56  1.8    glass #include <limits.h>
     57  1.8    glass #include <locale.h>
     58  1.5      jtc #include <stdio.h>
     59  1.5      jtc #include <stdlib.h>
     60  1.5      jtc #include <string.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.8    glass void	usage __P((void));
     72  1.8    glass 
     73  1.5      jtc int
     74  1.1      cgd main(argc, argv)
     75  1.1      cgd 	int argc;
     76  1.8    glass 	char *argv[];
     77  1.1      cgd {
     78  1.1      cgd 	FILE *fp;
     79  1.8    glass 	void (*fcn) __P((FILE *, char *));
     80  1.8    glass 	int ch;
     81  1.6      jtc 
     82  1.6      jtc 	setlocale (LC_ALL, "");
     83  1.1      cgd 
     84  1.1      cgd 	dchar = '\t';			/* default delimiter is \t */
     85  1.1      cgd 
     86  1.4      jtc 	/* Since we don't support multi-byte characters, the -c and -b
     87  1.4      jtc 	   options are equivalent, and the -n option is meaningless. */
     88  1.4      jtc 	while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != EOF)
     89  1.1      cgd 		switch(ch) {
     90  1.4      jtc 		case 'b':
     91  1.1      cgd 		case 'c':
     92  1.1      cgd 			fcn = c_cut;
     93  1.1      cgd 			get_list(optarg);
     94  1.1      cgd 			cflag = 1;
     95  1.1      cgd 			break;
     96  1.1      cgd 		case 'd':
     97  1.1      cgd 			dchar = *optarg;
     98  1.1      cgd 			dflag = 1;
     99  1.1      cgd 			break;
    100  1.1      cgd 		case 'f':
    101  1.1      cgd 			get_list(optarg);
    102  1.1      cgd 			fcn = f_cut;
    103  1.1      cgd 			fflag = 1;
    104  1.1      cgd 			break;
    105  1.1      cgd 		case 's':
    106  1.1      cgd 			sflag = 1;
    107  1.4      jtc 			break;
    108  1.4      jtc 		case 'n':
    109  1.1      cgd 			break;
    110  1.1      cgd 		case '?':
    111  1.1      cgd 		default:
    112  1.1      cgd 			usage();
    113  1.1      cgd 		}
    114  1.1      cgd 	argc -= optind;
    115  1.1      cgd 	argv += optind;
    116  1.1      cgd 
    117  1.1      cgd 	if (fflag) {
    118  1.1      cgd 		if (cflag)
    119  1.1      cgd 			usage();
    120  1.1      cgd 	} else if (!cflag || dflag || sflag)
    121  1.1      cgd 		usage();
    122  1.1      cgd 
    123  1.1      cgd 	if (*argv)
    124  1.1      cgd 		for (; *argv; ++argv) {
    125  1.8    glass 			if (!(fp = fopen(*argv, "r")))
    126  1.6      jtc 				err(1, "%s", *argv);
    127  1.1      cgd 			fcn(fp, *argv);
    128  1.8    glass 			(void)fclose(fp);
    129  1.1      cgd 		}
    130  1.1      cgd 	else
    131  1.1      cgd 		fcn(stdin, "stdin");
    132  1.1      cgd 	exit(0);
    133  1.1      cgd }
    134  1.1      cgd 
    135  1.1      cgd int autostart, autostop, maxval;
    136  1.1      cgd 
    137  1.1      cgd char positions[_POSIX2_LINE_MAX + 1];
    138  1.1      cgd 
    139  1.8    glass void
    140  1.1      cgd get_list(list)
    141  1.1      cgd 	char *list;
    142  1.1      cgd {
    143  1.8    glass 	register int setautostart, start, stop;
    144  1.1      cgd 	register char *pos;
    145  1.6      jtc 	char *p;
    146  1.1      cgd 
    147  1.1      cgd 	/*
    148  1.1      cgd 	 * set a byte in the positions array to indicate if a field or
    149  1.1      cgd 	 * column is to be selected; use +1, it's 1-based, not 0-based.
    150  1.1      cgd 	 * This parser is less restrictive than the Draft 9 POSIX spec.
    151  1.1      cgd 	 * POSIX doesn't allow lists that aren't in increasing order or
    152  1.1      cgd 	 * overlapping lists.  We also handle "-3-5" although there's no
    153  1.1      cgd 	 * real reason too.
    154  1.1      cgd 	 */
    155  1.1      cgd 	for (; p = strtok(list, ", \t"); list = NULL) {
    156  1.1      cgd 		setautostart = start = stop = 0;
    157  1.1      cgd 		if (*p == '-') {
    158  1.1      cgd 			++p;
    159  1.1      cgd 			setautostart = 1;
    160  1.1      cgd 		}
    161  1.1      cgd 		if (isdigit(*p)) {
    162  1.1      cgd 			start = stop = strtol(p, &p, 10);
    163  1.1      cgd 			if (setautostart && start > autostart)
    164  1.1      cgd 				autostart = start;
    165  1.1      cgd 		}
    166  1.1      cgd 		if (*p == '-') {
    167  1.1      cgd 			if (isdigit(p[1]))
    168  1.1      cgd 				stop = strtol(p + 1, &p, 10);
    169  1.1      cgd 			if (*p == '-') {
    170  1.1      cgd 				++p;
    171  1.1      cgd 				if (!autostop || autostop > stop)
    172  1.1      cgd 					autostop = stop;
    173  1.1      cgd 			}
    174  1.1      cgd 		}
    175  1.1      cgd 		if (*p)
    176  1.8    glass 			errx(1, "[-cf] list: illegal list value\n");
    177  1.1      cgd 		if (!stop || !start)
    178  1.8    glass 			errx(1, "[-cf] list: values may not include zero\n");
    179  1.8    glass 		if (stop > _POSIX2_LINE_MAX)
    180  1.8    glass 			errx(1, "[-cf] list: %d too large (max %d)\n",
    181  1.1      cgd 			    stop, _POSIX2_LINE_MAX);
    182  1.1      cgd 		if (maxval < stop)
    183  1.1      cgd 			maxval = stop;
    184  1.1      cgd 		for (pos = positions + start; start++ <= stop; *pos++ = 1);
    185  1.1      cgd 	}
    186  1.1      cgd 
    187  1.1      cgd 	/* overlapping ranges */
    188  1.1      cgd 	if (autostop && maxval > autostop)
    189  1.1      cgd 		maxval = autostop;
    190  1.1      cgd 
    191  1.1      cgd 	/* set autostart */
    192  1.1      cgd 	if (autostart)
    193  1.1      cgd 		memset(positions + 1, '1', autostart);
    194  1.1      cgd }
    195  1.1      cgd 
    196  1.1      cgd /* ARGSUSED */
    197  1.8    glass void
    198  1.1      cgd c_cut(fp, fname)
    199  1.1      cgd 	FILE *fp;
    200  1.1      cgd 	char *fname;
    201  1.1      cgd {
    202  1.1      cgd 	register int ch, col;
    203  1.1      cgd 	register char *pos;
    204  1.1      cgd 
    205  1.1      cgd 	for (;;) {
    206  1.1      cgd 		pos = positions + 1;
    207  1.1      cgd 		for (col = maxval; col; --col) {
    208  1.1      cgd 			if ((ch = getc(fp)) == EOF)
    209  1.1      cgd 				return;
    210  1.1      cgd 			if (ch == '\n')
    211  1.1      cgd 				break;
    212  1.1      cgd 			if (*pos++)
    213  1.8    glass 				(void)putchar(ch);
    214  1.1      cgd 		}
    215  1.1      cgd 		if (ch != '\n')
    216  1.1      cgd 			if (autostop)
    217  1.1      cgd 				while ((ch = getc(fp)) != EOF && ch != '\n')
    218  1.8    glass 					(void)putchar(ch);
    219  1.1      cgd 			else
    220  1.1      cgd 				while ((ch = getc(fp)) != EOF && ch != '\n');
    221  1.8    glass 		(void)putchar('\n');
    222  1.1      cgd 	}
    223  1.1      cgd }
    224  1.1      cgd 
    225  1.8    glass void
    226  1.1      cgd f_cut(fp, fname)
    227  1.1      cgd 	FILE *fp;
    228  1.1      cgd 	char *fname;
    229  1.1      cgd {
    230  1.1      cgd 	register int ch, field, isdelim;
    231  1.1      cgd 	register char *pos, *p, sep;
    232  1.1      cgd 	int output;
    233  1.1      cgd 	char lbuf[_POSIX2_LINE_MAX + 1];
    234  1.1      cgd 
    235  1.7  mycroft 	for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) {
    236  1.7  mycroft 		output = 0;
    237  1.1      cgd 		for (isdelim = 0, p = lbuf;; ++p) {
    238  1.8    glass 			if (!(ch = *p))
    239  1.8    glass 				errx(1, "%s: line too long.\n", fname);
    240  1.1      cgd 			/* this should work if newline is delimiter */
    241  1.1      cgd 			if (ch == sep)
    242  1.1      cgd 				isdelim = 1;
    243  1.1      cgd 			if (ch == '\n') {
    244  1.1      cgd 				if (!isdelim && !sflag)
    245  1.1      cgd 					(void)printf("%s", lbuf);
    246  1.1      cgd 				break;
    247  1.1      cgd 			}
    248  1.1      cgd 		}
    249  1.1      cgd 		if (!isdelim)
    250  1.1      cgd 			continue;
    251  1.1      cgd 
    252  1.1      cgd 		pos = positions + 1;
    253  1.1      cgd 		for (field = maxval, p = lbuf; field; --field, ++pos) {
    254  1.1      cgd 			if (*pos) {
    255  1.1      cgd 				if (output++)
    256  1.8    glass 					(void)putchar(sep);
    257  1.1      cgd 				while ((ch = *p++) != '\n' && ch != sep)
    258  1.8    glass 					(void)putchar(ch);
    259  1.1      cgd 			} else
    260  1.1      cgd 				while ((ch = *p++) != '\n' && ch != sep);
    261  1.1      cgd 			if (ch == '\n')
    262  1.1      cgd 				break;
    263  1.1      cgd 		}
    264  1.1      cgd 		if (ch != '\n')
    265  1.1      cgd 			if (autostop) {
    266  1.1      cgd 				if (output)
    267  1.8    glass 					(void)putchar(sep);
    268  1.1      cgd 				for (; (ch = *p) != '\n'; ++p)
    269  1.8    glass 					(void)putchar(ch);
    270  1.1      cgd 			} else
    271  1.1      cgd 				for (; (ch = *p) != '\n'; ++p);
    272  1.8    glass 		(void)putchar('\n');
    273  1.1      cgd 	}
    274  1.1      cgd }
    275  1.1      cgd 
    276  1.8    glass void
    277  1.1      cgd usage()
    278  1.1      cgd {
    279  1.1      cgd 	(void)fprintf(stderr,
    280  1.1      cgd "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
    281  1.1      cgd 	exit(1);
    282  1.1      cgd }
    283