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