📄 gaptelnet.c
字号:
if (argc == 1) { printf("Commands may be abbreviated. Commands are:\n\n"); for (c = cmdtab; c->name; c++) printf("%-*s\t%s\n", HELPINDENT, c->name, c->help); return; } while (--argc > 0) { register char *arg; arg = *++argv; c = getcmd(arg); if (c == (struct cmd *)-1) printf("?Ambiguous help command %s\n", arg); else if (c == (struct cmd *)0) printf("?Invalid help command %s\n", arg); else printf("%s\n", c->help); }}/* * Call routine with argc, argv set from args (terminated by 0). * VARARGS2 */call(routine, args) int (*routine)(); int args;{ register int *argp; register int argc; for (argc = 0, argp = &args; *argp++ != 0; argc++) ; (*routine)(argc, &args);}struct tchars notc = { -1, -1, -1, -1, -1, -1 };struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };mode(f) register int f;{ static int prevmode = 0; struct tchars *tc; struct ltchars *ltc; struct sgttyb sb; int onoff, old; if (prevmode == f) return (f); old = prevmode; prevmode = f; sb = ottyb; switch (f) { case 0: onoff = 0; tc = &otc; ltc = &oltc; break; case 1: case 2: sb.sg_flags |= CBREAK; if (f == 1) sb.sg_flags &= ~(ECHO|CRMOD); else sb.sg_flags |= ECHO|CRMOD; sb.sg_erase = sb.sg_kill = -1; tc = ¬c; ltc = &noltc; onoff = 1; break; default: return (old); } ioctl(fileno(stdin), TIOCSLTC, (char *)ltc); ioctl(fileno(stdin), TIOCSETC, (char *)tc); ioctl(fileno(stdin), TIOCSETP, (char *)&sb); ioctl(fileno(stdin), FIONBIO, &onoff); ioctl(fileno(stdout), FIONBIO, &onoff); return (old);}struct {struct sphdr hdr; char data[BUFSIZ];} sibuf;char *sbp;char tibuf[BUFSIZ], *tbp;int scc, tcc;/* * Select from tty and network... */telnet(s) int s;{ register int c; int tin = fileno(stdin), tout = fileno(stdout); int on = 1; int ibits, obits; (void) mode(1); ioctl(s, FIONBIO, &on); changeSPPopts(net, GAPCTLnone, 1); /* datastream "normal", eom */ for (;;) { ibits = obits = 0; if (nfrontp - nbackp) obits |= (1 << s); else ibits |= (1 << tin); if (tfrontp - tbackp) obits |= (1 << tout); else ibits |= (1 << s); if (scc < 0 && tcc < 0) break; select(16, &ibits, &obits, 0, 0); if (ibits == 0 && obits == 0) { sleep(5); continue; } /* * Something to read from the network... */ if (ibits & (1 << s)) { scc = read(s, &sibuf, sizeof (sibuf)) - sizeof(struct sphdr);#ifdef DEBUG if (debug) printf("reading %d bytes from net\n", scc);#endif if (scc < 0 && errno == EWOULDBLOCK) scc = 0; else if (scc < 0) break; /* protocol violation? */ else if (sibuf.hdr.sp_cc & SP_OB) { /* status or OOB control */ switch ((u_char) *sibuf.data) { case GAPCTLareYouThere: sendoobdata(GAPCTLiAmHere); break; case GAPCTLmediumDown: (void) mode(0); longjmp(peerdied, -1); /*NOTREACHED*/ default: /* ignore others */ break; } scc = 0; } else if (sibuf.hdr.sp_dt == GAPCTLnone || sibuf.hdr.sp_dt == 0) { /* normal case, plus Lisp bogosity */ sbp = sibuf.data; } else if(sibuf.hdr.sp_dt == GAPCTLcleanup){ sendoobdata(GAPCTLcleanup); /* should get an END next */ scc = 0; } else if(sibuf.hdr.sp_dt == SPPSST_END) { setsockopt(net, NSPROTO_SPP, SO_HEADERS_ON_OUTPUT, &on, sizeof(on)); sppclosereply(net); (void) mode(0); longjmp(peerdied, -1); /*NOTREACHED*/ } else scc = 0; /* ignore other inband controls */ } /* * Something to read from the tty... */ if (ibits & (1 << tin)) { tcc = read(tin, tibuf, sizeof (tibuf)); if (tcc < 0 && errno == EWOULDBLOCK) tcc = 0; else { if (tcc <= 0) break; tbp = tibuf; } } while (tcc > 0) { register int c; if ((&netobuf[BUFSIZ] - nfrontp) < 2) break; c = *tbp++ & 0377, tcc--; if (strip(c) == escape) { command(0); tcc = 0; break; } /* We don't do any input translation at the moment */#ifdef notdef switch (c) { case '\n': *nfrontp++ = '\r'; *nfrontp++ = '\n'; break; case '\r': *nfrontp++ = '\r'; *nfrontp++ = '\n'; break; default: *nfrontp++ = c; break; }#else *nfrontp++ = c;#endif /* notdef */ } if ((obits & (1 << s)) && (nfrontp - nbackp) > 0) netflush(s); while (scc > 0) { register int c; c = *sbp++&0377; scc--; /* nor do we do any output translation */ *tfrontp++ = c; } if ((obits & (1 << tout)) && (tfrontp - tbackp) > 0) ttyflush(tout); } (void) mode(0);}command(top) int top;{ register struct cmd *c; int oldmode, wasopen; oldmode = mode(0); if (!top) putchar('\n'); else signal(SIGINT, SIG_DFL); for (;;) { printf("%s> ", prompt); if (gets(line) == 0) { if (feof(stdin)) { clearerr(stdin); putchar('\n'); } break; } if (line[0] == 0) break; makeargv(); c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); continue; } if (c == 0) { printf("?Invalid command\n"); continue; } (*c->handler)(margc, margv); if (c->handler != help) break; } if (!top) { if (!connected) longjmp(toplevel, 1); (void) mode(oldmode); }}/* * Set the escape character. */setescape(argc, argv) int argc; char *argv[];{ register char *arg; char buf[50]; if (argc > 2) arg = argv[1]; else { printf("new escape character: "); gets(buf); arg = buf; } if (arg[0] != '\0') escape = arg[0]; printf("Escape character is '%s'.\n", control(escape)); fflush(stdout);}/* * Construct a control character sequence * for a special character. */char *control(c) register int c;{ static char buf[3]; if (c == 0177) return ("^?"); if (c >= 040) { buf[0] = c; buf[1] = 0; } else { buf[0] = '^'; buf[1] = '@'+c; buf[2] = 0; } return (buf);}struct cmd *getcmd(name) register char *name;{ register char *p, *q; register struct cmd *c, *found; register int nmatches, longest; longest = 0; nmatches = 0; found = 0; for (c = cmdtab; p = c->name; c++) { for (q = name; *q == *p++; q++) if (*q == 0) /* exact match? */ return (c); if (!*q) { /* the name was a prefix */ if (q - name > longest) { longest = q - name; nmatches = 1; found = c; } else if (q - name == longest) nmatches++; } } if (nmatches > 1) return ((struct cmd *)-1); return (found);}deadpeer(){ (void) mode(0); longjmp(peerdied, -1);}intr(){ (void) mode(0); longjmp(toplevel, -1);}ttyflush(fd){ register int n; if ((n = tfrontp - tbackp) > 0) { if (logfile != (FILE*)0) fwrite(tbackp, 1, n, logfile); n = write(fd, tbackp, n); } if (n < 0) return; tbackp += n; if (tbackp == tfrontp) tbackp = tfrontp = ttyobuf;}netflush(fd){ int n; if ((n = nfrontp - nbackp) > 0) n = write(fd, nbackp, n);#ifdef DEBUG if (debug) printf("writing %d of %d bytes to net\n", n, nfrontp-nbackp);#endif if (n < 0) { if (errno != ENOBUFS && errno != EWOULDBLOCK) { (void) mode(0); perror(hostname); close(fd); longjmp(peerdied, -1); /*NOTREACHED*/ } n = 0; } nbackp += n; if (nbackp == nfrontp) nbackp = nfrontp = netobuf;}/* * Send out of band data to other end of network */sendoobdata(value) char value;{ send(net, &value, 1, MSG_OOB);}changeSPPopts(s, stream, eom) int s; /* SPP socket */ u_char stream; /* datastream type */ char eom; /* Boolean EOM */{ struct sphdr sphdr; int off = 0; sphdr.sp_dt = stream; sphdr.sp_cc = (eom ? SP_EM : 0); setsockopt(s, NSPROTO_SPP, SO_HEADERS_ON_OUTPUT, &off, sizeof(off)); setsockopt(s, NSPROTO_SPP, SO_DEFAULT_HEADERS, &sphdr, sizeof(sphdr));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -