Factor by Chris Simmons
Tput by Jan-Rixt Van Hoye (fixed doesnt require ncurses now) Cut by Jan-Rixt Van Hoye git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6362 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,291 @@
|
|||||||
|
/* $NetBSD: cut.c,v 1.16 2003/08/07 11:13:32 agc Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1989, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* This code is derived from software contributed to Berkeley by
|
||||||
|
* Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
|
||||||
|
*
|
||||||
|
* 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. 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.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* adapted by Jan-Rixt Van Hoye for OpenBeOs
|
||||||
|
*/
|
||||||
|
#define _POSIX2_LINE_MAX 2048
|
||||||
|
|
||||||
|
//#include <sys/cdefs.h>
|
||||||
|
#ifndef lint
|
||||||
|
static char copyright[]= "@(#) Copyright (c) 1989, 1993\n\
|
||||||
|
The Regents of the University of California. All rights reserved.\n";
|
||||||
|
#endif /* not lint */
|
||||||
|
|
||||||
|
#ifndef lint
|
||||||
|
#if 0
|
||||||
|
static char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
|
||||||
|
#endif
|
||||||
|
static char rcsid[]="$NetBSD: cut.c,v 1.16 2003/08/07 11:13:32 agc Exp $";
|
||||||
|
#endif /* not lint */
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
//#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int cflag;
|
||||||
|
char dchar;
|
||||||
|
int dflag;
|
||||||
|
int fflag;
|
||||||
|
int sflag;
|
||||||
|
|
||||||
|
void c_cut __P((FILE *, char *));
|
||||||
|
void f_cut __P((FILE *, char *));
|
||||||
|
void get_list __P((char *));
|
||||||
|
int main __P((int, char **));
|
||||||
|
void usage __P((void));
|
||||||
|
|
||||||
|
int
|
||||||
|
main(argc, argv)
|
||||||
|
int argc;
|
||||||
|
char *argv[];
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
void (*fcn) __P((FILE *, char *));
|
||||||
|
int ch;
|
||||||
|
|
||||||
|
fcn = NULL;
|
||||||
|
setlocale (LC_ALL, "");
|
||||||
|
|
||||||
|
dchar = '\t'; /* default delimiter is \t */
|
||||||
|
|
||||||
|
/* Since we don't support multi-byte characters, the -c and -b
|
||||||
|
options are equivalent, and the -n option is meaningless. */
|
||||||
|
while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1)
|
||||||
|
switch(ch) {
|
||||||
|
case 'b':
|
||||||
|
case 'c':
|
||||||
|
fcn = c_cut;
|
||||||
|
get_list(optarg);
|
||||||
|
cflag = 1;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
dchar = *optarg;
|
||||||
|
dflag = 1;
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
get_list(optarg);
|
||||||
|
fcn = f_cut;
|
||||||
|
fflag = 1;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
sflag = 1;
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
break;
|
||||||
|
case '?':
|
||||||
|
default:
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
argc -= optind;
|
||||||
|
argv += optind;
|
||||||
|
|
||||||
|
if (fflag) {
|
||||||
|
if (cflag)
|
||||||
|
usage();
|
||||||
|
} else if (!cflag || dflag || sflag)
|
||||||
|
usage();
|
||||||
|
|
||||||
|
if (*argv)
|
||||||
|
for (; *argv; ++argv) {
|
||||||
|
if (!(fp = fopen(*argv, "r")))
|
||||||
|
fprintf(stderr, "%s", *argv);
|
||||||
|
fcn(fp, *argv);
|
||||||
|
(void)fclose(fp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
fcn(stdin, "stdin");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int autostart, autostop, maxval;
|
||||||
|
|
||||||
|
char positions[_POSIX2_LINE_MAX + 1];
|
||||||
|
|
||||||
|
void
|
||||||
|
get_list(list)
|
||||||
|
char *list;
|
||||||
|
{
|
||||||
|
int setautostart, start, stop;
|
||||||
|
char *pos;
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* set a byte in the positions array to indicate if a field or
|
||||||
|
* column is to be selected; use +1, it's 1-based, not 0-based.
|
||||||
|
* This parser is less restrictive than the Draft 9 POSIX spec.
|
||||||
|
* POSIX doesn't allow lists that aren't in increasing order or
|
||||||
|
* overlapping lists. We also handle "-3-5" although there's no
|
||||||
|
* real reason too.
|
||||||
|
*/
|
||||||
|
for (; (p = strtok(list, ", \t")) != NULL; list = NULL) {
|
||||||
|
setautostart = start = stop = 0;
|
||||||
|
if (*p == '-') {
|
||||||
|
++p;
|
||||||
|
setautostart = 1;
|
||||||
|
}
|
||||||
|
if (isdigit((unsigned char)*p)) {
|
||||||
|
start = stop = strtol(p, &p, 10);
|
||||||
|
if (setautostart && start > autostart)
|
||||||
|
autostart = start;
|
||||||
|
}
|
||||||
|
if (*p == '-') {
|
||||||
|
if (isdigit((unsigned char)p[1]))
|
||||||
|
stop = strtol(p + 1, &p, 10);
|
||||||
|
if (*p == '-') {
|
||||||
|
++p;
|
||||||
|
if (!autostop || autostop > stop)
|
||||||
|
autostop = stop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (*p)
|
||||||
|
fprintf(stderr, "[-cf] list: illegal list value");
|
||||||
|
if (!stop || !start)
|
||||||
|
fprintf(stderr, "[-cf] list: values may not include zero");
|
||||||
|
if (stop > _POSIX2_LINE_MAX)
|
||||||
|
fprintf(stderr, "[-cf] list: %d too large (max %d)",
|
||||||
|
stop, _POSIX2_LINE_MAX);
|
||||||
|
if (maxval < stop)
|
||||||
|
maxval = stop;
|
||||||
|
for (pos = positions + start; start++ <= stop; *pos++ = 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* overlapping ranges */
|
||||||
|
if (autostop && maxval > autostop)
|
||||||
|
maxval = autostop;
|
||||||
|
|
||||||
|
/* set autostart */
|
||||||
|
if (autostart)
|
||||||
|
memset(positions + 1, '1', autostart);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ARGSUSED */
|
||||||
|
void
|
||||||
|
c_cut(fp, fname)
|
||||||
|
FILE *fp;
|
||||||
|
char *fname;
|
||||||
|
{
|
||||||
|
int ch, col;
|
||||||
|
char *pos;
|
||||||
|
|
||||||
|
ch = 0;
|
||||||
|
for (;;) {
|
||||||
|
pos = positions + 1;
|
||||||
|
for (col = maxval; col; --col) {
|
||||||
|
if ((ch = getc(fp)) == EOF)
|
||||||
|
return;
|
||||||
|
if (ch == '\n')
|
||||||
|
break;
|
||||||
|
if (*pos++)
|
||||||
|
(void)putchar(ch);
|
||||||
|
}
|
||||||
|
if (ch != '\n') {
|
||||||
|
if (autostop)
|
||||||
|
while ((ch = getc(fp)) != EOF && ch != '\n')
|
||||||
|
(void)putchar(ch);
|
||||||
|
else
|
||||||
|
while ((ch = getc(fp)) != EOF && ch != '\n');
|
||||||
|
}
|
||||||
|
(void)putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
f_cut(fp, fname)
|
||||||
|
FILE *fp;
|
||||||
|
char *fname;
|
||||||
|
{
|
||||||
|
int ch, field, isdelim;
|
||||||
|
char *pos, *p, sep;
|
||||||
|
int output;
|
||||||
|
char lbuf[_POSIX2_LINE_MAX + 1];
|
||||||
|
|
||||||
|
for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) {
|
||||||
|
output = 0;
|
||||||
|
for (isdelim = 0, p = lbuf;; ++p) {
|
||||||
|
if (!(ch = *p))
|
||||||
|
fprintf(stderr, "%s: line too long.", fname);
|
||||||
|
/* this should work if newline is delimiter */
|
||||||
|
if (ch == sep)
|
||||||
|
isdelim = 1;
|
||||||
|
if (ch == '\n') {
|
||||||
|
if (!isdelim && !sflag)
|
||||||
|
(void)printf("%s", lbuf);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isdelim)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
pos = positions + 1;
|
||||||
|
for (field = maxval, p = lbuf; field; --field, ++pos) {
|
||||||
|
if (*pos) {
|
||||||
|
if (output++)
|
||||||
|
(void)putchar(sep);
|
||||||
|
while ((ch = *p++) != '\n' && ch != sep)
|
||||||
|
(void)putchar(ch);
|
||||||
|
} else {
|
||||||
|
while ((ch = *p++) != '\n' && ch != sep)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (ch == '\n')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ch != '\n') {
|
||||||
|
if (autostop) {
|
||||||
|
if (output)
|
||||||
|
(void)putchar(sep);
|
||||||
|
for (; (ch = *p) != '\n'; ++p)
|
||||||
|
(void)putchar(ch);
|
||||||
|
} else
|
||||||
|
for (; (ch = *p) != '\n'; ++p);
|
||||||
|
}
|
||||||
|
(void)putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
(void)fprintf(stderr, "usage:\tcut -b list [-n] [file ...]\n"
|
||||||
|
"\tcut -c list [file1 ...]\n"
|
||||||
|
"\tcut -f list [-d delim] [-s] [file ...]\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
+129
-300
@@ -1,20 +1,5 @@
|
|||||||
/* $OpenBSD: tput.c,v 1.15 2003/06/17 21:56:26 millert Exp $ */
|
/* $NetBSD: tput.c,v 1.15 2003/08/07 11:16:46 agc Exp $ */
|
||||||
|
|
||||||
/*
|
|
||||||
* Copyright (c) 1999 Todd C. Miller <[email protected]>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
/*-
|
/*-
|
||||||
* Copyright (c) 1980, 1988, 1993
|
* Copyright (c) 1980, 1988, 1993
|
||||||
* The Regents of the University of California. All rights reserved.
|
* The Regents of the University of California. All rights reserved.
|
||||||
@@ -44,6 +29,7 @@
|
|||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//#include <sys/cdefs.h>
|
||||||
#ifndef lint
|
#ifndef lint
|
||||||
static char copyright[] ="@(#) Copyright (c) 1980, 1988, 1993\n\
|
static char copyright[] ="@(#) Copyright (c) 1980, 1988, 1993\n\
|
||||||
The Regents of the University of California. All rights reserved.\n";
|
The Regents of the University of California. All rights reserved.\n";
|
||||||
@@ -53,52 +39,39 @@ static char copyright[] = "@(#) Copyright (c) 1980, 1988, 1993\n\
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95";
|
static char sccsid[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95";
|
||||||
#endif
|
#endif
|
||||||
static char rcsid[] = "$OpenBSD: tput.c,v 1.15 2003/06/17 21:56:26 millert Exp $";
|
static char rcsid[] = "$NetBSD: tput.c,v 1.15 2003/08/07 11:16:46 agc Exp $";
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <termios.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
//#include <err.h>
|
||||||
#include <curses.h>
|
|
||||||
#include <term.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <termios.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <termcap.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <sys/wait.h>
|
int main __P((int, char **));
|
||||||
|
static int outc __P((int));
|
||||||
static void init(void);
|
static void prlongname __P((char *));
|
||||||
static char **process(char *, char *, char **);
|
static void setospeed __P((void));
|
||||||
static void reset(void);
|
static void usage __P((void));
|
||||||
static void set_margins(void);
|
static char **process __P((char *, char *, char **));
|
||||||
static void usage(void);
|
|
||||||
|
|
||||||
extern char *__progname;
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(argc, argv)
|
||||||
|
int argc;
|
||||||
|
char **argv;
|
||||||
{
|
{
|
||||||
int ch, exitval, n, Sflag, Vflag;
|
int ch, exitval, n;
|
||||||
char *p, *term, *str;
|
char *cptr, *p, *term, buf[1024], tbuf[1024];
|
||||||
char **oargv;
|
|
||||||
|
|
||||||
oargv = argv;
|
|
||||||
term = NULL;
|
term = NULL;
|
||||||
Sflag = Vflag = exitval = 0;
|
while ((ch = getopt(argc, argv, "T:")) != -1)
|
||||||
while ((ch = getopt(argc, argv, "VST:")) != -1)
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case 'T':
|
case 'T':
|
||||||
term = optarg;
|
term = optarg;
|
||||||
break;
|
break;
|
||||||
case 'S':
|
|
||||||
Sflag = 1;
|
|
||||||
break;
|
|
||||||
/*<Jan-Rixt> 25/09/2003 version of ncursus*/
|
|
||||||
case 'V':
|
|
||||||
Vflag = 1;
|
|
||||||
break;
|
|
||||||
case '?':
|
case '?':
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
@@ -106,300 +79,156 @@ main(int argc, char *argv[])
|
|||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (Sflag && argc > 0)
|
|
||||||
usage();
|
|
||||||
|
|
||||||
if (!term && !(term = getenv("TERM")))
|
if (!term && !(term = getenv("TERM")))
|
||||||
fprintf(stderr, "No value for $TERM and no -T specified");
|
fprintf(stderr, "no terminal type specified and no TERM environmental variable.");
|
||||||
|
if (tgetent(tbuf, term) != 1)
|
||||||
/*
|
fprintf(stderr, "tgetent failure");
|
||||||
* NOTE: tgetent() will call setupterm() and set ospeed for us
|
setospeed();
|
||||||
* (this is ncurses-specific behavior)
|
for (exitval = 0; (p = *argv) != NULL; ++argv) {
|
||||||
*/
|
|
||||||
if (tgetent(NULL, term) != 1)
|
|
||||||
fprintf(stderr, "Unknown terminal type `%s'", term);
|
|
||||||
|
|
||||||
if (strcmp(__progname, "clear") == 0) {
|
|
||||||
if (Sflag)
|
|
||||||
usage();
|
|
||||||
argv = oargv;
|
|
||||||
*argv = __progname;
|
|
||||||
*(argv+1) = NULL;
|
|
||||||
}
|
|
||||||
if (Sflag) {
|
|
||||||
char **av;
|
|
||||||
/* Build new argv based on stdin */
|
|
||||||
argc = n = 0;
|
|
||||||
av = NULL;
|
|
||||||
str = malloc(1024);
|
|
||||||
while (fgets(str,1024,stdin) && (str[0] != '!')) {
|
|
||||||
if (str[strlen(str)-1] != '\n')
|
|
||||||
fprintf(stderr, "premature EOF");
|
|
||||||
str[strlen(str)-1] = '\0';
|
|
||||||
/* grow av as needed */
|
|
||||||
if (argc + 1 >= n) {
|
|
||||||
n += 64;
|
|
||||||
av = (char **)realloc(av, sizeof(char *) * n);
|
|
||||||
if (av == NULL)
|
|
||||||
fprintf(stderr, "out of memory 1");
|
|
||||||
}
|
|
||||||
/*<Jan-Rixt> 25/09/2003 adaption needed for OpenBeOs*/
|
|
||||||
p = strtok(str," ");
|
|
||||||
while (p != NULL) {
|
|
||||||
if (*p != '\0' && (av[argc++] = strdup(p)) == NULL)
|
|
||||||
fprintf(stderr, "out of memory 2");
|
|
||||||
p = strtok(NULL, " \t");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (argc > 0) {
|
|
||||||
av[argc] = NULL;
|
|
||||||
argv = av;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(Vflag)
|
|
||||||
{
|
|
||||||
printf("%s\n",curses_version());
|
|
||||||
}
|
|
||||||
while ((p = *argv++)) {
|
|
||||||
switch (*p) {
|
switch (*p) {
|
||||||
|
case 'c':
|
||||||
|
if (!strcmp(p, "clear"))
|
||||||
|
p = "cl";
|
||||||
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
if (!strcmp(p, "init")) {
|
if (!strcmp(p, "init"))
|
||||||
init();
|
p = "is";
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
if (!strcmp(p, "longname")) {
|
if (!strcmp(p, "longname")) {
|
||||||
puts(longname());
|
prlongname(tbuf);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
if (!strcmp(p, "reset")) {
|
if (!strcmp(p, "reset"))
|
||||||
reset();
|
p = "rs";
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
cptr = buf;
|
||||||
/* First try as terminfo */
|
if (tgetstr(p, &cptr))
|
||||||
if ((str = tigetstr(p)) && str != (char *)-1)
|
argv = process(p, buf, argv);
|
||||||
argv = process(p, str, argv);
|
|
||||||
else if ((n = tigetnum(p)) != -2)
|
|
||||||
(void)printf("%d\n", n);
|
|
||||||
else if ((n = tigetflag(p)) != -1)
|
|
||||||
exitval = !n;
|
|
||||||
/* Then fall back on termcap */
|
|
||||||
else if ((str = tgetstr(p, NULL)))
|
|
||||||
argv = process(p, str, argv);
|
|
||||||
else if ((n = tgetnum(p)) != -1)
|
else if ((n = tgetnum(p)) != -1)
|
||||||
(void)printf("%d\n", n);
|
(void)printf("%d\n", n);
|
||||||
else if ((exitval = tgetflag(p)) != 0)
|
else
|
||||||
exitval = !exitval;
|
exitval = !tgetflag(p);
|
||||||
else {
|
|
||||||
(void)printf("Unknown terminfo capability `%s'\n", p);
|
if (argv == NULL)
|
||||||
exitval = 4;
|
break;
|
||||||
}
|
}
|
||||||
|
exit(argv ? exitval : 2);
|
||||||
}
|
}
|
||||||
exit(exitval);
|
|
||||||
|
static void
|
||||||
|
prlongname(buf)
|
||||||
|
char *buf;
|
||||||
|
{
|
||||||
|
int savech;
|
||||||
|
char *p, *savep;
|
||||||
|
|
||||||
|
for (p = buf; *p && *p != ':'; ++p)
|
||||||
|
continue;
|
||||||
|
savech = *(savep = p);
|
||||||
|
for (*p = '\0'; p >= buf && *p != '|'; --p)
|
||||||
|
continue;
|
||||||
|
(void)printf("%s\n", p + 1);
|
||||||
|
*savep = savech;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **
|
static char **
|
||||||
process(char *cap, char *str, char **argv)
|
process(cap, str, argv)
|
||||||
|
char *cap, *str, **argv;
|
||||||
{
|
{
|
||||||
char *cp, *s, *nargv[9];
|
static const char errfew[] =
|
||||||
int arg_need, popcount, i;
|
"not enough arguments (%d) for capability `%s'";
|
||||||
|
static const char errmany[] =
|
||||||
|
"too many arguments (%d) for capability `%s'";
|
||||||
|
static const char erresc[] =
|
||||||
|
"unknown %% escape `%c' for capability `%s'";
|
||||||
|
char *cp;
|
||||||
|
int arg_need, arg_rows, arg_cols;
|
||||||
|
|
||||||
/* Count how many values we need for this capability. */
|
/* Count how many values we need for this capability. */
|
||||||
for (cp = str, arg_need = popcount = 0; *cp != '\0'; cp++) {
|
for (cp = str, arg_need = 0; *cp != '\0'; cp++)
|
||||||
if (*cp == '%') {
|
if (*cp == '%')
|
||||||
switch (*++cp) {
|
switch (*++cp) {
|
||||||
case '%':
|
|
||||||
cp++;
|
|
||||||
break;
|
|
||||||
case 'i':
|
|
||||||
if (popcount < 2)
|
|
||||||
popcount = 2;
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
cp++;
|
|
||||||
if (isdigit(cp[1]) && popcount < cp[1] - '0')
|
|
||||||
popcount = cp[1] - '0';
|
|
||||||
break;
|
|
||||||
case 'd':
|
case 'd':
|
||||||
case 's':
|
|
||||||
case '0':
|
|
||||||
case '1':
|
|
||||||
case '2':
|
case '2':
|
||||||
case '3':
|
case '3':
|
||||||
case '4':
|
|
||||||
case '5':
|
|
||||||
case '6':
|
|
||||||
case '7':
|
|
||||||
case '8':
|
|
||||||
case '9':
|
|
||||||
case '.':
|
case '.':
|
||||||
case '+':
|
case '+':
|
||||||
arg_need++;
|
arg_need++;
|
||||||
break;
|
break;
|
||||||
default:
|
case '%':
|
||||||
break;
|
case '>':
|
||||||
}
|
case 'i':
|
||||||
}
|
case 'r':
|
||||||
}
|
case 'n':
|
||||||
arg_need = MAX(arg_need, popcount);
|
case 'B':
|
||||||
if (arg_need > 9)
|
case 'D':
|
||||||
fprintf(stderr, "too many arguments (%d) for capability `%s'",
|
|
||||||
arg_need, cap);
|
|
||||||
|
|
||||||
for (i = 0; i < arg_need; i++) {
|
|
||||||
long l;
|
|
||||||
|
|
||||||
if (argv[i] == NULL)
|
|
||||||
fprintf(stderr, "not enough arguments (%d) for capability `%s'",
|
|
||||||
arg_need, cap);
|
|
||||||
|
|
||||||
/* convert ascii representation of numbers to longs */
|
|
||||||
if (isdigit(argv[i][0]) && (l = strtol(argv[i], &cp, 10)) >= 0
|
|
||||||
&& l < LONG_MAX && *cp == '\0')
|
|
||||||
nargv[i] = (char *)l;
|
|
||||||
else
|
|
||||||
nargv[i] = argv[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
s = tparm(str, nargv[0], nargv[1], nargv[2], nargv[3],
|
|
||||||
nargv[4], nargv[5], nargv[6], nargv[7], nargv[8]);
|
|
||||||
putp(s);
|
|
||||||
fflush(stdout);
|
|
||||||
|
|
||||||
return (argv + arg_need);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
init(void)
|
|
||||||
{
|
|
||||||
FILE *ifile;
|
|
||||||
char *buf;
|
|
||||||
int wstatus;
|
|
||||||
|
|
||||||
if (init_prog /*&& !issetugid()*/) {
|
|
||||||
switch (fork()) {
|
|
||||||
case -1:
|
|
||||||
fprintf(stderr, "fork");
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
/* child */
|
|
||||||
execl(init_prog, init_prog, (char *)NULL);
|
|
||||||
_exit(127);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
wait(&wstatus);
|
|
||||||
/* parent */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (init_1string)
|
|
||||||
putp(init_1string);
|
|
||||||
if (init_2string)
|
|
||||||
putp(init_2string);
|
|
||||||
set_margins();
|
|
||||||
/* always use 8 space tabs */
|
|
||||||
if (init_tabs != 8 && clear_all_tabs && set_tab) {
|
|
||||||
int i;
|
|
||||||
|
|
||||||
putp(clear_all_tabs);
|
|
||||||
for (i = 0; i < (columns - 1) / 8; i++) {
|
|
||||||
if (parm_right_cursor)
|
|
||||||
putp(tparm(parm_right_cursor, 8));
|
|
||||||
else
|
|
||||||
fputs(" ", stdout);
|
|
||||||
putp(set_tab);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buf = malloc(1024);
|
|
||||||
if (init_file /*&& !issetugid()*/ && (ifile = fopen(init_file, "r"))) {
|
|
||||||
while (fgets(buf,1024,ifile) != NULL) {
|
|
||||||
if (buf[strlen(buf)-1] != '\n')
|
|
||||||
fprintf(stderr, "premature EOF reading %s", init_file);
|
|
||||||
buf[strlen(buf)-1] = '\0';
|
|
||||||
putp(buf);
|
|
||||||
}
|
|
||||||
fclose(ifile);
|
|
||||||
}
|
|
||||||
if (init_3string)
|
|
||||||
putp(init_3string);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
reset(void)
|
|
||||||
{
|
|
||||||
FILE *rfile;
|
|
||||||
char *buf;
|
|
||||||
|
|
||||||
if (reset_1string)
|
|
||||||
putp(reset_1string);
|
|
||||||
if (reset_2string)
|
|
||||||
putp(reset_2string);
|
|
||||||
set_margins();
|
|
||||||
buf = malloc(1024);
|
|
||||||
if (reset_file /*&& !issetugid()*/ && (rfile = fopen(reset_file, "r"))) {
|
|
||||||
while (fgets(buf,1024,rfile) != NULL) {
|
|
||||||
if (buf[strlen(buf)-1] != '\n')
|
|
||||||
fprintf(stderr, "premature EOF reading %s", reset_file);
|
|
||||||
buf[strlen(buf)-1] = '\0';
|
|
||||||
putp(buf);
|
|
||||||
}
|
|
||||||
fclose(rfile);
|
|
||||||
}
|
|
||||||
if (reset_3string)
|
|
||||||
putp(reset_3string);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
set_margins(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Four possibilities:
|
* hpux has lot's of them, but we complain
|
||||||
* 1) we have set_lr_margin and can set things with one call
|
|
||||||
* 2) we have set_{left,right}_margin_parm, use two calls
|
|
||||||
* 3) we have set_{left,right}_margin, set based on position
|
|
||||||
* 4) none of the above, leave things the way they are
|
|
||||||
*/
|
*/
|
||||||
if (set_lr_margin) {
|
fprintf(stderr, erresc, *cp, cap);
|
||||||
putp(tparm(set_lr_margin, 0, columns - 1));
|
|
||||||
} else if (set_left_margin_parm && set_right_margin_parm) {
|
|
||||||
putp(tparm(set_left_margin_parm, 0));
|
|
||||||
putp(tparm(set_right_margin_parm, columns - 1));
|
|
||||||
} else if (set_left_margin && set_right_margin && clear_margins) {
|
|
||||||
putp(clear_margins);
|
|
||||||
|
|
||||||
/* go to column 0 and set the left margin */
|
|
||||||
putp(carriage_return ? carriage_return : "\r");
|
|
||||||
putp(set_left_margin);
|
|
||||||
|
|
||||||
/* go to last column and set the right margin */
|
|
||||||
if (parm_right_cursor)
|
|
||||||
putp(tparm(parm_right_cursor, columns - 1));
|
|
||||||
else
|
|
||||||
printf("%*s", columns - 1, " ");
|
|
||||||
putp(set_right_margin);
|
|
||||||
putp(carriage_return ? carriage_return : "\r");
|
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
|
||||||
|
/* And print them. */
|
||||||
|
switch (arg_need) {
|
||||||
|
case 0:
|
||||||
|
(void)tputs(str, 1, outc);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
arg_cols = 0;
|
||||||
|
|
||||||
|
if (*++argv == NULL || *argv[0] == '\0')
|
||||||
|
fprintf(stderr, errfew);
|
||||||
|
arg_rows = atoi(*argv);
|
||||||
|
|
||||||
|
(void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (*++argv == NULL || *argv[0] == '\0')
|
||||||
|
fprintf(stderr, errfew);
|
||||||
|
arg_rows = atoi(*argv);
|
||||||
|
|
||||||
|
if (*++argv == NULL || *argv[0] == '\0')
|
||||||
|
fprintf(stderr, errfew);
|
||||||
|
arg_cols = atoi(*argv);
|
||||||
|
|
||||||
|
(void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
fprintf(stderr, errmany);
|
||||||
|
}
|
||||||
|
return (argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
setospeed()
|
||||||
{
|
{
|
||||||
|
#undef ospeed
|
||||||
|
extern short ospeed;
|
||||||
|
struct termios t;
|
||||||
|
|
||||||
if (strcmp(__progname, "clear") == 0)
|
if (tcgetattr(STDOUT_FILENO, &t) != -1)
|
||||||
(void)fprintf(stderr, "usage: %s [-T term]\n", __progname);
|
ospeed = 0;
|
||||||
else
|
else
|
||||||
(void)fprintf(stderr,
|
ospeed = cfgetospeed(&t);
|
||||||
"usage: %s [-T term] attribute [attribute-args] ...\n"
|
}
|
||||||
" %s [-T term] -S\n", __progname, __progname);
|
|
||||||
|
static int
|
||||||
|
outc(c)
|
||||||
|
int c;
|
||||||
|
{
|
||||||
|
return (putchar(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user