unexpand.c revision 28456
1117395Skan/*-
2117395Skan * Copyright (c) 1980, 1993
3169689Skan *	The Regents of the University of California.  All rights reserved.
4117395Skan *
5117395Skan * Redistribution and use in source and binary forms, with or without
6132718Skan * modification, are permitted provided that the following conditions
7117395Skan * are met:
8132718Skan * 1. Redistributions of source code must retain the above copyright
9132718Skan *    notice, this list of conditions and the following disclaimer.
10132718Skan * 2. Redistributions in binary form must reproduce the above copyright
11132718Skan *    notice, this list of conditions and the following disclaimer in the
12117395Skan *    documentation and/or other materials provided with the distribution.
13132718Skan * 3. All advertising materials mentioning features or use of this software
14132718Skan *    must display the following acknowledgement:
15132718Skan *	This product includes software developed by the University of
16132718Skan *	California, Berkeley and its contributors.
17117395Skan * 4. Neither the name of the University nor the names of its contributors
18132718Skan *    may be used to endorse or promote products derived from this software
19132718Skan *    without specific prior written permission.
20169689Skan *
21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22117395Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23117395Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24117395Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25117395Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26117395Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27117395Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28117395Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29117395Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30117395Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31117395Skan * SUCH DAMAGE.
32117395Skan */
33117395Skan
34117395Skan#ifndef lint
35117395Skanstatic const char copyright[] =
36117395Skan"@(#) Copyright (c) 1980, 1993\n\
37117395Skan	The Regents of the University of California.  All rights reserved.\n";
38169689Skan#endif /* not lint */
39117395Skan
40117395Skan#ifndef lint
41117395Skan#if 0
42117395Skanstatic char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
43169689Skan#endif
44117395Skanstatic const char rcsid[] =
45169689Skan	"$Id$";
46169689Skan#endif /* not lint */
47169689Skan
48169689Skan/*
49169689Skan * unexpand - put tabs into a file replacing blanks
50169689Skan */
51117395Skan#include <err.h>
52117395Skan#include <stdio.h>
53117395Skan
54117395Skanchar	genbuf[BUFSIZ];
55117395Skanchar	linebuf[BUFSIZ];
56117395Skanint	all;
57117395Skan
58132718Skanstatic void usage __P((void));
59117395Skanvoid tabify __P((char));
60132718Skan
61117395Skanvoid
62117395Skanmain(argc, argv)
63117395Skan	int argc;
64117395Skan	char *argv[];
65117395Skan{
66117395Skan	register char *cp;
67132718Skan
68132718Skan	argc--, argv++;
69169689Skan	if (argc > 0 && argv[0][0] == '-') {
70169689Skan		if (strcmp(argv[0], "-a") != 0)
71169689Skan			usage();
72117395Skan		all++;
73117395Skan		argc--, argv++;
74117395Skan	}
75117395Skan	do {
76117395Skan		if (argc > 0) {
77117395Skan			if (freopen(argv[0], "r", stdin) == NULL)
78132718Skan				err(1, "%s", argv[0]);
79169689Skan			argc--, argv++;
80169689Skan		}
81169689Skan		while (fgets(genbuf, BUFSIZ, stdin) != NULL) {
82117395Skan			for (cp = linebuf; *cp; cp++)
83117395Skan				continue;
84117395Skan			if (cp > linebuf)
85117395Skan				cp[-1] = 0;
86117395Skan			tabify(all);
87169689Skan			printf("%s", linebuf);
88169689Skan		}
89169689Skan	} while (argc > 0);
90169689Skan	exit(0);
91169689Skan}
92169689Skan
93169689Skanstatic void
94169689Skanusage()
95117395Skan{
96117395Skan	fprintf(stderr, "usage: unexpand [-a] file ...\n");
97117395Skan	exit(1);
98117395Skan}
99117395Skan
100117395Skanvoid
101117395Skantabify(c)
102117395Skan	char c;
103117395Skan{
104117395Skan	register char *cp, *dp;
105169689Skan	register int dcol;
106117395Skan	int ocol;
107117395Skan
108169689Skan	ocol = 0;
109117395Skan	dcol = 0;
110117395Skan	cp = genbuf, dp = linebuf;
111117395Skan	for (;;) {
112117395Skan		switch (*cp) {
113117395Skan
114117395Skan		case ' ':
115117395Skan			dcol++;
116117395Skan			break;
117169689Skan
118117395Skan		case '\t':
119117395Skan			dcol += 8;
120117395Skan			dcol &= ~07;
121117395Skan			break;
122117395Skan
123117395Skan		default:
124117395Skan			while (((ocol + 8) &~ 07) <= dcol) {
125117395Skan				if (ocol + 1 == dcol)
126117395Skan					break;
127117395Skan				*dp++ = '\t';
128117395Skan				ocol += 8;
129117395Skan				ocol &= ~07;
130117395Skan			}
131117395Skan			while (ocol < dcol) {
132117395Skan				*dp++ = ' ';
133117395Skan				ocol++;
134117395Skan			}
135117395Skan			if (*cp == 0 || c == 0) {
136117395Skan				strcpy(dp, cp);
137117395Skan				return;
138117395Skan			}
139117395Skan			*dp++ = *cp;
140117395Skan			ocol++, dcol++;
141117395Skan		}
142117395Skan		cp++;
143117395Skan	}
144117395Skan}
145117395Skan