5172 tftpd service is not registered by default and its manual is giving misleading...
[unleashed.git] / usr / src / cmd / expand / unexpand.c
blob82de1608d947fbd974eaee4daa429b511680311a
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * unexpand - put tabs into a file replacing blanks
45 #include <stdio.h>
46 #include <limits.h>
47 #include <stdlib.h>
48 #include <locale.h>
49 #include <libintl.h>
50 #include <wchar.h>
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);
62 int
63 main(argc, argv)
64 int argc;
65 char *argv[];
67 int flag; /* option flag read by getopt() */
68 int all = 0; /* -a flag */
69 int status = 0;
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 */
75 #endif
76 (void) textdomain(TEXT_DOMAIN);
78 while ((flag = getopt(argc, argv, "at:")) != EOF) {
79 switch (flag) {
80 case 'a':
81 all++;
82 break;
84 case 't': /* POSIX.2 */
85 all++; /* -t turns on -a */
86 getstops(optarg);
87 break;
89 default:
90 usage();
91 break;
95 argc -= optind;
96 argv = &argv[optind];
98 do {
99 if (argc > 0) {
100 if (freopen(argv[0], "r", stdin) == NULL) {
101 (void) fprintf(stderr, "unexpand: ");
102 perror(argv[0]);
103 status++;
105 argc--, argv++;
108 while (fgetws(input_buf, INPUT_SIZ, stdin) != NULL) {
109 input_buf[INPUT_SIZ] = 0;
110 tabify(input_buf, all);
112 } while (argc > 0);
114 return (status);
115 /* NOTREACHED */
118 void
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 */
125 int p_col;
127 cp = ibuf;
129 for (;;) {
130 switch (*cp) {
131 case ' ':
132 cp++;
134 spaces++;
135 ocol++;
137 if (nstops == 0) { /* default tab = 8 */
138 if ((ocol & 7) != 0)
139 break;
140 } else if (nstops == 1) { /* tab width */
141 if ((ocol % tabstops[0]) != 0)
142 break;
143 } else { /* explicit tabstops */
144 while (cstop < nstops &&
145 ocol > tabstops[cstop])
146 cstop++;
148 if (cstop >= nstops) {
149 (void) putchar(' ');
150 spaces = 0;
151 break;
154 if (ocol != tabstops[cstop])
155 break;
156 cstop++;
160 * if we get to this point, we must be at a
161 * tab stop. if spaces, then write out a tab.
163 if (spaces > 0) {
164 (void) putchar(((spaces > 1) ? '\t' : ' '));
165 spaces = 0;
168 break;
170 case '\b': /* POSIX.2 */
171 while (spaces-- > 0)
172 (void) putchar(' ');
173 spaces = 0;
175 cp++;
176 (void) putchar('\b');
178 if (--ocol < 0)
179 ocol = 0;
181 /* just in case */
182 cstop = 0;
183 break;
185 case '\t':
186 cp++;
187 (void) putchar('\t');
189 /* adjust ocol to current tabstop */
190 if (nstops == 0) {
191 ocol = (ocol + 8) & ~07;
192 } else if (nstops == 1) {
193 ocol += ocol % tabstops[0];
194 } else {
195 if (cstop < nstops &&
196 ocol < tabstops[cstop])
197 ocol = tabstops[cstop++];
198 else
199 ocol++;
202 spaces = 0;
203 break;
205 default:
206 while (spaces-- > 0)
207 (void) putchar(' ');
208 spaces = 0;
210 if (*cp == 0 || *cp == '\n' || all == 0) {
212 * either end of input line or -a not set
214 while (*cp != 0)
215 (void) putwchar(*cp++);
216 return;
219 (void) putwchar(*cp++);
220 if ((p_col = wcwidth(*cp)) < 0)
221 p_col = 0;
222 ocol += p_col;
223 break;
228 static void
229 getstops(const char *cp)
231 register int i;
233 for (;;) {
234 i = 0;
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"));
241 usage();
244 if (nstops > 0 && i <= tabstops[nstops-1]) {
245 (void) fprintf(stderr, gettext(
246 "unexpand: tablist must be increasing\n"));
247 usage();
250 if (nstops == MAX_TABS) {
251 (void) fprintf(stderr, gettext(
252 "unexpand: number of tabstops limited to %d\n"),
253 MAX_TABS);
254 usage();
257 tabstops[nstops++] = i;
258 if (*cp == 0)
259 break;
260 if (*cp != ',' && *cp != ' ') {
261 (void) fprintf(stderr, gettext(
262 "unexpand: invalid tablist separator\n"));
263 usage();
266 cp++;
270 static void
271 usage(void)
273 (void) fprintf(stderr, gettext(
274 "usage: unexpand [-a ] [-t tablist] [file ...]\n"));
275 exit(2);
276 /* NOTREACHED */