static void print_shell_commands(const char *ttype) { const char *p; int len; char *var; char *leaf; /* * Figure out what shell we're using. A hack, we look for an * environmental variable SHELL ending in "csh". */ if ((var = getenv("SHELL")) != 0 && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3) && !strcmp(leaf + len - 3, "csh")) p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n"; else p = "TERM=%s;\n"; (void) printf(p, ttype); }
_nc_access(const char *path, int mode) { if (access(path, mode) < 0) { if ((mode & W_OK) != 0 && errno == ENOENT && strlen(path) < PATH_MAX) { char head[PATH_MAX]; char *leaf = _nc_basename(strcpy(head, path)); if (leaf == 0) leaf = head; *leaf = '\0'; if (head == leaf) (void) strcpy(head, "."); return access(head, R_OK | W_OK | X_OK); } return -1; } return 0; }
_nc_rootname(char *path) { char *result = _nc_basename(path); #if !MIXEDCASE_FILENAMES || defined(PROG_EXT) static char *temp; char *s; temp = strdup(result); result = temp; #if !MIXEDCASE_FILENAMES for (s = result; *s != '\0'; ++s) { *s = LOWERCASE(*s); } #endif #if defined(PROG_EXT) if ((s = strrchr(result, '.')) != 0) { if (!strcmp(s, PROG_EXT)) *s = '\0'; } #endif #endif return result; }
/* * 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), fp) != NULL) { 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 && !_nc_is_abs_path(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); }
int main(int argc, char **argv) { int ch, noinit, noset, quiet, Sflag, sflag, showterm; const char *p; const char *ttype; if (pledge("stdio rpath wpath tty", NULL) == -1) err("pledge: %s", strerror(errno)); obsolete(argv); noinit = noset = quiet = Sflag = sflag = showterm = 0; while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:np:qQSrsVw")) != -1) { switch (ch) { case 'c': /* set control-chars */ opt_c = TRUE; break; case 'a': /* OBSOLETE: map identifier to type */ add_mapping("arpanet", optarg); break; case 'd': /* OBSOLETE: map identifier to type */ add_mapping("dialup", optarg); break; case 'e': /* erase character */ terasechar = arg_to_char(); break; case 'I': /* no initialization strings */ noinit = 1; break; case 'i': /* interrupt character */ intrchar = arg_to_char(); break; case 'k': /* kill character */ tkillchar = arg_to_char(); break; case 'm': /* map identifier to type */ add_mapping(0, optarg); break; case 'n': /* OBSOLETE: set new tty driver */ break; case 'p': /* OBSOLETE: map identifier to type */ add_mapping("plugboard", optarg); break; case 'Q': /* don't output control key settings */ quiet = 1; break; case 'q': /* display term only */ noset = 1; break; case 'r': /* display term on stderr */ showterm = 1; break; case 'S': /* OBSOLETE: output TERM & TERMCAP */ Sflag = 1; break; case 's': /* output TERM set command */ sflag = 1; break; case 'V': /* print curses-version */ puts(curses_version()); ExitProgram(EXIT_SUCCESS); case 'w': /* set window-size */ opt_w = TRUE; break; case '?': default: usage(); } } _nc_progname = _nc_rootname(*argv); argc -= optind; argv += optind; if (argc > 1) usage(); if (!opt_c && !opt_w) opt_c = opt_w = TRUE; if (GET_TTY(STDERR_FILENO, &mode) < 0) failed("standard error"); can_restore = TRUE; original = oldmode = mode; #ifdef TERMIOS ospeed = (NCURSES_OSPEED) cfgetospeed(&mode); #else ospeed = (NCURSES_OSPEED) mode.sg_ospeed; #endif if (!strcmp(_nc_progname, PROG_RESET)) { isreset = TRUE; reset_mode(); } ttype = get_termcap_entry(*argv); if (!noset) { tcolumns = columns; tlines = lines; #if HAVE_SIZECHANGE if (opt_w) { struct winsize win; /* Set window size if not set already */ (void) ioctl(STDERR_FILENO, TIOCGWINSZ, &win); if (win.ws_row == 0 && win.ws_col == 0 && tlines > 0 && tcolumns > 0) { win.ws_row = tlines; win.ws_col = tcolumns; (void) ioctl(STDERR_FILENO, TIOCSWINSZ, &win); } } #endif if (opt_c) { set_control_chars(); set_conversions(); if (!noinit) set_init(); /* Set the modes if they've changed. */ if (memcmp(&mode, &oldmode, sizeof(mode))) { SET_TTY(STDERR_FILENO, &mode); } } } /* Get the terminal name from the entry. */ ttype = _nc_first_name(cur_term->type.term_names); if (noset) (void) printf("%s\n", ttype); else { if (showterm) (void) fprintf(stderr, "Terminal type is %s.\n", ttype); /* * If erase, kill and interrupt characters could have been * modified and not -Q, display the changes. */ #ifdef TERMIOS if (!quiet) { report("Erase", VERASE, CERASE); report("Kill", VKILL, CKILL); report("Interrupt", VINTR, CINTR); } #endif } if (Sflag) err("The -S option is not supported under terminfo."); if (sflag) { int len; char *var; char *leaf; /* * Figure out what shell we're using. A hack, we look for an * environmental variable SHELL ending in "csh". */ if ((var = getenv("SHELL")) != 0 && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3) && !strcmp(leaf + len - 3, "csh")) p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n"; else p = "TERM=%s;\n"; (void) printf(p, ttype); } ExitProgram(EXIT_SUCCESS); }
int main(int argc, char **argv) { char *term; int errret; bool cmdline = TRUE; int c; char buf[BUFSIZ]; int errors = 0; check_aliases(prg_name = _nc_basename(argv[0])); term = getenv("TERM"); while ((c = getopt(argc, argv, "ST:V")) != EOF) { switch (c) { case 'S': cmdline = FALSE; break; case 'T': use_env(FALSE); term = optarg; break; case 'V': puts(curses_version()); return EXIT_SUCCESS; default: usage(); /* NOTREACHED */ } } /* * Modify the argument list to omit the options we processed. */ if (is_reset || is_init) { if (optind-- < argc) { argc -= optind; argv += optind; } argv[0] = prg_name; } else { argc -= optind; argv += optind; } if (term == 0 || *term == '\0') quit(2, "No value for $TERM and no -T specified"); if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0) quit(3, "unknown terminal \"%s\"", term); if (cmdline) { if ((argc <= 0) && !is_reset && !is_init) usage(); return tput(argc, argv); } while (fgets(buf, sizeof(buf), stdin) != 0) { char *argvec[16]; /* command, 9 parms, null, & slop */ int argnum = 0; char *cp; /* crack the argument list into a dope vector */ for (cp = buf; *cp; cp++) { if (isspace(*cp)) *cp = '\0'; else if (cp == buf || cp[-1] == 0) argvec[argnum++] = cp; } argvec[argnum] = 0; if (tput(argnum, argvec) != 0) errors++; } return errors > 0; }