]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/tput.c
ncurses 4.1
[ncurses.git] / progs / tput.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
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.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23 /*
24  * tput.c -- shellscript access to terminal capabilities
25  *
26  * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
27  * Ross Ridge's mytinfo package.
28  */
29
30 #include <progs.priv.h>
31
32 #include <ctype.h>
33 #include <curses.h>
34
35 MODULE_ID("$Id: tput.c,v 1.8 1996/12/21 17:34:36 tom Exp $")
36
37 #define PUTS(s)         fputs(s, stdout)
38 #define PUTCHAR(c)      putchar(c)
39 #define FLUSH           fflush(stdout)
40
41 static char *prg_name;
42
43 static void quit(int status, const char *fmt, ...)
44 {
45 va_list argp;
46
47         va_start(argp,fmt);
48         vfprintf (stderr, fmt, argp);
49         fprintf(stderr, "\n");
50         va_end(argp);
51         exit(status);
52 }
53
54 static void usage(void)
55 {
56         fprintf(stderr, "usage: %s [-S] [-T term] capname\n", prg_name);
57         exit(EXIT_FAILURE);
58 }
59
60 static int tput(int argc, char *argv[])
61 {
62 char *s;
63 int i, j, c;
64 int reset, status;
65 FILE *f;
66
67         reset = 0;
68         if (strcmp(argv[0], "reset") == 0) {
69                 reset = 1;
70         }
71         if (reset || strcmp(argv[0], "init") == 0) {
72                 if (init_prog != NULL) {
73                         system(init_prog);
74                 }
75                 FLUSH;
76
77                 if (reset && reset_1string != NULL) {
78                         PUTS(reset_1string);
79                 } else if (init_1string != NULL) {
80                         PUTS(init_1string);
81                 }
82                 FLUSH;
83         
84                 if (reset && reset_2string != NULL) {
85                         PUTS(reset_2string);
86                 } else if (init_2string != NULL) {
87                         PUTS(init_2string);
88                 }
89                 FLUSH;
90         
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) {
99                         PUTS(clear_margins);
100                         if (carriage_return != NULL) {
101                                 PUTS(carriage_return);
102                         } else {
103                                 PUTCHAR('\r');
104                         }
105                         PUTS(set_left_margin);
106                         if (parm_right_cursor) {
107                                 PUTS(tparm(parm_right_cursor, columns - 1));
108                         } else {
109                                 for(i = 0; i < columns - 1; i++) {
110                                         PUTCHAR(' ');
111                                 }
112                         }
113                         PUTS(set_right_margin);
114                         if (carriage_return != NULL) {
115                                 PUTS(carriage_return);
116                         } else {
117                                 PUTCHAR('\r');
118                         }
119                 }
120                 FLUSH;
121         
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));
127                                         } else {
128                                                 for(j = 0; j < 8; j++) 
129                                                         PUTCHAR(' ');
130                                         }
131                                         PUTS(set_tab);
132                                 }
133                                 FLUSH;
134                         }
135                 }
136         
137                 if (reset && reset_file != NULL) {
138                         f = fopen(reset_file, "r");
139                         if (f == NULL) {
140                                 quit(errno, "Can't open reset_file: '%s'", reset_file);
141                         }
142                         while((c = fgetc(f)) != EOF) {
143                                 PUTCHAR(c);
144                         }
145                         fclose(f);
146                 } else if (init_file != NULL) {
147                         f = fopen(init_file, "r");
148                         if (f == NULL) {
149                                 quit(errno, "Can't open init_file: '%s'", init_file);
150                         }
151                         while((c = fgetc(f)) != EOF) {
152                                 PUTCHAR(c);
153                         }
154                         fclose(f);
155                 }
156                 FLUSH;
157         
158                 if (reset && reset_3string != NULL) {
159                         PUTS(reset_3string);
160                 } else if (init_2string != NULL) {
161                         PUTS(init_2string);
162                 }
163                 FLUSH;
164                 return 0;
165         }
166         
167         if (strcmp(argv[0], "longname") == 0) {
168                 PUTS(longname());
169                 return 0;
170         }
171
172         if ((status = tigetflag(argv[0])) != -1)
173                 return(status != 0);
174         else if ((status = tigetnum(argv[0])) != CANCELLED_NUMERIC) {
175                 (void) printf("%d\n", status);
176                 return(0);
177         }
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) {
181                 if (argc > 1) {
182                 int k;
183
184                         /* Nasty hack time. The tparm function needs to see numeric
185                          * parameters as numbers, not as pointers to their string
186                          * representations
187                          */
188
189                          for (k = 1; k < argc; k++)
190                                 if (isdigit(argv[k][0])) {
191                                         long val = atol(argv[k]);
192                                         argv[k] = (char *)val;
193                                 }
194
195                                 s = tparm(s,argv[1],argv[2],argv[3],argv[4],
196                                             argv[5],argv[6],argv[7],argv[8],
197                                             argv[9]);
198                 }
199
200                 /* use putp() in order to perform padding */
201                 putp(s);
202                 return(0);
203         }
204         return(0);
205 }
206
207 int main(int argc, char **argv)
208 {
209 char *s, *term;
210 int errret, cmdline = 1;
211 int c;
212
213         prg_name = argv[0];
214         s = strrchr(prg_name, '/');
215         if (s != NULL && *++s != '\0')
216         prg_name = s;
217
218         term = getenv("TERM");
219
220         while ((c = getopt (argc, argv, "ST:")) != EOF)
221             switch (c)
222             {
223             case 'S':
224                 cmdline = 0;
225                 break;
226             case 'T':
227                 use_env(FALSE);
228                 term = optarg;
229                 break;
230             default:
231                 usage();
232                 /* NOTREACHED */
233             }
234         argc -= optind;
235         argv += optind;
236
237         if (cmdline && argc == 0) {
238                 usage();
239                 /* NOTREACHED */
240         }
241
242         if (term == NULL || *term == '\0') {
243                 quit(2, "No value for $TERM and no -T specified");
244         }
245
246         setupterm(term, STDOUT_FILENO, &errret);
247         if (errret == ERR)
248         quit(3, "unknown terminal \"%s\"", term);
249
250         if (cmdline)
251         return(tput(argc, argv));
252         else {
253         char    buf[BUFSIZ];
254         int errors = 0;
255
256         while (fgets(buf, sizeof(buf), stdin) != (char *)NULL) {
257                 char    *argvec[16];    /* command, 9 parms, null, & slop */
258                 int      argnum = 0;
259                 char    *cp;
260
261                 /* crack the argument list into a dope vector */
262                 for (cp = buf; *cp; cp++) {
263                 if (isspace(*cp))
264                         *cp = '\0';
265                 else if (cp == buf || cp[-1] == 0)
266                         argvec[argnum++] = cp;
267                 }
268                 argvec[argnum] = (char *)NULL;
269
270                 if (tput(argnum, argvec) != 0)
271                 errors++;
272         }
273
274         return(errors > 0);
275         }
276 }
277