]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tabs.c
ncurses 5.9 - patch 20120505
[ncurses.git] / progs / tabs.c
1 /****************************************************************************
2  * Copyright (c) 2008-2011,2012 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                        2008                    *
31  ****************************************************************************/
32
33 /*
34  * tabs.c --  set terminal hard-tabstops
35  */
36
37 #define USE_LIBTINFO
38 #include <progs.priv.h>
39
40 MODULE_ID("$Id: tabs.c,v 1.23 2012/02/22 23:57:44 tom Exp $")
41
42 static void usage(void) GCC_NORETURN;
43
44 static int max_cols;
45
46 static int
47 putch(int c)
48 {
49     return putchar(c);
50 }
51
52 static void
53 do_tabs(int *tab_list)
54 {
55     int last = 1;
56     int stop;
57
58     putchar('\r');
59     while ((stop = *tab_list++) > 0) {
60         if (last < stop) {
61             while (last++ < stop) {
62                 if (last > max_cols)
63                     break;
64                 putchar(' ');
65             }
66         }
67         if (stop <= max_cols) {
68             tputs(tparm(set_tab, stop), 1, putch);
69             last = stop;
70         } else {
71             break;
72         }
73     }
74     putchar('\n');
75 }
76
77 static int *
78 decode_tabs(const char *tab_list)
79 {
80     int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols);
81     int n = 0;
82     int value = 0;
83     int prior = 0;
84     int ch;
85
86     if (result != 0) {
87         while ((ch = *tab_list++) != '\0') {
88             if (isdigit(UChar(ch))) {
89                 value *= 10;
90                 value += (ch - '0');
91             } else if (ch == ',') {
92                 result[n] = value + prior;
93                 if (n > 0 && result[n] <= result[n - 1]) {
94                     fprintf(stderr,
95                             "tab-stops are not in increasing order: %d %d\n",
96                             value, result[n - 1]);
97                     free(result);
98                     result = 0;
99                     break;
100                 }
101                 ++n;
102                 value = 0;
103                 prior = 0;
104             } else if (ch == '+') {
105                 if (n)
106                     prior = result[n - 1];
107             }
108         }
109     }
110
111     if (result != 0) {
112         /*
113          * If there is only one value, then it is an option such as "-8".
114          */
115         if ((n == 0) && (value > 0)) {
116             int step = value;
117             while (n < max_cols - 1) {
118                 result[n++] = value;
119                 value += step;
120             }
121         }
122
123         /*
124          * Add the last value, if any.
125          */
126         result[n++] = value + prior;
127         result[n] = 0;
128     }
129     return result;
130 }
131
132 static void
133 print_ruler(int *tab_list)
134 {
135     int last = 0;
136     int stop;
137     int n;
138
139     /* first print a readable ruler */
140     for (n = 0; n < max_cols; n += 10) {
141         int ch = 1 + (n / 10);
142         char buffer[20];
143         _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
144                     "----+----%c",
145                     ((ch < 10)
146                      ? (ch + '0')
147                      : (ch + 'A' - 10)));
148         printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
149     }
150     putchar('\n');
151
152     /* now, print '*' for each stop */
153     for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) {
154         stop = tab_list[n];
155         while (++last < stop) {
156             if (last <= max_cols) {
157                 putchar('-');
158             } else {
159                 break;
160             }
161         }
162         if (last <= max_cols) {
163             putchar('*');
164             last = stop;
165         } else {
166             break;
167         }
168     }
169     while (++last <= max_cols)
170         putchar('-');
171     putchar('\n');
172 }
173
174 /*
175  * Write an '*' on each tabstop, to demonstrate whether it lines up with the
176  * ruler.
177  */
178 static void
179 write_tabs(int *tab_list)
180 {
181     int stop;
182
183     while ((stop = *tab_list++) > 0 && stop <= max_cols) {
184         fputs((stop == 1) ? "*" : "\t*", stdout);
185     };
186     /* also show a tab _past_ the stops */
187     if (stop < max_cols)
188         fputs("\t+", stdout);
189     putchar('\n');
190 }
191
192 /*
193  * Trim leading/trailing blanks, as well as blanks after a comma.
194  * Convert embedded blanks to commas.
195  */
196 static char *
197 trimmed_tab_list(const char *source)
198 {
199     char *result = strdup(source);
200     int ch, j, k, last;
201
202     if (result != 0) {
203         for (j = k = last = 0; result[j] != 0; ++j) {
204             ch = UChar(result[j]);
205             if (isspace(ch)) {
206                 if (last == '\0') {
207                     continue;
208                 } else if (isdigit(last) || last == ',') {
209                     ch = ',';
210                 }
211             } else if (ch == ',') {
212                 ;
213             } else {
214                 if (last == ',')
215                     result[k++] = (char) last;
216                 result[k++] = (char) ch;
217             }
218             last = ch;
219         }
220         result[k] = '\0';
221     }
222     return result;
223 }
224
225 static bool
226 comma_is_needed(const char *source)
227 {
228     bool result = FALSE;
229
230     if (source != 0) {
231         size_t len = strlen(source);
232         if (len != 0)
233             result = (source[len - 1] != ',');
234     } else {
235         result = FALSE;
236     }
237     return result;
238 }
239
240 /*
241  * Add a command-line parameter to the tab-list.  It can be blank- or comma-
242  * separated (or a mixture).  For simplicity, empty tabs are ignored, e.g.,
243  *      tabs 1,,6,11
244  *      tabs 1,6,11
245  * are treated the same.
246  */
247 static const char *
248 add_to_tab_list(char **append, const char *value)
249 {
250     char *result = *append;
251     char *copied = trimmed_tab_list(value);
252
253     if (copied != 0 && *copied != '\0') {
254         const char *comma = ",";
255         size_t need = 1 + strlen(copied);
256
257         if (*copied == ',')
258             comma = "";
259         else if (!comma_is_needed(*append))
260             comma = "";
261
262         need += strlen(comma);
263         if (*append != 0)
264             need += strlen(*append);
265
266         result = malloc(need);
267         if (result != 0) {
268             *result = '\0';
269             if (*append != 0) {
270                 _nc_STRCPY(result, *append, need);
271                 free(*append);
272             }
273             _nc_STRCAT(result, comma, need);
274             _nc_STRCAT(result, copied, need);
275         }
276
277         *append = result;
278     }
279     return result;
280 }
281
282 /*
283  * Check for illegal characters in the tab-list.
284  */
285 static bool
286 legal_tab_list(const char *program, const char *tab_list)
287 {
288     bool result = TRUE;
289
290     if (tab_list != 0 && *tab_list != '\0') {
291         if (comma_is_needed(tab_list)) {
292             int n, ch;
293             for (n = 0; tab_list[n] != '\0'; ++n) {
294                 ch = UChar(tab_list[n]);
295                 if (!(isdigit(ch) || ch == ',' || ch == '+')) {
296                     fprintf(stderr,
297                             "%s: unexpected character found '%c'\n",
298                             program, ch);
299                     result = FALSE;
300                     break;
301                 }
302             }
303         } else {
304             fprintf(stderr, "%s: trailing comma found '%s'\n", program, tab_list);
305             result = FALSE;
306         }
307     } else {
308         fprintf(stderr, "%s: no tab-list given\n", program);
309         result = FALSE;
310     }
311     return result;
312 }
313
314 static void
315 usage(void)
316 {
317     static const char *msg[] =
318     {
319         "Usage: tabs [options] [tabstop-list]"
320         ,""
321         ,"Options:"
322         ,"  -0       reset tabs"
323         ,"  -8       set tabs to standard interval"
324         ,"  -a       Assembler, IBM S/370, first format"
325         ,"  -a2      Assembler, IBM S/370, second format"
326         ,"  -c       COBOL, normal format"
327         ,"  -c2      COBOL compact format"
328         ,"  -c3      COBOL compact format extended"
329         ,"  -d       debug (show ruler with expected/actual tab positions)"
330         ,"  -f       FORTRAN"
331         ,"  -n       no-op (do not modify terminal settings)"
332         ,"  -p       PL/I"
333         ,"  -s       SNOBOL"
334         ,"  -u       UNIVAC 1100 Assembler"
335         ,"  -T name  use terminal type 'name'"
336         ,""
337         ,"A tabstop-list is an ordered list of column numbers, e.g., 1,11,21"
338         ,"or 1,+10,+10 which is the same."
339     };
340     unsigned n;
341
342     fflush(stdout);
343     for (n = 0; n < SIZEOF(msg); ++n) {
344         fprintf(stderr, "%s\n", msg[n]);
345     }
346     ExitProgram(EXIT_FAILURE);
347 }
348
349 int
350 main(int argc, char *argv[])
351 {
352     int rc = EXIT_FAILURE;
353     bool debug = FALSE;
354     bool no_op = FALSE;
355     int n, ch;
356     NCURSES_CONST char *term_name = 0;
357     char *append = 0;
358     const char *tab_list = 0;
359
360     if ((term_name = getenv("TERM")) == 0)
361         term_name = "ansi+tabs";
362
363     /* cannot use getopt, since some options are two-character */
364     for (n = 1; n < argc; ++n) {
365         char *option = argv[n];
366         switch (option[0]) {
367         case '-':
368             while ((ch = *++option) != '\0') {
369                 switch (ch) {
370                 case 'a':
371                     switch (*option) {
372                     case '\0':
373                         tab_list = "1,10,16,36,72";
374                         /* Assembler, IBM S/370, first format */
375                         break;
376                     case '2':
377                         tab_list = "1,10,16,40,72";
378                         /* Assembler, IBM S/370, second format */
379                         break;
380                     default:
381                         usage();
382                     }
383                     break;
384                 case 'c':
385                     switch (*option) {
386                     case '\0':
387                         tab_list = "1,8,12,16,20,55";
388                         /* COBOL, normal format */
389                         break;
390                     case '2':
391                         tab_list = "1,6,10,14,49";
392                         /* COBOL compact format */
393                         break;
394                     case '3':
395                         tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67";
396                         /* COBOL compact format extended */
397                         break;
398                     default:
399                         usage();
400                     }
401                     break;
402                 case 'd':       /* ncurses extension */
403                     debug = TRUE;
404                     break;
405                 case 'f':
406                     tab_list = "1,7,11,15,19,23";
407                     /* FORTRAN */
408                     break;
409                 case 'n':       /* ncurses extension */
410                     no_op = TRUE;
411                     break;
412                 case 'p':
413                     tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
414                     /* PL/I */
415                     break;
416                 case 's':
417                     tab_list = "1,10,55";
418                     /* SNOBOL */
419                     break;
420                 case 'u':
421                     tab_list = "1,12,20,44";
422                     /* UNIVAC 1100 Assembler */
423                     break;
424                 case 'T':
425                     ++n;
426                     if (*++option != '\0') {
427                         term_name = option;
428                     } else {
429                         term_name = argv[n++];
430                     }
431                     option += ((int) strlen(option)) - 1;
432                     continue;
433                 default:
434                     if (isdigit(UChar(*option))) {
435                         tab_list = option;
436                         ++n;
437                     } else {
438                         usage();
439                     }
440                     option += ((int) strlen(option)) - 1;
441                     break;
442                 }
443             }
444             break;
445         case '+':
446             while ((ch = *++option) != '\0') {
447                 switch (ch) {
448                 case 'm':
449                     /*
450                      * The "+mXXX" option is unimplemented because only the long-obsolete
451                      * att510d implements smgl, which is needed to support
452                      * this option.
453                      */
454                     break;
455                 default:
456                     /* special case of relative stops separated by spaces? */
457                     if (option == argv[n] + 1) {
458                         tab_list = add_to_tab_list(&append, argv[n]);
459                     }
460                     break;
461                 }
462             }
463             break;
464         default:
465             if (append != 0) {
466                 if (tab_list != (const char *) append) {
467                     /* one of the predefined options was used */
468                     free(append);
469                     append = 0;
470                 }
471             }
472             tab_list = add_to_tab_list(&append, option);
473             break;
474         }
475     }
476
477     setupterm(term_name, STDOUT_FILENO, (int *) 0);
478
479     max_cols = (columns > 0) ? columns : 80;
480
481     if (!VALID_STRING(clear_all_tabs)) {
482         fprintf(stderr,
483                 "%s: terminal type '%s' cannot reset tabs\n",
484                 argv[0], term_name);
485     } else if (!VALID_STRING(set_tab)) {
486         fprintf(stderr,
487                 "%s: terminal type '%s' cannot set tabs\n",
488                 argv[0], term_name);
489     } else if (legal_tab_list(argv[0], tab_list)) {
490         int *list = decode_tabs(tab_list);
491
492         if (!no_op)
493             tputs(clear_all_tabs, 1, putch);
494
495         if (list != 0) {
496             if (!no_op)
497                 do_tabs(list);
498             if (debug) {
499                 fflush(stderr);
500                 printf("tabs %s\n", tab_list);
501                 print_ruler(list);
502                 write_tabs(list);
503             }
504             free(list);
505         } else if (debug) {
506             fflush(stderr);
507             printf("tabs %s\n", tab_list);
508         }
509         rc = EXIT_SUCCESS;
510     }
511     if (append != 0)
512         free(append);
513     ExitProgram(rc);
514 }