- static char tnet[128];
- register int i, c, x;
- register u_char *rcp;
- int off, first = 1;
- u_char *osp;
-
- off = 0;
- x = 0;
-
- while (length > 0 && *sp == IAC) {
- osp = (u_char *) sp;
- tnet[0] = '\0';
-
- c = *sp++;
- length--;
- switch (*sp) {
- case IAC: /* <IAC><IAC>! */
- if (length > 1 && sp[1] == IAC) {
- (void)strlcpy(tnet, "IAC IAC", sizeof(tnet));
- } else {
- length = 0;
- continue;
- }
+ int i, c, x;
+ const u_char *osp, *p;
+#define PEEK(c, sp, length) \
+ do { \
+ if (length < 1) \
+ goto trunc; \
+ c = *sp; \
+ } while (0)
+#define FETCH(c, sp, length) \
+ do { \
+ PEEK((c), (sp), (length)); \
+ sp++; \
+ length--; \
+ } while (0)
+
+ osp = sp;
+
+ FETCH(c, sp, length);
+ if (c != IAC)
+ goto trunc;
+ FETCH(c, sp, length);
+ if (c == IAC) { /* <IAC><IAC>! */
+ if (print)
+ printf("IAC IAC");
+ goto done;
+ }
+
+ i = c - TELCMD_FIRST;
+ if (i < 0 || i > IAC - TELCMD_FIRST) {
+ (void)printf("unknown: ff%02x\n", c);
+ goto trunc;
+ }
+
+ switch (c) {
+ case DONT:
+ case DO:
+ case WONT:
+ case WILL:
+ case SB:
+ /* DONT/DO/WONT/WILL x */
+ FETCH(x, sp, length);
+ if (x >= 0 && x < NTELOPTS) {
+ if (print)
+ (void)printf("%s %s", telcmds[i], telopts[x]);
+ } else {
+ if (print)
+ (void)printf("%s %#x", telcmds[i], x);
+ }
+ if (c != SB)