4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * unexpand - put tabs into a file replacing blanks
52 #define INPUT_SIZ LINE_MAX /* POSIX.2 */
53 #define MAX_TABS 100 /* maximum number of tabstops */
55 static int nstops
= 0; /* total number of tabstops */
56 static int tabstops
[MAX_TABS
]; /* the tabstops themselves */
58 static void tabify(wchar_t *, int);
59 static void getstops(const char *);
60 static void usage(void);
67 int flag
; /* option flag read by getopt() */
68 int all
= 0; /* -a flag */
70 wchar_t input_buf
[INPUT_SIZ
+1];
72 (void) setlocale(LC_ALL
, "");
73 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
74 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
76 (void) textdomain(TEXT_DOMAIN
);
78 while ((flag
= getopt(argc
, argv
, "at:")) != EOF
) {
84 case 't': /* POSIX.2 */
85 all
++; /* -t turns on -a */
100 if (freopen(argv
[0], "r", stdin
) == NULL
) {
101 (void) fprintf(stderr
, "unexpand: ");
108 while (fgetws(input_buf
, INPUT_SIZ
, stdin
) != NULL
) {
109 input_buf
[INPUT_SIZ
] = 0;
110 tabify(input_buf
, all
);
119 tabify(wchar_t *ibuf
, int all
)
121 wchar_t *cp
; /* current position in ibuf */
122 int ocol
= 0; /* current output column */
123 int cstop
= 0; /* current tabstop */
124 int spaces
= 0; /* spaces to convert to tab */
137 if (nstops
== 0) { /* default tab = 8 */
140 } else if (nstops
== 1) { /* tab width */
141 if ((ocol
% tabstops
[0]) != 0)
143 } else { /* explicit tabstops */
144 while (cstop
< nstops
&&
145 ocol
> tabstops
[cstop
])
148 if (cstop
>= nstops
) {
154 if (ocol
!= tabstops
[cstop
])
160 * if we get to this point, we must be at a
161 * tab stop. if spaces, then write out a tab.
164 (void) putchar(((spaces
> 1) ? '\t' : ' '));
170 case '\b': /* POSIX.2 */
176 (void) putchar('\b');
187 (void) putchar('\t');
189 /* adjust ocol to current tabstop */
191 ocol
= (ocol
+ 8) & ~07;
192 } else if (nstops
== 1) {
193 ocol
+= ocol
% tabstops
[0];
195 if (cstop
< nstops
&&
196 ocol
< tabstops
[cstop
])
197 ocol
= tabstops
[cstop
++];
210 if (*cp
== 0 || *cp
== '\n' || all
== 0) {
212 * either end of input line or -a not set
215 (void) putwchar(*cp
++);
219 (void) putwchar(*cp
++);
220 if ((p_col
= wcwidth(*cp
)) < 0)
229 getstops(const char *cp
)
235 while (*cp
>= '0' && *cp
<= '9')
236 i
= i
* 10 + *cp
++ - '0';
238 if (i
<= 0 || i
> INT_MAX
) {
239 (void) fprintf(stderr
, gettext(
240 "unexpand: invalid tablist item\n"));
244 if (nstops
> 0 && i
<= tabstops
[nstops
-1]) {
245 (void) fprintf(stderr
, gettext(
246 "unexpand: tablist must be increasing\n"));
250 if (nstops
== MAX_TABS
) {
251 (void) fprintf(stderr
, gettext(
252 "unexpand: number of tabstops limited to %d\n"),
257 tabstops
[nstops
++] = i
;
260 if (*cp
!= ',' && *cp
!= ' ') {
261 (void) fprintf(stderr
, gettext(
262 "unexpand: invalid tablist separator\n"));
273 (void) fprintf(stderr
, gettext(
274 "usage: unexpand [-a ] [-t tablist] [file ...]\n"));