xref: /netbsd-src/usr.bin/expand/expand.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
1 /*
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)expand.c	5.3 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: expand.c,v 1.3 1993/08/28 02:34:04 jtc Exp $";
43 #endif /* not lint */
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 
49 /*
50  * expand - expand tabs to equivalent spaces
51  */
52 int	nstops;
53 int	tabstops[100];
54 
55 static void getstops();
56 static void usage();
57 
58 int
59 main(argc, argv)
60 	int argc;
61 	char *argv[];
62 {
63 	register int c, column;
64 	register int n;
65 
66 	/* handle obsolete syntax */
67 	while (argc > 1 && argv[1][0] && isdigit(argv[1][1])) {
68 		getstops(&argv[1][1]);
69 		argc--; argv++;
70 	}
71 
72 	while ((c = getopt (argc, argv, "t:")) != -1) {
73 		switch (c) {
74 		case 't':
75 			getstops(optarg);
76 			break;
77 		case '?':
78 		default:
79 			usage();
80 			/* NOTREACHED */
81 		}
82 	}
83 	argc -= optind;
84 	argv += optind;
85 
86 	do {
87 		if (argc > 0) {
88 			if (freopen(argv[0], "r", stdin) == NULL) {
89 				perror(argv[0]);
90 				exit(1);
91 			}
92 			argc--, argv++;
93 		}
94 		column = 0;
95 		while ((c = getchar()) != EOF) {
96 			switch (c) {
97 			case '\t':
98 				if (nstops == 0) {
99 					do {
100 						putchar(' ');
101 						column++;
102 					} while (column & 07);
103 					continue;
104 				}
105 				if (nstops == 1) {
106 					do {
107 						putchar(' ');
108 						column++;
109 					} while (((column - 1) % 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(cp)
149 	register char *cp;
150 {
151 	register int i;
152 
153 	nstops = 0;
154 	for (;;) {
155 		i = 0;
156 		while (*cp >= '0' && *cp <= '9')
157 			i = i * 10 + *cp++ - '0';
158 		if (i <= 0 || i > 256) {
159 bad:
160 			fprintf(stderr, "Bad tab stop spec\n");
161 			exit(1);
162 		}
163 		if (nstops > 0 && i <= tabstops[nstops-1])
164 			goto bad;
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()
176 {
177 	(void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n");
178 	exit(1);
179 }
180