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