]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tput.c
ncurses 6.0 - patch 20170204
[ncurses.git] / progs / tput.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 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 #include <tty_settings.h>
52
53 MODULE_ID("$Id: tput.c,v 1.69 2017/01/21 17:40:51 tom Exp $")
54
55 #define PUTS(s)         fputs(s, stdout)
56
57 const char *_nc_progname = "tput";
58
59 static char *prg_name;
60 static bool is_init = FALSE;
61 static bool is_reset = FALSE;
62 static bool is_clear = FALSE;
63
64 static void
65 quit(int status, const char *fmt,...)
66 {
67     va_list argp;
68
69     va_start(argp, fmt);
70     fprintf(stderr, "%s: ", prg_name);
71     vfprintf(stderr, fmt, argp);
72     fprintf(stderr, "\n");
73     va_end(argp);
74     ExitProgram(status);
75 }
76
77 static void
78 usage(void)
79 {
80     fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
81     ExitProgram(EXIT_FAILURE);
82 }
83
84 static char *
85 check_aliases(char *name, bool program)
86 {
87     static char my_init[] = "init";
88     static char my_reset[] = "reset";
89     static char my_clear[] = "clear";
90
91     char *result = name;
92     if ((is_init = same_program(name, program ? PROG_INIT : my_init)))
93         result = my_init;
94     if ((is_reset = same_program(name, program ? PROG_RESET : my_reset)))
95         result = my_reset;
96     if ((is_clear = same_program(name, program ? PROG_CLEAR : my_clear)))
97         result = my_clear;
98     return result;
99 }
100
101 static int
102 exit_code(int token, int value)
103 {
104     int result = 99;
105
106     switch (token) {
107     case BOOLEAN:
108         result = !value;        /* TRUE=0, FALSE=1 */
109         break;
110     case NUMBER:
111         result = 0;             /* always zero */
112         break;
113     case STRING:
114         result = value;         /* 0=normal, 1=missing */
115         break;
116     }
117     return result;
118 }
119
120 static int
121 tput_cmd(int fd, TTY * saved_settings, int argc, char *argv[])
122 {
123     NCURSES_CONST char *name;
124     char *s;
125     int status;
126 #if !PURE_TERMINFO
127     bool termcap = FALSE;
128 #endif
129
130     name = check_aliases(argv[0], FALSE);
131     if (is_reset || is_init) {
132         TTY oldmode;
133
134         int terasechar = -1;    /* new erase character */
135         int intrchar = -1;      /* new interrupt character */
136         int tkillchar = -1;     /* new kill character */
137
138         if (is_reset) {
139             reset_start(stdout, TRUE, FALSE);
140             reset_tty_settings(fd, saved_settings);
141         } else {
142             reset_start(stdout, FALSE, TRUE);
143         }
144
145 #if HAVE_SIZECHANGE
146         set_window_size(fd, &lines, &columns);
147 #else
148         (void) fd;
149 #endif
150         set_control_chars(saved_settings, terasechar, intrchar, tkillchar);
151         set_conversions(saved_settings);
152         if (send_init_strings(fd, &oldmode)) {
153             reset_flush();
154         }
155
156         update_tty_settings(&oldmode, saved_settings);
157         return 0;
158     }
159
160     if (strcmp(name, "longname") == 0) {
161         PUTS(longname());
162         return 0;
163     }
164 #if !PURE_TERMINFO
165   retry:
166 #endif
167     if (strcmp(name, "clear") == 0) {
168         return clear_cmd();
169     } else if ((status = tigetflag(name)) != -1) {
170         return exit_code(BOOLEAN, status);
171     } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
172         (void) printf("%d\n", status);
173         return exit_code(NUMBER, 0);
174     } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
175 #if !PURE_TERMINFO
176         if (!termcap) {
177             const struct name_table_entry *np;
178
179             termcap = TRUE;
180             if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
181                 switch (np->nte_type) {
182                 case BOOLEAN:
183                     name = boolnames[np->nte_index];
184                     break;
185
186                 case NUMBER:
187                     name = numnames[np->nte_index];
188                     break;
189
190                 case STRING:
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     int fd;
266     TTY tty_settings;
267
268     prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
269
270     term = getenv("TERM");
271
272     while ((c = getopt(argc, argv, "ST:V")) != -1) {
273         switch (c) {
274         case 'S':
275             cmdline = FALSE;
276             break;
277         case 'T':
278             use_env(FALSE);
279             term = optarg;
280             break;
281         case 'V':
282             puts(curses_version());
283             ExitProgram(EXIT_SUCCESS);
284         default:
285             usage();
286             /* NOTREACHED */
287         }
288     }
289
290     /*
291      * Modify the argument list to omit the options we processed.
292      */
293     if (is_clear || is_reset || is_init) {
294         if (optind-- < argc) {
295             argc -= optind;
296             argv += optind;
297         }
298         argv[0] = prg_name;
299     } else {
300         argc -= optind;
301         argv += optind;
302     }
303
304     if (term == 0 || *term == '\0')
305         quit(2, "No value for $TERM and no -T specified");
306
307     fd = save_tty_settings(&tty_settings);
308
309     if (setupterm(term, fd, &errret) != OK && errret <= 0)
310         quit(3, "unknown terminal \"%s\"", term);
311
312     if (cmdline) {
313         if ((argc <= 0) && !(is_clear || is_reset || is_init))
314             usage();
315         ExitProgram(tput_cmd(fd, &tty_settings, argc, argv));
316     }
317
318     while (fgets(buf, sizeof(buf), stdin) != 0) {
319         char *argvec[16];       /* command, 9 parms, null, & slop */
320         int argnum = 0;
321         char *cp;
322
323         /* crack the argument list into a dope vector */
324         for (cp = buf; *cp; cp++) {
325             if (isspace(UChar(*cp))) {
326                 *cp = '\0';
327             } else if (cp == buf || cp[-1] == 0) {
328                 argvec[argnum++] = cp;
329                 if (argnum >= (int) SIZEOF(argvec) - 1)
330                     break;
331             }
332         }
333         argvec[argnum] = 0;
334
335         if (argnum != 0
336             && tput_cmd(fd, &tty_settings, argnum, argvec) != 0) {
337             if (result == 0)
338                 result = 4;     /* will return value >4 */
339             ++result;
340         }
341     }
342
343     ExitProgram(result);
344 }