Annotation of src/usr.bin/tput/tput.c, Revision 1.20
1.20 ! doug 1: /* $OpenBSD: tput.c,v 1.19 2013/11/27 15:23:01 yasuoka Exp $ */
1.1 deraadt 2:
1.8 millert 3: /*
4: * Copyright (c) 1999 Todd C. Miller <Todd.Miller@courtesan.com>
5: *
1.13 millert 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.8 millert 9: *
1.15 millert 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.8 millert 17: */
1.1 deraadt 18: /*-
19: * Copyright (c) 1980, 1988, 1993
20: * The Regents of the University of California. All rights reserved.
21: *
22: * Redistribution and use in source and binary forms, with or without
23: * modification, are permitted provided that the following conditions
24: * are met:
25: * 1. Redistributions of source code must retain the above copyright
26: * notice, this list of conditions and the following disclaimer.
27: * 2. Redistributions in binary form must reproduce the above copyright
28: * notice, this list of conditions and the following disclaimer in the
29: * documentation and/or other materials provided with the distribution.
1.13 millert 30: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 31: * may be used to endorse or promote products derived from this software
32: * without specific prior written permission.
33: *
34: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: * SUCH DAMAGE.
45: */
46:
1.8 millert 47: #include <sys/param.h>
1.1 deraadt 48:
1.8 millert 49: #include <ctype.h>
1.1 deraadt 50: #include <err.h>
51: #include <curses.h>
1.8 millert 52: #include <term.h>
1.1 deraadt 53: #include <stdio.h>
54: #include <stdlib.h>
1.8 millert 55: #include <termios.h>
1.1 deraadt 56: #include <unistd.h>
1.5 dgregor 57: #include <string.h>
1.1 deraadt 58:
1.8 millert 59: #include <sys/wait.h>
60:
1.12 millert 61: static void init(void);
62: static char **process(char *, char *, char **);
63: static void reset(void);
64: static void set_margins(void);
65: static void usage(void);
1.8 millert 66:
67: extern char *__progname;
1.1 deraadt 68:
69: int
1.14 deraadt 70: main(int argc, char *argv[])
1.1 deraadt 71: {
1.8 millert 72: int ch, exitval, n, Sflag;
73: size_t len;
74: char *p, *term, *str;
75: char **oargv;
1.1 deraadt 76:
1.8 millert 77: oargv = argv;
1.1 deraadt 78: term = NULL;
1.8 millert 79: Sflag = exitval = 0;
80: while ((ch = getopt(argc, argv, "ST:")) != -1)
1.1 deraadt 81: switch(ch) {
82: case 'T':
83: term = optarg;
84: break;
1.8 millert 85: case 'S':
86: Sflag = 1;
87: break;
1.1 deraadt 88: case '?':
89: default:
90: usage();
91: }
92: argc -= optind;
93: argv += optind;
94:
1.8 millert 95: if (Sflag && argc > 0)
96: usage();
1.4 gene 97:
1.1 deraadt 98: if (!term && !(term = getenv("TERM")))
1.8 millert 99: errx(2, "No value for $TERM and no -T specified");
100:
101: /*
102: * NOTE: tgetent() will call setupterm() and set ospeed for us
103: * (this is ncurses-specific behavior)
104: */
105: if (tgetent(NULL, term) != 1)
106: errx(3, "Unknown terminal type `%s'", term);
107:
108: if (strcmp(__progname, "clear") == 0) {
109: if (Sflag)
110: usage();
111: argv = oargv;
112: *argv = __progname;
1.4 gene 113: *(argv+1) = NULL;
114: }
1.8 millert 115: if (Sflag) {
116: char **av;
117:
118: /* Build new argv based on stdin */
119: argc = n = 0;
120: av = NULL;
121: while ((str = fgetln(stdin, &len)) != NULL) {
122: if (str[len-1] != '\n')
123: errx(1, "premature EOF");
124: str[len-1] = '\0';
125: while ((p = strsep(&str, " \t")) != NULL) {
1.16 jaredy 126: /* grow av as needed */
127: if (argc + 1 >= n) {
128: n += 64;
1.20 ! doug 129: av = reallocarray(av, n,
! 130: sizeof(char *));
1.16 jaredy 131: if (av == NULL)
132: errx(1, "out of memory");
133: }
1.8 millert 134: if (*p != '\0' &&
135: (av[argc++] = strdup(p)) == NULL)
136: errx(1, "out of memory");
137: }
138: }
139: if (argc > 0) {
140: av[argc] = NULL;
141: argv = av;
142: }
143: }
144: while ((p = *argv++)) {
1.1 deraadt 145: switch (*p) {
146: case 'i':
1.8 millert 147: if (!strcmp(p, "init")) {
148: init();
149: continue;
150: }
1.1 deraadt 151: break;
152: case 'l':
153: if (!strcmp(p, "longname")) {
1.8 millert 154: puts(longname());
1.1 deraadt 155: continue;
156: }
157: break;
158: case 'r':
1.8 millert 159: if (!strcmp(p, "reset")) {
160: reset();
161: continue;
162: }
1.1 deraadt 163: break;
164: }
1.8 millert 165:
166: /* First try as terminfo */
167: if ((str = tigetstr(p)) && str != (char *)-1)
168: argv = process(p, str, argv);
169: else if ((n = tigetnum(p)) != -2)
170: (void)printf("%d\n", n);
171: else if ((n = tigetflag(p)) != -1)
172: exitval = !n;
173: /* Then fall back on termcap */
174: else if ((str = tgetstr(p, NULL)))
175: argv = process(p, str, argv);
1.7 millert 176: else if ((n = tgetnum(p)) != -1)
1.1 deraadt 177: (void)printf("%d\n", n);
1.8 millert 178: else if ((exitval = tgetflag(p)) != 0)
179: exitval = !exitval;
180: else {
181: warnx("Unknown terminfo capability `%s'", p);
182: exitval = 4;
183: }
1.1 deraadt 184: }
1.10 millert 185: exit(exitval);
1.1 deraadt 186: }
187:
188: static char **
1.14 deraadt 189: process(char *cap, char *str, char **argv)
1.1 deraadt 190: {
1.8 millert 191: char *cp, *s, *nargv[9];
192: int arg_need, popcount, i;
1.1 deraadt 193:
194: /* Count how many values we need for this capability. */
1.8 millert 195: for (cp = str, arg_need = popcount = 0; *cp != '\0'; cp++) {
196: if (*cp == '%') {
197: switch (*++cp) {
198: case '%':
199: cp++;
200: break;
201: case 'i':
202: if (popcount < 2)
203: popcount = 2;
204: break;
205: case 'p':
206: cp++;
1.18 deraadt 207: if (isdigit((unsigned char)cp[1]) &&
208: popcount < cp[1] - '0')
1.8 millert 209: popcount = cp[1] - '0';
210: break;
211: case 'd':
212: case 's':
213: case '0':
214: case '1':
215: case '2':
216: case '3':
217: case '4':
218: case '5':
219: case '6':
220: case '7':
221: case '8':
222: case '9':
223: case '.':
224: case '+':
225: arg_need++;
226: break;
227: default:
228: break;
229: }
230: }
231: }
232: arg_need = MAX(arg_need, popcount);
233: if (arg_need > 9)
234: errx(2, "too many arguments (%d) for capability `%s'",
235: arg_need, cap);
236:
237: for (i = 0; i < arg_need; i++) {
238: long l;
239:
240: if (argv[i] == NULL)
241: errx(2, "not enough arguments (%d) for capability `%s'",
242: arg_need, cap);
243:
244: /* convert ascii representation of numbers to longs */
1.19 yasuoka 245: if (isdigit((unsigned char)argv[i][0])
1.18 deraadt 246: && (l = strtol(argv[i], &cp, 10)) >= 0
1.8 millert 247: && l < LONG_MAX && *cp == '\0')
248: nargv[i] = (char *)l;
249: else
250: nargv[i] = argv[i];
251: }
1.1 deraadt 252:
1.8 millert 253: s = tparm(str, nargv[0], nargv[1], nargv[2], nargv[3],
254: nargv[4], nargv[5], nargv[6], nargv[7], nargv[8]);
255: putp(s);
256: fflush(stdout);
1.1 deraadt 257:
1.8 millert 258: return (argv + arg_need);
1.1 deraadt 259: }
260:
261: static void
1.14 deraadt 262: init(void)
1.1 deraadt 263: {
1.8 millert 264: FILE *ifile;
265: size_t len;
266: char *buf;
1.9 millert 267: int wstatus;
1.1 deraadt 268:
1.8 millert 269: if (init_prog && !issetugid()) {
270: switch (vfork()) {
271: case -1:
272: err(4, "vfork");
273: break;
274: case 0:
275: /* child */
1.11 deraadt 276: execl(init_prog, init_prog, (char *)NULL);
1.8 millert 277: _exit(127);
278: break;
279: default:
1.9 millert 280: wait(&wstatus);
281: /* parent */
1.8 millert 282: break;
283: }
284: }
285: if (init_1string)
286: putp(init_1string);
287: if (init_2string)
288: putp(init_2string);
1.9 millert 289: set_margins();
290: /* always use 8 space tabs */
291: if (init_tabs != 8 && clear_all_tabs && set_tab) {
292: int i;
293:
294: putp(clear_all_tabs);
295: for (i = 0; i < (columns - 1) / 8; i++) {
296: if (parm_right_cursor)
297: putp(tparm(parm_right_cursor, 8));
298: else
299: fputs(" ", stdout);
300: putp(set_tab);
301: }
302: }
1.8 millert 303: if (init_file && !issetugid() && (ifile = fopen(init_file, "r"))) {
304: while ((buf = fgetln(ifile, &len)) != NULL) {
305: if (buf[len-1] != '\n')
306: errx(1, "premature EOF reading %s", init_file);
307: buf[len-1] = '\0';
308: putp(buf);
309: }
310: fclose(ifile);
311: }
312: if (init_3string)
313: putp(init_3string);
314: fflush(stdout);
1.1 deraadt 315: }
316:
317: static void
1.14 deraadt 318: reset(void)
1.7 millert 319: {
1.8 millert 320: FILE *rfile;
321: size_t len;
322: char *buf;
323:
324: if (reset_1string)
325: putp(reset_1string);
326: if (reset_2string)
327: putp(reset_2string);
1.9 millert 328: set_margins();
1.8 millert 329: if (reset_file && !issetugid() && (rfile = fopen(reset_file, "r"))) {
330: while ((buf = fgetln(rfile, &len)) != NULL) {
331: if (buf[len-1] != '\n')
332: errx(1, "premature EOF reading %s", reset_file);
333: buf[len-1] = '\0';
334: putp(buf);
335: }
336: fclose(rfile);
337: }
338: if (reset_3string)
339: putp(reset_3string);
1.9 millert 340: fflush(stdout);
341: }
342:
343: static void
1.14 deraadt 344: set_margins(void)
1.9 millert 345: {
346:
347: /*
348: * Four possibilities:
349: * 1) we have set_lr_margin and can set things with one call
350: * 2) we have set_{left,right}_margin_parm, use two calls
351: * 3) we have set_{left,right}_margin, set based on position
352: * 4) none of the above, leave things the way they are
353: */
354: if (set_lr_margin) {
355: putp(tparm(set_lr_margin, 0, columns - 1));
356: } else if (set_left_margin_parm && set_right_margin_parm) {
357: putp(tparm(set_left_margin_parm, 0));
358: putp(tparm(set_right_margin_parm, columns - 1));
359: } else if (set_left_margin && set_right_margin && clear_margins) {
360: putp(clear_margins);
361:
362: /* go to column 0 and set the left margin */
363: putp(carriage_return ? carriage_return : "\r");
364: putp(set_left_margin);
365:
366: /* go to last column and set the right margin */
367: if (parm_right_cursor)
368: putp(tparm(parm_right_cursor, columns - 1));
369: else
370: printf("%*s", columns - 1, " ");
371: putp(set_right_margin);
372: putp(carriage_return ? carriage_return : "\r");
373: }
1.8 millert 374: fflush(stdout);
1.7 millert 375: }
376:
377: static void
1.14 deraadt 378: usage(void)
1.1 deraadt 379: {
1.8 millert 380:
381: if (strcmp(__progname, "clear") == 0)
382: (void)fprintf(stderr, "usage: %s [-T term]\n", __progname);
383: else
384: (void)fprintf(stderr,
385: "usage: %s [-T term] attribute [attribute-args] ...\n"
386: " %s [-T term] -S\n", __progname, __progname);
1.1 deraadt 387: exit(1);
388: }