2010-02-03 18:34:37 +03:00
|
|
|
/* $NetBSD: tput.c,v 1.20 2010/02/03 15:34:46 roy Exp $ */
|
1994-12-07 11:49:07 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-12-07 11:49:07 +03:00
|
|
|
* Copyright (c) 1980, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-14 05:46:32 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-07 11:49:07 +03:00
|
|
|
#if 0
|
1995-09-01 02:11:37 +04:00
|
|
|
static char sccsid[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95";
|
1994-12-07 11:49:07 +03:00
|
|
|
#endif
|
2010-02-03 18:34:37 +03:00
|
|
|
__RCSID("$NetBSD: tput.c,v 1.20 2010/02/03 15:34:46 roy Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-06-20 02:47:37 +04:00
|
|
|
#include <termios.h>
|
1994-12-07 11:49:07 +03:00
|
|
|
|
|
|
|
#include <err.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-01-25 02:54:57 +03:00
|
|
|
#include <stdlib.h>
|
2000-07-03 06:51:12 +04:00
|
|
|
#include <string.h>
|
2010-02-03 18:34:37 +03:00
|
|
|
#include <term.h>
|
1997-10-14 05:46:32 +04:00
|
|
|
#include <termcap.h>
|
1994-12-07 11:49:07 +03:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-07-30 18:43:13 +04:00
|
|
|
static int outc(int);
|
|
|
|
static void prlongname(char *);
|
2007-12-15 22:44:37 +03:00
|
|
|
static void usage(void) __dead;
|
2010-02-03 18:34:37 +03:00
|
|
|
static char **process(const char *, const char *, char **);
|
1994-01-25 02:54:57 +03:00
|
|
|
|
|
|
|
int
|
2005-07-30 18:43:13 +04:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-01-25 02:54:57 +03:00
|
|
|
int ch, exitval, n;
|
2010-02-03 18:34:37 +03:00
|
|
|
char *term, tbuf[1024];
|
|
|
|
const char *p, *s;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
term = NULL;
|
1997-10-20 04:50:53 +04:00
|
|
|
while ((ch = getopt(argc, argv, "T:")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
|
|
|
case 'T':
|
|
|
|
term = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1994-12-07 11:49:07 +03:00
|
|
|
if (!term && !(term = getenv("TERM")))
|
2005-07-30 18:43:13 +04:00
|
|
|
errx(2, "No terminal type specified and no TERM "
|
|
|
|
"variable set in the environment.");
|
2010-02-03 18:34:37 +03:00
|
|
|
setupterm(term, 0, NULL);
|
1994-01-25 02:54:57 +03:00
|
|
|
for (exitval = 0; (p = *argv) != NULL; ++argv) {
|
1994-12-07 11:49:07 +03:00
|
|
|
switch (*p) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'c':
|
|
|
|
if (!strcmp(p, "clear"))
|
2010-02-03 18:34:37 +03:00
|
|
|
p = "clear";
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'i':
|
2010-02-03 18:34:37 +03:00
|
|
|
if (!strcmp(p, "init")) {
|
|
|
|
s = tigetstr("is1");
|
|
|
|
if (s != NULL)
|
|
|
|
tputs(s, 0, outc);
|
|
|
|
p = "is2";
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
1994-03-19 10:42:18 +03:00
|
|
|
if (!strcmp(p, "longname")) {
|
1993-03-21 12:45:37 +03:00
|
|
|
prlongname(tbuf);
|
1994-03-19 10:42:18 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'r':
|
2010-02-03 18:34:37 +03:00
|
|
|
if (!strcmp(p, "reset")) {
|
|
|
|
s = tigetstr("rs1");
|
|
|
|
if (s != NULL)
|
|
|
|
tputs(s, 0, outc);
|
|
|
|
p = "rs2";
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-02-03 18:34:37 +03:00
|
|
|
if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
|
|
|
|
((s = tgetstr(p, NULL)) != NULL))
|
|
|
|
argv = process(p, s, argv);
|
|
|
|
else if ((((n = tigetnum(p)) != -1 && n != -2 )||
|
|
|
|
(n = tgetnum(p)) != -1))
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("%d\n", n);
|
2010-02-03 18:34:37 +03:00
|
|
|
else {
|
|
|
|
exitval = tigetflag(p);
|
|
|
|
if (exitval == -1)
|
|
|
|
exitval = !tgetflag(p);
|
|
|
|
else
|
|
|
|
exitval = !exitval;
|
|
|
|
}
|
1994-01-25 02:54:57 +03:00
|
|
|
|
|
|
|
if (argv == NULL)
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-07-30 18:43:13 +04:00
|
|
|
return argv ? exitval : 2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-01-25 02:54:57 +03:00
|
|
|
static void
|
2005-07-30 18:43:13 +04:00
|
|
|
prlongname(char *buf)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int savech;
|
1994-12-07 11:49:07 +03:00
|
|
|
char *p, *savep;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-01-25 02:54:57 +03:00
|
|
|
for (p = buf; *p && *p != ':'; ++p)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
savech = *(savep = p);
|
1994-01-25 02:54:57 +03:00
|
|
|
for (*p = '\0'; p >= buf && *p != '|'; --p)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("%s\n", p + 1);
|
|
|
|
*savep = savech;
|
|
|
|
}
|
|
|
|
|
1994-01-25 02:54:57 +03:00
|
|
|
static char **
|
2010-02-03 18:34:37 +03:00
|
|
|
process(const char *cap, const char *str, char **argv)
|
1994-01-25 02:54:57 +03:00
|
|
|
{
|
2000-10-11 18:46:00 +04:00
|
|
|
static const char errfew[] =
|
2005-07-30 18:43:13 +04:00
|
|
|
"Not enough arguments (%d) for capability `%s'";
|
2000-10-11 18:46:00 +04:00
|
|
|
static const char erresc[] =
|
2005-07-30 18:43:13 +04:00
|
|
|
"Unknown %% escape `%c' for capability `%s'";
|
2010-02-03 18:34:37 +03:00
|
|
|
char c, l;
|
|
|
|
const char *p;
|
|
|
|
int arg_need, p1, p2, p3, p4, p5, p6, p7, p8, p9;
|
1994-12-07 11:49:07 +03:00
|
|
|
|
|
|
|
/* Count how many values we need for this capability. */
|
2010-02-03 18:34:37 +03:00
|
|
|
arg_need = 0;
|
|
|
|
p = str;
|
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
if (c != '%')
|
|
|
|
continue;
|
|
|
|
c = *p++;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
if (c != 'p')
|
|
|
|
continue;
|
|
|
|
c = *p++;
|
|
|
|
if (c < '1' || c > '9')
|
|
|
|
errx(2, erresc, c, cap);
|
|
|
|
l = c - '0';
|
|
|
|
if (l > arg_need)
|
|
|
|
arg_need = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NEXT_ARG \
|
|
|
|
{ \
|
|
|
|
if (*++argv == NULL || *argv[0] == '\0') \
|
|
|
|
errx(2, errfew, 1, cap); \
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_need > 0) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p1 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p1 = 0;
|
|
|
|
if (arg_need > 1) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p2 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p2 = 0;
|
|
|
|
if (arg_need > 2) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p3 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p3 = 0;
|
|
|
|
if (arg_need > 3) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p4 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p4 = 0;
|
|
|
|
if (arg_need > 4) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p5 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p5 = 0;
|
|
|
|
if (arg_need > 5) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p6 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p6 = 0;
|
|
|
|
if (arg_need > 6) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p7 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p7 = 0;
|
|
|
|
if (arg_need > 7) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p8 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p8 = 0;
|
|
|
|
if (arg_need > 8) {
|
|
|
|
NEXT_ARG;
|
|
|
|
p9 = atoi(*argv);
|
|
|
|
} else
|
|
|
|
p9 = 0;
|
1994-01-25 02:54:57 +03:00
|
|
|
|
1994-12-07 11:49:07 +03:00
|
|
|
/* And print them. */
|
2010-02-03 18:34:37 +03:00
|
|
|
(void)tputs(tparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9), 0, outc);
|
2005-07-30 18:43:13 +04:00
|
|
|
return argv;
|
1994-12-07 11:49:07 +03:00
|
|
|
}
|
|
|
|
|
1999-10-05 03:26:59 +04:00
|
|
|
static int
|
2010-02-03 18:34:37 +03:00
|
|
|
outc(int c)
|
1994-12-07 11:49:07 +03:00
|
|
|
{
|
2005-07-30 18:43:13 +04:00
|
|
|
return putchar(c);
|
1994-12-07 11:49:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-07-30 18:43:13 +04:00
|
|
|
usage(void)
|
1994-12-07 11:49:07 +03:00
|
|
|
{
|
2004-07-23 17:33:22 +04:00
|
|
|
(void)fprintf(stderr,
|
2005-07-30 18:43:13 +04:00
|
|
|
"Usage: %s [-T term] attribute [attribute-args] ...\n",
|
2004-07-23 17:33:22 +04:00
|
|
|
getprogname());
|
2005-07-30 18:43:13 +04:00
|
|
|
exit(2);
|
1994-01-25 02:54:57 +03:00
|
|
|
}
|