xref: /netbsd-src/usr.bin/tabs/tabs.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: tabs.c,v 1.2 2009/01/18 07:08:30 lukem Exp $ */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roy Marples.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 2008 \
35 The NetBSD Foundation, inc. All rights reserved.");
36 __RCSID("$NetBSD: tabs.c,v 1.2 2009/01/18 07:08:30 lukem Exp $");
37 #endif /* not lint */
38 
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <termcap.h>
49 #include <unistd.h>
50 
51 #define NSTOPS 20
52 
53 struct tabspec {
54 	const char *opt;
55 	const char *spec;
56 };
57 static const struct tabspec tabspecs[] = {
58 	{"a",	"1,10,16,36,72"},
59 	{"a2",	"1,10,16,40,72"},
60 	{"c",	"1,8,12,16,20,55"},
61 	{"c2",	"1,6,10,14,49"},
62 	{"c3",	"1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67"},
63 	{"f",	"1,7,11,15,19,23"},
64 	{"p",	"1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61"},
65 	{"s",	"1,10,55"},
66 	{"u",	"1,12,20,44"}
67 };
68 static const size_t ntabspecs = sizeof(tabspecs) / sizeof(tabspecs[0]);
69 
70 static char tbuf[2048];
71 
72 static void
73 usage(void)
74 {
75 	fprintf(stderr,
76 		"usage: tabs [-n|-a|-a2|-c|-c2|-c3|-f|-p|-s|-u] [+m[n]]"
77 		" [-T type]\n"
78 		"       tabs [-T type] [+[n]] n1[,n2,...]\n");
79 	exit(EXIT_FAILURE);
80 	/* NOTREACHED */
81 }
82 
83 int
84 main(int argc, char **argv)
85 {
86 	char *term, *arg, *tbp, *token, *end, *tabs = NULL, *p;
87 	const char *cr, *ct, *st, *ML, *spec = NULL;
88 	int i, n, inc = 8, stops[NSTOPS], nstops, last, cols, margin = 0;
89 	size_t j;
90 	struct winsize ws;
91 
92 	term = getenv("TERM");
93 	for (i = 1; i < argc; i++) {
94 		if (argv[i][0] == '+') {
95 			arg = argv[i] + 1;
96 			if (arg[0] == 'm')
97 				arg++;
98 			if (arg[0] == '\0')
99 				margin = 10;
100 			else {
101 				errno = 0;
102 				margin = strtol(arg, &end, 10);
103 				if (errno != 0 || *end != '\0' || margin < 0)
104 					errx(EXIT_FAILURE,
105 					     "%s: invalid margin", arg);
106 			}
107 			continue;
108 		}
109 		if (argv[i][0] != '-') {
110 			tabs = argv[i];
111 			break;
112 		}
113 		arg = argv[i] + 1;
114 		if (arg[0] == '\0')
115 			usage();
116 		if (arg[0] == '-' && arg[1] == '\0') {
117 			if (argv[i + 1] != '\0')
118 				tabs = argv[i + 1];
119 			break;
120 		}
121 		if (arg[0] == 'T' && arg[1] == '\0') {
122 			term = argv[++i];
123 			if (term == NULL)
124 				usage();
125 			continue;
126 		}
127 		if (isdigit((int)arg[0])) {
128 			if (arg[1] != '\0')
129 				errx(EXIT_FAILURE,
130 				     "%s: invalid increament", arg);
131 			inc = arg[0] - '0';
132 			continue;
133 		}
134 		for (j = 0; j < ntabspecs; j++) {
135 			if (arg[0] == tabspecs[j].opt[0] &&
136 			    arg[1] == tabspecs[j].opt[1]) {
137 				spec = tabspecs[j].spec;
138 				break;
139 			}
140 		}
141 		if (j == ntabspecs)
142 			usage();
143 	}
144 	if (tabs == NULL && spec != NULL)
145 		tabs = strdup(spec);
146 
147 	if (tabs != NULL)
148 		last = nstops = 0;
149 	else
150 		nstops = -1;
151 	p = tabs;
152 	while ((token = strsep(&p, ", ")) != NULL) {
153 		if (*token == '\0')
154 			continue;
155 		if (nstops >= NSTOPS)
156 			errx(EXIT_FAILURE,
157 			     "too many tab stops (max %d)", NSTOPS);
158 		errno = 0;
159 		n = strtol(token, &end, 10);
160 		if (errno != 0 || *end != '\0' || n <= 0)
161 			errx(EXIT_FAILURE, "%s: invalid tab stop", token);
162 		if (*token == '+') {
163 			if (nstops == 0)
164 				errx(EXIT_FAILURE,
165 				     "first tab stop may not be relative");
166 			n += last;
167 		}
168 		if (last > n)
169 			errx(EXIT_FAILURE, "tab stops may not go backwards");
170 		last = stops[nstops++] = n;
171 	}
172 
173 	if (term == NULL)
174 		errx(EXIT_FAILURE, "no value for $TERM and -T not given");
175 	switch (tgetent(tbuf, term)) {
176 	case -1:
177 		err(EXIT_FAILURE, "termcap database");
178 	case 0:
179 		errx(EXIT_FAILURE, "%s: no termcap entry", term);
180 	}
181 	tbp = tbuf;
182 	cr = tgetstr("cr", &tbp);
183 	if (cr == NULL)
184 		cr = "\r";
185 	ct = tgetstr("ct", &tbp);
186 	if (ct == NULL)
187 		errx(EXIT_FAILURE, "terminal cannot clear tabs");
188 	st = tgetstr("st", &tbp);
189 	if (st == NULL)
190 		errx(EXIT_FAILURE, "terminal cannot set tabs");
191 	ML = tgetstr("ML", &tbp);
192 
193 	/* Clear existing tabs */
194 	tputs(cr, 1, putchar);
195 	tputs(ct, 1, putchar);
196 	tputs(cr, 1, putchar);
197 
198 	if (ML != NULL) {
199 		printf("%*s", margin, "");
200 		tputs(ML, 1, putchar);
201 	} else if (margin != 0)
202 		warnx("terminal cannot set left margin");
203 
204 	if (nstops >= 0) {
205 		printf("%*s", stops[0] - 1, "");
206 		tputs(st, 1, putchar);
207 		for (i = 1; i < nstops; i++) {
208 			printf("%*s", stops[i] - stops[i - 1], "");
209 			tputs(st, 1, putchar);
210 		}
211 	} else if (inc > 0) {
212 		cols = 0;
213 		term = getenv("COLUMNS");
214 		if (term != NULL) {
215 			errno = 0;
216 			cols = strtol(term, &end, 10);
217 			if (errno != 0 || *end != '\0' || cols < 0)
218 				cols = 0;
219 		}
220 		if (cols == 0) {
221 			if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
222 				cols = ws.ws_col;
223 			else {
224 				cols = tgetnum("co");
225 				if (cols == 0) {
226 					cols = 80;
227 					warnx("terminal does not specify number"
228 					      "columns; defaulting to %d",
229 					      cols);
230 				}
231 			}
232 		}
233 		for (i = 0; i < cols / inc; i++) {
234 			printf("%*s", inc, "");
235 			tputs(st, 1, putchar);
236 		}
237 	}
238 	tputs(cr, 1, putchar);
239 
240 	exit(EXIT_SUCCESS);
241 	/* NOTREACHED */
242 }
243