1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate *
35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate * contributors.
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate * unexpand - put tabs into a file replacing blanks
44*7c478bd9Sstevel@tonic-gate */
45*7c478bd9Sstevel@tonic-gate #include <stdio.h>
46*7c478bd9Sstevel@tonic-gate #include <limits.h>
47*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
48*7c478bd9Sstevel@tonic-gate #include <locale.h>
49*7c478bd9Sstevel@tonic-gate #include <libintl.h>
50*7c478bd9Sstevel@tonic-gate #include <wchar.h>
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #define INPUT_SIZ LINE_MAX /* POSIX.2 */
53*7c478bd9Sstevel@tonic-gate #define MAX_TABS 100 /* maximum number of tabstops */
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate static int nstops = 0; /* total number of tabstops */
56*7c478bd9Sstevel@tonic-gate static int tabstops[MAX_TABS]; /* the tabstops themselves */
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate static void tabify(wchar_t *, int);
59*7c478bd9Sstevel@tonic-gate static void getstops(const char *);
60*7c478bd9Sstevel@tonic-gate static void usage(void);
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate int
main(argc,argv)63*7c478bd9Sstevel@tonic-gate main(argc, argv)
64*7c478bd9Sstevel@tonic-gate int argc;
65*7c478bd9Sstevel@tonic-gate char *argv[];
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate int flag; /* option flag read by getopt() */
68*7c478bd9Sstevel@tonic-gate int all = 0; /* -a flag */
69*7c478bd9Sstevel@tonic-gate int status = 0;
70*7c478bd9Sstevel@tonic-gate wchar_t input_buf[INPUT_SIZ+1];
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
73*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
74*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
75*7c478bd9Sstevel@tonic-gate #endif
76*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate while ((flag = getopt(argc, argv, "at:")) != EOF) {
79*7c478bd9Sstevel@tonic-gate switch (flag) {
80*7c478bd9Sstevel@tonic-gate case 'a':
81*7c478bd9Sstevel@tonic-gate all++;
82*7c478bd9Sstevel@tonic-gate break;
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate case 't': /* POSIX.2 */
85*7c478bd9Sstevel@tonic-gate all++; /* -t turns on -a */
86*7c478bd9Sstevel@tonic-gate getstops(optarg);
87*7c478bd9Sstevel@tonic-gate break;
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate default:
90*7c478bd9Sstevel@tonic-gate usage();
91*7c478bd9Sstevel@tonic-gate break;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate argc -= optind;
96*7c478bd9Sstevel@tonic-gate argv = &argv[optind];
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate do {
99*7c478bd9Sstevel@tonic-gate if (argc > 0) {
100*7c478bd9Sstevel@tonic-gate if (freopen(argv[0], "r", stdin) == NULL) {
101*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "unexpand: ");
102*7c478bd9Sstevel@tonic-gate perror(argv[0]);
103*7c478bd9Sstevel@tonic-gate status++;
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate argc--, argv++;
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate while (fgetws(input_buf, INPUT_SIZ, stdin) != NULL) {
109*7c478bd9Sstevel@tonic-gate input_buf[INPUT_SIZ] = 0;
110*7c478bd9Sstevel@tonic-gate tabify(input_buf, all);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate } while (argc > 0);
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate return (status);
115*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate void
tabify(wchar_t * ibuf,int all)119*7c478bd9Sstevel@tonic-gate tabify(wchar_t *ibuf, int all)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate wchar_t *cp; /* current position in ibuf */
122*7c478bd9Sstevel@tonic-gate int ocol = 0; /* current output column */
123*7c478bd9Sstevel@tonic-gate int cstop = 0; /* current tabstop */
124*7c478bd9Sstevel@tonic-gate int spaces = 0; /* spaces to convert to tab */
125*7c478bd9Sstevel@tonic-gate int p_col;
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate cp = ibuf;
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate for (;;) {
130*7c478bd9Sstevel@tonic-gate switch (*cp) {
131*7c478bd9Sstevel@tonic-gate case ' ':
132*7c478bd9Sstevel@tonic-gate cp++;
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate spaces++;
135*7c478bd9Sstevel@tonic-gate ocol++;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate if (nstops == 0) { /* default tab = 8 */
138*7c478bd9Sstevel@tonic-gate if ((ocol & 7) != 0)
139*7c478bd9Sstevel@tonic-gate break;
140*7c478bd9Sstevel@tonic-gate } else if (nstops == 1) { /* tab width */
141*7c478bd9Sstevel@tonic-gate if ((ocol % tabstops[0]) != 0)
142*7c478bd9Sstevel@tonic-gate break;
143*7c478bd9Sstevel@tonic-gate } else { /* explicit tabstops */
144*7c478bd9Sstevel@tonic-gate while (cstop < nstops &&
145*7c478bd9Sstevel@tonic-gate ocol > tabstops[cstop])
146*7c478bd9Sstevel@tonic-gate cstop++;
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate if (cstop >= nstops) {
149*7c478bd9Sstevel@tonic-gate (void) putchar(' ');
150*7c478bd9Sstevel@tonic-gate spaces = 0;
151*7c478bd9Sstevel@tonic-gate break;
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate if (ocol != tabstops[cstop])
155*7c478bd9Sstevel@tonic-gate break;
156*7c478bd9Sstevel@tonic-gate cstop++;
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate * if we get to this point, we must be at a
161*7c478bd9Sstevel@tonic-gate * tab stop. if spaces, then write out a tab.
162*7c478bd9Sstevel@tonic-gate */
163*7c478bd9Sstevel@tonic-gate if (spaces > 0) {
164*7c478bd9Sstevel@tonic-gate (void) putchar(((spaces > 1) ? '\t' : ' '));
165*7c478bd9Sstevel@tonic-gate spaces = 0;
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate break;
169*7c478bd9Sstevel@tonic-gate
170*7c478bd9Sstevel@tonic-gate case '\b': /* POSIX.2 */
171*7c478bd9Sstevel@tonic-gate while (spaces-- > 0)
172*7c478bd9Sstevel@tonic-gate (void) putchar(' ');
173*7c478bd9Sstevel@tonic-gate spaces = 0;
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate cp++;
176*7c478bd9Sstevel@tonic-gate (void) putchar('\b');
177*7c478bd9Sstevel@tonic-gate
178*7c478bd9Sstevel@tonic-gate if (--ocol < 0)
179*7c478bd9Sstevel@tonic-gate ocol = 0;
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /* just in case */
182*7c478bd9Sstevel@tonic-gate cstop = 0;
183*7c478bd9Sstevel@tonic-gate break;
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate case '\t':
186*7c478bd9Sstevel@tonic-gate cp++;
187*7c478bd9Sstevel@tonic-gate (void) putchar('\t');
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate /* adjust ocol to current tabstop */
190*7c478bd9Sstevel@tonic-gate if (nstops == 0) {
191*7c478bd9Sstevel@tonic-gate ocol = (ocol + 8) & ~07;
192*7c478bd9Sstevel@tonic-gate } else if (nstops == 1) {
193*7c478bd9Sstevel@tonic-gate ocol += ocol % tabstops[0];
194*7c478bd9Sstevel@tonic-gate } else {
195*7c478bd9Sstevel@tonic-gate if (cstop < nstops &&
196*7c478bd9Sstevel@tonic-gate ocol < tabstops[cstop])
197*7c478bd9Sstevel@tonic-gate ocol = tabstops[cstop++];
198*7c478bd9Sstevel@tonic-gate else
199*7c478bd9Sstevel@tonic-gate ocol++;
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate
202*7c478bd9Sstevel@tonic-gate spaces = 0;
203*7c478bd9Sstevel@tonic-gate break;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate default:
206*7c478bd9Sstevel@tonic-gate while (spaces-- > 0)
207*7c478bd9Sstevel@tonic-gate (void) putchar(' ');
208*7c478bd9Sstevel@tonic-gate spaces = 0;
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate if (*cp == 0 || *cp == '\n' || all == 0) {
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate * either end of input line or -a not set
213*7c478bd9Sstevel@tonic-gate */
214*7c478bd9Sstevel@tonic-gate while (*cp != 0)
215*7c478bd9Sstevel@tonic-gate (void) putwchar(*cp++);
216*7c478bd9Sstevel@tonic-gate return;
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate
219*7c478bd9Sstevel@tonic-gate (void) putwchar(*cp++);
220*7c478bd9Sstevel@tonic-gate if ((p_col = wcwidth(*cp)) < 0)
221*7c478bd9Sstevel@tonic-gate p_col = 0;
222*7c478bd9Sstevel@tonic-gate ocol += p_col;
223*7c478bd9Sstevel@tonic-gate break;
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate static void
getstops(const char * cp)229*7c478bd9Sstevel@tonic-gate getstops(const char *cp)
230*7c478bd9Sstevel@tonic-gate {
231*7c478bd9Sstevel@tonic-gate register int i;
232*7c478bd9Sstevel@tonic-gate
233*7c478bd9Sstevel@tonic-gate for (;;) {
234*7c478bd9Sstevel@tonic-gate i = 0;
235*7c478bd9Sstevel@tonic-gate while (*cp >= '0' && *cp <= '9')
236*7c478bd9Sstevel@tonic-gate i = i * 10 + *cp++ - '0';
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate if (i <= 0 || i > INT_MAX) {
239*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
240*7c478bd9Sstevel@tonic-gate "unexpand: invalid tablist item\n"));
241*7c478bd9Sstevel@tonic-gate usage();
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate if (nstops > 0 && i <= tabstops[nstops-1]) {
245*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
246*7c478bd9Sstevel@tonic-gate "unexpand: tablist must be increasing\n"));
247*7c478bd9Sstevel@tonic-gate usage();
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate if (nstops == MAX_TABS) {
251*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
252*7c478bd9Sstevel@tonic-gate "unexpand: number of tabstops limited to %d\n"),
253*7c478bd9Sstevel@tonic-gate MAX_TABS);
254*7c478bd9Sstevel@tonic-gate usage();
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate tabstops[nstops++] = i;
258*7c478bd9Sstevel@tonic-gate if (*cp == 0)
259*7c478bd9Sstevel@tonic-gate break;
260*7c478bd9Sstevel@tonic-gate if (*cp != ',' && *cp != ' ') {
261*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
262*7c478bd9Sstevel@tonic-gate "unexpand: invalid tablist separator\n"));
263*7c478bd9Sstevel@tonic-gate usage();
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate
266*7c478bd9Sstevel@tonic-gate cp++;
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate
270*7c478bd9Sstevel@tonic-gate static void
usage(void)271*7c478bd9Sstevel@tonic-gate usage(void)
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
274*7c478bd9Sstevel@tonic-gate "usage: unexpand [-a ] [-t tablist] [file ...]\n"));
275*7c478bd9Sstevel@tonic-gate exit(2);
276*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
277*7c478bd9Sstevel@tonic-gate }
278