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