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