]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxputty.c
54e0d71bde7302aba40b33721a1023f369a090f1
[PuTTY.git] / 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     int i;
37     Backend *back = NULL;
38     for (i = 0; backends[i].backend != NULL; i++)
39         if (backends[i].protocol == cfg->protocol) {
40             back = backends[i].backend;
41             break;
42         }
43     assert(back != NULL);
44     return back;
45 }
46
47 int cfgbox(Config *cfg)
48 {
49     return do_config_box("PuTTY Configuration", cfg, 0, 0);
50 }
51
52 static int got_host = 0;
53
54 const int use_event_log = 1, new_session = 1, saved_sessions = 1;
55
56 int process_nonoption_arg(char *arg, Config *cfg, int *allow_launch)
57 {
58     char *p, *q = arg;
59
60     if (got_host) {
61         /*
62          * If we already have a host name, treat this argument as a
63          * port number. NB we have to treat this as a saved -P
64          * argument, so that it will be deferred until it's a good
65          * moment to run it.
66          */
67         int ret = cmdline_process_param("-P", arg, 1, cfg);
68         assert(ret == 2);
69     } else if (!strncmp(q, "telnet:", 7)) {
70         /*
71          * If the hostname starts with "telnet:",
72          * set the protocol to Telnet and process
73          * the string as a Telnet URL.
74          */
75         char c;
76
77         q += 7;
78         if (q[0] == '/' && q[1] == '/')
79             q += 2;
80         cfg->protocol = PROT_TELNET;
81         p = q;
82         while (*p && *p != ':' && *p != '/')
83             p++;
84         c = *p;
85         if (*p)
86             *p++ = '\0';
87         if (c == ':')
88             cfg->port = atoi(p);
89         else
90             cfg->port = -1;
91         strncpy(cfg->host, q, sizeof(cfg->host) - 1);
92         cfg->host[sizeof(cfg->host) - 1] = '\0';
93         got_host = 1;
94     } else {
95         /*
96          * Otherwise, treat this argument as a host name.
97          */
98         p = arg;
99         while (*p && !isspace((unsigned char)*p))
100             p++;
101         if (*p)
102             *p++ = '\0';
103         strncpy(cfg->host, q, sizeof(cfg->host) - 1);
104         cfg->host[sizeof(cfg->host) - 1] = '\0';
105         got_host = 1;
106     }
107     if (got_host)
108         *allow_launch = TRUE;
109     return 1;
110 }
111
112 char *make_default_wintitle(char *hostname)
113 {
114     return dupcat(hostname, " - PuTTY", NULL);
115 }
116
117 /*
118  * X11-forwarding-related things suitable for Gtk app.
119  */
120
121 const char platform_x11_best_transport[] = "unix";
122
123 char *platform_get_x_display(void) {
124     const char *display;
125     /* Try to take account of --display and what have you. */
126     if (!(display = gdk_get_display()))
127         /* fall back to traditional method */
128         display = getenv("DISPLAY");
129     return dupstr(display);
130 }
131
132 int main(int argc, char **argv)
133 {
134     extern int pt_main(int argc, char **argv);
135     sk_init();
136     flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
137     default_protocol = be_default_protocol;
138     /* Find the appropriate default port. */
139     {
140         int i;
141         default_port = 0; /* illegal */
142         for (i = 0; backends[i].backend != NULL; i++)
143             if (backends[i].protocol == default_protocol) {
144                 default_port = backends[i].backend->default_port;
145                 break;
146             }
147     }
148     return pt_main(argc, argv);
149 }