1 /****************************************************************************
2 * Copyright (c) 2008-2013,2015 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.37 2015/07/04 21:14:42 tom Exp $")
42 static void usage(void) GCC_NORETURN
;
44 static char *prg_name
;
51 ExitProgram(EXIT_FAILURE
);
61 do_tabs(int *tab_list
)
67 while ((stop
= *tab_list
++) > 0) {
69 while (last
++ < stop
) {
75 if (stop
<= max_cols
) {
76 tputs(tparm(set_tab
, stop
), 1, putch
);
86 decode_tabs(const char *tab_list
)
88 int *result
= typeCalloc(int, strlen(tab_list
) + (unsigned) max_cols
);
95 failed("decode_tabs");
97 while ((ch
= *tab_list
++) != '\0') {
98 if (isdigit(UChar(ch
))) {
101 } else if (ch
== ',') {
102 result
[n
] = value
+ prior
;
103 if (n
> 0 && result
[n
] <= result
[n
- 1]) {
105 "%s: tab-stops are not in increasing order: %d %d\n",
106 prg_name
, value
, result
[n
- 1]);
114 } else if (ch
== '+') {
116 prior
= result
[n
- 1];
122 * If there is only one value, then it is an option such as "-8".
124 if ((n
== 0) && (value
> 0)) {
127 while (n
< max_cols
- 1) {
134 * Add the last value, if any.
136 result
[n
++] = value
+ prior
;
144 print_ruler(int *tab_list
)
150 /* first print a readable ruler */
151 for (n
= 0; n
< max_cols
; n
+= 10) {
152 int ch
= 1 + (n
/ 10);
154 _nc_SPRINTF(buffer
, _nc_SLIMIT(sizeof(buffer
))
159 printf("%.*s", ((max_cols
- n
) > 10) ? 10 : (max_cols
- n
), buffer
);
163 /* now, print '*' for each stop */
164 for (n
= 0, last
= 0; (tab_list
[n
] > 0) && (last
< max_cols
); ++n
) {
166 while (++last
< stop
) {
167 if (last
<= max_cols
) {
173 if (last
<= max_cols
) {
180 while (++last
<= max_cols
)
186 * Write an '*' on each tabstop, to demonstrate whether it lines up with the
190 write_tabs(int *tab_list
)
194 while ((stop
= *tab_list
++) > 0 && stop
<= max_cols
) {
195 fputs((stop
== 1) ? "*" : "\t*", stdout
);
197 /* also show a tab _past_ the stops */
199 fputs("\t+", stdout
);
204 * Trim leading/trailing blanks, as well as blanks after a comma.
205 * Convert embedded blanks to commas.
208 trimmed_tab_list(const char *source
)
210 char *result
= strdup(source
);
214 for (j
= k
= last
= 0; result
[j
] != 0; ++j
) {
215 ch
= UChar(result
[j
]);
219 } else if (isdigit(last
) || last
== ',') {
222 } else if (ch
== ',') {
226 result
[k
++] = (char) last
;
227 result
[k
++] = (char) ch
;
237 comma_is_needed(const char *source
)
242 size_t len
= strlen(source
);
244 result
= (source
[len
- 1] != ',');
252 * Add a command-line parameter to the tab-list. It can be blank- or comma-
253 * separated (or a mixture). For simplicity, empty tabs are ignored, e.g.,
256 * are treated the same.
259 add_to_tab_list(char **append
, const char *value
)
261 char *result
= *append
;
262 char *copied
= trimmed_tab_list(value
);
264 if (copied
!= 0 && *copied
!= '\0') {
265 const char *comma
= ",";
266 size_t need
= 1 + strlen(copied
);
270 else if (!comma_is_needed(*append
))
273 need
+= strlen(comma
);
275 need
+= strlen(*append
);
277 result
= malloc(need
);
279 failed("add_to_tab_list");
283 _nc_STRCPY(result
, *append
, need
);
286 _nc_STRCAT(result
, comma
, need
);
287 _nc_STRCAT(result
, copied
, need
);
296 * Check for illegal characters in the tab-list.
299 legal_tab_list(const char *tab_list
)
303 if (tab_list
!= 0 && *tab_list
!= '\0') {
304 if (comma_is_needed(tab_list
)) {
306 for (n
= 0; tab_list
[n
] != '\0'; ++n
) {
307 ch
= UChar(tab_list
[n
]);
308 if (!(isdigit(ch
) || ch
== ',' || ch
== '+')) {
310 "%s: unexpected character found '%c'\n",
317 fprintf(stderr
, "%s: trailing comma found '%s'\n", prg_name
, tab_list
);
321 fprintf(stderr
, "%s: no tab-list given\n", prg_name
);
328 skip_list(char *value
)
330 while (*value
!= '\0' &&
331 (isdigit(UChar(*value
)) ||
332 isspace(UChar(*value
)) ||
333 strchr("+,", UChar(*value
)) != 0)) {
342 #define DATA(s) s "\n"
343 static const char msg
[] =
345 DATA("Usage: tabs [options] [tabstop-list]")
348 DATA(" -0 reset tabs")
349 DATA(" -8 set tabs to standard interval")
350 DATA(" -a Assembler, IBM S/370, first format")
351 DATA(" -a2 Assembler, IBM S/370, second format")
352 DATA(" -c COBOL, normal format")
353 DATA(" -c2 COBOL compact format")
354 DATA(" -c3 COBOL compact format extended")
355 DATA(" -d debug (show ruler with expected/actual tab positions)")
357 DATA(" -n no-op (do not modify terminal settings)")
360 DATA(" -u UNIVAC 1100 Assembler")
361 DATA(" -T name use terminal type 'name'")
362 DATA(" -V print version")
364 DATA("A tabstop-list is an ordered list of column numbers, e.g., 1,11,21")
365 DATA("or 1,+10,+10 which is the same.")
371 ExitProgram(EXIT_FAILURE
);
375 main(int argc
, char *argv
[])
377 int rc
= EXIT_FAILURE
;
381 NCURSES_CONST
char *term_name
= 0;
383 const char *tab_list
= 0;
385 prg_name
= _nc_rootname(argv
[0]);
387 if ((term_name
= getenv("TERM")) == 0)
388 term_name
= "ansi+tabs";
390 /* cannot use getopt, since some options are two-character */
391 for (n
= 1; n
< argc
; ++n
) {
392 char *option
= argv
[n
];
395 while ((ch
= *++option
) != '\0') {
401 tab_list
= "1,10,16,36,72";
403 /* Assembler, IBM S/370, first format */
406 tab_list
= "1,10,16,40,72";
407 /* Assembler, IBM S/370, second format */
415 tab_list
= "1,8,12,16,20,55";
417 /* COBOL, normal format */
420 tab_list
= "1,6,10,14,49";
421 /* COBOL compact format */
424 tab_list
= "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67";
425 /* COBOL compact format extended */
429 case 'd': /* ncurses extension */
433 tab_list
= "1,7,11,15,19,23";
436 case 'n': /* ncurses extension */
440 tab_list
= "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
444 tab_list
= "1,10,55";
448 tab_list
= "1,12,20,44";
449 /* UNIVAC 1100 Assembler */
453 if (*++option
!= '\0') {
456 term_name
= argv
[n
++];
459 option
+= ((int) strlen(option
)) - 1;
462 puts(curses_version());
463 ExitProgram(EXIT_SUCCESS
);
465 if (isdigit(UChar(*option
))) {
466 char *copy
= strdup(option
);
467 *skip_list(copy
) = '\0';
469 option
= skip_list(option
) - 1;
478 while ((ch
= *++option
) != '\0') {
482 * The "+mXXX" option is unimplemented because only the long-obsolete
483 * att510d implements smgl, which is needed to support
488 /* special case of relative stops separated by spaces? */
489 if (option
== argv
[n
] + 1) {
490 tab_list
= add_to_tab_list(&append
, argv
[n
]);
498 if (tab_list
!= (const char *) append
) {
499 /* one of the predefined options was used */
504 tab_list
= add_to_tab_list(&append
, option
);
509 setupterm(term_name
, STDOUT_FILENO
, (int *) 0);
511 max_cols
= (columns
> 0) ? columns
: 80;
513 if (!VALID_STRING(clear_all_tabs
)) {
515 "%s: terminal type '%s' cannot reset tabs\n",
516 prg_name
, term_name
);
517 } else if (!VALID_STRING(set_tab
)) {
519 "%s: terminal type '%s' cannot set tabs\n",
520 prg_name
, term_name
);
521 } else if (legal_tab_list(tab_list
)) {
522 int *list
= decode_tabs(tab_list
);
525 tputs(clear_all_tabs
, 1, putch
);
532 printf("tabs %s\n", tab_list
);
539 printf("tabs %s\n", tab_list
);