tput.c revision 1.3 1 /*-
2 * Copyright (c) 1980, 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980, 1988 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)tput.c 5.7 (Berkeley) 6/7/90";*/
42 static char rcsid[] = "$Id: tput.c,v 1.3 1994/01/24 23:54:57 cgd Exp $";
43 #endif /* not lint */
44
45 #include <sys/termios.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <curses.h>
50
51 static void prlongname __P((char *));
52 static void setospeed __P((void));
53 static void outc __P((int));
54 static void usage __P((void));
55 static char **process __P((char *, char *, char **));
56
57 int
58 main(argc, argv)
59 int argc;
60 char **argv;
61 {
62 extern char *optarg;
63 extern int optind;
64 int ch, exitval, n;
65 char *cptr, *p, *term, buf[1024], tbuf[1024];
66
67 term = NULL;
68 while ((ch = getopt(argc, argv, "T:")) != EOF)
69 switch(ch) {
70 case 'T':
71 term = optarg;
72 break;
73 case '?':
74 default:
75 usage();
76 }
77 argc -= optind;
78 argv += optind;
79
80 if (!term && !(term = getenv("TERM"))) {
81 (void)fprintf(stderr, "tput: no terminal type specified.\n");
82 exit(2);
83 }
84 if (tgetent(tbuf, term) != 1) {
85 (void)fprintf(stderr, "tput: tgetent failure.\n");
86 exit(2);
87 }
88 setospeed();
89 for (exitval = 0; (p = *argv) != NULL; ++argv) {
90 switch(*p) {
91 case 'c':
92 if (!strcmp(p, "clear"))
93 p = "cl";
94 break;
95 case 'i':
96 if (!strcmp(p, "init"))
97 p = "is";
98 break;
99 case 'l':
100 if (!strcmp(p, "longname"))
101 prlongname(tbuf);
102 continue;
103 case 'r':
104 if (!strcmp(p, "reset"))
105 p = "rs";
106 break;
107 }
108 cptr = buf;
109 if (tgetstr(p, &cptr))
110 argv = process(p, buf, argv);
111 else if ((n = tgetnum(p)) != -1)
112 (void)printf("%d\n", n);
113 else
114 exitval = !tgetflag(p);
115
116 if (argv == NULL)
117 break;
118 }
119 exit(argv ? exitval : 2);
120 }
121
122 static void
123 prlongname(buf)
124 char *buf;
125 {
126 register char *p;
127 int savech;
128 char *savep;
129
130 for (p = buf; *p && *p != ':'; ++p)
131 continue;
132 savech = *(savep = p);
133 for (*p = '\0'; p >= buf && *p != '|'; --p)
134 continue;
135 (void)printf("%s\n", p + 1);
136 *savep = savech;
137 }
138
139 static void
140 setospeed()
141 {
142 #undef ospeed
143 extern short ospeed;
144 struct termios t;
145
146 if (tcgetattr(STDOUT_FILENO, &t) != -1)
147 ospeed = 0;
148 else
149 ospeed = cfgetospeed(&t);
150 }
151
152 static void
153 outc(c)
154 int c;
155 {
156 putchar(c);
157 }
158
159 static void
160 usage()
161 {
162 (void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
163 exit(1);
164 }
165
166 static char **
167 process(cap, str, argv)
168 char *cap;
169 char *str;
170 char **argv;
171 {
172 static char errfew[] =
173 "tput: Not enough arguments (%d) for capability `%s'\n";
174 static char errmany[] =
175 "tput: Too many arguments (%d) for capability `%s'\n";
176 static char erresc[] =
177 "tput: Unknown %% escape `%c' for capability `%s'\n";
178 /*
179 * Count home many values we need for this capability.
180 */
181 char *cp;
182 int arg_need, arg_rows, arg_cols;
183 for (cp = str, arg_need = 0; *cp; cp++)
184 if (*cp == '%')
185 switch (*++cp) {
186 case 'd':
187 case '2':
188 case '3':
189 case '.':
190 case '+':
191 arg_need++;
192 break;
193
194 case '%':
195 case '>':
196 case 'i':
197 case 'r':
198 case 'n':
199 case 'B':
200 case 'D':
201 break;
202
203 default:
204 /*
205 * hpux has lot's of them, but we complain
206 */
207 (void)fprintf(stderr, erresc, *cp, cap);
208 return NULL;
209 }
210
211 /*
212 * And print them
213 */
214 switch (arg_need) {
215 case 0:
216 (void) tputs(str, 1, outc);
217 break;
218
219 case 1:
220 arg_cols = 0;
221
222 argv++;
223 if (!*argv || *argv[0] == '\0') {
224 (void)fprintf(stderr, errfew, 1, cap);
225 return NULL;
226 }
227 arg_rows = atoi(*argv);
228
229 (void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
230 break;
231
232 case 2:
233 argv++;
234 if (!*argv || *argv[0] == '\0') {
235 (void)fprintf(stderr, errfew, 2, cap);
236 return NULL;
237 }
238 arg_cols = atoi(*argv);
239
240 argv++;
241 if (!*argv || *argv[0] == '\0') {
242 (void)fprintf(stderr, errfew, 2, cap);
243 return NULL;
244 }
245 arg_rows = atoi(*argv);
246
247 (void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
248 break;
249
250 default:
251 (void)fprintf(stderr, errmany, arg_need, cap);
252 return NULL;
253 }
254 return argv;
255 }
256