Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / contrib / ncurses-5.4 / progs / tput.c
blob70b762f7eb3f6c22e6bcc71c5b29fd73120327de
1 /****************************************************************************
2 * Copyright (c) 1998-2003,2004 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * tput.c -- shellscript access to terminal capabilities
37 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
38 * Ross Ridge's mytinfo package.
41 #include <progs.priv.h>
43 #if !PURE_TERMINFO
44 #include <termsort.c>
45 #endif
46 #include <transform.h>
48 MODULE_ID("$Id: tput.c,v 1.34 2004/01/16 23:23:11 Daniel.Jacobowitz Exp $")
50 #define PUTS(s) fputs(s, stdout)
51 #define PUTCHAR(c) putchar(c)
52 #define FLUSH fflush(stdout)
54 typedef enum {
55 Numbers = 0
56 ,Num_Str
57 ,Num_Str_Str
58 } TParams;
60 static char *prg_name;
61 static bool is_init = FALSE;
62 static bool is_reset = FALSE;
64 static void
65 quit(int status, const char *fmt,...)
67 va_list argp;
69 va_start(argp, fmt);
70 fprintf(stderr, "%s: ", prg_name);
71 vfprintf(stderr, fmt, argp);
72 fprintf(stderr, "\n");
73 va_end(argp);
74 ExitProgram(status);
77 static void
78 usage(void)
80 fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
81 ExitProgram(EXIT_FAILURE);
84 static void
85 check_aliases(const char *name)
87 is_init = (strcmp(name, PROG_INIT) == 0);
88 is_reset = (strcmp(name, PROG_RESET) == 0);
92 * Lookup the type of call we should make to tparm(). This ignores the actual
93 * terminfo capability (bad, because it is not extensible), but makes this
94 * code portable to platforms where sizeof(int) != sizeof(char *).
96 * FIXME: If we want extensibility, analyze the capability string as we do
97 * in tparm() to decide how to parse the varargs list.
99 static TParams
100 tparm_type(const char *name)
102 #define TD(code, longname, ti, tc) {code,longname},{code,ti},{code,tc}
103 TParams result = Numbers;
104 /* *INDENT-OFF* */
105 static const struct {
106 TParams code;
107 const char *name;
108 } table[] = {
109 TD(Num_Str, "pkey_key", "pfkey", "pk"),
110 TD(Num_Str, "pkey_local", "pfloc", "pl"),
111 TD(Num_Str, "pkey_xmit", "pfx", "px"),
112 TD(Num_Str, "plab_norm", "pln", "pn"),
113 TD(Num_Str_Str, "pkey_plab", "pfxl", "xl"),
115 /* *INDENT-ON* */
117 unsigned n;
118 for (n = 0; n < SIZEOF(table); n++) {
119 if (!strcmp(name, table[n].name)) {
120 result = table[n].code;
121 break;
124 return result;
127 static int
128 exit_code(int token, int value)
130 int result = 99;
132 switch (token) {
133 case BOOLEAN:
134 result = !value; /* TRUE=0, FALSE=1 */
135 break;
136 case NUMBER:
137 result = 0; /* always zero */
138 break;
139 case STRING:
140 result = value; /* 0=normal, 1=missing */
141 break;
143 return result;
146 static int
147 tput(int argc, char *argv[])
149 NCURSES_CONST char *name;
150 char *s;
151 int i, j, c;
152 int status;
153 FILE *f;
154 int token = UNDEF;
156 if ((name = argv[0]) == 0)
157 name = "";
158 check_aliases(name);
159 if (is_reset || is_init) {
160 if (init_prog != 0) {
161 system(init_prog);
163 FLUSH;
165 if (is_reset && reset_1string != 0) {
166 PUTS(reset_1string);
167 } else if (init_1string != 0) {
168 PUTS(init_1string);
170 FLUSH;
172 if (is_reset && reset_2string != 0) {
173 PUTS(reset_2string);
174 } else if (init_2string != 0) {
175 PUTS(init_2string);
177 FLUSH;
179 #ifdef set_lr_margin
180 if (set_lr_margin != 0) {
181 PUTS(tparm(set_lr_margin, 0, columns - 1));
182 } else
183 #endif
184 #ifdef set_left_margin_parm
185 if (set_left_margin_parm != 0
186 && set_right_margin_parm != 0) {
187 PUTS(tparm(set_left_margin_parm, 0));
188 PUTS(tparm(set_right_margin_parm, columns - 1));
189 } else
190 #endif
191 if (clear_margins != 0
192 && set_left_margin != 0
193 && set_right_margin != 0) {
194 PUTS(clear_margins);
195 if (carriage_return != 0) {
196 PUTS(carriage_return);
197 } else {
198 PUTCHAR('\r');
200 PUTS(set_left_margin);
201 if (parm_right_cursor) {
202 PUTS(tparm(parm_right_cursor, columns - 1));
203 } else {
204 for (i = 0; i < columns - 1; i++) {
205 PUTCHAR(' ');
208 PUTS(set_right_margin);
209 if (carriage_return != 0) {
210 PUTS(carriage_return);
211 } else {
212 PUTCHAR('\r');
215 FLUSH;
217 if (init_tabs != 8) {
218 if (clear_all_tabs != 0 && set_tab != 0) {
219 for (i = 0; i < columns - 1; i += 8) {
220 if (parm_right_cursor) {
221 PUTS(tparm(parm_right_cursor, 8));
222 } else {
223 for (j = 0; j < 8; j++)
224 PUTCHAR(' ');
226 PUTS(set_tab);
228 FLUSH;
232 if (is_reset && reset_file != 0) {
233 f = fopen(reset_file, "r");
234 if (f == 0) {
235 quit(4 + errno, "Can't open reset_file: '%s'", reset_file);
237 while ((c = fgetc(f)) != EOF) {
238 PUTCHAR(c);
240 fclose(f);
241 } else if (init_file != 0) {
242 f = fopen(init_file, "r");
243 if (f == 0) {
244 quit(4 + errno, "Can't open init_file: '%s'", init_file);
246 while ((c = fgetc(f)) != EOF) {
247 PUTCHAR(c);
249 fclose(f);
251 FLUSH;
253 if (is_reset && reset_3string != 0) {
254 PUTS(reset_3string);
255 } else if (init_2string != 0) {
256 PUTS(init_2string);
258 FLUSH;
259 return 0;
262 if (strcmp(name, "longname") == 0) {
263 PUTS(longname());
264 return 0;
266 #if !PURE_TERMINFO
268 const struct name_table_entry *np;
270 if ((np = _nc_find_entry(name, _nc_get_hash_table(1))) != 0)
271 switch (np->nte_type) {
272 case BOOLEAN:
273 if (bool_from_termcap[np->nte_index])
274 name = boolnames[np->nte_index];
275 break;
277 case NUMBER:
278 if (num_from_termcap[np->nte_index])
279 name = numnames[np->nte_index];
280 break;
282 case STRING:
283 if (str_from_termcap[np->nte_index])
284 name = strnames[np->nte_index];
285 break;
288 #endif
290 if ((status = tigetflag(name)) != -1) {
291 return exit_code(BOOLEAN, status);
292 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
293 (void) printf("%d\n", status);
294 return exit_code(NUMBER, 0);
295 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
296 quit(4, "unknown terminfo capability '%s'", name);
297 } else if (s != ABSENT_STRING) {
298 token = STRING;
299 if (argc > 1) {
300 int k;
301 int popcount;
302 long numbers[1 + NUM_PARM];
303 char *strings[1 + NUM_PARM];
304 char *p_is_s[NUM_PARM];
306 /* Nasty hack time. The tparm function needs to see numeric
307 * parameters as numbers, not as pointers to their string
308 * representations
311 for (k = 1; k < argc; k++) {
312 char *tmp = 0;
313 strings[k] = argv[k];
314 numbers[k] = strtol(argv[k], &tmp, 0);
315 if (tmp == 0 || *tmp != 0)
316 numbers[k] = 0;
318 for (k = argc; k <= NUM_PARM; k++) {
319 numbers[k] = 0;
320 strings[k] = 0;
323 switch (tparm_type(name)) {
324 case Num_Str:
325 s = tparm(s, numbers[1], strings[2]);
326 break;
327 case Num_Str_Str:
328 s = tparm(s, numbers[1], strings[2], strings[3]);
329 break;
330 default:
331 (void) _nc_tparm_analyze(s, p_is_s, &popcount);
332 #define myParam(n) (p_is_s[n - 1] != 0 ? ((long) strings[n]) : numbers[n])
333 s = tparm(s,
334 myParam(1),
335 myParam(2),
336 myParam(3),
337 myParam(4),
338 myParam(5),
339 myParam(6),
340 myParam(7),
341 myParam(8),
342 myParam(9));
343 break;
347 /* use putp() in order to perform padding */
348 putp(s);
349 return exit_code(STRING, 0);
351 return exit_code(STRING, 1);
355 main(int argc, char **argv)
357 char *term;
358 int errret;
359 bool cmdline = TRUE;
360 int c;
361 char buf[BUFSIZ];
362 int result = 0;
363 int err;
365 check_aliases(prg_name = _nc_rootname(argv[0]));
367 term = getenv("TERM");
369 while ((c = getopt(argc, argv, "ST:V")) != EOF) {
370 switch (c) {
371 case 'S':
372 cmdline = FALSE;
373 break;
374 case 'T':
375 use_env(FALSE);
376 term = optarg;
377 break;
378 case 'V':
379 puts(curses_version());
380 return EXIT_SUCCESS;
381 default:
382 usage();
383 /* NOTREACHED */
388 * Modify the argument list to omit the options we processed.
390 if (is_reset || is_init) {
391 if (optind-- < argc) {
392 argc -= optind;
393 argv += optind;
395 argv[0] = prg_name;
396 } else {
397 argc -= optind;
398 argv += optind;
401 if (term == 0 || *term == '\0')
402 quit(2, "No value for $TERM and no -T specified");
404 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
405 quit(3, "unknown terminal \"%s\"", term);
407 if (cmdline) {
408 if ((argc <= 0) && !is_reset && !is_init)
409 usage();
410 return tput(argc, argv);
413 while (fgets(buf, sizeof(buf), stdin) != 0) {
414 char *argvec[16]; /* command, 9 parms, null, & slop */
415 int argnum = 0;
416 char *cp;
418 /* crack the argument list into a dope vector */
419 for (cp = buf; *cp; cp++) {
420 if (isspace(UChar(*cp))) {
421 *cp = '\0';
422 } else if (cp == buf || cp[-1] == 0) {
423 argvec[argnum++] = cp;
424 if (argnum >= (int) SIZEOF(argvec) - 1)
425 break;
428 argvec[argnum] = 0;
430 if (argnum != 0
431 && (err = tput(argnum, argvec)) != 0) {
432 if (result == 0)
433 result = 4; /* will return value >4 */
434 ++result;
438 return result;