83d43549 |
1 | /* $Id: macstore.c,v 1.4 2002/12/28 22:25:31 ben Exp $ */ |
d082ac49 |
2 | |
3 | /* |
4 | * macstore.c: Macintosh-specific impementation of the interface |
5 | * defined in storage.h |
6 | */ |
7 | |
8 | #include <MacTypes.h> |
9 | #include <Folders.h> |
10 | #include <Memory.h> |
11 | #include <Resources.h> |
12 | #include <TextUtils.h> |
13 | |
14 | #include <string.h> |
15 | |
16 | #include "putty.h" |
17 | #include "storage.h" |
18 | |
19 | #define PUTTY_CREATOR FOUR_CHAR_CODE('pTTY') |
20 | #define SESS_TYPE FOUR_CHAR_CODE('Sess') |
21 | |
22 | |
23 | OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID); |
24 | OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit); |
25 | |
26 | /* |
27 | * We store each session as a file in the "PuTTY" sub-directory of the |
28 | * preferences folder. Each (key,value) pair is stored as a resource. |
29 | */ |
30 | |
31 | OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) { |
32 | OSErr error = noErr; |
33 | short prefVRefNum; |
34 | FSSpec puttydir, sessdir; |
35 | long prefDirID, puttyDirID, sessDirID; |
36 | |
37 | error = FindFolder(kOnSystemDisk, kPreferencesFolderType, makeit, |
38 | &prefVRefNum, &prefDirID); |
39 | if (error != noErr) goto out; |
40 | |
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; |
45 | |
46 | error = FSMakeFSSpec(prefVRefNum, puttyDirID, "\pSaved Sessions", |
47 | &sessdir); |
48 | if (error != noErr && error != fnfErr) goto out; |
49 | error = FSpGetDirID(&sessdir, &sessDirID, makeit); |
50 | if (error != noErr) goto out; |
51 | |
52 | *pVRefNum = prefVRefNum; |
53 | *pDirID = sessDirID; |
54 | |
55 | out: |
56 | return error; |
57 | } |
58 | |
59 | OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit) { |
60 | CInfoPBRec pb; |
61 | OSErr error = noErr; |
62 | |
63 | pb.dirInfo.ioNamePtr = f->name; |
64 | pb.dirInfo.ioVRefNum = f->vRefNum; |
65 | pb.dirInfo.ioDrDirID = f->parID; |
66 | pb.dirInfo.ioFDirIndex = 0; |
67 | error = PBGetCatInfoSync(&pb); |
68 | if (error == fnfErr && makeit) |
69 | return FSpDirCreate(f, smSystemScript, idp); |
70 | if (error != noErr) goto out; |
71 | if ((pb.dirInfo.ioFlAttrib & ioDirMask) == 0) { |
72 | error = dirNFErr; |
73 | goto out; |
74 | } |
75 | *idp = pb.dirInfo.ioDrDirID; |
76 | |
77 | out: |
78 | return error; |
79 | } |
80 | |
81 | struct write_settings { |
82 | int fd; |
83 | FSSpec tmpfile; |
84 | FSSpec dstfile; |
85 | }; |
86 | |
87 | void *open_settings_w(char *sessionname) { |
88 | short sessVRefNum, tmpVRefNum; |
89 | long sessDirID, tmpDirID; |
d082ac49 |
90 | OSErr error; |
91 | Str255 psessionname; |
92 | struct write_settings *ws; |
93 | |
94 | ws = safemalloc(sizeof *ws); |
95 | error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID); |
96 | if (error != noErr) goto out; |
97 | |
98 | c2pstrcpy(psessionname, sessionname); |
99 | error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &ws->dstfile); |
100 | if (error != noErr && error != fnfErr) goto out; |
101 | if (error == fnfErr) { |
102 | FSpCreateResFile(&ws->dstfile, PUTTY_CREATOR, SESS_TYPE, |
103 | smSystemScript); |
104 | if ((error = ResError()) != noErr) goto out; |
105 | } |
106 | |
107 | /* Create a temporary file to save to first. */ |
108 | error = FindFolder(sessVRefNum, kTemporaryFolderType, kCreateFolder, |
109 | &tmpVRefNum, &tmpDirID); |
110 | if (error != noErr) goto out; |
111 | error = FSMakeFSSpec(tmpVRefNum, tmpDirID, psessionname, &ws->tmpfile); |
112 | if (error != noErr && error != fnfErr) goto out; |
113 | if (error == noErr) { |
114 | error = FSpDelete(&ws->tmpfile); |
115 | if (error != noErr) goto out; |
116 | } |
117 | FSpCreateResFile(&ws->tmpfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript); |
118 | if ((error = ResError()) != noErr) goto out; |
119 | |
120 | ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm); |
121 | if (ws->fd == -1) {error = ResError(); goto out;} |
122 | |
123 | return ws; |
124 | |
125 | out: |
126 | safefree(ws); |
127 | fatalbox("Failed to open session for write (%d)", error); |
128 | } |
129 | |
130 | void write_setting_s(void *handle, char *key, char *value) { |
131 | int fd = *(int *)handle; |
132 | Handle h; |
133 | int id; |
d082ac49 |
134 | |
135 | UseResFile(fd); |
136 | if (ResError() != noErr) |
137 | fatalbox("Failed to open saved session (%d)", ResError()); |
138 | |
139 | error = PtrToHand(value, &h, strlen(value)); |
140 | if (error != noErr) |
141 | fatalbox("Failed to allocate memory"); |
142 | /* Put the data in a resource. */ |
143 | id = Unique1ID(FOUR_CHAR_CODE('TEXT')); |
144 | if (ResError() != noErr) |
145 | fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); |
146 | addresource(h, FOUR_CHAR_CODE('TEXT'), id, key); |
147 | if (ResError() != noErr) |
148 | fatalbox("Failed to add resource %s (%d)", key, ResError()); |
149 | } |
150 | |
151 | void write_setting_i(void *handle, char *key, int value) { |
152 | int fd = *(int *)handle; |
153 | Handle h; |
154 | int id; |
155 | OSErr error; |
156 | |
157 | UseResFile(fd); |
158 | if (ResError() != noErr) |
159 | fatalbox("Failed to open saved session (%d)", ResError()); |
160 | |
161 | /* XXX assume all systems have the same "int" format */ |
162 | error = PtrToHand(&value, &h, sizeof(int)); |
163 | if (error != noErr) |
164 | fatalbox("Failed to allocate memory (%d)", error); |
165 | |
166 | /* Put the data in a resource. */ |
167 | id = Unique1ID(FOUR_CHAR_CODE('Int ')); |
168 | if (ResError() != noErr) |
169 | fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); |
170 | addresource(h, FOUR_CHAR_CODE('Int '), id, key); |
171 | if (ResError() != noErr) |
172 | fatalbox("Failed to add resource %s (%d)", key, ResError()); |
173 | } |
174 | |
175 | void close_settings_w(void *handle) { |
176 | struct write_settings *ws = handle; |
177 | OSErr error; |
178 | |
179 | CloseResFile(ws->fd); |
180 | if ((error = ResError()) != noErr) |
181 | goto out; |
182 | error = FSpExchangeFiles(&ws->tmpfile, &ws->dstfile); |
183 | if (error != noErr) goto out; |
184 | error = FSpDelete(&ws->tmpfile); |
185 | if (error != noErr) goto out; |
186 | return; |
187 | |
188 | out: |
189 | fatalbox("Close of saved session failed (%d)", error); |
190 | safefree(handle); |
191 | } |
192 | |
193 | void *open_settings_r(char *sessionname) { |
194 | short sessVRefNum; |
195 | long sessDirID; |
196 | FSSpec sessfile; |
197 | OSErr error; |
198 | Str255 psessionname; |
199 | int fd; |
200 | int *handle; |
201 | |
202 | error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); |
203 | |
204 | c2pstrcpy(psessionname, sessionname); |
205 | error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); |
206 | if (error != noErr) goto out; |
207 | fd = FSpOpenResFile(&sessfile, fsRdPerm); |
208 | if (fd == 0) {error = ResError(); goto out;} |
209 | |
210 | handle = safemalloc(sizeof *handle); |
211 | *handle = fd; |
212 | return handle; |
213 | |
214 | out: |
215 | return NULL; |
216 | } |
217 | |
218 | |
219 | char *read_setting_s(void *handle, char *key, char *buffer, int buflen) { |
220 | int fd; |
221 | Handle h; |
222 | OSErr error; |
086efcde |
223 | size_t len; |
d082ac49 |
224 | |
225 | if (handle == NULL) goto out; |
226 | fd = *(int *)handle; |
227 | UseResFile(fd); |
228 | if (ResError() != noErr) goto out; |
229 | h = get1namedresource(FOUR_CHAR_CODE('TEXT'), key); |
230 | if (h == NULL) goto out; |
231 | |
086efcde |
232 | len = GetHandleSize(h); |
233 | if (len + 1 > buflen) goto out; |
234 | memcpy(buffer, *h, len); |
235 | buffer[len] = '\0'; |
236 | |
d082ac49 |
237 | ReleaseResource(h); |
238 | if (ResError() != noErr) goto out; |
239 | return buffer; |
240 | |
241 | out: |
242 | return NULL; |
243 | } |
244 | |
245 | int read_setting_i(void *handle, char *key, int defvalue) { |
246 | int fd; |
247 | Handle h; |
d082ac49 |
248 | int value; |
249 | |
250 | if (handle == NULL) goto out; |
251 | fd = *(int *)handle; |
252 | UseResFile(fd); |
253 | if (ResError() != noErr) goto out; |
254 | h = get1namedresource(FOUR_CHAR_CODE('Int '), key); |
255 | if (h == NULL) goto out; |
256 | value = *(int *)*h; |
257 | ReleaseResource(h); |
258 | if (ResError() != noErr) goto out; |
259 | return value; |
260 | |
261 | out: |
262 | return defvalue; |
263 | } |
264 | |
265 | void close_settings_r(void *handle) { |
266 | int fd; |
267 | |
268 | if (handle == NULL) return; |
269 | fd = *(int *)handle; |
270 | CloseResFile(fd); |
271 | if (ResError() != noErr) |
272 | fatalbox("Close of saved session failed (%d)", ResError()); |
273 | safefree(handle); |
274 | } |
275 | |
276 | void del_settings(char *sessionname) { |
277 | OSErr error; |
278 | FSSpec sessfile; |
279 | short sessVRefNum; |
280 | long sessDirID; |
281 | Str255 psessionname; |
282 | |
283 | error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); |
284 | |
285 | c2pstrcpy(psessionname, sessionname); |
286 | error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); |
287 | if (error != noErr) goto out; |
288 | |
289 | error = FSpDelete(&sessfile); |
290 | return; |
291 | out: |
292 | fatalbox("Delete session failed (%d)", error); |
293 | } |
294 | |
295 | struct enum_settings_state { |
296 | short vRefNum; |
297 | long dirID; |
298 | int index; |
299 | }; |
300 | |
301 | void *enum_settings_start(void) { |
302 | OSErr error; |
303 | struct enum_settings_state *state; |
304 | |
305 | state = safemalloc(sizeof(*state)); |
306 | error = get_session_dir(kDontCreateFolder, &state->vRefNum, &state->dirID); |
307 | if (error != noErr) { |
308 | safefree(state); |
309 | return NULL; |
310 | } |
311 | state->index = 1; |
312 | return state; |
313 | } |
314 | |
315 | char *enum_settings_next(void *handle, char *buffer, int buflen) { |
316 | struct enum_settings_state *e = handle; |
317 | CInfoPBRec pb; |
318 | OSErr error = noErr; |
319 | Str255 name; |
320 | |
321 | if (e == NULL) return NULL; |
322 | do { |
323 | pb.hFileInfo.ioNamePtr = name; |
324 | pb.hFileInfo.ioVRefNum = e->vRefNum; |
325 | pb.hFileInfo.ioDirID = e->dirID; |
326 | pb.hFileInfo.ioFDirIndex = e->index++; |
327 | error = PBGetCatInfoSync(&pb); |
328 | if (error != noErr) return NULL; |
329 | } while (!((pb.hFileInfo.ioFlAttrib & ioDirMask) == 0 && |
330 | pb.hFileInfo.ioFlFndrInfo.fdCreator == PUTTY_CREATOR && |
331 | pb.hFileInfo.ioFlFndrInfo.fdType == SESS_TYPE && |
332 | name[0] < buflen)); |
333 | |
334 | p2cstrcpy(buffer, name); |
335 | return buffer; |
336 | } |
337 | |
338 | void enum_settings_finish(void *handle) { |
339 | |
340 | safefree(handle); |
341 | } |
342 | |
343 | |
344 | /* |
345 | * Emacs magic: |
346 | * Local Variables: |
347 | * c-file-style: "simon" |
348 | * End: |
349 | */ |