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