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