Home | History | Annotate | Line # | Download | only in unexpand
unexpand.c revision 1.14
      1  1.14  christos /*	$NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*-
      4   1.3       jtc  * Copyright (c) 1980, 1993
      5   1.3       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.12       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.13     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     35  1.13     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.3       jtc #if 0
     40   1.3       jtc static char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
     41   1.3       jtc #endif
     42  1.14  christos __RCSID("$NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd /*
     46   1.1       cgd  * unexpand - put tabs into a file replacing blanks
     47   1.1       cgd  */
     48  1.14  christos #include <limits.h>
     49   1.1       cgd #include <stdio.h>
     50   1.7    kleink #include <stdlib.h>
     51   1.5       cgd #include <string.h>
     52   1.7    kleink #include <unistd.h>
     53  1.10       dsl #include <errno.h>
     54  1.10       dsl #include <err.h>
     55  1.14  christos #include <util.h>
     56   1.1       cgd 
     57   1.1       cgd 
     58  1.14  christos #define DSTOP	8
     59  1.14  christos static int	all;
     60  1.14  christos static size_t	nstops;
     61  1.14  christos static size_t	maxstops;
     62  1.14  christos static size_t	*tabstops;
     63  1.14  christos 
     64  1.14  christos static void	tabify(const char *, size_t);
     65  1.14  christos static void	usage(void) __attribute__((__noreturn__));
     66  1.14  christos 
     67  1.14  christos static void
     68  1.14  christos usage(void)
     69  1.14  christos {
     70  1.14  christos     (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
     71  1.14  christos 	getprogname());
     72  1.14  christos     exit(EXIT_FAILURE);
     73  1.14  christos }
     74   1.4       jtc 
     75   1.4       jtc int
     76   1.9       dsl main(int argc, char **argv)
     77   1.1       cgd {
     78  1.14  christos 	int c;
     79  1.14  christos 	char *ep, *tab;
     80  1.14  christos 	char *line;
     81  1.14  christos 	size_t len;
     82  1.14  christos 	unsigned long i;
     83  1.10       dsl 
     84  1.10       dsl 	setprogname(argv[0]);
     85   1.1       cgd 
     86  1.10       dsl 	while ((c = getopt(argc, argv, "at:")) != -1) {
     87   1.7    kleink 		switch (c) {
     88   1.7    kleink 		case 'a':
     89  1.14  christos 			if (nstops)
     90  1.14  christos 				usage();
     91   1.7    kleink 			all++;
     92   1.7    kleink 			break;
     93  1.10       dsl 		case 't':
     94  1.14  christos 			if (all)
     95  1.14  christos 				usage();
     96  1.14  christos 			while ((tab = strsep(&optarg, ", \t")) != NULL) {
     97  1.14  christos 				if (*tab == '\0')
     98  1.14  christos 					continue;
     99  1.14  christos 				errno = 0;
    100  1.14  christos 				i = strtoul(tab, &ep, 0);
    101  1.14  christos 				if (*ep || (errno == ERANGE && i == ULONG_MAX))
    102  1.14  christos 					errx(EXIT_FAILURE,
    103  1.14  christos 					    "Invalid tabstop `%s'", tab);
    104  1.14  christos 				if (nstops >= maxstops) {
    105  1.14  christos 					maxstops += 20;
    106  1.14  christos 					tabstops = erealloc(tabstops, maxstops);
    107  1.14  christos 				}
    108  1.14  christos 				if (nstops && tabstops[nstops - 1] >= (size_t)i)
    109  1.14  christos 					errx(EXIT_FAILURE,
    110  1.14  christos 					    "Bad tabstop spec `%s', must be "
    111  1.14  christos 					    "greater than the previous `%zu'",
    112  1.14  christos 					    tab, tabstops[nstops - 1]);
    113  1.14  christos 				tabstops[nstops++] = i;
    114  1.14  christos 			}
    115  1.10       dsl 			break;
    116   1.7    kleink 		case '?':
    117   1.7    kleink 		default:
    118  1.14  christos 			usage();
    119   1.1       cgd 		}
    120   1.1       cgd 	}
    121   1.7    kleink 	argc -= optind;
    122   1.7    kleink 	argv += optind;
    123   1.7    kleink 
    124  1.14  christos 	for (i = 0; i < nstops; i++)
    125  1.14  christos 		fprintf(stderr, "%lu %zu\n", i, tabstops[i]);
    126  1.14  christos 
    127   1.1       cgd 	do {
    128   1.1       cgd 		if (argc > 0) {
    129  1.14  christos 			if (freopen(argv[0], "r", stdin) == NULL)
    130  1.14  christos 				err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
    131   1.1       cgd 			argc--, argv++;
    132   1.1       cgd 		}
    133  1.14  christos 		while ((line = fgetln(stdin, &len)) != NULL)
    134  1.14  christos 			tabify(line, len);
    135   1.1       cgd 	} while (argc > 0);
    136  1.14  christos 	return EXIT_SUCCESS;
    137   1.1       cgd }
    138   1.1       cgd 
    139  1.14  christos static void
    140  1.14  christos tabify(const char *line, size_t len)
    141   1.1       cgd {
    142  1.14  christos 	const char *e, *p;
    143  1.14  christos 	size_t dcol, ocol, limit, n;
    144   1.1       cgd 
    145  1.14  christos 	dcol = ocol = 0;
    146  1.14  christos 	limit = nstops == 0 ? UINT_MAX : tabstops[nstops - 1] - 1;
    147  1.14  christos 	e = line + len;
    148  1.14  christos 	for (p = line; p < e; p++) {
    149  1.14  christos 		if (*p == ' ') {
    150   1.1       cgd 			dcol++;
    151  1.14  christos 			continue;
    152  1.14  christos 		} else if (*p == '\t') {
    153  1.14  christos 			if (nstops == 0) {
    154  1.14  christos 				dcol = (1 + dcol / DSTOP) * DSTOP;
    155  1.14  christos 				continue;
    156  1.14  christos 			} else {
    157  1.14  christos 				for (n = 0; tabstops[n] - 1 < dcol &&
    158  1.14  christos 				    n < nstops; n++)
    159  1.14  christos 					continue;
    160  1.14  christos 				if (n < nstops - 1 && tabstops[n] - 1 < limit) {
    161  1.14  christos 					dcol = tabstops[n];
    162  1.14  christos 					continue;
    163  1.14  christos 				}
    164  1.14  christos 			}
    165  1.14  christos 		}
    166   1.1       cgd 
    167  1.14  christos 		/* Output our tabs */
    168  1.14  christos 		if (nstops == 0) {
    169  1.14  christos 			while (((ocol + DSTOP) / DSTOP) <= (dcol / DSTOP)) {
    170  1.14  christos 				if (dcol - ocol < 2)
    171  1.14  christos 					break;
    172  1.14  christos 				if (putchar('\t') == EOF)
    173  1.14  christos 					goto out;
    174  1.14  christos 				ocol = (1 + ocol / DSTOP) * DSTOP;
    175   1.1       cgd 			}
    176  1.14  christos 		} else {
    177  1.14  christos 			for (n = 0; tabstops[n] <= ocol && n < nstops; n++)
    178  1.14  christos 				continue;
    179  1.14  christos 			while (tabstops[n] <= dcol && ocol < dcol &&
    180  1.14  christos 			    n < nstops && ocol < limit) {
    181  1.14  christos 				if (putchar('\t') == EOF)
    182  1.14  christos 					goto out;
    183  1.14  christos 				ocol = tabstops[n++];
    184   1.1       cgd 			}
    185  1.14  christos 		}
    186  1.14  christos 
    187  1.14  christos 		/* Output remaining spaces */
    188  1.14  christos 		while (ocol < dcol && ocol < limit) {
    189  1.14  christos 			if (putchar(' ') == EOF)
    190  1.14  christos 				goto out;
    191  1.14  christos 			ocol++;
    192  1.14  christos 		}
    193  1.14  christos 
    194  1.14  christos 		/* Output our char */
    195  1.14  christos 		if (putchar(*p) == EOF)
    196  1.14  christos 			goto out;
    197  1.14  christos 		if (*p == '\b') {
    198  1.14  christos 			if (ocol > 0) {
    199  1.14  christos 				ocol--;
    200  1.14  christos 				dcol--;
    201   1.1       cgd 			}
    202  1.14  christos 		} else {
    203  1.14  christos 			ocol++;
    204  1.14  christos 			dcol++;
    205  1.14  christos 		}
    206  1.14  christos 
    207  1.14  christos 		/* Output remainder of line */
    208  1.14  christos 		if (!all || dcol >= limit) {
    209  1.14  christos 			for (p++; p < e; p++)
    210  1.14  christos 				if (putchar(*p) == EOF)
    211  1.14  christos 					goto out;
    212  1.14  christos 			return;
    213   1.1       cgd 		}
    214   1.1       cgd 	}
    215  1.14  christos 	return;
    216  1.14  christos out:
    217  1.14  christos 	err(EXIT_FAILURE, "write failed");
    218   1.1       cgd }
    219