Rethink the whole line discipline architecture. Instead of having
[u/mdw/putty] / telnet.c
index ff6681f..3e65cd4 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -132,6 +132,8 @@ static struct Opt *opts[] = {
     &o_we_sga, &o_they_sga, NULL
 };
 
+static int echoing = TRUE, editing = TRUE;
+
 static int in_synch;
 static int sb_opt, sb_len;
 static char *sb_buf = NULL;
@@ -166,6 +168,17 @@ static void deactivate_option (struct Opt *o) {
     o->state = REALLY_INACTIVE;
 }
 
+/*
+ * Generate side effects of enabling or disabling an option.
+ */
+static void option_side_effects(struct Opt *o, int enabled) {
+    if (o->option == TELOPT_ECHO && o->send == DO)
+        echoing = !enabled;
+    else if (o->option = TELOPT_SGA && o->send == DO)
+        editing = !enabled;
+    ldisc_send(NULL, 0);               /* cause ldisc to notice the change */
+}
+
 static void activate_option (struct Opt *o) {
     if (o->send == WILL && o->option == TELOPT_NAWS)
        telnet_size();
@@ -178,8 +191,7 @@ static void activate_option (struct Opt *o) {
         */
        deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
     }
-    if (o->option == TELOPT_ECHO && cfg.ldisc_term)
-       ldisc = &ldisc_simple;
+    option_side_effects(o, 1);
 }
 
 static void refused_option (struct Opt *o) {
@@ -188,8 +200,7 @@ static void refused_option (struct Opt *o) {
        send_opt (WILL, TELOPT_OLD_ENVIRON);
        o_oenv.state = REQUESTED;
     }
-    if (o->option == TELOPT_ECHO && cfg.ldisc_term)
-       ldisc = &ldisc_term;
+    option_side_effects(o, 0);
 }
 
 static void proc_rec_opt (int cmd, int option) {
@@ -224,6 +235,7 @@ static void proc_rec_opt (int cmd, int option) {
              case ACTIVE:
                (*o)->state = INACTIVE;
                send_opt ((*o)->nsend, option);
+                option_side_effects(*o, 0);
                break;
              case INACTIVE:
              case REALLY_INACTIVE:
@@ -454,6 +466,11 @@ static void do_telnet_read (char *buf, int len) {
 }
 
 static int telnet_receive(Socket s, int urgent, char *data, int len) {
+    if (urgent==3) {
+        /* A socket error has occurred. */
+        connection_fatal(data);
+        len = 0;
+    }
     if (!len) {
        /* Connection has closed. */
        sk_close(s);
@@ -488,7 +505,7 @@ static char *telnet_init (char *host, int port, char **realhost) {
     /*
      * Open socket.
      */
-    s = sk_new(addr, port, telnet_receive);
+    s = sk_new(addr, port, 0, telnet_receive);
     if ( (err = sk_socket_error(s)) )
        return err;
 
@@ -497,15 +514,6 @@ static char *telnet_init (char *host, int port, char **realhost) {
     /*
      * Initialise option states.
      */
-    if( cfg.ldisc_term )
-    {
-       struct Opt **o;
-
-       for (o = opts; *o; o++)
-           if ((*o)->state == REQUESTED)
-               (*o)->state = INACTIVE;
-    }
-    else
     {
        struct Opt **o;
 
@@ -519,11 +527,6 @@ static char *telnet_init (char *host, int port, char **realhost) {
      */
     in_synch = FALSE;
 
-    /*
-     * We have no pre-session phase.
-     */
-    begin_session();
-
     return NULL;
 }
 
@@ -626,6 +629,12 @@ static Socket telnet_socket(void) { return s; }
 
 static int telnet_sendok(void) { return 1; }
 
+static int telnet_ldisc(int option) {
+    if (option == LD_ECHO) return echoing;
+    if (option == LD_EDIT) return editing;
+    return FALSE;
+}
+
 Backend telnet_backend = {
     telnet_init,
     telnet_send,
@@ -633,5 +642,6 @@ Backend telnet_backend = {
     telnet_special,
     telnet_socket,
     telnet_sendok,
+    telnet_ldisc,
     23
 };