1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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 MODULE_ID("$Id: tput.c,v 1.16 2000/03/19 01:08:08 tom Exp $")
48 #define PUTS(s) fputs(s, stdout)
49 #define PUTCHAR(c) putchar(c)
50 #define FLUSH fflush(stdout)
52 static char *prg_name;
55 quit(int status, const char *fmt,...)
60 vfprintf(stderr, fmt, argp);
61 fprintf(stderr, "\n");
69 fprintf(stderr, "usage: %s [-S] [-T term] capname\n", prg_name);
74 tput(int argc, char *argv[])
76 NCURSES_CONST char *name;
84 if (strcmp(name, "reset") == 0) {
87 if (reset || strcmp(name, "init") == 0) {
93 if (reset && reset_1string != 0) {
95 } else if (init_1string != 0) {
100 if (reset && reset_2string != 0) {
102 } else if (init_2string != 0) {
107 if (set_lr_margin != 0) {
108 PUTS(tparm(set_lr_margin, 0, columns - 1));
109 } else if (set_left_margin_parm != 0
110 && set_right_margin_parm != 0) {
111 PUTS(tparm(set_left_margin_parm, 0));
112 PUTS(tparm(set_right_margin_parm, columns - 1));
113 } else if (clear_margins != 0
114 && set_left_margin != 0
115 && set_right_margin != 0) {
117 if (carriage_return != 0) {
118 PUTS(carriage_return);
122 PUTS(set_left_margin);
123 if (parm_right_cursor) {
124 PUTS(tparm(parm_right_cursor, columns - 1));
126 for (i = 0; i < columns - 1; i++) {
130 PUTS(set_right_margin);
131 if (carriage_return != 0) {
132 PUTS(carriage_return);
139 if (init_tabs != 8) {
140 if (clear_all_tabs != 0 && set_tab != 0) {
141 for (i = 0; i < columns - 1; i += 8) {
142 if (parm_right_cursor) {
143 PUTS(tparm(parm_right_cursor, 8));
145 for (j = 0; j < 8; j++)
154 if (reset && reset_file != 0) {
155 f = fopen(reset_file, "r");
157 quit(errno, "Can't open reset_file: '%s'", reset_file);
159 while ((c = fgetc(f)) != EOF) {
163 } else if (init_file != 0) {
164 f = fopen(init_file, "r");
166 quit(errno, "Can't open init_file: '%s'", init_file);
168 while ((c = fgetc(f)) != EOF) {
175 if (reset && reset_3string != 0) {
177 } else if (init_2string != 0) {
184 if (strcmp(name, "longname") == 0) {
188 #ifndef PURE_TERMINFO
190 const struct name_table_entry *np;
192 if ((np = _nc_find_entry(name, _nc_get_hash_table(1))) != 0)
193 switch (np->nte_type) {
195 if (bool_from_termcap[np->nte_index])
196 name = boolnames[np->nte_index];
200 if (num_from_termcap[np->nte_index])
201 name = numnames[np->nte_index];
205 if (str_from_termcap[np->nte_index])
206 name = strnames[np->nte_index];
212 if ((status = tigetflag(name)) != -1) {
213 return (status != 0);
214 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
215 (void) printf("%d\n", status);
217 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
218 quit(4, "%s: unknown terminfo capability '%s'", prg_name, name);
224 /* Nasty hack time. The tparm function needs to see numeric
225 * parameters as numbers, not as pointers to their string
229 for (k = 1; k < argc; k++) {
230 if (isdigit(argv[k][0])) {
231 long val = atol(argv[k]);
232 params[k] = (char *)val;
237 for (k = argc; k <= 9; k++)
241 params[1], params[2], params[3],
242 params[4], params[5], params[6],
243 params[7], params[8], params[9]);
246 /* use putp() in order to perform padding */
254 main(int argc, char **argv)
257 int errret, cmdline = 1;
263 s = strrchr(prg_name, '/');
264 if (s != 0 && *++s != '\0')
267 term = getenv("TERM");
269 while ((c = getopt(argc, argv, "ST:")) != EOF)
285 if (cmdline && argc == 0) {
290 if (term == 0 || *term == '\0')
291 quit(2, "No value for $TERM and no -T specified");
293 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
294 quit(3, "unknown terminal \"%s\"", term);
297 return tput(argc, argv);
299 while (fgets(buf, sizeof(buf), stdin) != 0) {
300 char *argvec[16]; /* command, 9 parms, null, & slop */
304 /* crack the argument list into a dope vector */
305 for (cp = buf; *cp; cp++) {
308 else if (cp == buf || cp[-1] == 0)
309 argvec[argnum++] = cp;
313 if (tput(argnum, argvec) != 0)