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