📄 commands.c
字号:
for (;;) { if (fgets(line, sizeof(line), rcfile) == NULL) break; if (line[0] == 0) break; if (line[0] == '#') continue; if (gotmachine) { if (!isspace(line[0])) gotmachine = 0; } if (gotmachine == 0) { if (isspace(line[0])) continue; if (strncasecmp(line, m1, l1) == 0) strncpy(line, &line[l1], sizeof(line) - l1); else if (strncasecmp(line, m2, l2) == 0) strncpy(line, &line[l2], sizeof(line) - l2); else if (strncasecmp(line, "DEFAULT", 7) == 0) strncpy(line, &line[7], sizeof(line) - 7); else continue; if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n') continue; gotmachine = 1; } makeargv(); if (margv[0] == 0) continue; c = getcmd(margv[0]); if (Ambiguous(c)) { printf("?Ambiguous command: %s\r\n", margv[0]); continue; } if (c == 0) { printf("?Invalid command: %s\r\n", margv[0]); continue; } /* * This should never happen... */ if (c->needconnect && !connected) { printf("?Need to be connected first for %s.\r\n", margv[0]); continue; } (*c->handler)(margc, margv); } fclose(rcfile);} inttn(argc, argv) int argc; char *argv[];{ struct addrinfo hints, *res, *res0; int error; struct sockaddr_in sin; unsigned long temp; extern char *inet_ntoa();#if defined(IP_OPTIONS) && defined(IPPROTO_IP) char *srp = 0; int srlen;#endif char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0; int retry;#ifdef NI_WITHSCOPEID const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;#else const int niflags = NI_NUMERICHOST;#endif /* clear the socket address prior to use */ memset((char *)&sin, 0, sizeof(sin)); if (connected) { printf("?Already connected to %s\r\n", hostname); return 0; } if (argc < 2) { strlcpy(line, "open ", sizeof(line)); printf("(to) "); (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin); makeargv(); argc = margc; argv = margv; } cmd = *argv; --argc; ++argv; while (argc) { if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?")) goto usage; if (strcmp(*argv, "-l") == 0) { --argc; ++argv; if (argc == 0) goto usage; if ((user = strdup(*argv++)) == NULL) err(1, "strdup"); --argc; continue; } if (strcmp(*argv, "-b") == 0) { --argc; ++argv; if (argc == 0) goto usage; aliasp = *argv++; --argc; continue; } if (strcmp(*argv, "-a") == 0) { --argc; ++argv; autologin = 1; continue; } if (hostp == 0) { hostp = *argv++; --argc; continue; } if (portp == 0) { portp = *argv++; --argc; continue; } usage: printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd); return 0; } if (hostp == 0) goto usage;#if defined(IP_OPTIONS) && defined(IPPROTO_IP) if (hostp[0] == '@' || hostp[0] == '!') { if ((hostname = strrchr(hostp, ':')) == NULL) hostname = strrchr(hostp, '@'); hostname++; srp = 0; temp = sourceroute(hostp, &srp, &srlen); if (temp == 0) { herror(srp); return 0; } else if (temp == -1) { printf("Bad source route option: %s\r\n", hostp); return 0; } else { abort(); } } else#endif { hostname = hostp; memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME; if (portp == NULL) { portp = "telnet"; telnetport = 1; } else if (*portp == '-') { portp++; telnetport = 1; } else telnetport = 0; h_errno = 0; error = getaddrinfo(hostp, portp, &hints, &res0); if (error) { if (error == EAI_SERVICE) warnx("%s: bad port", portp); else warnx("%s: %s", hostp, gai_strerror(error)); if (h_errno) herror(hostp); return 0; } } net = -1; retry = 0; for (res = res0; res; res = res->ai_next) { if (1 /* retry */) { char hbuf[NI_MAXHOST]; if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) { strlcpy(hbuf, "(invalid)", sizeof(hbuf)); } printf("Trying %s...\r\n", hbuf); } net = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (net < 0) continue; if (aliasp) { struct addrinfo ahints, *ares; memset(&ahints, 0, sizeof(ahints)); ahints.ai_family = family; ahints.ai_socktype = SOCK_STREAM; ahints.ai_flags = AI_PASSIVE; error = getaddrinfo(aliasp, "0", &ahints, &ares); if (error) { warn("%s: %s", aliasp, gai_strerror(error)); close(net); continue; } if (bind(net, ares->ai_addr, ares->ai_addrlen) < 0) { perror(aliasp); (void) close(net); /* dump descriptor */ freeaddrinfo(ares); continue; } freeaddrinfo(ares); }#if defined(IP_OPTIONS) && defined(IPPROTO_IP) if (srp && res->ai_family == AF_INET && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0) perror("setsockopt (IP_OPTIONS)");#endif#if defined(IPPROTO_IP) && defined(IP_TOS) if (res->ai_family == AF_INET) {# if defined(HAS_GETTOS) struct tosent *tp; if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) tos = tp->t_tos;# endif if (tos < 0) tos = IPTOS_LOWDELAY; /* Low Delay bit */ if (tos && (setsockopt(net, IPPROTO_IP, IP_TOS, (void *)&tos, sizeof(int)) < 0) && (errno != ENOPROTOOPT)) perror("telnet: setsockopt (IP_TOS) (ignored)"); }#endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) { perror("setsockopt (SO_DEBUG)"); } if (connect(net, res->ai_addr, res->ai_addrlen) < 0) { char hbuf[NI_MAXHOST]; if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) { strlcpy(hbuf, "(invalid)", sizeof(hbuf)); } fprintf(stderr, "telnet: connect to address %s: %s\n", hbuf, strerror(errno)); close(net); net = -1; retry++; continue; } connected++;#if defined(AUTHENTICATION) || defined(ENCRYPTION) auth_encrypt_connect(connected);#endif /* defined(AUTHENTICATION) */ break; } freeaddrinfo(res0); if (net < 0) { return 0; } cmdrc(hostp, hostname); if (autologin && user == NULL) { struct passwd *pw; user = getenv("USER"); if (user == NULL || ((pw = getpwnam(user)) && pw->pw_uid != getuid())) { if ((pw = getpwuid(getuid())) != NULL) user = pw->pw_name; else user = NULL; } } if (user) { env_define((unsigned char *)"USER", (unsigned char *)user); env_export((unsigned char *)"USER"); } (void) call(status, "status", "notmuch", 0); if (setjmp(peerdied) == 0) telnet(user); (void) NetClose(net); ExitString("Connection closed by foreign host.\r\n",1); /*NOTREACHED*/ return 0;}#define HELPINDENT (sizeof ("connect"))static char openhelp[] = "connect to a site", closehelp[] = "close current connection", logouthelp[] = "forcibly logout remote user and close the connection", quithelp[] = "exit telnet", statushelp[] = "print status information", helphelp[] = "print help information", sendhelp[] = "transmit special characters ('send ?' for more)", sethelp[] = "set operating parameters ('set ?' for more)", unsethelp[] = "unset operating parameters ('unset ?' for more)", togglestring[] ="toggle operating parameters ('toggle ?' for more)", slchelp[] = "change state of special charaters ('slc ?' for more)", displayhelp[] = "display operating parameters",#if defined(TN3270) && defined(unix) transcomhelp[] = "specify Unix command for transparent mode pipe",#endif /* defined(TN3270) && defined(unix) */#if defined(AUTHENTICATION) authhelp[] = "turn on (off) authentication ('auth ?' for more)",#endif#if defined(ENCRYPTION) encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",#endif zhelp[] = "suspend telnet",#ifdef SKEY skeyhelp[] = "compute response to s/key challenge",#endif shellhelp[] = "invoke a subshell", envhelp[] = "change environment variables ('environ ?' for more)", modestring[] = "try to enter line or character mode ('mode ?' for more)";static int help(int, char**);static Command cmdtab[] = { { "close", closehelp, bye, 1 }, { "logout", logouthelp, logout, 1 }, { "display", displayhelp, display, 0 }, { "mode", modestring, modecmd, 0 }, { "open", openhelp, tn, 0 }, { "quit", quithelp, quit, 0 }, { "send", sendhelp, sendcmd, 0 }, { "set", sethelp, setcmd, 0 }, { "unset", unsethelp, unsetcmd, 0 }, { "status", statushelp, status, 0 }, { "toggle", togglestring, toggle, 0 }, { "slc", slchelp, slccmd, 0 },#if defined(TN3270) && defined(unix) { "transcom", transcomhelp, settranscom, 0 },#endif /* defined(TN3270) && defined(unix) */#if defined(AUTHENTICATION) { "auth", authhelp, auth_cmd, 0 },#endif#if defined(ENCRYPTION) { "encrypt", encrypthelp, encrypt_cmd, 0 },#endif { "z", zhelp, telnetsuspend, 0 },#if defined(TN3270) { "!", shellhelp, shell, 1 },#else { "!", shellhelp, shell, 0 },#endif { "environ", envhelp, env_cmd, 0 }, { "?", helphelp, help, 0 },#if defined(SKEY) { "skey", skeyhelp, skey_calc, 0 },#endif { 0, 0, 0, 0 }};static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";static char escapehelp[] = "deprecated command -- use 'set escape' instead";static Command cmdtab2[] = { { "help", 0, help, 0 }, { "escape", escapehelp, setescape, 0 }, { "crmod", crmodhelp, togcrmod, 0 }, { 0, 0, 0, 0 }};/* * Call routine with argc, argv set from args (terminated by 0). */ /*VARARGS1*/ static intcall(intrtn_t routine, ...){ va_list ap; char *args[100]; int argno = 0; va_start(ap, routine); while ((args[argno++] = va_arg(ap, char *)) != 0); va_end(ap); return (*routine)(argno-1, args);} static Command *getcmd(name) char *name;{ Command *cm; if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))) return cm; return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));} voidcommand(top, tbuf, cnt) int top; char *tbuf; int cnt;{ Command *c; setcommandmode(); if (!top) { putchar('\n');#if defined(unix) } else { (void) signal(SIGINT, SIG_DFL); (void) signal(SIGQUIT, SIG_DFL);#endif /* defined(unix) */ } for (;;) { if (rlogin == _POSIX_VDISABLE) printf("%s> ", prompt); if (tbuf) { char *cp; cp = line; while (cnt > 0 && (*cp++ = *tbuf++) != '\n') cnt--; tbuf = 0; if (cp == line || *--cp != '\n' || cp == line) goto getline; *cp = '\0'; if (rlogin == _POSIX_VDISABLE) printf("%s\r\n", line); } else { getline: if (rlogin != _POSIX_VDISABLE) printf("%s> ", prompt); if (fgets(line, sizeof(line), stdin) == NULL) { if (feof(stdin) || ferror(stdin)) { (void) quit(); /*NOTREACHED*/ } break; } } if (line[0] == 0) break; makeargv(); if (margv[0] == 0) { break; } c = getcmd(margv[0]); if (Ambiguous(c)) { printf("?Ambiguous command\r\n"); continue; } if (c == 0) { printf("?Invalid command\r\n"); continue; } if (c->needconnect && !connected) { printf("?Need to be connected first.\r\n"); continue; } if ((*c->handler)(margc, margv)) { break; } } if (!top) { if (!connected) { longjmp(toplevel, 1); /*NOTREACHED*/ }#if defined(TN3270) if (shell_active == 0) { setconnmode(0); }#else /* defined(TN3270) */ setconnmode(0);#endif /* defined(TN3270) */ }}/* * Help command. */ static inthelp(argc, argv) int argc; char *argv[];{ Command *c; if (argc == 1) { printf("Commands may be abbreviated. Commands are:\r\n\r\n"); for (c = cmdtab; c->name; c++) if (c->help) { printf("%-*s\t%s\r\n", (int)HELPINDENT, c->name, c->help); } return 0; } while (--argc > 0) { char *arg; arg = *++argv; c = getcmd(arg); if (Ambiguous(c)) printf("?Ambiguous help command %s\r\n", arg); else if (c == (Command *)0) printf("?Invalid help command %s\r\n", arg); else printf("%s\r\n", c->help); } return 0;}#if defined(IP_OPTIONS) && defined(IPPROTO_IP)/* * Source route is handed in as * [!]@hop1@hop2...[@|:]dst * If the leading ! is present, it is a * strict source route, otherwise it is * assmed to be a loose source route. * * We fill in the source route option as * hop1,hop2,hop3...dest * and return a pointer to hop1, which will * be the address to connect() to. * * Arguments: * arg: pointer to route list to decipher * * cpp: If *cpp is not equal to NULL, this is a * pointer to a pointer to a character array * that should be filled in with the option. * * lenp: pointer to an integer that contains the * length of *cpp if *cpp != NULL. * * Return values: * * Returns the address of the host to connect to. If the * return value is -1, there was a syntax error in the * option, either unknown characters, or too many hosts. * If the return val
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -