xref: /original-bsd/usr.bin/tput/tput.c (revision da7c76f1)
1 /*
2  * Copyright (c) 1980, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)tput.c	5.6 (Berkeley) 05/12/90";
26 #endif /* not lint */
27 
28 #include <sys/termios.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 
32 main(argc, argv)
33 	int argc;
34 	char **argv;
35 {
36 	extern char *optarg;
37 	extern int optind;
38 	int ch, exitval, n, outc();
39 	char *cptr, *p, *term, buf[1024], tbuf[1024];
40 	char *getenv(), *tgetstr(), *realname();
41 
42 	term = NULL;
43 	while ((ch = getopt(argc, argv, "T:")) != EOF)
44 		switch(ch) {
45 		case 'T':
46 			term = optarg;
47 			break;
48 		case '?':
49 		default:
50 			usage();
51 		}
52 	argc -= optind;
53 	argv += optind;
54 
55 	if (!term && !(term = getenv("TERM"))) {
56 		(void)fprintf(stderr, "tput: no terminal type specified.\n");
57 		exit(2);
58 	}
59 	if (tgetent(tbuf, term) != 1) {
60 		(void)fprintf(stderr, "tput: tgetent failure.\n");
61 		exit(2);
62 	}
63 	setospeed();
64 	for (cptr = buf, exitval = 0; p = *argv; ++argv) {
65 		switch(*p) {
66 		case 'c':
67 			if (!strcmp(p, "clear"))
68 				p = "cl";
69 			break;
70 		case 'i':
71 			if (!strcmp(p, "init"))
72 				p = "is";
73 			break;
74 		case 'l':
75 			if (!strcmp(p, "longname"))
76 				prlongname(tbuf);
77 			continue;
78 		case 'r':
79 			if (!strcmp(p, "reset"))
80 				p = "rs";
81 			break;
82 		}
83 		if (tgetstr(p, &cptr))
84 			(void)tputs(buf, 1, outc);
85 		else if ((n = tgetnum(p)) != -1)
86 			(void)printf("%d\n", n);
87 		else
88 			exitval = !tgetflag(p);
89 	}
90 	exit(exitval);
91 }
92 
93 prlongname(buf)
94 	char *buf;
95 {
96 	register char *p;
97 	int savech;
98 	char *savep;
99 
100 	for (p = buf; *p && *p != ':'; ++p);
101 	savech = *(savep = p);
102 	for (*p = '\0'; p >= buf && *p != '|'; --p);
103 	(void)printf("%s\n", p + 1);
104 	*savep = savech;
105 }
106 
107 setospeed()
108 {
109 	extern int errno, ospeed;
110 	struct termios t;
111 	char *strerror();
112 
113 	if (tcgetattr(STDOUT_FILENO, &t) != -1)
114 		ospeed = 0;
115 	else
116 		ospeed = cfgetospeed(&t);
117 }
118 
119 outc(c)
120 	int c;
121 {
122 	putchar(c);
123 }
124 
125 usage()
126 {
127 	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
128 	exit(1);
129 }
130