1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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.80 2009/12/12 19:49:03 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
144 extern char **environ;
147 #define CTRL(x) ((x) & 0x1f)
149 const char *_nc_progname = "tset";
151 static TTY mode, oldmode, original;
153 static bool opt_c; /* set control-chars */
154 static bool opt_w; /* set window-size */
156 static bool can_restore = FALSE;
157 static bool isreset = FALSE; /* invoked as reset */
158 static int terasechar = -1; /* new erase character */
159 static int intrchar = -1; /* new interrupt character */
160 static int tkillchar = -1; /* new kill character */
161 static int tlines, tcolumns; /* window size */
163 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
166 CaselessCmp(const char *a, const char *b)
167 { /* strcasecmp isn't portable */
169 int cmp = LOWERCASE(*a) - LOWERCASE(*b);
174 return LOWERCASE(*a) - LOWERCASE(*b);
181 SET_TTY(STDERR_FILENO, &original);
182 (void) fprintf(stderr, "\n");
184 ExitProgram(EXIT_FAILURE);
189 err(const char *fmt,...)
193 (void) fprintf(stderr, "%s: ", _nc_progname);
194 (void) vfprintf(stderr, fmt, ap);
201 failed(const char *msg)
204 unsigned len = strlen(_nc_progname) + 2;
206 if ((int) len < (int) sizeof(temp) - 12) {
207 strcpy(temp, _nc_progname);
210 strcpy(temp, "tset: ");
212 perror(strncat(temp, msg, sizeof(temp) - strlen(temp) - 2));
224 if ((fp = fopen(file, "r")) == 0)
227 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
228 if (fwrite(buf, sizeof(char), nr, stderr) != nr)
229 failed("write to stderr");
236 return putc(c, stderr);
239 /* Prompt the user for a terminal type. */
241 askuser(const char *dflt)
243 static char answer[256];
246 /* We can get recalled; if so, don't continue uselessly. */
248 if (feof(stdin) || ferror(stdin)) {
249 (void) fprintf(stderr, "\n");
255 (void) fprintf(stderr, "Terminal type? [%s] ", dflt);
257 (void) fprintf(stderr, "Terminal type? ");
258 (void) fflush(stderr);
260 if (fgets(answer, sizeof(answer), stdin) == 0) {
268 if ((p = strchr(answer, '\n')) != 0)
277 /**************************************************************************
279 * Mapping logic begins here
281 **************************************************************************/
283 /* Baud rate conditionals for mapping. */
292 struct map *next; /* Linked list of maps. */
293 const char *porttype; /* Port type, or "" for any. */
294 const char *type; /* Terminal type to select. */
295 int conditional; /* Baud rate conditionals bitmask. */
296 int speed; /* Baud rate to compare against. */
299 static MAP *cur, *maplist;
301 typedef struct speeds {
306 static const SPEEDS speeds[] =
323 /* sgttyb may define up to this point */
366 tbaudrate(char *rate)
371 /* The baudrate number can be preceded by a 'B', which is ignored. */
375 for (sp = speeds; sp->string; ++sp) {
376 if (!CaselessCmp(rate, sp->string)) {
382 err("unknown baud rate %s", rate);
388 * [port-type][test baudrate]:terminal-type
389 * The baud rate tests are: >, <, @, =, !
392 add_mapping(const char *port, char *arg)
400 mapp = typeMalloc(MAP, 1);
401 if (copy == 0 || mapp == 0)
405 cur = maplist = mapp;
411 mapp->porttype = arg;
412 mapp->conditional = 0;
414 arg = strpbrk(arg, "><@=!:");
416 if (arg == 0) { /* [?]term */
417 mapp->type = mapp->porttype;
422 if (arg == mapp->porttype) /* [><@=! baud]:term */
423 termp = mapp->porttype = 0;
427 for (;; ++arg) { /* Optional conditionals. */
430 if (mapp->conditional & GT)
432 mapp->conditional |= LT;
435 if (mapp->conditional & LT)
437 mapp->conditional |= GT;
440 case '=': /* Not documented. */
441 mapp->conditional |= EQ;
444 mapp->conditional |= NOT;
453 if (mapp->conditional)
456 } else { /* Optional baudrate. */
457 arg = strchr(p = arg, ':');
461 mapp->speed = tbaudrate(p);
464 if (arg == (char *) 0) /* Non-optional type. */
469 /* Terminate porttype, if specified. */
473 /* If a NOT conditional, reverse the test. */
474 if (mapp->conditional & NOT)
475 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
477 /* If user specified a port with an option flag, set it. */
480 if (mapp->porttype) {
482 err("illegal -m option format: %s", copy);
484 mapp->porttype = port;
488 (void) printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
489 (void) printf("type: %s\n", mapp->type);
490 (void) printf("conditional: ");
492 if (mapp->conditional & GT) {
496 if (mapp->conditional & EQ) {
497 (void) printf("%sEQ", p);
500 if (mapp->conditional & LT)
501 (void) printf("%sLT", p);
502 (void) printf("\nspeed: %d\n", mapp->speed);
507 * Return the type of terminal to use for a port of type 'type', as specified
508 * by the first applicable mapping in 'map'. If no mappings apply, return
512 mapped(const char *type)
517 for (mapp = maplist; mapp; mapp = mapp->next)
518 if (mapp->porttype == 0 || !strcmp(mapp->porttype, type)) {
519 switch (mapp->conditional) {
520 case 0: /* No test specified. */
524 match = (ospeed == mapp->speed);
527 match = (ospeed >= mapp->speed);
530 match = (ospeed > mapp->speed);
533 match = (ospeed <= mapp->speed);
536 match = (ospeed < mapp->speed);
544 /* No match found; return given type. */
548 /**************************************************************************
552 **************************************************************************/
555 * Figure out what kind of terminal we're dealing with, and then read in
559 get_termcap_entry(char *userarg)
576 /* Try the environment. */
577 if ((ttype = getenv("TERM")) != 0)
580 if ((ttypath = ttyname(STDERR_FILENO)) != 0) {
581 p = _nc_basename(ttypath);
584 * We have the 4.3BSD library call getttynam(3); that means
585 * there's an /etc/ttys to look up device-to-type mappings in.
586 * Try ttyname(3); check for dialup or other mapping.
588 if ((t = getttynam(p))) {
593 if ((fp = fopen("/etc/ttytype", "r")) != 0
594 || (fp = fopen("/etc/ttys", "r")) != 0) {
598 while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) {
599 for (s = buffer, t = d = 0; *s; s++) {
600 if (isspace(UChar(*s)))
604 else if (d == 0 && s != buffer && s[-1] == '\0')
607 if (t != 0 && d != 0 && !strcmp(d, p)) {
615 #endif /* HAVE_GETTTYNAM */
618 /* If still undefined, use "unknown". */
621 map:ttype = mapped(ttype);
624 * If not a path, remove TERMCAP from the environment so we get a
625 * real entry from /etc/termcap. This prevents us from being fooled
626 * by out of date stuff in the environment.
628 found:if ((p = getenv("TERMCAP")) != 0 && !_nc_is_abs_path(p)) {
629 /* 'unsetenv("TERMCAP")' is not portable.
630 * The 'environ' array is better.
633 for (n = 0; environ[n] != 0; n++) {
634 if (!strncmp("TERMCAP=", environ[n], 8)) {
635 while ((environ[n] = environ[n + 1]) != 0) {
644 * ttype now contains a pointer to the type of the terminal.
645 * If the first character is '?', ask the user.
647 if (ttype[0] == '?') {
648 if (ttype[1] != '\0')
649 ttype = askuser(ttype + 1);
653 /* Find the terminfo entry. If it doesn't exist, ask the user. */
654 while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
657 (void) fprintf(stderr, "%s: unknown terminal type %s\n",
658 _nc_progname, ttype);
661 (void) fprintf(stderr,
662 "%s: can't initialize terminal type %s (error %d)\n",
663 _nc_progname, ttype, errret);
666 ttype = askuser(ttype);
669 tgetflag("am"); /* force lib_termcap.o to be linked for 'ospeed' */
674 /**************************************************************************
678 **************************************************************************/
680 /* some BSD systems have these built in, some systems are missing
681 * one or more definitions. The safest solution is to override unless the
682 * commonly-altered ones are defined.
684 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
697 /* control-character defaults */
699 #define CEOF CTRL('D')
702 #define CERASE CTRL('H')
705 #define CINTR 127 /* ^? */
708 #define CKILL CTRL('U')
711 #define CLNEXT CTRL('v')
714 #define CRPRNT CTRL('r')
717 #define CQUIT CTRL('\\')
720 #define CSTART CTRL('Q')
723 #define CSTOP CTRL('S')
726 #define CSUSP CTRL('Z')
729 #if defined(_POSIX_VDISABLE)
730 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
731 && ((val) == _POSIX_VDISABLE)) \
734 #define DISABLED(val) ((int)(val) <= 0)
737 #define CHK(val, dft) (DISABLED(val) ? dft : val)
739 static bool set_tabs(void);
742 * Reset the terminal mode bits to a sensible state. Very useful after
743 * a child program dies in raw mode.
749 tcgetattr(STDERR_FILENO, &mode);
751 stty(STDERR_FILENO, &mode);
755 #if defined(VDISCARD) && defined(CDISCARD)
756 mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
758 mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
759 mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
760 #if defined(VFLUSH) && defined(CFLUSH)
761 mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
763 mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
764 mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
765 #if defined(VLNEXT) && defined(CLNEXT)
766 mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
768 mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
769 #if defined(VREPRINT) && defined(CRPRNT)
770 mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
772 #if defined(VSTART) && defined(CSTART)
773 mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
775 #if defined(VSTOP) && defined(CSTOP)
776 mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
778 #if defined(VSUSP) && defined(CSUSP)
779 mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
781 #if defined(VWERASE) && defined(CWERASE)
782 mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
785 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
794 mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
839 mode.c_oflag |= (OPOST
845 mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
846 mode.c_cflag |= (CS8 | CREAD);
847 mode.c_lflag &= ~(ECHONL | NOFLSH
859 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
869 SET_TTY(STDERR_FILENO, &mode);
873 * Returns a "good" value for the erase character. This is loosely based on
883 && key_backspace != 0
884 && strlen(key_backspace) == 1)
885 result = key_backspace[0];
894 * Update the values of the erase, interrupt, and kill characters in 'mode'.
896 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
897 * characters if they're unset, or if we specify them as options. This differs
898 * from BSD 4.4 tset, which always sets erase.
901 set_control_chars(void)
904 if (DISABLED(mode.c_cc[VERASE]) || terasechar >= 0)
905 mode.c_cc[VERASE] = (terasechar >= 0) ? terasechar : default_erase();
907 if (DISABLED(mode.c_cc[VINTR]) || intrchar >= 0)
908 mode.c_cc[VINTR] = (intrchar >= 0) ? intrchar : CINTR;
910 if (DISABLED(mode.c_cc[VKILL]) || tkillchar >= 0)
911 mode.c_cc[VKILL] = (tkillchar >= 0) ? tkillchar : CKILL;
916 * Set up various conversions in 'mode', including parity, tabs, returns,
917 * echo, and case, according to the termcap entry. If the program we're
918 * running was named with a leading upper-case character, map external
919 * uppercase to internal lowercase.
922 set_conversions(void)
926 * Conversion logic for some *really* ancient terminal glitches,
927 * not supported in terminfo. Left here for succeeding generations
930 if (tgetflag("UC")) {
932 mode.c_iflag |= IUCLC;
933 mode.c_oflag |= OLCUC;
935 } else if (tgetflag("LC")) {
937 mode.c_iflag &= ~IUCLC;
938 mode.c_oflag &= ~OLCUC;
941 mode.c_iflag &= ~(PARMRK | INPCK);
942 mode.c_lflag |= ICANON;
943 if (tgetflag("EP")) {
944 mode.c_cflag |= PARENB;
945 mode.c_cflag &= ~PARODD;
947 if (tgetflag("OP")) {
948 mode.c_cflag |= PARENB;
949 mode.c_cflag |= PARODD;
951 #endif /* __OBSOLETE__ */
955 mode.c_oflag |= ONLCR;
957 mode.c_iflag |= ICRNL;
958 mode.c_lflag |= ECHO;
960 mode.c_oflag |= OXTABS;
963 /* test used to be tgetflag("NL") */
964 if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
965 /* Newline, not linefeed. */
967 mode.c_oflag &= ~ONLCR;
969 mode.c_iflag &= ~ICRNL;
972 if (tgetflag("HD")) /* Half duplex. */
973 mode.c_lflag &= ~ECHO;
974 #endif /* __OBSOLETE__ */
976 /* test used to be tgetflag("pt") */
977 if (has_hardware_tabs) /* Print tabs. */
978 mode.c_oflag &= ~OXTABS;
980 mode.c_lflag |= (ECHOE | ECHOK);
984 /* Output startup string. */
992 if (pad_char != (char *) 0) /* Get/set pad character. */
994 #endif /* OBSOLETE */
997 if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
998 oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
999 SET_TTY(STDERR_FILENO, &oldmode);
1002 settle = set_tabs();
1005 if ((p = reset_1string) != 0) {
1009 if ((p = reset_2string) != 0) {
1013 /* What about rf, rs3, as per terminfo man page? */
1014 /* also might be nice to send rmacs, rmul, rmm */
1015 if ((p = reset_file) != 0
1016 || (p = init_file) != 0) {
1023 (void) putc('\r', stderr);
1024 (void) fflush(stderr);
1025 (void) napms(1000); /* Settle the terminal. */
1030 * Set the hardware tabs on the terminal, using the ct (clear all tabs),
1031 * st (set one tab) and ch (horizontal cursor addressing) capabilities.
1032 * This is done before if and is, so they can patch in case we blow this.
1033 * Return TRUE if we set any tab stops, FALSE if not.
1038 if (set_tab && clear_all_tabs) {
1041 (void) putc('\r', stderr); /* Force to left margin. */
1042 tputs(clear_all_tabs, 0, outc);
1044 for (c = 8; c < tcolumns; c += 8) {
1045 /* Get to the right column. In BSD tset, this
1046 * used to try a bunch of half-clever things
1047 * with cup and hpa, for an average saving of
1048 * somewhat less than two character times per
1049 * tab stop, less than .01 sec at 2400cps. We
1050 * lost all this cruft because it seemed to be
1051 * introducing some odd bugs.
1052 * -----------12345678----------- */
1053 (void) fputs(" ", stderr);
1054 tputs(set_tab, 0, outc);
1062 /**************************************************************************
1066 **************************************************************************/
1069 * Tell the user if a control key has been changed from the default value.
1073 report(const char *name, int which, unsigned def)
1075 unsigned older, newer;
1078 newer = mode.c_cc[which];
1079 older = oldmode.c_cc[which];
1081 if (older == newer && older == def)
1084 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
1086 if (DISABLED(newer))
1087 (void) fprintf(stderr, "undef.\n");
1089 * Check 'delete' before 'backspace', since the key_backspace value
1092 else if (newer == 0177)
1093 (void) fprintf(stderr, "delete.\n");
1094 else if ((p = key_backspace) != 0
1095 && newer == (unsigned char) p[0]
1097 (void) fprintf(stderr, "backspace.\n");
1098 else if (newer < 040) {
1100 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
1102 (void) fprintf(stderr, "%c.\n", UChar(newer));
1107 * Convert the obsolete argument forms into something that getopt can handle.
1108 * This means that -e, -i and -k get default arguments supplied for them.
1111 obsolete(char **argv)
1113 for (; *argv; ++argv) {
1114 char *parm = argv[0];
1116 if (parm[0] == '-' && parm[1] == '\0') {
1117 argv[0] = strdup("-q");
1121 if ((parm[0] != '-')
1122 || (argv[1] && argv[1][0] != '-')
1123 || (parm[1] != 'e' && parm[1] != 'i' && parm[1] != 'k')
1124 || (parm[2] != '\0'))
1126 switch (argv[0][1]) {
1128 argv[0] = strdup("-e^H");
1131 argv[0] = strdup("-i^C");
1134 argv[0] = strdup("-k^U");
1143 static const char *tbl[] =
1147 ," -c set control characters"
1148 ," -e ch erase character"
1149 ," -I no initialization strings"
1150 ," -i ch interrupt character"
1151 ," -k ch kill character"
1152 ," -m mapping map identifier to type"
1153 ," -Q do not output control key settings"
1154 ," -r display term on stderr"
1155 ," -s output TERM set command"
1156 ," -V print curses-version"
1157 ," -w set window-size"
1160 (void) fprintf(stderr, "Usage: %s [options] [terminal]\n", _nc_progname);
1161 for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n)
1162 fprintf(stderr, "%s\n", tbl[n]);
1170 return (char) ((optarg[0] == '^' && optarg[1] != '\0')
1171 ? ((optarg[1] == '?') ? '\177' : CTRL(optarg[1]))
1176 main(int argc, char **argv)
1178 int ch, noinit, noset, quiet, Sflag, sflag, showterm;
1183 noinit = noset = quiet = Sflag = sflag = showterm = 0;
1184 while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:np:qQSrsVw")) != -1) {
1186 case 'c': /* set control-chars */
1189 case 'a': /* OBSOLETE: map identifier to type */
1190 add_mapping("arpanet", optarg);
1192 case 'd': /* OBSOLETE: map identifier to type */
1193 add_mapping("dialup", optarg);
1195 case 'e': /* erase character */
1196 terasechar = arg_to_char();
1198 case 'I': /* no initialization strings */
1201 case 'i': /* interrupt character */
1202 intrchar = arg_to_char();
1204 case 'k': /* kill character */
1205 tkillchar = arg_to_char();
1207 case 'm': /* map identifier to type */
1208 add_mapping(0, optarg);
1210 case 'n': /* OBSOLETE: set new tty driver */
1212 case 'p': /* OBSOLETE: map identifier to type */
1213 add_mapping("plugboard", optarg);
1215 case 'Q': /* don't output control key settings */
1218 case 'q': /* display term only */
1221 case 'r': /* display term on stderr */
1224 case 'S': /* OBSOLETE: output TERM & TERMCAP */
1227 case 's': /* output TERM set command */
1230 case 'V': /* print curses-version */
1231 puts(curses_version());
1232 ExitProgram(EXIT_SUCCESS);
1233 case 'w': /* set window-size */
1242 _nc_progname = _nc_rootname(*argv);
1249 if (!opt_c && !opt_w)
1250 opt_c = opt_w = TRUE;
1252 if (GET_TTY(STDERR_FILENO, &mode) < 0)
1253 failed("standard error");
1255 original = oldmode = mode;
1257 ospeed = (NCURSES_OSPEED) cfgetospeed(&mode);
1259 ospeed = (NCURSES_OSPEED) mode.sg_ospeed;
1262 if (same_program(_nc_progname, PROG_RESET)) {
1267 ttype = get_termcap_entry(*argv);
1276 /* Set window size if not set already */
1277 (void) ioctl(STDERR_FILENO, IOCTL_GET_WINSIZE, &win);
1278 if (WINSIZE_ROWS(win) == 0 &&
1279 WINSIZE_COLS(win) == 0 &&
1280 tlines > 0 && tcolumns > 0) {
1281 WINSIZE_ROWS(win) = tlines;
1282 WINSIZE_COLS(win) = tcolumns;
1283 (void) ioctl(STDERR_FILENO, IOCTL_SET_WINSIZE, &win);
1288 set_control_chars();
1294 /* Set the modes if they've changed. */
1295 if (memcmp(&mode, &oldmode, sizeof(mode))) {
1296 SET_TTY(STDERR_FILENO, &mode);
1301 /* Get the terminal name from the entry. */
1302 ttype = _nc_first_name(cur_term->type.term_names);
1305 (void) printf("%s\n", ttype);
1308 (void) fprintf(stderr, "Terminal type is %s.\n", ttype);
1310 * If erase, kill and interrupt characters could have been
1311 * modified and not -Q, display the changes.
1315 report("Erase", VERASE, CERASE);
1316 report("Kill", VKILL, CKILL);
1317 report("Interrupt", VINTR, CINTR);
1323 err("The -S option is not supported under terminfo.");
1330 * Figure out what shell we're using. A hack, we look for an
1331 * environmental variable SHELL ending in "csh".
1333 if ((var = getenv("SHELL")) != 0
1334 && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3)
1335 && !strcmp(leaf + len - 3, "csh"))
1336 p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n";
1339 (void) printf(p, ttype);
1342 ExitProgram(EXIT_SUCCESS);