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