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