]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Rationalise access to, and content of, backends[] array.
authorJacob Nevins <jacobn@chiark.greenend.org.uk>
Sat, 30 Jun 2007 21:56:44 +0000 (21:56 +0000)
committerJacob Nevins <jacobn@chiark.greenend.org.uk>
Sat, 30 Jun 2007 21:56:44 +0000 (21:56 +0000)
Should be no significant change in behaviour.
(Well, entering usernames containing commas on Plink's command line will be
a little harder now.)

[originally from svn r7628]

23 files changed:
be_all.c
be_all_s.c
be_none.c
be_nos_s.c
be_nossh.c
config.c
mac/mac.c
mac/macterm.c
macosx/osxwin.m
putty.h
raw.c
rlogin.c
settings.c
ssh.c
telnet.c
testback.c
unix/uxplink.c
unix/uxpty.c
unix/uxputty.c
unix/uxser.c
windows/window.c
windows/winplink.c
windows/winser.c

index 024a39e9c70229afee4f9466b61f557157120f52..c58903cc50d8f782a8b60be71cbc3606e246990e 100644 (file)
--- a/be_all.c
+++ b/be_all.c
@@ -22,10 +22,10 @@ const int be_default_protocol = PROT_TELNET;
 const int be_default_protocol = PROT_SSH;
 #endif
 
