xref: /freebsd/usr.bin/unexpand/unexpand.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 
48 /*
49  * unexpand - put tabs into a file replacing blanks
50  */
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 char	genbuf[BUFSIZ];
57 char	linebuf[BUFSIZ];
58 int	all;
59 
60 static void usage __P((void));
61 void tabify __P((char));
62 
63 int
64 main(argc, argv)
65 	int argc;
66 	char *argv[];
67 {
68 	register char *cp;
69 
70 	argc--, argv++;
71 	if (argc > 0 && argv[0][0] == '-') {
72 		if (strcmp(argv[0], "-a") != 0)
73 			usage();
74 		all++;
75 		argc--, argv++;
76 	}
77 	do {
78 		if (argc > 0) {
79 			if (freopen(argv[0], "r", stdin) == NULL)
80 				err(1, "%s", argv[0]);
81 			argc--, argv++;
82 		}
83 		while (fgets(genbuf, BUFSIZ, stdin) != NULL) {
84 			for (cp = linebuf; *cp; cp++)
85 				continue;
86 			if (cp > linebuf)
87 				cp[-1] = 0;
88 			tabify(all);
89 			printf("%s", linebuf);
90 		}
91 	} while (argc > 0);
92 	exit(0);
93 }
94 
95 static void
96 usage()
97 {
98 	fprintf(stderr, "usage: unexpand [-a] file ...\n");
99 	exit(1);
100 }
101 
102 void
103 tabify(c)
104 	char c;
105 {
106 	register char *cp, *dp;
107 	register int dcol;
108 	int ocol;
109 
110 	ocol = 0;
111 	dcol = 0;
112 	cp = genbuf, dp = linebuf;
113 	for (;;) {
114 		switch (*cp) {
115 
116 		case ' ':
117 			dcol++;
118 			break;
119 
120 		case '\t':
121 			dcol += 8;
122 			dcol &= ~07;
123 			break;
124 
125 		default:
126 			while (((ocol + 8) &~ 07) <= dcol) {
127 				if (ocol + 1 == dcol)
128 					break;
129 				*dp++ = '\t';
130 				ocol += 8;
131 				ocol &= ~07;
132 			}
133 			while (ocol < dcol) {
134 				*dp++ = ' ';
135 				ocol++;
136 			}
137 			if (*cp == 0 || c == 0) {
138 				strcpy(dp, cp);
139 				return;
140 			}
141 			*dp++ = *cp;
142 			ocol++, dcol++;
143 		}
144 		cp++;
145 	}
146 }
147