2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
24 * tput.c -- shellscript access to terminal capabilities
26 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
27 * Ross Ridge's mytinfo package.
30 #include <progs.priv.h>
35 MODULE_ID("$Id: tput.c,v 1.8 1996/12/21 17:34:36 tom Exp $")
37 #define PUTS(s) fputs(s, stdout)
38 #define PUTCHAR(c) putchar(c)
39 #define FLUSH fflush(stdout)
41 static char *prg_name;
43 static void quit(int status, const char *fmt, ...)
48 vfprintf (stderr, fmt, argp);
49 fprintf(stderr, "\n");
54 static void usage(void)
56 fprintf(stderr, "usage: %s [-S] [-T term] capname\n", prg_name);
60 static int tput(int argc, char *argv[])
68 if (strcmp(argv[0], "reset") == 0) {
71 if (reset || strcmp(argv[0], "init") == 0) {
72 if (init_prog != NULL) {
77 if (reset && reset_1string != NULL) {
79 } else if (init_1string != NULL) {
84 if (reset && reset_2string != NULL) {
86 } else if (init_2string != NULL) {
91 if (set_lr_margin != NULL) {
92 PUTS(tparm(set_lr_margin, 0, columns - 1));
93 } else if (set_left_margin_parm != NULL
94 && set_right_margin_parm != NULL) {
95 PUTS(tparm(set_left_margin_parm, 0));
96 PUTS(tparm(set_right_margin_parm, columns - 1));
97 } else if (clear_margins != NULL && set_left_margin != NULL
98 && set_right_margin != NULL) {
100 if (carriage_return != NULL) {
101 PUTS(carriage_return);
105 PUTS(set_left_margin);
106 if (parm_right_cursor) {
107 PUTS(tparm(parm_right_cursor, columns - 1));
109 for(i = 0; i < columns - 1; i++) {
113 PUTS(set_right_margin);
114 if (carriage_return != NULL) {
115 PUTS(carriage_return);
122 if (init_tabs != 8) {
123 if (clear_all_tabs != NULL && set_tab != NULL) {
124 for(i = 0; i < columns - 1; i += 8) {
125 if (parm_right_cursor) {
126 PUTS(tparm(parm_right_cursor, 8));
128 for(j = 0; j < 8; j++)
137 if (reset && reset_file != NULL) {
138 f = fopen(reset_file, "r");
140 quit(errno, "Can't open reset_file: '%s'", reset_file);
142 while((c = fgetc(f)) != EOF) {
146 } else if (init_file != NULL) {
147 f = fopen(init_file, "r");
149 quit(errno, "Can't open init_file: '%s'", init_file);
151 while((c = fgetc(f)) != EOF) {
158 if (reset && reset_3string != NULL) {
160 } else if (init_2string != NULL) {
167 if (strcmp(argv[0], "longname") == 0) {
172 if ((status = tigetflag(argv[0])) != -1)
174 else if ((status = tigetnum(argv[0])) != CANCELLED_NUMERIC) {
175 (void) printf("%d\n", status);
178 else if ((s = tigetstr(argv[0])) == CANCELLED_STRING)
179 quit(4, "%s: unknown terminfo capability '%s'", prg_name, argv[0]);
180 else if (s != (char *)NULL) {
184 /* Nasty hack time. The tparm function needs to see numeric
185 * parameters as numbers, not as pointers to their string
189 for (k = 1; k < argc; k++)
190 if (isdigit(argv[k][0])) {
191 long val = atol(argv[k]);
192 argv[k] = (char *)val;
195 s = tparm(s,argv[1],argv[2],argv[3],argv[4],
196 argv[5],argv[6],argv[7],argv[8],
200 /* use putp() in order to perform padding */
207 int main(int argc, char **argv)
210 int errret, cmdline = 1;
214 s = strrchr(prg_name, '/');
215 if (s != NULL && *++s != '\0')
218 term = getenv("TERM");
220 while ((c = getopt (argc, argv, "ST:")) != EOF)
237 if (cmdline && argc == 0) {
242 if (term == NULL || *term == '\0') {
243 quit(2, "No value for $TERM and no -T specified");
246 setupterm(term, STDOUT_FILENO, &errret);
248 quit(3, "unknown terminal \"%s\"", term);
251 return(tput(argc, argv));
256 while (fgets(buf, sizeof(buf), stdin) != (char *)NULL) {
257 char *argvec[16]; /* command, 9 parms, null, & slop */
261 /* crack the argument list into a dope vector */
262 for (cp = buf; *cp; cp++) {
265 else if (cp == buf || cp[-1] == 0)
266 argvec[argnum++] = cp;
268 argvec[argnum] = (char *)NULL;
270 if (tput(argnum, argvec) != 0)