Unix can't sensibly enumerate printers (since they're defined as
[sgt/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 * - 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 *
47e4e735 23 * - Remainder of the context menu:
5bf9955d 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)
1d009ae7 61 */
5bf9955d 62
1d009ae7 63/*
64 * Clean up and exit.
65 */
66void cleanup_exit(int code)
67{
68 /*
69 * Clean up.
70 */
71 sk_cleanup();
72 random_save_seed();
73 exit(code);
74}
75
1d009ae7 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
84char *cp_name(int codepage)
85{
86 return "";
87}
88char *cp_enumerate(int index)
89{
90 return NULL;
91}
92int decode_codepage(char *cp_name)
93{
94 return -2;
95}
96
47e4e735 97const char *const appname = "PuTTY";
98
1d009ae7 99Backend *select_backend(Config *cfg)
100{
101 int i;
102 Backend *back = NULL;
103 for (i = 0; backends[i].backend != NULL; i++)
104 if (backends[i].protocol == cfg->protocol) {
105 back = backends[i].backend;
106 break;
107 }
108 assert(back != NULL);
109 return back;
110}
111
112int cfgbox(Config *cfg)
113{
114 extern int do_config_box(const char *title, Config *cfg);
115 return do_config_box("PuTTY Configuration", cfg);
116}
117
46a3419b 118static int got_host = 0;
119
120int process_nonoption_arg(char *arg, Config *cfg)
121{
122 char *p, *q = arg;
123
124 if (got_host) {
125 /*
126 * If we already have a host name, treat this argument as a
127 * port number. NB we have to treat this as a saved -P
128 * argument, so that it will be deferred until it's a good
129 * moment to run it.
130 */
131 int ret = cmdline_process_param("-P", arg, 1, cfg);
132 assert(ret == 2);
133 } else if (!strncmp(q, "telnet:", 7)) {
134 /*
135 * If the hostname starts with "telnet:",
136 * set the protocol to Telnet and process
137 * the string as a Telnet URL.
138 */
139 char c;
140
141 q += 7;
142 if (q[0] == '/' && q[1] == '/')
143 q += 2;
144 cfg->protocol = PROT_TELNET;
145 p = q;
146 while (*p && *p != ':' && *p != '/')
147 p++;
148 c = *p;
149 if (*p)
150 *p++ = '\0';
151 if (c == ':')
152 cfg->port = atoi(p);
153 else
154 cfg->port = -1;
155 strncpy(cfg->host, q, sizeof(cfg->host) - 1);
156 cfg->host[sizeof(cfg->host) - 1] = '\0';
157 got_host = 1;
158 } else {
159 /*
160 * Otherwise, treat this argument as a host name.
161 */
320cb4be 162 p = arg;
46a3419b 163 while (*p && !isspace((unsigned char)*p))
164 p++;
165 if (*p)
166 *p++ = '\0';
167 strncpy(cfg->host, q, sizeof(cfg->host) - 1);
168 cfg->host[sizeof(cfg->host) - 1] = '\0';
169 got_host = 1;
170 }
171 return 1;
172}
173
10705014 174char *make_default_wintitle(char *hostname)
175{
176 return dupcat(hostname, " - PuTTY", NULL);
177}
178
1d009ae7 179int main(int argc, char **argv)
180{
181 extern int pt_main(int argc, char **argv);
182 sk_init();
183 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
184 default_protocol = be_default_protocol;
185 /* Find the appropriate default port. */
186 {
187 int i;
188 default_port = 0; /* illegal */
189 for (i = 0; backends[i].backend != NULL; i++)
190 if (backends[i].protocol == default_protocol) {
191 default_port = backends[i].backend->default_port;
192 break;
193 }
194 }
195 return pt_main(argc, argv);
196}