1994-12-13 11:17:29 +03:00
|
|
|
/* $NetBSD: tput.c,v 1.6 1994/12/13 08:17:29 jtc 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-07 11:49:07 +03:00
|
|
|
static char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1980, 1988, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-07 11:49:07 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)tput.c 8.2 (Berkeley) 3/19/94";
|
|
|
|
#endif
|
1994-12-13 11:17:29 +03:00
|
|
|
static char rcsid[] = "$NetBSD: tput.c,v 1.6 1994/12/13 08:17:29 jtc Exp $";
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/termios.h>
|
1994-12-07 11:49:07 +03:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <curses.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-01-25 02:54:57 +03:00
|
|
|
#include <stdlib.h>
|
1994-12-07 11:49:07 +03:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-01-25 02:54:57 +03:00
|
|
|
static void prlongname __P((char *));
|
|
|
|
static void setospeed __P((void));
|
1994-12-07 11:49:07 +03:00
|
|
|
static void outc __P((int));
|
1994-01-25 02:54:57 +03:00
|
|
|
static void usage __P((void));
|
|
|
|
static char **process __P((char *, char *, char **));
|
|
|
|
|
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
1994-01-25 02:54:57 +03:00
|
|
|
int ch, exitval, n;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *cptr, *p, *term, buf[1024], tbuf[1024];
|
|
|
|
|
|
|
|
term = NULL;
|
|
|
|
while ((ch = getopt(argc, argv, "T:")) != EOF)
|
|
|
|
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")))
|
|
|
|
errx(2, "no terminal type specified and no TERM environmental variable.");
|
|
|
|
if (tgetent(tbuf, term) != 1)
|
|
|
|
err(2, "tgetent failure");
|
1993-03-21 12:45:37 +03:00
|
|
|
setospeed();
|
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"))
|
|
|
|
p = "cl";
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (!strcmp(p, "init"))
|
|
|
|
p = "is";
|
|
|
|
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':
|
|
|
|
if (!strcmp(p, "reset"))
|
|
|
|
p = "rs";
|
|
|
|
break;
|
|
|
|
}
|
1994-01-25 02:54:57 +03:00
|
|
|
cptr = buf;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (tgetstr(p, &cptr))
|
1994-01-25 02:54:57 +03:00
|
|
|
argv = process(p, buf, argv);
|
1993-03-21 12:45:37 +03:00
|
|
|
else if ((n = tgetnum(p)) != -1)
|
|
|
|
(void)printf("%d\n", n);
|
|
|
|
else
|
|
|
|
exitval = !tgetflag(p);
|
1994-01-25 02:54:57 +03:00
|
|
|
|
|
|
|
if (argv == NULL)
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-01-25 02:54:57 +03:00
|
|
|
exit(argv ? exitval : 2);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-01-25 02:54:57 +03:00
|
|
|
static void
|
1993-03-21 12:45:37 +03:00
|
|
|
prlongname(buf)
|
|
|
|
char *buf;
|
|
|
|
{
|
|
|
|
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 **
|
|
|
|
process(cap, str, argv)
|
1994-12-07 11:49:07 +03:00
|
|
|
char *cap, *str, **argv;
|
1994-01-25 02:54:57 +03:00
|
|
|
{
|
|
|
|
static char errfew[] =
|
1994-12-07 11:49:07 +03:00
|
|
|
"not enough arguments (%d) for capability `%s'";
|
1994-01-25 02:54:57 +03:00
|
|
|
static char errmany[] =
|
1994-12-07 11:49:07 +03:00
|
|
|
"too many arguments (%d) for capability `%s'";
|
1994-01-25 02:54:57 +03:00
|
|
|
static char erresc[] =
|
1994-12-07 11:49:07 +03:00
|
|
|
"unknown %% escape `%c' for capability `%s'";
|
1994-01-25 02:54:57 +03:00
|
|
|
char *cp;
|
|
|
|
int arg_need, arg_rows, arg_cols;
|
1994-12-07 11:49:07 +03:00
|
|
|
|
|
|
|
/* Count how many values we need for this capability. */
|
|
|
|
for (cp = str, arg_need = 0; *cp != '\0'; cp++)
|
1994-01-25 02:54:57 +03:00
|
|
|
if (*cp == '%')
|
|
|
|
switch (*++cp) {
|
|
|
|
case 'd':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '.':
|
|
|
|
case '+':
|
|
|
|
arg_need++;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
case '>':
|
|
|
|
case 'i':
|
|
|
|
case 'r':
|
|
|
|
case 'n':
|
|
|
|
case 'B':
|
|
|
|
case 'D':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* hpux has lot's of them, but we complain
|
|
|
|
*/
|
1994-12-07 11:49:07 +03:00
|
|
|
errx(2, erresc, *cp, cap);
|
1994-01-25 02:54:57 +03:00
|
|
|
}
|
|
|
|
|
1994-12-07 11:49:07 +03:00
|
|
|
/* And print them. */
|
1994-01-25 02:54:57 +03:00
|
|
|
switch (arg_need) {
|
|
|
|
case 0:
|
1994-12-07 11:49:07 +03:00
|
|
|
(void)tputs(str, 1, outc);
|
1994-01-25 02:54:57 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
arg_cols = 0;
|
|
|
|
|
1994-12-07 11:49:07 +03:00
|
|
|
if (*++argv == NULL || *argv[0] == '\0')
|
|
|
|
errx(2, errfew, 1, cap);
|
1994-01-25 02:54:57 +03:00
|
|
|
arg_rows = atoi(*argv);
|
|
|
|
|
|
|
|
(void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
|
|
|
|
break;
|
|
|
|
case 2:
|
1994-12-07 11:49:07 +03:00
|
|
|
if (*++argv == NULL || *argv[0] == '\0')
|
|
|
|
errx(2, errfew, 2, cap);
|
1994-12-13 11:17:29 +03:00
|
|
|
arg_rows = atoi(*argv);
|
1994-01-25 02:54:57 +03:00
|
|
|
|
1994-12-07 11:49:07 +03:00
|
|
|
if (*++argv == NULL || *argv[0] == '\0')
|
|
|
|
errx(2, errfew, 2, cap);
|
1994-12-13 11:17:29 +03:00
|
|
|
arg_cols = atoi(*argv);
|
1994-01-25 02:54:57 +03:00
|
|
|
|
|
|
|
(void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1994-12-07 11:49:07 +03:00
|
|
|
errx(2, errmany, arg_need, cap);
|
1994-01-25 02:54:57 +03:00
|
|
|
}
|
1994-12-07 11:49:07 +03:00
|
|
|
return (argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setospeed()
|
|
|
|
{
|
|
|
|
#undef ospeed
|
|
|
|
extern short ospeed;
|
|
|
|
struct termios t;
|
|
|
|
|
|
|
|
if (tcgetattr(STDOUT_FILENO, &t) != -1)
|
|
|
|
ospeed = 0;
|
|
|
|
else
|
|
|
|
ospeed = cfgetospeed(&t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
outc(c)
|
|
|
|
int c;
|
|
|
|
{
|
|
|
|
(void)putchar(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
|
|
|
|
exit(1);
|
1994-01-25 02:54:57 +03:00
|
|
|
}
|