]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tabs.c
ncurses 5.7 - patch 20100501
[ncurses.git] / progs / tabs.c
1 /****************************************************************************
2  * Copyright (c) 2008-2009,2010 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.17 2010/05/01 22:04:08 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 && value <= result[n - 1]) {
94                     fprintf(stderr,
95                             "tab-stops are not in increasing order\n");
96                     free(result);
97                     result = 0;
98                     break;
99                 }
100                 ++n;
101                 value = 0;
102                 prior = 0;
103             } else if (ch == '+') {
104                 if (n)
105                     prior = result[n - 1];
106             }
107         }
108     }
109
110     if (result != 0) {
111         /*
112          * If there is only one value, then it is an option such as "-8".
113          */
114         if ((n == 0) && (value > 0)) {
115             int step = value;
116             while (n < max_cols - 1) {
117                 result[n++] = value;
118                 value += step;
119             }
120         }
121
122         /*
123          * Add the last value, if any.
124          */
125         result[n++] = value;
126         result[n] = 0;
127     }
128     return result;
129 }
130
131 static void
132 print_ruler(int *tab_list)
133 {
134     int last = 0;
135     int stop;
136     int n;
137
138     /* first print a readable ruler */
139     for (n = 0; n < max_cols; n += 10) {
140         int ch = 1 + (n / 10);
141         char buffer[20];
142         sprintf(buffer, "----+----%c",
143                 ((ch < 10)
144                  ? (ch + '0')
145                  : (ch + 'A' - 10)));
146         printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
147     }
148     putchar('\n');
149
150     /* now, print '*' for each stop */
151     for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) {
152         stop = tab_list[n];
153         while (++last < stop) {
154             if (last <= max_cols) {
155                 putchar('-');
156             } else {
157                 break;
158             }
159         }
160         if (last <= max_cols) {
161             putchar('*');
162             last = stop;
163         } else {
164             break;
165         }
166     }
167     while (++last <= max_cols)
168         putchar('-');
169     putchar('\n');
170 }
171
172 /*
173  * Write an '*' on each tabstop, to demonstrate whether it lines up with the
174  * ruler.
175  */
176 static void
177 write_tabs(int *tab_list)
178 {
179     int stop;
180
181     while ((stop = *tab_list++) > 0 && stop <= max_cols) {
182         fputs((stop == 1) ? "*" : "\t*", stdout);
183     };
184     /* also show a tab _past_ the stops */
185     if (stop < max_cols)
186         fputs("\t+", stdout);
187     putchar('\n');
188 }
189
190 /*
191  * Trim leading/trailing blanks, as well as blanks after a comma.
192  * Convert embedded blanks to commas.
193  */
194 static char *
195 trimmed_tab_list(const char *source)
196 {
197     char *result = strdup(source);
198     int ch, j, k, last;
199
200     if (result != 0) {
201         for (j = k = last = 0; result[j] != 0; ++j) {
202             ch = UChar(result[j]);
203             if (isspace(ch)) {
204                 if (last == '\0') {
205                     continue;
206                 } else if (isdigit(last) || last == ',') {
207                     ch = ',';
208                 }
209             } else if (ch == ',') {
210                 ;
211             } else {
212                 if (last == ',')
213                     result[k++] = (char) last;
214                 result[k++] = (char) ch;
215             }
216             last = ch;
217         }
218         result[k] = '\0';
219     }
220     return result;
221 }
222
223 static bool
224 comma_is_needed(const char *source)
225 {
226     bool result = FALSE;
227
228     if (source != 0) {
229         unsigned len = strlen(source);
230         if (len != 0)
231             result = (source[len - 1] != ',');
232     } else {
233         result = FALSE;
234     }
235     return result;
236 }
237
238 /*
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.,
241  *      tabs 1,,6,11
242  *      tabs 1,6,11
243  * are treated the same.
244  */
245 static const char *
246 add_to_tab_list(char **append, const char *value)
247 {
248     char *result = *append;
249     char *copied = trimmed_tab_list(value);
250
251     if (copied != 0 && *copied != '\0') {
252         const char *comma = ",";
253         unsigned need = 1 + strlen(copied);
254
255         if (*copied == ',')
256             comma = "";
257         else if (!comma_is_needed(*append))
258             comma = "";
259
260         need += strlen(comma);
261         if (*append != 0)
262             need += strlen(*append);
263
264         result = malloc(need);
265         if (result != 0) {
266             *result = '\0';
267             if (*append != 0) {
268                 strcpy(result, *append);
269                 free(*append);
270             }
271             strcat(result, comma);
272             strcat(result, copied);
273         }
274
275         *append = result;
276     }
277     return result;
278 }
279
280 /*
281  * Check for illegal characters in the tab-list.
282  */
283 static bool
284 legal_tab_list(const char *program, const char *tab_list)
285 {
286     bool result = TRUE;
287
288     if (tab_list != 0 && *tab_list != '\0') {
289         if (comma_is_needed(tab_list)) {
290             int n, ch;
291             for (n = 0; tab_list[n] != '\0'; ++n) {
292                 ch = UChar(tab_list[n]);
293                 if (!(isdigit(ch) || ch == ',')) {
294                     fprintf(stderr,
295                             "%s: unexpected character found '%c'\n",
296                             program, ch);
297                     result = FALSE;
298                     break;
299                 }
300             }
301         } else {
302             fprintf(stderr, "%s: trailing comma found '%s'\n", program, tab_list);
303             result = FALSE;
304         }
305     } else {
306         fprintf(stderr, "%s: no tab-list given\n", program);
307         result = FALSE;
308     }
309     return result;
310 }
311
312 static void
313 usage(void)
314 {
315     static const char *msg[] =
316     {
317         "Usage: tabs [options] [tabstop-list]"
318         ,""
319         ,"Options:"
320         ,"  -0       reset tabs"
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)"
328         ,"  -f       FORTRAN"
329         ,"  -n       no-op (do not modify terminal settings)"
330         ,"  -p       PL/I"
331         ,"  -s       SNOBOL"
332         ,"  -u       UNIVAC 1100 Assembler"
333         ,"  -T name  use terminal type 'name'"
334         ,""
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."
337     };
338     unsigned n;
339
340     fflush(stdout);
341     for (n = 0; n < SIZEOF(msg); ++n) {
342         fprintf(stderr, "%s\n", msg[n]);
343     }
344     ExitProgram(EXIT_FAILURE);
345 }
346
347 int
348 main(int argc, char *argv[])
349 {
350     int rc = EXIT_FAILURE;
351     bool debug = FALSE;
352     bool no_op = FALSE;
353     int n, ch;
354     NCURSES_CONST char *term_name = 0;
355     const char *mar_list = 0;   /* ignored */
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                     mar_list = option;
449                     break;
450                 default:
451                     usage();
452                 }
453             }
454             break;
455         default:
456             if (append != 0) {
457                 if (tab_list != (const char *) append) {
458                     /* one of the predefined options was used */
459                     free(append);
460                     append = 0;
461                 }
462             }
463             tab_list = add_to_tab_list(&append, option);
464             break;
465         }
466     }
467
468     setupterm(term_name, STDOUT_FILENO, (int *) 0);
469
470     max_cols = (columns > 0) ? columns : 80;
471
472     if (!VALID_STRING(clear_all_tabs)) {
473         fprintf(stderr,
474                 "%s: terminal type '%s' cannot reset tabs\n",
475                 argv[0], term_name);
476     } else if (!VALID_STRING(set_tab)) {
477         fprintf(stderr,
478                 "%s: terminal type '%s' cannot set tabs\n",
479                 argv[0], term_name);
480     } else if (legal_tab_list(argv[0], tab_list)) {
481         int *list = decode_tabs(tab_list);
482
483         if (!no_op)
484             tputs(clear_all_tabs, 1, putch);
485
486         if (list != 0) {
487             if (!no_op)
488                 do_tabs(list);
489             if (debug) {
490                 fflush(stderr);
491                 printf("tabs %s\n", tab_list);
492                 print_ruler(list);
493                 write_tabs(list);
494             }
495             free(list);
496         } else if (debug) {
497             fflush(stderr);
498             printf("tabs %s\n", tab_list);
499         }
500         rc = EXIT_SUCCESS;
501     }
502     if (append != 0)
503         free(append);
504     ExitProgram(rc);
505 }