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