source: trunk/src/helpers/prfh2.c@ 9

Last change on this file since 9 was 8, checked in by umoeller, 25 years ago

Initial checkin of helpers code which used to be in WarpIN.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 22.0 KB
Line 
1
2/*
3 *@@sourcefile prfh2.c:
4 * contains more Presentation Manager helper functions
5 * which deal with Profile (Prf*) functions.
6 *
7 * This file is new with V0.9.4 (2000-07-26) [umoeller].
8 *
9 * As opposed to the functions in prfh.c, these require
10 * linking against other helpers. As a result, these have
11 * been separated from prfh.c to allow linking against
12 * prfh.obj only.
13 *
14 * Function prefixes:
15 * -- prfh* Prf (profile, INI) helper functions
16 *
17 * Note: Version numbering in this file relates to XWorkplace version
18 * numbering.
19 *
20 *@@header "helpers\prfh.h"
21 *@@added V0.9.4 (2000-07-27) [umoeller]
22 */
23
24/*
25 * Copyright (C) 1997-2000 Ulrich M”ller.
26 * This file is part of the XWorkplace source package.
27 * XWorkplace is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published
29 * by the Free Software Foundation, in version 2 as it comes in the
30 * "COPYING" file of the XWorkplace main distribution.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 */
36
37#define OS2EMX_PLAIN_CHAR
38 // this is needed for "os2emx.h"; if this is defined,
39 // emx will define PSZ as _signed_ char, otherwise
40 // as unsigned char
41
42#define INCL_DOS
43#define INCL_DOSERRORS
44#define INCL_WIN
45#include <os2.h>
46
47#include <stdlib.h>
48#include <stdio.h>
49#include <string.h>
50
51#include "setup.h" // code generation and debugging options
52
53#include "helpers\dosh.h"
54#include "helpers\stringh.h"
55
56#include "helpers\prfh.h"
57
58#pragma hdrstop
59
60/*
61 *@@category: Helpers\Profile (INI) helpers
62 */
63
64/*
65 * prfhINIError:
66 * this is called whenever an error occurs saving a
67 * profile. This will compose an error string and
68 * all the callback func fncbError.
69 *
70 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
71 */
72
73ULONG prfhINIError(ULONG ulOptions,
74 FILE* fLog,
75 PFNWP fncbError,
76 PSZ pszErrorString)
77{
78 ULONG ulrc;
79 CHAR szError2[2000];
80
81 if (fncbError)
82 {
83 if (ulOptions == MB_ABORTRETRYIGNORE)
84 sprintf(szError2, "%s"
85 "\nPress 'Abort' to abort saving the INI files. This will restart the WPS to avoid loss of data."
86 "\nPress 'Retry' to attempt saving this INI file again. "
87 "\nPress 'Ignore' to ignore this error. This might help, but "
88 "also lead to more errors or loss of the currently processed data.",
89 pszErrorString);
90 else
91 sprintf(szError2, "%s\nPress 'Cancel' to abort shutdown. ", pszErrorString);
92 ulrc = ( (ULONG)(*fncbError)(0, 0, szError2, (MPARAM)ulOptions) );
93
94 }
95 else
96 ulrc = (MBID_ABORT);
97
98 if (fLog)
99 {
100 fprintf(fLog, " Error occured: %s\n Return code: %s (%d)\n",
101 pszErrorString,
102 (ulrc == MBID_ABORT) ? "MBID_ABORT"
103 : (ulrc == MBID_IGNORE) ? "MBID_IGNORE"
104 : (ulrc == MBID_RETRY) ? "MBID_RETRY"
105 : "unknown",
106 ulrc);
107 fflush(fLog);
108 }
109
110 return (ulrc);
111}
112
113/*
114 * prfhINIError2:
115 * like the previous func, but with additional info
116 * for the error string.
117 *
118 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
119 */
120
121ULONG prfhINIError2(ULONG ulOptions,
122 PSZ pszINI,
123 FILE* fLog,
124 PFNWP fncbError,
125 PSZ pszErrorString)
126{
127 CHAR szError2[2000];
128 sprintf(szError2, "An error occured copying the profile %s: \n%s",
129 pszINI, pszErrorString);
130 return (prfhINIError(ulOptions, fLog, fncbError, szError2));
131}
132
133/*
134 *@@ prfhCopyProfile:
135 * this function copies a given profile entirely into a
136 * new file. hOld is the handle, pszOld the filename
137 * of the profile to be copied (e.g. HINI_USERPROFILE
138 * and "?:\OS2\OS2.INI"; pszNew must point to a
139 * buffer which will contain the new filename ending
140 * in ".XFL" after prfhCopyProfile returns
141 * (e.g. "?:\OS2\OS2.XFL").
142 *
143 * You may specify a callback procedure which gets
144 * called upon every copied application in the INI
145 * file; this way, you can provide a progress bar.
146 * The second callback, fncbError, gets called upon
147 * errors.
148 *
149 *@@changed V0.9.0 [umoeller]: this now calls prfhCopyApp.
150 *@@changed V0.9.0 [umoeller]: fixed crashes with lower-case INI file names.
151 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
152 *@@changed V0.9.4 (2000-07-15) [umoeller]: this never reported any errors, causing 0-byte INI files without warning. Fixed.
153 */
154
155BOOL prfhCopyProfile(HAB hab, // in: anchor block
156 FILE* fLog, // in: stdio logfile or NULL for no log
157 HINI hOld, // in: HINI to copy
158 PSZ pszOld, // in: fully qualif. filename of hOld
159 PSZ pszNew, // out: new filename
160 PFNWP fncbUpdate, // in: progress callback
161 HWND hwnd, ULONG msg, ULONG ulCount, ULONG ulMax,
162 // in: passed to fncbUpdate
163 PFNWP fncbError) // in: error callback
164{
165 // BOOL rc = TRUE;
166 HINI hNew;
167 PSZ pApps, // pKeys,
168 pApp2; // , pKey2;
169 ULONG ulSizeOfAppsList; // , ulSizeOfKeysList; // , ulSizeOfData;
170 // CHAR szLog[1000]; // removed V0.9.0
171 #define MBID_NOERROR 999
172 ULONG ulErrorStatus = MBID_NOERROR;
173 PSZ p_;
174 CHAR szError2[2000];
175
176 if (fLog)
177 {
178 fprintf(fLog, " Entering prfhCopyProfile, params: "
179 "hiniOld 0x%lX, pszOld %s, pfnwpCallback 0x%lX, "
180 "hwnd 0x%lX, msg 0x%lX, ulCount 0x%lX, ulMax 0x%lX\n",
181 hOld, pszOld, fncbUpdate, hwnd, msg, ulCount, ulMax);
182 fflush(fLog);
183 }
184
185 if ( (!pszOld) || (!pszNew) )
186 ulErrorStatus = prfhINIError2(MB_CANCEL, pszOld, fLog, fncbError,
187 "Invalid name buffers given.");
188
189 if (ulErrorStatus == MBID_NOERROR)
190 {
191 strupr(pszOld);
192 strcpy(pszNew, pszOld);
193 p_ = strhistr(pszNew, ".INI");
194 if (!p_)
195 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
196 "Error composing new filename.");
197 }
198
199 if (ulErrorStatus == MBID_NOERROR)
200 {
201 strcpy(p_, ".XFL");
202
203 // delete new profile, in case something's left
204 if (fLog)
205 fprintf(fLog, " DEL %s\n", pszNew);
206
207 DosDelete(pszNew);
208
209 // open new profile
210 if (fLog)
211 fprintf(fLog, " PrfOpenProfile %s\n", pszNew);
212
213 hNew = PrfOpenProfile(hab, pszNew);
214 if (!hNew)
215 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
216 "Error creating temporary profile.");
217 }
218
219 // get size of applications list
220 if (ulErrorStatus == MBID_NOERROR)
221 {
222 if (!PrfQueryProfileSize(hOld, NULL, NULL, &ulSizeOfAppsList))
223 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
224 "Error querying applications list size.");
225 else
226 if (ulSizeOfAppsList == 0)
227 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
228 "Size of applications list cannot be 0.");
229 }
230
231 if (ulErrorStatus == MBID_NOERROR)
232 {
233 // do-while loop in case of MBID_RETRY
234 do
235 {
236 ulErrorStatus = MBID_NOERROR; // if we have a retry in the do-while loop
237
238 // get applications list
239 pApps = (PSZ)malloc(ulSizeOfAppsList);
240 pApp2 = pApps;
241 if (!PrfQueryProfileData(hOld,
242 NULL,
243 NULL,
244 pApps,
245 &ulSizeOfAppsList))
246 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
247 "Could not query application list.");
248
249 // applications loop
250
251 while ( (*pApp2 != 0)
252 && (ulErrorStatus == MBID_NOERROR)
253 )
254 {
255 CHAR szErrorKey[1000];
256 // copy application (this will call prfhCopyKey in turn)
257 ULONG ulrc = prfhCopyApp(hOld,
258 pApp2,
259 hNew,
260 pApp2,
261 szErrorKey);
262
263 ulErrorStatus = MBID_ABORT;
264
265 switch (ulrc)
266 {
267 case 0: // no error
268 ulErrorStatus = MBID_NOERROR;
269 break;
270
271 case PRFERR_KEYSLIST:
272 // couldn't query keys list
273 sprintf(szError2,
274 "Error querying keys list for \"%s\".",
275 pApp2);
276 break;
277
278 case PRFERR_DATASIZE:
279 // couldn't query data size for key
280 sprintf(szError2,
281 "Error querying data size for \"%s\"/\"%s\".",
282 pApp2, szErrorKey);
283 break;
284
285 case ERROR_NOT_ENOUGH_MEMORY:
286 // couldn't allocate memory
287 sprintf(szError2,
288 "Error allocating memory for copying \"%s\"/\"%s\".",
289 pApp2, szErrorKey);
290 break;
291
292 case PRFERR_READ:
293 // couldn't read data from source (PrfQueryProfileData error)
294 sprintf(szError2,
295 "Error reading data from app \"%s\", key \"%s\".",
296 pApp2, szErrorKey);
297 break;
298
299 case PRFERR_WRITE:
300 // couldn't write data to target (PrfWriteProfileData error)
301 sprintf(szError2,
302 "Error writing data to app \"%s\", key \"%s\".",
303 pApp2, szErrorKey);
304 break;
305 }
306
307 if (ulErrorStatus != MBID_NOERROR) // V0.9.4 (2000-07-15) [umoeller]
308 {
309 ulErrorStatus = prfhINIError2(MB_ABORTRETRYIGNORE, pszNew, fLog, fncbError,
310 szError2);
311 }
312
313 if (fncbUpdate)
314 {
315 ULONG ulNow2, ulMax2;
316 ulNow2 = ((1000*(pApp2-pApps))/ulSizeOfAppsList) + (ulCount*1000);
317 ulMax2 = (ulMax+1)*1000; // ulAppsSize;
318 (*fncbUpdate)(hwnd, msg, (MPARAM)ulNow2, (MPARAM)ulMax2);
319 }
320
321 // go for next app
322 pApp2 += strlen(pApp2)+1;
323
324 if (ulErrorStatus == MBID_IGNORE)
325 // error occured, but user pressed "Ignore":
326 // skip this app
327 ulErrorStatus = MBID_NOERROR;
328
329 } // end while (*pApp2 != 0) && MBID_NOERROR
330
331 if (pApps)
332 free(pApps);
333
334 if (fLog)
335 fprintf(fLog, " Done copying apps\n");
336
337 } while (ulErrorStatus == MBID_RETRY);
338 } // end if (ulErrorOccured == MBID_NOERROR)
339
340 if (fLog)
341 fprintf(fLog, " PrfCloseProfile %s\n", pszNew);
342
343 PrfCloseProfile(hNew);
344 if (fncbUpdate)
345 (*fncbUpdate)(hwnd, msg, (MPARAM)(ulCount+1), (MPARAM)(ulMax+1));
346
347 if (fLog)
348 {
349 fprintf(fLog, " Leaving prfhCopyProfile.\n");
350 fflush(fLog);
351 }
352
353 return (ulErrorStatus == MBID_NOERROR); // FALSE if error occured
354}
355
356/*
357 *@@ prfhSaveINIs:
358 * this will enforce saving of OS2.INI and OS2SYS.INI
359 * by calling prfhCopyProfile (above) on them; the therefrom
360 * resulting ".XFL" files will replace the original
361 * ".INI" files.
362 * Specify fncbUpdate and fncbError like in prfhCopyProfile.
363 *
364 *@@changed V0.9.0 [umoeller]: fixed crashes with lower-case INI file names.
365 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
366 */
367
368APIRET prfhSaveINIs(HAB hab, // in: anchor block
369 FILE* fLog, // in: stdio logfile or NULL for no log
370 PFNWP fncbUpdate, // in: progress callback
371 HWND hwnd, ULONG msg, // in: params passed to fncbUpdate
372 PFNWP fncbError) // in: error callback
373{
374 PRFPROFILE Profiles;
375 APIRET arc = NO_ERROR, arc2;
376 BOOL brc = TRUE;
377
378 // FILESTATUS3 fs3;
379 CHAR szSysNew[CCHMAXPATH],
380 szUserNew[CCHMAXPATH],
381 szSysBackup[CCHMAXPATH],
382 szUserBackup[CCHMAXPATH];
383
384 // the following flag may be one of the following:
385 // MBID_NOERROR--- everything's fine, continue
386 // MBID_IGNORE --- error occured, but ignore
387 // MBID_RETRY --- error occured, but retry
388 // MBID_ABORT --- error occured, abort saving
389 ULONG ulErrorOccured = MBID_IGNORE;
390
391 if (fLog)
392 fprintf(fLog, " Entering prfhSaveINIs...\n");
393
394 /*
395 * get system profiles:
396 *
397 */
398
399 Profiles.cchUserName = Profiles.cchSysName = 0;
400 brc = PrfQueryProfile(hab, &Profiles);
401
402 if (!brc)
403 ulErrorOccured = prfhINIError(MB_CANCEL, fLog, fncbError,
404 "Error querying system profiles size.");
405
406 if (ulErrorOccured == MBID_IGNORE)
407 {
408 Profiles.pszUserName = (PSZ)malloc(Profiles.cchUserName);
409 Profiles.pszSysName = (PSZ)malloc(Profiles.cchSysName);
410 if ( (Profiles.pszSysName == NULL)
411 || (Profiles.pszUserName == NULL)
412 )
413 ulErrorOccured = prfhINIError(MB_CANCEL, fLog, fncbError,
414 "Error allocating memory (1).");
415 }
416
417 if (ulErrorOccured == MBID_IGNORE)
418 {
419 if (!PrfQueryProfile(hab, &Profiles))
420 ulErrorOccured = prfhINIError(MB_CANCEL, fLog, fncbError,
421 "Error querying profiles (2).");
422 }
423
424 if (ulErrorOccured == MBID_IGNORE)
425 {
426 if (fLog)
427 fprintf(fLog, " System profiles are %s, %s\n",
428 Profiles.pszUserName, Profiles.pszSysName);
429
430 /*
431 * create new profile names:
432 *
433 */
434
435 strcpy(szSysBackup, Profiles.pszSysName);
436 strcpy(strhistr(szSysBackup, ".INI"), ".BAK");
437 strcpy(szUserBackup, Profiles.pszUserName);
438 strcpy(strhistr(szUserBackup, ".INI"), ".BAK");
439
440 /*
441 * create OS2SYS.XFL:
442 *
443 */
444
445 if (fLog)
446 fprintf(fLog, " Storing %s in *.XFL\n",
447 Profiles.pszSysName);
448
449 if (!prfhCopyProfile(hab,
450 fLog,
451 HINI_SYSTEMPROFILE,
452 Profiles.pszSysName, // old filename
453 szSysNew, // new filename
454 fncbUpdate, hwnd, msg, 0, 1,
455 fncbError))
456 {
457 // abort, since prfhCopyProfile already has error handling
458 ulErrorOccured = MBID_ABORT;
459 }
460 }
461
462 if (ulErrorOccured == MBID_IGNORE)
463 {
464 // create OS2.XFL
465
466 if (fLog)
467 fprintf(fLog, " Storing %s in *.XFL\n",
468 Profiles.pszUserName);
469
470 if (!prfhCopyProfile(hab,
471 fLog,
472 HINI_USERPROFILE,
473 Profiles.pszUserName, szUserNew,
474 fncbUpdate, hwnd, msg, 1, 1,
475 fncbError))
476 {
477 // abort, since prfhCopyProfile already has error handling
478 ulErrorOccured = MBID_ABORT;
479 }
480 }
481
482 /*
483 * renaming stuff for OS2SYS.INI
484 *
485 */
486
487 if (ulErrorOccured == MBID_IGNORE)
488 {
489 do
490 {
491 ulErrorOccured = MBID_IGNORE; // if we have a retry in the do-while loop
492
493 if (ulErrorOccured == MBID_IGNORE)
494 {
495 // attrib -r -s -h -a OS2SYS.BAK
496 if (fLog)
497 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
498 szSysBackup);
499
500 arc2 = doshSetPathAttr(szSysBackup, FILE_NORMAL);
501 if (fLog)
502 fprintf(fLog, " rc2: %d\n", arc2);
503
504 // delete OS2SYS.BAK
505 if (fLog)
506 fprintf(fLog, " DEL %s\n",
507 szSysBackup);
508
509 arc2 = DosDelete(szSysBackup);
510 if (fLog)
511 fprintf(fLog, " rc2: %d\n", arc2);
512
513 // attrib -r -s -h -a OS2SYS.INI
514 if (fLog)
515 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
516 Profiles.pszSysName);
517
518 arc2 = doshSetPathAttr(Profiles.pszSysName, FILE_NORMAL);
519 if (fLog)
520 fprintf(fLog, " rc2: %d\n", arc2);
521
522 // move OS2SYS.INI OS2SYS.BAK
523 if (fLog)
524 fprintf(fLog, " MOVE %s %s\n",
525 Profiles.pszSysName, szSysBackup);
526 arc = DosMove(Profiles.pszSysName, szSysBackup);
527 if (fLog)
528 fprintf(fLog, " rc: %d\n", arc);
529
530 if (arc)
531 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving original system profile to backup.");
532 }
533
534 if (ulErrorOccured == MBID_IGNORE)
535 {
536 if (fLog)
537 fprintf(fLog, " MOVE %s %s\n",
538 szSysNew, Profiles.pszSysName);
539 arc = DosMove(szSysNew, Profiles.pszSysName);
540 if (fLog)
541 fprintf(fLog, " rc: %d\n", arc);
542 if (arc)
543 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving newly created profile to system profile.");
544 }
545 } while (ulErrorOccured == MBID_RETRY);
546 }
547
548 /*
549 * renaming stuff for OS2.INI
550 *
551 */
552
553 if (ulErrorOccured == MBID_IGNORE)
554 {
555 do
556 {
557 ulErrorOccured = MBID_IGNORE; // if we have a retry in the do-while loop
558
559 if (ulErrorOccured == MBID_IGNORE)
560 {
561 // attrib -r -s -h -a OS2.BAK
562 if (fLog)
563 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
564 szUserBackup);
565
566 arc2 = doshSetPathAttr(szUserBackup, FILE_NORMAL);
567 if (fLog)
568 fprintf(fLog, " rc2: %d\n", arc2);
569
570 // delete OS2.BAK
571 if (fLog)
572 fprintf(fLog," DEL %s\n",
573 szUserBackup);
574
575 arc2 = DosDelete(szUserBackup);
576 if (fLog)
577 fprintf(fLog, " rc2: %d\n", arc2);
578
579 // attrib -r -s -h -a OS2.INI
580 if (fLog)
581 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
582 Profiles.pszUserName);
583 arc2 = doshSetPathAttr(Profiles.pszUserName, FILE_NORMAL);
584 if (fLog)
585 fprintf(fLog, " rc2: %d\n", arc2);
586
587 // move OS2.INI OS2.BAK
588 if (fLog)
589 fprintf(fLog, " MOVE %s %s\n",
590 Profiles.pszUserName, szUserBackup);
591 arc = DosMove(Profiles.pszUserName, szUserBackup);
592 if (fLog)
593 fprintf(fLog, " rc: %d\n", arc);
594
595 if (arc)
596 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving original user profile to backup.");
597 }
598
599 if (ulErrorOccured == MBID_IGNORE)
600 {
601 // move OS2.XFL OS2.INI
602 if (fLog)
603 fprintf(fLog, " MOVE %s %s\n",
604 szUserNew, Profiles.pszUserName);
605
606 arc = DosMove(szUserNew, Profiles.pszUserName);
607 if (fLog)
608 fprintf(fLog, " rc: %d\n", arc);
609
610 if (arc)
611 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving newly created profile to user profile.");
612 }
613 } while (ulErrorOccured == MBID_RETRY);
614 }
615
616 // DosExitCritSec();
617
618 if (ulErrorOccured != MBID_IGNORE)
619 {
620 DosMove(szSysBackup, Profiles.pszSysName);
621 DosMove(szUserBackup, Profiles.pszUserName);
622 }
623
624 if ( (Profiles.pszSysName)
625 && (Profiles.pszUserName)
626 )
627 {
628 free(Profiles.pszSysName);
629 free(Profiles.pszUserName);
630 }
631
632 if (fLog)
633 fprintf(fLog, " Done with prfhSaveINIs\n");
634
635 if (ulErrorOccured != MBID_IGNORE)
636 return (999);
637 else
638 return (NO_ERROR);
639}
640
641
Note: See TracBrowser for help on using the repository browser.