/****************************************************************************
- * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
+ * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
#if NEED_PTEM_H
/* they neglected to define struct winsize in termios.h -- it's only
in termio.h */
-#include <sys/stream.h>
-#include <sys/ptem.h>
+#include <sys/stream.h>
+#include <sys/ptem.h>
#endif
#include <curses.h> /* for bool typedef */
#include <dump_entry.h>
+#include <transform.h>
-MODULE_ID("$Id: tset.c,v 0.41 2000/03/12 00:03:00 tom Exp $")
+MODULE_ID("$Id: tset.c,v 0.56 2003/12/06 17:21:01 tom Exp $")
extern char **environ;
const char *_nc_progname = "tset";
-static TTY mode, oldmode;
+static TTY mode, oldmode, original;
+static bool can_restore = FALSE;
+static bool isreset = FALSE; /* invoked as reset */
static int terasechar = -1; /* new erase character */
static int intrchar = -1; /* new interrupt character */
-static int isreset; /* invoked as reset */
static int tkillchar = -1; /* new kill character */
static int tlines, tcolumns; /* window size */
-#define LOWERCASE(c) ((isalpha(c) && isupper(c)) ? tolower(c) : (c))
+#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
static int
CaselessCmp(const char *a, const char *b)
return LOWERCASE(*a) - LOWERCASE(*b);
}
+static void
+exit_error(void)
+{
+ if (can_restore)
+ SET_TTY(STDERR_FILENO, &original);
+ (void) fprintf(stderr, "\n");
+ fflush(stderr);
+ ExitProgram(EXIT_FAILURE);
+ /* NOTREACHED */
+}
+
static void
err(const char *fmt,...)
{
(void) fprintf(stderr, "tset: ");
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
- (void) fprintf(stderr, "\n");
- exit(EXIT_FAILURE);
+ exit_error();
/* NOTREACHED */
}
{
char temp[BUFSIZ];
perror(strncat(strcpy(temp, "tset: "), msg, sizeof(temp) - 10));
- exit(EXIT_FAILURE);
+ exit_error();
/* NOTREACHED */
}
static void
cat(char *file)
{
- register int fd, nr, nw;
+ FILE *fp;
+ size_t nr;
char buf[BUFSIZ];
- if ((fd = open(file, O_RDONLY, 0)) < 0)
+ if ((fp = fopen(file, "r")) == 0)
failed(file);
- while ((nr = read(fd, buf, sizeof(buf))) > 0)
- if ((nw = write(STDERR_FILENO, buf, (size_t) nr)) == -1)
- failed("write to stderr");
- if (nr != 0)
- failed(file);
- (void) close(fd);
+ while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
+ if (fwrite(buf, sizeof(char), nr, stderr) != nr)
+ failed("write to stderr");
+ fclose(fp);
}
static int
char *p;
/* We can get recalled; if so, don't continue uselessly. */
+ clearerr(stdin);
if (feof(stdin) || ferror(stdin)) {
(void) fprintf(stderr, "\n");
- exit(EXIT_FAILURE);
+ exit_error();
+ /* NOTREACHED */
}
for (;;) {
if (dflt)
if (fgets(answer, sizeof(answer), stdin) == 0) {
if (dflt == 0) {
- (void) fprintf(stderr, "\n");
- exit(EXIT_FAILURE);
+ exit_error();
+ /* NOTREACHED */
}
return (dflt);
}
const char *porttype; /* Port type, or "" for any. */
const char *type; /* Terminal type to select. */
int conditional; /* Baud rate conditionals bitmask. */
- speed_t speed; /* Baud rate to compare against. */
+ int speed; /* Baud rate to compare against. */
} MAP;
static MAP *cur, *maplist;
{"2400", B2400},
{"4800", B4800},
{"9600", B9600},
+ /* sgttyb may define up to this point */
+#ifdef B19200
{"19200", B19200},
+#endif
+#ifdef B38400
{"38400", B38400},
+#endif
+#ifdef B19200
{"19200", B19200},
+#endif
+#ifdef B38400
{"38400", B38400},
+#endif
#ifdef B19200
{"19200", B19200},
#else
char *base = 0;
copy = strdup(arg);
- mapp = malloc(sizeof(MAP));
+ mapp = (MAP *) malloc(sizeof(MAP));
if (copy == 0 || mapp == 0)
failed("malloc");
mapp->next = 0;
static const char *
get_termcap_entry(char *userarg)
{
- int rval, errret;
+ int errret;
char *p;
const char *ttype;
#if HAVE_GETTTYNAM
goto map;
if ((ttypath = ttyname(STDERR_FILENO)) != 0) {
- if ((p = strrchr(ttypath, '/')) != 0)
- ++p;
- else
- p = ttypath;
+ p = _nc_basename(ttypath);
#if HAVE_GETTTYNAM
/*
* We have the 4.3BSD library call getttynam(3); that means
while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) {
for (s = buffer, t = d = 0; *s; s++) {
- if (isspace(*s))
+ if (isspace(UChar(*s)))
*s = '\0';
else if (t == 0)
t = s;
ttype = askuser(0);
}
/* Find the terminfo entry. If it doesn't exist, ask the user. */
- while ((rval = setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO,
- &errret)) != OK) {
+ while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
+ != OK) {
if (errret == 0) {
(void) fprintf(stderr, "tset: unknown terminal type %s\n",
- ttype);
+ ttype);
ttype = 0;
} else {
(void) fprintf(stderr,
- "tset: can't initialize terminal type %s (error %d)\n",
- ttype, errret);
+ "tset: can't initialize terminal type %s (error %d)\n",
+ ttype, errret);
ttype = 0;
}
ttype = askuser(ttype);
**************************************************************************/
/* some BSD systems have these built in, some systems are missing
- * one or more definitions. The safest solution is to override.
+ * one or more definitions. The safest solution is to override unless the
+ * commonly-altered ones are defined.
*/
+#if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
#undef CEOF
#undef CERASE
#undef CINTR
#undef CSTART
#undef CSTOP
#undef CSUSP
+#endif
/* control-character defaults */
+#ifndef CEOF
#define CEOF CTRL('D')
+#endif
+#ifndef CERASE
#define CERASE CTRL('H')
+#endif
+#ifndef CINTR
#define CINTR 127 /* ^? */
+#endif
+#ifndef CKILL
#define CKILL CTRL('U')
+#endif
+#ifndef CLNEXT
#define CLNEXT CTRL('v')
+#endif
+#ifndef CRPRNT
#define CRPRNT CTRL('r')
+#endif
+#ifndef CQUIT
#define CQUIT CTRL('\\')
+#endif
+#ifndef CSTART
#define CSTART CTRL('Q')
+#endif
+#ifndef CSTOP
#define CSTOP CTRL('S')
+#endif
+#ifndef CSUSP
#define CSUSP CTRL('Z')
+#endif
#define CHK(val, dft) ((int)val <= 0 ? dft : val)
mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
#ifdef IUCLC
- | IUCLC
+ | IUCLC
#endif
#ifdef IXANY
- | IXANY
+ | IXANY
#endif
- | IXOFF);
+ | IXOFF);
mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
#ifdef IMAXBEL
- | IMAXBEL
+ | IMAXBEL
#endif
);
mode.c_oflag &= ~(0
#ifdef OLCUC
- | OLCUC
+ | OLCUC
#endif
#ifdef OCRNL
- | OCRNL
+ | OCRNL
#endif
#ifdef ONOCR
- | ONOCR
+ | ONOCR
#endif
#ifdef ONLRET
- | ONLRET
+ | ONLRET
#endif
#ifdef OFILL
- | OFILL
+ | OFILL
#endif
#ifdef OFDEL
- | OFDEL
+ | OFDEL
#endif
#ifdef NLDLY
- | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
+ | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
#endif
);
mode.c_oflag |= (OPOST
#ifdef ONLCR
- | ONLCR
+ | ONLCR
#endif
);
mode.c_cflag |= (CS8 | CREAD);
mode.c_lflag &= ~(ECHONL | NOFLSH
#ifdef TOSTOP
- | TOSTOP
+ | TOSTOP
#endif
#ifdef ECHOPTR
- | ECHOPRT
+ | ECHOPRT
#endif
#ifdef XCASE
- | XCASE
+ | XCASE
#endif
);
mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
#ifdef ECHOCTL
- | ECHOCTL
+ | ECHOCTL
#endif
#ifdef ECHOKE
- | ECHOKE
+ | ECHOKE
#endif
);
#endif
-#ifdef TERMIOS
- tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
-#else
- stty(STDERR_FILENO, &mode);
-#endif
+ SET_TTY(STDERR_FILENO, &mode);
}
/*
* Returns a "good" value for the erase character. This is loosely based on
* the BSD4.4 logic.
*/
+#ifdef TERMIOS
static int
default_erase(void)
{
return result;
}
+#endif
/*
* Update the values of the erase, interrupt, and kill characters in 'mode'.
#ifdef TAB3
if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
- tcsetattr(STDERR_FILENO, TCSADRAIN, &oldmode);
+ SET_TTY(STDERR_FILENO, &oldmode);
}
#endif
settle = set_tabs();
/*
* Tell the user if a control key has been changed from the default value.
*/
+#ifdef TERMIOS
static void
report(const char *name, int which, unsigned def)
{
-#ifdef TERMIOS
unsigned older, newer;
char *p;
if (newer == 0177)
(void) fprintf(stderr, "delete.\n");
else if ((p = key_backspace) != 0
- && newer == (unsigned char) p[0]
- && p[1] == '\0')
+ && newer == (unsigned char) p[0]
+ && p[1] == '\0')
(void) fprintf(stderr, "backspace.\n");
else if (newer < 040) {
newer ^= 0100;
(void) fprintf(stderr, "control-%c (^%c).\n", newer, newer);
} else
(void) fprintf(stderr, "%c.\n", newer);
-#endif
}
+#endif
/*
* Convert the obsolete argument forms into something that getopt can handle.
usage(const char *pname)
{
(void) fprintf(stderr,
- "usage: %s [-IQrs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n", pname);
- exit(EXIT_FAILURE);
+ "usage: %s [-IQVrs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]", pname);
+ exit_error();
+ /* NOTREACHED */
}
static char
const char *p;
const char *ttype;
-#ifdef TERMIOS
- if (tcgetattr(STDERR_FILENO, &mode) < 0)
+ if (GET_TTY(STDERR_FILENO, &mode) < 0)
failed("standard error");
-
- oldmode = mode;
+ can_restore = TRUE;
+ original = oldmode = mode;
+#ifdef TERMIOS
ospeed = cfgetospeed(&mode);
#else
- if (gtty(STDERR_FILENO, &mode) < 0)
- failed("standard error");
-
- oldmode = mode;
ospeed = mode.sg_ospeed;
#endif
- if ((p = strrchr(*argv, '/')) != 0)
- ++p;
- else
- p = *argv;
- if (!CaselessCmp(p, "reset")) {
- isreset = 1;
+ p = _nc_rootname(*argv);
+ if (!strcmp(p, PROG_RESET)) {
+ isreset = TRUE;
reset_mode();
}
obsolete(argv);
noinit = noset = quiet = Sflag = sflag = showterm = 0;
- while ((ch = getopt(argc, argv, "a:d:e:Ii:k:m:np:qQSrs")) != EOF) {
+ while ((ch = getopt(argc, argv, "a:d:e:Ii:k:m:np:qQSrsV")) != EOF) {
switch (ch) {
case 'q': /* display term only */
noset = 1;
case 's': /* output TERM set command */
sflag = 1;
break;
+ case 'V':
+ puts(curses_version());
+ return EXIT_SUCCESS;
case '?':
default:
usage(*argv);
/* Set the modes if they've changed. */
if (memcmp(&mode, &oldmode, sizeof(mode))) {
-#ifdef TERMIOS
- tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
-#else
- stty(STDERR_FILENO, &mode);
-#endif
+ SET_TTY(STDERR_FILENO, &mode);
}
}
* If erase, kill and interrupt characters could have been
* modified and not -Q, display the changes.
*/
+#ifdef TERMIOS
if (!quiet) {
report("Erase", VERASE, CERASE);
- report("Kill", VKILL, CINTR);
- report("Interrupt", VINTR, CKILL);
+ report("Kill", VKILL, CKILL);
+ report("Interrupt", VINTR, CINTR);
}
+#endif
}
if (Sflag)
err("The -S option is not supported under terminfo.");
if (sflag) {
+ int len;
/*
* Figure out what shell we're using. A hack, we look for an
* environmental variable SHELL ending in "csh".
*/
if ((p = getenv("SHELL")) != 0
- && !strcmp(p + strlen(p) - 3, "csh"))
+ && (len = strlen(p)) >= 3
+ && !strcmp(p + len - 3, "csh"))
p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n";
else
p = "TERM=%s;\n";
return EXIT_SUCCESS;
}
-
-/* tset.c ends here */