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