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>
46 MODULE_ID("$Id: tput.c,v 1.11 1998/02/11 12:14:02 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;
54 static void quit(int status, const char *fmt, ...)
59 vfprintf (stderr, fmt, argp);
60 fprintf(stderr, "\n");
65 static void usage(void)
67 fprintf(stderr, "usage: %s [-S] [-T term] capname\n", prg_name);
71 static int tput(int argc, char *argv[])
79 if (strcmp(argv[0], "reset") == 0) {
82 if (reset || strcmp(argv[0], "init") == 0) {
83 if (init_prog != NULL) {
88 if (reset && reset_1string != NULL) {
90 } else if (init_1string != NULL) {
95 if (reset && reset_2string != NULL) {
97 } else if (init_2string != NULL) {
102 if (set_lr_margin != NULL) {
103 PUTS(tparm(set_lr_margin, 0, columns - 1));
104 } else if (set_left_margin_parm != NULL
105 && set_right_margin_parm != NULL) {
106 PUTS(tparm(set_left_margin_parm, 0));
107 PUTS(tparm(set_right_margin_parm, columns - 1));
108 } else if (clear_margins != NULL && set_left_margin != NULL
109 && set_right_margin != NULL) {
111 if (carriage_return != NULL) {
112 PUTS(carriage_return);
116 PUTS(set_left_margin);
117 if (parm_right_cursor) {
118 PUTS(tparm(parm_right_cursor, columns - 1));
120 for(i = 0; i < columns - 1; i++) {
124 PUTS(set_right_margin);
125 if (carriage_return != NULL) {
126 PUTS(carriage_return);
133 if (init_tabs != 8) {
134 if (clear_all_tabs != NULL && set_tab != NULL) {
135 for(i = 0; i < columns - 1; i += 8) {
136 if (parm_right_cursor) {
137 PUTS(tparm(parm_right_cursor, 8));
139 for(j = 0; j < 8; j++)
148 if (reset && reset_file != NULL) {
149 f = fopen(reset_file, "r");
151 quit(errno, "Can't open reset_file: '%s'", reset_file);
153 while((c = fgetc(f)) != EOF) {
157 } else if (init_file != NULL) {
158 f = fopen(init_file, "r");
160 quit(errno, "Can't open init_file: '%s'", init_file);
162 while((c = fgetc(f)) != EOF) {
169 if (reset && reset_3string != NULL) {
171 } else if (init_2string != NULL) {
178 if (strcmp(argv[0], "longname") == 0) {
183 if ((status = tigetflag(argv[0])) != -1)
185 else if ((status = tigetnum(argv[0])) != CANCELLED_NUMERIC) {
186 (void) printf("%d\n", status);
189 else if ((s = tigetstr(argv[0])) == CANCELLED_STRING)
190 quit(4, "%s: unknown terminfo capability '%s'", prg_name, argv[0]);
191 else if (s != (char *)NULL) {
195 /* Nasty hack time. The tparm function needs to see numeric
196 * parameters as numbers, not as pointers to their string
200 for (k = 1; k < argc; k++)
201 if (isdigit(argv[k][0])) {
202 long val = atol(argv[k]);
203 argv[k] = (char *)val;
206 s = tparm(s,argv[1],argv[2],argv[3],argv[4],
207 argv[5],argv[6],argv[7],argv[8],
211 /* use putp() in order to perform padding */
218 int main(int argc, char **argv)
221 int errret, cmdline = 1;
227 s = strrchr(prg_name, '/');
228 if (s != NULL && *++s != '\0')
231 term = getenv("TERM");
233 while ((c = getopt (argc, argv, "ST:")) != EOF)
250 if (cmdline && argc == 0) {
255 if (term == NULL || *term == '\0')
256 quit(2, "No value for $TERM and no -T specified");
258 if (setupterm(term, STDOUT_FILENO, &errret) != OK)
259 quit(3, "unknown terminal \"%s\"", term);
262 return tput(argc, argv);
264 while (fgets(buf, sizeof(buf), stdin) != (char *)NULL) {
265 char *argvec[16]; /* command, 9 parms, null, & slop */
269 /* crack the argument list into a dope vector */
270 for (cp = buf; *cp; cp++) {
273 else if (cp == buf || cp[-1] == 0)
274 argvec[argnum++] = cp;
276 argvec[argnum] = (char *)NULL;
278 if (tput(argnum, argvec) != 0)