]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tput.c
ncurses 6.0 - patch 20161224
[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 #include <tty_settings.h>
52
53 MODULE_ID("$Id: tput.c,v 1.65 2016/12/24 18:44:32 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         reset_start(stdout, is_reset, is_init);
139         reset_tty_settings(fd, saved_settings);
140
141 #if HAVE_SIZECHANGE
142         set_window_size(fd, &lines, &columns);
143 #else
144         (void) fd;
145 #endif
146         set_control_chars(saved_settings, terasechar, intrchar, tkillchar);
147         set_conversions(saved_settings);
148         if (send_init_strings(fd, &oldmode)) {
149             reset_flush();
150         }
151
152         update_tty_settings(&oldmode, saved_settings);
153         return 0;
154     }
155
156     if (strcmp(name, "longname") == 0) {
157         PUTS(longname());
158         return 0;
159     }
160 #if !PURE_TERMINFO
161   retry:
162 #endif
163     if (strcmp(name, "clear") == 0) {
164         return clear_cmd();
165     } else if ((status = tigetflag(name)) != -1) {
166         return exit_code(BOOLEAN, status);
167     } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
168         (void) printf("%d\n", status);
169         return exit_code(NUMBER, 0);
170     } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
171 #if !PURE_TERMINFO
172         if (!termcap) {
173             const struct name_table_entry *np;
174
175             termcap = TRUE;
176             if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
177                 switch (np->nte_type) {
178                 case BOOLEAN:
179                     if (bool_from_termcap[np->nte_index])
180                         name = boolnames[np->nte_index];
181                     break;
182
183                 case NUMBER:
184                     if (num_from_termcap[np->nte_index])
185                         name = numnames[np->nte_index];
186                     break;
187
188                 case STRING:
189                     if (str_from_termcap[np->nte_index])
190                         name = strnames[np->nte_index];
191                     break;
192                 }
193                 goto retry;
194             }
195         }
196 #endif
197         quit(4, "unknown terminfo capability '%s'", name);
198     } else if (s != ABSENT_STRING) {
199         if (argc > 1) {
200             int k;
201             int ignored;
202             long numbers[1 + NUM_PARM];
203             char *strings[1 + NUM_PARM];
204             char *p_is_s[NUM_PARM];
205
206             /* Nasty hack time. The tparm function needs to see numeric
207              * parameters as numbers, not as pointers to their string
208              * representations
209              */
210
211             for (k = 1; k < argc; k++) {
212                 char *tmp = 0;
213                 strings[k] = argv[k];
214                 numbers[k] = strtol(argv[k], &tmp, 0);
215                 if (tmp == 0 || *tmp != 0)
216                     numbers[k] = 0;
217             }
218             for (k = argc; k <= NUM_PARM; k++) {
219                 numbers[k] = 0;
220                 strings[k] = 0;
221             }
222
223             switch (tparm_type(name)) {
224             case Num_Str:
225                 s = TPARM_2(s, numbers[1], strings[2]);
226                 break;
227             case Num_Str_Str:
228                 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
229                 break;
230             case Numbers:
231             default:
232                 (void) _nc_tparm_analyze(s, p_is_s, &ignored);
233 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
234                 s = TPARM_9(s,
235                             myParam(1),
236                             myParam(2),
237                             myParam(3),
238                             myParam(4),
239                             myParam(5),
240                             myParam(6),
241                             myParam(7),
242                             myParam(8),
243                             myParam(9));
244                 break;
245             }
246         }
247
248         /* use putp() in order to perform padding */
249         putp(s);
250         return exit_code(STRING, 0);
251     }
252     return exit_code(STRING, 1);
253 }
254
255 int
256 main(int argc, char **argv)
257 {
258     char *term;
259     int errret;
260     bool cmdline = TRUE;
261     int c;
262     char buf[BUFSIZ];
263     int result = 0;
264     int fd;
265     TTY tty_settings;
266
267     prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
268
269     term = getenv("TERM");
270
271     while ((c = getopt(argc, argv, "ST:V")) != -1) {
272         switch (c) {
273         case 'S':
274             cmdline = FALSE;
275             break;
276         case 'T':
277             use_env(FALSE);
278             term = optarg;
279             break;
280         case 'V':
281             puts(curses_version());
282             ExitProgram(EXIT_SUCCESS);
283         default:
284             usage();
285             /* NOTREACHED */
286         }
287     }
288
289     /*
290      * Modify the argument list to omit the options we processed.
291      */
292     if (is_clear || is_reset || is_init) {
293         if (optind-- < argc) {
294             argc -= optind;
295             argv += optind;
296         }
297         argv[0] = prg_name;
298     } else {
299         argc -= optind;
300         argv += optind;
301     }
302
303     if (term == 0 || *term == '\0')
304         quit(2, "No value for $TERM and no -T specified");
305
306     fd = save_tty_settings(&tty_settings);
307
308     if (setupterm(term, fd, &errret) != OK && errret <= 0)
309         quit(3, "unknown terminal \"%s\"", term);
310
311     if (cmdline) {
312         if ((argc <= 0) && !(is_clear || is_reset || is_init))
313             usage();
314         ExitProgram(tput_cmd(fd, &tty_settings, argc, argv));
315     }
316
317     while (fgets(buf, sizeof(buf), stdin) != 0) {
318         char *argvec[16];       /* command, 9 parms, null, & slop */
319         int argnum = 0;
320         char *cp;
321
322         /* crack the argument list into a dope vector */
323         for (cp = buf; *cp; cp++) {
324             if (isspace(UChar(*cp))) {
325                 *cp = '\0';
326             } else if (cp == buf || cp[-1] == 0) {
327                 argvec[argnum++] = cp;
328                 if (argnum >= (int) SIZEOF(argvec) - 1)
329                     break;
330             }
331         }
332         argvec[argnum] = 0;
333
334         if (argnum != 0
335             && tput_cmd(fd, &tty_settings, argnum, argvec) != 0) {
336             if (result == 0)
337                 result = 4;     /* will return value >4 */
338             ++result;
339         }
340     }
341
342     ExitProgram(result);
343 }