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