1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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 ****************************************************************************/
37 * The initial adaptation from 4.4BSD Lite sources in September 1995 used 686
38 * lines from that version, and made changes/additions for 150 lines. There
39 * was no reformatting, so with/without ignoring whitespace, the amount of
42 * Comparing with current (2009) source, excluding this comment:
43 * a) 209 lines match identically to the 4.4BSD Lite sources, with 771 lines
45 * a) Ignoring whitespace, the current version still uses 516 lines from the
46 * 4.4BSD Lite sources, with 402 lines changed/added.
48 * Raymond's original comment on this follows...
52 * tset.c - terminal initialization utility
54 * This code was mostly swiped from 4.4BSD tset, with some obsolescent
55 * cruft removed and substantial portions rewritten. A Regents of the
56 * University of California copyright applies to some portions of the
57 * code, and is reproduced below:
60 * Copyright (c) 1980, 1991, 1993
61 * The Regents of the University of California. All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. Neither the name of the University nor the names of its contributors
72 * may be used to endorse or promote products derived from this software
73 * without specific prior written permission.
75 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 #define __INTERNAL_CAPS_VISIBLE /* we need to see has_hardware_tabs */
90 #include <progs.priv.h>
97 #if HAVE_GETTTYNAM && HAVE_TTYENT_H
101 char *ttyname(int fd);
105 # if !defined(sun) || !TERMIOS
106 # if HAVE_SYS_IOCTL_H
107 # include <sys/ioctl.h>
113 /* they neglected to define struct winsize in termios.h -- it's only
115 #include <sys/stream.h>
116 #include <sys/ptem.h>
119 #include <dump_entry.h>
120 #include <transform.h>
122 MODULE_ID("$Id: tset.c,v 1.82 2010/05/01 21:42:46 tom Exp $")
125 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
126 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
129 # define IOCTL_GET_WINSIZE TIOCGSIZE
130 # define IOCTL_SET_WINSIZE TIOCSSIZE
131 # define STRUCT_WINSIZE struct ttysize
132 # define WINSIZE_ROWS(n) n.ts_lines
133 # define WINSIZE_COLS(n) n.ts_cols
136 # define IOCTL_GET_WINSIZE TIOCGWINSZ
137 # define IOCTL_SET_WINSIZE TIOCSWINSZ
138 # define STRUCT_WINSIZE struct winsize
139 # define WINSIZE_ROWS(n) n.ws_row
140 # define WINSIZE_COLS(n) n.ws_col
145 extern char **environ;
149 #define CTRL(x) ((x) & 0x1f)
151 const char *_nc_progname = "tset";
153 static TTY mode, oldmode, original;
155 static bool opt_c; /* set control-chars */
156 static bool opt_w; /* set window-size */
158 static bool can_restore = FALSE;
159 static bool isreset = FALSE; /* invoked as reset */
160 static int terasechar = -1; /* new erase character */
161 static int intrchar = -1; /* new interrupt character */
162 static int tkillchar = -1; /* new kill character */
163 static int tlines, tcolumns; /* window size */
165 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
168 CaselessCmp(const char *a, const char *b)
169 { /* strcasecmp isn't portable */
171 int cmp = LOWERCASE(*a) - LOWERCASE(*b);
176 return LOWERCASE(*a) - LOWERCASE(*b);
183 SET_TTY(STDERR_FILENO, &original);
184 (void) fprintf(stderr, "\n");
186 ExitProgram(EXIT_FAILURE);
191 err(const char *fmt,...)
195 (void) fprintf(stderr, "%s: ", _nc_progname);
196 (void) vfprintf(stderr, fmt, ap);
203 failed(const char *msg)
206 unsigned len = strlen(_nc_progname) + 2;
208 if ((int) len < (int) sizeof(temp) - 12) {
209 strcpy(temp, _nc_progname);
212 strcpy(temp, "tset: ");
214 perror(strncat(temp, msg, sizeof(temp) - strlen(temp) - 2));
226 if ((fp = fopen(file, "r")) == 0)
229 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
230 if (fwrite(buf, sizeof(char), nr, stderr) != nr)
231 failed("write to stderr");
238 return putc(c, stderr);
241 /* Prompt the user for a terminal type. */
243 askuser(const char *dflt)
245 static char answer[256];
248 /* We can get recalled; if so, don't continue uselessly. */
250 if (feof(stdin) || ferror(stdin)) {
251 (void) fprintf(stderr, "\n");
257 (void) fprintf(stderr, "Terminal type? [%s] ", dflt);
259 (void) fprintf(stderr, "Terminal type? ");
260 (void) fflush(stderr);
262 if (fgets(answer, sizeof(answer), stdin) == 0) {
270 if ((p = strchr(answer, '\n')) != 0)
279 /**************************************************************************
281 * Mapping logic begins here
283 **************************************************************************/
285 /* Baud rate conditionals for mapping. */
294 struct map *next; /* Linked list of maps. */
295 const char *porttype; /* Port type, or "" for any. */
296 const char *type; /* Terminal type to select. */
297 int conditional; /* Baud rate conditionals bitmask. */
298 int speed; /* Baud rate to compare against. */
301 static MAP *cur, *maplist;
303 typedef struct speeds {
308 static const SPEEDS speeds[] =
325 /* sgttyb may define up to this point */
368 tbaudrate(char *rate)
373 /* The baudrate number can be preceded by a 'B', which is ignored. */
377 for (sp = speeds; sp->string; ++sp) {
378 if (!CaselessCmp(rate, sp->string)) {
384 err("unknown baud rate %s", rate);
390 * [port-type][test baudrate]:terminal-type
391 * The baud rate tests are: >, <, @, =, !
394 add_mapping(const char *port, char *arg)
402 mapp = typeMalloc(MAP, 1);
403 if (copy == 0 || mapp == 0)
411 cur = maplist = mapp;
417 mapp->porttype = arg;
418 mapp->conditional = 0;
420 arg = strpbrk(arg, "><@=!:");
422 if (arg == 0) { /* [?]term */
423 mapp->type = mapp->porttype;
428 if (arg == mapp->porttype) /* [><@=! baud]:term */
429 termp = mapp->porttype = 0;
433 for (;; ++arg) { /* Optional conditionals. */
436 if (mapp->conditional & GT)
438 mapp->conditional |= LT;
441 if (mapp->conditional & LT)
443 mapp->conditional |= GT;
446 case '=': /* Not documented. */
447 mapp->conditional |= EQ;
450 mapp->conditional |= NOT;
459 if (mapp->conditional)
462 } else { /* Optional baudrate. */
463 arg = strchr(p = arg, ':');
467 mapp->speed = tbaudrate(p);
470 if (arg == (char *) 0) /* Non-optional type. */
475 /* Terminate porttype, if specified. */
479 /* If a NOT conditional, reverse the test. */
480 if (mapp->conditional & NOT)
481 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
483 /* If user specified a port with an option flag, set it. */
486 if (mapp->porttype) {
488 err("illegal -m option format: %s", copy);
490 mapp->porttype = port;
494 (void) printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
495 (void) printf("type: %s\n", mapp->type);
496 (void) printf("conditional: ");
498 if (mapp->conditional & GT) {
502 if (mapp->conditional & EQ) {
503 (void) printf("%sEQ", p);
506 if (mapp->conditional & LT)
507 (void) printf("%sLT", p);
508 (void) printf("\nspeed: %d\n", mapp->speed);
513 * Return the type of terminal to use for a port of type 'type', as specified
514 * by the first applicable mapping in 'map'. If no mappings apply, return
518 mapped(const char *type)
523 for (mapp = maplist; mapp; mapp = mapp->next)
524 if (mapp->porttype == 0 || !strcmp(mapp->porttype, type)) {
525 switch (mapp->conditional) {
526 case 0: /* No test specified. */
530 match = (ospeed == mapp->speed);
533 match = (ospeed >= mapp->speed);
536 match = (ospeed > mapp->speed);
539 match = (ospeed <= mapp->speed);
542 match = (ospeed < mapp->speed);
550 /* No match found; return given type. */
554 /**************************************************************************
558 **************************************************************************/
561 * Figure out what kind of terminal we're dealing with, and then read in
565 get_termcap_entry(char *userarg)
582 /* Try the environment. */
583 if ((ttype = getenv("TERM")) != 0)
586 if ((ttypath = ttyname(STDERR_FILENO)) != 0) {
587 p = _nc_basename(ttypath);
590 * We have the 4.3BSD library call getttynam(3); that means
591 * there's an /etc/ttys to look up device-to-type mappings in.
592 * Try ttyname(3); check for dialup or other mapping.
594 if ((t = getttynam(p))) {
599 if ((fp = fopen("/etc/ttytype", "r")) != 0
600 || (fp = fopen("/etc/ttys", "r")) != 0) {
604 while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) {
605 for (s = buffer, t = d = 0; *s; s++) {
606 if (isspace(UChar(*s)))
610 else if (d == 0 && s != buffer && s[-1] == '\0')
613 if (t != 0 && d != 0 && !strcmp(d, p)) {
621 #endif /* HAVE_GETTTYNAM */
624 /* If still undefined, use "unknown". */
627 map:ttype = mapped(ttype);
630 * If not a path, remove TERMCAP from the environment so we get a
631 * real entry from /etc/termcap. This prevents us from being fooled
632 * by out of date stuff in the environment.
634 found:if ((p = getenv("TERMCAP")) != 0 && !_nc_is_abs_path(p)) {
635 /* 'unsetenv("TERMCAP")' is not portable.
636 * The 'environ' array is better.
639 for (n = 0; environ[n] != 0; n++) {
640 if (!strncmp("TERMCAP=", environ[n], 8)) {
641 while ((environ[n] = environ[n + 1]) != 0) {
650 * ttype now contains a pointer to the type of the terminal.
651 * If the first character is '?', ask the user.
653 if (ttype[0] == '?') {
654 if (ttype[1] != '\0')
655 ttype = askuser(ttype + 1);
659 /* Find the terminfo entry. If it doesn't exist, ask the user. */
660 while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
663 (void) fprintf(stderr, "%s: unknown terminal type %s\n",
664 _nc_progname, ttype);
667 (void) fprintf(stderr,
668 "%s: can't initialize terminal type %s (error %d)\n",
669 _nc_progname, ttype, errret);
672 ttype = askuser(ttype);
675 tgetflag("am"); /* force lib_termcap.o to be linked for 'ospeed' */
680 /**************************************************************************
684 **************************************************************************/
686 /* some BSD systems have these built in, some systems are missing
687 * one or more definitions. The safest solution is to override unless the
688 * commonly-altered ones are defined.
690 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
703 /* control-character defaults */
705 #define CEOF CTRL('D')
708 #define CERASE CTRL('H')
711 #define CINTR 127 /* ^? */
714 #define CKILL CTRL('U')
717 #define CLNEXT CTRL('v')
720 #define CRPRNT CTRL('r')
723 #define CQUIT CTRL('\\')
726 #define CSTART CTRL('Q')
729 #define CSTOP CTRL('S')
732 #define CSUSP CTRL('Z')
735 #if defined(_POSIX_VDISABLE)
736 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
737 && ((val) == _POSIX_VDISABLE)) \
740 #define DISABLED(val) ((int)(val) <= 0)
743 #define CHK(val, dft) (DISABLED(val) ? dft : val)
745 static bool set_tabs(void);
748 * Reset the terminal mode bits to a sensible state. Very useful after
749 * a child program dies in raw mode.
755 tcgetattr(STDERR_FILENO, &mode);
757 stty(STDERR_FILENO, &mode);
761 #if defined(VDISCARD) && defined(CDISCARD)
762 mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
764 mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
765 mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
766 #if defined(VFLUSH) && defined(CFLUSH)
767 mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
769 mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
770 mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
771 #if defined(VLNEXT) && defined(CLNEXT)
772 mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
774 mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
775 #if defined(VREPRINT) && defined(CRPRNT)
776 mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
778 #if defined(VSTART) && defined(CSTART)
779 mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
781 #if defined(VSTOP) && defined(CSTOP)
782 mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
784 #if defined(VSUSP) && defined(CSUSP)
785 mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
787 #if defined(VWERASE) && defined(CWERASE)
788 mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
791 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
800 mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
845 mode.c_oflag |= (OPOST
851 mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
852 mode.c_cflag |= (CS8 | CREAD);
853 mode.c_lflag &= ~(ECHONL | NOFLSH
865 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
875 SET_TTY(STDERR_FILENO, &mode);
879 * Returns a "good" value for the erase character. This is loosely based on
889 && key_backspace != 0
890 && strlen(key_backspace) == 1)
891 result = key_backspace[0];
900 * Update the values of the erase, interrupt, and kill characters in 'mode'.
902 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
903 * characters if they're unset, or if we specify them as options. This differs
904 * from BSD 4.4 tset, which always sets erase.
907 set_control_chars(void)
910 if (DISABLED(mode.c_cc[VERASE]) || terasechar >= 0)
911 mode.c_cc[VERASE] = (terasechar >= 0) ? terasechar : default_erase();
913 if (DISABLED(mode.c_cc[VINTR]) || intrchar >= 0)
914 mode.c_cc[VINTR] = (intrchar >= 0) ? intrchar : CINTR;
916 if (DISABLED(mode.c_cc[VKILL]) || tkillchar >= 0)
917 mode.c_cc[VKILL] = (tkillchar >= 0) ? tkillchar : CKILL;
922 * Set up various conversions in 'mode', including parity, tabs, returns,
923 * echo, and case, according to the termcap entry. If the program we're
924 * running was named with a leading upper-case character, map external
925 * uppercase to internal lowercase.
928 set_conversions(void)
932 * Conversion logic for some *really* ancient terminal glitches,
933 * not supported in terminfo. Left here for succeeding generations
936 if (tgetflag("UC")) {
938 mode.c_iflag |= IUCLC;
939 mode.c_oflag |= OLCUC;
941 } else if (tgetflag("LC")) {
943 mode.c_iflag &= ~IUCLC;
944 mode.c_oflag &= ~OLCUC;
947 mode.c_iflag &= ~(PARMRK | INPCK);
948 mode.c_lflag |= ICANON;
949 if (tgetflag("EP")) {
950 mode.c_cflag |= PARENB;
951 mode.c_cflag &= ~PARODD;
953 if (tgetflag("OP")) {
954 mode.c_cflag |= PARENB;
955 mode.c_cflag |= PARODD;
957 #endif /* __OBSOLETE__ */
961 mode.c_oflag |= ONLCR;
963 mode.c_iflag |= ICRNL;
964 mode.c_lflag |= ECHO;
966 mode.c_oflag |= OXTABS;
969 /* test used to be tgetflag("NL") */
970 if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
971 /* Newline, not linefeed. */
973 mode.c_oflag &= ~ONLCR;
975 mode.c_iflag &= ~ICRNL;
978 if (tgetflag("HD")) /* Half duplex. */
979 mode.c_lflag &= ~ECHO;
980 #endif /* __OBSOLETE__ */
982 /* test used to be tgetflag("pt") */
983 if (has_hardware_tabs) /* Print tabs. */
984 mode.c_oflag &= ~OXTABS;
986 mode.c_lflag |= (ECHOE | ECHOK);
990 /* Output startup string. */
998 if (pad_char != (char *) 0) /* Get/set pad character. */
1000 #endif /* OBSOLETE */
1003 if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
1004 oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
1005 SET_TTY(STDERR_FILENO, &oldmode);
1008 settle = set_tabs();
1011 if ((p = reset_1string) != 0) {
1015 if ((p = reset_2string) != 0) {
1019 /* What about rf, rs3, as per terminfo man page? */
1020 /* also might be nice to send rmacs, rmul, rmm */
1021 if ((p = reset_file) != 0
1022 || (p = init_file) != 0) {
1029 (void) putc('\r', stderr);
1030 (void) fflush(stderr);
1031 (void) napms(1000); /* Settle the terminal. */
1036 * Set the hardware tabs on the terminal, using the ct (clear all tabs),
1037 * st (set one tab) and ch (horizontal cursor addressing) capabilities.
1038 * This is done before if and is, so they can patch in case we blow this.
1039 * Return TRUE if we set any tab stops, FALSE if not.
1044 if (set_tab && clear_all_tabs) {
1047 (void) putc('\r', stderr); /* Force to left margin. */
1048 tputs(clear_all_tabs, 0, outc);
1050 for (c = 8; c < tcolumns; c += 8) {
1051 /* Get to the right column. In BSD tset, this
1052 * used to try a bunch of half-clever things
1053 * with cup and hpa, for an average saving of
1054 * somewhat less than two character times per
1055 * tab stop, less than .01 sec at 2400cps. We
1056 * lost all this cruft because it seemed to be
1057 * introducing some odd bugs.
1058 * -----------12345678----------- */
1059 (void) fputs(" ", stderr);
1060 tputs(set_tab, 0, outc);
1068 /**************************************************************************
1072 **************************************************************************/
1075 * Tell the user if a control key has been changed from the default value.
1079 report(const char *name, int which, unsigned def)
1081 unsigned older, newer;
1084 newer = mode.c_cc[which];
1085 older = oldmode.c_cc[which];
1087 if (older == newer && older == def)
1090 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
1092 if (DISABLED(newer))
1093 (void) fprintf(stderr, "undef.\n");
1095 * Check 'delete' before 'backspace', since the key_backspace value
1098 else if (newer == 0177)
1099 (void) fprintf(stderr, "delete.\n");
1100 else if ((p = key_backspace) != 0
1101 && newer == (unsigned char) p[0]
1103 (void) fprintf(stderr, "backspace.\n");
1104 else if (newer < 040) {
1106 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
1108 (void) fprintf(stderr, "%c.\n", UChar(newer));
1113 * Convert the obsolete argument forms into something that getopt can handle.
1114 * This means that -e, -i and -k get default arguments supplied for them.
1117 obsolete(char **argv)
1119 for (; *argv; ++argv) {
1120 char *parm = argv[0];
1122 if (parm[0] == '-' && parm[1] == '\0') {
1123 argv[0] = strdup("-q");
1127 if ((parm[0] != '-')
1128 || (argv[1] && argv[1][0] != '-')
1129 || (parm[1] != 'e' && parm[1] != 'i' && parm[1] != 'k')
1130 || (parm[2] != '\0'))
1132 switch (argv[0][1]) {
1134 argv[0] = strdup("-e^H");
1137 argv[0] = strdup("-i^C");
1140 argv[0] = strdup("-k^U");
1149 static const char *tbl[] =
1153 ," -c set control characters"
1154 ," -e ch erase character"
1155 ," -I no initialization strings"
1156 ," -i ch interrupt character"
1157 ," -k ch kill character"
1158 ," -m mapping map identifier to type"
1159 ," -Q do not output control key settings"
1160 ," -r display term on stderr"
1161 ," -s output TERM set command"
1162 ," -V print curses-version"
1163 ," -w set window-size"
1166 (void) fprintf(stderr, "Usage: %s [options] [terminal]\n", _nc_progname);
1167 for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n)
1168 fprintf(stderr, "%s\n", tbl[n]);
1176 return (char) ((optarg[0] == '^' && optarg[1] != '\0')
1177 ? ((optarg[1] == '?') ? '\177' : CTRL(optarg[1]))
1182 main(int argc, char **argv)
1184 int ch, noinit, noset, quiet, Sflag, sflag, showterm;
1189 noinit = noset = quiet = Sflag = sflag = showterm = 0;
1190 while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:np:qQSrsVw")) != -1) {
1192 case 'c': /* set control-chars */
1195 case 'a': /* OBSOLETE: map identifier to type */
1196 add_mapping("arpanet", optarg);
1198 case 'd': /* OBSOLETE: map identifier to type */
1199 add_mapping("dialup", optarg);
1201 case 'e': /* erase character */
1202 terasechar = arg_to_char();
1204 case 'I': /* no initialization strings */
1207 case 'i': /* interrupt character */
1208 intrchar = arg_to_char();
1210 case 'k': /* kill character */
1211 tkillchar = arg_to_char();
1213 case 'm': /* map identifier to type */
1214 add_mapping(0, optarg);
1216 case 'n': /* OBSOLETE: set new tty driver */
1218 case 'p': /* OBSOLETE: map identifier to type */
1219 add_mapping("plugboard", optarg);
1221 case 'Q': /* don't output control key settings */
1224 case 'q': /* display term only */
1227 case 'r': /* display term on stderr */
1230 case 'S': /* OBSOLETE: output TERM & TERMCAP */
1233 case 's': /* output TERM set command */
1236 case 'V': /* print curses-version */
1237 puts(curses_version());
1238 ExitProgram(EXIT_SUCCESS);
1239 case 'w': /* set window-size */
1248 _nc_progname = _nc_rootname(*argv);
1255 if (!opt_c && !opt_w)
1256 opt_c = opt_w = TRUE;
1258 if (GET_TTY(STDERR_FILENO, &mode) < 0)
1259 failed("standard error");
1261 original = oldmode = mode;
1263 ospeed = (NCURSES_OSPEED) cfgetospeed(&mode);
1265 ospeed = (NCURSES_OSPEED) mode.sg_ospeed;
1268 if (same_program(_nc_progname, PROG_RESET)) {
1273 (void) get_termcap_entry(*argv);
1282 /* Set window size if not set already */
1283 (void) ioctl(STDERR_FILENO, IOCTL_GET_WINSIZE, &win);
1284 if (WINSIZE_ROWS(win) == 0 &&
1285 WINSIZE_COLS(win) == 0 &&
1286 tlines > 0 && tcolumns > 0) {
1287 WINSIZE_ROWS(win) = tlines;
1288 WINSIZE_COLS(win) = tcolumns;
1289 (void) ioctl(STDERR_FILENO, IOCTL_SET_WINSIZE, &win);
1294 set_control_chars();
1300 /* Set the modes if they've changed. */
1301 if (memcmp(&mode, &oldmode, sizeof(mode))) {
1302 SET_TTY(STDERR_FILENO, &mode);
1307 /* Get the terminal name from the entry. */
1308 ttype = _nc_first_name(cur_term->type.term_names);
1311 (void) printf("%s\n", ttype);
1314 (void) fprintf(stderr, "Terminal type is %s.\n", ttype);
1316 * If erase, kill and interrupt characters could have been
1317 * modified and not -Q, display the changes.
1321 report("Erase", VERASE, CERASE);
1322 report("Kill", VKILL, CKILL);
1323 report("Interrupt", VINTR, CINTR);
1329 err("The -S option is not supported under terminfo.");
1336 * Figure out what shell we're using. A hack, we look for an
1337 * environmental variable SHELL ending in "csh".
1339 if ((var = getenv("SHELL")) != 0
1340 && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3)
1341 && !strcmp(leaf + len - 3, "csh"))
1342 p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n";
1345 (void) printf(p, ttype);
1348 ExitProgram(EXIT_SUCCESS);