Add a .cvsignore file
[u/mdw/putty] / settings.c
CommitLineData
a9422f39 1/*
2 * settings.c: read and write saved sessions.
3 */
4
5#include <windows.h>
6#include <stdio.h>
49bad831 7#include <stdlib.h>
a9422f39 8#include "putty.h"
9#include "storage.h"
10
11static 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
18static void gppi(void *handle, char *name, int def, int *i) {
19 *i = read_setting_i(handle, name, def);
20}
21
22void 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);
e1c8e0ed 35 write_setting_s (sesskey, "LogFileName", cfg->logfilename);
36 write_setting_i (sesskey, "LogType", cfg->logtype);
a9422f39 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);
ec55b220 47 write_setting_i (sesskey, "PingInterval", cfg->ping_interval);
a9422f39 48 write_setting_s (sesskey, "TerminalType", cfg->termtype);
49 write_setting_s (sesskey, "TerminalSpeed", cfg->termspeed);
50 {
51 char buf[2*sizeof(cfg->environmt)], *p, *q;
52 p = buf;
53 q = cfg->environmt;
54 while (*q) {
55 while (*q) {
56 int c = *q++;
57 if (c == '=' || c == ',' || c == '\\')
58 *p++ = '\\';
59 if (c == '\t')
60 c = '=';
61 *p++ = c;
62 }
63 *p++ = ',';
64 q++;
65 }
66 *p = '\0';
67 write_setting_s (sesskey, "Environment", buf);
68 }
69 write_setting_s (sesskey, "UserName", cfg->username);
70 write_setting_i (sesskey, "NoPTY", cfg->nopty);
4ba9b64b 71 write_setting_i (sesskey, "Compression", cfg->compression);
a9422f39 72 write_setting_i (sesskey, "AgentFwd", cfg->agentfwd);
73 write_setting_s (sesskey, "RemoteCmd", cfg->remote_cmd);
74 write_setting_s (sesskey, "Cipher", cfg->cipher == CIPHER_BLOWFISH ? "blowfish" :
75 cfg->cipher == CIPHER_DES ? "des" : "3des");
76 write_setting_i (sesskey, "AuthTIS", cfg->try_tis_auth);
77 write_setting_i (sesskey, "SshProt", cfg->sshprot);
7591b9ff 78 write_setting_i (sesskey, "BuggyMAC", cfg->buggymac);
a9422f39 79 write_setting_s (sesskey, "PublicKeyFile", cfg->keyfile);
80 write_setting_s (sesskey, "RemoteCommand", cfg->remote_cmd);
81 write_setting_i (sesskey, "RFCEnviron", cfg->rfc_environ);
82 write_setting_i (sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
83 write_setting_i (sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
84 write_setting_i (sesskey, "LinuxFunctionKeys", cfg->funky_type);
b00f8b34 85 write_setting_i (sesskey, "NoApplicationKeys", cfg->no_applic_k);
86 write_setting_i (sesskey, "NoApplicationCursors", cfg->no_applic_c);
a9422f39 87 write_setting_i (sesskey, "ApplicationCursorKeys", cfg->app_cursor);
88 write_setting_i (sesskey, "ApplicationKeypad", cfg->app_keypad);
89 write_setting_i (sesskey, "NetHackKeypad", cfg->nethack_keypad);
90 write_setting_i (sesskey, "AltF4", cfg->alt_f4);
91 write_setting_i (sesskey, "AltSpace", cfg->alt_space);
a094ae43 92 write_setting_i (sesskey, "AltOnly", cfg->alt_only);
93 write_setting_i (sesskey, "ComposeKey", cfg->compose_key);
a9422f39 94 write_setting_i (sesskey, "LdiscTerm", cfg->ldisc_term);
e95edc00 95 write_setting_i (sesskey, "AlwaysOnTop", cfg->alwaysontop);
4e30ff69 96 write_setting_i (sesskey, "CurType", cfg->cursor_type);
a9422f39 97 write_setting_i (sesskey, "BlinkCur", cfg->blink_cur);
98 write_setting_i (sesskey, "Beep", cfg->beep);
99 write_setting_i (sesskey, "ScrollbackLines", cfg->savelines);
100 write_setting_i (sesskey, "DECOriginMode", cfg->dec_om);
101 write_setting_i (sesskey, "AutoWrapMode", cfg->wrap_mode);
102 write_setting_i (sesskey, "LFImpliesCR", cfg->lfhascr);
103 write_setting_i (sesskey, "WinNameAlways", cfg->win_name_always);
104 write_setting_s (sesskey, "WinTitle", cfg->wintitle);
105 write_setting_i (sesskey, "TermWidth", cfg->width);
106 write_setting_i (sesskey, "TermHeight", cfg->height);
107 write_setting_s (sesskey, "Font", cfg->font);
108 write_setting_i (sesskey, "FontIsBold", cfg->fontisbold);
109 write_setting_i (sesskey, "FontCharSet", cfg->fontcharset);
110 write_setting_i (sesskey, "FontHeight", cfg->fontheight);
111 write_setting_i (sesskey, "FontVTMode", cfg->vtmode);
112 write_setting_i (sesskey, "TryPalette", cfg->try_palette);
113 write_setting_i (sesskey, "BoldAsColour", cfg->bold_colour);
114 for (i=0; i<22; i++) {
115 char buf[20], buf2[30];
116 sprintf(buf, "Colour%d", i);
117 sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
118 cfg->colours[i][1], cfg->colours[i][2]);
119 write_setting_s (sesskey, buf, buf2);
120 }
121 write_setting_i (sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
122 for (i=0; i<256; i+=32) {
123 char buf[20], buf2[256];
124 int j;
125 sprintf(buf, "Wordness%d", i);
126 *buf2 = '\0';
127 for (j=i; j<i+32; j++) {
128 sprintf(buf2+strlen(buf2), "%s%d",
129 (*buf2 ? "," : ""), cfg->wordness[j]);
130 }
131 write_setting_s (sesskey, buf, buf2);
132 }
133 write_setting_i (sesskey, "KoiWinXlat", cfg->xlat_enablekoiwin);
134 write_setting_i (sesskey, "88592Xlat", cfg->xlat_88592w1250);
b0faa571 135 write_setting_i (sesskey, "88592-CP852", cfg->xlat_88592cp852);
a9422f39 136 write_setting_i (sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
137 write_setting_i (sesskey, "ScrollBar", cfg->scrollbar);
138 write_setting_i (sesskey, "ScrollOnKey", cfg->scroll_on_key);
a094ae43 139 write_setting_i (sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
a9422f39 140 write_setting_i (sesskey, "LockSize", cfg->locksize);
141 write_setting_i (sesskey, "BCE", cfg->bce);
142 write_setting_i (sesskey, "BlinkText", cfg->blinktext);
143
144 close_settings_w(sesskey);
145}
146
147void load_settings (char *section, int do_host, Config *cfg) {
148 int i;
149 char prot[10];
150 void *sesskey;
151
152 sesskey = open_settings_r(section);
153
154 gpps (sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
155 gppi (sesskey, "PortNumber", default_port, &cfg->port);
e1c8e0ed 156 gpps (sesskey, "LogFileName", "putty.log",
157 cfg->logfilename, sizeof(cfg->logfilename));
158 gppi (sesskey, "LogType", 0, &cfg->logtype);
a9422f39 159
160 gpps (sesskey, "Protocol", "default", prot, 10);
161 cfg->protocol = default_protocol;
162 for (i = 0; backends[i].name != NULL; i++)
163 if (!strcmp(prot, backends[i].name)) {
164 cfg->protocol = backends[i].protocol;
165 break;
166 }
167
168 gppi (sesskey, "CloseOnExit", 1, &cfg->close_on_exit);
169 gppi (sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
ec55b220 170 gppi (sesskey, "PingInterval", 0, &cfg->ping_interval);
a9422f39 171 gpps (sesskey, "TerminalType", "xterm", cfg->termtype,
172 sizeof(cfg->termtype));
173 gpps (sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
174 sizeof(cfg->termspeed));
175 {
176 char buf[2*sizeof(cfg->environmt)], *p, *q;
177 gpps (sesskey, "Environment", "", buf, sizeof(buf));
178 p = buf;
179 q = cfg->environmt;
180 while (*p) {
181 while (*p && *p != ',') {
182 int c = *p++;
183 if (c == '=')
184 c = '\t';
185 if (c == '\\')
186 c = *p++;
187 *q++ = c;
188 }
189 if (*p == ',') p++;
190 *q++ = '\0';
191 }
192 *q = '\0';
193 }
194 gpps (sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
195 gppi (sesskey, "NoPTY", 0, &cfg->nopty);
4ba9b64b 196 gppi (sesskey, "Compression", 0, &cfg->compression);
a9422f39 197 gppi (sesskey, "AgentFwd", 0, &cfg->agentfwd);
198 gpps (sesskey, "RemoteCmd", "", cfg->remote_cmd, sizeof(cfg->remote_cmd));
199 {
200 char cipher[10];
201 gpps (sesskey, "Cipher", "3des", cipher, 10);
202 if (!strcmp(cipher, "blowfish"))
203 cfg->cipher = CIPHER_BLOWFISH;
204 else if (!strcmp(cipher, "des"))
205 cfg->cipher = CIPHER_DES;
206 else
207 cfg->cipher = CIPHER_3DES;
208 }
209 gppi (sesskey, "SshProt", 1, &cfg->sshprot);
7591b9ff 210 gppi (sesskey, "BuggyMAC", 0, &cfg->buggymac);
a9422f39 211 gppi (sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
212 gpps (sesskey, "PublicKeyFile", "", cfg->keyfile, sizeof(cfg->keyfile));
213 gpps (sesskey, "RemoteCommand", "", cfg->remote_cmd,
214 sizeof(cfg->remote_cmd));
215 gppi (sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
216 gppi (sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
217 gppi (sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
218 gppi (sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
b00f8b34 219 gppi (sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
220 gppi (sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
a9422f39 221 gppi (sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
222 gppi (sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
223 gppi (sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
224 gppi (sesskey, "AltF4", 1, &cfg->alt_f4);
225 gppi (sesskey, "AltSpace", 0, &cfg->alt_space);
a094ae43 226 gppi (sesskey, "AltOnly", 0, &cfg->alt_only);
227 gppi (sesskey, "ComposeKey", 0, &cfg->compose_key);
a9422f39 228 gppi (sesskey, "LdiscTerm", 0, &cfg->ldisc_term);
e95edc00 229 gppi (sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
4e30ff69 230 gppi (sesskey, "CurType", 0, &cfg->cursor_type);
a9422f39 231 gppi (sesskey, "BlinkCur", 0, &cfg->blink_cur);
232 gppi (sesskey, "Beep", 1, &cfg->beep);
233 gppi (sesskey, "ScrollbackLines", 200, &cfg->savelines);
234 gppi (sesskey, "DECOriginMode", 0, &cfg->dec_om);
235 gppi (sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
236 gppi (sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
237 gppi (sesskey, "WinNameAlways", 0, &cfg->win_name_always);
238 gpps (sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
239 gppi (sesskey, "TermWidth", 80, &cfg->width);
240 gppi (sesskey, "TermHeight", 24, &cfg->height);
241 gpps (sesskey, "Font", "Courier", cfg->font, sizeof(cfg->font));
242 gppi (sesskey, "FontIsBold", 0, &cfg->fontisbold);
243 gppi (sesskey, "FontCharSet", ANSI_CHARSET, &cfg->fontcharset);
244 gppi (sesskey, "FontHeight", 10, &cfg->fontheight);
245 gppi (sesskey, "FontVTMode", VT_OEMANSI, (int *)&cfg->vtmode);
246 gppi (sesskey, "TryPalette", 0, &cfg->try_palette);
247 gppi (sesskey, "BoldAsColour", 1, &cfg->bold_colour);
248 for (i=0; i<22; i++) {
249 static char *defaults[] = {
250 "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
251 "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
252 "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
253 "85,85,255", "187,0,187", "255,85,255", "0,187,187",
254 "85,255,255", "187,187,187", "255,255,255"
255 };
256 char buf[20], buf2[30];
257 int c0, c1, c2;
258 sprintf(buf, "Colour%d", i);
259 gpps (sesskey, buf, defaults[i], buf2, sizeof(buf2));
260 if(sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
261 cfg->colours[i][0] = c0;
262 cfg->colours[i][1] = c1;
263 cfg->colours[i][2] = c2;
264 }
265 }
266 gppi (sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
267 for (i=0; i<256; i+=32) {
268 static char *defaults[] = {
269 "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",
270 "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",
271 "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",
272 "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",
273 "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",
274 "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",
275 "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",
276 "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"
277 };
278 char buf[20], buf2[256], *p;
279 int j;
280 sprintf(buf, "Wordness%d", i);
281 gpps (sesskey, buf, defaults[i/32], buf2, sizeof(buf2));
282 p = buf2;
283 for (j=i; j<i+32; j++) {
284 char *q = p;
285 while (*p && *p != ',') p++;
286 if (*p == ',') *p++ = '\0';
287 cfg->wordness[j] = atoi(q);
288 }
289 }
290 gppi (sesskey, "KoiWinXlat", 0, &cfg->xlat_enablekoiwin);
291 gppi (sesskey, "88592Xlat", 0, &cfg->xlat_88592w1250);
b0faa571 292 gppi (sesskey, "88592-CP852", 0, &cfg->xlat_88592cp852);
a9422f39 293 gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
294 gppi (sesskey, "ScrollBar", 1, &cfg->scrollbar);
295 gppi (sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
a094ae43 296 gppi (sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
a9422f39 297 gppi (sesskey, "LockSize", 0, &cfg->locksize);
298 gppi (sesskey, "BCE", 0, &cfg->bce);
299 gppi (sesskey, "BlinkText", 0, &cfg->blinktext);
300
301 close_settings_r(sesskey);
302}
303
304void do_defaults (char *session, Config *cfg) {
305 if (session)
306 load_settings (session, TRUE, cfg);
307 else
308 load_settings ("Default Settings", FALSE, cfg);
309}
310
a84ca2f5 311static int sessioncmp(const void *av, const void *bv) {
312 const char *a = *(const char *const *)av;
313 const char *b = *(const char *const *)bv;
314
315 /*
316 * Alphabetical order, except that "Default Settings" is a
317 * special case and comes first.
318 */
319 if (!strcmp(a, "Default Settings"))
320 return -1; /* a comes first */
321 if (!strcmp(b, "Default Settings"))
322 return +1; /* b comes first */
323 return strcmp(a, b); /* otherwise, compare normally */
324}
325
a9422f39 326void get_sesslist(int allocate) {
327 static char otherbuf[2048];
328 static char *buffer;
329 int buflen, bufsize, i;
330 char *p, *ret;
331 void *handle;
332
333 if (allocate) {
334
335 if ((handle = enum_settings_start()) == NULL)
336 return;
337
338 buflen = bufsize = 0;
339 buffer = NULL;
340 do {
341 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
342 if (ret) {
343 int len = strlen(otherbuf)+1;
344 if (bufsize < buflen+len) {
345 bufsize = buflen + len + 2048;
346 buffer = srealloc(buffer, bufsize);
347 }
348 strcpy(buffer+buflen, otherbuf);
349 buflen += strlen(buffer+buflen)+1;
350 }
351 } while (ret);
352 enum_settings_finish(handle);
353 buffer = srealloc(buffer, buflen+1);
354 buffer[buflen] = '\0';
355
2221af18 356 /*
357 * Now set up the list of sessions. Note that "Default
358 * Settings" must always be claimed to exist, even if it
359 * doesn't really.
360 */
361
a9422f39 362 p = buffer;
2221af18 363 nsessions = 1; /* "Default Settings" counts as one */
a9422f39 364 while (*p) {
2221af18 365 if (strcmp(p, "Default Settings"))
366 nsessions++;
a9422f39 367 while (*p) p++;
368 p++;
369 }
370
2221af18 371 sessions = smalloc((nsessions+1) * sizeof(char *));
372 sessions[0] = "Default Settings";
a9422f39 373 p = buffer;
2221af18 374 i = 1;
a9422f39 375 while (*p) {
2221af18 376 if (strcmp(p, "Default Settings"))
377 sessions[i++] = p;
a9422f39 378 while (*p) p++;
379 p++;
380 }
a84ca2f5 381
382 qsort(sessions, i, sizeof(char *), sessioncmp);
a9422f39 383 } else {
384 sfree (buffer);
385 sfree (sessions);
386 }
387}