Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / contrib / ncurses-5.4 / progs / tset.c
blobf67931f0506e31766ea6ec3e707e67282eaa5746
1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc. *
3 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * tset.c - terminal initialization utility
37 * This code was mostly swiped from 4.4BSD tset, with some obsolescent
38 * cruft removed and substantial portions rewritten. A Regents of the
39 * University of California copyright applies to some portions of the
40 * code, and is reproduced below:
42 /*-
43 * Copyright (c) 1980, 1991, 1993
44 * The Regents of the University of California. All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
75 #define __INTERNAL_CAPS_VISIBLE /* we need to see has_hardware_tabs */
76 #include <progs.priv.h>
78 #include <errno.h>
79 #include <stdio.h>
80 #include <termcap.h>
81 #include <fcntl.h>
83 #if HAVE_GETTTYNAM && HAVE_TTYENT_H
84 #include <ttyent.h>
85 #endif
86 #ifdef NeXT
87 char *ttyname(int fd);
88 #endif
90 /* this is just to stifle a missing-prototype warning */
91 #ifdef linux
92 # include <sys/ioctl.h>
93 #endif
95 #if NEED_PTEM_H
96 /* they neglected to define struct winsize in termios.h -- it's only
97 in termio.h */
98 #include <sys/stream.h>
99 #include <sys/ptem.h>
100 #endif
102 #include <curses.h> /* for bool typedef */
103 #include <dump_entry.h>
104 #include <transform.h>
106 MODULE_ID("$Id: tset.c,v 0.56 2003/12/06 17:21:01 tom Exp $")
108 extern char **environ;
110 #undef CTRL
111 #define CTRL(x) ((x) & 0x1f)
113 const char *_nc_progname = "tset";
115 static TTY mode, oldmode, original;
117 static bool can_restore = FALSE;
118 static bool isreset = FALSE; /* invoked as reset */
119 static int terasechar = -1; /* new erase character */
120 static int intrchar = -1; /* new interrupt character */
121 static int tkillchar = -1; /* new kill character */
122 static int tlines, tcolumns; /* window size */
124 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
126 static int
127 CaselessCmp(const char *a, const char *b)
128 { /* strcasecmp isn't portable */
129 while (*a && *b) {
130 int cmp = LOWERCASE(*a) - LOWERCASE(*b);
131 if (cmp != 0)
132 break;
133 a++, b++;
135 return LOWERCASE(*a) - LOWERCASE(*b);
138 static void
139 exit_error(void)
141 if (can_restore)
142 SET_TTY(STDERR_FILENO, &original);
143 (void) fprintf(stderr, "\n");
144 fflush(stderr);
145 ExitProgram(EXIT_FAILURE);
146 /* NOTREACHED */
149 static void
150 err(const char *fmt,...)
152 va_list ap;
153 va_start(ap, fmt);
154 (void) fprintf(stderr, "tset: ");
155 (void) vfprintf(stderr, fmt, ap);
156 va_end(ap);
157 exit_error();
158 /* NOTREACHED */
161 static void
162 failed(const char *msg)
164 char temp[BUFSIZ];
165 perror(strncat(strcpy(temp, "tset: "), msg, sizeof(temp) - 10));
166 exit_error();
167 /* NOTREACHED */
170 static void
171 cat(char *file)
173 FILE *fp;
174 size_t nr;
175 char buf[BUFSIZ];
177 if ((fp = fopen(file, "r")) == 0)
178 failed(file);
180 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
181 if (fwrite(buf, sizeof(char), nr, stderr) != nr)
182 failed("write to stderr");
183 fclose(fp);
186 static int
187 outc(int c)
189 return putc(c, stderr);
192 /* Prompt the user for a terminal type. */
193 static const char *
194 askuser(const char *dflt)
196 static char answer[256];
197 char *p;
199 /* We can get recalled; if so, don't continue uselessly. */
200 clearerr(stdin);
201 if (feof(stdin) || ferror(stdin)) {
202 (void) fprintf(stderr, "\n");
203 exit_error();
204 /* NOTREACHED */
206 for (;;) {
207 if (dflt)
208 (void) fprintf(stderr, "Terminal type? [%s] ", dflt);
209 else
210 (void) fprintf(stderr, "Terminal type? ");
211 (void) fflush(stderr);
213 if (fgets(answer, sizeof(answer), stdin) == 0) {
214 if (dflt == 0) {
215 exit_error();
216 /* NOTREACHED */
218 return (dflt);
221 if ((p = strchr(answer, '\n')) != 0)
222 *p = '\0';
223 if (answer[0])
224 return (answer);
225 if (dflt != 0)
226 return (dflt);
230 /**************************************************************************
232 * Mapping logic begins here
234 **************************************************************************/
236 /* Baud rate conditionals for mapping. */
237 #define GT 0x01
238 #define EQ 0x02
239 #define LT 0x04
240 #define NOT 0x08
241 #define GE (GT | EQ)
242 #define LE (LT | EQ)
244 typedef struct map {
245 struct map *next; /* Linked list of maps. */
246 const char *porttype; /* Port type, or "" for any. */
247 const char *type; /* Terminal type to select. */
248 int conditional; /* Baud rate conditionals bitmask. */
249 int speed; /* Baud rate to compare against. */
250 } MAP;
252 static MAP *cur, *maplist;
254 typedef struct speeds {
255 const char *string;
256 int speed;
257 } SPEEDS;
259 static const SPEEDS speeds[] =
261 {"0", B0},
262 {"50", B50},
263 {"75", B75},
264 {"110", B110},
265 {"134", B134},
266 {"134.5", B134},
267 {"150", B150},
268 {"200", B200},
269 {"300", B300},
270 {"600", B600},
271 {"1200", B1200},
272 {"1800", B1800},
273 {"2400", B2400},
274 {"4800", B4800},
275 {"9600", B9600},
276 /* sgttyb may define up to this point */
277 #ifdef B19200
278 {"19200", B19200},
279 #endif
280 #ifdef B38400
281 {"38400", B38400},
282 #endif
283 #ifdef B19200
284 {"19200", B19200},
285 #endif
286 #ifdef B38400
287 {"38400", B38400},
288 #endif
289 #ifdef B19200
290 {"19200", B19200},
291 #else
292 #ifdef EXTA
293 {"19200", EXTA},
294 #endif
295 #endif
296 #ifdef B38400
297 {"38400", B38400},
298 #else
299 #ifdef EXTB
300 {"38400", EXTB},
301 #endif
302 #endif
303 #ifdef B57600
304 {"57600", B57600},
305 #endif
306 #ifdef B115200
307 {"115200", B115200},
308 #endif
309 #ifdef B230400
310 {"230400", B230400},
311 #endif
312 #ifdef B460800
313 {"460800", B460800},
314 #endif
315 {(char *) 0, 0}
318 static int
319 tbaudrate(char *rate)
321 const SPEEDS *sp;
322 int found = FALSE;
324 /* The baudrate number can be preceded by a 'B', which is ignored. */
325 if (*rate == 'B')
326 ++rate;
328 for (sp = speeds; sp->string; ++sp) {
329 if (!CaselessCmp(rate, sp->string)) {
330 found = TRUE;
331 break;
334 if (!found)
335 err("unknown baud rate %s", rate);
336 return (sp->speed);
340 * Syntax for -m:
341 * [port-type][test baudrate]:terminal-type
342 * The baud rate tests are: >, <, @, =, !
344 static void
345 add_mapping(const char *port, char *arg)
347 MAP *mapp;
348 char *copy, *p;
349 const char *termp;
350 char *base = 0;
352 copy = strdup(arg);
353 mapp = (MAP *) malloc(sizeof(MAP));
354 if (copy == 0 || mapp == 0)
355 failed("malloc");
356 mapp->next = 0;
357 if (maplist == 0)
358 cur = maplist = mapp;
359 else {
360 cur->next = mapp;
361 cur = mapp;
364 mapp->porttype = arg;
365 mapp->conditional = 0;
367 arg = strpbrk(arg, "><@=!:");
369 if (arg == 0) { /* [?]term */
370 mapp->type = mapp->porttype;
371 mapp->porttype = 0;
372 goto done;
375 if (arg == mapp->porttype) /* [><@=! baud]:term */
376 termp = mapp->porttype = 0;
377 else
378 termp = base = arg;
380 for (;; ++arg) { /* Optional conditionals. */
381 switch (*arg) {
382 case '<':
383 if (mapp->conditional & GT)
384 goto badmopt;
385 mapp->conditional |= LT;
386 break;
387 case '>':
388 if (mapp->conditional & LT)
389 goto badmopt;
390 mapp->conditional |= GT;
391 break;
392 case '@':
393 case '=': /* Not documented. */
394 mapp->conditional |= EQ;
395 break;
396 case '!':
397 mapp->conditional |= NOT;
398 break;
399 default:
400 goto next;
404 next:
405 if (*arg == ':') {
406 if (mapp->conditional)
407 goto badmopt;
408 ++arg;
409 } else { /* Optional baudrate. */
410 arg = strchr(p = arg, ':');
411 if (arg == 0)
412 goto badmopt;
413 *arg++ = '\0';
414 mapp->speed = tbaudrate(p);
417 if (arg == (char *) 0) /* Non-optional type. */
418 goto badmopt;
420 mapp->type = arg;
422 /* Terminate porttype, if specified. */
423 if (termp != 0)
424 *base = '\0';
426 /* If a NOT conditional, reverse the test. */
427 if (mapp->conditional & NOT)
428 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
430 /* If user specified a port with an option flag, set it. */
431 done:if (port) {
432 if (mapp->porttype)
433 badmopt:err("illegal -m option format: %s", copy);
434 mapp->porttype = port;
436 #ifdef MAPDEBUG
437 (void) printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
438 (void) printf("type: %s\n", mapp->type);
439 (void) printf("conditional: ");
440 p = "";
441 if (mapp->conditional & GT) {
442 (void) printf("GT");
443 p = "/";
445 if (mapp->conditional & EQ) {
446 (void) printf("%sEQ", p);
447 p = "/";
449 if (mapp->conditional & LT)
450 (void) printf("%sLT", p);
451 (void) printf("\nspeed: %d\n", mapp->speed);
452 #endif
456 * Return the type of terminal to use for a port of type 'type', as specified
457 * by the first applicable mapping in 'map'. If no mappings apply, return
458 * 'type'.
460 static const char *
461 mapped(const char *type)
463 MAP *mapp;
464 int match;
466 for (mapp = maplist; mapp; mapp = mapp->next)
467 if (mapp->porttype == 0 || !strcmp(mapp->porttype, type)) {
468 switch (mapp->conditional) {
469 case 0: /* No test specified. */
470 match = TRUE;
471 break;
472 case EQ:
473 match = (ospeed == mapp->speed);
474 break;
475 case GE:
476 match = (ospeed >= mapp->speed);
477 break;
478 case GT:
479 match = (ospeed > mapp->speed);
480 break;
481 case LE:
482 match = (ospeed <= mapp->speed);
483 break;
484 case LT:
485 match = (ospeed < mapp->speed);
486 break;
487 default:
488 match = FALSE;
490 if (match)
491 return (mapp->type);
493 /* No match found; return given type. */
494 return (type);
497 /**************************************************************************
499 * Entry fetching
501 **************************************************************************/
504 * Figure out what kind of terminal we're dealing with, and then read in
505 * its termcap entry.
507 static const char *
508 get_termcap_entry(char *userarg)
510 int errret;
511 char *p;
512 const char *ttype;
513 #if HAVE_GETTTYNAM
514 struct ttyent *t;
515 #else
516 FILE *fp;
517 #endif
518 char *ttypath;
520 if (userarg) {
521 ttype = userarg;
522 goto found;
525 /* Try the environment. */
526 if ((ttype = getenv("TERM")) != 0)
527 goto map;
529 if ((ttypath = ttyname(STDERR_FILENO)) != 0) {
530 p = _nc_basename(ttypath);
531 #if HAVE_GETTTYNAM
533 * We have the 4.3BSD library call getttynam(3); that means
534 * there's an /etc/ttys to look up device-to-type mappings in.
535 * Try ttyname(3); check for dialup or other mapping.
537 if ((t = getttynam(p))) {
538 ttype = t->ty_type;
539 goto map;
541 #else
542 if ((fp = fopen("/etc/ttytype", "r")) != 0
543 || (fp = fopen("/etc/ttys", "r")) != 0) {
544 char buffer[BUFSIZ];
545 char *s, *t, *d;
547 while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) {
548 for (s = buffer, t = d = 0; *s; s++) {
549 if (isspace(UChar(*s)))
550 *s = '\0';
551 else if (t == 0)
552 t = s;
553 else if (d == 0 && s != buffer && s[-1] == '\0')
554 d = s;
556 if (t != 0 && d != 0 && !strcmp(d, p)) {
557 ttype = strdup(t);
558 fclose(fp);
559 goto map;
562 fclose(fp);
564 #endif /* HAVE_GETTTYNAM */
567 /* If still undefined, use "unknown". */
568 ttype = "unknown";
570 map:ttype = mapped(ttype);
573 * If not a path, remove TERMCAP from the environment so we get a
574 * real entry from /etc/termcap. This prevents us from being fooled
575 * by out of date stuff in the environment.
577 found:if ((p = getenv("TERMCAP")) != 0 && *p != '/') {
578 /* 'unsetenv("TERMCAP")' is not portable.
579 * The 'environ' array is better.
581 int n;
582 for (n = 0; environ[n] != 0; n++) {
583 if (!strncmp("TERMCAP=", environ[n], 8)) {
584 while ((environ[n] = environ[n + 1]) != 0) {
585 n++;
587 break;
593 * ttype now contains a pointer to the type of the terminal.
594 * If the first character is '?', ask the user.
596 if (ttype[0] == '?') {
597 if (ttype[1] != '\0')
598 ttype = askuser(ttype + 1);
599 else
600 ttype = askuser(0);
602 /* Find the terminfo entry. If it doesn't exist, ask the user. */
603 while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
604 != OK) {
605 if (errret == 0) {
606 (void) fprintf(stderr, "tset: unknown terminal type %s\n",
607 ttype);
608 ttype = 0;
609 } else {
610 (void) fprintf(stderr,
611 "tset: can't initialize terminal type %s (error %d)\n",
612 ttype, errret);
613 ttype = 0;
615 ttype = askuser(ttype);
617 #if BROKEN_LINKER
618 tgetflag("am"); /* force lib_termcap.o to be linked for 'ospeed' */
619 #endif
620 return (ttype);
623 /**************************************************************************
625 * Mode-setting logic
627 **************************************************************************/
629 /* some BSD systems have these built in, some systems are missing
630 * one or more definitions. The safest solution is to override unless the
631 * commonly-altered ones are defined.
633 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
634 #undef CEOF
635 #undef CERASE
636 #undef CINTR
637 #undef CKILL
638 #undef CLNEXT
639 #undef CRPRNT
640 #undef CQUIT
641 #undef CSTART
642 #undef CSTOP
643 #undef CSUSP
644 #endif
646 /* control-character defaults */
647 #ifndef CEOF
648 #define CEOF CTRL('D')
649 #endif
650 #ifndef CERASE
651 #define CERASE CTRL('H')
652 #endif
653 #ifndef CINTR
654 #define CINTR 127 /* ^? */
655 #endif
656 #ifndef CKILL
657 #define CKILL CTRL('U')
658 #endif
659 #ifndef CLNEXT
660 #define CLNEXT CTRL('v')
661 #endif
662 #ifndef CRPRNT
663 #define CRPRNT CTRL('r')
664 #endif
665 #ifndef CQUIT
666 #define CQUIT CTRL('\\')
667 #endif
668 #ifndef CSTART
669 #define CSTART CTRL('Q')
670 #endif
671 #ifndef CSTOP
672 #define CSTOP CTRL('S')
673 #endif
674 #ifndef CSUSP
675 #define CSUSP CTRL('Z')
676 #endif
678 #define CHK(val, dft) ((int)val <= 0 ? dft : val)
680 static bool set_tabs(void);
683 * Reset the terminal mode bits to a sensible state. Very useful after
684 * a child program dies in raw mode.
686 static void
687 reset_mode(void)
689 #ifdef TERMIOS
690 tcgetattr(STDERR_FILENO, &mode);
691 #else
692 stty(STDERR_FILENO, &mode);
693 #endif
695 #ifdef TERMIOS
696 #if defined(VDISCARD) && defined(CDISCARD)
697 mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
698 #endif
699 mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
700 mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
701 #if defined(VFLUSH) && defined(CFLUSH)
702 mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
703 #endif
704 mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
705 mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
706 #if defined(VLNEXT) && defined(CLNEXT)
707 mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
708 #endif
709 mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
710 #if defined(VREPRINT) && defined(CRPRNT)
711 mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
712 #endif
713 #if defined(VSTART) && defined(CSTART)
714 mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
715 #endif
716 #if defined(VSTOP) && defined(CSTOP)
717 mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
718 #endif
719 #if defined(VSUSP) && defined(CSUSP)
720 mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
721 #endif
722 #if defined(VWERASE) && defined(CWERASE)
723 mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
724 #endif
726 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
727 #ifdef IUCLC
728 | IUCLC
729 #endif
730 #ifdef IXANY
731 | IXANY
732 #endif
733 | IXOFF);
735 mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
736 #ifdef IMAXBEL
737 | IMAXBEL
738 #endif
741 mode.c_oflag &= ~(0
742 #ifdef OLCUC
743 | OLCUC
744 #endif
745 #ifdef OCRNL
746 | OCRNL
747 #endif
748 #ifdef ONOCR
749 | ONOCR
750 #endif
751 #ifdef ONLRET
752 | ONLRET
753 #endif
754 #ifdef OFILL
755 | OFILL
756 #endif
757 #ifdef OFDEL
758 | OFDEL
759 #endif
760 #ifdef NLDLY
761 | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
762 #endif
765 mode.c_oflag |= (OPOST
766 #ifdef ONLCR
767 | ONLCR
768 #endif
771 mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
772 mode.c_cflag |= (CS8 | CREAD);
773 mode.c_lflag &= ~(ECHONL | NOFLSH
774 #ifdef TOSTOP
775 | TOSTOP
776 #endif
777 #ifdef ECHOPTR
778 | ECHOPRT
779 #endif
780 #ifdef XCASE
781 | XCASE
782 #endif
785 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
786 #ifdef ECHOCTL
787 | ECHOCTL
788 #endif
789 #ifdef ECHOKE
790 | ECHOKE
791 #endif
793 #endif
795 SET_TTY(STDERR_FILENO, &mode);
799 * Returns a "good" value for the erase character. This is loosely based on
800 * the BSD4.4 logic.
802 #ifdef TERMIOS
803 static int
804 default_erase(void)
806 int result;
808 if (over_strike
809 && key_backspace != 0
810 && strlen(key_backspace) == 1)
811 result = key_backspace[0];
812 else
813 result = CERASE;
815 return result;
817 #endif
820 * Update the values of the erase, interrupt, and kill characters in 'mode'.
822 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
823 * characters if they're unset, or if we specify them as options. This differs
824 * from BSD 4.4 tset, which always sets erase.
826 static void
827 set_control_chars(void)
829 #ifdef TERMIOS
830 if (mode.c_cc[VERASE] == 0 || terasechar >= 0)
831 mode.c_cc[VERASE] = terasechar >= 0 ? terasechar : default_erase();
833 if (mode.c_cc[VINTR] == 0 || intrchar >= 0)
834 mode.c_cc[VINTR] = intrchar >= 0 ? intrchar : CINTR;
836 if (mode.c_cc[VKILL] == 0 || tkillchar >= 0)
837 mode.c_cc[VKILL] = tkillchar >= 0 ? tkillchar : CKILL;
838 #endif
842 * Set up various conversions in 'mode', including parity, tabs, returns,
843 * echo, and case, according to the termcap entry. If the program we're
844 * running was named with a leading upper-case character, map external
845 * uppercase to internal lowercase.
847 static void
848 set_conversions(void)
850 #ifdef __OBSOLETE__
852 * Conversion logic for some *really* ancient terminal glitches,
853 * not supported in terminfo. Left here for succeeding generations
854 * to marvel at.
856 if (tgetflag("UC")) {
857 #ifdef IUCLC
858 mode.c_iflag |= IUCLC;
859 mode.c_oflag |= OLCUC;
860 #endif
861 } else if (tgetflag("LC")) {
862 #ifdef IUCLC
863 mode.c_iflag &= ~IUCLC;
864 mode.c_oflag &= ~OLCUC;
865 #endif
867 mode.c_iflag &= ~(PARMRK | INPCK);
868 mode.c_lflag |= ICANON;
869 if (tgetflag("EP")) {
870 mode.c_cflag |= PARENB;
871 mode.c_cflag &= ~PARODD;
873 if (tgetflag("OP")) {
874 mode.c_cflag |= PARENB;
875 mode.c_cflag |= PARODD;
877 #endif /* __OBSOLETE__ */
879 #ifdef TERMIOS
880 #ifdef ONLCR
881 mode.c_oflag |= ONLCR;
882 #endif
883 mode.c_iflag |= ICRNL;
884 mode.c_lflag |= ECHO;
885 #ifdef OXTABS
886 mode.c_oflag |= OXTABS;
887 #endif /* OXTABS */
889 /* test used to be tgetflag("NL") */
890 if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
891 /* Newline, not linefeed. */
892 #ifdef ONLCR
893 mode.c_oflag &= ~ONLCR;
894 #endif
895 mode.c_iflag &= ~ICRNL;
897 #ifdef __OBSOLETE__
898 if (tgetflag("HD")) /* Half duplex. */
899 mode.c_lflag &= ~ECHO;
900 #endif /* __OBSOLETE__ */
901 #ifdef OXTABS
902 /* test used to be tgetflag("pt") */
903 if (has_hardware_tabs) /* Print tabs. */
904 mode.c_oflag &= ~OXTABS;
905 #endif /* OXTABS */
906 mode.c_lflag |= (ECHOE | ECHOK);
907 #endif
910 /* Output startup string. */
911 static void
912 set_init(void)
914 char *p;
915 bool settle;
917 #ifdef __OBSOLETE__
918 if (pad_char != (char *) 0) /* Get/set pad character. */
919 PC = pad_char[0];
920 #endif /* OBSOLETE */
922 #ifdef TAB3
923 if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
924 oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
925 SET_TTY(STDERR_FILENO, &oldmode);
927 #endif
928 settle = set_tabs();
930 if (isreset) {
931 if ((p = reset_1string) != 0) {
932 tputs(p, 0, outc);
933 settle = TRUE;
935 if ((p = reset_2string) != 0) {
936 tputs(p, 0, outc);
937 settle = TRUE;
939 /* What about rf, rs3, as per terminfo man page? */
940 /* also might be nice to send rmacs, rmul, rmm */
941 if ((p = reset_file) != 0
942 || (p = init_file) != 0) {
943 cat(p);
944 settle = TRUE;
948 if (settle) {
949 (void) putc('\r', stderr);
950 (void) fflush(stderr);
951 (void) napms(1000); /* Settle the terminal. */
956 * Set the hardware tabs on the terminal, using the ct (clear all tabs),
957 * st (set one tab) and ch (horizontal cursor addressing) capabilities.
958 * This is done before if and is, so they can patch in case we blow this.
959 * Return TRUE if we set any tab stops, FALSE if not.
961 static bool
962 set_tabs()
964 if (set_tab && clear_all_tabs) {
965 int c;
967 (void) putc('\r', stderr); /* Force to left margin. */
968 tputs(clear_all_tabs, 0, outc);
970 for (c = 8; c < tcolumns; c += 8) {
971 /* Get to the right column. In BSD tset, this
972 * used to try a bunch of half-clever things
973 * with cup and hpa, for an average saving of
974 * somewhat less than two character times per
975 * tab stop, less that .01 sec at 2400cps. We
976 * lost all this cruft because it seemed to be
977 * introducing some odd bugs.
978 * ----------12345678----------- */
979 (void) fputs(" ", stderr);
980 tputs(set_tab, 0, outc);
982 putc('\r', stderr);
983 return (TRUE);
985 return (FALSE);
988 /**************************************************************************
990 * Main sequence
992 **************************************************************************/
995 * Tell the user if a control key has been changed from the default value.
997 #ifdef TERMIOS
998 static void
999 report(const char *name, int which, unsigned def)
1001 unsigned older, newer;
1002 char *p;
1004 newer = mode.c_cc[which];
1005 older = oldmode.c_cc[which];
1007 if (older == newer && older == def)
1008 return;
1010 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
1013 * Check 'delete' before 'backspace', since the key_backspace value
1014 * is ambiguous.
1016 if (newer == 0177)
1017 (void) fprintf(stderr, "delete.\n");
1018 else if ((p = key_backspace) != 0
1019 && newer == (unsigned char) p[0]
1020 && p[1] == '\0')
1021 (void) fprintf(stderr, "backspace.\n");
1022 else if (newer < 040) {
1023 newer ^= 0100;
1024 (void) fprintf(stderr, "control-%c (^%c).\n", newer, newer);
1025 } else
1026 (void) fprintf(stderr, "%c.\n", newer);
1028 #endif
1031 * Convert the obsolete argument forms into something that getopt can handle.
1032 * This means that -e, -i and -k get default arguments supplied for them.
1034 static void
1035 obsolete(char **argv)
1037 for (; *argv; ++argv) {
1038 char *parm = argv[0];
1040 if (parm[0] == '-' && parm[1] == '\0') {
1041 argv[0] = strdup("-q");
1042 continue;
1045 if ((parm[0] != '-')
1046 || (argv[1] && argv[1][0] != '-')
1047 || (parm[1] != 'e' && parm[1] != 'i' && parm[1] != 'k')
1048 || (parm[2] != '\0'))
1049 continue;
1050 switch (argv[0][1]) {
1051 case 'e':
1052 argv[0] = strdup("-e^H");
1053 break;
1054 case 'i':
1055 argv[0] = strdup("-i^C");
1056 break;
1057 case 'k':
1058 argv[0] = strdup("-k^U");
1059 break;
1064 static void
1065 usage(const char *pname)
1067 (void) fprintf(stderr,
1068 "usage: %s [-IQVrs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]", pname);
1069 exit_error();
1070 /* NOTREACHED */
1073 static char
1074 arg_to_char(void)
1076 return (optarg[0] == '^' && optarg[1] != '\0')
1077 ? ((optarg[1] == '?') ? '\177' : CTRL(optarg[1]))
1078 : optarg[0];
1082 main(int argc, char **argv)
1084 #if defined(TIOCGWINSZ) && defined(TIOCSWINSZ)
1085 struct winsize win;
1086 #endif
1087 int ch, noinit, noset, quiet, Sflag, sflag, showterm;
1088 const char *p;
1089 const char *ttype;
1091 if (GET_TTY(STDERR_FILENO, &mode) < 0)
1092 failed("standard error");
1093 can_restore = TRUE;
1094 original = oldmode = mode;
1095 #ifdef TERMIOS
1096 ospeed = cfgetospeed(&mode);
1097 #else
1098 ospeed = mode.sg_ospeed;
1099 #endif
1101 p = _nc_rootname(*argv);
1102 if (!strcmp(p, PROG_RESET)) {
1103 isreset = TRUE;
1104 reset_mode();
1107 obsolete(argv);
1108 noinit = noset = quiet = Sflag = sflag = showterm = 0;
1109 while ((ch = getopt(argc, argv, "a:d:e:Ii:k:m:np:qQSrsV")) != EOF) {
1110 switch (ch) {
1111 case 'q': /* display term only */
1112 noset = 1;
1113 break;
1114 case 'a': /* OBSOLETE: map identifier to type */
1115 add_mapping("arpanet", optarg);
1116 break;
1117 case 'd': /* OBSOLETE: map identifier to type */
1118 add_mapping("dialup", optarg);
1119 break;
1120 case 'e': /* erase character */
1121 terasechar = arg_to_char();
1122 break;
1123 case 'I': /* no initialization strings */
1124 noinit = 1;
1125 break;
1126 case 'i': /* interrupt character */
1127 intrchar = arg_to_char();
1128 break;
1129 case 'k': /* kill character */
1130 tkillchar = arg_to_char();
1131 break;
1132 case 'm': /* map identifier to type */
1133 add_mapping(0, optarg);
1134 break;
1135 case 'n': /* OBSOLETE: set new tty driver */
1136 break;
1137 case 'p': /* OBSOLETE: map identifier to type */
1138 add_mapping("plugboard", optarg);
1139 break;
1140 case 'Q': /* don't output control key settings */
1141 quiet = 1;
1142 break;
1143 case 'S': /* OBSOLETE: output TERM & TERMCAP */
1144 Sflag = 1;
1145 break;
1146 case 'r': /* display term on stderr */
1147 showterm = 1;
1148 break;
1149 case 's': /* output TERM set command */
1150 sflag = 1;
1151 break;
1152 case 'V':
1153 puts(curses_version());
1154 return EXIT_SUCCESS;
1155 case '?':
1156 default:
1157 usage(*argv);
1160 argc -= optind;
1161 argv += optind;
1163 if (argc > 1)
1164 usage(*argv);
1166 ttype = get_termcap_entry(*argv);
1168 if (!noset) {
1169 tcolumns = columns;
1170 tlines = lines;
1172 #if defined(TIOCGWINSZ) && defined(TIOCSWINSZ)
1173 /* Set window size */
1174 (void) ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
1175 if (win.ws_row == 0 && win.ws_col == 0 &&
1176 tlines > 0 && tcolumns > 0) {
1177 win.ws_row = tlines;
1178 win.ws_col = tcolumns;
1179 (void) ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
1181 #endif
1182 set_control_chars();
1183 set_conversions();
1185 if (!noinit)
1186 set_init();
1188 /* Set the modes if they've changed. */
1189 if (memcmp(&mode, &oldmode, sizeof(mode))) {
1190 SET_TTY(STDERR_FILENO, &mode);
1194 /* Get the terminal name from the entry. */
1195 ttype = _nc_first_name(cur_term->type.term_names);
1197 if (noset)
1198 (void) printf("%s\n", ttype);
1199 else {
1200 if (showterm)
1201 (void) fprintf(stderr, "Terminal type is %s.\n", ttype);
1203 * If erase, kill and interrupt characters could have been
1204 * modified and not -Q, display the changes.
1206 #ifdef TERMIOS
1207 if (!quiet) {
1208 report("Erase", VERASE, CERASE);
1209 report("Kill", VKILL, CKILL);
1210 report("Interrupt", VINTR, CINTR);
1212 #endif
1215 if (Sflag)
1216 err("The -S option is not supported under terminfo.");
1218 if (sflag) {
1219 int len;
1221 * Figure out what shell we're using. A hack, we look for an
1222 * environmental variable SHELL ending in "csh".
1224 if ((p = getenv("SHELL")) != 0
1225 && (len = strlen(p)) >= 3
1226 && !strcmp(p + len - 3, "csh"))
1227 p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n";
1228 else
1229 p = "TERM=%s;\n";
1230 (void) printf(p, ttype);
1233 return EXIT_SUCCESS;