Rationalise access to, and content of, backends[] array.
[u/mdw/putty] / unix / uxputty.c
1 /*
2 * Unix PuTTY main program.
3 */
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <unistd.h>
10 #include <gdk/gdk.h>
11
12 #include "putty.h"
13 #include "storage.h"
14
15 /*
16 * Stubs to avoid uxpty.c needing to be linked in.
17 */
18 const int use_pty_argv = FALSE;
19 char **pty_argv; /* never used */
20
21 /*
22 * Clean up and exit.
23 */
24 void cleanup_exit(int code)
25 {
26 /*
27 * Clean up.
28 */
29 sk_cleanup();
30 random_save_seed();
31 exit(code);
32 }
33
34 Backend *select_backend(Config *cfg)
35 {
36 Backend *back = backend_from_proto(cfg->protocol);
37 assert(back != NULL);
38 return back;
39 }
40
41 int cfgbox(Config *cfg)
42 {
43 return do_config_box("PuTTY Configuration", cfg, 0, 0);
44 }
45
46 static int got_host = 0;
47
48 const int use_event_log = 1, new_session = 1, saved_sessions = 1;
49
50 int process_nonoption_arg(char *arg, Config *cfg, int *allow_launch)
51 {
52 char *p, *q = arg;
53
54 if (got_host) {
55 /*
56 * If we already have a host name, treat this argument as a
57 * port number. NB we have to treat this as a saved -P
58 * argument, so that it will be deferred until it's a good
59 * moment to run it.
60 */
61 int ret = cmdline_process_param("-P", arg, 1, cfg);
62 assert(ret == 2);
63 } else if (!strncmp(q, "telnet:", 7)) {
64 /*
65 * If the hostname starts with "telnet:",
66 * set the protocol to Telnet and process
67 * the string as a Telnet URL.
68 */
69 char c;
70
71 q += 7;
72 if (q[0] == '/' && q[1] == '/')
73 q += 2;
74 cfg->protocol = PROT_TELNET;
75 p = q;
76 while (*p && *p != ':' && *p != '/')
77 p++;
78 c = *p;
79 if (*p)
80 *p++ = '\0';
81 if (c == ':')
82 cfg->port = atoi(p);
83 else
84 cfg->port = -1;
85 strncpy(cfg->host, q, sizeof(cfg->host) - 1);
86 cfg->host[sizeof(cfg->host) - 1] = '\0';
87 got_host = 1;
88 } else {
89 /*
90 * Otherwise, treat this argument as a host name.
91 */
92 p = arg;
93 while (*p && !isspace((unsigned char)*p))
94 p++;
95 if (*p)
96 *p++ = '\0';
97 strncpy(cfg->host, q, sizeof(cfg->host) - 1);
98 cfg->host[sizeof(cfg->host) - 1] = '\0';
99 got_host = 1;
100 }
101 if (got_host)
102 *allow_launch = TRUE;
103 return 1;
104 }
105
106 char *make_default_wintitle(char *hostname)
107 {
108 return dupcat(hostname, " - PuTTY", NULL);
109 }
110
111 /*
112 * X11-forwarding-related things suitable for Gtk app.
113 */
114
115 const char platform_x11_best_transport[] = "unix";
116
117 char *platform_get_x_display(void) {
118 const char *display;
119 /* Try to take account of --display and what have you. */
120 if (!(display = gdk_get_display()))
121 /* fall back to traditional method */
122 display = getenv("DISPLAY");
123 return dupstr(display);
124 }
125
126 int main(int argc, char **argv)
127 {
128 extern int pt_main(int argc, char **argv);
129 sk_init();
130 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
131 default_protocol = be_default_protocol;
132 /* Find the appropriate default port. */
133 {
134 Backend *b = backend_from_proto(default_protocol);
135 default_port = 0; /* illegal */
136 if (b)
137 default_port = b->default_port;
138 }
139 return pt_main(argc, argv);
140 }