Rationalise access to, and content of, backends[] array.
[u/mdw/putty] / unix / uxputty.c
CommitLineData
1d009ae7 1/*
2 * Unix PuTTY main program.
3 */
4
5#include <stdio.h>
46a3419b 6#include <ctype.h>
5bf9955d 7#include <stdlib.h>
1d009ae7 8#include <assert.h>
1d009ae7 9#include <unistd.h>
46ed7b64 10#include <gdk/gdk.h>
1d009ae7 11
12#include "putty.h"
13#include "storage.h"
14
15/*
fbcc43d3 16 * Stubs to avoid uxpty.c needing to be linked in.
17 */
18const int use_pty_argv = FALSE;
19char **pty_argv; /* never used */
20
21/*
1d009ae7 22 * Clean up and exit.
23 */
24void cleanup_exit(int code)
25{
26 /*
27 * Clean up.
28 */
29 sk_cleanup();
30 random_save_seed();
31 exit(code);
32}
33
1d009ae7 34Backend *select_backend(Config *cfg)
35{
9e164d82 36 Backend *back = backend_from_proto(cfg->protocol);
1d009ae7 37 assert(back != NULL);
38 return back;
39}
40
41int cfgbox(Config *cfg)
42{
f89c3294 43 return do_config_box("PuTTY Configuration", cfg, 0, 0);
1d009ae7 44}
45
46a3419b 46static int got_host = 0;
47
56801e3d 48const int use_event_log = 1, new_session = 1, saved_sessions = 1;
8eed910d 49
df7c7e52 50int process_nonoption_arg(char *arg, Config *cfg, int *allow_launch)
46a3419b 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 */
320cb4be 92 p = arg;
46a3419b 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 }
df7c7e52 101 if (got_host)
102 *allow_launch = TRUE;
46a3419b 103 return 1;
104}
105
10705014 106char *make_default_wintitle(char *hostname)
107{
108 return dupcat(hostname, " - PuTTY", NULL);
109}
110
46ed7b64 111/*
112 * X11-forwarding-related things suitable for Gtk app.
113 */
114
115const char platform_x11_best_transport[] = "unix";
116
117char *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
1d009ae7 126int 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 {
9e164d82 134 Backend *b = backend_from_proto(default_protocol);
1d009ae7 135 default_port = 0; /* illegal */
9e164d82 136 if (b)
137 default_port = b->default_port;
1d009ae7 138 }
139 return pt_main(argc, argv);
140}