Christopher Allene's patch for going full-screen on Alt-Enter.
[u/mdw/putty] / settings.c
1 /*
2 * settings.c: read and write saved sessions.
3 */
4
5 #include <windows.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "putty.h"
9 #include "storage.h"
10
11 /*
12 * Tables of string <-> enum value mappings
13 */
14 struct keyval { char *s; int v; };
15
16 static const struct keyval ciphernames[] = {
17 { "aes", CIPHER_AES },
18 { "blowfish", CIPHER_BLOWFISH },
19 { "3des", CIPHER_3DES },
20 { "WARN", CIPHER_WARN },
21 { "des", CIPHER_DES }
22 };
23
24 static void gpps(void *handle, char *name, char *def, char *val, int len)
25 {
26 if (!read_setting_s(handle, name, val, len)) {
27 strncpy(val, def, len);
28 val[len - 1] = '\0';
29 }
30 }
31
32 static void gppi(void *handle, char *name, int def, int *i)
33 {
34 *i = read_setting_i(handle, name, def);
35 }
36
37 static 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
45 static 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 */
59 static 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 {
84 int i;
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 */
97 static 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
119 void save_settings(char *section, int do_host, Config * cfg)
120 {
121 int i;
122 char *p;
123 void *sesskey;
124
125 sesskey = open_settings_w(section);
126 if (!sesskey)
127 return;
128
129 write_setting_i(sesskey, "Present", 1);
130 if (do_host) {
131 write_setting_s(sesskey, "HostName", cfg->host);
132 write_setting_s(sesskey, "LogFileName", cfg->logfilename);
133 write_setting_i(sesskey, "LogType", cfg->logtype);
134 write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
135 }
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);
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 */
148 write_setting_s(sesskey, "TerminalType", cfg->termtype);
149 write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
150 {
151 char buf[2 * sizeof(cfg->environmt)], *p, *q;
152 p = buf;
153 q = cfg->environmt;
154 while (*q) {
155 while (*q) {
156 int c = *q++;
157 if (c == '=' || c == ',' || c == '\\')
158 *p++ = '\\';
159 if (c == '\t')
160 c = '=';
161 *p++ = c;
162 }
163 *p++ = ',';
164 q++;
165 }
166 *p = '\0';
167 write_setting_s(sesskey, "Environment", buf);
168 }
169 write_setting_s(sesskey, "UserName", cfg->username);
170 write_setting_s(sesskey, "LocalUserName", cfg->localusername);
171 write_setting_i(sesskey, "NoPTY", cfg->nopty);
172 write_setting_i(sesskey, "Compression", cfg->compression);
173 write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
174 wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
175 cfg->ssh_cipherlist);
176 write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
177 write_setting_i(sesskey, "SshProt", cfg->sshprot);
178 write_setting_i(sesskey, "BuggyMAC", cfg->buggymac);
179 write_setting_s(sesskey, "PublicKeyFile", cfg->keyfile);
180 write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
181 write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
182 write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
183 write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
184 write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
185 write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
186 write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
187 write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
188 write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
189 write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
190 write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
191 write_setting_i(sesskey, "AltF4", cfg->alt_f4);
192 write_setting_i(sesskey, "AltSpace", cfg->alt_space);
193 write_setting_i(sesskey, "AltOnly", cfg->alt_only);
194 write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
195 write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
196 write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
197 write_setting_i(sesskey, "LocalEcho", cfg->localecho);
198 write_setting_i(sesskey, "LocalEdit", cfg->localedit);
199 write_setting_s(sesskey, "Answerback", cfg->answerback);
200 write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
201 write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
202 write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
203 write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
204 write_setting_i(sesskey, "CurType", cfg->cursor_type);
205 write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
206 write_setting_i(sesskey, "Beep", cfg->beep);
207 write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
208 write_setting_s(sesskey, "BellWaveFile", cfg->bell_wavefile);
209 write_setting_i(sesskey, "BellOverload", cfg->bellovl);
210 write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
211 write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
212 write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
213 write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
214 write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
215 write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
216 write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
217 write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
218 write_setting_s(sesskey, "WinTitle", cfg->wintitle);
219 write_setting_i(sesskey, "TermWidth", cfg->width);
220 write_setting_i(sesskey, "TermHeight", cfg->height);
221 write_setting_s(sesskey, "Font", cfg->font);
222 write_setting_i(sesskey, "FontIsBold", cfg->fontisbold);
223 write_setting_i(sesskey, "FontCharSet", cfg->fontcharset);
224 write_setting_i(sesskey, "FontHeight", cfg->fontheight);
225 write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
226 write_setting_i(sesskey, "TryPalette", cfg->try_palette);
227 write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
228 for (i = 0; i < 22; i++) {
229 char buf[20], buf2[30];
230 sprintf(buf, "Colour%d", i);
231 sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
232 cfg->colours[i][1], cfg->colours[i][2]);
233 write_setting_s(sesskey, buf, buf2);
234 }
235 write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
236 write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
237 write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
238 for (i = 0; i < 256; i += 32) {
239 char buf[20], buf2[256];
240 int j;
241 sprintf(buf, "Wordness%d", i);
242 *buf2 = '\0';
243 for (j = i; j < i + 32; j++) {
244 sprintf(buf2 + strlen(buf2), "%s%d",
245 (*buf2 ? "," : ""), cfg->wordness[j]);
246 }
247 write_setting_s(sesskey, buf, buf2);
248 }
249 write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
250 write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
251 write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
252 write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
253 write_setting_i(sesskey, "LockSize", cfg->locksize);
254 write_setting_i(sesskey, "BCE", cfg->bce);
255 write_setting_i(sesskey, "BlinkText", cfg->blinktext);
256 write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
257 write_setting_s(sesskey, "X11Display", cfg->x11_display);
258 write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
259 {
260 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
261 p = buf;
262 q = cfg->portfwd;
263 while (*q) {
264 while (*q) {
265 int c = *q++;
266 if (c == '=' || c == ',' || c == '\\')
267 *p++ = '\\';
268 if (c == '\t')
269 c = '=';
270 *p++ = c;
271 }
272 *p++ = ',';
273 q++;
274 }
275 *p = '\0';
276 write_setting_s(sesskey, "PortForwardings", buf);
277 }
278
279 close_settings_w(sesskey);
280 }
281
282 void load_settings(char *section, int do_host, Config * cfg)
283 {
284 int i;
285 char prot[10];
286 void *sesskey;
287
288 sesskey = open_settings_r(section);
289
290 cfg->ssh_subsys = 0; /* FIXME: load this properly */
291 cfg->remote_cmd_ptr = cfg->remote_cmd;
292 cfg->remote_cmd_ptr2 = NULL;
293
294 gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
295 gpps(sesskey, "LogFileName", "putty.log",
296 cfg->logfilename, sizeof(cfg->logfilename));
297 gppi(sesskey, "LogType", 0, &cfg->logtype);
298 gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
299
300 gpps(sesskey, "Protocol", "default", prot, 10);
301 cfg->protocol = default_protocol;
302 cfg->port = default_port;
303 for (i = 0; backends[i].name != NULL; i++)
304 if (!strcmp(prot, backends[i].name)) {
305 cfg->protocol = backends[i].protocol;
306 gppi(sesskey, "PortNumber", default_port, &cfg->port);
307 break;
308 }
309
310 gppi(sesskey, "CloseOnExit", COE_NORMAL, &cfg->close_on_exit);
311 gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
312 {
313 /* This is two values for backward compatibility with 0.50/0.51 */
314 int pingmin, pingsec;
315 gppi(sesskey, "PingInterval", 0, &pingmin);
316 gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
317 cfg->ping_interval = pingmin * 60 + pingsec;
318 }
319 gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
320 sizeof(cfg->termtype));
321 gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
322 sizeof(cfg->termspeed));
323 {
324 char buf[2 * sizeof(cfg->environmt)], *p, *q;
325 gpps(sesskey, "Environment", "", buf, sizeof(buf));
326 p = buf;
327 q = cfg->environmt;
328 while (*p) {
329 while (*p && *p != ',') {
330 int c = *p++;
331 if (c == '=')
332 c = '\t';
333 if (c == '\\')
334 c = *p++;
335 *q++ = c;
336 }
337 if (*p == ',')
338 p++;
339 *q++ = '\0';
340 }
341 *q = '\0';
342 }
343 gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
344 gpps(sesskey, "LocalUserName", "", cfg->localusername,
345 sizeof(cfg->localusername));
346 gppi(sesskey, "NoPTY", 0, &cfg->nopty);
347 gppi(sesskey, "Compression", 0, &cfg->compression);
348 gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
349 gprefs(sesskey, "Cipher", "\0",
350 ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
351 gppi(sesskey, "SshProt", 1, &cfg->sshprot);
352 gppi(sesskey, "BuggyMAC", 0, &cfg->buggymac);
353 gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
354 gpps(sesskey, "PublicKeyFile", "", cfg->keyfile, sizeof(cfg->keyfile));
355 gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
356 sizeof(cfg->remote_cmd));
357 gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
358 gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
359 gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
360 gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
361 gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
362 gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
363 gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
364 gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
365 gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
366 gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
367 gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
368 gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
369 gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
370 gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
371 gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
372 gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
373 gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho);
374 gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit);
375 gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
376 sizeof(cfg->answerback));
377 gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
378 gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
379 gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
380 gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
381 gppi(sesskey, "CurType", 0, &cfg->cursor_type);
382 gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
383 /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
384 gppi(sesskey, "Beep", 1, &i); cfg->beep = i;
385 gppi(sesskey, "BeepInd", 0, &i); cfg->beep_ind = i;
386 gpps(sesskey, "BellWaveFile", "", cfg->bell_wavefile,
387 sizeof(cfg->bell_wavefile));
388 gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
389 gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
390 gppi(sesskey, "BellOverloadT", 2000, &cfg->bellovl_t);
391 gppi(sesskey, "BellOverloadS", 5000, &cfg->bellovl_s);
392 gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
393 gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
394 gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
395 gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
396 gppi(sesskey, "WinNameAlways", 0, &cfg->win_name_always);
397 gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
398 gppi(sesskey, "TermWidth", 80, &cfg->width);
399 gppi(sesskey, "TermHeight", 24, &cfg->height);
400 gpps(sesskey, "Font", "Courier", cfg->font, sizeof(cfg->font));
401 gppi(sesskey, "FontIsBold", 0, &cfg->fontisbold);
402 gppi(sesskey, "FontCharSet", ANSI_CHARSET, &cfg->fontcharset);
403 gppi(sesskey, "FontHeight", 10, &cfg->fontheight);
404 if (cfg->fontheight < 0) {
405 int oldh, newh;
406 HDC hdc = GetDC(NULL);
407 int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
408 ReleaseDC(NULL, hdc);
409
410 oldh = -cfg->fontheight;
411 newh = MulDiv(oldh, 72, logpix) + 1;
412 if (MulDiv(newh, logpix, 72) > oldh)
413 newh--;
414 cfg->fontheight = newh;
415 }
416 gppi(sesskey, "FontVTMode", VT_OEMANSI, (int *) &cfg->vtmode);
417 gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
418 gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
419 for (i = 0; i < 22; i++) {
420 static char *defaults[] = {
421 "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
422 "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
423 "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
424 "85,85,255", "187,0,187", "255,85,255", "0,187,187",
425 "85,255,255", "187,187,187", "255,255,255"
426 };
427 char buf[20], buf2[30];
428 int c0, c1, c2;
429 sprintf(buf, "Colour%d", i);
430 gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
431 if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
432 cfg->colours[i][0] = c0;
433 cfg->colours[i][1] = c1;
434 cfg->colours[i][2] = c2;
435 }
436 }
437 gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
438 gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
439 gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
440 for (i = 0; i < 256; i += 32) {
441 static char *defaults[] = {
442 "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",
443 "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",
444 "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",
445 "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",
446 "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",
447 "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",
448 "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",
449 "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"
450 };
451 char buf[20], buf2[256], *p;
452 int j;
453 sprintf(buf, "Wordness%d", i);
454 gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
455 p = buf2;
456 for (j = i; j < i + 32; j++) {
457 char *q = p;
458 while (*p && *p != ',')
459 p++;
460 if (*p == ',')
461 *p++ = '\0';
462 cfg->wordness[j] = atoi(q);
463 }
464 }
465 /*
466 * The empty default for LineCodePage will be converted later
467 * into a plausible default for the locale.
468 */
469 gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
470 sizeof(cfg->line_codepage));
471 gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
472 gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
473 gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
474 gppi(sesskey, "LockSize", 0, &cfg->locksize);
475 gppi(sesskey, "BCE", 0, &cfg->bce);
476 gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
477 gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
478 gpps(sesskey, "X11Display", "localhost:0", cfg->x11_display,
479 sizeof(cfg->x11_display));
480
481 gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
482 {
483 char buf[2 * sizeof(cfg->portfwd)], *p, *q;
484 gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
485 p = buf;
486 q = cfg->portfwd;
487 while (*p) {
488 while (*p && *p != ',') {
489 int c = *p++;
490 if (c == '=')
491 c = '\t';
492 if (c == '\\')
493 c = *p++;
494 *q++ = c;
495 }
496 if (*p == ',')
497 p++;
498 *q++ = '\0';
499 }
500 *q = '\0';
501 }
502
503 close_settings_r(sesskey);
504 }
505
506 void do_defaults(char *session, Config * cfg)
507 {
508 if (session)
509 load_settings(session, TRUE, cfg);
510 else
511 load_settings("Default Settings", FALSE, cfg);
512 }
513
514 static int sessioncmp(const void *av, const void *bv)
515 {
516 const char *a = *(const char *const *) av;
517 const char *b = *(const char *const *) bv;
518
519 /*
520 * Alphabetical order, except that "Default Settings" is a
521 * special case and comes first.
522 */
523 if (!strcmp(a, "Default Settings"))
524 return -1; /* a comes first */
525 if (!strcmp(b, "Default Settings"))
526 return +1; /* b comes first */
527 return strcmp(a, b); /* otherwise, compare normally */
528 }
529
530 void get_sesslist(int allocate)
531 {
532 static char otherbuf[2048];
533 static char *buffer;
534 int buflen, bufsize, i;
535 char *p, *ret;
536 void *handle;
537
538 if (allocate) {
539
540 if ((handle = enum_settings_start()) == NULL)
541 return;
542
543 buflen = bufsize = 0;
544 buffer = NULL;
545 do {
546 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
547 if (ret) {
548 int len = strlen(otherbuf) + 1;
549 if (bufsize < buflen + len) {
550 bufsize = buflen + len + 2048;
551 buffer = srealloc(buffer, bufsize);
552 }
553 strcpy(buffer + buflen, otherbuf);
554 buflen += strlen(buffer + buflen) + 1;
555 }
556 } while (ret);
557 enum_settings_finish(handle);
558 buffer = srealloc(buffer, buflen + 1);
559 buffer[buflen] = '\0';
560
561 /*
562 * Now set up the list of sessions. Note that "Default
563 * Settings" must always be claimed to exist, even if it
564 * doesn't really.
565 */
566
567 p = buffer;
568 nsessions = 1; /* "Default Settings" counts as one */
569 while (*p) {
570 if (strcmp(p, "Default Settings"))
571 nsessions++;
572 while (*p)
573 p++;
574 p++;
575 }
576
577 sessions = smalloc((nsessions + 1) * sizeof(char *));
578 sessions[0] = "Default Settings";
579 p = buffer;
580 i = 1;
581 while (*p) {
582 if (strcmp(p, "Default Settings"))
583 sessions[i++] = p;
584 while (*p)
585 p++;
586 p++;
587 }
588
589 qsort(sessions, i, sizeof(char *), sessioncmp);
590 } else {
591 sfree(buffer);
592 sfree(sessions);
593 }
594 }