1 /****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
36 * tput.c -- shellscript access to terminal capabilities
38 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
39 * Ross Ridge's mytinfo package.
42 #include <progs.priv.h>
47 MODULE_ID("$Id: tput.c,v 1.14 1999/07/31 21:18:29 Goran.Uddeborg Exp $")
49 #define PUTS(s) fputs(s, stdout)
50 #define PUTCHAR(c) putchar(c)
51 #define FLUSH fflush(stdout)
53 static char *prg_name;
55 static void quit(int status, const char *fmt, ...)
60 vfprintf (stderr, fmt, argp);
61 fprintf(stderr, "\n");
66 static void usage(void)
68 fprintf(stderr, "usage: %s [-S] [-T term] capname\n", prg_name);
72 static int tput(int argc, char *argv[])
82 if (strcmp(name, "reset") == 0) {
85 if (reset || strcmp(name, "init") == 0) {
86 if (init_prog != NULL) {
91 if (reset && reset_1string != NULL) {
93 } else if (init_1string != NULL) {
98 if (reset && reset_2string != NULL) {
100 } else if (init_2string != NULL) {
105 if (set_lr_margin != NULL) {
106 PUTS(tparm(set_lr_margin, 0, columns - 1));
107 } else if (set_left_margin_parm != NULL
108 && set_right_margin_parm != NULL) {
109 PUTS(tparm(set_left_margin_parm, 0));
110 PUTS(tparm(set_right_margin_parm, columns - 1));
111 } else if (clear_margins != NULL && set_left_margin != NULL
112 && set_right_margin != NULL) {
114 if (carriage_return != NULL) {
115 PUTS(carriage_return);
119 PUTS(set_left_margin);
120 if (parm_right_cursor) {
121 PUTS(tparm(parm_right_cursor, columns - 1));
123 for(i = 0; i < columns - 1; i++) {
127 PUTS(set_right_margin);
128 if (carriage_return != NULL) {
129 PUTS(carriage_return);
136 if (init_tabs != 8) {
137 if (clear_all_tabs != NULL && set_tab != NULL) {
138 for(i = 0; i < columns - 1; i += 8) {
139 if (parm_right_cursor) {
140 PUTS(tparm(parm_right_cursor, 8));
142 for(j = 0; j < 8; j++)
151 if (reset && reset_file != NULL) {
152 f = fopen(reset_file, "r");
154 quit(errno, "Can't open reset_file: '%s'", reset_file);
156 while((c = fgetc(f)) != EOF) {
160 } else if (init_file != NULL) {
161 f = fopen(init_file, "r");
163 quit(errno, "Can't open init_file: '%s'", init_file);
165 while((c = fgetc(f)) != EOF) {
172 if (reset && reset_3string != NULL) {
174 } else if (init_2string != NULL) {
181 if (strcmp(name, "longname") == 0) {
186 #ifndef PURE_TERMINFO
188 const struct name_table_entry *np;
190 if ((np = _nc_find_entry(name, _nc_get_hash_table(1))) != 0)
194 if (bool_from_termcap[np->nte_index])
195 name = boolnames[np->nte_index];
199 if (num_from_termcap[np->nte_index])
200 name = numnames[np->nte_index];
204 if (str_from_termcap[np->nte_index])
205 name = strnames[np->nte_index];
211 if ((status = tigetflag(name)) != -1)
213 else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
214 (void) printf("%d\n", status);
217 else if ((s = tigetstr(name)) == CANCELLED_STRING)
218 quit(4, "%s: unknown terminfo capability '%s'", prg_name, name);
219 else if (s != (char *)NULL) {
223 /* Nasty hack time. The tparm function needs to see numeric
224 * parameters as numbers, not as pointers to their string
228 for (k = 1; k < argc; k++)
229 if (isdigit(argv[k][0])) {
230 long val = atol(argv[k]);
231 argv[k] = (char *)val;
234 s = tparm(s,argv[1],argv[2],argv[3],argv[4],
235 argv[5],argv[6],argv[7],argv[8],
239 /* use putp() in order to perform padding */
246 int main(int argc, char **argv)
249 int errret, cmdline = 1;
255 s = strrchr(prg_name, '/');
256 if (s != NULL && *++s != '\0')
259 term = getenv("TERM");
261 while ((c = getopt (argc, argv, "ST:")) != EOF)
278 if (cmdline && argc == 0) {
283 if (term == NULL || *term == '\0')
284 quit(2, "No value for $TERM and no -T specified");
286 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
287 quit(3, "unknown terminal \"%s\"", term);
290 return tput(argc, argv);
292 while (fgets(buf, sizeof(buf), stdin) != (char *)NULL) {
293 char *argvec[16]; /* command, 9 parms, null, & slop */
297 /* crack the argument list into a dope vector */
298 for (cp = buf; *cp; cp++) {
301 else if (cp == buf || cp[-1] == 0)
302 argvec[argnum++] = cp;
304 argvec[argnum] = (char *)NULL;
306 if (tput(argnum, argvec) != 0)