Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / usr.bin / tput / tput.c
1 /*-
2  * Copyright (c) 1980, 1988, 1993
3  *      The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)tput.c   8.2 (Berkeley) 3/19/94
31  * $FreeBSD: src/usr.bin/tput/tput.c,v 1.7.6.3 2002/08/17 14:52:50 tjr Exp $
32  * $DragonFly: src/usr.bin/tput/tput.c,v 1.5 2005/07/30 11:53:52 liamfoy Exp $
33  */
34
35 #include <termios.h>
36
37 #include <err.h>
38 #include <termcap.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #undef putchar
45 #define outc putchar
46
47 static void   prlongname(char *);
48 static void   usage(void);
49 static char **process(const char *, const char *, char **);
50
51 int
52 main(int argc, char **argv)
53 {
54         int ch, exitval, n;
55         char *cptr, *term, buf[1024], tbuf[1024];
56         const char *p;
57
58         term = NULL;
59         while ((ch = getopt(argc, argv, "T:")) != -1)
60                 switch(ch) {
61                 case 'T':
62                         term = optarg;
63                         break;
64                 case '?':
65                 default:
66                         usage();
67                 }
68         argc -= optind;
69         argv += optind;
70
71         if (!term && !(term = getenv("TERM")))
72 errx(2, "no terminal type specified and no TERM environmental variable.");
73         if (tgetent(tbuf, term) != 1)
74                 err(3, "tgetent failure");
75         for (exitval = 0; (p = *argv) != NULL; ++argv) {
76                 switch (*p) {
77                 case 'c':
78                         if (!strcmp(p, "clear"))
79                                 p = "cl";
80                         break;
81                 case 'i':
82                         if (!strcmp(p, "init"))
83                                 p = "is";
84                         break;
85                 case 'l':
86                         if (!strcmp(p, "longname")) {
87                                 prlongname(tbuf);
88                                 continue;
89                         }
90                         break;
91                 case 'r':
92                         if (!strcmp(p, "reset"))
93                                 p = "rs";
94                         break;
95                 }
96                 cptr = buf;
97                 if (tgetstr(p, &cptr))
98                         argv = process(p, buf, argv);
99                 else if ((n = tgetnum(p)) != -1)
100                         (void)printf("%d\n", n);
101                 else
102                         exitval = !tgetflag(p);
103         }
104         exit(exitval);
105 }
106
107 static void
108 prlongname(char *buf)
109 {
110         int savech;
111         char *p, *savep;
112
113         for (p = buf; *p && *p != ':'; ++p);
114         savech = *(savep = p);
115         for (*p = '\0'; p >= buf && *p != '|'; --p);
116         (void)printf("%s\n", p + 1);
117         *savep = savech;
118 }
119
120 static char **
121 process(const char *cap, const char *str, char **argv)
122 {
123         static char errfew[] =
124             "not enough arguments (%d) for capability `%s'";
125         static char errmany[] =
126             "too many arguments (%d) for capability `%s'";
127         static char erresc[] =
128             "unknown %% escape `%c' for capability `%s'";
129         const char *cp;
130         int arg_need, arg_rows, arg_cols;
131
132         /* Count how many values we need for this capability. */
133         for (cp = str, arg_need = 0; *cp != '\0'; cp++)
134                 if (*cp == '%')
135                             switch (*++cp) {
136                             case 'd':
137                             case '2':
138                             case '3':
139                             case '.':
140                             case '+':
141                                     arg_need++;
142                                     break;
143                             case '%':
144                             case '>':
145                             case 'i':
146                             case 'r':
147                             case 'n':
148                             case 'B':
149                             case 'D':
150                                     break;
151                             case 'p':
152                                     if (cp[1]) {
153                                         cp++;
154                                         break;
155                                     }
156                             default:
157                                 /*
158                                  * hpux has lot's of them, but we complain
159                                  */
160                                  warnx(erresc, *cp, cap);
161                             }
162
163         /* And print them. */
164         switch (arg_need) {
165         case 0:
166                 (void)tputs(str, 1, outc);
167                 break;
168         case 1:
169                 arg_cols = 0;
170
171                 if (*++argv == NULL || *argv[0] == '\0')
172                         errx(2, errfew, 1, cap);
173                 arg_rows = atoi(*argv);
174
175                 (void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
176                 break;
177         case 2:
178                 if (*++argv == NULL || *argv[0] == '\0')
179                         errx(2, errfew, 2, cap);
180                 arg_cols = atoi(*argv);
181
182                 if (*++argv == NULL || *argv[0] == '\0')
183                         errx(2, errfew, 2, cap);
184                 arg_rows = atoi(*argv);
185
186                 (void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
187                 break;
188
189         default:
190                 errx(2, errmany, arg_need, cap);
191         }
192         return (argv);
193 }
194
195 static void
196 usage(void)
197 {
198         (void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
199         exit(2);
200 }