xref: /openbsd/usr.bin/expand/expand.c (revision 7b36286a)
1 /*	$OpenBSD: expand.c,v 1.10 2005/12/27 19:19:55 moritz 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 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static const char sccsid[] = "@(#)expand.c	8.1 (Berkeley) 6/9/93";
42 #endif
43 static const char rcsid[] = "$OpenBSD: expand.c,v 1.10 2005/12/27 19:19:55 moritz Exp $";
44 #endif /* not lint */
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <ctype.h>
49 #include <unistd.h>
50 #include <err.h>
51 
52 /*
53  * expand - expand tabs to equivalent spaces
54  */
55 int	nstops;
56 int	tabstops[100];
57 
58 static void getstops(char *);
59 static void usage(void);
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	int c, column;
65 	int n;
66 
67 	/* handle obsolete syntax */
68 	while (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) {
69 		getstops(&argv[1][1]);
70 		argc--; argv++;
71 	}
72 
73 	while ((c = getopt (argc, argv, "t:")) != -1) {
74 		switch (c) {
75 		case 't':
76 			getstops(optarg);
77 			break;
78 		case '?':
79 		default:
80 			usage();
81 			/* NOTREACHED */
82 		}
83 	}
84 	argc -= optind;
85 	argv += optind;
86 
87 	do {
88 		if (argc > 0) {
89 			if (freopen(argv[0], "r", stdin) == NULL)
90 				err(1, "%s", argv[0]);
91 			argc--, argv++;
92 		}
93 		column = 0;
94 		while ((c = getchar()) != EOF) {
95 			switch (c) {
96 			case '\t':
97 				if (nstops == 0) {
98 					do {
99 						putchar(' ');
100 						column++;
101 					} while (column & 07);
102 					continue;
103 				}
104 				if (nstops == 1) {
105 					do {
106 						putchar(' ');
107 						column++;
108 					} while (((column - 1) %
109 					    tabstops[0]) != (tabstops[0] - 1));
110 					continue;
111 				}
112 				for (n = 0; n < nstops; n++)
113 					if (tabstops[n] > column)
114 						break;
115 				if (n == nstops) {
116 					putchar(' ');
117 					column++;
118 					continue;
119 				}
120 				while (column < tabstops[n]) {
121 					putchar(' ');
122 					column++;
123 				}
124 				continue;
125 
126 			case '\b':
127 				if (column)
128 					column--;
129 				putchar('\b');
130 				continue;
131 
132 			default:
133 				putchar(c);
134 				column++;
135 				continue;
136 
137 			case '\n':
138 				putchar(c);
139 				column = 0;
140 				continue;
141 			}
142 		}
143 	} while (argc > 0);
144 	exit(0);
145 }
146 
147 static void
148 getstops(char *cp)
149 {
150 	int i;
151 
152 	nstops = 0;
153 	for (;;) {
154 		i = 0;
155 		while (*cp >= '0' && *cp <= '9')
156 			i = i * 10 + *cp++ - '0';
157 		if (i <= 0 || i > 256) {
158 bad:
159 			errx(1, "Bad tab stop spec");
160 		}
161 		if (nstops > 0 && i <= tabstops[nstops-1])
162 			goto bad;
163 		if (nstops >= sizeof(tabstops) / sizeof(tabstops[0]))
164 			errx(1, "Too many tab stops");
165 		tabstops[nstops++] = i;
166 		if (*cp == 0)
167 			break;
168 		if (*cp != ',' && *cp != ' ')
169 			goto bad;
170 		cp++;
171 	}
172 }
173 
174 static void
175 usage(void)
176 {
177 	extern char *__progname;
178 	fprintf (stderr, "usage: %s [-t tablist] [file ...]\n", __progname);
179 	exit(1);
180 }
181