]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - settings.c
Move MODULE files out of individual project directories into a
[PuTTY.git] / settings.c
1 /*
2  * settings.c: read and write saved sessions. (platform-independent)
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "putty.h"
8 #include "storage.h"
9
10 /*
11  * Tables of string <-> enum value mappings
12  */
13 struct keyval { char *s; int v; };
14
15 static const struct keyval ciphernames[] = {
16     { "aes",        CIPHER_AES },
17     { "blowfish",   CIPHER_BLOWFISH },
18     { "3des",       CIPHER_3DES },
19     { "WARN",       CIPHER_WARN },
20     { "des",        CIPHER_DES }
21 };
22
23 static void gpps(void *handle, const char *name, const char *def,
24                  char *val, int len)
25 {
26     if (!read_setting_s(handle, name, val, len)) {
27         char *pdef;
28
29         pdef = platform_default_s(name);
30         if (pdef) {
31             strncpy(val, pdef, len);
32             sfree(pdef);
33         } else {
34             strncpy(val, def, len);
35         }
36
37         val[len - 1] = '\0';
38     }
39 }
40
41 /*
42  * gppfont and gppfile cannot have local defaults, since the very
43  * format of a Filename or Font is platform-dependent. So the
44  * platform-dependent functions MUST return some sort of value.
45  */
46 static void gppfont(void *handle, const char *name, FontSpec *result)
47 {
48     if (!read_setting_fontspec(handle, name, result))
49         *result = platform_default_fontspec(name);
50 }
51 static void gppfile(void *handle, const char *name, Filename *result)
52 {
53     if (!read_setting_filename(handle, name, result))
54         *result = platform_default_filename(name);
55 }
56
57 static void gppi(void *handle, char *name, int def, int *i)
58 {
59     def = platform_default_i(name, def);
60     *i = read_setting_i(handle, name, def);
61 }
62
63 static int key2val(const struct keyval *mapping, int nmaps, char *key)
64 {
65     int i;
66     for (i = 0; i < nmaps; i++)
67         if (!strcmp(mapping[i].s, key)) return mapping[i].v;
68     return -1;
69 }
70
71 static const char *val2key(const struct keyval *mapping, int nmaps, int val)
72 {
73     int i;
74     for (i = 0; i < nmaps; i++)
75         if (mapping[i].v == val) return mapping[i].s;
76     return NULL;
77 }
78
79 /*
80  * Helper function to parse a comma-separated list of strings into
81  * a preference list array of values. Any missing values are added
82  * to the end and duplicates are weeded.
83  * XXX: assumes vals in 'mapping' are small +ve integers
84  */
85 static void gprefs(void *sesskey, char *name, char *def,
86                    const struct keyval *mapping, int nvals,
87                    int *array)
88 {
89     char commalist[80];
90     int n;
91     unsigned long seen = 0;            /* bitmap for weeding dups etc */
92     gpps(sesskey, name, def, commalist, sizeof(commalist));
93
94     /* Grotty parsing of commalist. */
95     n = 0;
96     do {
97         int v;
98         char *key;
99         key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
100         if (!key) break;
101         if (((v = key2val(mapping, nvals, key)) != -1) &&
102             !(seen & 1<<v)) {
103             array[n] = v;
104             n++;
105             seen |= 1<<v;
106         }
107     } while (n < nvals);
108     /* Add any missing values (backward compatibility ect). */
109     {
110         int i;
111         for (i = 0; i < nvals; i++) {
112             if (!(seen & 1<<mapping[i].v)) {
113                 array[n] = mapping[i].v;
114                 n++;
115             }
116         }
117     }
118 }
119
120 /* 
121  * Write out a preference list.
122  */
123 static void wprefs(void *sesskey, char *name,
124                    const struct keyval *mapping, int nvals,
125                    int *array)
126 {
127     char buf[80] = "";  /* XXX assumed big enough */
128     int l = sizeof(buf)-1, i;
129     buf[l] = '\0';
130     for (i = 0; l > 0 && i < nvals; i++) {
131         const char *s = val2key(mapping, nvals, array[i]);
132         if (s) {
133             int sl = strlen(s);
134             if (i > 0) {
135                 strncat(buf, ",", l);
136                 l--;
137             }
138             strncat(buf, s, l);
139             l -= sl;
140         }
141     }
142     write_setting_s(sesskey, name, buf);
143 }
144
145 char *save_settings(char *section, int do_host, Config * cfg)
146 {
147     void *sesskey;
148     char *errmsg;
149
150     sesskey = open_settings_w(section, &errmsg);
151     if (!sesskey)
152         return errmsg;
153     save_open_settings(sesskey, do_host, cfg);
154     close_settings_w(sesskey);
155     return NULL;
156 }
157
158 void save_open_settings(void *sesskey, int do_host, Config *cfg)
159 {
160     int i;
161     char *p;
162
163     write_setting_i(sesskey, "Present", 1);
164     if (do_host) {
165         write_setting_s(sesskey, "HostName", cfg->host);
166     }
167     write_setting_filename(sesskey, "LogFileName", cfg->logfilename);
168     write_setting_i(sesskey, "LogType", cfg->logtype);
169     write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
170     p = "raw";
171     for (i = 0; backends[i].name != NULL; i++)
172         if (backends[i].protocol == cfg->protocol) {
173             p = backends[i].name;
174             break;
175         }
176     write_setting_s(sesskey, "Protocol", p);
177     write_setting_i(sesskey, "PortNumber", cfg->port);
178     /* The CloseOnExit numbers are arranged in a different order from
179      * the standard FORCE_ON / FORCE_OFF / AUTO. */
180     write_setting_i(sesskey, "CloseOnExit", (cfg->close_on_exit+2)%3);
181     write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
182     write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60);  /* minutes */
183     write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60);      /* seconds */
184     write_setting_i(sesskey, "TCPNoDelay", cfg->tcp_nodelay);
185     write_setting_i(sesskey, "TCPKeepalives", cfg->tcp_keepalives);
186     write_setting_s(sesskey, "TerminalType", cfg->termtype);
187     write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
188
189     /* proxy settings */
190     write_setting_s(sesskey, "ProxyExcludeList", cfg->proxy_exclude_list);
191     write_setting_i(sesskey, "ProxyDNS", (cfg->proxy_dns+2)%3);
192     write_setting_i(sesskey, "ProxyLocalhost", cfg->even_proxy_localhost);
193     write_setting_i(sesskey, "ProxyMethod", cfg->proxy_type);
194     write_setting_s(sesskey, "ProxyHost", cfg->proxy_host);
195     write_setting_i(sesskey, "ProxyPort", cfg->proxy_port);
196     write_setting_s(sesskey, "ProxyUsername", cfg->proxy_username);
197     write_setting_s(sesskey, "ProxyPassword", cfg->proxy_password);
198     write_setting_s(sesskey, "ProxyTelnetCommand", cfg->proxy_telnet_command);
199
200     {
201         char buf[2 * sizeof(cfg->environmt)], *p, *q;
202         p = buf;
203         q = cfg->environmt;
204         while (*q) {
205             while (*q) {
206                 int c = *q++;
207                 if (c == '=' || c == ',' || c == '\\')
208                     *p++ = '\\';
209                 if (c == '\t')
210                     c = '=';
211                 *p++ = c;
212             }
213             *p++ = ',';
214             q++;
215         }
216         *p = '\0';
217         write_setting_s(sesskey, "Environment", buf);
218     }
219     write_setting_s(sesskey, "UserName", cfg->username);
220     write_setting_s(sesskey, "LocalUserName", cfg->localusername);
221     write_setting_i(sesskey, "NoPTY", cfg->nopty);
222     write_setting_i(sesskey, "Compression", cfg->compression);
223     write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
224     write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
225     wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
226            cfg->ssh_cipherlist);
227     write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
228     write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
229     write_setting_i(sesskey, "SshProt", cfg->sshprot);
230     write_setting_i(sesskey, "SSH2DES", cfg->ssh2_des_cbc);
231     write_setting_filename(sesskey, "PublicKeyFile", cfg->keyfile);
232     write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
233     write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
234     write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
235     write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
236     write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
237     write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
238     write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
239     write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
240     write_setting_i(sesskey, "NoMouseReporting", cfg->no_mouse_rep);
241     write_setting_i(sesskey, "NoRemoteResize", cfg->no_remote_resize);
242     write_setting_i(sesskey, "NoAltScreen", cfg->no_alt_screen);
243     write_setting_i(sesskey, "NoRemoteWinTitle", cfg->no_remote_wintitle);
244     write_setting_i(sesskey, "NoRemoteQTitle", cfg->no_remote_qtitle);
245     write_setting_i(sesskey, "NoDBackspace", cfg->no_dbackspace);
246     write_setting_i(sesskey, "NoRemoteCharset", cfg->no_remote_charset);
247     write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
248     write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
249     write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
250     write_setting_i(sesskey, "AltF4", cfg->alt_f4);
251     write_setting_i(sesskey, "AltSpace", cfg->alt_space);
252     write_setting_i(sesskey, "AltOnly", cfg->alt_only);
253     write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
254     write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
255     write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
256     write_setting_i(sesskey, "TelnetRet", cfg->telnet_newline);
257     write_setting_i(sesskey, "LocalEcho", cfg->localecho);
258     write_setting_i(sesskey, "LocalEdit", cfg->localedit);
259     write_setting_s(sesskey, "Answerback", cfg->answerback);
260     write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
261     write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
262     write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
263     write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
264     write_setting_i(sesskey, "WindowBorder", cfg->window_border);
265     write_setting_i(sesskey, "CurType", cfg->cursor_type);
266     write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
267     write_setting_i(sesskey, "Beep", cfg->beep);
268     write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
269     write_setting_filename(sesskey, "BellWaveFile", cfg->bell_wavefile);
270     write_setting_i(sesskey, "BellOverload", cfg->bellovl);
271     write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
272     write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
273     write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
274     write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
275     write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
276     write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
277     write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
278     write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
279     write_setting_s(sesskey, "WinTitle", cfg->wintitle);
280     write_setting_i(sesskey, "TermWidth", cfg->width);
281     write_setting_i(sesskey, "TermHeight", cfg->height);
282     write_setting_fontspec(sesskey, "Font", cfg->font);
283     write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
284     write_setting_i(sesskey, "UseSystemColours", cfg->system_colour);
285     write_setting_i(sesskey, "TryPalette", cfg->try_palette);
286     write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
287     for (i = 0; i < 22; i++) {
288         char buf[20], buf2[30];
289         sprintf(buf, "Colour%d", i);
290         sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
291                 cfg->colours[i][1], cfg->colours[i][2]);
292         write_setting_s(sesskey, buf, buf2);
293     }
294     write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
295     write_setting_i(sesskey, "PasteRTF", cfg->rtf_paste);
296     write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
297     write_setting_i(sesskey, "RectSelect", cfg->rect_select);
298     write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
299     for (i = 0; i < 256; i += 32) {
300         char buf[20], buf2[256];
301         int j;
302         sprintf(buf, "Wordness%d", i);
303         *buf2 = '\0';
304         for (j = i; j < i + 32; j++) {
305             sprintf(buf2 + strlen(buf2), "%s%d",
306                     (*buf2 ? "," : ""), cfg->wordness[j]);
307         }
308         write_setting_s(sesskey, buf, buf2);
309     }
310     write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
311     write_setting_s(sesskey, "Printer", cfg->printer);
312     write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
313     write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
314     write_setting_i(sesskey, "ScrollBarFullScreen", cfg->scrollbar_in_fullscreen);
315     write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
316     write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
317     write_setting_i(sesskey, "EraseToScrollback", cfg->erase_to_scrollback);
318     write_setting_i(sesskey, "LockSize", cfg->resize_action);
319     write_setting_i(sesskey, "BCE", cfg->bce);
320     write_setting_i(sesskey, "BlinkText", cfg->blinktext);
321     write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
322     write_setting_s(sesskey, "X11Display", cfg->x11_display);
323     write_setting_i(sesskey, "X11AuthType", cfg->x11_auth);
324     write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
325     write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
326     {
327         char buf[2 * sizeof(cfg->portfwd)], *p, *q;
328         p = buf;
329         q = cfg->portfwd;
330         while (*q) {
331             while (*q) {
332                 int c = *q++;
333                 if (c == '=' || c == ',' || c == '\\')
334                     *p++ = '\\';
335                 if (c == '\t')
336                     c = '=';
337                 *p++ = c;
338             }
339             *p++ = ',';
340             q++;
341         }
342         *p = '\0';
343         write_setting_s(sesskey, "PortForwardings", buf);
344     }
345     write_setting_i(sesskey, "BugIgnore1", 2-cfg->sshbug_ignore1);
346     write_setting_i(sesskey, "BugPlainPW1", 2-cfg->sshbug_plainpw1);
347     write_setting_i(sesskey, "BugRSA1", 2-cfg->sshbug_rsa1);
348     write_setting_i(sesskey, "BugHMAC2", 2-cfg->sshbug_hmac2);
349     write_setting_i(sesskey, "BugDeriveKey2", 2-cfg->sshbug_derivekey2);
350     write_setting_i(sesskey, "BugRSAPad2", 2-cfg->sshbug_rsapad2);
351     write_setting_i(sesskey, "BugDHGEx2", 2-cfg->sshbug_dhgex2);
352     write_setting_i(sesskey, "BugPKSessID2", 2-cfg->sshbug_pksessid2);
353     write_setting_i(sesskey, "StampUtmp", cfg->stamp_utmp);
354     write_setting_i(sesskey, "LoginShell", cfg->login_shell);
355     write_setting_i(sesskey, "ScrollbarOnLeft", cfg->scrollbar_on_left);
356     write_setting_fontspec(sesskey, "BoldFont", cfg->boldfont);
357     write_setting_fontspec(sesskey, "WideFont", cfg->widefont);
358     write_setting_fontspec(sesskey, "WideBoldFont", cfg->wideboldfont);
359     write_setting_i(sesskey, "ShadowBold", cfg->shadowbold);
360     write_setting_i(sesskey, "ShadowBoldOffset", cfg->shadowboldoffset);
361 }
362
363 void load_settings(char *section, int do_host, Config * cfg)
364 {
365     void *sesskey;
366
367     sesskey = open_settings_r(section);
368     load_open_settings(sesskey, do_host, cfg);
369     close_settings_r(sesskey);
370 }
371
372 void load_open_settings(void *sesskey, int do_host, Config *cfg)
373 {
374     int i;
375     char prot[10];
376
377     cfg->ssh_subsys = 0;               /* FIXME: load this properly */
378     cfg->remote_cmd_ptr = cfg->remote_cmd;
379     cfg->remote_cmd_ptr2 = NULL;
380
381     if (do_host) {
382         gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
383     } else {
384         cfg->host[0] = '\0';           /* blank hostname */
385     }
386     gppfile(sesskey, "LogFileName", &cfg->logfilename);
387     gppi(sesskey, "LogType", 0, &cfg->logtype);
388     gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
389
390     gpps(sesskey, "Protocol", "default", prot, 10);
391     cfg->protocol = default_protocol;
392     cfg->port = default_port;
393     for (i = 0; backends[i].name != NULL; i++)
394         if (!strcmp(prot, backends[i].name)) {
395             cfg->protocol = backends[i].protocol;
396             gppi(sesskey, "PortNumber", default_port, &cfg->port);
397             break;
398         }
399
400     /* The CloseOnExit numbers are arranged in a different order from
401      * the standard FORCE_ON / FORCE_OFF / AUTO. */
402     gppi(sesskey, "CloseOnExit", 1, &i); cfg->close_on_exit = (i+1)%3;
403     gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
404     {
405         /* This is two values for backward compatibility with 0.50/0.51 */
406         int pingmin, pingsec;
407         gppi(sesskey, "PingInterval", 0, &pingmin);
408         gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
409         cfg->ping_interval = pingmin * 60 + pingsec;
410     }
411     gppi(sesskey, "TCPNoDelay", 1, &cfg->tcp_nodelay);
412     gppi(sesskey, "TCPKeepalives", 0, &cfg->tcp_keepalives);
413     gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
414          sizeof(cfg->termtype));
415     gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
416          sizeof(cfg->termspeed));
417
418     /* proxy settings */
419     gpps(sesskey, "ProxyExcludeList", "", cfg->proxy_exclude_list,
420          sizeof(cfg->proxy_exclude_list));
421     gppi(sesskey, "ProxyDNS", 1, &i); cfg->proxy_dns = (i+1)%3;
422     gppi(sesskey, "ProxyLocalhost", 0, &cfg->even_proxy_localhost);
423     gppi(sesskey, "ProxyMethod", -1, &cfg->proxy_type);
424     if (cfg->proxy_type == -1) {
425         int i;
426         gppi(sesskey, "ProxyType", 0, &i);
427         if (i == 0)
428             cfg->proxy_type = PROXY_NONE;
429         else if (i == 1)
430             cfg->proxy_type = PROXY_HTTP;
431         else if (i == 3)
432             cfg->proxy_type = PROXY_TELNET;
433         else if (i == 4)
434             cfg->proxy_type = PROXY_CMD;
435         else {
436             gppi(sesskey, "ProxySOCKSVersion", 5, &i);
437             if (i == 5)
438                 cfg->proxy_type = PROXY_SOCKS5;
439             else
440                 cfg->proxy_type = PROXY_SOCKS4;
441         }
442     }
443     gpps(sesskey, "ProxyHost", "proxy", cfg->proxy_host,
444          sizeof(cfg->proxy_host));
445     gppi(sesskey, "ProxyPort", 80, &cfg->proxy_port);
446     gpps(sesskey, "ProxyUsername", "", cfg->proxy_username,
447          sizeof(cfg->proxy_username));
448     gpps(sesskey, "ProxyPassword", "", cfg->proxy_password,
449          sizeof(cfg->proxy_password));
450     gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
451          cfg->proxy_telnet_command, sizeof(cfg->proxy_telnet_command));
452
453     {
454         char buf[2 * sizeof(cfg->environmt)], *p, *q;
455         gpps(sesskey, "Environment", "", buf, sizeof(buf));
456         p = buf;
457         q = cfg->environmt;
458         while (*p) {
459             while (*p && *p != ',') {
460                 int c = *p++;
461                 if (c == '=')
462                     c = '\t';
463                 if (c == '\\')
464                     c = *p++;
465                 *q++ = c;
466             }
467             if (*p == ',')
468                 p++;
469             *q++ = '\0';
470         }
471         *q = '\0';
472     }
473     gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
474     gpps(sesskey, "LocalUserName", "", cfg->localusername,
475          sizeof(cfg->localusername));
476     gppi(sesskey, "NoPTY", 0, &cfg->nopty);
477     gppi(sesskey, "Compression", 0, &cfg->compression);
478     gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
479     gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
480     gprefs(sesskey, "Cipher", "\0",
481            ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
482     gppi(sesskey, "SshProt", 2, &cfg->sshprot);
483     gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
484     gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
485     gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
486     gppfile(sesskey, "PublicKeyFile", &cfg->keyfile);
487     gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
488          sizeof(cfg->remote_cmd));
489     gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
490     gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
491     gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
492     gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
493     gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
494     gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
495     gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
496     gppi(sesskey, "NoMouseReporting", 0, &cfg->no_mouse_rep);
497     gppi(sesskey, "NoRemoteResize", 0, &cfg->no_remote_resize);
498     gppi(sesskey, "NoAltScreen", 0, &cfg->no_alt_screen);
499     gppi(sesskey, "NoRemoteWinTitle", 0, &cfg->no_remote_wintitle);
500     gppi(sesskey, "NoRemoteQTitle", 1, &cfg->no_remote_qtitle);
501     gppi(sesskey, "NoDBackspace", 0, &cfg->no_dbackspace);
502     gppi(sesskey, "NoRemoteCharset", 0, &cfg->no_remote_charset);
503     gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
504     gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
505     gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
506     gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
507     gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
508     gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
509     gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
510     gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
511     gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
512     gppi(sesskey, "TelnetRet", 1, &cfg->telnet_newline);
513     gppi(sesskey, "LocalEcho", AUTO, &cfg->localecho);
514     gppi(sesskey, "LocalEdit", AUTO, &cfg->localedit);
515     gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
516          sizeof(cfg->answerback));
517     gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
518     gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
519     gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
520     gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
521     gppi(sesskey, "WindowBorder", 1, &cfg->window_border);
522     gppi(sesskey, "CurType", 0, &cfg->cursor_type);
523     gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
524     /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
525     gppi(sesskey, "Beep", 1, &cfg->beep);
526     gppi(sesskey, "BeepInd", 0, &cfg->beep_ind);
527     gppfile(sesskey, "BellWaveFile", &cfg->bell_wavefile);
528     gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
529     gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
530     gppi(sesskey, "BellOverloadT", 2*TICKSPERSEC, &cfg->bellovl_t);
531     gppi(sesskey, "BellOverloadS", 5*TICKSPERSEC, &cfg->bellovl_s);
532     gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
533     gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
534     gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
535     gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
536     gppi(sesskey, "WinNameAlways", 1, &cfg->win_name_always);
537     gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
538     gppi(sesskey, "TermWidth", 80, &cfg->width);
539     gppi(sesskey, "TermHeight", 24, &cfg->height);
540     gppfont(sesskey, "Font", &cfg->font);
541     gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
542     gppi(sesskey, "UseSystemColours", 0, &cfg->system_colour);
543     gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
544     gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
545     for (i = 0; i < 22; i++) {
546         static const char *const defaults[] = {
547             "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
548             "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
549             "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
550             "85,85,255", "187,0,187", "255,85,255", "0,187,187",
551             "85,255,255", "187,187,187", "255,255,255"
552         };
553         char buf[20], buf2[30];
554         int c0, c1, c2;
555         sprintf(buf, "Colour%d", i);
556         gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
557         if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
558             cfg->colours[i][0] = c0;
559             cfg->colours[i][1] = c1;
560             cfg->colours[i][2] = c2;
561         }
562     }
563     gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
564     gppi(sesskey, "PasteRTF", 0, &cfg->rtf_paste);
565     gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
566     gppi(sesskey, "RectSelect", 0, &cfg->rect_select);
567     gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
568     for (i = 0; i < 256; i += 32) {
569         static const char *const defaults[] = {
570             "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",
571             "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",
572             "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",
573             "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",
574             "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",
575             "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",
576             "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",
577             "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"
578         };
579         char buf[20], buf2[256], *p;
580         int j;
581         sprintf(buf, "Wordness%d", i);
582         gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
583         p = buf2;
584         for (j = i; j < i + 32; j++) {
585             char *q = p;
586             while (*p && *p != ',')
587                 p++;
588             if (*p == ',')
589                 *p++ = '\0';
590             cfg->wordness[j] = atoi(q);
591         }
592     }
593     /*
594      * The empty default for LineCodePage will be converted later
595      * into a plausible default for the locale.
596      */
597     gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
598          sizeof(cfg->line_codepage));
599     gpps(sesskey, "Printer", "", cfg->printer, sizeof(cfg->printer));
600     gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
601     gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
602     gppi(sesskey, "ScrollBarFullScreen", 0, &cfg->scrollbar_in_fullscreen);
603     gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
604     gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
605     gppi(sesskey, "EraseToScrollback", 1, &cfg->erase_to_scrollback);
606     gppi(sesskey, "LockSize", 0, &cfg->resize_action);
607     gppi(sesskey, "BCE", 1, &cfg->bce);
608     gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
609     gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
610     gpps(sesskey, "X11Display", "", cfg->x11_display,
611          sizeof(cfg->x11_display));
612     gppi(sesskey, "X11AuthType", X11_MIT, &cfg->x11_auth);
613
614     gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
615     gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
616     {
617         char buf[2 * sizeof(cfg->portfwd)], *p, *q;
618         gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
619         p = buf;
620         q = cfg->portfwd;
621         while (*p) {
622             while (*p && *p != ',') {
623                 int c = *p++;
624                 if (c == '=')
625                     c = '\t';
626                 if (c == '\\')
627                     c = *p++;
628                 *q++ = c;
629             }
630             if (*p == ',')
631                 p++;
632             *q++ = '\0';
633         }
634         *q = '\0';
635     }
636     gppi(sesskey, "BugIgnore1", 0, &i); cfg->sshbug_ignore1 = 2-i;
637     gppi(sesskey, "BugPlainPW1", 0, &i); cfg->sshbug_plainpw1 = 2-i;
638     gppi(sesskey, "BugRSA1", 0, &i); cfg->sshbug_rsa1 = 2-i;
639     {
640         int i;
641         gppi(sesskey, "BugHMAC2", 0, &i); cfg->sshbug_hmac2 = 2-i;
642         if (cfg->sshbug_hmac2 == AUTO) {
643             gppi(sesskey, "BuggyMAC", 0, &i);
644             if (i == 1)
645                 cfg->sshbug_hmac2 = FORCE_ON;
646         }
647     }
648     gppi(sesskey, "BugDeriveKey2", 0, &i); cfg->sshbug_derivekey2 = 2-i;
649     gppi(sesskey, "BugRSAPad2", 0, &i); cfg->sshbug_rsapad2 = 2-i;
650     gppi(sesskey, "BugDHGEx2", 0, &i); cfg->sshbug_dhgex2 = 2-i;
651     gppi(sesskey, "BugPKSessID2", 0, &i); cfg->sshbug_pksessid2 = 2-i;
652     gppi(sesskey, "StampUtmp", 1, &cfg->stamp_utmp);
653     gppi(sesskey, "LoginShell", 1, &cfg->login_shell);
654     gppi(sesskey, "ScrollbarOnLeft", 0, &cfg->scrollbar_on_left);
655     gppi(sesskey, "ShadowBold", 0, &cfg->shadowbold);
656     gppfont(sesskey, "BoldFont", &cfg->boldfont);
657     gppfont(sesskey, "WideFont", &cfg->widefont);
658     gppfont(sesskey, "WideBoldFont", &cfg->wideboldfont);
659     gppi(sesskey, "ShadowBoldOffset", 1, &cfg->shadowboldoffset);
660 }
661
662 void do_defaults(char *session, Config * cfg)
663 {
664     load_settings(session, (session != NULL && *session), cfg);
665 }
666
667 static int sessioncmp(const void *av, const void *bv)
668 {
669     const char *a = *(const char *const *) av;
670     const char *b = *(const char *const *) bv;
671
672     /*
673      * Alphabetical order, except that "Default Settings" is a
674      * special case and comes first.
675      */
676     if (!strcmp(a, "Default Settings"))
677         return -1;                     /* a comes first */
678     if (!strcmp(b, "Default Settings"))
679         return +1;                     /* b comes first */
680     /*
681      * FIXME: perhaps we should ignore the first & in determining
682      * sort order.
683      */
684     return strcmp(a, b);               /* otherwise, compare normally */
685 }
686
687 void get_sesslist(struct sesslist *list, int allocate)
688 {
689     char otherbuf[2048];
690     int buflen, bufsize, i;
691     char *p, *ret;
692     void *handle;
693
694     if (allocate) {
695
696         buflen = bufsize = 0;
697         list->buffer = NULL;
698         if ((handle = enum_settings_start()) != NULL) {
699             do {
700                 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
701                 if (ret) {
702                     int len = strlen(otherbuf) + 1;
703                     if (bufsize < buflen + len) {
704                         bufsize = buflen + len + 2048;
705                         list->buffer = sresize(list->buffer, bufsize, char);
706                     }
707                     strcpy(list->buffer + buflen, otherbuf);
708                     buflen += strlen(list->buffer + buflen) + 1;
709                 }
710             } while (ret);
711             enum_settings_finish(handle);
712         }
713         list->buffer = sresize(list->buffer, buflen + 1, char);
714         list->buffer[buflen] = '\0';
715
716         /*
717          * Now set up the list of sessions. Note that "Default
718          * Settings" must always be claimed to exist, even if it
719          * doesn't really.
720          */
721
722         p = list->buffer;
723         list->nsessions = 1;           /* "Default Settings" counts as one */
724         while (*p) {
725             if (strcmp(p, "Default Settings"))
726                 list->nsessions++;
727             while (*p)
728                 p++;
729             p++;
730         }
731
732         list->sessions = snewn(list->nsessions + 1, char *);
733         list->sessions[0] = "Default Settings";
734         p = list->buffer;
735         i = 1;
736         while (*p) {
737             if (strcmp(p, "Default Settings"))
738                 list->sessions[i++] = p;
739             while (*p)
740                 p++;
741             p++;
742         }
743
744         qsort(list->sessions, i, sizeof(char *), sessioncmp);
745     } else {
746         sfree(list->buffer);
747         sfree(list->sessions);
748     }
749 }