1 /* $NetBSD: tabs.c,v 1.3 2010/02/03 15:34:46 roy 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.3 2010/02/03 15:34:46 roy 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 <term.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 void 71 usage(void) 72 { 73 fprintf(stderr, 74 "usage: tabs [-n|-a|-a2|-c|-c2|-c3|-f|-p|-s|-u] [+m[n]]" 75 " [-T type]\n" 76 " tabs [-T type] [+[n]] n1[,n2,...]\n"); 77 exit(EXIT_FAILURE); 78 /* NOTREACHED */ 79 } 80 81 int 82 main(int argc, char **argv) 83 { 84 char *term, *arg, *token, *end, *tabs = NULL, *p; 85 const char *cr, *spec = NULL; 86 int i, n, inc = 8, stops[NSTOPS], nstops, last, cols, margin = 0; 87 size_t j; 88 struct winsize ws; 89 90 term = getenv("TERM"); 91 for (i = 1; i < argc; i++) { 92 if (argv[i][0] == '+') { 93 arg = argv[i] + 1; 94 if (arg[0] == 'm') 95 arg++; 96 if (arg[0] == '\0') 97 margin = 10; 98 else { 99 errno = 0; 100 margin = strtol(arg, &end, 10); 101 if (errno != 0 || *end != '\0' || margin < 0) 102 errx(EXIT_FAILURE, 103 "%s: invalid margin", arg); 104 } 105 continue; 106 } 107 if (argv[i][0] != '-') { 108 tabs = argv[i]; 109 break; 110 } 111 arg = argv[i] + 1; 112 if (arg[0] == '\0') 113 usage(); 114 if (arg[0] == '-' && arg[1] == '\0') { 115 if (argv[i + 1] != '\0') 116 tabs = argv[i + 1]; 117 break; 118 } 119 if (arg[0] == 'T' && arg[1] == '\0') { 120 term = argv[++i]; 121 if (term == NULL) 122 usage(); 123 continue; 124 } 125 if (isdigit((int)arg[0])) { 126 if (arg[1] != '\0') 127 errx(EXIT_FAILURE, 128 "%s: invalid increament", arg); 129 inc = arg[0] - '0'; 130 continue; 131 } 132 for (j = 0; j < ntabspecs; j++) { 133 if (arg[0] == tabspecs[j].opt[0] && 134 arg[1] == tabspecs[j].opt[1]) { 135 spec = tabspecs[j].spec; 136 break; 137 } 138 } 139 if (j == ntabspecs) 140 usage(); 141 } 142 if (tabs == NULL && spec != NULL) 143 tabs = strdup(spec); 144 145 if (tabs != NULL) 146 last = nstops = 0; 147 else 148 nstops = -1; 149 p = tabs; 150 while ((token = strsep(&p, ", ")) != NULL) { 151 if (*token == '\0') 152 continue; 153 if (nstops >= NSTOPS) 154 errx(EXIT_FAILURE, 155 "too many tab stops (max %d)", NSTOPS); 156 errno = 0; 157 n = strtol(token, &end, 10); 158 if (errno != 0 || *end != '\0' || n <= 0) 159 errx(EXIT_FAILURE, "%s: invalid tab stop", token); 160 if (*token == '+') { 161 if (nstops == 0) 162 errx(EXIT_FAILURE, 163 "first tab stop may not be relative"); 164 n += last; 165 } 166 if (last > n) 167 errx(EXIT_FAILURE, "tab stops may not go backwards"); 168 last = stops[nstops++] = n; 169 } 170 171 if (term == NULL) 172 errx(EXIT_FAILURE, "no value for $TERM and -T not given"); 173 if (setupterm(term, STDOUT_FILENO, NULL) != 0) 174 err(EXIT_FAILURE, "setupterm:"); 175 cr = carriage_return; 176 if (cr == NULL) 177 cr = "\r"; 178 if (clear_all_tabs == NULL) 179 errx(EXIT_FAILURE, "terminal cannot clear tabs"); 180 if (set_tab == NULL) 181 errx(EXIT_FAILURE, "terminal cannot set tabs"); 182 183 /* Clear existing tabs */ 184 putp(cr); 185 putp(clear_all_tabs); 186 putp(cr); 187 188 if (set_lr_margin != NULL) { 189 printf("%*s", margin, ""); 190 putp(set_lr_margin); 191 } else if (margin != 0) 192 warnx("terminal cannot set left margin"); 193 194 if (nstops >= 0) { 195 printf("%*s", stops[0] - 1, ""); 196 putp(set_tab); 197 for (i = 1; i < nstops; i++) { 198 printf("%*s", stops[i] - stops[i - 1], ""); 199 putp(set_tab); 200 } 201 } else if (inc > 0) { 202 cols = 0; 203 term = getenv("COLUMNS"); 204 if (term != NULL) { 205 errno = 0; 206 cols = strtol(term, &end, 10); 207 if (errno != 0 || *end != '\0' || cols < 0) 208 cols = 0; 209 } 210 if (cols == 0) { 211 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0) 212 cols = ws.ws_col; 213 else { 214 cols = tigetnum("cols"); 215 if (cols == 0) { 216 cols = 80; 217 warnx("terminal does not specify number" 218 "columns; defaulting to %d", 219 cols); 220 } 221 } 222 } 223 for (i = 0; i < cols / inc; i++) { 224 printf("%*s", inc, ""); 225 putp(set_tab); 226 } 227 } 228 putp(cr); 229 230 exit(EXIT_SUCCESS); 231 /* NOTREACHED */ 232 } 233