1 /****************************************************************************
2 * Copyright (c) 2008-2011,2012 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey 2008 *
31 ****************************************************************************/
34 * tabs.c -- set terminal hard-tabstops
38 #include <progs.priv.h>
40 MODULE_ID("$Id: tabs.c,v 1.24 2012/10/27 19:53:53 tom Exp $")
42 static void usage(void) GCC_NORETURN;
50 ExitProgram(EXIT_FAILURE);
60 do_tabs(int *tab_list)
66 while ((stop = *tab_list++) > 0) {
68 while (last++ < stop) {
74 if (stop <= max_cols) {
75 tputs(tparm(set_tab, stop), 1, putch);
85 decode_tabs(const char *tab_list)
87 int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols);
94 failed("decode_tabs");
96 while ((ch = *tab_list++) != '\0') {
97 if (isdigit(UChar(ch))) {
100 } else if (ch == ',') {
101 result[n] = value + prior;
102 if (n > 0 && result[n] <= result[n - 1]) {
104 "tab-stops are not in increasing order: %d %d\n",
105 value, result[n - 1]);
113 } else if (ch == '+') {
115 prior = result[n - 1];
120 * If there is only one value, then it is an option such as "-8".
122 if ((n == 0) && (value > 0)) {
124 while (n < max_cols - 1) {
131 * Add the last value, if any.
133 result[n++] = value + prior;
140 print_ruler(int *tab_list)
146 /* first print a readable ruler */
147 for (n = 0; n < max_cols; n += 10) {
148 int ch = 1 + (n / 10);
150 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
155 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
159 /* now, print '*' for each stop */
160 for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) {
162 while (++last < stop) {
163 if (last <= max_cols) {
169 if (last <= max_cols) {
176 while (++last <= max_cols)
182 * Write an '*' on each tabstop, to demonstrate whether it lines up with the
186 write_tabs(int *tab_list)
190 while ((stop = *tab_list++) > 0 && stop <= max_cols) {
191 fputs((stop == 1) ? "*" : "\t*", stdout);
193 /* also show a tab _past_ the stops */
195 fputs("\t+", stdout);
200 * Trim leading/trailing blanks, as well as blanks after a comma.
201 * Convert embedded blanks to commas.
204 trimmed_tab_list(const char *source)
206 char *result = strdup(source);
210 for (j = k = last = 0; result[j] != 0; ++j) {
211 ch = UChar(result[j]);
215 } else if (isdigit(last) || last == ',') {
218 } else if (ch == ',') {
222 result[k++] = (char) last;
223 result[k++] = (char) ch;
233 comma_is_needed(const char *source)
238 size_t len = strlen(source);
240 result = (source[len - 1] != ',');
248 * Add a command-line parameter to the tab-list. It can be blank- or comma-
249 * separated (or a mixture). For simplicity, empty tabs are ignored, e.g.,
252 * are treated the same.
255 add_to_tab_list(char **append, const char *value)
257 char *result = *append;
258 char *copied = trimmed_tab_list(value);
260 if (copied != 0 && *copied != '\0') {
261 const char *comma = ",";
262 size_t need = 1 + strlen(copied);
266 else if (!comma_is_needed(*append))
269 need += strlen(comma);
271 need += strlen(*append);
273 result = malloc(need);
275 failed("add_to_tab_list");
279 _nc_STRCPY(result, *append, need);
282 _nc_STRCAT(result, comma, need);
283 _nc_STRCAT(result, copied, need);
291 * Check for illegal characters in the tab-list.
294 legal_tab_list(const char *program, const char *tab_list)
298 if (tab_list != 0 && *tab_list != '\0') {
299 if (comma_is_needed(tab_list)) {
301 for (n = 0; tab_list[n] != '\0'; ++n) {
302 ch = UChar(tab_list[n]);
303 if (!(isdigit(ch) || ch == ',' || ch == '+')) {
305 "%s: unexpected character found '%c'\n",
312 fprintf(stderr, "%s: trailing comma found '%s'\n", program, tab_list);
316 fprintf(stderr, "%s: no tab-list given\n", program);
325 static const char *msg[] =
327 "Usage: tabs [options] [tabstop-list]"
331 ," -8 set tabs to standard interval"
332 ," -a Assembler, IBM S/370, first format"
333 ," -a2 Assembler, IBM S/370, second format"
334 ," -c COBOL, normal format"
335 ," -c2 COBOL compact format"
336 ," -c3 COBOL compact format extended"
337 ," -d debug (show ruler with expected/actual tab positions)"
339 ," -n no-op (do not modify terminal settings)"
342 ," -u UNIVAC 1100 Assembler"
343 ," -T name use terminal type 'name'"
345 ,"A tabstop-list is an ordered list of column numbers, e.g., 1,11,21"
346 ,"or 1,+10,+10 which is the same."
351 for (n = 0; n < SIZEOF(msg); ++n) {
352 fprintf(stderr, "%s\n", msg[n]);
354 ExitProgram(EXIT_FAILURE);
358 main(int argc, char *argv[])
360 int rc = EXIT_FAILURE;
364 NCURSES_CONST char *term_name = 0;
366 const char *tab_list = 0;
368 if ((term_name = getenv("TERM")) == 0)
369 term_name = "ansi+tabs";
371 /* cannot use getopt, since some options are two-character */
372 for (n = 1; n < argc; ++n) {
373 char *option = argv[n];
376 while ((ch = *++option) != '\0') {
381 tab_list = "1,10,16,36,72";
382 /* Assembler, IBM S/370, first format */
385 tab_list = "1,10,16,40,72";
386 /* Assembler, IBM S/370, second format */
395 tab_list = "1,8,12,16,20,55";
396 /* COBOL, normal format */
399 tab_list = "1,6,10,14,49";
400 /* COBOL compact format */
403 tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67";
404 /* COBOL compact format extended */
410 case 'd': /* ncurses extension */
414 tab_list = "1,7,11,15,19,23";
417 case 'n': /* ncurses extension */
421 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
425 tab_list = "1,10,55";
429 tab_list = "1,12,20,44";
430 /* UNIVAC 1100 Assembler */
434 if (*++option != '\0') {
437 term_name = argv[n++];
439 option += ((int) strlen(option)) - 1;
442 if (isdigit(UChar(*option))) {
448 option += ((int) strlen(option)) - 1;
454 while ((ch = *++option) != '\0') {
458 * The "+mXXX" option is unimplemented because only the long-obsolete
459 * att510d implements smgl, which is needed to support
464 /* special case of relative stops separated by spaces? */
465 if (option == argv[n] + 1) {
466 tab_list = add_to_tab_list(&append, argv[n]);
474 if (tab_list != (const char *) append) {
475 /* one of the predefined options was used */
480 tab_list = add_to_tab_list(&append, option);
485 setupterm(term_name, STDOUT_FILENO, (int *) 0);
487 max_cols = (columns > 0) ? columns : 80;
489 if (!VALID_STRING(clear_all_tabs)) {
491 "%s: terminal type '%s' cannot reset tabs\n",
493 } else if (!VALID_STRING(set_tab)) {
495 "%s: terminal type '%s' cannot set tabs\n",
497 } else if (legal_tab_list(argv[0], tab_list)) {
498 int *list = decode_tabs(tab_list);
501 tputs(clear_all_tabs, 1, putch);
508 printf("tabs %s\n", tab_list);
515 printf("tabs %s\n", tab_list);