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