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