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