]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tput.c
ncurses 6.0 - patch 20161203
[ncurses.git] / progs / tput.c
1 /****************************************************************************
2  * Copyright (c) 1998-2015,2016 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  * tput.c -- shellscript access to terminal capabilities
37  *
38  * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
39  * Ross Ridge's mytinfo package.
40  */
41
42 #include <tparm_type.h>
43 #include <clear_cmd.h>
44 #include <reset_cmd.h>
45
46 #if !PURE_TERMINFO
47 #include <dump_entry.h>
48 #include <termsort.c>
49 #endif
50 #include <transform.h>
51
52 MODULE_ID("$Id: tput.c,v 1.63 2016/10/23 01:08:28 tom Exp $")
53
54 #define PUTS(s)         fputs(s, stdout)
55
56 const char *_nc_progname = "tput";
57
58 static char *prg_name;
59 static bool is_init = FALSE;
60 static bool is_reset = FALSE;
61 static bool is_clear = FALSE;
62
63 static void
64 quit(int status, const char *fmt,...)
65 {
66     va_list argp;
67
68     va_start(argp, fmt);
69     fprintf(stderr, "%s: ", prg_name);
70     vfprintf(stderr, fmt, argp);
71     fprintf(stderr, "\n");
72     va_end(argp);
73     ExitProgram(status);
74 }
75
76 static void
77 usage(void)
78 {
79     fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
80     ExitProgram(EXIT_FAILURE);
81 }
82
83 static char *
84 check_aliases(char *name, bool program)
85 {
86     static char my_init[] = "init";
87     static char my_reset[] = "reset";
88     static char my_clear[] = "clear";
89
90     char *result = name;
91     if ((is_init = same_program(name, program ? PROG_INIT : my_init)))
92         result = my_init;
93     if ((is_reset = same_program(name, program ? PROG_RESET : my_reset)))
94         result = my_reset;
95     if ((is_clear = same_program(name, program ? PROG_CLEAR : my_clear)))
96         result = my_clear;
97     return result;
98 }
99
100 static int
101 exit_code(int token, int value)
102 {
103     int result = 99;
104
105     switch (token) {
106     case BOOLEAN:
107         result = !value;        /* TRUE=0, FALSE=1 */
108         break;
109     case NUMBER:
110         result = 0;             /* always zero */
111         break;
112     case STRING:
113         result = value;         /* 0=normal, 1=missing */
114         break;
115     }
116     return result;
117 }
118
119 static int
120 tput_cmd(int argc, char *argv[])
121 {
122     NCURSES_CONST char *name;
123     char *s;
124     int status;
125 #if !PURE_TERMINFO
126     bool termcap = FALSE;
127 #endif
128
129     name = check_aliases(argv[0], FALSE);
130     if (is_reset || is_init) {
131         TTY mode, oldmode;
132
133         int terasechar = -1;    /* new erase character */
134         int intrchar = -1;      /* new interrupt character */
135         int tkillchar = -1;     /* new kill character */
136
137         int my_fd = save_tty_settings(&mode);
138
139         reset_start(stdout, is_reset, is_init);
140         reset_tty_settings(&mode);
141
142 #if HAVE_SIZECHANGE
143         set_window_size(my_fd, &lines, &columns);
144 #else
145         (void) my_fd;
146 #endif
147         set_control_chars(&mode, terasechar, intrchar, tkillchar);
148         set_conversions(&mode);
149         if (send_init_strings(&oldmode)) {
150             reset_flush();
151         }
152
153         update_tty_settings(&oldmode, &mode);
154         return 0;
155     }
156
157     if (strcmp(name, "longname") == 0) {
158         PUTS(longname());
159         return 0;
160     }
161 #if !PURE_TERMINFO
162   retry:
163 #endif
164     if (strcmp(name, "clear") == 0) {
165         return clear_cmd();
166     } else if ((status = tigetflag(name)) != -1) {
167         return exit_code(BOOLEAN, status);
168     } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
169         (void) printf("%d\n", status);
170         return exit_code(NUMBER, 0);
171     } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
172 #if !PURE_TERMINFO
173         if (!termcap) {
174             const struct name_table_entry *np;
175
176             termcap = TRUE;
177             if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
178                 switch (np->nte_type) {
179                 case BOOLEAN:
180                     if (bool_from_termcap[np->nte_index])
181                         name = boolnames[np->nte_index];
182                     break;
183
184                 case NUMBER:
185                     if (num_from_termcap[np->nte_index])
186                         name = numnames[np->nte_index];
187                     break;
188
189                 case STRING:
190                     if (str_from_termcap[np->nte_index])
191                         name = strnames[np->nte_index];
192                     break;
193                 }
194                 goto retry;
195             }
196         }
197 #endif
198         quit(4, "unknown terminfo capability '%s'", name);
199     } else if (s != ABSENT_STRING) {
200         if (argc > 1) {
201             int k;
202             int ignored;
203             long numbers[1 + NUM_PARM];
204             char *strings[1 + NUM_PARM];
205             char *p_is_s[NUM_PARM];
206
207             /* Nasty hack time. The tparm function needs to see numeric
208              * parameters as numbers, not as pointers to their string
209              * representations
210              */
211
212             for (k = 1; k < argc; k++) {
213                 char *tmp = 0;
214                 strings[k] = argv[k];
215                 numbers[k] = strtol(argv[k], &tmp, 0);
216                 if (tmp == 0 || *tmp != 0)
217                     numbers[k] = 0;
218             }
219             for (k = argc; k <= NUM_PARM; k++) {
220                 numbers[k] = 0;
221                 strings[k] = 0;
222             }
223
224             switch (tparm_type(name)) {
225             case Num_Str:
226                 s = TPARM_2(s, numbers[1], strings[2]);
227                 break;
228             case Num_Str_Str:
229                 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
230                 break;
231             case Numbers:
232             default:
233                 (void) _nc_tparm_analyze(s, p_is_s, &ignored);
234 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
235                 s = TPARM_9(s,
236                             myParam(1),
237                             myParam(2),
238                             myParam(3),
239                             myParam(4),
240                             myParam(5),
241                             myParam(6),
242                             myParam(7),
243                             myParam(8),
244                             myParam(9));
245                 break;
246             }
247         }
248
249         /* use putp() in order to perform padding */
250         putp(s);
251         return exit_code(STRING, 0);
252     }
253     return exit_code(STRING, 1);
254 }
255
256 int
257 main(int argc, char **argv)
258 {
259     char *term;
260     int errret;
261     bool cmdline = TRUE;
262     int c;
263     char buf[BUFSIZ];
264     int result = 0;
265
266     prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
267
268     term = getenv("TERM");
269
270     while ((c = getopt(argc, argv, "ST:V")) != -1) {
271         switch (c) {
272         case 'S':
273             cmdline = FALSE;
274             break;
275         case 'T':
276             use_env(FALSE);
277             term = optarg;
278             break;
279         case 'V':
280             puts(curses_version());
281             ExitProgram(EXIT_SUCCESS);
282         default:
283             usage();
284             /* NOTREACHED */
285         }
286     }
287
288     /*
289      * Modify the argument list to omit the options we processed.
290      */
291     if (is_clear || is_reset || is_init) {
292         if (optind-- < argc) {
293             argc -= optind;
294             argv += optind;
295         }
296         argv[0] = prg_name;
297     } else {
298         argc -= optind;
299         argv += optind;
300     }
301
302     if (term == 0 || *term == '\0')
303         quit(2, "No value for $TERM and no -T specified");
304
305     if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
306         quit(3, "unknown terminal \"%s\"", term);
307
308     if (cmdline) {
309         if ((argc <= 0) && !(is_clear || is_reset || is_init))
310             usage();
311         ExitProgram(tput_cmd(argc, argv));
312     }
313
314     while (fgets(buf, sizeof(buf), stdin) != 0) {
315         char *argvec[16];       /* command, 9 parms, null, & slop */
316         int argnum = 0;
317         char *cp;
318
319         /* crack the argument list into a dope vector */
320         for (cp = buf; *cp; cp++) {
321             if (isspace(UChar(*cp))) {
322                 *cp = '\0';
323             } else if (cp == buf || cp[-1] == 0) {
324                 argvec[argnum++] = cp;
325                 if (argnum >= (int) SIZEOF(argvec) - 1)
326                     break;
327             }
328         }
329         argvec[argnum] = 0;
330
331         if (argnum != 0
332             && tput_cmd(argnum, argvec) != 0) {
333             if (result == 0)
334                 result = 4;     /* will return value >4 */
335             ++result;
336         }
337     }
338
339     ExitProgram(result);
340 }