Ctrl+rightclick now pops up a context menu in Unix PuTTY and pterm.
[sgt/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
11 #include "putty.h"
12 #include "storage.h"
13
14 /*
15 * TODO:
16 *
17 * - libcharset enumeration.
18 *
19 * - fix the printer enum (I think the sensible thing is simply to
20 * have uxcfg.c remove the drop-down list completely, since you
21 * can't sensibly provide an enumerated list of lpr commands!).
22 *
23 * - Remainder of the context menu:
24 *
25 * - Event Log (this means we must implement the Event Log; not
26 * in pterm)
27 *
28 * - New Session and Duplicate Session (perhaps in pterm, in fact?!)
29 * + Duplicate Session will be fun, since we must work out
30 * how to pass the config data through.
31 * + In fact this should be easier on Unix, since fork() is
32 * available so we need not even exec (this also saves us
33 * the trouble of scrabbling around trying to find our own
34 * binary). Possible scenario: respond to Duplicate
35 * Session by forking. Parent continues as before; child
36 * unceremoniously frees all extant resources (backend,
37 * terminal, ldisc, frontend etc) and then _longjmps_ (I
38 * kid you not) back to a point in pt_main() which causes
39 * it to go back round to the point of opening a new
40 * terminal window and a new backend.
41 * + A tricky bit here is how to free everything without
42 * also _destroying_ things - calling GTK to free up
43 * existing widgets is liable to send destroy messages to
44 * the X server, which won't go down too well with the
45 * parent process. exec() is a much cleaner solution to
46 * this bit, but requires us to invent some ghastly IPC as
47 * we did in Windows PuTTY.
48 * + Arrgh! Also, this won't work in pterm since we'll
49 * already have dropped privileges by this point, so we
50 * can't get another pty. Sigh. Looks like exec has to be
51 * the way forward then :-/
52 *
53 * - Saved Sessions submenu (not in pterm of course)
54 *
55 * - Change Settings
56 * + we must also implement mid-session reconfig in pterm.c.
57 * + note this also requires config.c and uxcfg.c to be able
58 * to get hold of the application name.
59 *
60 * - Copy All to Clipboard (for what that's worth)
61 */
62
63 /*
64 * Clean up and exit.
65 */
66 void cleanup_exit(int code)
67 {
68 /*
69 * Clean up.
70 */
71 sk_cleanup();
72 random_save_seed();
73 exit(code);
74 }
75
76 /*
77 * Another bunch of temporary stub functions. These ones will want
78 * removing by means of implementing them properly: libcharset
79 * should invent its own sensible format for codepage names and a
80 * means of enumerating them, and printer_enum needs to be dealt
81 * with somehow or other too.
82 */
83
84 char *cp_name(int codepage)
85 {
86 return "";
87 }
88 char *cp_enumerate(int index)
89 {
90 return NULL;
91 }
92 int decode_codepage(char *cp_name)
93 {
94 return -2;
95 }
96
97 printer_enum *printer_start_enum(int *nprinters_ptr) {
98 *nprinters_ptr = 0;
99 return NULL;
100 }
101 char *printer_get_name(printer_enum *pe, int i) { return NULL;
102 }
103 void printer_finish_enum(printer_enum *pe) { }
104
105 const char *const appname = "PuTTY";
106
107 Backend *select_backend(Config *cfg)
108 {
109 int i;
110 Backend *back = NULL;
111 for (i = 0; backends[i].backend != NULL; i++)
112 if (backends[i].protocol == cfg->protocol) {
113 back = backends[i].backend;
114 break;
115 }
116 assert(back != NULL);
117 return back;
118 }
119
120 int cfgbox(Config *cfg)
121 {
122 extern int do_config_box(const char *title, Config *cfg);
123 return do_config_box("PuTTY Configuration", cfg);
124 }
125
126 static int got_host = 0;
127
128 int process_nonoption_arg(char *arg, Config *cfg)
129 {
130 char *p, *q = arg;
131
132 if (got_host) {
133 /*
134 * If we already have a host name, treat this argument as a
135 * port number. NB we have to treat this as a saved -P
136 * argument, so that it will be deferred until it's a good
137 * moment to run it.
138 */
139 int ret = cmdline_process_param("-P", arg, 1, cfg);
140 assert(ret == 2);
141 } else if (!strncmp(q, "telnet:", 7)) {
142 /*
143 * If the hostname starts with "telnet:",
144 * set the protocol to Telnet and process
145 * the string as a Telnet URL.
146 */
147 char c;
148
149 q += 7;
150 if (q[0] == '/' && q[1] == '/')
151 q += 2;
152 cfg->protocol = PROT_TELNET;
153 p = q;
154 while (*p && *p != ':' && *p != '/')
155 p++;
156 c = *p;
157 if (*p)
158 *p++ = '\0';
159 if (c == ':')
160 cfg->port = atoi(p);
161 else
162 cfg->port = -1;
163 strncpy(cfg->host, q, sizeof(cfg->host) - 1);
164 cfg->host[sizeof(cfg->host) - 1] = '\0';
165 got_host = 1;
166 } else {
167 /*
168 * Otherwise, treat this argument as a host name.
169 */
170 p = arg;
171 while (*p && !isspace((unsigned char)*p))
172 p++;
173 if (*p)
174 *p++ = '\0';
175 strncpy(cfg->host, q, sizeof(cfg->host) - 1);
176 cfg->host[sizeof(cfg->host) - 1] = '\0';
177 got_host = 1;
178 }
179 return 1;
180 }
181
182 char *make_default_wintitle(char *hostname)
183 {
184 return dupcat(hostname, " - PuTTY", NULL);
185 }
186
187 int main(int argc, char **argv)
188 {
189 extern int pt_main(int argc, char **argv);
190 sk_init();
191 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
192 default_protocol = be_default_protocol;
193 /* Find the appropriate default port. */
194 {
195 int i;
196 default_port = 0; /* illegal */
197 for (i = 0; backends[i].backend != NULL; i++)
198 if (backends[i].protocol == default_protocol) {
199 default_port = backends[i].backend->default_port;
200 break;
201 }
202 }
203 return pt_main(argc, argv);
204 }