A couple of X forwarding fixes for Unix Plink. Firstly, under Unix
[u/mdw/putty] / settings.c
1 /*
2 * settings.c: read and write saved sessions.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "putty.h"
8 #include "storage.h"
9
10 /*
11 * Tables of string <-> enum value mappings
12 */
13 struct keyval { char *s; int v; };
14
15 static const struct keyval ciphernames[] = {
16 { "aes", CIPHER_AES },
17 { "blowfish", CIPHER_BLOWFISH },
18 { "3des", CIPHER_3DES },
19 { "WARN", CIPHER_WARN },
20 { "des", CIPHER_DES }
21 };
22
23 static void gpps(void *handle, char *name, char *def, char *val, int len)
24 {
25 if (!read_setting_s(handle, name, val, len)) {
26 strncpy(val, def, len);
27 val[len - 1] = '\0';
28 }
29 }
30
31 static void gppi(void *handle, char *name, int def, int *i)
32 {
33 *i = read_setting_i(handle, name, def);
34 }
35
36 static int key2val(const struct keyval *mapping, int nmaps, char *key)
37 {
38 int i;
39 for (i = 0; i < nmaps; i++)
40 if (!strcmp(mapping[i].s, key)) return mapping[i].v;
41 return -1;
42 }
43
44 static const char *val2key(const struct keyval *mapping, int nmaps, int val)
45 {
46 int i;
47 for (i = 0; i < nmaps; i++)
48 if (mapping[i].v == val) return mapping[i].s;
49 return NULL;
50 }
51
52 /*
53 * Helper function to parse a comma-separated list of strings into
54 * a preference list array of values. Any missing values are added
55 * to the end and duplicates are weeded.
56 * XXX: assumes vals in 'mapping' are small +ve integers
57 */
58 static void gprefs(void *sesskey, char *name, char *def,
59 const struct keyval *mapping, int nvals,
60 int *array)
61 {
62 char commalist[80];
63 int n;
64 unsigned long seen = 0; /* bitmap for weeding dups etc */
65 gpps(sesskey, name, def, commalist, sizeof(commalist));
66
67 /* Grotty parsing of commalist. */
68 n = 0;
69 do {
70 int v;
71 char *key;
72 key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
73 if (!key) break;
74 if (((v = key2val(mapping, nvals, key)) != -1) &&
75 !(seen & 1<<v)) {
76 array[n] = v;
77 n++;
78 seen |= 1<<v;
79 }
80 } while (n < nvals);
81 /* Add any missing values (backward compatibility ect). */
82 {
83 int i;
84 for (i = 0; i < nvals; i++) {
85 if (!(seen & 1<<mapping[i].v)) {
86 array[n] = mapping[i].v;
87 n++;
88 }
89 }
90 }
91 }
92
93 /*
94 * Write out a preference list.
95 */
96 static void wprefs(void *sesskey, char *name,
97 const struct keyval *mapping, int nvals,
98 int *array)
99 {
100 char buf[80] = ""; /* XXX assumed big enough */
101 int l = sizeof(buf)-1, i;
102 buf[l] = '\0';
103 for (i = 0; l > 0 && i < nvals; i++) {
104 const char *s = val2key(mapping, nvals, array[i]);
105 if (s) {
106 int sl = strlen(s);
107 if (i > 0) {
108 strncat(buf, ",", l);
109 l--;
110 }
111 strncat(buf, s, l);
112 l -= sl;
113 }
114 }
115 write_setting_s(sesskey, name, buf);
116 }
117
118 void save_settings(char *section, int do_host, Config * cfg)
119 {
120 int i;
121 char *p;
122 void *sesskey;
123
124 sesskey = open_settings_w(section);
125 if (!sesskey)
126 return;
127
128 write_setting_i(sesskey, "Present", 1);
129 if (do_host) {
130 write_setting_s(sesskey, "HostName", cfg->host);
131 write_setting_s(sesskey, "LogFileName", cfg->logfilename);
132 write_setting_i(sesskey, "LogType", cfg->logtype);
133 write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
134 }
135 p = "raw";
136 for (i = 0; backends[i].name != NULL; i++)
137 if (backends[i].protocol == cfg->protocol) {
138 p = backends[i].name;
139 break;
140 }
141 write_setting_s(sesskey, "Protocol", p);
142 write_setting_i(sesskey, "PortNumber", cfg->port);
143 write_setting_i(sesskey, "CloseOnExit", cfg->close_on_exit);
144 write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
145 write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60); /* minutes */
146 write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60); /* seconds */
147 write_setting_i(sesskey, "TCPNoDelay", cfg->tcp_nodelay);
148 write_setting_s(sesskey, "TerminalType", cfg->termtype);
149 write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
150
151 /* proxy settings */
152 write_setting_s(sesskey, "ProxyExcludeList", cfg->proxy_exclude_list);
153 write_setting_i(sesskey, "ProxyDNS", cfg->proxy_dns);
154 write_setting_i(sesskey, "ProxyLocalhost", cfg->even_proxy_localhost);
155 write_setting_i(sesskey, "ProxyType", cfg->proxy_type);
156 write_setting_s(sesskey, "ProxyHost", cfg->proxy_host);
157 write_setting_i(sesskey, "ProxyPort", cfg->proxy_port);
158 write_setting_s(sesskey, "ProxyUsername", cfg->proxy_username);
159 write_setting_s(sesskey, "ProxyPassword", cfg->proxy_password);
160 write_setting_s(sesskey, "ProxyTelnetCommand", cfg->proxy_telnet_command);
161 write_setting_i(sesskey, "ProxySOCKSVersion", cfg->proxy_socks_version);
162
163 {
164 char buf[2 * sizeof(cfg->environmt)], *p, *q;
165 p = buf;
166 q = cfg->environmt;
167 while (*q) {
168 while (*q) {
169 int c = *q++;
170 if (c == '=' || c == ',' || c == '\\')
171 *p++ = '\\';
172 if (c == '\t')
173 c = '=';
174 *p++ = c;
175 }
176 *p++ = ',';
177 q++;
178 }
179 *p = '\0';
180 write_setting_s(sesskey, "Environment", buf);
181 }
182 write_setting_s(sesskey, "UserName", cfg->username);
183 write_setting_s(sesskey, "LocalUserName", cfg->localusername);
184 write_setting_i(sesskey, "NoPTY", cfg->nopty);
185 write_setting_i(sesskey, "Compression", cfg->compression);
186 write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
187 write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
188 wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
189 cfg->ssh_cipherlist);
190 write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
191 write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
192 write_setting_i(sesskey, "SshProt", cfg->sshprot);
193 write_setting_i(sesskey, "SSH2DES", cfg->ssh2_des_cbc);
194 write_setting_s(sesskey, "PublicKeyFile", cfg->keyfile);
195 write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
196 write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
197 write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
198 write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
199 write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
200 write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
201 write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
202 write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
203 write_setting_i(sesskey, "NoMouseReporting", cfg->no_mouse_rep);
204 write_setting_i(sesskey, "NoRemoteResize", cfg->no_remote_resize);
205 write_setting_i(sesskey, "NoAltScreen", cfg->no_alt_screen);
206 write_setting_i(sesskey, "NoRemoteWinTitle", cfg->no_remote_wintitle);
207 write_setting_i(sesskey, "NoDBackspace", cfg->no_dbackspace);
208 write_setting_i(sesskey, "NoRemoteCharset", cfg->no_remote_charset);
209 write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
210 write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
211 write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
212 write_setting_i(sesskey, "AltF4", cfg->alt_f4);
213 write_setting_i(sesskey, "AltSpace", cfg->alt_space);
214 write_setting_i(sesskey, "AltOnly", cfg->alt_only);
215 write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
216 write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
217 write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
218 write_setting_i(sesskey, "TelnetRet", cfg->telnet_newline);
219 write_setting_i(sesskey, "LocalEcho", cfg->localecho);
220 write_setting_i(sesskey, "LocalEdit", cfg->localedit);
221 write_setting_s(sesskey, "Answerback", cfg->answerback);
222 write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
223 write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
224 write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
225 write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
226 write_setting_i(sesskey, "WindowBorder", cfg->window_border);
227 write_setting_i(sesskey, "CurType", cfg->cursor_type);
228 write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
229 write_setting_i(sesskey, "Beep", cfg->beep);
230 write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
231 write_setting_s(sesskey, "BellWaveFile", cfg->bell_wavefile);
232 write_setting_i(sesskey, "BellOverload", cfg->bellovl);
233 write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
234 write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
235 write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
236 write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
237 write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
238 write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
239 write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
240 write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
241 write_setting_s(sesskey, "WinTitle", cfg->wintitle);
242 write_setting_i(sesskey, "TermWidth", cfg->width);
243 write_setting_i(sesskey, "TermHeight", cfg->height);
244 write_setting_s(sesskey, "Font", cfg->font);
245 write_setting_i(sesskey, "FontIsBold", cfg->fontisbold);
246 write_setting_i(sesskey, "FontCharSet", cfg->fontcharset);
247 write_setting_i(sesskey, "FontHeight", cfg->fontheight);
248 write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
249 write_setting_i(sesskey, "TryPalette", cfg->try_palette);
250 write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
251 for (i = 0; i < 22; i++) {
252 char buf[20], buf2[30];
253 sprintf(buf, "Colour%d", i);
254 sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
255 cfg->colours[i][1], cfg->colours[i][2]);
256 write_setting_s(sesskey, buf, buf2);
257 }
258 write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
259 write_setting_i(sesskey, "PasteRTF", cfg->rtf_paste);
260 write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
261 write_setting_i(sesskey, "RectSelect", cfg->rect_select);
262 write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
263 for (i = 0; i < 256; i += 32) {
264 char buf[20], buf2[256];
265 int j;
266 sprintf(buf, "Wordness%d", i);
267 *buf2 = '\0';
268 for (j = i; j < i + 32; j++) {
269 sprintf(buf2 + strlen(buf2), "%s%d",
270 (*buf2 ? "," : ""), cfg->wordness[j]);
271 }
272 write_setting_s(sesskey, buf, buf2);
273 }
274 write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
275 write_setting_s(sesskey, "Printer", cfg->printer);
276 write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
277 write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
278 write_setting_i(sesskey, "ScrollBarFullScreen", cfg->scrollbar_in_fullscreen);
279 write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
280 write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
281 write_setting_i(sesskey, "LockSize", cfg->resize_action);
282 write_setting_i(sesskey, "BCE", cfg->bce);
283 write_setting_i(sesskey, "BlinkText", cfg->blinktext);
284 write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
285 write_setting_s(sesskey, "X11Display", cfg->x11_display);
286 write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
287 write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
288 {
289 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
290 p = buf;
291 q = cfg->portfwd;
292 while (*q) {
293 while (*q) {
294 int c = *q++;
295 if (c == '=' || c == ',' || c == '\\')
296 *p++ = '\\';
297 if (c == '\t')
298 c = '=';
299 *p++ = c;
300 }
301 *p++ = ',';
302 q++;
303 }
304 *p = '\0';
305 write_setting_s(sesskey, "PortForwardings", buf);
306 }
307 write_setting_i(sesskey, "BugIgnore1", cfg->sshbug_ignore1);
308 write_setting_i(sesskey, "BugPlainPW1", cfg->sshbug_plainpw1);
309 write_setting_i(sesskey, "BugRSA1", cfg->sshbug_rsa1);
310 write_setting_i(sesskey, "BugHMAC2", cfg->sshbug_hmac2);
311 write_setting_i(sesskey, "BugDeriveKey2", cfg->sshbug_derivekey2);
312 write_setting_i(sesskey, "BugRSAPad2", cfg->sshbug_rsapad2);
313 write_setting_i(sesskey, "BugDHGEx2", cfg->sshbug_dhgex2);
314 write_setting_i(sesskey, "StampUtmp", cfg->stamp_utmp);
315 write_setting_i(sesskey, "LoginShell", cfg->login_shell);
316 write_setting_i(sesskey, "ScrollbarOnLeft", cfg->scrollbar_on_left);
317 write_setting_s(sesskey, "BoldFont", cfg->boldfont);
318 write_setting_i(sesskey, "ShadowBoldOffset", cfg->shadowboldoffset);
319 close_settings_w(sesskey);
320 }
321
322 void load_settings(char *section, int do_host, Config * cfg)
323 {
324 void *sesskey;
325
326 sesskey = open_settings_r(section);
327 load_open_settings(sesskey, do_host, cfg);
328 close_settings_r(sesskey);
329 }
330
331 void load_open_settings(void *sesskey, int do_host, Config *cfg)
332 {
333 int i;
334 char prot[10];
335
336 cfg->ssh_subsys = 0; /* FIXME: load this properly */
337 cfg->remote_cmd_ptr = cfg->remote_cmd;
338 cfg->remote_cmd_ptr2 = NULL;
339
340 if (do_host) {
341 gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
342 } else {
343 cfg->host[0] = '\0'; /* blank hostname */
344 }
345 gpps(sesskey, "LogFileName", "putty.log",
346 cfg->logfilename, sizeof(cfg->logfilename));
347 gppi(sesskey, "LogType", 0, &cfg->logtype);
348 gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
349
350 gpps(sesskey, "Protocol", "default", prot, 10);
351 cfg->protocol = default_protocol;
352 cfg->port = default_port;
353 for (i = 0; backends[i].name != NULL; i++)
354 if (!strcmp(prot, backends[i].name)) {
355 cfg->protocol = backends[i].protocol;
356 gppi(sesskey, "PortNumber", default_port, &cfg->port);
357 break;
358 }
359
360 /*
361 * CloseOnExit defaults to closing only on a clean exit - but
362 * unfortunately not on Unix (pterm). On Unix, the exit code of
363 * a shell is the last exit code of one of its child processes,
364 * even if it's an interactive shell - so some pterms will
365 * close and some will not for no particularly good reason. The
366 * mode is still useful for specialist purposes (running a
367 * single command in its own pterm), but I don't think it's a
368 * sane default, unfortunately.
369 */
370 gppi(sesskey, "CloseOnExit",
371 #ifdef _WINDOWS
372 COE_NORMAL,
373 #else
374 COE_ALWAYS,
375 #endif
376 &cfg->close_on_exit);
377 gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
378 {
379 /* This is two values for backward compatibility with 0.50/0.51 */
380 int pingmin, pingsec;
381 gppi(sesskey, "PingInterval", 0, &pingmin);
382 gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
383 cfg->ping_interval = pingmin * 60 + pingsec;
384 }
385 gppi(sesskey, "TCPNoDelay", 1, &cfg->tcp_nodelay);
386 gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
387 sizeof(cfg->termtype));
388 gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
389 sizeof(cfg->termspeed));
390
391 /* proxy settings */
392 gpps(sesskey, "ProxyExcludeList", "", cfg->proxy_exclude_list,
393 sizeof(cfg->proxy_exclude_list));
394 gppi(sesskey, "ProxyDNS", PROXYDNS_AUTO, &i); cfg->proxy_dns = i;
395 gppi(sesskey, "ProxyLocalhost", 0, &cfg->even_proxy_localhost);
396 gppi(sesskey, "ProxyType", PROXY_NONE, &i); cfg->proxy_type = i;
397 gpps(sesskey, "ProxyHost", "proxy", cfg->proxy_host,
398 sizeof(cfg->proxy_host));
399 gppi(sesskey, "ProxyPort", 80, &cfg->proxy_port);
400 gpps(sesskey, "ProxyUsername", "", cfg->proxy_username,
401 sizeof(cfg->proxy_username));
402 gpps(sesskey, "ProxyPassword", "", cfg->proxy_password,
403 sizeof(cfg->proxy_password));
404 gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
405 cfg->proxy_telnet_command, sizeof(cfg->proxy_telnet_command));
406 gppi(sesskey, "ProxySOCKSVersion", 5, &cfg->proxy_socks_version);
407
408 {
409 char buf[2 * sizeof(cfg->environmt)], *p, *q;
410 gpps(sesskey, "Environment", "", buf, sizeof(buf));
411 p = buf;
412 q = cfg->environmt;
413 while (*p) {
414 while (*p && *p != ',') {
415 int c = *p++;
416 if (c == '=')
417 c = '\t';
418 if (c == '\\')
419 c = *p++;
420 *q++ = c;
421 }
422 if (*p == ',')
423 p++;
424 *q++ = '\0';
425 }
426 *q = '\0';
427 }
428 gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
429 gpps(sesskey, "LocalUserName", "", cfg->localusername,
430 sizeof(cfg->localusername));
431 gppi(sesskey, "NoPTY", 0, &cfg->nopty);
432 gppi(sesskey, "Compression", 0, &cfg->compression);
433 gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
434 gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
435 gprefs(sesskey, "Cipher", "\0",
436 ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
437 gppi(sesskey, "SshProt", 2, &cfg->sshprot);
438 gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
439 gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
440 gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
441 gpps(sesskey, "PublicKeyFile", "", cfg->keyfile, sizeof(cfg->keyfile));
442 gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
443 sizeof(cfg->remote_cmd));
444 gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
445 gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
446 gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
447 gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
448 gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
449 gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
450 gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
451 gppi(sesskey, "NoMouseReporting", 0, &cfg->no_mouse_rep);
452 gppi(sesskey, "NoRemoteResize", 0, &cfg->no_remote_resize);
453 gppi(sesskey, "NoAltScreen", 0, &cfg->no_alt_screen);
454 gppi(sesskey, "NoRemoteWinTitle", 0, &cfg->no_remote_wintitle);
455 gppi(sesskey, "NoDBackspace", 0, &cfg->no_dbackspace);
456 gppi(sesskey, "NoRemoteCharset", 0, &cfg->no_remote_charset);
457 gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
458 gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
459 gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
460 gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
461 gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
462 gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
463 gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
464 gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
465 gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
466 gppi(sesskey, "TelnetRet", 1, &cfg->telnet_newline);
467 gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho);
468 gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit);
469 gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
470 sizeof(cfg->answerback));
471 gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
472 gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
473 gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
474 gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
475 gppi(sesskey, "WindowBorder", 1, &cfg->window_border);
476 gppi(sesskey, "CurType", 0, &cfg->cursor_type);
477 gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
478 /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
479 gppi(sesskey, "Beep", 1, &i); cfg->beep = i;
480 gppi(sesskey, "BeepInd", 0, &i); cfg->beep_ind = i;
481 gpps(sesskey, "BellWaveFile", "", cfg->bell_wavefile,
482 sizeof(cfg->bell_wavefile));
483 gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
484 gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
485 gppi(sesskey, "BellOverloadT", 2*TICKSPERSEC, &cfg->bellovl_t);
486 gppi(sesskey, "BellOverloadS", 5*TICKSPERSEC, &cfg->bellovl_s);
487 gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
488 gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
489 gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
490 gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
491 gppi(sesskey, "WinNameAlways", 0, &cfg->win_name_always);
492 gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
493 gppi(sesskey, "TermWidth", 80, &cfg->width);
494 gppi(sesskey, "TermHeight", 24, &cfg->height);
495 #ifdef _WINDOWS
496 gpps(sesskey, "Font", "Courier New", cfg->font, sizeof(cfg->font));
497 #elif defined(macintosh)
498 gpps(sesskey, "Font", "Monaco", cfg->font, sizeof(cfg->font));
499 #else
500 gpps(sesskey, "Font", "fixed", cfg->font, sizeof(cfg->font));
501 #endif
502 gppi(sesskey, "FontIsBold", 0, &cfg->fontisbold);
503 #ifdef _WINDOWS
504 gppi(sesskey, "FontCharSet", ANSI_CHARSET, &cfg->fontcharset);
505 #endif
506 #ifdef macintosh
507 gppi(sesskey, "FontHeight", 9, &cfg->fontheight);
508 #else
509 gppi(sesskey, "FontHeight", 10, &cfg->fontheight);
510 #endif
511 #ifdef _WINDOWS
512 if (cfg->fontheight < 0) {
513 int oldh, newh;
514 HDC hdc = GetDC(NULL);
515 int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
516 ReleaseDC(NULL, hdc);
517
518 oldh = -cfg->fontheight;
519 newh = MulDiv(oldh, 72, logpix) + 1;
520 if (MulDiv(newh, logpix, 72) > oldh)
521 newh--;
522 cfg->fontheight = newh;
523 }
524 #endif
525 gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
526 gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
527 gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
528 for (i = 0; i < 22; i++) {
529 static char *defaults[] = {
530 "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
531 "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
532 "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
533 "85,85,255", "187,0,187", "255,85,255", "0,187,187",
534 "85,255,255", "187,187,187", "255,255,255"
535 };
536 char buf[20], buf2[30];
537 int c0, c1, c2;
538 sprintf(buf, "Colour%d", i);
539 gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
540 if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
541 cfg->colours[i][0] = c0;
542 cfg->colours[i][1] = c1;
543 cfg->colours[i][2] = c2;
544 }
545 }
546 #ifndef _WINDOWS
547 /* Non-raw cut and paste of line-drawing chars works badly on the
548 * current Unix stub implementation of the Unicode functions.
549 * So I'm going to temporarily set the default to raw mode so
550 * that the failure mode isn't quite so drastically horrid.
551 * When Unicode comes in, this can all be put right. */
552 gppi(sesskey, "RawCNP", 1, &cfg->rawcnp);
553 #else
554 gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
555 #endif
556 gppi(sesskey, "PasteRTF", 0, &cfg->rtf_paste);
557 gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
558 gppi(sesskey, "RectSelect", 0, &cfg->rect_select);
559 gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
560 for (i = 0; i < 256; i += 32) {
561 static char *defaults[] = {
562 "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
563 "0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1",
564 "1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2",
565 "1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1",
566 "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
567 "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
568 "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2",
569 "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2"
570 };
571 char buf[20], buf2[256], *p;
572 int j;
573 sprintf(buf, "Wordness%d", i);
574 gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
575 p = buf2;
576 for (j = i; j < i + 32; j++) {
577 char *q = p;
578 while (*p && *p != ',')
579 p++;
580 if (*p == ',')
581 *p++ = '\0';
582 cfg->wordness[j] = atoi(q);
583 }
584 }
585 /*
586 * The empty default for LineCodePage will be converted later
587 * into a plausible default for the locale.
588 */
589 gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
590 sizeof(cfg->line_codepage));
591 gpps(sesskey, "Printer", "", cfg->printer, sizeof(cfg->printer));
592 gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
593 gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
594 gppi(sesskey, "ScrollBarFullScreen", 0, &cfg->scrollbar_in_fullscreen);
595 gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
596 gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
597 gppi(sesskey, "LockSize", 0, &i); cfg->resize_action = i;
598 gppi(sesskey, "BCE", 1, &cfg->bce);
599 gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
600 gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
601 #ifdef _WINDOWS
602 gpps(sesskey, "X11Display", "localhost:0", cfg->x11_display,
603 sizeof(cfg->x11_display));
604 #else
605 {
606 /* On Unix, the default X display should simply be $DISPLAY. */
607 char *disp = getenv("DISPLAY");
608 gpps(sesskey, "X11Display", disp, cfg->x11_display,
609 sizeof(cfg->x11_display));
610 }
611 #endif
612
613 gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
614 gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
615 {
616 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
617 gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
618 p = buf;
619 q = cfg->portfwd;
620 while (*p) {
621 while (*p && *p != ',') {
622 int c = *p++;
623 if (c == '=')
624 c = '\t';
625 if (c == '\\')
626 c = *p++;
627 *q++ = c;
628 }
629 if (*p == ',')
630 p++;
631 *q++ = '\0';
632 }
633 *q = '\0';
634 }
635 gppi(sesskey, "BugIgnore1", BUG_AUTO, &i); cfg->sshbug_ignore1 = i;
636 gppi(sesskey, "BugPlainPW1", BUG_AUTO, &i); cfg->sshbug_plainpw1 = i;
637 gppi(sesskey, "BugRSA1", BUG_AUTO, &i); cfg->sshbug_rsa1 = i;
638 {
639 int i;
640 gppi(sesskey, "BugHMAC2", BUG_AUTO, &i); cfg->sshbug_hmac2 = i;
641 if (cfg->sshbug_hmac2 == BUG_AUTO) {
642 gppi(sesskey, "BuggyMAC", 0, &i);
643 if (i == 1)
644 cfg->sshbug_hmac2 = BUG_ON;
645 }
646 }
647 gppi(sesskey, "BugDeriveKey2", BUG_AUTO, &i); cfg->sshbug_derivekey2 = i;
648 gppi(sesskey, "BugRSAPad2", BUG_AUTO, &i); cfg->sshbug_rsapad2 = i;
649 gppi(sesskey, "BugDHGEx2", BUG_AUTO, &i); cfg->sshbug_dhgex2 = i;
650 gppi(sesskey, "StampUtmp", 1, &cfg->stamp_utmp);
651 gppi(sesskey, "LoginShell", 1, &cfg->login_shell);
652 gppi(sesskey, "ScrollbarOnLeft", 0, &cfg->scrollbar_on_left);
653 gpps(sesskey, "BoldFont", "", cfg->boldfont, sizeof(cfg->boldfont));
654 gppi(sesskey, "ShadowBoldOffset", 1, &cfg->shadowboldoffset);
655 }
656
657 void do_defaults(char *session, Config * cfg)
658 {
659 if (session)
660 load_settings(session, TRUE, cfg);
661 else
662 load_settings("Default Settings", FALSE, cfg);
663 }
664
665 static int sessioncmp(const void *av, const void *bv)
666 {
667 const char *a = *(const char *const *) av;
668 const char *b = *(const char *const *) bv;
669
670 /*
671 * Alphabetical order, except that "Default Settings" is a
672 * special case and comes first.
673 */
674 if (!strcmp(a, "Default Settings"))
675 return -1; /* a comes first */
676 if (!strcmp(b, "Default Settings"))
677 return +1; /* b comes first */
678 /*
679 * FIXME: perhaps we should ignore the first & in determining
680 * sort order.
681 */
682 return strcmp(a, b); /* otherwise, compare normally */
683 }
684
685 void get_sesslist(struct sesslist *list, int allocate)
686 {
687 char otherbuf[2048];
688 int buflen, bufsize, i;
689 char *p, *ret;
690 void *handle;
691
692 if (allocate) {
693
694 buflen = bufsize = 0;
695 list->buffer = NULL;
696 if ((handle = enum_settings_start()) != NULL) {
697 do {
698 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
699 if (ret) {
700 int len = strlen(otherbuf) + 1;
701 if (bufsize < buflen + len) {
702 bufsize = buflen + len + 2048;
703 list->buffer = srealloc(list->buffer, bufsize);
704 }
705 strcpy(list->buffer + buflen, otherbuf);
706 buflen += strlen(list->buffer + buflen) + 1;
707 }
708 } while (ret);
709 enum_settings_finish(handle);
710 }
711 list->buffer = srealloc(list->buffer, buflen + 1);
712 list->buffer[buflen] = '\0';
713
714 /*
715 * Now set up the list of sessions. Note that "Default
716 * Settings" must always be claimed to exist, even if it
717 * doesn't really.
718 */
719
720 p = list->buffer;
721 list->nsessions = 1; /* "Default Settings" counts as one */
722 while (*p) {
723 if (strcmp(p, "Default Settings"))
724 list->nsessions++;
725 while (*p)
726 p++;
727 p++;
728 }
729
730 list->sessions = smalloc((list->nsessions + 1) * sizeof(char *));
731 list->sessions[0] = "Default Settings";
732 p = list->buffer;
733 i = 1;
734 while (*p) {
735 if (strcmp(p, "Default Settings"))
736 list->sessions[i++] = p;
737 while (*p)
738 p++;
739 p++;
740 }
741
742 qsort(list->sessions, i, sizeof(char *), sessioncmp);
743 } else {
744 sfree(list->buffer);
745 sfree(list->sessions);
746 }
747 }