Commit | Line | Data |
---|---|---|
fcd2465c | 1 | /* |
3b1bb731 KB |
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. | |
fcd2465c DF |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
3b1bb731 | 20 | "@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\ |
fcd2465c | 21 | All rights reserved.\n"; |
3b1bb731 | 22 | #endif /* not lint */ |
fcd2465c DF |
23 | |
24 | #ifndef lint | |
eee80001 | 25 | static char sccsid[] = "@(#)tput.c 5.3 (Berkeley) %G%"; |
3b1bb731 | 26 | #endif /* not lint */ |
45e33619 | 27 | |
eee80001 KB |
28 | #include <sys/termios.h> |
29 | #include <stdio.h> | |
30 | #include <unistd.h> | |
31 | ||
32 | main(argc, argv) | |
33 | int argc; | |
34 | char **argv; | |
45e33619 | 35 | { |
eee80001 KB |
36 | extern char *optarg; |
37 | extern int optind; | |
38 | int ch, exitval, myputchar(); | |
39 | char *ap, *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(); | |
3b1bb731 | 51 | } |
eee80001 KB |
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(3); | |
58 | } | |
59 | if (tgetent(tbuf, term) != 1) { | |
60 | (void)fprintf(stderr, "tput: tgetent failure.\n"); | |
61 | exit(3); | |
62 | } | |
63 | setospeed(); | |
64 | for (p = buf, exitval = 0; *argv; ++argv) | |
65 | if (ap = tgetstr(realname(*argv), &p)) | |
66 | tputs(ap, 1, myputchar); | |
67 | else | |
68 | exitval = 2; | |
69 | exit(exitval); | |
70 | } | |
71 | ||
72 | char * | |
73 | realname(s) | |
74 | char *s; | |
75 | { | |
76 | switch(*s) { | |
77 | case 'c': | |
78 | if (!strcmp(s, "clear")) | |
79 | return("cl"); | |
80 | break; | |
81 | case 'i': | |
82 | if (!strcmp(s, "init")) | |
83 | return("is"); | |
84 | break; | |
85 | case 'r': | |
86 | if (!strcmp(s, "reset")) | |
87 | return("rs"); | |
88 | break; | |
3b1bb731 | 89 | } |
eee80001 KB |
90 | return(s); |
91 | } | |
92 | ||
93 | myputchar(c) | |
94 | int c; | |
95 | { | |
96 | putchar(c); | |
97 | } | |
98 | ||
99 | setospeed() | |
100 | { | |
101 | extern int errno, ospeed; | |
102 | struct termios t; | |
103 | char *strerror(); | |
104 | ||
105 | if (tcgetattr(STDOUT_FILENO, &t) == -1) { | |
106 | (void)fprintf(stderr, "tput: %s\n", strerror(errno)); | |
107 | exit(1); | |
108 | } | |
109 | ospeed = cfgetospeed(&t); | |
110 | } | |
111 | ||
112 | usage() | |
113 | { | |
114 | (void)fprintf(stderr, "usage: tput [-T term] attribute ...\n"); | |
3b1bb731 | 115 | exit(1); |
45e33619 | 116 | } |