]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - settings.c
From RDB: a patch to allow special keys (^C, ^Z, Delete, Return) to
[PuTTY.git] / settings.c
1 /*
2  * settings.c: read and write saved sessions.
3  */
4
5 #include <windows.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "putty.h"
9 #include "storage.h"
10
11 static void gpps(void *handle, char *name, char *def, char *val, int len)
12 {
13     if (!read_setting_s(handle, name, val, len)) {
14         strncpy(val, def, len);
15         val[len - 1] = '\0';
16     }
17 }
18
19 static void gppi(void *handle, char *name, int def, int *i)
20 {
21     *i = read_setting_i(handle, name, def);
22 }
23
24 void save_settings(char *section, int do_host, Config * cfg)
25 {
26     int i;
27     char *p;
28     void *sesskey;
29
30     sesskey = open_settings_w(section);
31     if (!sesskey)
32         return;
33
34     write_setting_i(sesskey, "Present", 1);
35     if (do_host) {
36         write_setting_s(sesskey, "HostName", cfg->host);
37         write_setting_s(sesskey, "LogFileName", cfg->logfilename);
38         write_setting_i(sesskey, "LogType", cfg->logtype);
39         write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
40     }
41     p = "raw";
42     for (i = 0; backends[i].name != NULL; i++)
43         if (backends[i].protocol == cfg->protocol) {
44             p = backends[i].name;
45             break;
46         }
47     write_setting_s(sesskey, "Protocol", p);
48     write_setting_i(sesskey, "PortNumber", cfg->port);
49     write_setting_i(sesskey, "CloseOnExit", cfg->close_on_exit);
50     write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
51     write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60);  /* minutes */
52     write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60);      /* seconds */
53     write_setting_s(sesskey, "TerminalType", cfg->termtype);
54     write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
55     {
56         char buf[2 * sizeof(cfg->environmt)], *p, *q;
57         p = buf;
58         q = cfg->environmt;
59         while (*q) {
60             while (*q) {
61                 int c = *q++;
62                 if (c == '=' || c == ',' || c == '\\')
63                     *p++ = '\\';
64                 if (c == '\t')
65                     c = '=';
66                 *p++ = c;
67             }
68             *p++ = ',';
69             q++;
70         }
71         *p = '\0';
72         write_setting_s(sesskey, "Environment", buf);
73     }
74     write_setting_s(sesskey, "UserName", cfg->username);
75     write_setting_s(sesskey, "LocalUserName", cfg->localusername);
76     write_setting_i(sesskey, "NoPTY", cfg->nopty);
77     write_setting_i(sesskey, "Compression", cfg->compression);
78     write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
79     write_setting_s(sesskey, "Cipher",
80                     cfg->cipher == CIPHER_BLOWFISH ? "blowfish" :
81                     cfg->cipher == CIPHER_DES ? "des" :
82                     cfg->cipher == CIPHER_AES ? "aes" : "3des");
83     write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
84     write_setting_i(sesskey, "SshProt", cfg->sshprot);
85     write_setting_i(sesskey, "BuggyMAC", cfg->buggymac);
86     write_setting_s(sesskey, "PublicKeyFile", cfg->keyfile);
87     write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
88     write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
89     write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
90     write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
91     write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
92     write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
93     write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
94     write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
95     write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
96     write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
97     write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
98     write_setting_i(sesskey, "AltF4", cfg->alt_f4);
99     write_setting_i(sesskey, "AltSpace", cfg->alt_space);
100     write_setting_i(sesskey, "AltOnly", cfg->alt_only);
101     write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
102     write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
103     write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
104     write_setting_i(sesskey, "LocalEcho", cfg->localecho);
105     write_setting_i(sesskey, "LocalEdit", cfg->localedit);
106     write_setting_s(sesskey, "Answerback", cfg->answerback);
107     write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
108     write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
109     write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
110     write_setting_i(sesskey, "CurType", cfg->cursor_type);
111     write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
112     write_setting_i(sesskey, "Beep", cfg->beep);
113     write_setting_s(sesskey, "BellWaveFile", cfg->bell_wavefile);
114     write_setting_i(sesskey, "BellOverload", cfg->bellovl);
115     write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
116     write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
117     write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
118     write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
119     write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
120     write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
121     write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
122     write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
123     write_setting_s(sesskey, "WinTitle", cfg->wintitle);
124     write_setting_i(sesskey, "TermWidth", cfg->width);
125     write_setting_i(sesskey, "TermHeight", cfg->height);
126     write_setting_s(sesskey, "Font", cfg->font);
127     write_setting_i(sesskey, "FontIsBold", cfg->fontisbold);
128     write_setting_i(sesskey, "FontCharSet", cfg->fontcharset);
129     write_setting_i(sesskey, "FontHeight", cfg->fontheight);
130     write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
131     write_setting_i(sesskey, "TryPalette", cfg->try_palette);
132     write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
133     for (i = 0; i < 22; i++) {
134         char buf[20], buf2[30];
135         sprintf(buf, "Colour%d", i);
136         sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
137                 cfg->colours[i][1], cfg->colours[i][2]);
138         write_setting_s(sesskey, buf, buf2);
139     }
140     write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
141     write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
142     for (i = 0; i < 256; i += 32) {
143         char buf[20], buf2[256];
144         int j;
145         sprintf(buf, "Wordness%d", i);
146         *buf2 = '\0';
147         for (j = i; j < i + 32; j++) {
148             sprintf(buf2 + strlen(buf2), "%s%d",
149                     (*buf2 ? "," : ""), cfg->wordness[j]);
150         }
151         write_setting_s(sesskey, buf, buf2);
152     }
153     write_setting_i(sesskey, "KoiWinXlat", cfg->xlat_enablekoiwin);
154     write_setting_i(sesskey, "88592Xlat", cfg->xlat_88592w1250);
155     write_setting_i(sesskey, "88592-CP852", cfg->xlat_88592cp852);
156     write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
157     write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
158     write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
159     write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
160     write_setting_i(sesskey, "LockSize", cfg->locksize);
161     write_setting_i(sesskey, "BCE", cfg->bce);
162     write_setting_i(sesskey, "BlinkText", cfg->blinktext);
163     write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
164     write_setting_s(sesskey, "X11Display", cfg->x11_display);
165
166     close_settings_w(sesskey);
167 }
168
169 void load_settings(char *section, int do_host, Config * cfg)
170 {
171     int i;
172     char prot[10];
173     void *sesskey;
174
175     sesskey = open_settings_r(section);
176
177     cfg->ssh_subsys = 0;               /* FIXME: load this properly */
178     cfg->remote_cmd_ptr = cfg->remote_cmd;
179
180     gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
181     gppi(sesskey, "PortNumber", default_port, &cfg->port);
182     gpps(sesskey, "LogFileName", "putty.log",
183          cfg->logfilename, sizeof(cfg->logfilename));
184     gppi(sesskey, "LogType", 0, &cfg->logtype);
185     gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
186
187     gpps(sesskey, "Protocol", "default", prot, 10);
188     cfg->protocol = default_protocol;
189     for (i = 0; backends[i].name != NULL; i++)
190         if (!strcmp(prot, backends[i].name)) {
191             cfg->protocol = backends[i].protocol;
192             break;
193         }
194
195     gppi(sesskey, "CloseOnExit", COE_NORMAL, &cfg->close_on_exit);
196     gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
197     {
198         /* This is two values for backward compatibility with 0.50/0.51 */
199         int pingmin, pingsec;
200         gppi(sesskey, "PingInterval", 0, &pingmin);
201         gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
202         cfg->ping_interval = pingmin * 60 + pingsec;
203     }
204     gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
205          sizeof(cfg->termtype));
206     gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
207          sizeof(cfg->termspeed));
208     {
209         char buf[2 * sizeof(cfg->environmt)], *p, *q;
210         gpps(sesskey, "Environment", "", buf, sizeof(buf));
211         p = buf;
212         q = cfg->environmt;
213         while (*p) {
214             while (*p && *p != ',') {
215                 int c = *p++;
216                 if (c == '=')
217                     c = '\t';
218                 if (c == '\\')
219                     c = *p++;
220                 *q++ = c;
221             }
222             if (*p == ',')
223                 p++;
224             *q++ = '\0';
225         }
226         *q = '\0';
227     }
228     gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
229     gpps(sesskey, "LocalUserName", "", cfg->localusername,
230          sizeof(cfg->localusername));
231     gppi(sesskey, "NoPTY", 0, &cfg->nopty);
232     gppi(sesskey, "Compression", 0, &cfg->compression);
233     gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
234     {
235         char cipher[10];
236         gpps(sesskey, "Cipher", "3des", cipher, 10);
237         if (!strcmp(cipher, "blowfish"))
238             cfg->cipher = CIPHER_BLOWFISH;
239         else if (!strcmp(cipher, "des"))
240             cfg->cipher = CIPHER_DES;
241         else if (!strcmp(cipher, "aes"))
242             cfg->cipher = CIPHER_AES;
243         else
244             cfg->cipher = CIPHER_3DES;
245     }
246     gppi(sesskey, "SshProt", 1, &cfg->sshprot);
247     gppi(sesskey, "BuggyMAC", 0, &cfg->buggymac);
248     gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
249     gpps(sesskey, "PublicKeyFile", "", cfg->keyfile, sizeof(cfg->keyfile));
250     gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
251          sizeof(cfg->remote_cmd));
252     gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
253     gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
254     gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
255     gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
256     gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
257     gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
258     gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
259     gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
260     gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
261     gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
262     gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
263     gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
264     gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
265     gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
266     gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
267     gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
268     gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho);
269     gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit);
270     gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
271          sizeof(cfg->answerback));
272     gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
273     gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
274     gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
275     gppi(sesskey, "CurType", 0, &cfg->cursor_type);
276     gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
277     gppi(sesskey, "Beep", 1, &cfg->beep);
278     gpps(sesskey, "BellWaveFile", "", cfg->bell_wavefile,
279          sizeof(cfg->bell_wavefile));
280     gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
281     gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
282     gppi(sesskey, "BellOverloadT", 2000, &cfg->bellovl_t);
283     gppi(sesskey, "BellOverloadS", 5000, &cfg->bellovl_s);
284     gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
285     gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
286     gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
287     gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
288     gppi(sesskey, "WinNameAlways", 0, &cfg->win_name_always);
289     gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
290     gppi(sesskey, "TermWidth", 80, &cfg->width);
291     gppi(sesskey, "TermHeight", 24, &cfg->height);
292     gpps(sesskey, "Font", "Courier", cfg->font, sizeof(cfg->font));
293     gppi(sesskey, "FontIsBold", 0, &cfg->fontisbold);
294     gppi(sesskey, "FontCharSet", ANSI_CHARSET, &cfg->fontcharset);
295     gppi(sesskey, "FontHeight", 10, &cfg->fontheight);
296     if (cfg->fontheight < 0) {
297         int oldh, newh;
298         HDC hdc = GetDC(NULL);
299         int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
300         ReleaseDC(NULL, hdc);
301
302         oldh = -cfg->fontheight;
303         newh = MulDiv(oldh, 72, logpix) + 1;
304         if (MulDiv(newh, logpix, 72) > oldh)
305             newh--;
306         cfg->fontheight = newh;
307     }
308     gppi(sesskey, "FontVTMode", VT_OEMANSI, (int *) &cfg->vtmode);
309     gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
310     gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
311     for (i = 0; i < 22; i++) {
312         static char *defaults[] = {
313             "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
314             "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
315             "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
316             "85,85,255", "187,0,187", "255,85,255", "0,187,187",
317             "85,255,255", "187,187,187", "255,255,255"
318         };
319         char buf[20], buf2[30];
320         int c0, c1, c2;
321         sprintf(buf, "Colour%d", i);
322         gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
323         if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
324             cfg->colours[i][0] = c0;
325             cfg->colours[i][1] = c1;
326             cfg->colours[i][2] = c2;
327         }
328     }
329     gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
330     gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
331     for (i = 0; i < 256; i += 32) {
332         static char *defaults[] = {
333             "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",
334             "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",
335             "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",
336             "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",
337             "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",
338             "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",
339             "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",
340             "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"
341         };
342         char buf[20], buf2[256], *p;
343         int j;
344         sprintf(buf, "Wordness%d", i);
345         gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
346         p = buf2;
347         for (j = i; j < i + 32; j++) {
348             char *q = p;
349             while (*p && *p != ',')
350                 p++;
351             if (*p == ',')
352                 *p++ = '\0';
353             cfg->wordness[j] = atoi(q);
354         }
355     }
356     gppi(sesskey, "KoiWinXlat", 0, &cfg->xlat_enablekoiwin);
357     gppi(sesskey, "88592Xlat", 0, &cfg->xlat_88592w1250);
358     gppi(sesskey, "88592-CP852", 0, &cfg->xlat_88592cp852);
359     gppi(sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
360     gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
361     gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
362     gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
363     gppi(sesskey, "LockSize", 0, &cfg->locksize);
364     gppi(sesskey, "BCE", 0, &cfg->bce);
365     gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
366     gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
367     gpps(sesskey, "X11Display", "localhost:0", cfg->x11_display,
368          sizeof(cfg->x11_display));
369
370     close_settings_r(sesskey);
371 }
372
373 void do_defaults(char *session, Config * cfg)
374 {
375     if (session)
376         load_settings(session, TRUE, cfg);
377     else
378         load_settings("Default Settings", FALSE, cfg);
379 }
380
381 static int sessioncmp(const void *av, const void *bv)
382 {
383     const char *a = *(const char *const *) av;
384     const char *b = *(const char *const *) bv;
385
386     /*
387      * Alphabetical order, except that "Default Settings" is a
388      * special case and comes first.
389      */
390     if (!strcmp(a, "Default Settings"))
391         return -1;                     /* a comes first */
392     if (!strcmp(b, "Default Settings"))
393         return +1;                     /* b comes first */
394     return strcmp(a, b);               /* otherwise, compare normally */
395 }
396
397 void get_sesslist(int allocate)
398 {
399     static char otherbuf[2048];
400     static char *buffer;
401     int buflen, bufsize, i;
402     char *p, *ret;
403     void *handle;
404
405     if (allocate) {
406
407         if ((handle = enum_settings_start()) == NULL)
408             return;
409
410         buflen = bufsize = 0;
411         buffer = NULL;
412         do {
413             ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
414             if (ret) {
415                 int len = strlen(otherbuf) + 1;
416                 if (bufsize < buflen + len) {
417                     bufsize = buflen + len + 2048;
418                     buffer = srealloc(buffer, bufsize);
419                 }
420                 strcpy(buffer + buflen, otherbuf);
421                 buflen += strlen(buffer + buflen) + 1;
422             }
423         } while (ret);
424         enum_settings_finish(handle);
425         buffer = srealloc(buffer, buflen + 1);
426         buffer[buflen] = '\0';
427
428         /*
429          * Now set up the list of sessions. Note that "Default
430          * Settings" must always be claimed to exist, even if it
431          * doesn't really.
432          */
433
434         p = buffer;
435         nsessions = 1;                 /* "Default Settings" counts as one */
436         while (*p) {
437             if (strcmp(p, "Default Settings"))
438                 nsessions++;
439             while (*p)
440                 p++;
441             p++;
442         }
443
444         sessions = smalloc((nsessions + 1) * sizeof(char *));
445         sessions[0] = "Default Settings";
446         p = buffer;
447         i = 1;
448         while (*p) {
449             if (strcmp(p, "Default Settings"))
450                 sessions[i++] = p;
451             while (*p)
452                 p++;
453             p++;
454         }
455
456         qsort(sessions, i, sizeof(char *), sessioncmp);
457     } else {
458         sfree(buffer);
459         sfree(sessions);
460     }
461 }