1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000,2001 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.
41 #include <progs.priv.h>
46 #include <transform.h>
48 MODULE_ID("$Id: tput.c,v 1.30 2001/07/22 00:16:33 tom Exp $")
50 #define PUTS(s) fputs(s, stdout)
51 #define PUTCHAR(c) putchar(c)
52 #define FLUSH fflush(stdout)
60 static char *prg_name;
61 static bool is_init = FALSE;
62 static bool is_reset = FALSE;
65 quit(int status, const char *fmt,...)
70 vfprintf(stderr, fmt, argp);
71 fprintf(stderr, "\n");
79 fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
84 check_aliases(const char *name)
86 is_init = (strcmp(name, PROG_INIT) == 0);
87 is_reset = (strcmp(name, PROG_RESET) == 0);
91 * Lookup the type of call we should make to tparm(). This ignores the actual
92 * terminfo capability (bad, because it is not extensible), but makes this
93 * code portable to platforms where sizeof(int) != sizeof(char *).
95 * FIXME: If we want extensibility, analyze the capability string as we do
96 * in tparm() to decide how to parse the varargs list.
99 tparm_type(const char *name)
101 #define TD(code, longname, ti, tc) {code,longname},{code,ti},{code,tc}
102 TParams result = Numbers;
104 static const struct {
108 TD(Num_Str, "pkey_key", "pfkey", "pk"),
109 TD(Num_Str, "pkey_local", "pfloc", "pl"),
110 TD(Num_Str, "pkey_xmit", "pfx", "px"),
111 TD(Num_Str, "plab_norm", "pln", "pn"),
112 TD(Num_Str_Str, "pkey_plab", "pfxl", "xl"),
117 for (n = 0; n < SIZEOF(table); n++) {
118 if (!strcmp(name, table[n].name)) {
119 result = table[n].code;
127 tput(int argc, char *argv[])
129 NCURSES_CONST char *name;
135 if ((name = argv[0]) == 0)
138 if (is_reset || is_init) {
139 if (init_prog != 0) {
144 if (is_reset && reset_1string != 0) {
146 } else if (init_1string != 0) {
151 if (is_reset && reset_2string != 0) {
153 } else if (init_2string != 0) {
159 if (set_lr_margin != 0) {
160 PUTS(tparm(set_lr_margin, 0, columns - 1));
163 #ifdef set_left_margin_parm
164 if (set_left_margin_parm != 0
165 && set_right_margin_parm != 0) {
166 PUTS(tparm(set_left_margin_parm, 0));
167 PUTS(tparm(set_right_margin_parm, columns - 1));
170 if (clear_margins != 0
171 && set_left_margin != 0
172 && set_right_margin != 0) {
174 if (carriage_return != 0) {
175 PUTS(carriage_return);
179 PUTS(set_left_margin);
180 if (parm_right_cursor) {
181 PUTS(tparm(parm_right_cursor, columns - 1));
183 for (i = 0; i < columns - 1; i++) {
187 PUTS(set_right_margin);
188 if (carriage_return != 0) {
189 PUTS(carriage_return);
196 if (init_tabs != 8) {
197 if (clear_all_tabs != 0 && set_tab != 0) {
198 for (i = 0; i < columns - 1; i += 8) {
199 if (parm_right_cursor) {
200 PUTS(tparm(parm_right_cursor, 8));
202 for (j = 0; j < 8; j++)
211 if (is_reset && reset_file != 0) {
212 f = fopen(reset_file, "r");
214 quit(errno, "Can't open reset_file: '%s'", reset_file);
216 while ((c = fgetc(f)) != EOF) {
220 } else if (init_file != 0) {
221 f = fopen(init_file, "r");
223 quit(errno, "Can't open init_file: '%s'", init_file);
225 while ((c = fgetc(f)) != EOF) {
232 if (is_reset && reset_3string != 0) {
234 } else if (init_2string != 0) {
241 if (strcmp(name, "longname") == 0) {
247 const struct name_table_entry *np;
249 if ((np = _nc_find_entry(name, _nc_get_hash_table(1))) != 0)
250 switch (np->nte_type) {
252 if (bool_from_termcap[np->nte_index])
253 name = boolnames[np->nte_index];
257 if (num_from_termcap[np->nte_index])
258 name = numnames[np->nte_index];
262 if (str_from_termcap[np->nte_index])
263 name = strnames[np->nte_index];
269 if ((status = tigetflag(name)) != -1) {
270 return (status != 0);
271 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
272 (void) printf("%d\n", status);
274 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
275 quit(4, "%s: unknown terminfo capability '%s'", prg_name, name);
276 } else if (s != ABSENT_STRING) {
282 /* Nasty hack time. The tparm function needs to see numeric
283 * parameters as numbers, not as pointers to their string
287 for (k = 1; k < argc; k++) {
289 strings[k] = argv[k];
290 numbers[k] = strtol(argv[k], &tmp, 0);
291 if (tmp == 0 || *tmp != 0)
294 for (k = argc; k <= 9; k++) {
299 switch (tparm_type(name)) {
301 s = tparm(s, numbers[1], strings[2]);
304 s = tparm(s, numbers[1], strings[2], strings[3]);
308 numbers[1], numbers[2], numbers[3],
309 numbers[4], numbers[5], numbers[6],
310 numbers[7], numbers[8], numbers[9]);
315 /* use putp() in order to perform padding */
323 main(int argc, char **argv)
332 check_aliases(prg_name = _nc_rootname(argv[0]));
334 term = getenv("TERM");
336 while ((c = getopt(argc, argv, "ST:V")) != EOF) {
346 puts(curses_version());
355 * Modify the argument list to omit the options we processed.
357 if (is_reset || is_init) {
358 if (optind-- < argc) {
368 if (term == 0 || *term == '\0')
369 quit(2, "No value for $TERM and no -T specified");
371 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
372 quit(3, "unknown terminal \"%s\"", term);
375 if ((argc <= 0) && !is_reset && !is_init)
377 return tput(argc, argv);
380 while (fgets(buf, sizeof(buf), stdin) != 0) {
381 char *argvec[16]; /* command, 9 parms, null, & slop */
385 /* crack the argument list into a dope vector */
386 for (cp = buf; *cp; cp++) {
387 if (isspace(UChar(*cp))) {
389 } else if (cp == buf || cp[-1] == 0) {
390 argvec[argnum++] = cp;
391 if (argnum >= (int) SIZEOF(argvec) - 1)
398 && tput(argnum, argvec) != 0)