xref: /openbsd-src/usr.bin/expand/expand.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: expand.c,v 1.11 2009/10/27 23:59:37 deraadt 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 	/* handle obsolete syntax */
55 	while (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) {
56 		getstops(&argv[1][1]);
57 		argc--; argv++;
58 	}
59 
60 	while ((c = getopt (argc, argv, "t:")) != -1) {
61 		switch (c) {
62 		case 't':
63 			getstops(optarg);
64 			break;
65 		case '?':
66 		default:
67 			usage();
68 			/* NOTREACHED */
69 		}
70 	}
71 	argc -= optind;
72 	argv += optind;
73 
74 	do {
75 		if (argc > 0) {
76 			if (freopen(argv[0], "r", stdin) == NULL)
77 				err(1, "%s", argv[0]);
78 			argc--, argv++;
79 		}
80 		column = 0;
81 		while ((c = getchar()) != EOF) {
82 			switch (c) {
83 			case '\t':
84 				if (nstops == 0) {
85 					do {
86 						putchar(' ');
87 						column++;
88 					} while (column & 07);
89 					continue;
90 				}
91 				if (nstops == 1) {
92 					do {
93 						putchar(' ');
94 						column++;
95 					} while (((column - 1) %
96 					    tabstops[0]) != (tabstops[0] - 1));
97 					continue;
98 				}
99 				for (n = 0; n < nstops; n++)
100 					if (tabstops[n] > column)
101 						break;
102 				if (n == nstops) {
103 					putchar(' ');
104 					column++;
105 					continue;
106 				}
107 				while (column < tabstops[n]) {
108 					putchar(' ');
109 					column++;
110 				}
111 				continue;
112 
113 			case '\b':
114 				if (column)
115 					column--;
116 				putchar('\b');
117 				continue;
118 
119 			default:
120 				putchar(c);
121 				column++;
122 				continue;
123 
124 			case '\n':
125 				putchar(c);
126 				column = 0;
127 				continue;
128 			}
129 		}
130 	} while (argc > 0);
131 	exit(0);
132 }
133 
134 static void
135 getstops(char *cp)
136 {
137 	int i;
138 
139 	nstops = 0;
140 	for (;;) {
141 		i = 0;
142 		while (*cp >= '0' && *cp <= '9')
143 			i = i * 10 + *cp++ - '0';
144 		if (i <= 0 || i > 256) {
145 bad:
146 			errx(1, "Bad tab stop spec");
147 		}
148 		if (nstops > 0 && i <= tabstops[nstops-1])
149 			goto bad;
150 		if (nstops >= sizeof(tabstops) / sizeof(tabstops[0]))
151 			errx(1, "Too many tab stops");
152 		tabstops[nstops++] = i;
153 		if (*cp == 0)
154 			break;
155 		if (*cp != ',' && *cp != ' ')
156 			goto bad;
157 		cp++;
158 	}
159 }
160 
161 static void
162 usage(void)
163 {
164 	extern char *__progname;
165 	fprintf (stderr, "usage: %s [-t tablist] [file ...]\n", __progname);
166 	exit(1);
167 }
168