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