Home | History | Annotate | Line # | Download | only in unexpand
unexpand.c revision 1.17
      1 /*	$NetBSD: unexpand.c,v 1.17 2019/09/13 17:26:27 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
     41 #endif
     42 __RCSID("$NetBSD: unexpand.c,v 1.17 2019/09/13 17:26:27 dyoung Exp $");
     43 #endif /* not lint */
     44 
     45 /*
     46  * unexpand - put tabs into a file replacing blanks
     47  */
     48 #include <limits.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 #include <errno.h>
     54 #include <err.h>
     55 #include <util.h>
     56 
     57 
     58 #define DSTOP	8
     59 static int	all;
     60 static size_t	nstops;
     61 static size_t	maxstops;
     62 static size_t	*tabstops;
     63 
     64 static void	tabify(const char *, size_t);
     65 static void	usage(void) __attribute__((__noreturn__));
     66 
     67 static void
     68 usage(void)
     69 {
     70     (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n",
     71 	getprogname());
     72     exit(EXIT_FAILURE);
     73 }
     74 
     75 int
     76 main(int argc, char **argv)
     77 {
     78 	int c;
     79 	char *ep, *tab;
     80 	char *line;
     81 	size_t len;
     82 	unsigned long i;
     83 
     84 	setprogname(argv[0]);
     85 
     86 	while ((c = getopt(argc, argv, "at:")) != -1) {
     87 		switch (c) {
     88 		case 'a':
     89 			all = 1;
     90 			break;
     91 		case 't':
     92 			while ((tab = strsep(&optarg, ", \t")) != NULL) {
     93 				if (*tab == '\0')
     94 					continue;
     95 				errno = 0;
     96 				i = strtoul(tab, &ep, 0);
     97 				if (*ep || (errno == ERANGE && i == ULONG_MAX))
     98 					errx(EXIT_FAILURE,
     99 					    "Invalid tabstop `%s'", tab);
    100 				if (nstops >= maxstops) {
    101 					maxstops += 20;
    102 					tabstops = erealloc(tabstops, maxstops);
    103 				}
    104 				if (nstops && tabstops[nstops - 1] >= (size_t)i)
    105 					errx(EXIT_FAILURE,
    106 					    "Bad tabstop spec `%s', must be "
    107 					    "greater than the previous `%zu'",
    108 					    tab, tabstops[nstops - 1]);
    109 				tabstops[nstops++] = i;
    110 			}
    111 			break;
    112 		case '?':
    113 		default:
    114 			usage();
    115 		}
    116 	}
    117 	argc -= optind;
    118 	argv += optind;
    119 
    120 	do {
    121 		if (argc > 0) {
    122 			if (freopen(argv[0], "r", stdin) == NULL)
    123 				err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
    124 			argc--, argv++;
    125 		}
    126 		while ((line = fgetln(stdin, &len)) != NULL)
    127 			tabify(line, len);
    128 	} while (argc > 0);
    129 	return EXIT_SUCCESS;
    130 }
    131 
    132 static void
    133 tabify(const char *line, size_t len)
    134 {
    135 	const char *e, *p;
    136 	size_t dcol, ocol, limit, n;
    137 
    138 	dcol = ocol = 0;
    139 	limit = nstops <= 1 ? UINT_MAX : tabstops[nstops - 1] - 1;
    140 	e = line + len;
    141 	for (p = line; p < e; p++) {
    142 		if (*p == ' ') {
    143 			dcol++;
    144 			continue;
    145 		} else if (*p == '\t') {
    146 			if (nstops <= 1) {
    147 				size_t stop = (nstops == 0)
    148 				    ? DSTOP
    149 				    : tabstops[0];
    150 				dcol = (1 + dcol / stop) * stop;
    151 				continue;
    152 			} else {
    153 				for (n = 0; n < nstops &&
    154 				    tabstops[n] - 1 < dcol; n++)
    155 					continue;
    156 				if (n < nstops - 1 && tabstops[n] - 1 < limit) {
    157 					dcol = tabstops[n];
    158 					continue;
    159 				}
    160 			}
    161 		}
    162 
    163 		/* Output our tabs */
    164 		if (nstops <= 1) {
    165 			size_t stop = (nstops == 0)
    166 			    ? DSTOP
    167 			    : tabstops[0];
    168 			while (((ocol + stop) / stop) <= (dcol / stop)) {
    169 				if (dcol - ocol < 2)
    170 					break;
    171 				if (putchar('\t') == EOF)
    172 					goto out;
    173 				ocol = (1 + ocol / stop) * stop;
    174 			}
    175 		} else {
    176 			for (n = 0; n < nstops && tabstops[n] <= ocol; n++)
    177 				continue;
    178 			while (n < nstops && tabstops[n] <= dcol && ocol < dcol
    179 			    && ocol < limit) {
    180 				if (putchar('\t') == EOF)
    181 					goto out;
    182 				ocol = tabstops[n++];
    183 			}
    184 		}
    185 
    186 		/* Output remaining spaces */
    187 		while (ocol < dcol && ocol < limit) {
    188 			if (putchar(' ') == EOF)
    189 				goto out;
    190 			ocol++;
    191 		}
    192 
    193 		/* Output our char */
    194 		if (putchar(*p) == EOF)
    195 			goto out;
    196 		if (*p == '\b') {
    197 			if (ocol > 0) {
    198 				ocol--;
    199 				dcol--;
    200 			}
    201 		} else {
    202 			ocol++;
    203 			dcol++;
    204 		}
    205 
    206 		/* Output remainder of line */
    207 		if (!all || dcol >= limit) {
    208 			for (p++; p < e; p++)
    209 				if (putchar(*p) == EOF)
    210 					goto out;
    211 			return;
    212 		}
    213 	}
    214 	return;
    215 out:
    216 	err(EXIT_FAILURE, "write failed");
    217 }
    218