Fix a few stylistic warnings from Apple's C compilers.
[u/mdw/putty] / telnet.c
index cae64d8..592a2bb 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -86,7 +86,9 @@
 #define LF 10
 #define NUL 0
 
-#define iswritable(x) ( (x) != IAC && (x) != CR )
+#define iswritable(x) \
+       ( (x) != IAC && \
+             (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
 
 static char *telopt(int opt)
 {
@@ -157,6 +159,8 @@ enum {
     OPTINDEX_ECHO,
     OPTINDEX_WE_SGA,
     OPTINDEX_THEY_SGA,
+    OPTINDEX_WE_BIN,
+    OPTINDEX_THEY_BIN,
     NUM_OPTS
 };
 
@@ -178,10 +182,14 @@ static const struct Opt o_we_sga =
     { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
 static const struct Opt o_they_sga =
     { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
+static const struct Opt o_we_bin =
+    { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE };
+static const struct Opt o_they_bin =
+    { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE };
 
 static const struct Opt *const opts[] = {
     &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
-    &o_we_sga, &o_they_sga, NULL
+    &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL
 };
 
 typedef struct telnet_tag {
@@ -521,7 +529,7 @@ static void do_telnet_read(Telnet telnet, char *buf, int len)
                else if (c == DM)
                    telnet->in_synch = 0;
 #endif
-               if (c == CR)
+               if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE)
                    telnet->state = SEENCR;
                else
                    telnet->state = TOP_LEVEL;
@@ -678,7 +686,7 @@ static char *telnet_init(void *frontend_handle, void **backend_handle,
        logevent(telnet->frontend, buf);
        sfree(buf);
     }
-    addr = sk_namelookup(host, realhost);
+    addr = name_lookup(host, port, realhost);
     if ((err = sk_addr_error(addr)))
        return err;
 
@@ -778,7 +786,8 @@ static int telnet_sendbuffer(void *handle)
 static void telnet_size(void *handle, int width, int height)
 {
     Telnet telnet = (Telnet) handle;
-    unsigned char b[16];
+    unsigned char b[24];
+    int n;
     char *logbuf;
 
     telnet->term_width = width;
@@ -786,19 +795,23 @@ static void telnet_size(void *handle, int width, int height)
 
     if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE)
        return;
-    b[0] = IAC;
-    b[1] = SB;
-    b[2] = TELOPT_NAWS;
-    b[3] = telnet->term_width >> 8;
-    b[4] = telnet->term_width & 0xFF;
-    b[5] = telnet->term_height >> 8;
-    b[6] = telnet->term_height & 0xFF;
-    b[7] = IAC;
-    b[8] = SE;
-    telnet->bufsize = sk_write(telnet->s, b, 9);
+    n = 0;
+    b[n++] = IAC;
+    b[n++] = SB;
+    b[n++] = TELOPT_NAWS;
+    b[n++] = telnet->term_width >> 8;
+    if (b[n-1] == IAC) b[n++] = IAC;   /* duplicate any IAC byte occurs */
+    b[n++] = telnet->term_width & 0xFF;
+    if (b[n-1] == IAC) b[n++] = IAC;   /* duplicate any IAC byte occurs */
+    b[n++] = telnet->term_height >> 8;
+    if (b[n-1] == IAC) b[n++] = IAC;   /* duplicate any IAC byte occurs */
+    b[n++] = telnet->term_height & 0xFF;
+    if (b[n-1] == IAC) b[n++] = IAC;   /* duplicate any IAC byte occurs */
+    b[n++] = IAC;
+    b[n++] = SE;
+    telnet->bufsize = sk_write(telnet->s, b, n);
     logbuf = dupprintf("client:\tSB NAWS %d,%d",
-                      ((unsigned char) b[3] << 8) + (unsigned char) b[4],
-                      ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
+                      telnet->term_width, telnet->term_height);
     logevent(telnet->frontend, logbuf);
     sfree(logbuf);
 }
@@ -865,7 +878,11 @@ static void telnet_special(void *handle, Telnet_Special code)
        telnet->bufsize = sk_write(telnet->s, b, 2);
        break;
       case TS_EOL:
-       telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
+       /* In BINARY mode, CR-LF becomes just CR. */
+       if (telnet->opt_states[o_we_bin.index] == ACTIVE)
+           telnet->bufsize = sk_write(telnet->s, "\r", 2);
+       else
+           telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
        break;
       case TS_SYNCH:
        b[1] = DM;