Well, there was bound to be one I'd forgotten: the new Features
[sgt/putty] / settings.c
CommitLineData
a9422f39 1/*
2 * settings.c: read and write saved sessions.
3 */
4
5#include <windows.h>
6#include <stdio.h>
49bad831 7#include <stdlib.h>
a9422f39 8#include "putty.h"
9#include "storage.h"
10
ca20bfcf 11/*
12 * Tables of string <-> enum value mappings
13 */
14struct keyval { char *s; int v; };
15
16static const struct keyval ciphernames[] = {
ca20bfcf 17 { "aes", CIPHER_AES },
e411be57 18 { "blowfish", CIPHER_BLOWFISH },
19 { "3des", CIPHER_3DES },
20 { "WARN", CIPHER_WARN },
ca20bfcf 21 { "des", CIPHER_DES }
22};
23
32874aea 24static void gpps(void *handle, char *name, char *def, char *val, int len)
25{
a9422f39 26 if (!read_setting_s(handle, name, val, len)) {
27 strncpy(val, def, len);
32874aea 28 val[len - 1] = '\0';
a9422f39 29 }
30}
31
32874aea 32static void gppi(void *handle, char *name, int def, int *i)
33{
a9422f39 34 *i = read_setting_i(handle, name, def);
35}
36
ca20bfcf 37static int key2val(const struct keyval *mapping, int nmaps, char *key)
38{
39 int i;
40 for (i = 0; i < nmaps; i++)
41 if (!strcmp(mapping[i].s, key)) return mapping[i].v;
42 return -1;
43}
44
45static const char *val2key(const struct keyval *mapping, int nmaps, int val)
46{
47 int i;
48 for (i = 0; i < nmaps; i++)
49 if (mapping[i].v == val) return mapping[i].s;
50 return NULL;
51}
52
53/*
54 * Helper function to parse a comma-separated list of strings into
55 * a preference list array of values. Any missing values are added
56 * to the end and duplicates are weeded.
57 * XXX: assumes vals in 'mapping' are small +ve integers
58 */
59static void gprefs(void *sesskey, char *name, char *def,
60 const struct keyval *mapping, int nvals,
61 int *array)
62{
63 char commalist[80];
64 int n;
65 unsigned long seen = 0; /* bitmap for weeding dups etc */
66 gpps(sesskey, name, def, commalist, sizeof(commalist));
67
68 /* Grotty parsing of commalist. */
69 n = 0;
70 do {
71 int v;
72 char *key;
73 key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
74 if (!key) break;
75 if (((v = key2val(mapping, nvals, key)) != -1) &&
76 !(seen & 1<<v)) {
77 array[n] = v;
78 n++;
79 seen |= 1<<v;
80 }
81 } while (n < nvals);
82 /* Add any missing values (backward compatibility ect). */
83 {
cdcbdf3b 84 int i;
ca20bfcf 85 for (i = 0; i < nvals; i++) {
86 if (!(seen & 1<<mapping[i].v)) {
87 array[n] = mapping[i].v;
88 n++;
89 }
90 }
91 }
92}
93
94/*
95 * Write out a preference list.
96 */
97static void wprefs(void *sesskey, char *name,
98 const struct keyval *mapping, int nvals,
99 int *array)
100{
101 char buf[80] = ""; /* XXX assumed big enough */
102 int l = sizeof(buf)-1, i;
103 buf[l] = '\0';
104 for (i = 0; l > 0 && i < nvals; i++) {
105 const char *s = val2key(mapping, nvals, array[i]);
106 if (s) {
107 int sl = strlen(s);
108 if (i > 0) {
109 strncat(buf, ",", l);
110 l--;
111 }
112 strncat(buf, s, l);
113 l -= sl;
114 }
115 }
116 write_setting_s(sesskey, name, buf);
117}
118
32874aea 119void save_settings(char *section, int do_host, Config * cfg)
120{
a9422f39 121 int i;
122 char *p;
123 void *sesskey;
124
125 sesskey = open_settings_w(section);
126 if (!sesskey)
32874aea 127 return;
a9422f39 128
32874aea 129 write_setting_i(sesskey, "Present", 1);
a9422f39 130 if (do_host) {
32874aea 131 write_setting_s(sesskey, "HostName", cfg->host);
32874aea 132 write_setting_s(sesskey, "LogFileName", cfg->logfilename);
133 write_setting_i(sesskey, "LogType", cfg->logtype);
134 write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
a9422f39 135 }
348a9ad6 136 p = "raw";
137 for (i = 0; backends[i].name != NULL; i++)
138 if (backends[i].protocol == cfg->protocol) {
139 p = backends[i].name;
140 break;
141 }
142 write_setting_s(sesskey, "Protocol", p);
143 write_setting_i(sesskey, "PortNumber", cfg->port);
32874aea 144 write_setting_i(sesskey, "CloseOnExit", cfg->close_on_exit);
145 write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
146 write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60); /* minutes */
147 write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60); /* seconds */
2184a5d9 148 write_setting_i(sesskey, "TCPNoDelay", cfg->tcp_nodelay);
32874aea 149 write_setting_s(sesskey, "TerminalType", cfg->termtype);
150 write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
a9422f39 151 {
32874aea 152 char buf[2 * sizeof(cfg->environmt)], *p, *q;
a9422f39 153 p = buf;
32874aea 154 q = cfg->environmt;
a9422f39 155 while (*q) {
156 while (*q) {
157 int c = *q++;
158 if (c == '=' || c == ',' || c == '\\')
159 *p++ = '\\';
160 if (c == '\t')
161 c = '=';
162 *p++ = c;
163 }
164 *p++ = ',';
165 q++;
166 }
167 *p = '\0';
32874aea 168 write_setting_s(sesskey, "Environment", buf);
a9422f39 169 }
32874aea 170 write_setting_s(sesskey, "UserName", cfg->username);
171 write_setting_s(sesskey, "LocalUserName", cfg->localusername);
172 write_setting_i(sesskey, "NoPTY", cfg->nopty);
173 write_setting_i(sesskey, "Compression", cfg->compression);
174 write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
5bb641e1 175 write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
e411be57 176 wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
ca20bfcf 177 cfg->ssh_cipherlist);
32874aea 178 write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
f091e308 179 write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
32874aea 180 write_setting_i(sesskey, "SshProt", cfg->sshprot);
181 write_setting_i(sesskey, "BuggyMAC", cfg->buggymac);
cb4d4768 182 write_setting_i(sesskey, "SSH2DES", cfg->ssh2_des_cbc);
32874aea 183 write_setting_s(sesskey, "PublicKeyFile", cfg->keyfile);
184 write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
185 write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
8faa456c 186 write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
32874aea 187 write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
188 write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
189 write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
190 write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
191 write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
c0d36a72 192 write_setting_i(sesskey, "NoMouseReporting", cfg->no_mouse_rep);
0d2086c5 193 write_setting_i(sesskey, "NoRemoteResize", cfg->no_remote_resize);
194 write_setting_i(sesskey, "NoAltScreen", cfg->no_alt_screen);
195 write_setting_i(sesskey, "NoRemoteWinTitle", cfg->no_remote_wintitle);
196 write_setting_i(sesskey, "NoDBackspace", cfg->no_dbackspace);
197 write_setting_i(sesskey, "NoRemoteCharset", cfg->no_remote_charset);
32874aea 198 write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
199 write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
200 write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
201 write_setting_i(sesskey, "AltF4", cfg->alt_f4);
202 write_setting_i(sesskey, "AltSpace", cfg->alt_space);
203 write_setting_i(sesskey, "AltOnly", cfg->alt_only);
204 write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
205 write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
a5f3e637 206 write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
eee63b77 207 write_setting_i(sesskey, "TelnetRet", cfg->telnet_newline);
32874aea 208 write_setting_i(sesskey, "LocalEcho", cfg->localecho);
209 write_setting_i(sesskey, "LocalEdit", cfg->localedit);
210 write_setting_s(sesskey, "Answerback", cfg->answerback);
211 write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
8f57d753 212 write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
32874aea 213 write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
214 write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
5a73255e 215 write_setting_i(sesskey, "WindowBorder", cfg->window_border);
32874aea 216 write_setting_i(sesskey, "CurType", cfg->cursor_type);
217 write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
218 write_setting_i(sesskey, "Beep", cfg->beep);
f8a28d1f 219 write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
32874aea 220 write_setting_s(sesskey, "BellWaveFile", cfg->bell_wavefile);
221 write_setting_i(sesskey, "BellOverload", cfg->bellovl);
222 write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
223 write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
224 write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
225 write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
226 write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
227 write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
228 write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
229 write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
230 write_setting_s(sesskey, "WinTitle", cfg->wintitle);
231 write_setting_i(sesskey, "TermWidth", cfg->width);
232 write_setting_i(sesskey, "TermHeight", cfg->height);
233 write_setting_s(sesskey, "Font", cfg->font);
234 write_setting_i(sesskey, "FontIsBold", cfg->fontisbold);
235 write_setting_i(sesskey, "FontCharSet", cfg->fontcharset);
236 write_setting_i(sesskey, "FontHeight", cfg->fontheight);
237 write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
238 write_setting_i(sesskey, "TryPalette", cfg->try_palette);
239 write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
240 for (i = 0; i < 22; i++) {
a9422f39 241 char buf[20], buf2[30];
242 sprintf(buf, "Colour%d", i);
243 sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
244 cfg->colours[i][1], cfg->colours[i][2]);
32874aea 245 write_setting_s(sesskey, buf, buf2);
a9422f39 246 }
32874aea 247 write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
a7419ea4 248 write_setting_i(sesskey, "PasteRTF", cfg->rtf_paste);
32874aea 249 write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
6908fed7 250 write_setting_i(sesskey, "RectSelect", cfg->rect_select);
b90840c3 251 write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
32874aea 252 for (i = 0; i < 256; i += 32) {
a9422f39 253 char buf[20], buf2[256];
254 int j;
255 sprintf(buf, "Wordness%d", i);
256 *buf2 = '\0';
32874aea 257 for (j = i; j < i + 32; j++) {
258 sprintf(buf2 + strlen(buf2), "%s%d",
a9422f39 259 (*buf2 ? "," : ""), cfg->wordness[j]);
260 }
32874aea 261 write_setting_s(sesskey, buf, buf2);
a9422f39 262 }
4eeb7d09 263 write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
a9c02454 264 write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
32874aea 265 write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
a401e5f3 266 write_setting_i(sesskey, "ScrollBarFullScreen", cfg->scrollbar_in_fullscreen);
32874aea 267 write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
268 write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
ad5c93cc 269 write_setting_i(sesskey, "LockSize", cfg->resize_action);
32874aea 270 write_setting_i(sesskey, "BCE", cfg->bce);
271 write_setting_i(sesskey, "BlinkText", cfg->blinktext);
272 write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
273 write_setting_s(sesskey, "X11Display", cfg->x11_display);
d74d141c 274 write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
beefa433 275 write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
d74d141c 276 {
277 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
278 p = buf;
279 q = cfg->portfwd;
280 while (*q) {
281 while (*q) {
282 int c = *q++;
283 if (c == '=' || c == ',' || c == '\\')
284 *p++ = '\\';
285 if (c == '\t')
286 c = '=';
287 *p++ = c;
288 }
289 *p++ = ',';
290 q++;
291 }
292 *p = '\0';
293 write_setting_s(sesskey, "PortForwardings", buf);
294 }
a9422f39 295
296 close_settings_w(sesskey);
297}
298
32874aea 299void load_settings(char *section, int do_host, Config * cfg)
300{
a9422f39 301 int i;
302 char prot[10];
303 void *sesskey;
304
305 sesskey = open_settings_r(section);
306
32874aea 307 cfg->ssh_subsys = 0; /* FIXME: load this properly */
96621a84 308 cfg->remote_cmd_ptr = cfg->remote_cmd;
fd5e5847 309 cfg->remote_cmd_ptr2 = NULL;
a9100732 310
3b2eb121 311 if (do_host) {
312 gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
313 } else {
314 cfg->host[0] = '\0'; /* blank hostname */
315 }
32874aea 316 gpps(sesskey, "LogFileName", "putty.log",
317 cfg->logfilename, sizeof(cfg->logfilename));
318 gppi(sesskey, "LogType", 0, &cfg->logtype);
319 gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
a9422f39 320
32874aea 321 gpps(sesskey, "Protocol", "default", prot, 10);
a9422f39 322 cfg->protocol = default_protocol;
1ff52e17 323 cfg->port = default_port;
a9422f39 324 for (i = 0; backends[i].name != NULL; i++)
32874aea 325 if (!strcmp(prot, backends[i].name)) {
326 cfg->protocol = backends[i].protocol;
1ff52e17 327 gppi(sesskey, "PortNumber", default_port, &cfg->port);
32874aea 328 break;
329 }
a9422f39 330
32874aea 331 gppi(sesskey, "CloseOnExit", COE_NORMAL, &cfg->close_on_exit);
332 gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
edce8e45 333 {
32874aea 334 /* This is two values for backward compatibility with 0.50/0.51 */
335 int pingmin, pingsec;
336 gppi(sesskey, "PingInterval", 0, &pingmin);
337 gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
338 cfg->ping_interval = pingmin * 60 + pingsec;
edce8e45 339 }
2184a5d9 340 gppi(sesskey, "TCPNoDelay", 1, &cfg->tcp_nodelay);
32874aea 341 gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
342 sizeof(cfg->termtype));
343 gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
344 sizeof(cfg->termspeed));
a9422f39 345 {
32874aea 346 char buf[2 * sizeof(cfg->environmt)], *p, *q;
347 gpps(sesskey, "Environment", "", buf, sizeof(buf));
a9422f39 348 p = buf;
349 q = cfg->environmt;
350 while (*p) {
351 while (*p && *p != ',') {
352 int c = *p++;
353 if (c == '=')
354 c = '\t';
355 if (c == '\\')
356 c = *p++;
357 *q++ = c;
358 }
32874aea 359 if (*p == ',')
360 p++;
a9422f39 361 *q++ = '\0';
362 }
363 *q = '\0';
364 }
32874aea 365 gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
366 gpps(sesskey, "LocalUserName", "", cfg->localusername,
367 sizeof(cfg->localusername));
368 gppi(sesskey, "NoPTY", 0, &cfg->nopty);
369 gppi(sesskey, "Compression", 0, &cfg->compression);
370 gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
5bb641e1 371 gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
e411be57 372 gprefs(sesskey, "Cipher", "\0",
373 ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
32874aea 374 gppi(sesskey, "SshProt", 1, &cfg->sshprot);
375 gppi(sesskey, "BuggyMAC", 0, &cfg->buggymac);
cb4d4768 376 gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
32874aea 377 gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
f091e308 378 gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
32874aea 379 gpps(sesskey, "PublicKeyFile", "", cfg->keyfile, sizeof(cfg->keyfile));
380 gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
381 sizeof(cfg->remote_cmd));
382 gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
8faa456c 383 gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
32874aea 384 gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
385 gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
386 gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
387 gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
388 gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
c0d36a72 389 gppi(sesskey, "NoMouseReporting", 0, &cfg->no_mouse_rep);
0d2086c5 390 gppi(sesskey, "NoRemoteResize", 0, &cfg->no_remote_resize);
391 gppi(sesskey, "NoAltScreen", 0, &cfg->no_alt_screen);
392 gppi(sesskey, "NoRemoteWinTitle", 0, &cfg->no_remote_wintitle);
393 gppi(sesskey, "NoDBackspace", 0, &cfg->no_dbackspace);
394 gppi(sesskey, "NoRemoteCharset", 0, &cfg->no_remote_charset);
32874aea 395 gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
396 gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
397 gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
398 gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
399 gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
400 gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
401 gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
402 gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
a5f3e637 403 gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
eee63b77 404 gppi(sesskey, "TelnetRet", 1, &cfg->telnet_newline);
32874aea 405 gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho);
406 gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit);
407 gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
408 sizeof(cfg->answerback));
409 gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
8f57d753 410 gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
32874aea 411 gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
412 gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
5a73255e 413 gppi(sesskey, "WindowBorder", 1, &cfg->window_border);
32874aea 414 gppi(sesskey, "CurType", 0, &cfg->cursor_type);
415 gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
2d466ffd 416 /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
417 gppi(sesskey, "Beep", 1, &i); cfg->beep = i;
f8a28d1f 418 gppi(sesskey, "BeepInd", 0, &i); cfg->beep_ind = i;
32874aea 419 gpps(sesskey, "BellWaveFile", "", cfg->bell_wavefile,
420 sizeof(cfg->bell_wavefile));
421 gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
422 gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
423 gppi(sesskey, "BellOverloadT", 2000, &cfg->bellovl_t);
424 gppi(sesskey, "BellOverloadS", 5000, &cfg->bellovl_s);
425 gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
426 gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
427 gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
428 gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
429 gppi(sesskey, "WinNameAlways", 0, &cfg->win_name_always);
430 gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
431 gppi(sesskey, "TermWidth", 80, &cfg->width);
432 gppi(sesskey, "TermHeight", 24, &cfg->height);
5a73255e 433 gpps(sesskey, "Font", "Courier New", cfg->font, sizeof(cfg->font));
32874aea 434 gppi(sesskey, "FontIsBold", 0, &cfg->fontisbold);
435 gppi(sesskey, "FontCharSet", ANSI_CHARSET, &cfg->fontcharset);
436 gppi(sesskey, "FontHeight", 10, &cfg->fontheight);
3b2c664e 437 if (cfg->fontheight < 0) {
32874aea 438 int oldh, newh;
439 HDC hdc = GetDC(NULL);
440 int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
441 ReleaseDC(NULL, hdc);
3b2c664e 442
32874aea 443 oldh = -cfg->fontheight;
444 newh = MulDiv(oldh, 72, logpix) + 1;
445 if (MulDiv(newh, logpix, 72) > oldh)
446 newh--;
447 cfg->fontheight = newh;
3b2c664e 448 }
5a73255e 449 gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
32874aea 450 gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
451 gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
452 for (i = 0; i < 22; i++) {
a9422f39 453 static char *defaults[] = {
454 "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
455 "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
456 "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
457 "85,85,255", "187,0,187", "255,85,255", "0,187,187",
458 "85,255,255", "187,187,187", "255,255,255"
32874aea 459 };
a9422f39 460 char buf[20], buf2[30];
461 int c0, c1, c2;
462 sprintf(buf, "Colour%d", i);
32874aea 463 gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
464 if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
a9422f39 465 cfg->colours[i][0] = c0;
466 cfg->colours[i][1] = c1;
467 cfg->colours[i][2] = c2;
468 }
469 }
32874aea 470 gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
a7419ea4 471 gppi(sesskey, "PasteRTF", 0, &cfg->rtf_paste);
32874aea 472 gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
6908fed7 473 gppi(sesskey, "RectSelect", 0, &cfg->rect_select);
b90840c3 474 gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
32874aea 475 for (i = 0; i < 256; i += 32) {
a9422f39 476 static char *defaults[] = {
477 "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",
478 "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",
479 "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",
480 "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",
481 "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",
482 "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",
483 "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",
484 "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"
485 };
486 char buf[20], buf2[256], *p;
487 int j;
488 sprintf(buf, "Wordness%d", i);
32874aea 489 gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
a9422f39 490 p = buf2;
32874aea 491 for (j = i; j < i + 32; j++) {
a9422f39 492 char *q = p;
32874aea 493 while (*p && *p != ',')
494 p++;
495 if (*p == ',')
496 *p++ = '\0';
a9422f39 497 cfg->wordness[j] = atoi(q);
498 }
499 }
b8ae1f0f 500 /*
501 * The empty default for LineCodePage will be converted later
502 * into a plausible default for the locale.
503 */
504 gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
4eeb7d09 505 sizeof(cfg->line_codepage));
a9c02454 506 gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
32874aea 507 gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
a401e5f3 508 gppi(sesskey, "ScrollBarFullScreen", 0, &cfg->scrollbar_in_fullscreen);
32874aea 509 gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
510 gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
ad5c93cc 511 gppi(sesskey, "LockSize", 0, &cfg->resize_action);
32874aea 512 gppi(sesskey, "BCE", 0, &cfg->bce);
513 gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
514 gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
515 gpps(sesskey, "X11Display", "localhost:0", cfg->x11_display,
516 sizeof(cfg->x11_display));
a9422f39 517
d74d141c 518 gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
beefa433 519 gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
d74d141c 520 {
521 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
522 gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
523 p = buf;
524 q = cfg->portfwd;
525 while (*p) {
526 while (*p && *p != ',') {
527 int c = *p++;
528 if (c == '=')
529 c = '\t';
530 if (c == '\\')
531 c = *p++;
532 *q++ = c;
533 }
534 if (*p == ',')
535 p++;
536 *q++ = '\0';
537 }
538 *q = '\0';
539 }
540
a9422f39 541 close_settings_r(sesskey);
542}
543
32874aea 544void do_defaults(char *session, Config * cfg)
545{
a9422f39 546 if (session)
32874aea 547 load_settings(session, TRUE, cfg);
a9422f39 548 else
32874aea 549 load_settings("Default Settings", FALSE, cfg);
a9422f39 550}
551
32874aea 552static int sessioncmp(const void *av, const void *bv)
553{
554 const char *a = *(const char *const *) av;
555 const char *b = *(const char *const *) bv;
a84ca2f5 556
557 /*
558 * Alphabetical order, except that "Default Settings" is a
559 * special case and comes first.
560 */
561 if (!strcmp(a, "Default Settings"))
32874aea 562 return -1; /* a comes first */
a84ca2f5 563 if (!strcmp(b, "Default Settings"))
32874aea 564 return +1; /* b comes first */
78a8d889 565 /*
566 * FIXME: perhaps we should ignore the first & in determining
567 * sort order.
568 */
32874aea 569 return strcmp(a, b); /* otherwise, compare normally */
a84ca2f5 570}
571
32874aea 572void get_sesslist(int allocate)
573{
a9422f39 574 static char otherbuf[2048];
575 static char *buffer;
576 int buflen, bufsize, i;
577 char *p, *ret;
578 void *handle;
579
580 if (allocate) {
32874aea 581
a9422f39 582 buflen = bufsize = 0;
583 buffer = NULL;
5f2df4d2 584 if ((handle = enum_settings_start())) {
585 do {
586 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
587 if (ret) {
588 int len = strlen(otherbuf) + 1;
589 if (bufsize < buflen + len) {
590 bufsize = buflen + len + 2048;
591 buffer = srealloc(buffer, bufsize);
592 }
593 strcpy(buffer + buflen, otherbuf);
594 buflen += strlen(buffer + buflen) + 1;
32874aea 595 }
5f2df4d2 596 } while (ret);
597 enum_settings_finish(handle);
598 }
32874aea 599 buffer = srealloc(buffer, buflen + 1);
a9422f39 600 buffer[buflen] = '\0';
601
2221af18 602 /*
603 * Now set up the list of sessions. Note that "Default
604 * Settings" must always be claimed to exist, even if it
605 * doesn't really.
606 */
607
a9422f39 608 p = buffer;
2221af18 609 nsessions = 1; /* "Default Settings" counts as one */
a9422f39 610 while (*p) {
32874aea 611 if (strcmp(p, "Default Settings"))
2221af18 612 nsessions++;
32874aea 613 while (*p)
614 p++;
a9422f39 615 p++;
616 }
617
32874aea 618 sessions = smalloc((nsessions + 1) * sizeof(char *));
2221af18 619 sessions[0] = "Default Settings";
a9422f39 620 p = buffer;
2221af18 621 i = 1;
a9422f39 622 while (*p) {
32874aea 623 if (strcmp(p, "Default Settings"))
2221af18 624 sessions[i++] = p;
32874aea 625 while (*p)
626 p++;
a9422f39 627 p++;
628 }
a84ca2f5 629
32874aea 630 qsort(sessions, i, sizeof(char *), sessioncmp);
a9422f39 631 } else {
32874aea 632 sfree(buffer);
633 sfree(sessions);
a9422f39 634 }
635}