📄 commands.cc
字号:
int (*handler)(int); /* routine which executes command */ int needconnect; /* Do we need to be connected to execute? */ int arg1;};extern int modehelp(int);static struct modelist ModeList[] = { { "character", "Disable LINEMODE option", docharmode, 1,0},#ifdef KLUDGELINEMODE { "", "(or disable obsolete line-by-line mode)", NULL, 0,0 },#endif { "line", "Enable LINEMODE option", dolinemode, 1,0},#ifdef KLUDGELINEMODE { "", "(or enable obsolete line-by-line mode)", NULL, 0,0 },#endif { "", "", NULL, 0, 0 }, { "", "These require the LINEMODE option to be enabled", NULL, 0, 0}, { "isig", "Enable signal trapping", setmode, 1, MODE_TRAPSIG }, { "+isig", 0, setmode, 1, MODE_TRAPSIG }, { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG }, { "edit", "Enable character editing", setmode, 1, MODE_EDIT }, { "+edit", 0, setmode, 1, MODE_EDIT }, { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT }, { "softtabs", "Enable tab expansion", setmode, 1, MODE_SOFT_TAB }, { "+softtabs", 0, setmode, 1, MODE_SOFT_TAB }, { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB }, { "litecho", "Enable literal character echo", setmode, 1, MODE_LIT_ECHO }, { "+litecho", 0, setmode, 1, MODE_LIT_ECHO }, { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO }, { "help", 0, modehelp, 0, 0 },#ifdef KLUDGELINEMODE { "kludgeline", 0, dokludgemode, 1, 0 },#endif { "", "", 0, 0, 0 }, { "?", "Print help information", modehelp, 0, 0 }, { 0, 0, 0, 0, 0 },};int modehelp(int) { struct modelist *mt; printf("format is: 'mode Mode', where 'Mode' is one of:\n\n"); for (mt = ModeList; mt->name; mt++) { if (mt->help) { if (*mt->help) printf("%-15s %s\n", mt->name, mt->help); else printf("\n"); } } return 0;}#define GETMODECMD(name) (struct modelist *) \ genget(name, (char **) ModeList, sizeof(struct modelist))static int modecmd(const char *arg) { struct modelist *mt; mt = GETMODECMD(arg); if (mt == 0) { fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", arg); } else if (mt == AMBIGUOUS) { fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", arg); } else if (mt->needconnect && !connected) { printf("?Need to be connected first.\n"); printf("'mode ?' for help.\n"); } else if (mt->handler) { return (*mt->handler)(mt->arg1); } return 0;}/* * The following data structures and routines implement the * "display" command. */static void dotog(struct togglelist *tl) { if (tl->variable && tl->actionexplanation) { if (*tl->variable) { printf("will"); } else { printf("won't"); } printf(" %s.\n", tl->actionexplanation); }}static void doset(struct setlist *sl) { if (sl->name && *sl->name != ' ') { if (sl->handler == 0) { printf("%-15s [%s]\n", sl->name, control(*sl->charp)); } else { printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); } }}static int display(int argc, const char *argv[]) { struct togglelist *tl; struct setlist *sl; if (argc == 1) { for (tl = Togglelist; tl->name; tl++) { dotog(tl); } printf("\n"); for (sl = Setlist; sl->name; sl++) { doset(sl); } } else { int i; for (i = 1; i < argc; i++) { sl = getset(argv[i]); tl = GETTOGGLE(argv[i]); if (sl == AMBIGUOUS || tl == AMBIGUOUS) { printf("?Ambiguous argument '%s'.\n", argv[i]); return 0; } else if (!sl && !tl) { printf("?Unknown argument '%s'.\n", argv[i]); return 0; } else { if (tl) { dotog(tl); } if (sl) { doset(sl); } } } } optionstatus(); return 1;}/* * The following are the data structures, and many of the routines, * relating to command processing. *//* * Set the escape character. */static int setescape(int argc, const char *argv[]) { const char *arg; char buf[50]; printf( "Deprecated usage - please use 'set escape%s%s' in the future.\n", (argc > 2)? " ":"", (argc > 2)? argv[1]: ""); if (argc > 2) { arg = argv[1]; } else { printf("new escape character: "); (void) fgets(buf, sizeof(buf), stdin); arg = buf; } if (arg[0] != '\0') escapechar = arg[0]; if (!In3270) { printf("Escape character is '%s'.\n", control(escapechar)); } (void) fflush(stdout); return 1;}static int togcrmod(void) { crmod = !crmod; printf("Deprecated usage - please use 'toggle crmod' in the future.\n"); printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't"); fflush(stdout); return 1;}int suspend(void) {#ifdef SIGTSTP setcommandmode(); { long oldrows, oldcols, newrows, newcols, err; err = TerminalWindowSize(&oldrows, &oldcols); (void) kill(0, SIGTSTP); err += TerminalWindowSize(&newrows, &newcols); if (connected && !err && ((oldrows != newrows) || (oldcols != newcols))) { sendnaws(); } } /* reget parameters in case they were changed */ TerminalSaveState(); setconnmode(0);#else printf("Suspend is not supported. Try the '!' command instead\n");#endif return 1;}#if !defined(TN3270)int shell(int argc, const char **) { setcommandmode(); switch(vfork()) { case -1: perror("Fork failed\n"); break; case 0: { /* * Fire up the shell in the child. */ const char *shellp, *shellname; shellp = getenv("SHELL"); if (shellp == NULL) shellp = "/bin/sh"; if ((shellname = rindex(shellp, '/')) == 0) shellname = shellp; else shellname++; if (argc > 1) execl(shellp, shellname, "-c", &saveline[1], 0); else execl(shellp, shellname, 0); perror("Execl"); _exit(1); } default: wait(NULL); /* Wait for the shell to complete */ } return 1;}#endif /* !defined(TN3270) */static int dobye(int isfromquit) { extern int resettermname; if (connected) { nlink.close(1); printf("Connection closed.\n"); connected = 0; resettermname = 1; /* reset options */ tninit();#if defined(TN3270) SetIn3270(); /* Get out of 3270 mode */#endif /* defined(TN3270) */ } if (!isfromquit) { siglongjmp(toplevel, 1); /* NOTREACHED */ } return 1; /* Keep lint, etc., happy */}static int bye(void) { if (!connected) { printf("Need to be connected first for `bye'.\n"); return 0; } return dobye(0);}void quit(void) { dobye(1); Exit(0);}int logout(void) { if (!connected) { printf("Need to be connected first for `logout'.\n"); return 0; } send_do(TELOPT_LOGOUT, 1); netflush(); return 1;}/* * The ENVIRON command. */struct envcmd { const char *name; const char *help; void (*handler)(const char *, const char *); int narg;};static void env_help(const char *, const char *);typedef void (*envfunc)(const char *, const char *);struct envcmd EnvList[] = { { "define", "Define an environment variable", env_define, 2 }, { "undefine", "Undefine an environment variable", (envfunc) env_undefine, 1 }, { "export", "Mark an environment variable for automatic export", (envfunc) env_export, 1 }, { "unexport", "Don't mark an environment variable for automatic export", (envfunc) env_unexport, 1 }, { "send", "Send an environment variable", (envfunc) env_send, 1 }, { "list", "List the current environment variables", (envfunc) env_list, 0 }, { "help", 0, env_help, 0 }, { "?", "Print help information", env_help, 0 }, { 0, 0, 0, 0 },};static void env_help(const char *, const char *) { struct envcmd *c; for (c = EnvList; c->name; c++) { if (c->help) { if (*c->help) printf("%-15s %s\n", c->name, c->help); else printf("\n"); } }}static struct envcmd *getenvcmd(const char *name) { return (struct envcmd *) genget(name, (char **) EnvList, sizeof(struct envcmd));}int env_cmd(int argc, const char *argv[]) { struct envcmd *c; if (argc < 2) { fprintf(stderr, "Need an argument to 'environ' command. 'environ ?' for help.\n"); return 0; } c = getenvcmd(argv[1]); if (c == 0) { fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n", argv[1]); return 0; } if (c == AMBIGUOUS) { fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n", argv[1]); return 0; } if (c->narg + 2 != argc) { fprintf(stderr, "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n", c->narg < argc + 2 ? "only " : "", c->narg, c->narg == 1 ? "" : "s", c->name); return 0; } (*c->handler)(argv[2], argv[3]); return 1;}/* * The AUTHENTICATE command. * * auth status Display status * auth disable Disable an authentication type * auth enable Enable an authentication type * * The ENCRYPT command. * * encrypt enable Enable encryption * encrypt disable Disable encryption * encrypt type foo Set encryption type * encrypt start Start encryption * encrypt stop Stop encryption * encrypt input Start encrypting input stream * encrypt -input Stop encrypting input stream * encrypt output Start encrypting output stream * encrypt -output Stop encrypting output stream * encrypt status Print status */#ifdef TN3270char *oflgs[] = { "read-only", "write-only", "read-write" }; static void filestuff(int fd) { int res;#ifdef F_GETOWN setconnmode(0); res = fcntl(fd, F_GETOWN, 0); setcommandmode(); if (res == -1) { perror("fcntl"); return; } printf("\tOwner is %d.\n", res);#endif setconnmode(0); res = fcntl(fd, F_GETFL, 0); setcommandmode(); if (res == -1) { perror("fcntl"); return; } printf("\tFlags are 0x%x: %s\n", res, oflgs[res]);}#endif /* TN3270 *//* * Print status about the connection. */static int dostatus(int notmuch) { if (connected) { printf("Connected to %s.\n", hostname); if (!notmuch) { int mode = getconnmode(); if (my_want_state_is_will(TELOPT_LINEMODE)) { printf("Operating with LINEMODE option\n"); printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No"); printf("%s catching of signals\n", (mode&MODE_TRAPSIG) ? "Local" : "No"); slcstate();#ifdef KLUDGELINEMODE } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) { printf("Operating in obsolete linemode\n");#endif } else { printf("Operating in single character mode\n"); if (localchars) printf("Catching signals locally\n"); } printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote"); if (my_want_state_is_will(TELOPT_LFLOW)) printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No"); } } else { printf("No connection.\n"); }#if !defined(TN3270) printf("Escape character is '%s'.\n", control(escapechar)); (void) fflush(stdout);#else /* !defined(TN3270) */ if ((!In3270) && !notmuch) { printf("Escape character is '%s'.\n", control(escape)); } if ((argc >= 2) && !strcmp(argv[1], "everything")) { printf("SIGIO received %d time%s.\n", sigiocount, (sigiocount == 1)? "":"s"); if (In3270) { printf("Process ID %d, process group %d.\n", getpid(), getpgrp(getpid())); printf("Terminal input:\n"); filestuff(tin); printf("Terminal output:\n"); filestuff(tout); printf("Network socket:\n"); filestuff(net); } } if (In3270 && transcom) { printf("Transparent mode command is '%s'.\n", transcom); } fflush(stdout); if (In3270) { return 0; }#endif /* TN3270 */ return 1;}static int status(void) { int notmuch = 1; return dostatus(notmuch);}#ifdef SIGINFO/* * Function that gets called when SIGINFO is received. */void ayt_status(int) { dostatus(1);}#endifint tn(int argc, const char *argv[]) { register struct hostent *host = 0; struct sockaddr_in sn; struct servent *sp = 0; char *srp = NULL; int srlen; const char *cmd, *volatile user = 0; const char *portp = NULL; char *hostp = NULL; /* clear the socket address prior to use */ memset(&sn, 0, sizeof(sn)); if (connected) { printf("?Already connected to %s\n", hostname); return 0; } if (argc < 2) { (void) strcpy(line, "open "); printf("(to) "); (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin); makeargv(); argc = margc; argv = margv; } cmd = *argv; --argc; ++argv; while (argc) { /* * Having "telnet h" print usage is really stupid... * suppose your hostname is h? */ if (/*isprefix(*argv, "help") ||*/ isprefix(*argv, "?")) goto usage; if (strcmp(*argv, "-l") == 0) { --argc; ++argv; if (argc == 0) goto usage; user = *argv++; --argc; continue; } if (strcmp(*argv, "-a") == 0) { --argc; ++argv; autologin = 1; continue; } if (hostp == 0) { /* this leaks memory - FIXME */ hostp = strdup(*argv++); --argc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -