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