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