xterm apparently supports ESC[90m through ESC[97m to set bright
[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
c85623f9 23static void gpps(void *handle, const char *name, const char *def,
24 char *val, int len)
32874aea 25{
a9422f39 26 if (!read_setting_s(handle, name, val, len)) {
5a9eb105 27 char *pdef;
28
29 pdef = platform_default_s(name);
30 if (pdef) {
31 strncpy(val, pdef, len);
32 } else {
33 strncpy(val, def, len);
34 }
35
32874aea 36 val[len - 1] = '\0';
a9422f39 37 }
38}
39
32874aea 40static void gppi(void *handle, char *name, int def, int *i)
41{
5a9eb105 42 def = platform_default_i(name, def);
a9422f39 43 *i = read_setting_i(handle, name, def);
44}
45
ca20bfcf 46static int key2val(const struct keyval *mapping, int nmaps, char *key)
47{
48 int i;
49 for (i = 0; i < nmaps; i++)
50 if (!strcmp(mapping[i].s, key)) return mapping[i].v;
51 return -1;
52}
53
54static const char *val2key(const struct keyval *mapping, int nmaps, int val)
55{
56 int i;
57 for (i = 0; i < nmaps; i++)
58 if (mapping[i].v == val) return mapping[i].s;
59 return NULL;
60}
61
62/*
63 * Helper function to parse a comma-separated list of strings into
64 * a preference list array of values. Any missing values are added
65 * to the end and duplicates are weeded.
66 * XXX: assumes vals in 'mapping' are small +ve integers
67 */
68static void gprefs(void *sesskey, char *name, char *def,
69 const struct keyval *mapping, int nvals,
70 int *array)
71{
72 char commalist[80];
73 int n;
74 unsigned long seen = 0; /* bitmap for weeding dups etc */
75 gpps(sesskey, name, def, commalist, sizeof(commalist));
76
77 /* Grotty parsing of commalist. */
78 n = 0;
79 do {
80 int v;
81 char *key;
82 key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
83 if (!key) break;
84 if (((v = key2val(mapping, nvals, key)) != -1) &&
85 !(seen & 1<<v)) {
86 array[n] = v;
87 n++;
88 seen |= 1<<v;
89 }
90 } while (n < nvals);
91 /* Add any missing values (backward compatibility ect). */
92 {
cdcbdf3b 93 int i;
ca20bfcf 94 for (i = 0; i < nvals; i++) {
95 if (!(seen & 1<<mapping[i].v)) {
96 array[n] = mapping[i].v;
97 n++;
98 }
99 }
100 }
101}
102
103/*
104 * Write out a preference list.
105 */
106static void wprefs(void *sesskey, char *name,
107 const struct keyval *mapping, int nvals,
108 int *array)
109{
110 char buf[80] = ""; /* XXX assumed big enough */
111 int l = sizeof(buf)-1, i;
112 buf[l] = '\0';
113 for (i = 0; l > 0 && i < nvals; i++) {
114 const char *s = val2key(mapping, nvals, array[i]);
115 if (s) {
116 int sl = strlen(s);
117 if (i > 0) {
118 strncat(buf, ",", l);
119 l--;
120 }
121 strncat(buf, s, l);
122 l -= sl;
123 }
124 }
125 write_setting_s(sesskey, name, buf);
126}
127
32874aea 128void save_settings(char *section, int do_host, Config * cfg)
129{
a9422f39 130 void *sesskey;
131
132 sesskey = open_settings_w(section);
133 if (!sesskey)
32874aea 134 return;
b537dd42 135 save_open_settings(sesskey, do_host, cfg);
136 close_settings_w(sesskey);
137}
138
139void save_open_settings(void *sesskey, int do_host, Config *cfg)
140{
141 int i;
142 char *p;
a9422f39 143
32874aea 144 write_setting_i(sesskey, "Present", 1);
a9422f39 145 if (do_host) {
32874aea 146 write_setting_s(sesskey, "HostName", cfg->host);
32874aea 147 write_setting_s(sesskey, "LogFileName", cfg->logfilename);
148 write_setting_i(sesskey, "LogType", cfg->logtype);
149 write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
a9422f39 150 }
348a9ad6 151 p = "raw";
152 for (i = 0; backends[i].name != NULL; i++)
153 if (backends[i].protocol == cfg->protocol) {
154 p = backends[i].name;
155 break;
156 }
157 write_setting_s(sesskey, "Protocol", p);
158 write_setting_i(sesskey, "PortNumber", cfg->port);
32874aea 159 write_setting_i(sesskey, "CloseOnExit", cfg->close_on_exit);
160 write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
161 write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60); /* minutes */
162 write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60); /* seconds */
2184a5d9 163 write_setting_i(sesskey, "TCPNoDelay", cfg->tcp_nodelay);
32874aea 164 write_setting_s(sesskey, "TerminalType", cfg->termtype);
165 write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
8eebd221 166
167 /* proxy settings */
168 write_setting_s(sesskey, "ProxyExcludeList", cfg->proxy_exclude_list);
b7a189f3 169 write_setting_i(sesskey, "ProxyDNS", cfg->proxy_dns);
b804e1e5 170 write_setting_i(sesskey, "ProxyLocalhost", cfg->even_proxy_localhost);
8eebd221 171 write_setting_i(sesskey, "ProxyType", cfg->proxy_type);
172 write_setting_s(sesskey, "ProxyHost", cfg->proxy_host);
173 write_setting_i(sesskey, "ProxyPort", cfg->proxy_port);
174 write_setting_s(sesskey, "ProxyUsername", cfg->proxy_username);
175 write_setting_s(sesskey, "ProxyPassword", cfg->proxy_password);
176 write_setting_s(sesskey, "ProxyTelnetCommand", cfg->proxy_telnet_command);
177 write_setting_i(sesskey, "ProxySOCKSVersion", cfg->proxy_socks_version);
178
a9422f39 179 {
32874aea 180 char buf[2 * sizeof(cfg->environmt)], *p, *q;
a9422f39 181 p = buf;
32874aea 182 q = cfg->environmt;
a9422f39 183 while (*q) {
184 while (*q) {
185 int c = *q++;
186 if (c == '=' || c == ',' || c == '\\')
187 *p++ = '\\';
188 if (c == '\t')
189 c = '=';
190 *p++ = c;
191 }
192 *p++ = ',';
193 q++;
194 }
195 *p = '\0';
32874aea 196 write_setting_s(sesskey, "Environment", buf);
a9422f39 197 }
32874aea 198 write_setting_s(sesskey, "UserName", cfg->username);
199 write_setting_s(sesskey, "LocalUserName", cfg->localusername);
200 write_setting_i(sesskey, "NoPTY", cfg->nopty);
201 write_setting_i(sesskey, "Compression", cfg->compression);
202 write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
5bb641e1 203 write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
e411be57 204 wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
ca20bfcf 205 cfg->ssh_cipherlist);
32874aea 206 write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
f091e308 207 write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
32874aea 208 write_setting_i(sesskey, "SshProt", cfg->sshprot);
cb4d4768 209 write_setting_i(sesskey, "SSH2DES", cfg->ssh2_des_cbc);
32874aea 210 write_setting_s(sesskey, "PublicKeyFile", cfg->keyfile);
211 write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
212 write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
8faa456c 213 write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
32874aea 214 write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
215 write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
216 write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
217 write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
218 write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
c0d36a72 219 write_setting_i(sesskey, "NoMouseReporting", cfg->no_mouse_rep);
0d2086c5 220 write_setting_i(sesskey, "NoRemoteResize", cfg->no_remote_resize);
221 write_setting_i(sesskey, "NoAltScreen", cfg->no_alt_screen);
222 write_setting_i(sesskey, "NoRemoteWinTitle", cfg->no_remote_wintitle);
223 write_setting_i(sesskey, "NoDBackspace", cfg->no_dbackspace);
224 write_setting_i(sesskey, "NoRemoteCharset", cfg->no_remote_charset);
32874aea 225 write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
226 write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
227 write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
228 write_setting_i(sesskey, "AltF4", cfg->alt_f4);
229 write_setting_i(sesskey, "AltSpace", cfg->alt_space);
230 write_setting_i(sesskey, "AltOnly", cfg->alt_only);
231 write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
232 write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
a5f3e637 233 write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
eee63b77 234 write_setting_i(sesskey, "TelnetRet", cfg->telnet_newline);
32874aea 235 write_setting_i(sesskey, "LocalEcho", cfg->localecho);
236 write_setting_i(sesskey, "LocalEdit", cfg->localedit);
237 write_setting_s(sesskey, "Answerback", cfg->answerback);
238 write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
8f57d753 239 write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
32874aea 240 write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
241 write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
5a73255e 242 write_setting_i(sesskey, "WindowBorder", cfg->window_border);
32874aea 243 write_setting_i(sesskey, "CurType", cfg->cursor_type);
244 write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
245 write_setting_i(sesskey, "Beep", cfg->beep);
f8a28d1f 246 write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
32874aea 247 write_setting_s(sesskey, "BellWaveFile", cfg->bell_wavefile);
248 write_setting_i(sesskey, "BellOverload", cfg->bellovl);
249 write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
250 write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
251 write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
252 write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
253 write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
254 write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
255 write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
256 write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
257 write_setting_s(sesskey, "WinTitle", cfg->wintitle);
258 write_setting_i(sesskey, "TermWidth", cfg->width);
259 write_setting_i(sesskey, "TermHeight", cfg->height);
260 write_setting_s(sesskey, "Font", cfg->font);
261 write_setting_i(sesskey, "FontIsBold", cfg->fontisbold);
262 write_setting_i(sesskey, "FontCharSet", cfg->fontcharset);
263 write_setting_i(sesskey, "FontHeight", cfg->fontheight);
264 write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
265 write_setting_i(sesskey, "TryPalette", cfg->try_palette);
266 write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
267 for (i = 0; i < 22; i++) {
a9422f39 268 char buf[20], buf2[30];
269 sprintf(buf, "Colour%d", i);
270 sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
271 cfg->colours[i][1], cfg->colours[i][2]);
32874aea 272 write_setting_s(sesskey, buf, buf2);
a9422f39 273 }
32874aea 274 write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
a7419ea4 275 write_setting_i(sesskey, "PasteRTF", cfg->rtf_paste);
32874aea 276 write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
6908fed7 277 write_setting_i(sesskey, "RectSelect", cfg->rect_select);
b90840c3 278 write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
32874aea 279 for (i = 0; i < 256; i += 32) {
a9422f39 280 char buf[20], buf2[256];
281 int j;
282 sprintf(buf, "Wordness%d", i);
283 *buf2 = '\0';
32874aea 284 for (j = i; j < i + 32; j++) {
285 sprintf(buf2 + strlen(buf2), "%s%d",
a9422f39 286 (*buf2 ? "," : ""), cfg->wordness[j]);
287 }
32874aea 288 write_setting_s(sesskey, buf, buf2);
a9422f39 289 }
4eeb7d09 290 write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
b44b307a 291 write_setting_s(sesskey, "Printer", cfg->printer);
a9c02454 292 write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
32874aea 293 write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
a401e5f3 294 write_setting_i(sesskey, "ScrollBarFullScreen", cfg->scrollbar_in_fullscreen);
32874aea 295 write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
296 write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
ad5c93cc 297 write_setting_i(sesskey, "LockSize", cfg->resize_action);
32874aea 298 write_setting_i(sesskey, "BCE", cfg->bce);
299 write_setting_i(sesskey, "BlinkText", cfg->blinktext);
300 write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
301 write_setting_s(sesskey, "X11Display", cfg->x11_display);
b3ebaa28 302 write_setting_i(sesskey, "X11AuthType", cfg->x11_auth);
d74d141c 303 write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
beefa433 304 write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
d74d141c 305 {
306 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
307 p = buf;
308 q = cfg->portfwd;
309 while (*q) {
310 while (*q) {
311 int c = *q++;
312 if (c == '=' || c == ',' || c == '\\')
313 *p++ = '\\';
314 if (c == '\t')
315 c = '=';
316 *p++ = c;
317 }
318 *p++ = ',';
319 q++;
320 }
321 *p = '\0';
322 write_setting_s(sesskey, "PortForwardings", buf);
323 }
2c9c6388 324 write_setting_i(sesskey, "BugIgnore1", cfg->sshbug_ignore1);
325 write_setting_i(sesskey, "BugPlainPW1", cfg->sshbug_plainpw1);
326 write_setting_i(sesskey, "BugRSA1", cfg->sshbug_rsa1);
327 write_setting_i(sesskey, "BugHMAC2", cfg->sshbug_hmac2);
328 write_setting_i(sesskey, "BugDeriveKey2", cfg->sshbug_derivekey2);
329 write_setting_i(sesskey, "BugRSAPad2", cfg->sshbug_rsapad2);
8e975795 330 write_setting_i(sesskey, "BugDHGEx2", cfg->sshbug_dhgex2);
c8ee61b9 331 write_setting_i(sesskey, "StampUtmp", cfg->stamp_utmp);
332 write_setting_i(sesskey, "LoginShell", cfg->login_shell);
239b3b36 333 write_setting_i(sesskey, "ScrollbarOnLeft", cfg->scrollbar_on_left);
2ec078fa 334 write_setting_s(sesskey, "BoldFont", cfg->boldfont);
12994a99 335 write_setting_i(sesskey, "ShadowBoldOffset", cfg->shadowboldoffset);
a9422f39 336}
337
32874aea 338void load_settings(char *section, int do_host, Config * cfg)
339{
a9422f39 340 void *sesskey;
341
342 sesskey = open_settings_r(section);
ce283213 343 load_open_settings(sesskey, do_host, cfg);
344 close_settings_r(sesskey);
345}
346
347void load_open_settings(void *sesskey, int do_host, Config *cfg)
348{
349 int i;
350 char prot[10];
a9422f39 351
32874aea 352 cfg->ssh_subsys = 0; /* FIXME: load this properly */
96621a84 353 cfg->remote_cmd_ptr = cfg->remote_cmd;
fd5e5847 354 cfg->remote_cmd_ptr2 = NULL;
a9100732 355
3b2eb121 356 if (do_host) {
357 gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
358 } else {
359 cfg->host[0] = '\0'; /* blank hostname */
360 }
32874aea 361 gpps(sesskey, "LogFileName", "putty.log",
362 cfg->logfilename, sizeof(cfg->logfilename));
363 gppi(sesskey, "LogType", 0, &cfg->logtype);
364 gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
a9422f39 365
32874aea 366 gpps(sesskey, "Protocol", "default", prot, 10);
a9422f39 367 cfg->protocol = default_protocol;
1ff52e17 368 cfg->port = default_port;
a9422f39 369 for (i = 0; backends[i].name != NULL; i++)
32874aea 370 if (!strcmp(prot, backends[i].name)) {
371 cfg->protocol = backends[i].protocol;
1ff52e17 372 gppi(sesskey, "PortNumber", default_port, &cfg->port);
32874aea 373 break;
374 }
a9422f39 375
5ecd7ad0 376 gppi(sesskey, "CloseOnExit", AUTO, &cfg->close_on_exit);
32874aea 377 gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
edce8e45 378 {
32874aea 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;
edce8e45 384 }
2184a5d9 385 gppi(sesskey, "TCPNoDelay", 1, &cfg->tcp_nodelay);
32874aea 386 gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
387 sizeof(cfg->termtype));
388 gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
389 sizeof(cfg->termspeed));
8eebd221 390
391 /* proxy settings */
392 gpps(sesskey, "ProxyExcludeList", "", cfg->proxy_exclude_list,
393 sizeof(cfg->proxy_exclude_list));
5ecd7ad0 394 gppi(sesskey, "ProxyDNS", AUTO, &cfg->proxy_dns);
b804e1e5 395 gppi(sesskey, "ProxyLocalhost", 0, &cfg->even_proxy_localhost);
5ecd7ad0 396 gppi(sesskey, "ProxyType", PROXY_NONE, &cfg->proxy_type);
8eebd221 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));
0e8f4cda 404 gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
8eebd221 405 cfg->proxy_telnet_command, sizeof(cfg->proxy_telnet_command));
406 gppi(sesskey, "ProxySOCKSVersion", 5, &cfg->proxy_socks_version);
407
a9422f39 408 {
32874aea 409 char buf[2 * sizeof(cfg->environmt)], *p, *q;
410 gpps(sesskey, "Environment", "", buf, sizeof(buf));
a9422f39 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 }
32874aea 422 if (*p == ',')
423 p++;
a9422f39 424 *q++ = '\0';
425 }
426 *q = '\0';
427 }
32874aea 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);
5bb641e1 434 gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
e411be57 435 gprefs(sesskey, "Cipher", "\0",
436 ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
102b17eb 437 gppi(sesskey, "SshProt", 2, &cfg->sshprot);
cb4d4768 438 gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
32874aea 439 gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
f091e308 440 gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
32874aea 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);
8faa456c 445 gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
32874aea 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);
c0d36a72 451 gppi(sesskey, "NoMouseReporting", 0, &cfg->no_mouse_rep);
0d2086c5 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);
32874aea 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);
a5f3e637 465 gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
eee63b77 466 gppi(sesskey, "TelnetRet", 1, &cfg->telnet_newline);
5ecd7ad0 467 gppi(sesskey, "LocalEcho", AUTO, &cfg->localecho);
468 gppi(sesskey, "LocalEdit", AUTO, &cfg->localedit);
32874aea 469 gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
470 sizeof(cfg->answerback));
471 gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
8f57d753 472 gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
32874aea 473 gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
474 gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
5a73255e 475 gppi(sesskey, "WindowBorder", 1, &cfg->window_border);
32874aea 476 gppi(sesskey, "CurType", 0, &cfg->cursor_type);
477 gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
2d466ffd 478 /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
5ecd7ad0 479 gppi(sesskey, "Beep", 1, &cfg->beep);
480 gppi(sesskey, "BeepInd", 0, &cfg->beep_ind);
32874aea 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);
b1c10a70 485 gppi(sesskey, "BellOverloadT", 2*TICKSPERSEC, &cfg->bellovl_t);
486 gppi(sesskey, "BellOverloadS", 5*TICKSPERSEC, &cfg->bellovl_s);
32874aea 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);
505db88d 491 gppi(sesskey, "WinNameAlways", 1, &cfg->win_name_always);
32874aea 492 gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
493 gppi(sesskey, "TermWidth", 80, &cfg->width);
494 gppi(sesskey, "TermHeight", 24, &cfg->height);
5a9eb105 495 gpps(sesskey, "Font", "XXX", cfg->font, sizeof(cfg->font));
32874aea 496 gppi(sesskey, "FontIsBold", 0, &cfg->fontisbold);
5a9eb105 497 gppi(sesskey, "FontCharSet", 0, &cfg->fontcharset);
32874aea 498 gppi(sesskey, "FontHeight", 10, &cfg->fontheight);
1709795f 499#ifdef _WINDOWS
3b2c664e 500 if (cfg->fontheight < 0) {
32874aea 501 int oldh, newh;
502 HDC hdc = GetDC(NULL);
503 int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
504 ReleaseDC(NULL, hdc);
3b2c664e 505
32874aea 506 oldh = -cfg->fontheight;
507 newh = MulDiv(oldh, 72, logpix) + 1;
508 if (MulDiv(newh, logpix, 72) > oldh)
509 newh--;
510 cfg->fontheight = newh;
3b2c664e 511 }
1709795f 512#endif
5a73255e 513 gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
32874aea 514 gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
515 gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
516 for (i = 0; i < 22; i++) {
c85623f9 517 static const char *const defaults[] = {
a9422f39 518 "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
519 "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
520 "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
521 "85,85,255", "187,0,187", "255,85,255", "0,187,187",
522 "85,255,255", "187,187,187", "255,255,255"
32874aea 523 };
a9422f39 524 char buf[20], buf2[30];
525 int c0, c1, c2;
526 sprintf(buf, "Colour%d", i);
32874aea 527 gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
528 if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
a9422f39 529 cfg->colours[i][0] = c0;
530 cfg->colours[i][1] = c1;
531 cfg->colours[i][2] = c2;
532 }
533 }
32874aea 534 gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
a7419ea4 535 gppi(sesskey, "PasteRTF", 0, &cfg->rtf_paste);
32874aea 536 gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
6908fed7 537 gppi(sesskey, "RectSelect", 0, &cfg->rect_select);
b90840c3 538 gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
32874aea 539 for (i = 0; i < 256; i += 32) {
c85623f9 540 static const char *const defaults[] = {
a9422f39 541 "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",
542 "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",
543 "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",
544 "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",
545 "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",
546 "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",
547 "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",
548 "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"
549 };
550 char buf[20], buf2[256], *p;
551 int j;
552 sprintf(buf, "Wordness%d", i);
32874aea 553 gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
a9422f39 554 p = buf2;
32874aea 555 for (j = i; j < i + 32; j++) {
a9422f39 556 char *q = p;
32874aea 557 while (*p && *p != ',')
558 p++;
559 if (*p == ',')
560 *p++ = '\0';
a9422f39 561 cfg->wordness[j] = atoi(q);
562 }
563 }
b8ae1f0f 564 /*
565 * The empty default for LineCodePage will be converted later
566 * into a plausible default for the locale.
567 */
568 gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
4eeb7d09 569 sizeof(cfg->line_codepage));
b44b307a 570 gpps(sesskey, "Printer", "", cfg->printer, sizeof(cfg->printer));
a9c02454 571 gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
32874aea 572 gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
a401e5f3 573 gppi(sesskey, "ScrollBarFullScreen", 0, &cfg->scrollbar_in_fullscreen);
32874aea 574 gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
575 gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
5ecd7ad0 576 gppi(sesskey, "LockSize", 0, &cfg->resize_action);
102b17eb 577 gppi(sesskey, "BCE", 1, &cfg->bce);
32874aea 578 gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
579 gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
580 gpps(sesskey, "X11Display", "localhost:0", cfg->x11_display,
581 sizeof(cfg->x11_display));
b3ebaa28 582 gppi(sesskey, "X11AuthType", X11_MIT, &cfg->x11_auth);
a9422f39 583
d74d141c 584 gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
beefa433 585 gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
d74d141c 586 {
587 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
588 gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
589 p = buf;
590 q = cfg->portfwd;
591 while (*p) {
592 while (*p && *p != ',') {
593 int c = *p++;
594 if (c == '=')
595 c = '\t';
596 if (c == '\\')
597 c = *p++;
598 *q++ = c;
599 }
600 if (*p == ',')
601 p++;
602 *q++ = '\0';
603 }
604 *q = '\0';
605 }
5ecd7ad0 606 gppi(sesskey, "BugIgnore1", AUTO, &cfg->sshbug_ignore1);
607 gppi(sesskey, "BugPlainPW1", AUTO, &cfg->sshbug_plainpw1);
608 gppi(sesskey, "BugRSA1", AUTO, &cfg->sshbug_rsa1);
2c9c6388 609 {
610 int i;
5ecd7ad0 611 gppi(sesskey, "BugHMAC2", AUTO, &cfg->sshbug_hmac2);
612 if (cfg->sshbug_hmac2 == AUTO) {
2c9c6388 613 gppi(sesskey, "BuggyMAC", 0, &i);
614 if (i == 1)
5ecd7ad0 615 cfg->sshbug_hmac2 = FORCE_ON;
2c9c6388 616 }
617 }
5ecd7ad0 618 gppi(sesskey, "BugDeriveKey2", AUTO, &cfg->sshbug_derivekey2);
619 gppi(sesskey, "BugRSAPad2", AUTO, &cfg->sshbug_rsapad2);
620 gppi(sesskey, "BugDHGEx2", AUTO, &cfg->sshbug_dhgex2);
c8ee61b9 621 gppi(sesskey, "StampUtmp", 1, &cfg->stamp_utmp);
622 gppi(sesskey, "LoginShell", 1, &cfg->login_shell);
239b3b36 623 gppi(sesskey, "ScrollbarOnLeft", 0, &cfg->scrollbar_on_left);
2ec078fa 624 gpps(sesskey, "BoldFont", "", cfg->boldfont, sizeof(cfg->boldfont));
623f81b7 625 gppi(sesskey, "ShadowBoldOffset", 1, &cfg->shadowboldoffset);
a9422f39 626}
627
32874aea 628void do_defaults(char *session, Config * cfg)
629{
a9422f39 630 if (session)
32874aea 631 load_settings(session, TRUE, cfg);
a9422f39 632 else
32874aea 633 load_settings("Default Settings", FALSE, cfg);
a9422f39 634}
635
32874aea 636static int sessioncmp(const void *av, const void *bv)
637{
638 const char *a = *(const char *const *) av;
639 const char *b = *(const char *const *) bv;
a84ca2f5 640
641 /*
642 * Alphabetical order, except that "Default Settings" is a
643 * special case and comes first.
644 */
645 if (!strcmp(a, "Default Settings"))
32874aea 646 return -1; /* a comes first */
a84ca2f5 647 if (!strcmp(b, "Default Settings"))
32874aea 648 return +1; /* b comes first */
78a8d889 649 /*
650 * FIXME: perhaps we should ignore the first & in determining
651 * sort order.
652 */
32874aea 653 return strcmp(a, b); /* otherwise, compare normally */
a84ca2f5 654}
655
0b4f0bc0 656void get_sesslist(struct sesslist *list, int allocate)
32874aea 657{
0b4f0bc0 658 char otherbuf[2048];
a9422f39 659 int buflen, bufsize, i;
660 char *p, *ret;
661 void *handle;
662
663 if (allocate) {
32874aea 664
a9422f39 665 buflen = bufsize = 0;
0b4f0bc0 666 list->buffer = NULL;
1728c012 667 if ((handle = enum_settings_start()) != NULL) {
5f2df4d2 668 do {
669 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
670 if (ret) {
671 int len = strlen(otherbuf) + 1;
672 if (bufsize < buflen + len) {
673 bufsize = buflen + len + 2048;
0b4f0bc0 674 list->buffer = srealloc(list->buffer, bufsize);
5f2df4d2 675 }
0b4f0bc0 676 strcpy(list->buffer + buflen, otherbuf);
677 buflen += strlen(list->buffer + buflen) + 1;
32874aea 678 }
5f2df4d2 679 } while (ret);
680 enum_settings_finish(handle);
681 }
0b4f0bc0 682 list->buffer = srealloc(list->buffer, buflen + 1);
683 list->buffer[buflen] = '\0';
a9422f39 684
2221af18 685 /*
686 * Now set up the list of sessions. Note that "Default
687 * Settings" must always be claimed to exist, even if it
688 * doesn't really.
689 */
690
0b4f0bc0 691 p = list->buffer;
692 list->nsessions = 1; /* "Default Settings" counts as one */
a9422f39 693 while (*p) {
32874aea 694 if (strcmp(p, "Default Settings"))
0b4f0bc0 695 list->nsessions++;
32874aea 696 while (*p)
697 p++;
a9422f39 698 p++;
699 }
700
0b4f0bc0 701 list->sessions = smalloc((list->nsessions + 1) * sizeof(char *));
702 list->sessions[0] = "Default Settings";
703 p = list->buffer;
2221af18 704 i = 1;
a9422f39 705 while (*p) {
32874aea 706 if (strcmp(p, "Default Settings"))
0b4f0bc0 707 list->sessions[i++] = p;
32874aea 708 while (*p)
709 p++;
a9422f39 710 p++;
711 }
a84ca2f5 712
0b4f0bc0 713 qsort(list->sessions, i, sizeof(char *), sessioncmp);
a9422f39 714 } else {
0b4f0bc0 715 sfree(list->buffer);
716 sfree(list->sessions);
a9422f39 717 }
718}