xref: /freebsd/usr.bin/tabs/tabs.c (revision d68cc5a8390f5316400181b77a195f734567abd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Tim J. Robbins.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * tabs -- set terminal tabs
31  *
32  * This utility displays a series of characters that clears the terminal
33  * hardware tab settings, then initialises them to specified values,
34  * and optionally sets a soft margin.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/types.h>
41 #include <sys/tty.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <locale.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <term.h>
51 #include <unistd.h>
52 
53 /* Maximum number of tab stops allowed in table. */
54 #define NSTOPS		20
55 
56 #define NELEMS(a) (sizeof(a) / sizeof(a[0]))
57 
58 /* Predefined formats, taken from IEEE Std 1003.1-2001. */
59 static const struct {
60 	const char	*name;		/* Format name used on cmd. line */
61 	long		stops[NSTOPS];	/* Column positions */
62 } formats[] = {
63 	{ "a",	{ 1, 10, 16, 36, 72 } },
64 	{ "a2",	{ 1, 10, 16, 40, 72 } },
65 	{ "c",	{ 1, 8, 12, 16, 20, 55 } },
66 	{ "c2",	{ 1, 6, 10, 14, 49 } },
67 	{ "c3", { 1, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58,
68 		62, 67 } },
69 	{ "f",	{ 1, 7, 11, 15, 19, 23 } },
70 	{ "p",	{ 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57,
71 		61 } },
72 	{ "s",	{ 1, 10, 55 } },
73 	{ "u",	{ 1, 12, 20, 44 } }
74 };
75 
76 static void	 gettabs(char *, long *, long *);
77 static int	 ttywidth(void);
78 static void	 usage(void);
79 
80 int
81 main(int argc __unused, char *argv[])
82 {
83 	long cols, i, inc, j, margin, nstops, stops[NSTOPS];
84 	const char *cr, *ct, *st, *ML;
85 	char area[1024], *ap, *arg, *end;
86 
87 	setlocale(LC_ALL, "");
88 
89 	inc = 8;
90 	margin = 0;
91 	nstops = -1;
92 	while ((arg = *++argv) != NULL && (*arg == '-' || *arg == '+')) {
93 		if (*arg == '+') {
94 			/* +m[n] or +[n] */
95 			if (*++arg == 'm')
96 				arg++;
97 			if (*arg != '\0') {
98 				errno = 0;
99 				margin = strtol(arg, &end, 10);
100 				if (errno != 0 || *end != '\0' || margin < 0)
101 					errx(1, "%s: invalid margin width",
102 					    arg);
103 			} else
104 				margin = 10;
105 		} else if (isdigit(arg[1])) {
106 			/* -n */
107 			errno = 0;
108 			inc = strtol(arg + 1, &end, 10);
109 			if (errno != 0 || *end != '\0' || inc < 0)
110 				errx(1, "%s: invalid increment", arg + 1);
111 		} else if (arg[1] == 'T') {
112 			/* -Ttype or -T type */
113 			if (arg[2] != '\0')
114 				setenv("TERM", arg + 2, 1);
115 			else {
116 				if ((arg = *++argv) == NULL)
117 					usage();
118 				setenv("TERM", arg, 1);
119 			}
120 		} else if (arg[1] == '-') {
121 			arg = *++argv;
122 			break;
123 		} else {
124 			/* Predefined format */
125 			for (i = 0; i < (int)NELEMS(formats); i++)
126 				if (strcmp(formats[i].name, arg + 1) == 0)
127 					break;
128 			if (i == NELEMS(formats))
129 				usage();
130 			for (j = nstops = 0; j < NSTOPS &&
131 			    formats[i].stops[j] != 0; j++)
132 				stops[nstops++] = formats[i].stops[j];
133 		}
134 	}
135 
136 	if (arg != NULL) {
137 		if (nstops != -1)
138 			usage();
139 		gettabs(arg, stops, &nstops);
140 	}
141 
142 	/* Initialise terminal, get the strings we need */
143 	setupterm(NULL, 1, NULL);
144 	ap = area;
145 	if ((ct = tgetstr("ct", &ap)) == NULL)
146 		errx(1, "terminal cannot clear tabs");
147 	if ((st = tgetstr("st", &ap)) == NULL)
148 		errx(1, "terminal cannot set tabs");
149 	if ((cr = tgetstr("cr", &ap)) == NULL)
150 		cr = "\r";
151 	ML = tgetstr("ML", &ap);
152 	cols = ttywidth();
153 
154 	/* Clear all tabs. */
155 	putp(cr);
156 	putp(ct);
157 
158 	/*
159 	 * Set soft margin.
160 	 * XXX Does this actually work?
161 	 */
162 	if (ML != NULL) {
163 		printf("%*s", (int)margin, "");
164 		putp(ML);
165 	} else if (margin != 0)
166 		warnx("terminal cannot set left margin");
167 
168 	/* Optionally output new tab stops. */
169 	if (nstops >= 0) {
170 		printf("%*s", (int)stops[0] - 1, "");
171 		putp(st);
172 		for (i = 1; i < nstops; i++) {
173 			printf("%*s", (int)(stops[i] - stops[i - 1]), "");
174 			putp(st);
175 		}
176 	} else if (inc > 0) {
177 		for (i = 0; i < cols / inc; i++) {
178 			putp(st);
179 			printf("%*s", (int)inc, "");
180 		}
181 		putp(st);
182 	}
183 	putp(cr);
184 
185 	exit(0);
186 }
187 
188 static void
189 usage(void)
190 {
191 
192 	fprintf(stderr,
193 "usage: tabs [-n|-a|-a2|-c|-c2|-c3|-f|-p|-s|-u] [+m[n]] [-T type]\n");
194 	fprintf(stderr,
195 "       tabs [-T type] [+[n]] n1,[n2,...]\n");
196 	exit(1);
197 }
198 
199 static void
200 gettabs(char *arg, long stops[], long *nstops)
201 {
202 	char *tok, *end;
203 	long last, stop;
204 
205 	for (last = *nstops = 0, tok = strtok(arg, ","); tok != NULL;
206 	    tok = strtok(NULL, ",")) {
207 		if (*nstops >= NSTOPS)
208 			errx(1, "too many tab stops (limit %d)", NSTOPS);
209 		errno = 0;
210 		stop = strtol(tok, &end, 10);
211 		if (errno != 0 || *end != '\0' || stop <= 0)
212 			errx(1, "%s: invalid tab stop", tok);
213 		if (*tok == '+') {
214 			if (tok == arg)
215 				errx(1, "%s: first tab may not be relative",
216 				    tok);
217 			stop += last;
218 		}
219 		if (last > stop)
220 			errx(1, "cannot go backwards");
221 		last = stops[(*nstops)++] = stop;
222 	}
223 }
224 
225 static int
226 ttywidth(void)
227 {
228 	struct winsize ws;
229 	int width;
230 
231 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
232 		width = ws.ws_col;
233 	else if ((width = tgetnum("co")) == 0) {
234 		width = 80;
235 		warnx("cannot find terminal width; defaulted to %d", width);
236 	}
237 
238 	return (width);
239 }
240