1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * tput.c -- shellscript access to terminal capabilities
37 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
38 * Ross Ridge's mytinfo package.
42 #include <progs.priv.h>
45 #include <dump_entry.h>
48 #include <transform.h>
50 MODULE_ID("$Id: tput.c,v 1.46 2010/01/09 16:53:24 tom Exp $")
52 #define PUTS(s) fputs(s, stdout)
53 #define PUTCHAR(c) putchar(c)
54 #define FLUSH fflush(stdout)
62 static char *prg_name;
63 static bool is_init = FALSE;
64 static bool is_reset = FALSE;
67 quit(int status, const char *fmt,...)
72 fprintf(stderr, "%s: ", prg_name);
73 vfprintf(stderr, fmt, argp);
74 fprintf(stderr, "\n");
82 fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
83 ExitProgram(EXIT_FAILURE);
87 check_aliases(const char *name)
89 is_init = same_program(name, PROG_INIT);
90 is_reset = same_program(name, PROG_RESET);
94 * Lookup the type of call we should make to tparm(). This ignores the actual
95 * terminfo capability (bad, because it is not extensible), but makes this
96 * code portable to platforms where sizeof(int) != sizeof(char *).
98 * FIXME: If we want extensibility, analyze the capability string as we do
99 * in tparm() to decide how to parse the varargs list.
102 tparm_type(const char *name)
104 #define TD(code, longname, ti, tc) {code,longname},{code,ti},{code,tc}
105 TParams result = Numbers;
107 static const struct {
111 TD(Num_Str, "pkey_key", "pfkey", "pk"),
112 TD(Num_Str, "pkey_local", "pfloc", "pl"),
113 TD(Num_Str, "pkey_xmit", "pfx", "px"),
114 TD(Num_Str, "plab_norm", "pln", "pn"),
115 TD(Num_Str_Str, "pkey_plab", "pfxl", "xl"),
120 for (n = 0; n < SIZEOF(table); n++) {
121 if (!strcmp(name, table[n].name)) {
122 result = table[n].code;
130 exit_code(int token, int value)
136 result = !value; /* TRUE=0, FALSE=1 */
139 result = 0; /* always zero */
142 result = value; /* 0=normal, 1=missing */
149 tput(int argc, char *argv[])
151 NCURSES_CONST char *name;
157 bool termcap = FALSE;
160 if ((name = argv[0]) == 0)
163 if (is_reset || is_init) {
164 if (init_prog != 0) {
169 if (is_reset && reset_1string != 0) {
171 } else if (init_1string != 0) {
176 if (is_reset && reset_2string != 0) {
178 } else if (init_2string != 0) {
184 if (set_lr_margin != 0) {
185 PUTS(TPARM_2(set_lr_margin, 0, columns - 1));
188 #ifdef set_left_margin_parm
189 if (set_left_margin_parm != 0
190 && set_right_margin_parm != 0) {
191 PUTS(TPARM_1(set_left_margin_parm, 0));
192 PUTS(TPARM_1(set_right_margin_parm, columns - 1));
195 if (clear_margins != 0
196 && set_left_margin != 0
197 && set_right_margin != 0) {
199 if (carriage_return != 0) {
200 PUTS(carriage_return);
204 PUTS(set_left_margin);
205 if (parm_right_cursor) {
206 PUTS(TPARM_1(parm_right_cursor, columns - 1));
208 for (i = 0; i < columns - 1; i++) {
212 PUTS(set_right_margin);
213 if (carriage_return != 0) {
214 PUTS(carriage_return);
221 if (init_tabs != 8) {
222 if (clear_all_tabs != 0 && set_tab != 0) {
223 for (i = 0; i < columns - 1; i += 8) {
224 if (parm_right_cursor) {
225 PUTS(TPARM_1(parm_right_cursor, 8));
227 for (j = 0; j < 8; j++)
236 if (is_reset && reset_file != 0) {
237 f = fopen(reset_file, "r");
239 quit(4 + errno, "Can't open reset_file: '%s'", reset_file);
241 while ((c = fgetc(f)) != EOF) {
245 } else if (init_file != 0) {
246 f = fopen(init_file, "r");
248 quit(4 + errno, "Can't open init_file: '%s'", init_file);
250 while ((c = fgetc(f)) != EOF) {
257 if (is_reset && reset_3string != 0) {
259 } else if (init_3string != 0) {
266 if (strcmp(name, "longname") == 0) {
273 if ((status = tigetflag(name)) != -1) {
274 return exit_code(BOOLEAN, status);
275 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
276 (void) printf("%d\n", status);
277 return exit_code(NUMBER, 0);
278 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
281 const struct name_table_entry *np;
284 if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
285 switch (np->nte_type) {
287 if (bool_from_termcap[np->nte_index])
288 name = boolnames[np->nte_index];
292 if (num_from_termcap[np->nte_index])
293 name = numnames[np->nte_index];
297 if (str_from_termcap[np->nte_index])
298 name = strnames[np->nte_index];
305 quit(4, "unknown terminfo capability '%s'", name);
306 } else if (s != ABSENT_STRING) {
310 long numbers[1 + NUM_PARM];
311 char *strings[1 + NUM_PARM];
312 char *p_is_s[NUM_PARM];
314 /* Nasty hack time. The tparm function needs to see numeric
315 * parameters as numbers, not as pointers to their string
319 for (k = 1; k < argc; k++) {
321 strings[k] = argv[k];
322 numbers[k] = strtol(argv[k], &tmp, 0);
323 if (tmp == 0 || *tmp != 0)
326 for (k = argc; k <= NUM_PARM; k++) {
331 switch (tparm_type(name)) {
333 s = TPARM_2(s, numbers[1], strings[2]);
336 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
340 (void) _nc_tparm_analyze(s, p_is_s, &popcount);
341 #define myParam(n) (p_is_s[n - 1] != 0 ? ((long) strings[n]) : numbers[n])
356 /* use putp() in order to perform padding */
358 return exit_code(STRING, 0);
360 return exit_code(STRING, 1);
364 main(int argc, char **argv)
373 check_aliases(prg_name = _nc_rootname(argv[0]));
375 term = getenv("TERM");
377 while ((c = getopt(argc, argv, "ST:V")) != -1) {
387 puts(curses_version());
388 ExitProgram(EXIT_SUCCESS);
396 * Modify the argument list to omit the options we processed.
398 if (is_reset || is_init) {
399 if (optind-- < argc) {
409 if (term == 0 || *term == '\0')
410 quit(2, "No value for $TERM and no -T specified");
412 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
413 quit(3, "unknown terminal \"%s\"", term);
416 if ((argc <= 0) && !is_reset && !is_init)
418 ExitProgram(tput(argc, argv));
421 while (fgets(buf, sizeof(buf), stdin) != 0) {
422 char *argvec[16]; /* command, 9 parms, null, & slop */
426 /* crack the argument list into a dope vector */
427 for (cp = buf; *cp; cp++) {
428 if (isspace(UChar(*cp))) {
430 } else if (cp == buf || cp[-1] == 0) {
431 argvec[argnum++] = cp;
432 if (argnum >= (int) SIZEOF(argvec) - 1)
439 && tput(argnum, argvec) != 0) {
441 result = 4; /* will return value >4 */