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