📄 tset.c
字号:
if (arg == (char *) 0) /* Non-optional type. */ goto badmopt; mapp->type = arg; /* Terminate porttype, if specified. */ if (termp != 0) *base = '\0'; /* If a NOT conditional, reverse the test. */ if (mapp->conditional & NOT) mapp->conditional = ~mapp->conditional & (EQ | GT | LT); /* If user specified a port with an option flag, set it. */ done:if (port) { if (mapp->porttype) badmopt:err("illegal -m option format: %s", copy); mapp->porttype = port; }#ifdef MAPDEBUG (void) printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY"); (void) printf("type: %s\n", mapp->type); (void) printf("conditional: "); p = ""; if (mapp->conditional & GT) { (void) printf("GT"); p = "/"; } if (mapp->conditional & EQ) { (void) printf("%sEQ", p); p = "/"; } if (mapp->conditional & LT) (void) printf("%sLT", p); (void) printf("\nspeed: %d\n", mapp->speed);#endif}/* * Return the type of terminal to use for a port of type 'type', as specified * by the first applicable mapping in 'map'. If no mappings apply, return * 'type'. */static const char *mapped(const char *type){ MAP *mapp; int match; for (mapp = maplist; mapp; mapp = mapp->next) if (mapp->porttype == 0 || !strcmp(mapp->porttype, type)) { switch (mapp->conditional) { case 0: /* No test specified. */ match = TRUE; break; case EQ: match = (ospeed == mapp->speed); break; case GE: match = (ospeed >= mapp->speed); break; case GT: match = (ospeed > mapp->speed); break; case LE: match = (ospeed <= mapp->speed); break; case LT: match = (ospeed < mapp->speed); break; default: match = FALSE; } if (match) return (mapp->type); } /* No match found; return given type. */ return (type);}/************************************************************************** * * Entry fetching * **************************************************************************//* * Figure out what kind of terminal we're dealing with, and then read in * its termcap entry. */static const char *get_termcap_entry(char *userarg){ int errret; char *p; const char *ttype;#if HAVE_GETTTYNAM struct ttyent *t;#else FILE *fp;#endif char *ttypath; if (userarg) { ttype = userarg; goto found; } /* Try the environment. */ if ((ttype = getenv("TERM")) != 0) goto map; if ((ttypath = ttyname(STDERR_FILENO)) != 0) { p = _nc_basename(ttypath);#if HAVE_GETTTYNAM /* * We have the 4.3BSD library call getttynam(3); that means * there's an /etc/ttys to look up device-to-type mappings in. * Try ttyname(3); check for dialup or other mapping. */ if ((t = getttynam(p))) { ttype = t->ty_type; goto map; }#else if ((fp = fopen("/etc/ttytype", "r")) != 0 || (fp = fopen("/etc/ttys", "r")) != 0) { char buffer[BUFSIZ]; char *s, *t, *d; while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) { for (s = buffer, t = d = 0; *s; s++) { if (isspace(UChar(*s))) *s = '\0'; else if (t == 0) t = s; else if (d == 0 && s != buffer && s[-1] == '\0') d = s; } if (t != 0 && d != 0 && !strcmp(d, p)) { ttype = strdup(t); fclose(fp); goto map; } } fclose(fp); }#endif /* HAVE_GETTTYNAM */ } /* If still undefined, use "unknown". */ ttype = "unknown"; map:ttype = mapped(ttype); /* * If not a path, remove TERMCAP from the environment so we get a * real entry from /etc/termcap. This prevents us from being fooled * by out of date stuff in the environment. */ found:if ((p = getenv("TERMCAP")) != 0 && *p != '/') { /* 'unsetenv("TERMCAP")' is not portable. * The 'environ' array is better. */ int n; for (n = 0; environ[n] != 0; n++) { if (!strncmp("TERMCAP=", environ[n], 8)) { while ((environ[n] = environ[n + 1]) != 0) { n++; } break; } } } /* * ttype now contains a pointer to the type of the terminal. * If the first character is '?', ask the user. */ if (ttype[0] == '?') { if (ttype[1] != '\0') ttype = askuser(ttype + 1); else ttype = askuser(0); } /* Find the terminfo entry. If it doesn't exist, ask the user. */ while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret) != OK) { if (errret == 0) { (void) fprintf(stderr, "%s: unknown terminal type %s\n", _nc_progname, ttype); ttype = 0; } else { (void) fprintf(stderr, "%s: can't initialize terminal type %s (error %d)\n", _nc_progname, ttype, errret); ttype = 0; } ttype = askuser(ttype); }#if BROKEN_LINKER tgetflag("am"); /* force lib_termcap.o to be linked for 'ospeed' */#endif return (ttype);}/************************************************************************** * * Mode-setting logic * **************************************************************************//* some BSD systems have these built in, some systems are missing * one or more definitions. The safest solution is to override unless the * commonly-altered ones are defined. */#if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))#undef CEOF#undef CERASE#undef CINTR#undef CKILL#undef CLNEXT#undef CRPRNT#undef CQUIT#undef CSTART#undef CSTOP#undef CSUSP#endif/* control-character defaults */#ifndef CEOF#define CEOF CTRL('D')#endif#ifndef CERASE#define CERASE CTRL('H')#endif#ifndef CINTR#define CINTR 127 /* ^? */#endif#ifndef CKILL#define CKILL CTRL('U')#endif#ifndef CLNEXT#define CLNEXT CTRL('v')#endif#ifndef CRPRNT#define CRPRNT CTRL('r')#endif#ifndef CQUIT#define CQUIT CTRL('\\')#endif#ifndef CSTART#define CSTART CTRL('Q')#endif#ifndef CSTOP#define CSTOP CTRL('S')#endif#ifndef CSUSP#define CSUSP CTRL('Z')#endif#define CHK(val, dft) ((int)val <= 0 ? dft : val)static bool set_tabs(void);/* * Reset the terminal mode bits to a sensible state. Very useful after * a child program dies in raw mode. */static voidreset_mode(void){#ifdef TERMIOS tcgetattr(STDERR_FILENO, &mode);#else stty(STDERR_FILENO, &mode);#endif#ifdef TERMIOS#if defined(VDISCARD) && defined(CDISCARD) mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);#endif mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF); mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);#if defined(VFLUSH) && defined(CFLUSH) mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);#endif mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR); mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);#if defined(VLNEXT) && defined(CLNEXT) mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);#endif mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);#if defined(VREPRINT) && defined(CRPRNT) mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);#endif#if defined(VSTART) && defined(CSTART) mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);#endif#if defined(VSTOP) && defined(CSTOP) mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);#endif#if defined(VSUSP) && defined(CSUSP) mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);#endif#if defined(VWERASE) && defined(CWERASE) mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);#endif mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR#ifdef IUCLC | IUCLC#endif#ifdef IXANY | IXANY#endif | IXOFF); mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON#ifdef IMAXBEL | IMAXBEL#endif ); mode.c_oflag &= ~(0#ifdef OLCUC | OLCUC#endif#ifdef OCRNL | OCRNL#endif#ifdef ONOCR | ONOCR#endif#ifdef ONLRET | ONLRET#endif#ifdef OFILL | OFILL#endif#ifdef OFDEL | OFDEL#endif#ifdef NLDLY | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY#endif ); mode.c_oflag |= (OPOST#ifdef ONLCR | ONLCR#endif ); mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL); mode.c_cflag |= (CS8 | CREAD); mode.c_lflag &= ~(ECHONL | NOFLSH#ifdef TOSTOP | TOSTOP#endif#ifdef ECHOPTR | ECHOPRT#endif#ifdef XCASE | XCASE#endif ); mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK#ifdef ECHOCTL | ECHOCTL#endif#ifdef ECHOKE | ECHOKE#endif );#endif SET_TTY(STDERR_FILENO, &mode);}/* * Returns a "good" value for the erase character. This is loosely based on * the BSD4.4 logic. */#ifdef TERMIOSstatic intdefault_erase(void){ int result; if (over_strike && key_backspace != 0 && strlen(key_backspace) == 1) result = key_backspace[0]; else result = CERASE; return result;}#endif/* * Update the values of the erase, interrupt, and kill characters in 'mode'. * * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase * characters if they're unset, or if we specify them as options. This differs * from BSD 4.4 tset, which always sets erase. */static voidset_control_chars(void){#ifdef TERMIOS if (mode.c_cc[VERASE] == 0 || terasechar >= 0) mode.c_cc[VERASE] = terasechar >= 0 ? terasechar : default_erase(); if (mode.c_cc[VINTR] == 0 || intrchar >= 0) mode.c_cc[VINTR] = intrchar >= 0 ? intrchar : CINTR; if (mode.c_cc[VKILL] == 0 || tkillchar >= 0) mode.c_cc[VKILL] = tkillchar >= 0 ? tkillchar : CKILL;#endif}/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -