]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tput.c
aa05baf696fa8b4989b21fa5881c7496cd530530
[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.68 2017/01/07 23:08:24 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                     if (bool_from_termcap[np->nte_index])
184                         name = boolnames[np->nte_index];
185                     break;
186
187                 case NUMBER:
188                     if (num_from_termcap[np->nte_index])
189                         name = numnames[np->nte_index];
190                     break;
191
192                 case STRING:
193                     if (str_from_termcap[np->nte_index])
194                         name = strnames[np->nte_index];
195                     break;
196                 }
197                 goto retry;
198             }
199         }
200 #endif
201         quit(4, "unknown terminfo capability '%s'", name);
202     } else if (s != ABSENT_STRING) {
203         if (argc > 1) {
204             int k;
205             int ignored;
206             long numbers[1 + NUM_PARM];
207             char *strings[1 + NUM_PARM];
208             char *p_is_s[NUM_PARM];
209
210             /* Nasty hack time. The tparm function needs to see numeric
211              * parameters as numbers, not as pointers to their string
212              * representations
213              */
214
215             for (k = 1; k < argc; k++) {
216                 char *tmp = 0;
217                 strings[k] = argv[k];
218                 numbers[k] = strtol(argv[k], &tmp, 0);
219                 if (tmp == 0 || *tmp != 0)
220                     numbers[k] = 0;
221             }
222             for (k = argc; k <= NUM_PARM; k++) {
223                 numbers[k] = 0;
224                 strings[k] = 0;
225             }
226
227             switch (tparm_type(name)) {
228             case Num_Str:
229                 s = TPARM_2(s, numbers[1], strings[2]);
230                 break;
231             case Num_Str_Str:
232                 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
233                 break;
234             case Numbers:
235             default:
236                 (void) _nc_tparm_analyze(s, p_is_s, &ignored);
237 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
238                 s = TPARM_9(s,
239                             myParam(1),
240                             myParam(2),
241                             myParam(3),
242                             myParam(4),
243                             myParam(5),
244                             myParam(6),
245                             myParam(7),
246                             myParam(8),
247                             myParam(9));
248                 break;
249             }
250         }
251
252         /* use putp() in order to perform padding */
253         putp(s);
254         return exit_code(STRING, 0);
255     }
256     return exit_code(STRING, 1);
257 }
258
259 int
260 main(int argc, char **argv)
261 {
262     char *term;
263     int errret;
264     bool cmdline = TRUE;
265     int c;
266     char buf[BUFSIZ];
267     int result = 0;
268     int fd;
269     TTY tty_settings;
270
271     prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
272
273     term = getenv("TERM");
274
275     while ((c = getopt(argc, argv, "ST:V")) != -1) {
276         switch (c) {
277         case 'S':
278             cmdline = FALSE;
279             break;
280         case 'T':
281             use_env(FALSE);
282             term = optarg;
283             break;
284         case 'V':
285             puts(curses_version());
286             ExitProgram(EXIT_SUCCESS);
287         default:
288             usage();
289             /* NOTREACHED */
290         }
291     }
292
293     /*
294      * Modify the argument list to omit the options we processed.
295      */
296     if (is_clear || is_reset || is_init) {
297         if (optind-- < argc) {
298             argc -= optind;
299             argv += optind;
300         }
301         argv[0] = prg_name;
302     } else {
303         argc -= optind;
304         argv += optind;
305     }
306
307     if (term == 0 || *term == '\0')
308         quit(2, "No value for $TERM and no -T specified");
309
310     fd = save_tty_settings(&tty_settings);
311
312     if (setupterm(term, fd, &errret) != OK && errret <= 0)
313         quit(3, "unknown terminal \"%s\"", term);
314
315     if (cmdline) {
316         if ((argc <= 0) && !(is_clear || is_reset || is_init))
317             usage();
318         ExitProgram(tput_cmd(fd, &tty_settings, argc, argv));
319     }
320
321     while (fgets(buf, sizeof(buf), stdin) != 0) {
322         char *argvec[16];       /* command, 9 parms, null, & slop */
323         int argnum = 0;
324         char *cp;
325
326         /* crack the argument list into a dope vector */
327         for (cp = buf; *cp; cp++) {
328             if (isspace(UChar(*cp))) {
329                 *cp = '\0';
330             } else if (cp == buf || cp[-1] == 0) {
331                 argvec[argnum++] = cp;
332                 if (argnum >= (int) SIZEOF(argvec) - 1)
333                     break;
334             }
335         }
336         argvec[argnum] = 0;
337
338         if (argnum != 0
339             && tput_cmd(fd, &tty_settings, argnum, argvec) != 0) {
340             if (result == 0)
341                 result = 4;     /* will return value >4 */
342             ++result;
343         }
344     }
345
346     ExitProgram(result);
347 }