1 /* $Id: macstore.c,v 1.13 2003/02/01 12:54:40 simon Exp $ */
4 * macstore.c: Macintosh-specific impementation of the interface
11 #include <Resources.h>
12 #include <TextUtils.h>
23 OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit);
26 * We store each session as a file in the "PuTTY" sub-directory of the
27 * preferences folder. Each (key,value) pair is stored as a resource.
30 OSErr get_putty_dir(Boolean makeit, short *pVRefNum, long *pDirID)
35 long prefDirID, puttyDirID;
37 error = FindFolder(kOnSystemDisk, kPreferencesFolderType, makeit,
38 &prefVRefNum, &prefDirID);
39 if (error != noErr) goto out;
41 error = FSMakeFSSpec(prefVRefNum, prefDirID, "\pPuTTY", &puttydir);
42 if (error != noErr && error != fnfErr) goto out;
43 error = FSpGetDirID(&puttydir, &puttyDirID, makeit);
44 if (error != noErr) goto out;
46 *pVRefNum = prefVRefNum;
53 OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) {
57 long puttyDirID, sessDirID;
59 error = get_putty_dir(makeit, &puttyVRefNum, &puttyDirID);
60 if (error != noErr) goto out;
61 error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pSaved Sessions",
63 if (error != noErr && error != fnfErr) goto out;
64 error = FSpGetDirID(&sessdir, &sessDirID, makeit);
65 if (error != noErr) goto out;
67 *pVRefNum = puttyVRefNum;
74 OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit) {
78 pb.dirInfo.ioNamePtr = f->name;
79 pb.dirInfo.ioVRefNum = f->vRefNum;
80 pb.dirInfo.ioDrDirID = f->parID;
81 pb.dirInfo.ioFDirIndex = 0;
82 error = PBGetCatInfoSync(&pb);
83 if (error == fnfErr && makeit)
84 return FSpDirCreate(f, smSystemScript, idp);
85 if (error != noErr) goto out;
86 if ((pb.dirInfo.ioFlAttrib & ioDirMask) == 0) {
90 *idp = pb.dirInfo.ioDrDirID;
96 /* Copy a resource into the current resource file */
97 static OSErr copy_resource(ResType restype, short resid)
102 h = GetResource(restype, resid);
104 GetResInfo(h, &resid, &restype, resname);
106 AddResource(h, restype, resid, resname);
107 if (ResError() == noErr)
113 struct write_settings {
119 void *open_settings_w(char const *sessionname) {
126 error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID);
127 if (error != noErr) return NULL;
129 c2pstrcpy(psessionname, sessionname);
130 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &dstfile);
131 if (error == fnfErr) {
132 FSpCreateResFile(&dstfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript);
133 if ((error = ResError()) != noErr) return NULL;
134 } else if (error != noErr) return NULL;
136 return open_settings_w_fsp(&dstfile);
140 * NB: Destination file must exist.
142 void *open_settings_w_fsp(FSSpec *dstfile)
146 struct write_settings *ws;
150 ws = smalloc(sizeof *ws);
151 ws->dstfile = *dstfile;
153 /* Create a temporary file to save to first. */
154 error = FindFolder(ws->dstfile.vRefNum, kTemporaryFolderType,
155 kCreateFolder, &tmpVRefNum, &tmpDirID);
156 if (error != noErr) goto out;
157 c2pstrcpy(tmpname, tmpnam(NULL));
158 error = FSMakeFSSpec(tmpVRefNum, tmpDirID, tmpname, &ws->tmpfile);
159 if (error != noErr && error != fnfErr) goto out;
160 if (error == noErr) {
161 error = FSpDelete(&ws->tmpfile);
162 if (error != noErr) goto out;
164 FSpCreateResFile(&ws->tmpfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript);
165 if ((error = ResError()) != noErr) goto out;
167 ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm);
168 if (ws->fd == -1) {error = ResError(); goto out;}
170 /* Set up standard resources. Doesn't matter if these fail. */
171 copy_resource('STR ', -16396);
172 copy_resource('TMPL', TMPL_Int);
178 fatalbox("Failed to open session for write (%d)", error);
181 void write_setting_s(void *handle, char const *key, char const *value) {
182 int fd = *(int *)handle;
188 if (ResError() != noErr)
189 fatalbox("Failed to open saved session (%d)", ResError());
191 error = PtrToHand(value, &h, strlen(value));
193 fatalbox("Failed to allocate memory");
194 /* Put the data in a resource. */
195 id = Unique1ID(FOUR_CHAR_CODE('TEXT'));
196 if (ResError() != noErr)
197 fatalbox("Failed to get ID for resource %s (%d)", key, ResError());
198 addresource(h, FOUR_CHAR_CODE('TEXT'), id, key);
199 if (ResError() != noErr)
200 fatalbox("Failed to add resource %s (%d)", key, ResError());
203 void write_setting_i(void *handle, char const *key, int value) {
204 int fd = *(int *)handle;
210 if (ResError() != noErr)
211 fatalbox("Failed to open saved session (%d)", ResError());
213 /* XXX assume all systems have the same "int" format */
214 error = PtrToHand(&value, &h, sizeof(int));
216 fatalbox("Failed to allocate memory (%d)", error);
218 /* Put the data in a resource. */
219 id = Unique1ID(FOUR_CHAR_CODE('Int '));
220 if (ResError() != noErr)
221 fatalbox("Failed to get ID for resource %s (%d)", key, ResError());
222 addresource(h, FOUR_CHAR_CODE('Int '), id, key);
223 if (ResError() != noErr)
224 fatalbox("Failed to add resource %s (%d)", key, ResError());
227 void close_settings_w(void *handle) {
228 struct write_settings *ws = handle;
231 CloseResFile(ws->fd);
232 if ((error = ResError()) != noErr)
234 error = FSpExchangeFiles(&ws->tmpfile, &ws->dstfile);
235 if (error != noErr) goto out;
236 error = FSpDelete(&ws->tmpfile);
237 if (error != noErr) goto out;
241 fatalbox("Close of saved session failed (%d)", error);
245 void *open_settings_r(char const *sessionname)
253 error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID);
255 c2pstrcpy(psessionname, sessionname);
256 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile);
257 if (error != noErr) goto out;
258 return open_settings_r_fsp(&sessfile);
264 void *open_settings_r_fsp(FSSpec *sessfile)
270 fd = FSpOpenResFile(sessfile, fsRdPerm);
271 if (fd == 0) {error = ResError(); goto out;}
273 handle = safemalloc(sizeof *handle);
281 char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) {
286 if (handle == NULL) goto out;
289 if (ResError() != noErr) goto out;
290 h = get1namedresource(FOUR_CHAR_CODE('TEXT'), key);
291 if (h == NULL) goto out;
293 len = GetHandleSize(h);
294 if (len + 1 > buflen) goto out;
295 memcpy(buffer, *h, len);
299 if (ResError() != noErr) goto out;
306 int read_setting_i(void *handle, char const *key, int defvalue) {
311 if (handle == NULL) goto out;
314 if (ResError() != noErr) goto out;
315 h = get1namedresource(FOUR_CHAR_CODE('Int '), key);
316 if (h == NULL) goto out;
319 if (ResError() != noErr) goto out;
326 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
331 if (!read_setting_s(handle, name, ret.name, sizeof(ret.name)))
333 settingname = dupcat(name, "IsBold", NULL);
334 ret.isbold = read_setting_i(handle, settingname, -1);
336 if (ret.isbold == -1) return 0;
337 if (ret.charset == -1) return 0;
338 settingname = dupcat(name, "Height", NULL);
339 ret.height = read_setting_i(handle, settingname, INT_MIN);
341 if (ret.height == INT_MIN) return 0;
346 void write_setting_fontspec(void *handle, const char *name, FontSpec font)
350 write_setting_s(handle, name, font.name);
351 settingname = dupcat(name, "IsBold", NULL);
352 write_setting_i(handle, settingname, font.isbold);
354 settingname = dupcat(name, "Height", NULL);
355 write_setting_i(handle, settingname, font.height);
359 int read_setting_filename(void *handle, const char *name, Filename *result)
361 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
364 void write_setting_filename(void *handle, const char *name, Filename result)
366 write_setting_s(handle, name, result.path);
369 void close_settings_r(void *handle) {
372 if (handle == NULL) return;
375 if (ResError() != noErr)
376 fatalbox("Close of saved session failed (%d)", ResError());
380 void del_settings(char const *sessionname) {
387 error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID);
389 c2pstrcpy(psessionname, sessionname);
390 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile);
391 if (error != noErr) goto out;
393 error = FSpDelete(&sessfile);
396 fatalbox("Delete session failed (%d)", error);
399 struct enum_settings_state {
405 void *enum_settings_start(void) {
407 struct enum_settings_state *state;
409 state = safemalloc(sizeof(*state));
410 error = get_session_dir(kDontCreateFolder, &state->vRefNum, &state->dirID);
411 if (error != noErr) {
419 char *enum_settings_next(void *handle, char *buffer, int buflen) {
420 struct enum_settings_state *e = handle;
425 if (e == NULL) return NULL;
427 pb.hFileInfo.ioNamePtr = name;
428 pb.hFileInfo.ioVRefNum = e->vRefNum;
429 pb.hFileInfo.ioDirID = e->dirID;
430 pb.hFileInfo.ioFDirIndex = e->index++;
431 error = PBGetCatInfoSync(&pb);
432 if (error != noErr) return NULL;
433 } while (!((pb.hFileInfo.ioFlAttrib & ioDirMask) == 0 &&
434 pb.hFileInfo.ioFlFndrInfo.fdCreator == PUTTY_CREATOR &&
435 pb.hFileInfo.ioFlFndrInfo.fdType == SESS_TYPE &&
438 p2cstrcpy(buffer, name);
442 void enum_settings_finish(void *handle) {
447 #define SEED_SIZE 512
449 void read_random_seed(noise_consumer_t consumer)
456 long count = SEED_SIZE;
458 if (get_putty_dir(kDontCreateFolder, &puttyVRefNum, &puttyDirID) != noErr)
460 if (HOpenDF(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", fsRdPerm,
463 error = FSRead(refnum, &count, buf);
464 if (error != noErr && error != eofErr)
466 (*consumer)(buf, count);
471 * We don't bother with the usual FSpExchangeFiles dance here because
472 * it doesn't really matter if the old random seed gets lost.
474 void write_random_seed(void *data, int len)
483 if (get_putty_dir(kCreateFolder, &puttyVRefNum, &puttyDirID) != noErr)
486 error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed",
488 if (error == fnfErr) {
489 /* Set up standard resources */
490 FSpCreateResFile(&dstfile, INTERNAL_CREATOR, SEED_TYPE, smRoman);
491 refnum = FSpOpenResFile(&dstfile, fsWrPerm);
492 if (ResError() == noErr) {
493 copy_resource('STR ', -16397);
494 CloseResFile(refnum);
496 } else if (error != noErr) return;
498 if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return;
499 FSWrite(refnum, &count, data);
508 * c-file-style: "simon"