1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile xprf2.c:
|
---|
4 | * copy-profile functions which use the replacement
|
---|
5 | * profile functions in xprf.c.
|
---|
6 | *
|
---|
7 | * This can be used for a bomb-proof "save system profiles"
|
---|
8 | * (see xprfSaveINIs).
|
---|
9 | *
|
---|
10 | * Usage: All OS/2 programs.
|
---|
11 | *
|
---|
12 | * Function prefixes:
|
---|
13 | * -- xprf* replacement profile (INI) functions
|
---|
14 | *
|
---|
15 | * Note: Version numbering in this file relates to XWorkplace version
|
---|
16 | * numbering.
|
---|
17 | *
|
---|
18 | *@@header "helpers\xprf.h"
|
---|
19 | *@@added V0.9.5 (2000-08-10) [umoeller]
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * Copyright (C) 2000 Ulrich Mller.
|
---|
24 | * This file is part of the XWorkplace source package.
|
---|
25 | * XWorkplace is free software; you can redistribute it and/or modify
|
---|
26 | * it under the terms of the GNU General Public License as published
|
---|
27 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
28 | * "COPYING" file of the XWorkplace main distribution.
|
---|
29 | * This program is distributed in the hope that it will be useful,
|
---|
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
32 | * GNU General Public License for more details.
|
---|
33 | */
|
---|
34 |
|
---|
35 | #define OS2EMX_PLAIN_CHAR
|
---|
36 | // this is needed for "os2emx.h"; if this is defined,
|
---|
37 | // emx will define PSZ as _signed_ char, otherwise
|
---|
38 | // as unsigned char
|
---|
39 |
|
---|
40 | #define INCL_DOS
|
---|
41 | #define INCL_DOSERRORS
|
---|
42 | #define INCL_WINSHELLDATA
|
---|
43 | #include <os2.h>
|
---|
44 |
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <string.h>
|
---|
48 |
|
---|
49 | #include "setup.h" // code generation and debugging options
|
---|
50 |
|
---|
51 | #include "helpers\dosh.h"
|
---|
52 | #include "helpers\prfh.h"
|
---|
53 | #include "helpers\stringh.h"
|
---|
54 | #include "helpers\xprf.h"
|
---|
55 |
|
---|
56 | #pragma hdrstop
|
---|
57 |
|
---|
58 | /*
|
---|
59 | *@@category: Helpers\Profile (INI) replacement functions
|
---|
60 | */
|
---|
61 |
|
---|
62 | /* ******************************************************************
|
---|
63 | * *
|
---|
64 | * Copy API Functions *
|
---|
65 | * *
|
---|
66 | ********************************************************************/
|
---|
67 |
|
---|
68 | /*
|
---|
69 | *@@ xprfCopyKey:
|
---|
70 | * copies a single key from an Prf* HINI to an xprf* PXINI.
|
---|
71 | * hiniTarget must therefore have been opened using
|
---|
72 | * xprfOpenProfile.
|
---|
73 | *
|
---|
74 | * This returns 0 (NO_ERROR) if copying succeeded. Otherwise either
|
---|
75 | * an OS/2 error code (ERROR_*) or one of the profile error
|
---|
76 | * codes defined in prfh.h is returned.
|
---|
77 | */
|
---|
78 |
|
---|
79 | APIRET xprfCopyKey(HINI hiniSource, // in: source profile (can be HINI_USER or HINI_SYSTEM)
|
---|
80 | PSZ pszSourceApp, // in: source application
|
---|
81 | PSZ pszKey, // in: source/target key
|
---|
82 | PXINI hiniTarget, // in: target profile opened with xprfOpenProfile
|
---|
83 | PSZ pszTargetApp) // in: target app
|
---|
84 | {
|
---|
85 | ULONG ulSizeOfData = 0;
|
---|
86 | APIRET arc = NO_ERROR;
|
---|
87 |
|
---|
88 | if (PrfQueryProfileSize(hiniSource, pszSourceApp, pszKey, &ulSizeOfData))
|
---|
89 | {
|
---|
90 | PSZ pData = 0;
|
---|
91 |
|
---|
92 | // copy data
|
---|
93 | if (ulSizeOfData == 0)
|
---|
94 | {
|
---|
95 | // data size == 0: this shouldn't really happen,
|
---|
96 | // but if it does, we'll just create a NULL string.
|
---|
97 | // Users have reported that some INI files seem to
|
---|
98 | // contain those "empty" keys. I don't see how these
|
---|
99 | // can exist, but they seem to...
|
---|
100 | pData = (PSZ)malloc(1);
|
---|
101 | *pData = 0;
|
---|
102 | }
|
---|
103 | else
|
---|
104 | pData = (PSZ)malloc(ulSizeOfData);
|
---|
105 |
|
---|
106 | if (pData)
|
---|
107 | {
|
---|
108 | fflush(stdout);
|
---|
109 | if (PrfQueryProfileData(hiniSource,
|
---|
110 | pszSourceApp,
|
---|
111 | pszKey,
|
---|
112 | pData,
|
---|
113 | &ulSizeOfData))
|
---|
114 | {
|
---|
115 | if (!xprfWriteProfileData(hiniTarget,
|
---|
116 | pszTargetApp,
|
---|
117 | pszKey,
|
---|
118 | pData,
|
---|
119 | ulSizeOfData))
|
---|
120 | arc = PRFERR_WRITE;
|
---|
121 | }
|
---|
122 | else
|
---|
123 | arc = PRFERR_READ;
|
---|
124 |
|
---|
125 | free(pData);
|
---|
126 | }
|
---|
127 | else
|
---|
128 | arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | arc = PRFERR_DATASIZE;
|
---|
132 |
|
---|
133 | return (arc);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /*
|
---|
137 | *@@ xprfCopyApp:
|
---|
138 | * copies a single application from an Prf* HINI to an xprf* PXINI.
|
---|
139 | * hiniTarget must therefore have been opened using
|
---|
140 | * xprfOpenProfile.
|
---|
141 | *
|
---|
142 | * This calls xprfCopyKey for each key in the application.
|
---|
143 | *
|
---|
144 | * This returns 0 (NO_ERROR) if copying succeeded. Otherwise either
|
---|
145 | * an OS/2 error code (ERROR_*) or one of the profile error
|
---|
146 | * codes defined in prfh.h is returned.
|
---|
147 | */
|
---|
148 |
|
---|
149 | APIRET xprfCopyApp(HINI hiniSource, // in: source profile (can be HINI_USER or HINI_SYSTEM)
|
---|
150 | PSZ pszSourceApp, // in: source application
|
---|
151 | PXINI hiniTarget, // in: target profile opened with xprfOpenProfile
|
---|
152 | PSZ pszTargetApp, // in: name of pszSourceApp in hiniTarget
|
---|
153 | PSZ pszErrorKey) // out: failing key in case of error; ptr can be NULL
|
---|
154 | {
|
---|
155 | APIRET arc = NO_ERROR;
|
---|
156 | PSZ pszKeysList;
|
---|
157 |
|
---|
158 | if (pszErrorKey)
|
---|
159 | *pszErrorKey = 0;
|
---|
160 |
|
---|
161 | pszKeysList = prfhQueryKeysForApp(hiniSource, pszSourceApp);
|
---|
162 | if (pszKeysList)
|
---|
163 | {
|
---|
164 | PSZ pKey2 = pszKeysList;
|
---|
165 |
|
---|
166 | while (*pKey2 != 0)
|
---|
167 | {
|
---|
168 | // copy this key
|
---|
169 | arc = xprfCopyKey(hiniSource,
|
---|
170 | pszSourceApp,
|
---|
171 | pKey2,
|
---|
172 | hiniTarget,
|
---|
173 | pszTargetApp);
|
---|
174 | if (arc)
|
---|
175 | {
|
---|
176 | // error: copy failing key to buffer
|
---|
177 | if (pszErrorKey)
|
---|
178 | strcpy(pszErrorKey, pKey2);
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | pKey2 += strlen(pKey2)+1;
|
---|
182 | } // end while (*pKey2 != 0)
|
---|
183 |
|
---|
184 | free (pszKeysList);
|
---|
185 | }
|
---|
186 | else
|
---|
187 | arc = PRFERR_KEYSLIST;
|
---|
188 |
|
---|
189 | return (arc);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*
|
---|
193 | *@@ xprfCopyProfile:
|
---|
194 | * this copies an entire profile.
|
---|
195 | *
|
---|
196 | * The source profile must have been opened using the
|
---|
197 | * regular OS/2 PrfOpenProfile. You can also specify
|
---|
198 | * HINI_USER or HINI_SYSTEM.
|
---|
199 | *
|
---|
200 | * pszNew specifies the file name for the new profile.
|
---|
201 | * This must end in *.INI. The new profile is opened
|
---|
202 | * using xprfOpenProfile, so see additional remarks there.
|
---|
203 | *
|
---|
204 | * This returns 0 (NO_ERROR) on success. Otherwise either
|
---|
205 | * an OS/2 error code (ERROR_*) or one of the profile error
|
---|
206 | * codes defined in prfh.h is returned.
|
---|
207 | */
|
---|
208 |
|
---|
209 | APIRET xprfCopyProfile(HINI hOld, // in: source profile (can be HINI_USER or HINI_SYSTEM)
|
---|
210 | PSZ pszNew, // in: new filename (can be fully qualified)
|
---|
211 | PFN_PRF_PROGRESS pfnProgressCallback,
|
---|
212 | ULONG ulUser, // in: passed to pfnProgressCallback
|
---|
213 | ULONG ulCount, // in: index of INI being copied (0 <= ulCount <= ulMax)
|
---|
214 | ULONG ulMax) // in: maximum index (for progress); 0 means 1 INI, 1 means 2 INIs, ...
|
---|
215 | {
|
---|
216 | APIRET arc = NO_ERROR;
|
---|
217 | PXINI pxiniNew = NULL;
|
---|
218 | ULONG ulSizeOfAppsList;
|
---|
219 |
|
---|
220 | if (!pszNew)
|
---|
221 | arc = ERROR_INVALID_PARAMETER;
|
---|
222 |
|
---|
223 | if (arc == NO_ERROR)
|
---|
224 | {
|
---|
225 |
|
---|
226 | DosDelete(pszNew);
|
---|
227 |
|
---|
228 | // open new profile
|
---|
229 |
|
---|
230 | arc = xprfOpenProfile(pszNew,
|
---|
231 | &pxiniNew);
|
---|
232 | }
|
---|
233 |
|
---|
234 | // get size of applications list
|
---|
235 | if (arc == NO_ERROR)
|
---|
236 | {
|
---|
237 | if (!PrfQueryProfileSize(hOld, NULL, NULL, &ulSizeOfAppsList))
|
---|
238 | arc = PRFERR_APPSLIST;
|
---|
239 | else
|
---|
240 | if (ulSizeOfAppsList == 0)
|
---|
241 | arc = PRFERR_APPSLIST;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (arc == NO_ERROR)
|
---|
245 | {
|
---|
246 | // get applications list
|
---|
247 | PSZ pApps = (PSZ)malloc(ulSizeOfAppsList);
|
---|
248 | PSZ pApp2 = pApps;
|
---|
249 | if (!PrfQueryProfileData(hOld,
|
---|
250 | NULL,
|
---|
251 | NULL,
|
---|
252 | pApps,
|
---|
253 | &ulSizeOfAppsList))
|
---|
254 | arc = PRFERR_APPSLIST;
|
---|
255 |
|
---|
256 | // applications loop
|
---|
257 |
|
---|
258 | while ( (*pApp2 != 0)
|
---|
259 | && (arc == NO_ERROR)
|
---|
260 | )
|
---|
261 | {
|
---|
262 | CHAR szErrorKey[1000];
|
---|
263 | // copy application (this will call prfhCopyKey in turn)
|
---|
264 | arc = xprfCopyApp(hOld,
|
---|
265 | pApp2,
|
---|
266 | pxiniNew,
|
---|
267 | pApp2,
|
---|
268 | szErrorKey);
|
---|
269 |
|
---|
270 | if (pfnProgressCallback)
|
---|
271 | {
|
---|
272 | ULONG ulNow2, ulMax2;
|
---|
273 | ulNow2 = ((1000*(pApp2-pApps)) / ulSizeOfAppsList) + (ulCount*1000);
|
---|
274 | ulMax2 = (ulMax+1)*1000;
|
---|
275 | if (!pfnProgressCallback(ulUser, ulNow2, ulMax2))
|
---|
276 | // aborted:
|
---|
277 | arc = PRFERR_ABORTED;
|
---|
278 | }
|
---|
279 |
|
---|
280 | // go for next app
|
---|
281 | pApp2 += strlen(pApp2)+1;
|
---|
282 |
|
---|
283 | } // end while (*pApp2 != 0) && MBID_NOERROR
|
---|
284 |
|
---|
285 | if (pApps)
|
---|
286 | free(pApps);
|
---|
287 | }
|
---|
288 |
|
---|
289 | xprfCloseProfile(pxiniNew);
|
---|
290 |
|
---|
291 | // progress
|
---|
292 | if (pfnProgressCallback)
|
---|
293 | pfnProgressCallback(ulUser, (ulCount+1) * 1000, (ulMax+1) * 1000);
|
---|
294 |
|
---|
295 | return (arc);
|
---|
296 | }
|
---|
297 |
|
---|
298 | /*
|
---|
299 | *@@ xprfSaveINIs:
|
---|
300 | * this rewrites the OS/2 user and system profiles
|
---|
301 | * (OS2.INI and OS2SYS.INI) to disk. This is done
|
---|
302 | * by doing the following:
|
---|
303 | *
|
---|
304 | * -- First, both profiles are dumped into two temporary
|
---|
305 | * profiles (using xprfCopyProfile on each of them).
|
---|
306 | * These are put into the same directory as the source
|
---|
307 | * profiles (?:\OS2 normally) with a file name extension
|
---|
308 | * of OS2*.XFL.
|
---|
309 | *
|
---|
310 | * -- Only if that succeeded, the original profiles are
|
---|
311 | * renamed to OS2*.BAK. Existing OS2*.BAK files are
|
---|
312 | * deleted.
|
---|
313 | *
|
---|
314 | * -- If that succeded, the temporary OS2*.XFL files are
|
---|
315 | * renamed to OS2*.INI.
|
---|
316 | *
|
---|
317 | * This is now used during XShutdown to dump the system
|
---|
318 | * profiles before shutting down the system. As opposed to
|
---|
319 | * prfhSaveINIs, which was previously used, this does not
|
---|
320 | * use the Prf* functions for the target profiles so that
|
---|
321 | * those ugly "Error saving INI files" errors are less likely.
|
---|
322 | *
|
---|
323 | * This returns 0 (NO_ERROR) if copying succeeded. Otherwise either
|
---|
324 | * an OS/2 error code (ERROR_*) or one of the profile error
|
---|
325 | * codes defined in prfh.h is returned.
|
---|
326 | */
|
---|
327 |
|
---|
328 | APIRET xprfSaveINIs(HAB hab, // in: anchor block
|
---|
329 | PFN_PRF_PROGRESS pfnProgressCallback,
|
---|
330 | ULONG ulUser)
|
---|
331 | {
|
---|
332 | PRFPROFILE Profiles;
|
---|
333 | APIRET arc = NO_ERROR;
|
---|
334 |
|
---|
335 | // FILESTATUS3 fs3;
|
---|
336 | CHAR szSysNew[CCHMAXPATH],
|
---|
337 | szUserNew[CCHMAXPATH],
|
---|
338 | szSysBackup[CCHMAXPATH],
|
---|
339 | szUserBackup[CCHMAXPATH];
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * get system profiles:
|
---|
343 | *
|
---|
344 | */
|
---|
345 |
|
---|
346 | Profiles.cchUserName = Profiles.cchSysName = 0;
|
---|
347 | if (!PrfQueryProfile(hab, &Profiles))
|
---|
348 | arc = PRFERR_QUERY;
|
---|
349 | else
|
---|
350 | {
|
---|
351 | Profiles.pszUserName = (PSZ)malloc(Profiles.cchUserName);
|
---|
352 | Profiles.pszSysName = (PSZ)malloc(Profiles.cchSysName);
|
---|
353 | if ( (Profiles.pszSysName == NULL)
|
---|
354 | || (Profiles.pszUserName == NULL)
|
---|
355 | )
|
---|
356 | arc = PRFERR_QUERY;
|
---|
357 | else
|
---|
358 | if (!PrfQueryProfile(hab, &Profiles))
|
---|
359 | arc = PRFERR_QUERY;
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (arc == NO_ERROR)
|
---|
363 | {
|
---|
364 | PSZ _p;
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * create new profile names:
|
---|
368 | * same as old profiles, but with *.XFL ext.
|
---|
369 | */
|
---|
370 |
|
---|
371 | // system INI
|
---|
372 | strcpy(szSysBackup, Profiles.pszSysName);
|
---|
373 | strcpy(szSysNew, Profiles.pszSysName);
|
---|
374 | _p = strhistr(szSysBackup, ".INI");
|
---|
375 | if (!_p)
|
---|
376 | arc = PRFERR_INVALID_FILE_NAME;
|
---|
377 | else
|
---|
378 | strcpy(_p, ".BAK");
|
---|
379 | _p = strhistr(szSysNew, ".INI");
|
---|
380 | if (!_p)
|
---|
381 | arc = PRFERR_INVALID_FILE_NAME;
|
---|
382 | else
|
---|
383 | strcpy(_p, ".XFL");
|
---|
384 |
|
---|
385 | // user INI
|
---|
386 | strcpy(szUserBackup, Profiles.pszUserName);
|
---|
387 | strcpy(szUserNew, Profiles.pszUserName);
|
---|
388 | _p = strhistr(szUserBackup, ".INI");
|
---|
389 | if (!_p)
|
---|
390 | arc = PRFERR_INVALID_FILE_NAME;
|
---|
391 | else
|
---|
392 | strcpy(_p, ".BAK");
|
---|
393 | _p = strhistr(szUserNew, ".INI");
|
---|
394 | if (!_p)
|
---|
395 | arc = PRFERR_INVALID_FILE_NAME;
|
---|
396 | else
|
---|
397 | strcpy(_p, ".XFL");
|
---|
398 |
|
---|
399 | /*
|
---|
400 | * create OS2SYS.XFL:
|
---|
401 | *
|
---|
402 | */
|
---|
403 |
|
---|
404 | if (arc == NO_ERROR)
|
---|
405 | arc = xprfCopyProfile(HINI_SYSTEM,
|
---|
406 | szSysNew, // new filename
|
---|
407 | pfnProgressCallback,
|
---|
408 | ulUser, 0, 1);
|
---|
409 | /*
|
---|
410 | * create OS2SYS.XFL:
|
---|
411 | *
|
---|
412 | */
|
---|
413 |
|
---|
414 | if (arc == NO_ERROR)
|
---|
415 | arc = xprfCopyProfile(HINI_USER,
|
---|
416 | szUserNew, // new filename
|
---|
417 | pfnProgressCallback,
|
---|
418 | ulUser, 1, 1);
|
---|
419 | }
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * renaming stuff for OS2SYS.INI
|
---|
423 | *
|
---|
424 | */
|
---|
425 |
|
---|
426 | if (arc == NO_ERROR)
|
---|
427 | {
|
---|
428 | // attrib -r -s -h -a OS2SYS.BAK
|
---|
429 | doshSetPathAttr(szSysBackup, FILE_NORMAL);
|
---|
430 | // delete OS2SYS.BAK
|
---|
431 | DosDelete(szSysBackup);
|
---|
432 | // attrib -r -s -h -a OS2SYS.INI
|
---|
433 | doshSetPathAttr(Profiles.pszSysName, FILE_NORMAL);
|
---|
434 | // REN OS2SYS.INI OS2SYS.BAK
|
---|
435 | DosMove(Profiles.pszSysName, szSysBackup);
|
---|
436 | }
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * renaming stuff for OS2.INI
|
---|
440 | *
|
---|
441 | */
|
---|
442 |
|
---|
443 | if (arc == NO_ERROR)
|
---|
444 | {
|
---|
445 | // attrib -r -s -h -a OS2SYS.BAK
|
---|
446 | doshSetPathAttr(szUserBackup, FILE_NORMAL);
|
---|
447 | // delete OS2SYS.BAK
|
---|
448 | DosDelete(szUserBackup);
|
---|
449 | // attrib -r -s -h -a OS2SYS.INI
|
---|
450 | doshSetPathAttr(Profiles.pszUserName, FILE_NORMAL);
|
---|
451 | // REN OS2SYS.INI OS2SYS.BAK
|
---|
452 | DosMove(Profiles.pszUserName, szUserBackup);
|
---|
453 | }
|
---|
454 |
|
---|
455 | if (arc == NO_ERROR)
|
---|
456 | {
|
---|
457 | // finally, replace system profiles
|
---|
458 | arc = DosMove(szSysNew, Profiles.pszSysName);
|
---|
459 | if (arc == NO_ERROR)
|
---|
460 | arc = DosMove(szUserNew, Profiles.pszUserName);
|
---|
461 | }
|
---|
462 |
|
---|
463 | if ( (Profiles.pszSysName)
|
---|
464 | && (Profiles.pszUserName)
|
---|
465 | )
|
---|
466 | {
|
---|
467 | free(Profiles.pszSysName);
|
---|
468 | free(Profiles.pszUserName);
|
---|
469 | }
|
---|
470 |
|
---|
471 | return (arc);
|
---|
472 | }
|
---|
473 |
|
---|
474 | // testing
|
---|
475 |
|
---|
476 | /* BOOL _Optlink fnCallback(ULONG ulUser, ULONG ulNow, ULONG ulMax)
|
---|
477 | {
|
---|
478 | printf("\r done %03d%%", ulNow * 100 / ulMax);
|
---|
479 | return (TRUE);
|
---|
480 | }
|
---|
481 |
|
---|
482 | int main(int argc, char* argv[])
|
---|
483 | {
|
---|
484 | if (argc != 3)
|
---|
485 | printf("Syntax: xprf2 <source.ini> <target.ini>\n");
|
---|
486 | else
|
---|
487 | {
|
---|
488 | HAB hab = WinInitialize(0);
|
---|
489 |
|
---|
490 | // HINI hiniSource = PrfOpenProfile(hab, argv[1]);
|
---|
491 | // if (hiniSource)
|
---|
492 | {
|
---|
493 | APIRET arc = xprfCopyProfile(hiniSource,
|
---|
494 | argv[2],
|
---|
495 | fnCallback,
|
---|
496 | 0, 0, 0);
|
---|
497 | xprfSaveProfiles(hab, NULL, 0);
|
---|
498 | // if (arc)
|
---|
499 |
|
---|
500 |
|
---|
501 | // printf("xprfCopyProfile returned %d.\n", arc);
|
---|
502 | }
|
---|
503 | // else
|
---|
504 | // printf("Cannot open %s\n", argv[1]);
|
---|
505 |
|
---|
506 | WinTerminate(hab);
|
---|
507 | }
|
---|
508 |
|
---|
509 | return (0);
|
---|
510 | } */
|
---|
511 |
|
---|