1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * tset.c - terminal initialization utility
38 * This code was mostly swiped from 4.4BSD tset, with some obsolescent
39 * cruft removed and substantial portions rewritten. A Regents of the
40 * University of California copyright applies to some portions of the
41 * code, and is reproduced below:
44 * Copyright (c) 1980, 1991, 1993
45 * The Regents of the University of California. All rights reserved.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 #define __INTERNAL_CAPS_VISIBLE /* we need to see has_hardware_tabs */
78 #include <progs.priv.h>
85 #if HAVE_GETTTYNAM && HAVE_TTYENT_H
89 char *ttyname(int fd);
93 # if !defined(sun) || !TERMIOS
95 # include <sys/ioctl.h>
101 /* they neglected to define struct winsize in termios.h -- it's only
103 #include <sys/stream.h>
104 #include <sys/ptem.h>
107 #include <dump_entry.h>
108 #include <transform.h>
110 MODULE_ID("$Id: tset.c,v 1.76 2008/10/11 19:26:19 tom Exp $")
113 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
114 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
117 # define IOCTL_GET_WINSIZE TIOCGSIZE
118 # define IOCTL_SET_WINSIZE TIOCSSIZE
119 # define STRUCT_WINSIZE struct ttysize
120 # define WINSIZE_ROWS(n) n.ts_lines
121 # define WINSIZE_COLS(n) n.ts_cols
124 # define IOCTL_GET_WINSIZE TIOCGWINSZ
125 # define IOCTL_SET_WINSIZE TIOCSWINSZ
126 # define STRUCT_WINSIZE struct winsize
127 # define WINSIZE_ROWS(n) n.ws_row
128 # define WINSIZE_COLS(n) n.ws_col
132 extern char **environ;
135 #define CTRL(x) ((x) & 0x1f)
137 const char *_nc_progname = "tset";
139 static TTY mode, oldmode, original;
141 static bool opt_c; /* set control-chars */
142 static bool opt_w; /* set window-size */
144 static bool can_restore = FALSE;
145 static bool isreset = FALSE; /* invoked as reset */
146 static int terasechar = -1; /* new erase character */
147 static int intrchar = -1; /* new interrupt character */
148 static int tkillchar = -1; /* new kill character */
149 static int tlines, tcolumns; /* window size */
151 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
154 CaselessCmp(const char *a, const char *b)
155 { /* strcasecmp isn't portable */
157 int cmp = LOWERCASE(*a) - LOWERCASE(*b);
162 return LOWERCASE(*a) - LOWERCASE(*b);
169 SET_TTY(STDERR_FILENO, &original);
170 (void) fprintf(stderr, "\n");
172 ExitProgram(EXIT_FAILURE);
177 err(const char *fmt,...)
181 (void) fprintf(stderr, "%s: ", _nc_progname);
182 (void) vfprintf(stderr, fmt, ap);
189 failed(const char *msg)
192 unsigned len = strlen(_nc_progname) + 2;
194 if ((int) len < (int) sizeof(temp) - 12) {
195 strcpy(temp, _nc_progname);
198 strcpy(temp, "tset: ");
200 perror(strncat(temp, msg, sizeof(temp) - strlen(temp) - 2));
212 if ((fp = fopen(file, "r")) == 0)
215 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
216 if (fwrite(buf, sizeof(char), nr, stderr) != nr)
217 failed("write to stderr");
224 return putc(c, stderr);
227 /* Prompt the user for a terminal type. */
229 askuser(const char *dflt)
231 static char answer[256];
234 /* We can get recalled; if so, don't continue uselessly. */
236 if (feof(stdin) || ferror(stdin)) {
237 (void) fprintf(stderr, "\n");
243 (void) fprintf(stderr, "Terminal type? [%s] ", dflt);
245 (void) fprintf(stderr, "Terminal type? ");
246 (void) fflush(stderr);
248 if (fgets(answer, sizeof(answer), stdin) == 0) {
256 if ((p = strchr(answer, '\n')) != 0)
265 /**************************************************************************
267 * Mapping logic begins here
269 **************************************************************************/
271 /* Baud rate conditionals for mapping. */
280 struct map *next; /* Linked list of maps. */
281 const char *porttype; /* Port type, or "" for any. */
282 const char *type; /* Terminal type to select. */
283 int conditional; /* Baud rate conditionals bitmask. */
284 int speed; /* Baud rate to compare against. */
287 static MAP *cur, *maplist;
289 typedef struct speeds {
294 static const SPEEDS speeds[] =
311 /* sgttyb may define up to this point */
354 tbaudrate(char *rate)
359 /* The baudrate number can be preceded by a 'B', which is ignored. */
363 for (sp = speeds; sp->string; ++sp) {
364 if (!CaselessCmp(rate, sp->string)) {
370 err("unknown baud rate %s", rate);
376 * [port-type][test baudrate]:terminal-type
377 * The baud rate tests are: >, <, @, =, !
380 add_mapping(const char *port, char *arg)
388 mapp = (MAP *) malloc(sizeof(MAP));
389 if (copy == 0 || mapp == 0)
393 cur = maplist = mapp;
399 mapp->porttype = arg;
400 mapp->conditional = 0;
402 arg = strpbrk(arg, "><@=!:");
404 if (arg == 0) { /* [?]term */
405 mapp->type = mapp->porttype;
410 if (arg == mapp->porttype) /* [><@=! baud]:term */
411 termp = mapp->porttype = 0;
415 for (;; ++arg) { /* Optional conditionals. */
418 if (mapp->conditional & GT)
420 mapp->conditional |= LT;
423 if (mapp->conditional & LT)
425 mapp->conditional |= GT;
428 case '=': /* Not documented. */
429 mapp->conditional |= EQ;
432 mapp->conditional |= NOT;
441 if (mapp->conditional)
444 } else { /* Optional baudrate. */
445 arg = strchr(p = arg, ':');
449 mapp->speed = tbaudrate(p);
452 if (arg == (char *) 0) /* Non-optional type. */
457 /* Terminate porttype, if specified. */
461 /* If a NOT conditional, reverse the test. */
462 if (mapp->conditional & NOT)
463 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
465 /* If user specified a port with an option flag, set it. */
468 if (mapp->porttype) {
470 err("illegal -m option format: %s", copy);
472 mapp->porttype = port;
476 (void) printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
477 (void) printf("type: %s\n", mapp->type);
478 (void) printf("conditional: ");
480 if (mapp->conditional & GT) {
484 if (mapp->conditional & EQ) {
485 (void) printf("%sEQ", p);
488 if (mapp->conditional & LT)
489 (void) printf("%sLT", p);
490 (void) printf("\nspeed: %d\n", mapp->speed);
495 * Return the type of terminal to use for a port of type 'type', as specified
496 * by the first applicable mapping in 'map'. If no mappings apply, return
500 mapped(const char *type)
505 for (mapp = maplist; mapp; mapp = mapp->next)
506 if (mapp->porttype == 0 || !strcmp(mapp->porttype, type)) {
507 switch (mapp->conditional) {
508 case 0: /* No test specified. */
512 match = (ospeed == mapp->speed);
515 match = (ospeed >= mapp->speed);
518 match = (ospeed > mapp->speed);
521 match = (ospeed <= mapp->speed);
524 match = (ospeed < mapp->speed);
532 /* No match found; return given type. */
536 /**************************************************************************
540 **************************************************************************/
543 * Figure out what kind of terminal we're dealing with, and then read in
547 get_termcap_entry(char *userarg)
564 /* Try the environment. */
565 if ((ttype = getenv("TERM")) != 0)
568 if ((ttypath = ttyname(STDERR_FILENO)) != 0) {
569 p = _nc_basename(ttypath);
572 * We have the 4.3BSD library call getttynam(3); that means
573 * there's an /etc/ttys to look up device-to-type mappings in.
574 * Try ttyname(3); check for dialup or other mapping.
576 if ((t = getttynam(p))) {
581 if ((fp = fopen("/etc/ttytype", "r")) != 0
582 || (fp = fopen("/etc/ttys", "r")) != 0) {
586 while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) {
587 for (s = buffer, t = d = 0; *s; s++) {
588 if (isspace(UChar(*s)))
592 else if (d == 0 && s != buffer && s[-1] == '\0')
595 if (t != 0 && d != 0 && !strcmp(d, p)) {
603 #endif /* HAVE_GETTTYNAM */
606 /* If still undefined, use "unknown". */
609 map:ttype = mapped(ttype);
612 * If not a path, remove TERMCAP from the environment so we get a
613 * real entry from /etc/termcap. This prevents us from being fooled
614 * by out of date stuff in the environment.
616 found:if ((p = getenv("TERMCAP")) != 0 && !_nc_is_abs_path(p)) {
617 /* 'unsetenv("TERMCAP")' is not portable.
618 * The 'environ' array is better.
621 for (n = 0; environ[n] != 0; n++) {
622 if (!strncmp("TERMCAP=", environ[n], 8)) {
623 while ((environ[n] = environ[n + 1]) != 0) {
632 * ttype now contains a pointer to the type of the terminal.
633 * If the first character is '?', ask the user.
635 if (ttype[0] == '?') {
636 if (ttype[1] != '\0')
637 ttype = askuser(ttype + 1);
641 /* Find the terminfo entry. If it doesn't exist, ask the user. */
642 while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
645 (void) fprintf(stderr, "%s: unknown terminal type %s\n",
646 _nc_progname, ttype);
649 (void) fprintf(stderr,
650 "%s: can't initialize terminal type %s (error %d)\n",
651 _nc_progname, ttype, errret);
654 ttype = askuser(ttype);
657 tgetflag("am"); /* force lib_termcap.o to be linked for 'ospeed' */
662 /**************************************************************************
666 **************************************************************************/
668 /* some BSD systems have these built in, some systems are missing
669 * one or more definitions. The safest solution is to override unless the
670 * commonly-altered ones are defined.
672 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
685 /* control-character defaults */
687 #define CEOF CTRL('D')
690 #define CERASE CTRL('H')
693 #define CINTR 127 /* ^? */
696 #define CKILL CTRL('U')
699 #define CLNEXT CTRL('v')
702 #define CRPRNT CTRL('r')
705 #define CQUIT CTRL('\\')
708 #define CSTART CTRL('Q')
711 #define CSTOP CTRL('S')
714 #define CSUSP CTRL('Z')
717 #if defined(_POSIX_VDISABLE)
718 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
719 && ((val) == _POSIX_VDISABLE)) \
722 #define DISABLED(val) ((int)(val) <= 0)
725 #define CHK(val, dft) (DISABLED(val) ? dft : val)
727 static bool set_tabs(void);
730 * Reset the terminal mode bits to a sensible state. Very useful after
731 * a child program dies in raw mode.
737 tcgetattr(STDERR_FILENO, &mode);
739 stty(STDERR_FILENO, &mode);
743 #if defined(VDISCARD) && defined(CDISCARD)
744 mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
746 mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
747 mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
748 #if defined(VFLUSH) && defined(CFLUSH)
749 mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
751 mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
752 mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
753 #if defined(VLNEXT) && defined(CLNEXT)
754 mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
756 mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
757 #if defined(VREPRINT) && defined(CRPRNT)
758 mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
760 #if defined(VSTART) && defined(CSTART)
761 mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
763 #if defined(VSTOP) && defined(CSTOP)
764 mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
766 #if defined(VSUSP) && defined(CSUSP)
767 mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
769 #if defined(VWERASE) && defined(CWERASE)
770 mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
773 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
782 mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
827 mode.c_oflag |= (OPOST
833 mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
834 mode.c_cflag |= (CS8 | CREAD);
835 mode.c_lflag &= ~(ECHONL | NOFLSH
847 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
857 SET_TTY(STDERR_FILENO, &mode);
861 * Returns a "good" value for the erase character. This is loosely based on
871 && key_backspace != 0
872 && strlen(key_backspace) == 1)
873 result = key_backspace[0];
882 * Update the values of the erase, interrupt, and kill characters in 'mode'.
884 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
885 * characters if they're unset, or if we specify them as options. This differs
886 * from BSD 4.4 tset, which always sets erase.
889 set_control_chars(void)
892 if (DISABLED(mode.c_cc[VERASE]) || terasechar >= 0)
893 mode.c_cc[VERASE] = (terasechar >= 0) ? terasechar : default_erase();
895 if (DISABLED(mode.c_cc[VINTR]) || intrchar >= 0)
896 mode.c_cc[VINTR] = (intrchar >= 0) ? intrchar : CINTR;
898 if (DISABLED(mode.c_cc[VKILL]) || tkillchar >= 0)
899 mode.c_cc[VKILL] = (tkillchar >= 0) ? tkillchar : CKILL;
904 * Set up various conversions in 'mode', including parity, tabs, returns,
905 * echo, and case, according to the termcap entry. If the program we're
906 * running was named with a leading upper-case character, map external
907 * uppercase to internal lowercase.
910 set_conversions(void)
914 * Conversion logic for some *really* ancient terminal glitches,
915 * not supported in terminfo. Left here for succeeding generations
918 if (tgetflag("UC")) {
920 mode.c_iflag |= IUCLC;
921 mode.c_oflag |= OLCUC;
923 } else if (tgetflag("LC")) {
925 mode.c_iflag &= ~IUCLC;
926 mode.c_oflag &= ~OLCUC;
929 mode.c_iflag &= ~(PARMRK | INPCK);
930 mode.c_lflag |= ICANON;
931 if (tgetflag("EP")) {
932 mode.c_cflag |= PARENB;
933 mode.c_cflag &= ~PARODD;
935 if (tgetflag("OP")) {
936 mode.c_cflag |= PARENB;
937 mode.c_cflag |= PARODD;
939 #endif /* __OBSOLETE__ */
943 mode.c_oflag |= ONLCR;
945 mode.c_iflag |= ICRNL;
946 mode.c_lflag |= ECHO;
948 mode.c_oflag |= OXTABS;
951 /* test used to be tgetflag("NL") */
952 if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
953 /* Newline, not linefeed. */
955 mode.c_oflag &= ~ONLCR;
957 mode.c_iflag &= ~ICRNL;
960 if (tgetflag("HD")) /* Half duplex. */
961 mode.c_lflag &= ~ECHO;
962 #endif /* __OBSOLETE__ */
964 /* test used to be tgetflag("pt") */
965 if (has_hardware_tabs) /* Print tabs. */
966 mode.c_oflag &= ~OXTABS;
968 mode.c_lflag |= (ECHOE | ECHOK);
972 /* Output startup string. */
980 if (pad_char != (char *) 0) /* Get/set pad character. */
982 #endif /* OBSOLETE */
985 if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
986 oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
987 SET_TTY(STDERR_FILENO, &oldmode);
993 if ((p = reset_1string) != 0) {
997 if ((p = reset_2string) != 0) {
1001 /* What about rf, rs3, as per terminfo man page? */
1002 /* also might be nice to send rmacs, rmul, rmm */
1003 if ((p = reset_file) != 0
1004 || (p = init_file) != 0) {
1011 (void) putc('\r', stderr);
1012 (void) fflush(stderr);
1013 (void) napms(1000); /* Settle the terminal. */
1018 * Set the hardware tabs on the terminal, using the ct (clear all tabs),
1019 * st (set one tab) and ch (horizontal cursor addressing) capabilities.
1020 * This is done before if and is, so they can patch in case we blow this.
1021 * Return TRUE if we set any tab stops, FALSE if not.
1026 if (set_tab && clear_all_tabs) {
1029 (void) putc('\r', stderr); /* Force to left margin. */
1030 tputs(clear_all_tabs, 0, outc);
1032 for (c = 8; c < tcolumns; c += 8) {
1033 /* Get to the right column. In BSD tset, this
1034 * used to try a bunch of half-clever things
1035 * with cup and hpa, for an average saving of
1036 * somewhat less than two character times per
1037 * tab stop, less than .01 sec at 2400cps. We
1038 * lost all this cruft because it seemed to be
1039 * introducing some odd bugs.
1040 * -----------12345678----------- */
1041 (void) fputs(" ", stderr);
1042 tputs(set_tab, 0, outc);
1050 /**************************************************************************
1054 **************************************************************************/
1057 * Tell the user if a control key has been changed from the default value.
1061 report(const char *name, int which, unsigned def)
1063 unsigned older, newer;
1066 newer = mode.c_cc[which];
1067 older = oldmode.c_cc[which];
1069 if (older == newer && older == def)
1072 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
1074 if (DISABLED(newer))
1075 (void) fprintf(stderr, "undef.\n");
1077 * Check 'delete' before 'backspace', since the key_backspace value
1080 else if (newer == 0177)
1081 (void) fprintf(stderr, "delete.\n");
1082 else if ((p = key_backspace) != 0
1083 && newer == (unsigned char) p[0]
1085 (void) fprintf(stderr, "backspace.\n");
1086 else if (newer < 040) {
1088 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
1090 (void) fprintf(stderr, "%c.\n", UChar(newer));
1095 * Convert the obsolete argument forms into something that getopt can handle.
1096 * This means that -e, -i and -k get default arguments supplied for them.
1099 obsolete(char **argv)
1101 for (; *argv; ++argv) {
1102 char *parm = argv[0];
1104 if (parm[0] == '-' && parm[1] == '\0') {
1105 argv[0] = strdup("-q");
1109 if ((parm[0] != '-')
1110 || (argv[1] && argv[1][0] != '-')
1111 || (parm[1] != 'e' && parm[1] != 'i' && parm[1] != 'k')
1112 || (parm[2] != '\0'))
1114 switch (argv[0][1]) {
1116 argv[0] = strdup("-e^H");
1119 argv[0] = strdup("-i^C");
1122 argv[0] = strdup("-k^U");
1131 static const char *tbl[] =
1135 ," -c set control characters"
1136 ," -e ch erase character"
1137 ," -I no initialization strings"
1138 ," -i ch interrupt character"
1139 ," -k ch kill character"
1140 ," -m mapping map identifier to type"
1141 ," -Q do not output control key settings"
1142 ," -r display term on stderr"
1143 ," -s output TERM set command"
1144 ," -V print curses-version"
1145 ," -w set window-size"
1148 (void) fprintf(stderr, "Usage: %s [options] [terminal]\n", _nc_progname);
1149 for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n)
1150 fprintf(stderr, "%s\n", tbl[n]);
1158 return (char) ((optarg[0] == '^' && optarg[1] != '\0')
1159 ? ((optarg[1] == '?') ? '\177' : CTRL(optarg[1]))
1164 main(int argc, char **argv)
1166 int ch, noinit, noset, quiet, Sflag, sflag, showterm;
1171 noinit = noset = quiet = Sflag = sflag = showterm = 0;
1172 while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:np:qQSrsVw")) != -1) {
1174 case 'c': /* set control-chars */
1177 case 'a': /* OBSOLETE: map identifier to type */
1178 add_mapping("arpanet", optarg);
1180 case 'd': /* OBSOLETE: map identifier to type */
1181 add_mapping("dialup", optarg);
1183 case 'e': /* erase character */
1184 terasechar = arg_to_char();
1186 case 'I': /* no initialization strings */
1189 case 'i': /* interrupt character */
1190 intrchar = arg_to_char();
1192 case 'k': /* kill character */
1193 tkillchar = arg_to_char();
1195 case 'm': /* map identifier to type */
1196 add_mapping(0, optarg);
1198 case 'n': /* OBSOLETE: set new tty driver */
1200 case 'p': /* OBSOLETE: map identifier to type */
1201 add_mapping("plugboard", optarg);
1203 case 'Q': /* don't output control key settings */
1206 case 'q': /* display term only */
1209 case 'r': /* display term on stderr */
1212 case 'S': /* OBSOLETE: output TERM & TERMCAP */
1215 case 's': /* output TERM set command */
1218 case 'V': /* print curses-version */
1219 puts(curses_version());
1220 ExitProgram(EXIT_SUCCESS);
1221 case 'w': /* set window-size */
1230 _nc_progname = _nc_rootname(*argv);
1237 if (!opt_c && !opt_w)
1238 opt_c = opt_w = TRUE;
1240 if (GET_TTY(STDERR_FILENO, &mode) < 0)
1241 failed("standard error");
1243 original = oldmode = mode;
1245 ospeed = (NCURSES_OSPEED) cfgetospeed(&mode);
1247 ospeed = (NCURSES_OSPEED) mode.sg_ospeed;
1250 if (!strcmp(_nc_progname, PROG_RESET)) {
1255 ttype = get_termcap_entry(*argv);
1264 /* Set window size if not set already */
1265 (void) ioctl(STDERR_FILENO, IOCTL_GET_WINSIZE, &win);
1266 if (WINSIZE_ROWS(win) == 0 &&
1267 WINSIZE_COLS(win) == 0 &&
1268 tlines > 0 && tcolumns > 0) {
1269 WINSIZE_ROWS(win) = tlines;
1270 WINSIZE_COLS(win) = tcolumns;
1271 (void) ioctl(STDERR_FILENO, IOCTL_SET_WINSIZE, &win);
1276 set_control_chars();
1282 /* Set the modes if they've changed. */
1283 if (memcmp(&mode, &oldmode, sizeof(mode))) {
1284 SET_TTY(STDERR_FILENO, &mode);
1289 /* Get the terminal name from the entry. */
1290 ttype = _nc_first_name(cur_term->type.term_names);
1293 (void) printf("%s\n", ttype);
1296 (void) fprintf(stderr, "Terminal type is %s.\n", ttype);
1298 * If erase, kill and interrupt characters could have been
1299 * modified and not -Q, display the changes.
1303 report("Erase", VERASE, CERASE);
1304 report("Kill", VKILL, CKILL);
1305 report("Interrupt", VINTR, CINTR);
1311 err("The -S option is not supported under terminfo.");
1318 * Figure out what shell we're using. A hack, we look for an
1319 * environmental variable SHELL ending in "csh".
1321 if ((var = getenv("SHELL")) != 0
1322 && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3)
1323 && !strcmp(leaf + len - 3, "csh"))
1324 p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n";
1327 (void) printf(p, ttype);
1330 ExitProgram(EXIT_SUCCESS);