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