expand.c revision 1.12.6.1       1  1.12.6.1       jym /*	$NetBSD: expand.c,v 1.12.6.1 2009/05/13 19:19:49 jym Exp $	*/
      2       1.5       jtc 
      3       1.1       cgd /*
      4       1.5       jtc  * Copyright (c) 1980, 1993
      5       1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32       1.6     lukem #include <sys/cdefs.h>
     33       1.1       cgd #ifndef lint
     34      1.12     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     35      1.12     lukem  The Regents of the University of California.  All rights reserved.");
     36       1.1       cgd #endif /* not lint */
     37       1.1       cgd 
     38       1.1       cgd #ifndef lint
     39       1.5       jtc #if 0
     40       1.5       jtc static char sccsid[] = "@(#)expand.c	8.1 (Berkeley) 6/9/93";
     41       1.5       jtc #endif
     42  1.12.6.1       jym __RCSID("$NetBSD: expand.c,v 1.12.6.1 2009/05/13 19:19:49 jym Exp $");
     43       1.1       cgd #endif /* not lint */
     44       1.1       cgd 
     45       1.1       cgd #include <stdio.h>
     46       1.3       jtc #include <stdlib.h>
     47       1.3       jtc #include <ctype.h>
     48       1.4       jtc #include <unistd.h>
     49      1.10  christos #include <err.h>
     50       1.3       jtc 
     51       1.1       cgd /*
     52       1.1       cgd  * expand - expand tabs to equivalent spaces
     53       1.1       cgd  */
     54      1.10  christos size_t	nstops;
     55      1.10  christos size_t	tabstops[100];
     56       1.1       cgd 
     57      1.10  christos static	void	getstops(const char *);
     58      1.10  christos 	int	main(int, char **);
     59      1.11     perry static	void	usage(void) __dead;
     60       1.3       jtc 
     61       1.3       jtc int
     62      1.10  christos main(int argc, char *argv[])
     63       1.1       cgd {
     64      1.10  christos 	int c;
     65      1.10  christos 	size_t n, column;
     66      1.10  christos 
     67      1.10  christos 	setprogname(argv[0]);
     68       1.1       cgd 
     69       1.3       jtc 	/* handle obsolete syntax */
     70       1.8    kleink 	while (argc > 1 &&
     71       1.8    kleink 	    argv[1][0] == '-' && isdigit((unsigned char)argv[1][1])) {
     72       1.3       jtc 		getstops(&argv[1][1]);
     73       1.3       jtc 		argc--; argv++;
     74       1.3       jtc 	}
     75       1.3       jtc 
     76       1.3       jtc 	while ((c = getopt (argc, argv, "t:")) != -1) {
     77       1.3       jtc 		switch (c) {
     78       1.3       jtc 		case 't':
     79       1.3       jtc 			getstops(optarg);
     80       1.3       jtc 			break;
     81       1.3       jtc 		case '?':
     82       1.3       jtc 		default:
     83       1.3       jtc 			usage();
     84       1.3       jtc 			/* NOTREACHED */
     85       1.3       jtc 		}
     86       1.3       jtc 	}
     87       1.3       jtc 	argc -= optind;
     88       1.3       jtc 	argv += optind;
     89       1.3       jtc 
     90       1.1       cgd 	do {
     91       1.1       cgd 		if (argc > 0) {
     92      1.10  christos 			if (freopen(argv[0], "r", stdin) == NULL)
     93      1.10  christos 				err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
     94       1.1       cgd 			argc--, argv++;
     95       1.1       cgd 		}
     96       1.1       cgd 		column = 0;
     97       1.3       jtc 		while ((c = getchar()) != EOF) {
     98       1.1       cgd 			switch (c) {
     99       1.1       cgd 			case '\t':
    100       1.1       cgd 				if (nstops == 0) {
    101       1.1       cgd 					do {
    102       1.1       cgd 						putchar(' ');
    103       1.1       cgd 						column++;
    104       1.1       cgd 					} while (column & 07);
    105       1.1       cgd 					continue;
    106       1.1       cgd 				}
    107       1.1       cgd 				if (nstops == 1) {
    108       1.1       cgd 					do {
    109       1.1       cgd 						putchar(' ');
    110       1.1       cgd 						column++;
    111      1.10  christos 					} while (((column - 1) % tabstops[0])
    112      1.10  christos 					    != (tabstops[0] - 1));
    113       1.1       cgd 					continue;
    114       1.1       cgd 				}
    115       1.1       cgd 				for (n = 0; n < nstops; n++)
    116       1.1       cgd 					if (tabstops[n] > column)
    117       1.1       cgd 						break;
    118       1.1       cgd 				if (n == nstops) {
    119       1.1       cgd 					putchar(' ');
    120       1.1       cgd 					column++;
    121       1.1       cgd 					continue;
    122       1.1       cgd 				}
    123       1.1       cgd 				while (column < tabstops[n]) {
    124       1.1       cgd 					putchar(' ');
    125       1.1       cgd 					column++;
    126       1.1       cgd 				}
    127       1.1       cgd 				continue;
    128       1.1       cgd 
    129       1.1       cgd 			case '\b':
    130       1.1       cgd 				if (column)
    131       1.1       cgd 					column--;
    132       1.1       cgd 				putchar('\b');
    133       1.1       cgd 				continue;
    134       1.1       cgd 
    135       1.1       cgd 			default:
    136       1.1       cgd 				putchar(c);
    137       1.1       cgd 				column++;
    138       1.1       cgd 				continue;
    139       1.1       cgd 
    140       1.1       cgd 			case '\n':
    141       1.1       cgd 				putchar(c);
    142       1.1       cgd 				column = 0;
    143       1.1       cgd 				continue;
    144       1.1       cgd 			}
    145       1.1       cgd 		}
    146       1.1       cgd 	} while (argc > 0);
    147      1.10  christos 	return EXIT_SUCCESS;
    148       1.1       cgd }
    149       1.1       cgd 
    150       1.3       jtc static void
    151      1.10  christos getstops(const char *spec)
    152       1.1       cgd {
    153       1.6     lukem 	int i;
    154      1.10  christos 	const char *cp = spec;
    155       1.1       cgd 
    156       1.1       cgd 	nstops = 0;
    157       1.1       cgd 	for (;;) {
    158       1.1       cgd 		i = 0;
    159       1.1       cgd 		while (*cp >= '0' && *cp <= '9')
    160       1.1       cgd 			i = i * 10 + *cp++ - '0';
    161      1.10  christos 		if (i <= 0 || i > 256)
    162      1.10  christos 			errx(EXIT_FAILURE, "Too large tab stop spec `%d'", i);
    163  1.12.6.1       jym 		if (nstops > 0 && (size_t)i <= tabstops[nstops-1])
    164      1.10  christos 			errx(EXIT_FAILURE, "Out of order tabstop spec `%d'", i);
    165      1.10  christos 		if (nstops == sizeof(tabstops) / sizeof(tabstops[0]) - 1)
    166      1.10  christos 			errx(EXIT_FAILURE, "Too many tabstops");
    167       1.1       cgd 		tabstops[nstops++] = i;
    168      1.10  christos 		if (*cp == '\0')
    169       1.1       cgd 			break;
    170       1.3       jtc 		if (*cp != ',' && *cp != ' ')
    171      1.10  christos 			errx(EXIT_FAILURE, "Illegal tab stop spec `%s'", spec);
    172       1.3       jtc 		cp++;
    173       1.1       cgd 	}
    174       1.3       jtc }
    175       1.3       jtc 
    176       1.3       jtc static void
    177      1.10  christos usage(void)
    178       1.3       jtc {
    179       1.8    kleink 
    180      1.10  christos 	(void)fprintf(stderr, "Usage: %s [-t tablist] [file ...]\n",
    181      1.10  christos 	    getprogname());
    182       1.8    kleink 	exit(EXIT_FAILURE);
    183       1.1       cgd }
    184