1 /****************************************************************************
2 * Copyright (c) 2008,2009 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.16 2009/10/10 16:15:37 tom Exp $")
42 static void usage(void) GCC_NORETURN;
53 do_tabs(int *tab_list)
59 while ((stop = *tab_list++) > 0) {
61 while (last++ < stop) {
67 if (stop <= max_cols) {
68 tputs(tparm(set_tab, stop), 1, putch);
78 decode_tabs(const char *tab_list)
80 int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols);
87 while ((ch = *tab_list++) != '\0') {
88 if (isdigit(UChar(ch))) {
91 } else if (ch == ',') {
92 result[n] = value + prior;
93 if (n > 0 && value <= result[n - 1]) {
95 "tab-stops are not in increasing order\n");
103 } else if (ch == '+') {
105 prior = result[n - 1];
112 * If there is only one value, then it is an option such as "-8".
114 if ((n == 0) && (value > 0)) {
116 while (n < max_cols - 1) {
123 * Add the last value, if any.
132 print_ruler(int *tab_list)
138 /* first print a readable ruler */
139 for (n = 0; n < max_cols; n += 10) {
140 int ch = 1 + (n / 10);
142 sprintf(buffer, "----+----%c",
146 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
150 /* now, print '*' for each stop */
151 for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) {
153 while (++last < stop) {
154 if (last <= max_cols) {
160 if (last <= max_cols) {
167 while (++last <= max_cols)
173 * Write an '*' on each tabstop, to demonstrate whether it lines up with the
177 write_tabs(int *tab_list)
181 while ((stop = *tab_list++) > 0 && stop <= max_cols) {
182 fputs((stop == 1) ? "*" : "\t*", stdout);
184 /* also show a tab _past_ the stops */
186 fputs("\t+", stdout);
191 * Trim leading/trailing blanks, as well as blanks after a comma.
192 * Convert embedded blanks to commas.
195 trimmed_tab_list(const char *source)
197 char *result = strdup(source);
201 for (j = k = last = 0; result[j] != 0; ++j) {
202 ch = UChar(result[j]);
206 } else if (isdigit(last) || last == ',') {
209 } else if (ch == ',') {
213 result[k++] = (char) last;
214 result[k++] = (char) ch;
224 comma_is_needed(const char *source)
229 unsigned len = strlen(source);
231 result = (source[len - 1] != ',');
239 * Add a command-line parameter to the tab-list. It can be blank- or comma-
240 * separated (or a mixture). For simplicity, empty tabs are ignored, e.g.,
243 * are treated the same.
246 add_to_tab_list(char **append, const char *value)
248 char *result = *append;
249 char *copied = trimmed_tab_list(value);
251 if (copied != 0 && *copied != '\0') {
252 const char *comma = ",";
253 unsigned need = 1 + strlen(copied);
257 else if (!comma_is_needed(*append))
260 need += strlen(comma);
262 need += strlen(*append);
264 result = malloc(need);
268 strcpy(result, *append);
271 strcat(result, comma);
272 strcat(result, copied);
281 * Check for illegal characters in the tab-list.
284 legal_tab_list(const char *program, const char *tab_list)
288 if (tab_list != 0 && *tab_list != '\0') {
289 if (comma_is_needed(tab_list)) {
291 for (n = 0; tab_list[n] != '\0'; ++n) {
292 ch = UChar(tab_list[n]);
293 if (!(isdigit(ch) || ch == ',')) {
295 "%s: unexpected character found '%c'\n",
302 fprintf(stderr, "%s: trailing comma found '%s'\n", program, tab_list);
306 fprintf(stderr, "%s: no tab-list given\n", program);
315 static const char *msg[] =
317 "Usage: tabs [options] [tabstop-list]"
321 ," -8 set tabs to standard interval"
322 ," -a Assembler, IBM S/370, first format"
323 ," -a2 Assembler, IBM S/370, second format"
324 ," -c COBOL, normal format"
325 ," -c2 COBOL compact format"
326 ," -c3 COBOL compact format extended"
327 ," -d debug (show ruler with expected/actual tab positions)"
329 ," -n no-op (do not modify terminal settings)"
332 ," -u UNIVAC 1100 Assembler"
333 ," -T name use terminal type 'name'"
335 ,"A tabstop-list is an ordered list of column numbers, e.g., 1,11,21"
336 ,"or 1,+10,+10 which is the same."
341 for (n = 0; n < SIZEOF(msg); ++n) {
342 fprintf(stderr, "%s\n", msg[n]);
344 ExitProgram(EXIT_FAILURE);
348 main(int argc, char *argv[])
350 int rc = EXIT_FAILURE;
354 NCURSES_CONST char *term_name = 0;
355 const char *mar_list = 0; /* ignored */
357 const char *tab_list = 0;
359 if ((term_name = getenv("TERM")) == 0)
360 term_name = "ansi+tabs";
362 /* cannot use getopt, since some options are two-character */
363 for (n = 1; n < argc; ++n) {
364 char *option = argv[n];
367 while ((ch = *++option) != '\0') {
372 tab_list = "1,10,16,36,72";
373 /* Assembler, IBM S/370, first format */
376 tab_list = "1,10,16,40,72";
377 /* Assembler, IBM S/370, second format */
386 tab_list = "1,8,12,16,20,55";
387 /* COBOL, normal format */
390 tab_list = "1,6,10,14,49";
391 /* COBOL compact format */
394 tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67";
395 /* COBOL compact format extended */
401 case 'd': /* ncurses extension */
405 tab_list = "1,7,11,15,19,23";
408 case 'n': /* ncurses extension */
412 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
416 tab_list = "1,10,55";
420 tab_list = "1,12,20,44";
421 /* UNIVAC 1100 Assembler */
425 if (*++option != '\0') {
428 term_name = argv[n++];
430 option += ((int) strlen(option)) - 1;
433 if (isdigit(UChar(*option))) {
439 option += ((int) strlen(option)) - 1;
445 while ((ch = *++option) != '\0') {
457 if (tab_list != (const char *) append) {
458 /* one of the predefined options was used */
463 tab_list = add_to_tab_list(&append, option);
464 option += ((int) strlen(option)) - 1;
469 setupterm(term_name, STDOUT_FILENO, (int *) 0);
471 max_cols = (columns > 0) ? columns : 80;
473 if (!VALID_STRING(clear_all_tabs)) {
475 "%s: terminal type '%s' cannot reset tabs\n",
477 } else if (!VALID_STRING(set_tab)) {
479 "%s: terminal type '%s' cannot set tabs\n",
481 } else if (legal_tab_list(argv[0], tab_list)) {
482 int *list = decode_tabs(tab_list);
485 tputs(clear_all_tabs, 1, putch);
492 printf("tabs %s\n", tab_list);
499 printf("tabs %s\n", tab_list);