-struct backend_list backends[] = {
-    {PROT_SSH, "ssh", &ssh_backend},
-    {PROT_TELNET, "telnet", &telnet_backend},
-    {PROT_RLOGIN, "rlogin", &rlogin_backend},
-    {PROT_RAW, "raw", &raw_backend},
-    {0, NULL}
+Backend *backends[] = {
+    &ssh_backend,
+    &telnet_backend,
+    &rlogin_backend,
+    &raw_backend,
+    NULL
 };
index 4b3c19837261c44930fe3cf4b85bbff9eed29ccc..0ffd073767dbe3df5354ca1aa6c086a4c68c6e42 100644 (file)
@@ -22,11 +22,11 @@ const int be_default_protocol = PROT_TELNET;
 const int be_default_protocol = PROT_SSH;
 #endif
 
-struct backend_list backends[] = {
-    {PROT_SSH, "ssh", &ssh_backend},
-    {PROT_TELNET, "telnet", &telnet_backend},
-    {PROT_RLOGIN, "rlogin", &rlogin_backend},
-    {PROT_RAW, "raw", &raw_backend},
-    {PROT_SERIAL, "serial", &serial_backend},
-    {0, NULL}
+Backend *backends[] = {
+    &ssh_backend,
+    &telnet_backend,
+    &rlogin_backend,
+    &raw_backend,
+    &serial_backend,
+    NULL
 };
index 879a20c5052285efcc88aab7047c4cbf16d6561a..6ec037ac06753543778b7ea022b480ab531bc539 100644 (file)
--- a/be_none.c
+++ b/be_none.c
@@ -1,16 +1,11 @@
 /*
- * Linking module for PSCP: list the available backends, but
- * without accompanying function suites. Used only for name
- * lookups.
+ * Linking module for programs that do not support selection of backend
+ * (such as pscp or pterm).
  */
 
 #include <stdio.h>
 #include "putty.h"
 
-struct backend_list backends[] = {
-    {PROT_SSH, "ssh", NULL},
-    {PROT_TELNET, "telnet", NULL},
-    {PROT_RLOGIN, "rlogin", NULL},
-    {PROT_RAW, "raw", NULL},
-    {0, NULL}
+Backend *backends[] = {
+    NULL
 };
index ea8cf1441c9e08ca95fec0de350537500a945057..a574ead915a28be939a5b0fb68ce234fc650307f 100644 (file)
@@ -10,12 +10,12 @@ const int be_default_protocol = PROT_TELNET;
 
 const char *const appname = "PuTTYtel";
 
-struct backend_list backends[] = {
-    {PROT_TELNET, "telnet", &telnet_backend},
-    {PROT_RLOGIN, "rlogin", &rlogin_backend},
-    {PROT_RAW, "raw", &raw_backend},
-    {PROT_SERIAL, "serial", &serial_backend},
-    {0, NULL}
+Backend *backends[] = {
+    &telnet_backend,
+    &rlogin_backend,
+    &raw_backend,
+    &serial_backend,
+    NULL
 };
 
 /*
index 18ba32a7926cdb11e4dcef023b368583cf102ccf..33d783a841b531ed3a3a242e60cb4fb995404f47 100644 (file)
@@ -10,11 +10,11 @@ const int be_default_protocol = PROT_TELNET;
 
 const char *const appname = "PuTTYtel";
 
-struct backend_list backends[] = {
-    {PROT_TELNET, "telnet", &telnet_backend},
-    {PROT_RLOGIN, "rlogin", &rlogin_backend},
-    {PROT_RAW, "raw", &raw_backend},
-    {0, NULL}
+Backend *backends[] = {
+    &telnet_backend,
+    &rlogin_backend,
+    &raw_backend,
+    NULL
 };
 
 /*
index 41bb4943e3a113cec2ba7287c6fdd5ec39eb63ab..39740b8d71ed4c3ec74a03908160cb3ca9c82242 100644 (file)
--- a/config.c
+++ b/config.c
 #define HOST_BOX_TITLE "Host Name (or IP address)"
 #define PORT_BOX_TITLE "Port"
 
-/*
- * Convenience function: determine whether this binary supports a
- * given backend.
- */
-static int have_backend(int protocol)
-{
-    struct backend_list *p = backends;
-    for (p = backends; p->name; p++) {
-       if (p->protocol == protocol)
-           return 1;
-    }
-    return 0;
-}
-
 static void config_host_handler(union control *ctrl, void *dlg,
                                void *data, int event)
 {
@@ -1166,7 +1152,7 @@ void setup_config_box(struct controlbox *b, int midsession,
        hp->port = c;
        ctrl_columns(s, 1, 100);
 
-       if (!have_backend(PROT_SSH)) {
+       if (!backend_from_proto(PROT_SSH)) {
            ctrl_radiobuttons(s, "Connection type:", NO_SHORTCUT, 3,
                              HELPCTX(session_hostname),
                              config_protocolbuttons_handler, P(hp),
@@ -1257,7 +1243,7 @@ void setup_config_box(struct controlbox *b, int midsession,
     {
        char *sshlogname, *sshrawlogname;
        if ((midsession && protocol == PROT_SSH) ||
-           (!midsession && have_backend(PROT_SSH))) {
+           (!midsession && backend_from_proto(PROT_SSH))) {
            sshlogname = "SSH packets";
            sshrawlogname = "SSH packets and raw data";
         } else {
@@ -1293,7 +1279,7 @@ void setup_config_box(struct controlbox *b, int midsession,
                 dlg_stdcheckbox_handler, I(offsetof(Config,logflush)));
 
     if ((midsession && protocol == PROT_SSH) ||
-       (!midsession && have_backend(PROT_SSH))) {
+       (!midsession && backend_from_proto(PROT_SSH))) {
        s = ctrl_getset(b, "Session/Logging", "ssh",
                        "Options specific to SSH packet logging");
        ctrl_checkbox(s, "Omit known password fields", 'k',
@@ -1912,7 +1898,7 @@ void setup_config_box(struct controlbox *b, int midsession,
      * when we're not doing SSH.
      */
 
-    if (have_backend(PROT_SSH) && (!midsession || protocol == PROT_SSH)) {
+    if (backend_from_proto(PROT_SSH) && (!midsession || protocol == PROT_SSH)) {
 
        /*
         * The Connection/SSH panel.
index 9e1d1f30e2418fbdfd07ffbdc54ba8b127f508b7..25362061a210860eb29b1d2ed5601e803b4ee04b 100644 (file)
--- a/mac/mac.c
+++ b/mac/mac.c
@@ -210,13 +210,10 @@ static void mac_startup(void) {
     default_protocol = be_default_protocol;
     /* Find the appropriate default port. */
     {
-       int i;
+       Backend *b = backend_from_proto(default_protocol);
        default_port = 0; /* illegal */
-       for (i = 0; backends[i].backend != NULL; i++)
-           if (backends[i].protocol == default_protocol) {
-               default_port = backends[i].backend->default_port;
-               break;
-           }
+       if (b)
+           default_port = b->default_port;
     }
     flags = FLAG_INTERACTIVE;
 
index dad2688336251bbffa43c4447d4dd8c008cac163..4a75a0c6018be939f8a8108ca05c3be1e604e343 100644 (file)
@@ -115,12 +115,7 @@ void mac_startsession(Session *s)
      * Select protocol. This is farmed out into a table in a
      * separate file to enable an ssh-free variant.
      */
-    s->back = NULL;
-    for (i = 0; backends[i].backend != NULL; i++)
-       if (backends[i].protocol == s->cfg.protocol) {
-           s->back = backends[i].backend;
-           break;
-       }
+    s->back = backend_from_proto(s->cfg.protocol);
     if (s->back == NULL)
        fatalbox("Unsupported protocol number found");
 
index 361f548f246f361e6397e270c3cacc7833b4f640..f61e6bff73a9aee093e06cafb3cba06e08fe7872 100644 (file)
     /*
      * Set up a backend.
      */
-    {
-       int i;
+    back = backend_from_proto(cfg.protocol);
+    if (!back)
        back = &pty_backend;
-       for (i = 0; backends[i].backend != NULL; i++)
-           if (backends[i].protocol == cfg.protocol) {
-               back = backends[i].backend;
-               break;
-           }
-    }
 
     {
        const char *error;
diff --git a/putty.h b/putty.h
index 3583ecf053aed9c70b646d2d9608a0e71a4fd2fc..30b23bf50cb4201cd104cb62749da64944946332 100644 (file)
--- a/putty.h
+++ b/putty.h
@@ -389,14 +389,12 @@ struct backend_tag {
      */
     void (*unthrottle) (void *handle, int);
     int (*cfg_info) (void *handle);
+    char *name;
+    int protocol;
     int default_port;
 };
 
-extern struct backend_list {
-    int protocol;
-    char *name;
-    Backend *backend;
-} backends[];
+extern Backend *backends[];
 
 /*
  * Suggested default protocol provided by the backend link module.
@@ -778,6 +776,8 @@ void random_destroy_seed(void);
 /*
  * Exports from settings.c.
  */
+Backend *backend_from_name(const char *name);
+Backend *backend_from_proto(int proto);
 char *save_settings(char *section, Config * cfg);
 void save_open_settings(void *sesskey, Config *cfg);
 void load_settings(char *section, Config * cfg);
diff --git a/raw.c b/raw.c
index d64b6d7f7a1e44528ac77e50bfc477f5b3a4a1d3..49ca1ce2484ea4e17359bfa1125c26be96c2e56a 100644 (file)
--- a/raw.c
+++ b/raw.c
@@ -278,5 +278,7 @@ Backend raw_backend = {
     raw_provide_logctx,
     raw_unthrottle,
     raw_cfg_info,
+    "raw",
+    PROT_RAW,
     1
 };
index 833d4ea4db39005127e110601c55b47030414156..f9a546e929d7cd7161b45cd798de0ca4300ea650 100644 (file)
--- a/rlogin.c
+++ b/rlogin.c
@@ -349,5 +349,7 @@ Backend rlogin_backend = {
     rlogin_provide_logctx,
     rlogin_unthrottle,
     rlogin_cfg_info,
+    "rlogin",
+    PROT_RLOGIN,
     1
 };
index c937d3c9672b07961a887effad146637f93beb9f..44b02cbab5a02c8ee79a4e9813b52a286dac66bf 100644 (file)
@@ -52,6 +52,29 @@ const char *const ttymodes[] = {
     "CS8",     "PARENB",   "PARODD",   NULL
 };
 
+/*
+ * Convenience functions to access the backends[] array
+ * (which is only present in tools that manage settings).
+ */
+
+Backend *backend_from_name(const char *name)
+{
+    Backend **p;
+    for (p = backends; *p != NULL; p++)
+       if (!strcmp((*p)->name, name))
+           return *p;
+    return NULL;
+}
+
+Backend *backend_from_proto(int proto)
+{
+    Backend **p;
+    for (p = backends; *p != NULL; p++)
+       if ((*p)->protocol == proto)
+           return *p;
+    return NULL;
+}
+
 static void gpps(void *handle, const char *name, const char *def,
                 char *val, int len)
 {
@@ -259,11 +282,11 @@ void save_open_settings(void *sesskey, Config *cfg)
     write_setting_i(sesskey, "SSHLogOmitPasswords", cfg->logomitpass);
     write_setting_i(sesskey, "SSHLogOmitData", cfg->logomitdata);
     p = "raw";
-    for (i = 0; backends[i].name != NULL; i++)
-       if (backends[i].protocol == cfg->protocol) {
-           p = backends[i].name;
-           break;
-       }
+    {
+       const Backend *b = backend_from_proto(cfg->protocol);
+       if (b)
+           p = b->name;
+    }
     write_setting_s(sesskey, "Protocol", p);
     write_setting_i(sesskey, "PortNumber", cfg->port);
     /* The CloseOnExit numbers are arranged in a different order from
@@ -476,12 +499,13 @@ void load_open_settings(void *sesskey, Config *cfg)
     gpps(sesskey, "Protocol", "default", prot, 10);
     cfg->protocol = default_protocol;
     cfg->port = default_port;
-    for (i = 0; backends[i].name != NULL; i++)
-       if (!strcmp(prot, backends[i].name)) {
-           cfg->protocol = backends[i].protocol;
+    {
+       const Backend *b = backend_from_name(prot);
+       if (b) {
+           cfg->protocol = b->protocol;
            gppi(sesskey, "PortNumber", default_port, &cfg->port);
-           break;
        }
+    }
 
     /* Address family selection */
     gppi(sesskey, "AddressFamily", ADDRTYPE_UNSPEC, &cfg->addressfamily);
diff --git a/ssh.c b/ssh.c
index 5196b1b3415a3d84b60912e741a9380b98052a72..e5d2364d3d56d38ae7851d02625e3fdb9d61e5bb 100644 (file)
--- a/ssh.c
+++ b/ssh.c
@@ -9146,5 +9146,7 @@ Backend ssh_backend = {
     ssh_provide_logctx,
     ssh_unthrottle,
     ssh_cfg_info,
+    "ssh",
+    PROT_SSH,
     22
 };
index e6ad83e06d80b1840093adc95c37ee83779ab4b5..eeaa76f305bc33d8d606e77a1f32c0e63839eaf9 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -1106,5 +1106,7 @@ Backend telnet_backend = {
     telnet_provide_logctx,
     telnet_unthrottle,
     telnet_cfg_info,
+    "telnet",
+    PROT_TELNET,
     23
 };
index 159cdc54fb9e51adc63900e44e17d113a6402a49..0175e8e7b383baef173050b391b170ffed5dccfb 100644 (file)
@@ -59,14 +59,14 @@ Backend null_backend = {
     null_init, null_free, null_reconfig, null_send, null_sendbuffer, null_size,
     null_special, null_get_specials, null_connected, null_exitcode, null_sendok,
     null_ldisc, null_provide_ldisc, null_provide_logctx, null_unthrottle,
-    null_cfg_info, 0
+    null_cfg_info, "null", -1, 0
 };
 
 Backend loop_backend = {
     loop_init, loop_free, null_reconfig, loop_send, null_sendbuffer, null_size,
     null_special, null_get_specials, null_connected, null_exitcode, null_sendok,
     null_ldisc, null_provide_ldisc, null_provide_logctx, null_unthrottle,
-    null_cfg_info, 0
+    null_cfg_info, "loop", -1, 0
 };
 
 struct loop_state {
index e9c6c9e6eedae82017dee5849622d991c30b4002..6b56d2af1fc5d61db6b7102006a91265b8921639 100644 (file)
@@ -596,15 +596,11 @@ int main(int argc, char **argv)
         * Override the default protocol if PLINK_PROTOCOL is set.
         */
        char *p = getenv("PLINK_PROTOCOL");
-       int i;
        if (p) {
-           for (i = 0; backends[i].backend != NULL; i++) {
-               if (!strcmp(backends[i].name, p)) {
-                   default_protocol = cfg.protocol = backends[i].protocol;
-                   default_port = cfg.port =
-                       backends[i].backend->default_port;
-                   break;
-               }
+           const Backend *b = backend_from_name(p);
+           if (b) {
+               default_protocol = cfg.protocol = b->protocol;
+               default_port = cfg.port = b->default_port;
            }
        }
     }
@@ -681,19 +677,14 @@ int main(int argc, char **argv)
                     */
                    r = strchr(p, ',');
                    if (r) {
-                       int i, j;
-                       for (i = 0; backends[i].backend != NULL; i++) {
-                           j = strlen(backends[i].name);
-                           if (j == r - p &&
-                               !memcmp(backends[i].name, p, j)) {
-                               default_protocol = cfg.protocol =
-                                   backends[i].protocol;
-                               portnumber =
-                                   backends[i].backend->default_port;
-                               p = r + 1;
-                               break;
-                           }
+                       const Backend *b;
+                       *r = '\0';
+                       b = backend_from_name(p);
+                       if (b) {
+                           default_protocol = cfg.protocol = b->protocol;
+                           portnumber = b->default_port;
                        }
+                       p = r + 1;
                    }
 
                    /*
@@ -836,19 +827,11 @@ int main(int argc, char **argv)
      * Select protocol. This is farmed out into a table in a
      * separate file to enable an ssh-free variant.
      */
-    {
-       int i;
-       back = NULL;
-       for (i = 0; backends[i].backend != NULL; i++)
-           if (backends[i].protocol == cfg.protocol) {
-               back = backends[i].backend;
-               break;
-           }
-       if (back == NULL) {
-           fprintf(stderr,
-                   "Internal fault: Unsupported protocol found\n");
-           return 1;
-       }
+    back = backend_from_proto(cfg.protocol);
+    if (back == NULL) {
+       fprintf(stderr,
+               "Internal fault: Unsupported protocol found\n");
+       return 1;
     }
 
     /*
index 708e82d6be224bb354a911dc78693db7441f81cd..2e165cf2e9451e8d71f2d9dddeeac10197378eed 100644 (file)
@@ -1085,5 +1085,7 @@ Backend pty_backend = {
     pty_provide_logctx,
     pty_unthrottle,
     pty_cfg_info,
+    "pty",
+    -1,
     1
 };
index 54e0d71bde7302aba40b33721a1023f369a090f1..f42c428d6e4864713e634e2cf83ed6ad799403c2 100644 (file)
@@ -33,13 +33,7 @@ void cleanup_exit(int code)
 
 Backend *select_backend(Config *cfg)
 {
-    int i;
-    Backend *back = NULL;
-    for (i = 0; backends[i].backend != NULL; i++)
-       if (backends[i].protocol == cfg->protocol) {
-           back = backends[i].backend;
-           break;
-       }
+    Backend *back = backend_from_proto(cfg->protocol);
     assert(back != NULL);
     return back;
 }
@@ -137,13 +131,10 @@ int main(int argc, char **argv)
     default_protocol = be_default_protocol;
     /* Find the appropriate default port. */
     {
-       int i;
+       Backend *b = backend_from_proto(default_protocol);
        default_port = 0; /* illegal */
-       for (i = 0; backends[i].backend != NULL; i++)
-           if (backends[i].protocol == default_protocol) {
-               default_port = backends[i].backend->default_port;
-               break;
-           }
+       if (b)
+           default_port = b->default_port;
     }
     return pt_main(argc, argv);
 }
index 24b0124cb4aba9fad9735f1a3901bd8e6b8c2477..a12fdec76add7098a3e8b622a3845e1bc9200d9a 100644 (file)
@@ -536,5 +536,7 @@ Backend serial_backend = {
     serial_provide_logctx,
     serial_unthrottle,
     serial_cfg_info,
+    "serial",
+    PROT_SERIAL,
     1
 };
index 1df5feebe0d0931522e522ed23df680eef889a51..1d4b7152eb36066a9f42a5dce82cb5afe6f81e19 100644 (file)
@@ -219,12 +219,7 @@ static void start_backend(void)
      * Select protocol. This is farmed out into a table in a
      * separate file to enable an ssh-free variant.
      */
-    back = NULL;
-    for (i = 0; backends[i].backend != NULL; i++)
-       if (backends[i].protocol == cfg.protocol) {
-           back = backends[i].backend;
-           break;
-       }
+    back = backend_from_proto(cfg.protocol);
     if (back == NULL) {
        char *str = dupprintf("%s Internal Error", appname);
        MessageBox(NULL, "Unsupported protocol number found",
@@ -369,13 +364,10 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
        default_protocol = be_default_protocol;
        /* Find the appropriate default port. */
        {
-           int i;
+           Backend *b = backend_from_proto(default_protocol);
            default_port = 0; /* illegal */
-           for (i = 0; backends[i].backend != NULL; i++)
-               if (backends[i].protocol == default_protocol) {
-                   default_port = backends[i].backend->default_port;
-                   break;
-               }
+           if (b)
+               default_port = b->default_port;
        }
        cfg.logtype = LGTYP_NONE;
 
index 424260000682075516ceb9c4c33c66f0fc1e85a4..60232ffe7f85ad88d182fad78b4b0e739a34400b 100644 (file)
@@ -307,13 +307,10 @@ int main(int argc, char **argv)
        char *p = getenv("PLINK_PROTOCOL");
        int i;
        if (p) {
-           for (i = 0; backends[i].backend != NULL; i++) {
-               if (!strcmp(backends[i].name, p)) {
-                   default_protocol = cfg.protocol = backends[i].protocol;
-                   default_port = cfg.port =
-                       backends[i].backend->default_port;
-                   break;
-               }
+           const Backend *b = backend_from_name(p);
+           if (b) {
+               default_protocol = cfg.protocol = b->protocol;
+               default_port = cfg.port = b->default_port;
            }
        }
     }
@@ -380,19 +377,14 @@ int main(int argc, char **argv)
                     */
                    r = strchr(p, ',');
                    if (r) {
-                       int i, j;
-                       for (i = 0; backends[i].backend != NULL; i++) {
-                           j = strlen(backends[i].name);
-                           if (j == r - p &&
-                               !memcmp(backends[i].name, p, j)) {
-                               default_protocol = cfg.protocol =
-                                   backends[i].protocol;
-                               portnumber =
-                                   backends[i].backend->default_port;
-                               p = r + 1;
-                               break;
-                           }
+                       const Backend *b;
+                       *r = '\0';
+                       b = backend_from_name(p);
+                       if (b) {
+                           default_protocol = cfg.protocol = b->protocol;
+                           portnumber = b->default_port;
                        }
+                       p = r + 1;
                    }
 
                    /*
@@ -535,19 +527,11 @@ int main(int argc, char **argv)
      * Select protocol. This is farmed out into a table in a
      * separate file to enable an ssh-free variant.
      */
-    {
-       int i;
-       back = NULL;
-       for (i = 0; backends[i].backend != NULL; i++)
-           if (backends[i].protocol == cfg.protocol) {
-               back = backends[i].backend;
-               break;
-           }
-       if (back == NULL) {
-           fprintf(stderr,
-                   "Internal fault: Unsupported protocol found\n");
-           return 1;
-       }
+    back = backend_from_proto(cfg.protocol);
+    if (back == NULL) {
+       fprintf(stderr,
+               "Internal fault: Unsupported protocol found\n");
+       return 1;
     }
 
     /*
index 9e6415e3654855b7ec724fb78b50c1cf8c5cab0a..1188c46a2cce459dd94b13c0689c1d3b8e6b2835 100644 (file)
@@ -454,5 +454,7 @@ Backend serial_backend = {
     serial_provide_logctx,
     serial_unthrottle,
     serial_cfg_info,
+    "serial",
+    PROT_SERIAL,
     1
 };