Home | History | Annotate | Line # | Download | only in tabs
tabs.c revision 1.1
      1 /* $NetBSD: tabs.c,v 1.1 2008/12/11 11:18:35 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.1 2008/12/11 11:18:35 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 <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, j, n, inc = 8, stops[NSTOPS], nstops, last, cols, margin = 0;
     89 	struct winsize ws;
     90 
     91         term = getenv("TERM");
     92         for (i = 1; i < argc; i++) {
     93 		if (argv[i][0] == '+') {
     94 			arg = argv[i] + 1;
     95 			if (arg[0] == 'm')
     96 				arg++;
     97 			if (arg[0] == '\0')
     98 				margin = 10;
     99 			else {
    100 				errno = 0;
    101 				margin = strtol(arg, &end, 10);
    102 				if (errno != 0 || *end != '\0' || margin < 0)
    103 					errx(EXIT_FAILURE,
    104 					     "%s: invalid margin", arg);
    105 			}
    106 			continue;
    107 		}
    108 		if (argv[i][0] != '-') {
    109 			tabs = argv[i];
    110 			break;
    111 		}
    112 		arg = argv[i] + 1;
    113 		if (arg[0] == '\0')
    114 			usage();
    115 		if (arg[0] == '-' && arg[1] == '\0') {
    116 			if (argv[i + 1] != '\0')
    117 				tabs = argv[i + 1];
    118 			break;
    119 		}
    120 		if (arg[0] == 'T' && arg[1] == '\0') {
    121 			term = argv[++i];
    122 			if (term == NULL)
    123 				usage();
    124 			continue;
    125 		}
    126 		if (isdigit((int)arg[0])) {
    127 			if (arg[1] != '\0')
    128 				errx(EXIT_FAILURE,
    129 				     "%s: invalid increament", arg);
    130 			inc = arg[0] - '0';
    131 			continue;
    132 		}
    133 		for (j = 0; j < ntabspecs; j++) {
    134 			if (arg[0] == tabspecs[j].opt[0] &&
    135 			    arg[1] == tabspecs[j].opt[1])
    136 			{
    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