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