source: trunk/src/cppbase/bs_config.cpp@ 279

Last change on this file since 279 was 279, checked in by pr, 20 years ago

Fix for CLEARPROFILE and WRITEPROFILE. Bug 633.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 78.0 KB
Line 
1
2/*
3 *@@sourcefile bs_config.cpp:
4 * this has the code for the classes which manipulate CONFIG.SYS,
5 * the WPS class list, create WPS objects, or do other
6 * WarpIN system configuration.
7 *
8 * These classes are designed to work independently of WarpIN
9 * and could be used in any program.
10 * I have attempted to design these classes for easy, yet flexible use.
11 *
12 * All system config classes are derived from BSConfigBase.
13 * This allows for maintaining a list of all system changes
14 * with each WarpIN package, which have a somewhat uniform
15 * interface. Each config class has in turn a special method
16 * which will actually perform the system configuration change.
17 *
18 * This special method will usually take a logger instance
19 * as a parameter, which logs the changes that were actually
20 * made to the system. This logger can then be used later to
21 * undo the change.
22 * See BSCfgSysManip::BSCfgSysManip for an example usage.
23 *
24 * The loggers are all direct, unmodified descendants of the
25 * BSMemLoggerBase class (bs_logger.cpp) and only defined to
26 * use C++ type checking.
27 *
28 * Some config methods throw instances of the BSConfigExcpt
29 * class def'd in bs_config.h, which you should handle.
30 *
31 * These are the classes implemented here:
32 *
33 * -- BSCfgSysManip:
34 * a "manipulator" object of this class holds information
35 * for a CONFIG.SYS change, which is passed to BSConfigSys::Manipulate.
36 * See BSCfgSysManip::BSCfgSysManip for a description.
37 *
38 * -- BSConfigSys:
39 * the actual class which holds the CONFIG.SYS text file
40 * and allows changes to be made.
41 * This class does not have a corresponding "Undo" class.
42 * Instead, use BSCfgSysManip::AddToUndoList.
43 *
44 * -- BSRegisterClass:
45 * this allows registering classes with the WPS.
46 * See BSRegisterClass::Register for an example usage.
47 *
48 * -- BSDeregisterClass:
49 * the "Undo" class to the previous.
50 *
51 * -- BSReplaceClass:
52 * this allows WPS classes to be replaced.
53 * See BSReplaceClass::Replace for an example usage.
54 *
55 * -- BSUnreplaceClass:
56 * the "Undo" class to the previous.
57 *
58 * -- BSCreateWPSObject:
59 * for creating a WPS object.
60 *
61 * -- BSDeleteWPSObject:
62 * the "Undo" class for the previous, to delete a WPS object again.
63 *
64 * See bs_config.h for the declarations of these classes.
65 *
66 *@@header "cppbase\bs_config.h"
67 *@@header "cppbase\bs_config_impl.h"
68 */
69
70/*
71 * This file Copyright (C) 1999-2001 Ulrich M”ller.
72 * This program is free software; you can redistribute it and/or modify
73 * it under the terms of the GNU General Public License as published by
74 * the Free Software Foundation, in version 2 as it comes in the COPYING
75 * file of this distribution.
76 * This program is distributed in the hope that it will be useful,
77 * but WITHOUT ANY WARRANTY; without even the implied warranty of
78 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 * GNU General Public License for more details.
80 */
81
82#define OS2EMX_PLAIN_CHAR
83 // this is needed for "os2emx.h"; if this is defined,
84 // emx will define PSZ as _signed_ char, otherwise
85 // as unsigned char
86
87#define INCL_DOSSESMGR
88#define INCL_DOSSEMAPHORES
89#define INCL_DOSERRORS
90#define INCL_WINWINDOWMGR
91#define INCL_WINMESSAGEMGR
92#define INCL_WINDIALOGS
93#define INCL_WINSTDCNR
94#define INCL_WININPUT
95#define INCL_WINSYS
96#define INCL_WINSHELLDATA
97#define INCL_WINWORKPLACE
98#include <os2.h>
99
100#include <stdio.h>
101#include <stdlib.h>
102#include <string.h>
103#include <stdarg.h>
104
105#include "setup.h"
106
107// include's from helpers
108#include "helpers\dosh.h"
109#include "helpers\winh.h"
110#include "helpers\prfh.h"
111#include "helpers\stringh.h"
112#include "helpers\xstring.h"
113
114#include "helpers\configsys.h"
115
116// front-end includes
117#include "cppbase\bs_base.h"
118#include "cppbase\bs_list.h"
119#include "cppbase\bs_string.h"
120#include "cppbase\bs_errors.h"
121
122#include "cppbase\bs_logger.h"
123#include "cppbase\bs_config.h"
124#include "cppbase\bs_config_impl.h"
125
126#pragma hdrstop
127
128DEFINE_CLASS(BSConfigBase, BSRoot);
129
130DEFINE_CLASS(BSCfgSysManip, BSConfigBase);
131DEFINE_CLASS(BSConfigSys, BSRoot);
132DEFINE_CLASS(BSRegisterClass, BSConfigBase);
133DEFINE_CLASS(BSDeregisterClass, BSConfigBase);
134DEFINE_CLASS(BSReplaceClassBase, BSConfigBase);
135 DEFINE_CLASS(BSReplaceClass, BSReplaceClassBase);
136 DEFINE_CLASS(BSUnreplaceClass, BSReplaceClassBase);
137DEFINE_CLASS(BSCreateWPSObject, BSConfigBase);
138DEFINE_CLASS(BSDeleteWPSObject, BSConfigBase);
139DEFINE_CLASS(BSProfileBase, BSConfigBase);
140 DEFINE_CLASS(BSClearProfile, BSProfileBase);
141 DEFINE_CLASS(BSWriteProfile, BSProfileBase);
142DEFINE_CLASS(BSExecute, BSConfigBase);
143 DEFINE_CLASS(BSDeExecute, BSExecute);
144DEFINE_CLASS(BSKillProcess, BSConfigBase);
145
146 #define CONVERT(c, s) string str ## s(c, _ustr ## s); \
147 PCSZ pcsz ## s = str ## s.c_str()
148
149/* ******************************************************************
150 *
151 * BSCfgSysManip class
152 *
153 ********************************************************************/
154
155/*
156 *@@ BSCfgSysManip(char* pszConfigSys):
157 * this constructor translates a CONFIGSYS attribute (as used
158 * in the PCK tag and stored in the database) into a CONFIGSYSITEM
159 * structure.
160 *
161 * The BSCfgSysManip class is designed for use with the
162 * BSConfigSys class and describes manipulations to be done upon
163 * the CONFIG.SYS file (represented by the BSConfigSys class).
164 *
165 * Manipulating CONFIG.SYS works just as follows:
166 *
167 * 1) Create an instance of BSConfigSys:
168 *
169 + BSConfigSys *pConfigSys = new BSConfigSys;
170 *
171 * This will load your CONFIG.SYS file into the instance's memory.
172 *
173 * 2) Create an instance of BSCfgSysManip and specify the manipulation
174 * in the constructor:
175 *
176 + BSCfgSysManip *pManip = new BSCfgSysManip("SET TEST=YES | UNIQUE");
177 *
178 * The BSCfgSysManip class has a single constructor which takes a PSZ
179 * (char*) as input. This input string has exactly the format like
180 * with the CONFIGSYS attribute to the PCK tag in WarpIN installation
181 * scripts, like this (see the WarpIN Programmer's Reference for details):
182 *
183 + statement [| modifiers]
184 *
185 * "modifiers" can be one of the following:
186 + [UNIQUE[(xxx)]] [vertical]
187 + ADDRIGHT [vertical]
188 + ADDLEFT [vertical]
189 + REMOVELINE
190 + REMOVEPART
191 * with "xxx" being a search string.
192 *
193 * "vertical" can be one of the following:
194 + ADDTOP
195 + ADDAFTER(xxx)
196 + ADDBEFORE(xxx)
197 * with "xxx" being a search string.
198 *
199 * Examples:
200 + "BASEDEV=NEWDRIVR.SYS /WHATEVER | UNIQUE ADDAFTER(IBM1S506.ADD)"
201 + "SET PATH=C:\BLAH | ADDRIGHT"
202 *
203 * After this constructor has successfully converted pszConfigSys,
204 * all the instance data is valid (see cfgsys.h).
205 *
206 * However, this does _not_ handle macro resultion like in WarpIn
207 * scripts (which is done in warpin.cpp before calling the Manipulate
208 * method), because in this file scope we know nothing about the
209 * PackageInfo instances.
210 *
211 * 3) Create a CfgSysDone logger instance:
212 + BSCfgSysDoneLogger logger;
213 *
214 * 4) Invoke the BSConfigSys::Manipulate method with the BSCfgSysManip
215 * instance (this will add data to the logger):
216 + pConfigSys->Manipulate(logger, pManip);
217 *
218 * 5) Write CONFIG.SYS back to disk and clean up.
219 + pConfigSys->Flush(TRUE);
220 + delete pBSCfgSysManip;
221 + delete pConfigSys;
222 *
223 * Now, if you want to undo the changes later, call the static
224 * BSCfgSysManip::AddToUndoList method with the logger passed to
225 * to the various constructor calls (above), which will create
226 * a list of BSCfgSysManip instance which will be able to undo
227 * the changes again:
228 *
229 + list<BSCfgSysManip*> UndoList;
230 + BSCfgSysManip::AddToUndoList(UndoList, logger);
231 *
232 * and iterate over the list and call BSConfigSys::Manipulate with
233 * the objects on that list to undo the changes.
234 *
235 * Throws:
236 * -- BSConfigExcpt.
237 *
238 *@@changed V0.9.1 (2000-01-06) [umoeller]: added UNIQUE(xxx) support
239 *@@changed V0.9.5 (2000-08-26) [umoeller]: fixed UNIQUE(xxx) and REMOVELINE, which deleted wrong lines
240 */
241
242BSCfgSysManip::BSCfgSysManip(const ustring &ustrConfigSys)
243 : BSConfigBase(CFGT_CFGSYS, tBSCfgSysManip)
244{
245 PCSZ pcszConfigSys;
246
247 if (!(pcszConfigSys = ustrConfigSys.GetBuffer()))
248 throw BSConfigExcpt(CFGEXCPT_SYNTAX, 0);
249
250 // initialize all fields to zero
251 // memset(&_ConfigManip, 0, sizeof(_ConfigManip));
252 // V0.9.12 (2001-05-22) [umoeller]
253
254 _iReplaceMode = CFGRPL_ADD; // V1.0.4 (2004-12-18) [pr]
255 _iVertical = CFGVRT_BOTTOM;
256 // Moved above statements here from the else{} block below. If there wasn't a modifier,
257 // this was uninitialised garbage and CONFIG.SYS never got modified.
258
259 // now check if we have modifiers
260 PSZ pSep;
261 if (!(pSep = strchr(pcszConfigSys, '|')))
262 // no modifiers: just copy
263 _ustrNewLine = ustrConfigSys;
264 else
265 {
266 // we do have modifiers:
267 BOOL fVerticalsAllowed = TRUE;
268
269 // get rid of spaces before '|'
270 PSZ pSep2 = pSep-1;
271 while (*pSep2 == ' ')
272 pSep2--;
273
274 // get the "statement" part
275 _ustrNewLine.assignUtf8(pcszConfigSys, pSep2 + 1);
276
277 // remember the modifiers position
278 PSZ pModifiers = pSep + 1;
279 BOOL fReplaceModeFound = FALSE;
280 // now check for the replace mode;
281 // the default is 0 (CFGRPL_ADD)
282 fVerticalsAllowed = TRUE;
283
284 PSZ pszUnique;
285 if ((pszUnique = strhistr(pModifiers, "UNIQUE")))
286 {
287 _iReplaceMode = CFGRPL_UNIQUE;
288 fVerticalsAllowed = TRUE;
289 fReplaceModeFound = TRUE; // block future replacement modifiers
290
291 // check if we have a "UNIQUE(xxx)" syntax
292 PSZ psz;
293 if (psz = strhExtract(pszUnique, '(', ')', NULL))
294 {
295 // if found, this extracts the stuff between ( and );
296 // if not, this returns NULL
297 _ustrUniqueSearchString2.assignUtf8(psz);
298 free(psz);
299 }
300 }
301 if (strhistr(pModifiers, "ADDRIGHT"))
302 {
303 if (!fReplaceModeFound)
304 {
305 _iReplaceMode = CFGRPL_ADDRIGHT;
306 fVerticalsAllowed = TRUE;
307 fReplaceModeFound = TRUE; // block future replacement modifiers
308 }
309 else
310 {
311 // double replace mode found:
312 throw BSConfigExcpt(CFGEXCPT_DOUBLEREPLACEMODE,
313 ustrConfigSys);
314 }
315 }
316 if (strhistr(pModifiers, "ADDLEFT"))
317 {
318 if (!fReplaceModeFound)
319 {
320 _iReplaceMode = CFGRPL_ADDLEFT;
321 fVerticalsAllowed = TRUE;
322 fReplaceModeFound = TRUE; // block future replacement modifiers
323 }
324 else
325 // double replace mode found:
326 throw BSConfigExcpt(CFGEXCPT_DOUBLEREPLACEMODE,
327 ustrConfigSys);
328 }
329 if (strhistr(pModifiers, "REMOVELINE"))
330 {
331 if (!fReplaceModeFound)
332 {
333 _iReplaceMode = CFGRPL_REMOVELINE;
334 fVerticalsAllowed = FALSE;
335 fReplaceModeFound = TRUE; // block future replacement modifiers
336
337 // check also for stuff after "="
338 PSZ p;
339 if (p = strchr(_ustrNewLine.GetBuffer(), '='))
340 _ustrUniqueSearchString2.assignUtf8(p + 1);
341 }
342 else
343 // double replace mode found:
344 throw BSConfigExcpt(CFGEXCPT_DOUBLEREPLACEMODE,
345 ustrConfigSys);
346 }
347 if (strhistr(pModifiers, "REMOVEPART"))
348 {
349 if (!fReplaceModeFound)
350 {
351 _iReplaceMode = CFGRPL_REMOVEPART;
352 fVerticalsAllowed = FALSE;
353 fReplaceModeFound = TRUE;
354 }
355 else
356 // double replace mode found:
357 throw BSConfigExcpt(CFGEXCPT_DOUBLEREPLACEMODE,
358 ustrConfigSys);
359 }
360
361 // now parse vertical modifiers
362 BOOL fVerticalFound = FALSE;
363 if (strhistr(pModifiers, "ADDTOP"))
364 {
365 _iVertical = CFGVRT_TOP;
366 fVerticalFound = TRUE;
367 }
368 PSZ p2;
369 if ((p2 = strhistr(pModifiers, "ADDAFTER(")))
370 {
371 if (!fVerticalFound)
372 {
373 PSZ pEndOfSearch;
374 if (!(pEndOfSearch = strchr(p2, ')')))
375 {
376 ustring ustr;
377 ustr.assignUtf8(p2);
378 throw BSConfigExcpt(CFGEXCPT_INVALIDSEARCH, ustr);
379 }
380
381 _ustrVerticalSearchString.assignUtf8(
382 p2 + 9, // strlen("ADDAFTER(")
383 pEndOfSearch); // excluding that char
384
385 _iVertical = CFGVRT_AFTER;
386 fVerticalFound = TRUE;
387 }
388 else
389 {
390 ustring ustr;
391 ustr.assignUtf8(p2);
392 throw BSConfigExcpt(CFGEXCPT_INVALIDVERTICAL, ustr);
393 }
394 }
395 if ((p2 = strhistr(pModifiers, "ADDBEFORE(")))
396 {
397 if (!fVerticalFound)
398 {
399 PSZ pEndOfSearch;
400 if (!(pEndOfSearch = strchr(p2, ')')))
401 {
402 ustring ustr;
403 ustr.assignUtf8(p2);
404 throw BSConfigExcpt(CFGEXCPT_INVALIDSEARCH, ustr);
405 }
406
407 _ustrVerticalSearchString.assignUtf8(p2 + 10, // strlen("ADDBEFORE(")
408 pEndOfSearch); // excluding that char
409
410 _iVertical = CFGVRT_BEFORE;
411 fVerticalFound = TRUE;
412 }
413 else
414 {
415 ustring ustr;
416 ustr.assignUtf8(p2);
417 throw BSConfigExcpt(CFGEXCPT_INVALIDVERTICAL, ustr);
418 }
419 }
420
421 // finally check if vertical modifier is allowed at all
422 if ( (fVerticalFound) && (!fVerticalsAllowed) )
423 throw BSConfigExcpt(CFGEXCPT_INVALIDVERTICAL, ustrConfigSys);
424
425 } // end elseif (!pSep)
426}
427
428/*
429 *@@ DescribeType:
430 * describes the current manipulator type to the GUI.
431 *
432 *@@added V0.9.2 (2000-02-19) [umoeller]
433 */
434
435const char* BSCfgSysManip::DescribeType()
436{
437 return ("CONFIG.SYS manipulation");
438}
439
440/*
441 *@@ DescribeData:
442 * describes the current manipulator data to the GUI.
443 *
444 *@@added V0.9.2 (2000-02-19) [umoeller]
445 *@@changed V0.9.5 (2000-08-26) [umoeller]: UNIQUE wasn't reported right
446 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
447 */
448
449ustring BSCfgSysManip::DescribeData()
450{
451 ustring str;
452 BOOL fAddSpace = TRUE;
453
454 switch (_iReplaceMode)
455 {
456 case CFGRPL_UNIQUE:
457 str.appendUtf8("UNIQUE");
458 if (_ustrUniqueSearchString2())
459 {
460 str.appendUtf8("(");
461 str.append(_ustrUniqueSearchString2);
462 str.appendUtf8(")");
463 }
464 break;
465
466 case CFGRPL_ADDLEFT: str.appendUtf8("ADDLEFT"); break;
467 case CFGRPL_ADDRIGHT: str.appendUtf8("ADDRIGHT"); break;
468 case CFGRPL_REMOVELINE: str.appendUtf8("REMOVELINE"); break;
469 case CFGRPL_REMOVEPART: str.appendUtf8("REMOVEPART"); break;
470
471 default:
472 fAddSpace = FALSE;
473 break;
474 }
475
476 if (fAddSpace)
477 str.appendUtf8(" ");
478
479 switch (_iVertical)
480 {
481 case CFGVRT_TOP: str.appendUtf8("ADDTOP "); break;
482
483 case CFGVRT_BEFORE:
484 str.appendUtf8("BEFORE(");
485 str.append(_ustrVerticalSearchString);
486 str.appendUtf8(") ");
487 break;
488
489 case CFGVRT_AFTER:
490 str.appendUtf8("AFTER ");
491 str.append(_ustrVerticalSearchString);
492 str.appendUtf8(") ");
493 break;
494 }
495
496 str.append(_ustrNewLine);
497
498 return (str);
499}
500
501/*
502 *@@ AddToUndoList:
503 * static helper method to create BSCfgSysManip
504 * instances from the logger instance that was
505 * previously used with BSConfigSys::Manipulate.
506 *
507 * The new BSCfgSysManip objects are appended to
508 * the specified list and are exact opposites
509 * to the BSCfgSysManip objects that were stored
510 * in the logger. That is, if the logger registered
511 * that something was added to CONFIG.SYS, we
512 * create an object which removes that text again,
513 * and vice versa.
514 *
515 * Use this method to undo CONFIG.SYS changes
516 * and pass the objects on the list to BSConfigSys::Manipulate,
517 * since there is no corresponding "undo" class
518 * for BSConfigSys.
519 *
520 * See BSCfgSysManip::BSCfgSysManip for an example usage.
521 *
522 * This returns the number of items created.
523 *
524 * Throws:
525 * -- BSConfigExcpt.
526 */
527
528int BSCfgSysManip::AddToUndoList(list<BSConfigBase*> &List,
529 BSCfgSysDoneLogger &logger)
530{
531 // the logger in this case has a list of special PSZs,
532 // where the first three characters of each entry
533 // signify to us what was changed in CONFIG.SYS
534
535 PSZ pszLogStart = logger._pabLogString,
536 pszLogThis = pszLogStart;
537 int iCount = 0;
538
539 while (pszLogThis < pszLogStart + logger._cbLogString)
540 {
541 ULONG cbLogThis = strlen(pszLogThis);
542 string strNewModifiers;
543
544 // now check what we have in the log; the
545 // first three characters (followed by a
546 // space) signify what happened:
547 // -- "DLL": deleted line
548 // -- "DLP": deleted part of line
549 // -- "NWL": added an all new line
550 // -- "NWP": added a new part to an existing line
551
552 if (memicmp(pszLogThis, "DLL ", 4) == 0)
553 {
554 // line was deleted: re-insert that line (UNIQUE mode)
555 // strNewModifiers = "UNIQUE";
556 }
557 else if (memicmp(pszLogThis, "DLP ", 4) == 0)
558 {
559 // part of line was deleted: re-insert that line (ADDRIGHT mode)
560 strNewModifiers = "ADDRIGHT";
561 }
562 else if (memicmp(pszLogThis, "NWL ", 4) == 0)
563 {
564 // line was added: remove that whole line
565 strNewModifiers = "REMOVELINE";
566 // #### no, no, no!!! this removes an entire line...
567 }
568 else if (memicmp(pszLogThis, "NWP ", 4) == 0)
569 // part of line was added: remove that part
570 strNewModifiers = "REMOVEPART";
571 else
572 {
573 // none of the above: error
574 ustring ustr;
575 ustr.assignUtf8(pszLogThis);
576 throw BSConfigExcpt(CFGEXCPT_PARSELOG, ustr);
577 }
578
579 // something found: compose attribute string for manipulator
580 PSZ pszNewAttrs = (PSZ)malloc(strlen(pszLogThis)
581 + strNewModifiers.size()
582 + 30);
583 sprintf(pszNewAttrs, "%s | %s",
584 pszLogThis + 4, // stuff after "DLL " or whatever
585 strNewModifiers.c_str());
586
587 // add the undo manipulator to the _front_ of the
588 // list so that items are undone in reverse order
589 // (because if a line was replaced originally,
590 // we first have a "delete line" and then an
591 // "add line" entry in the log)
592 ustring ustr;
593 ustr.assignUtf8(pszNewAttrs);
594 List.push_front(new BSCfgSysManip(ustr));
595 free(pszNewAttrs);
596
597 pszLogThis += cbLogThis + 1; // go beyond null byte
598 iCount++;
599 }
600
601 return (iCount);
602}
603
604/* ******************************************************************
605 *
606 * BSConfigSys class
607 *
608 ********************************************************************/
609
610/*
611 *@@ BSConfigSys:
612 * the constructor, which loads the current CONFIG.SYS
613 * file from OS/2 boot drive into instance memory.
614 *
615 * Throws:
616 * -- BSConfigExcpt.
617 *
618 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using csysLoadConfigSys
619 */
620
621BSConfigSys::BSConfigSys() : BSRoot(tBSConfigSys)
622{
623 _pszContent = NULL;
624
625 /* sprintf(_szFilename, "%c:\\config.sys", doshQueryBootDrive());
626 // now read CONFIG.SYS file to initialize the dlg items
627 APIRET arc = doshLoadTextFile(_szFilename, &_pszContent); */
628
629 APIRET arc = csysLoadConfigSys(NULL, // default CONFIG.SYS
630 &_pszContent);
631
632 if (arc != NO_ERROR)
633 throw BSConfigExcpt(CFGEXCPT_OPEN, arc);
634
635 _fDirty = FALSE;
636}
637
638/*
639 *@@ ~ConfigSys:
640 * the destructor.
641 *
642 * This does _not_ write the file. Use BSConfigSys::Flush()
643 * before deleting an instance of this.
644 */
645
646BSConfigSys::~BSConfigSys()
647{
648 if (_pszContent)
649 free(_pszContent);
650}
651
652/*
653 *@@ Manipulate:
654 * this monster method changes the data in our memory copy of
655 * CONFIG.SYS according to the BSCfgSysManip object, which you must
656 * have created before. See BSCfgSysManip::BSCfgSysManip for a
657 * description of this usage.
658 *
659 * This also takes a BSCfgSysDoneLogger as input where all the
660 * changes made to the CONFIG.SYS memory copy are logged. This
661 * logger can then be passed to BSConfigSys::Undo to have the
662 * changes undone again.
663 *
664 * Call BSConfigSys::Flush to have CONFIG.SYS written back to disk.
665 *
666 * Returns NO_ERROR if everything was OK or an error code.
667 *
668 *@@changed V0.9.18 (2002-03-08) [umoeller]: adjusted for Unicode; added codec
669 */
670
671int BSConfigSys::Manipulate(BSUniCodec &codecProcess, // in: codec for process codepage
672 BSCfgSysManip &Manip, // in: manipulator object
673 BSCfgSysDoneLogger *pLogger, // in: logger object to append items to (can be NULL)
674 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
675{
676 int irc = 0;
677
678 XSTRING strLogger;
679 xstrInit(&strLogger, 0);
680
681 CONFIGMANIP cm;
682 memset(&cm, 0, sizeof(cm));
683
684 cm.iReplaceMode = Manip._iReplaceMode;
685
686 string strUniqueSearchString2(&codecProcess, Manip._ustrUniqueSearchString2);
687 cm.pszUniqueSearchString2 = strUniqueSearchString2.c_str();
688
689 cm.iVertical = Manip._iVertical;
690
691 string strVerticalSearchString(&codecProcess, Manip._ustrVerticalSearchString);
692 cm.pszVerticalSearchString = strVerticalSearchString.c_str();
693
694 string strNewLine(&codecProcess, Manip._ustrNewLine);
695 cm.pszNewLine = strNewLine.c_str();
696
697 irc = csysManipulate(&_pszContent,
698 &cm,
699 &_fDirty,
700 &strLogger);
701
702 // now parse the logger...
703 if (strLogger.ulLength)
704 {
705 // something to be logged:
706 PSZ p = strLogger.psz;
707
708 while (p)
709 {
710 PSZ pEOL;
711 if ( (pEOL = strhFindEOL(p, NULL))
712 && (*pEOL)
713 )
714 {
715 string strTemp(p, pEOL);
716
717 if (strTemp())
718 {
719 if (pLogFile)
720 // write to log file
721 pLogFile->Write("Updated CONFIG.SYS: \"%s\"",
722 strTemp.c_str());
723
724 if (pLogger)
725 {
726 // write to "done" logger:
727 ustring ustr(&codecProcess, strTemp);
728 pLogger->Append(ustr);
729 }
730 }
731 else
732 break;
733
734 p = pEOL + 1;
735 }
736 else
737 break;
738 }
739 }
740
741 xstrClear(&strLogger);
742
743 return (irc);
744}
745
746/*
747 *@@ Flush:
748 * this rewrites the CONFIG.SYS file on disk with the
749 * data we have in memory.
750 *
751 * This makes a backup copy in "CONFIG.003" style if
752 * (pszBackup != NULL). See doshCreateBackupFileName
753 * for details.
754 *
755 * Returns:
756 *
757 * -- 0: contents were dirty, written back to disk.
758 *
759 * -- 1: no error, but contents were clean, so no write-back
760 * was needed.
761 *
762 * Throws:
763 * -- BSConfigExcpt(CFGEXCPT_WRITE, APIRET).
764 *
765 *@@changed V0.9.3 (2000-05-03) [umoeller]: added more error checking
766 *@@changed V0.9.3 (2000-05-12) [umoeller]: added pszBackup
767 *@@changed V0.9.6 (2000-10-27) [umoeller]: added check if contents are dirty
768 */
769
770int BSConfigSys::Flush(string *pstrBackup, // in/out: create backup?
771 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
772 const
773{
774 int irc = 0;
775
776 if (_fDirty)
777 {
778 PSZ pszBackup = NULL;
779 CHAR szBackup[CCHMAXPATH];
780 if (pstrBackup)
781 pszBackup = szBackup;
782
783 APIRET arc = csysWriteConfigSys(NULL, // default _szFilename,
784 _pszContent,
785 pszBackup); // create backup
786
787 if (arc)
788 {
789 if (pLogFile)
790 pLogFile->Write("Error %d occurred writing CONFIG.SYS back to disk",
791 arc);
792
793 throw BSConfigExcpt(CFGEXCPT_WRITE, arc);
794 }
795
796 if (pLogFile)
797 pLogFile->Write("CONFIG.SYS file written back to disk, backup is \"%s\"",
798 pszBackup);
799
800 if (pstrBackup)
801 pstrBackup->assign(szBackup);
802 }
803 else
804 irc = 1;
805
806 return (irc);
807}
808
809/* ******************************************************************
810 *
811 * BSRegisterClass class
812 *
813 ********************************************************************/
814
815/*
816 *@@ BSRegisterClass:
817 * this constructor translates a REGISTERCLASS attribute (as used
818 * in the PCK tag and stored in the database) into the BSRegisterClass
819 * instance data.
820 *
821 * Syntax:
822 *
823 + REGISTERCLASS="classname|dllpath"
824 *
825 * Throws:
826 * -- BSConfigExcpt.
827 */
828
829BSRegisterClass::BSRegisterClass(const ustring &ustrRegisterClass)
830 : BSConfigBase(CFGT_REGISTERCLASS, tBSRegisterClass)
831{
832 // find separator
833 PCSZ pcszRegisterClass = ustrRegisterClass.GetBuffer();
834 PCSZ pBegin;
835 if (!(pBegin = pcszRegisterClass))
836 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrRegisterClass);
837
838 PCSZ pEnd;
839 if (!(pEnd = strchr(pBegin, '|')))
840 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrRegisterClass);
841
842 // strip trailing spaces
843 PCSZ pEnd2 = pEnd;
844 while ((*pEnd2 == ' ') && (pEnd2 > pcszRegisterClass))
845 pEnd2--;
846
847 _ustrClassName.assignUtf8(pBegin, pEnd2);
848 if (!_ustrClassName())
849 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrRegisterClass);
850
851 pBegin = pEnd + 1;
852 // strip leading spaces
853 while ((*pBegin) && (*pBegin == ' '))
854 pBegin++;
855 _ustrModule.assignUtf8(pBegin);
856 if (!_ustrModule())
857 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrRegisterClass);
858}
859
860/*
861 *@@ DescribeType:
862 * describes the current manipulator type to the GUI.
863 *
864 *@@added V0.9.9 (2001-03-27) [umoeller]
865 */
866
867const char* BSRegisterClass::DescribeType()
868{
869 return ("WPS class registration");
870}
871
872/*
873 *@@ DescribeData:
874 * describes the current manipulator data to the GUI.
875 *
876 *@@added V0.9.9 (2001-03-27) [umoeller]
877 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
878 */
879
880ustring BSRegisterClass::DescribeData()
881{
882 ustring str = _ustrClassName;
883 str.appendUtf8(" in ");
884 str += _ustrModule;
885
886 return (str);
887}
888
889/*
890 *@@ IsRegistered:
891 * this returns TRUE if a class of the same WPS class name
892 * as this instance is already registered with the WPS.
893 * In that case, the module of the registered class is
894 * copied into strModule.
895 *
896 * This throws a BSConfigExcpt with REGEXCPT_QUERYCLASSLIST
897 * if the class list could not be queried.
898 *
899 *@@changed V0.9.18 (2002-03-08) [umoeller]: now using string for buffer, added codec
900 */
901
902bool BSRegisterClass::IsRegistered(BSUniCodec &codecProcess, // in: codec for process codepage
903 ustring &ustrModule)
904 const
905{
906 BOOL fInstalled = FALSE;
907 PBYTE pClassList,
908 pClassThis = 0;
909 if (pClassList = winhQueryWPSClassList())
910 {
911 string strClassName(&codecProcess, _ustrClassName);
912 if ((pClassThis = winhQueryWPSClass(pClassList, strClassName.c_str())))
913 {
914 fInstalled = TRUE;
915 ustrModule.assignCP(&codecProcess, ((POBJCLASS)pClassThis)->pszModName);
916 }
917
918 free(pClassList);
919 }
920 else
921 throw BSConfigExcpt(REGEXCPT_QUERYCLASSLIST, 0);
922
923 return (fInstalled);
924}
925
926/*
927 *@@ Register:
928 * this attempts to register the class.
929 *
930 * If (fReplace == TRUE), we do not call
931 * BSRegisterClass::IsRegistered before registering
932 * this, i.e. we will always register the class,
933 * even if it's already registered.
934 *
935 * If (fReplace == FALSE) and BSRegisterClass::IsRegistered
936 * returned TRUE, we throw a BSConfigExcpt with
937 * REGEXCPT_ALREADYREGISTERED and pszSubstr set to the
938 * file name of the registered DLL. Note that IsRegistered might
939 * in turn throw BSConfigExcpt with REGEXCPT_QUERYCLASSLIST.
940 *
941 * If registering the class failed, this throws a
942 * BSConfigExcpt with REGEXCPT_REGISTER and iData set
943 * to the APIRET of winhRegisterClass.
944 *
945 * This method can take a BSDoneLoggerBase object as input, which
946 * can later be used with the BSDeregisterClass::AddToUndoList
947 * static class method to easily create a list of BSDeregisterClass
948 * objects to undo the changes made.
949 *
950 * Example usage (exception handling omitted):
951 + BSRegisterDoneLogger logger;
952 + // create logger instance
953 + BSRegisterClass RegClass("XFolder|C:\XFOLDER\XFLDR.DLL");
954 + // create BSRegisterClass instance:
955 + RegClass.Register(TRUE, logger);
956 + ... // register more classes with the same logger
957 +
958 + // now create undo list
959 + list<BSDeregisterClass*> List;
960 + BSDeregisterClass::AddToUndoList(List, logger);
961 + list<BSDeregisterClass*>::iterator DeregStart = List.begin(),
962 + DeregEnd = List.end();
963 + for (; DeregStart != DeregEnd; DeregStart++)
964 + (**DeregStart).Deregister;
965 *
966 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
967 *@@changed V0.9.18 (2002-03-08) [umoeller]: added missing log file entry
968 */
969
970int BSRegisterClass::Register(BSUniCodec &codecProcess, // in: codec for process codepage
971 bool fReplace,
972 BSRegisterDoneLogger *pLogger, // in: logger object to append items to (can be NULL)
973 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
974 const
975{
976 int irc = 0;
977
978 if (fReplace == FALSE)
979 {
980 ustring ustrModule;
981 if (IsRegistered(codecProcess, ustrModule))
982 throw BSConfigExcpt(REGEXCPT_ALREADYREGISTERED, ustrModule);
983 }
984
985 CONVERT(&codecProcess, ClassName);
986 CONVERT(&codecProcess, Module);
987
988 CHAR szBuf[1000];
989 APIRET arc = winhRegisterClass(pcszClassName,
990 pcszModule,
991 szBuf,
992 sizeof(szBuf));
993 // always record in logger, even if failed,
994 // because the class is in the class list anyway
995 if (pLogger)
996 pLogger->Append(_ustrClassName);
997
998 if (arc != NO_ERROR)
999 {
1000 // this was missing V0.9.18 (2002-03-08) [umoeller]
1001 if (pLogFile)
1002 pLogFile->Write("Error %d registering WPS class \"%s\"",
1003 arc,
1004 pcszClassName);
1005
1006 throw BSConfigExcpt(REGEXCPT_REGISTER, arc);
1007 }
1008
1009 // this was missing V0.9.18 (2002-03-08) [umoeller]
1010 if (pLogFile)
1011 pLogFile->Write("Registered WPS class \"%s\"",
1012 pcszClassName);
1013
1014 return (irc);
1015}
1016
1017/* ******************************************************************
1018 *
1019 * BSDeregisterClass class
1020 *
1021 ********************************************************************/
1022
1023/*
1024 *@@ BSDeregisterClass:
1025 * the constructor, which in this case takes a simple
1026 * PSZ class name as input.
1027 */
1028
1029BSDeregisterClass::BSDeregisterClass(const ustring &ustrDeregisterClass)
1030 : BSConfigBase(CFGT_DEREGISTERCLASS, tBSDeregisterClass)
1031{
1032 _ustrClassName = ustrDeregisterClass;
1033}
1034
1035/*
1036 *@@ DescribeType:
1037 * describes the current manipulator type to the GUI.
1038 *
1039 *@@added V0.9.2 (2000-02-19) [umoeller]
1040 */
1041
1042const char* BSDeregisterClass::DescribeType()
1043{
1044 return ("WPS class deregistration");
1045}
1046
1047/*
1048 *@@ DescribeData:
1049 * describes the current manipulator data to the GUI.
1050 *
1051 *@@added V0.9.2 (2000-02-19) [umoeller]
1052 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
1053 */
1054
1055ustring BSDeregisterClass::DescribeData()
1056{
1057 return (_ustrClassName);
1058}
1059
1060/*
1061 *@@ Deregister:
1062 * this attempts to deregister the class.
1063 *
1064 * Throws:
1065 * -- BSConfigExcpt.
1066 *
1067 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
1068 */
1069
1070int BSDeregisterClass::Deregister(BSUniCodec &codecProcess, // in: codec for process codepage
1071 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
1072 const
1073{
1074 int irc = 0;
1075
1076 CONVERT(&codecProcess, ClassName); // creates local string strXXX, PCSZ pcszXXX
1077
1078 if (!WinDeregisterObjectClass(pcszClassName))
1079 {
1080 if (pLogFile)
1081 pLogFile->Write("An error occurred deregistering WPS class \"%s\"",
1082 pcszClassName);
1083
1084 throw BSConfigExcpt(REGEXCPT_DEREGISTER, _ustrClassName);
1085 }
1086
1087 if (pLogFile)
1088 pLogFile->Write("Deregistered WPS class \"%s\"",
1089 pcszClassName);
1090
1091 return (irc);
1092}
1093
1094/*
1095 *@@ AddToUndoList:
1096 * static helper method to create BSDeregisterClass
1097 * instances from the logger instance that was
1098 * previously used with BSRegisterClass::Register.
1099 *
1100 * The new BSDeregisterClass objects are appended to
1101 * the specified list.
1102 *
1103 * See BSRegisterClass::Register for an example usage.
1104 *
1105 * This returns the number of items created.
1106 */
1107
1108int BSDeregisterClass::AddToUndoList(list<BSConfigBase*> &List,
1109 BSRegisterDoneLogger &logger)
1110{
1111 // the logger in this case has a simple list of PSZs with all
1112 // the class names that were registered, each of which is terminated
1113 // with a null character
1114
1115 PSZ pszLogStart = logger._pabLogString,
1116 pszLogThis = pszLogStart;
1117 int iCount = 0;
1118
1119 while (pszLogThis < pszLogStart + logger._cbLogString)
1120 {
1121 ULONG cbLogThis = strlen(pszLogThis);
1122 ustring ustr;
1123 ustr.assignUtf8(pszLogThis);
1124 List.push_back(new BSDeregisterClass(ustr));
1125 pszLogThis += cbLogThis + 1; // go beyond null byte
1126 iCount++;
1127 }
1128
1129 return (iCount);
1130}
1131
1132/* ******************************************************************
1133 *
1134 * BSReplaceClass class
1135 *
1136 ********************************************************************/
1137
1138/*
1139 *@@ BSReplaceClassBase:
1140 * this constructor translates a REPLACECLASS attribute (as used
1141 * in the PCK tag and stored in the database) into the BSReplaceClass
1142 * instance data.
1143 *
1144 * Syntax:
1145 *
1146 + REPLACECLASS="oldclassname|newclassname"
1147 *
1148 *@@changed V0.9.19 (2002-05-07) [umoeller]: changed inheritance hierarchy here
1149 */
1150
1151BSReplaceClassBase::BSReplaceClassBase(const ustring &ustrReplaceClass,
1152 ULONG cfgt,
1153 BSClassID &Class)
1154 : BSConfigBase(cfgt, Class)
1155{
1156 PCSZ pcszReplaceClass = ustrReplaceClass.GetBuffer();
1157
1158 // find separator
1159 PCSZ pBegin;
1160 if (!(pBegin = pcszReplaceClass))
1161 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrReplaceClass);
1162
1163 PCSZ pEnd;
1164 if (!(pEnd = strchr(pBegin, '|')))
1165 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrReplaceClass);
1166
1167 // strip trailing spaces
1168 PCSZ pEnd2 = pEnd;
1169 while ((*pEnd2 == ' ') && (pEnd2 > pcszReplaceClass))
1170 pEnd2--;
1171
1172 _ustrOldClassName.assignUtf8(pBegin, pEnd2);
1173 if (!_ustrOldClassName())
1174 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrReplaceClass);
1175
1176 pBegin = pEnd + 1;
1177 // strip leading spaces
1178 while ((*pBegin) && (*pBegin == ' '))
1179 pBegin++;
1180 _ustrNewClassName.assignUtf8(pBegin);
1181 if (!_ustrNewClassName())
1182 throw BSConfigExcpt(REGEXCPT_SYNTAX, ustrReplaceClass);
1183}
1184
1185/*
1186 *@@ DescribeType:
1187 * describes the current manipulator type to the GUI.
1188 *
1189 *@@added V0.9.2 (2000-02-19) [umoeller]
1190 */
1191
1192const char* BSReplaceClass::DescribeType()
1193{
1194 return ("WPS class replacement");
1195}
1196
1197/*
1198 *@@ DescribeData:
1199 * describes the current manipulator data to the GUI.
1200 *
1201 *@@added V0.9.2 (2000-02-19) [umoeller]
1202 *@@changed V0.9.5 (2000-08-26) [umoeller]: UNIQUE wasn't reported right
1203 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
1204 */
1205
1206ustring BSReplaceClass::DescribeData()
1207{
1208 ustring str = _ustrOldClassName;
1209 str.appendUtf8(" with ");
1210 str += _ustrNewClassName;
1211
1212 return (str);
1213}
1214
1215/*
1216 *@@ Replace:
1217 * this attempts to replace the class or undo
1218 * an existing replacement (if fReplace == FALSE).
1219 *
1220 * If replacing the class failed, this throws a
1221 * BSConfigExcpt with REGEXCPT_REPLACE.
1222 *
1223 * Example usage (exception handling omitted):
1224 + BSReplaceDoneLogger logger;
1225 + // create logger instance
1226 + BSReplaceClass ReplClass("WPFolder;XFolder");
1227 + // create BSReplaceClass instance:
1228 + ReplClass.Replace(TRUE, logger);
1229 + ... // replace more classes with the same logger
1230 +
1231 + // now create undo list
1232 + list<BSUnreplaceClass*> List;
1233 + BSUnreplaceClass::AddToUndoList(List, logger);
1234 + list<BSUnreplaceClass*>::iterator UnrplStart = List.begin(),
1235 + UnrplEnd = List.end();
1236 + for (; UnrplStart != UnrplEnd; UnrplStart++)
1237 + (**UnrplStart).Unreplace;
1238 *
1239 *@@changed V0.9.1 (2000-01-05) [umoeller]: finally added logging, which was missing
1240 *@@changed V0.9.9 (2001-03-30) [umoeller]: fixed wrong logging if replace failed
1241 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
1242 */
1243
1244int BSReplaceClass::Replace(BSUniCodec &codecProcess, // in: codec for process codepage
1245 BSReplaceDoneLogger *pLogger, // in: logger object to append items to (can be NULL)
1246 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
1247 const
1248{
1249 int irc = 0;
1250
1251 CONVERT(&codecProcess, OldClassName);
1252 CONVERT(&codecProcess, NewClassName);
1253
1254 BOOL brc = WinReplaceObjectClass(pcszOldClassName,
1255 pcszNewClassName,
1256 TRUE); // replace
1257 // always record in logger, even if failed,
1258 // because the class is in the class list anyway
1259 // V0.9.9 (2001-03-30) [umoeller]
1260 if (pLogger)
1261 {
1262 ustring ustr2Append(_ustrOldClassName);
1263 ustr2Append.appendUtf8("|");
1264 ustr2Append += _ustrNewClassName;
1265 pLogger->Append(ustr2Append);
1266 }
1267
1268
1269 if (!brc)
1270 {
1271 if (pLogFile)
1272 pLogFile->Write("An error occurred replacing WPS class \"%s\" with \"%s\"",
1273 pcszOldClassName,
1274 pcszNewClassName);
1275
1276 throw BSConfigExcpt(REGEXCPT_REPLACE, 0);
1277 }
1278
1279 // success:
1280 if (pLogFile)
1281 pLogFile->Write("Replaced WPS class \"%s\" with \"%s\"",
1282 pcszOldClassName,
1283 pcszNewClassName);
1284
1285 return (irc);
1286}
1287
1288/* ******************************************************************
1289 *
1290 * BSUnreplaceClass class
1291 *
1292 ********************************************************************/
1293
1294/*
1295 *@@ DescribeType:
1296 * describes the current manipulator type to the GUI.
1297 *
1298 *@@added V0.9.2 (2000-02-19) [umoeller]
1299 */
1300
1301const char* BSUnreplaceClass::DescribeType()
1302{
1303 return ("WPS class un-replacement");
1304}
1305
1306/*
1307 *@@ DescribeData:
1308 * describes the current manipulator data to the GUI.
1309 *
1310 *@@added V0.9.2 (2000-02-19) [umoeller]
1311 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
1312 */
1313
1314ustring BSUnreplaceClass::DescribeData()
1315{
1316 ustring ustr = _ustrOldClassName;
1317 ustr.appendUtf8(" with ");
1318 ustr += _ustrNewClassName;
1319
1320 return (ustr);
1321}
1322
1323/*
1324 *@@ Unreplace:
1325 * this attempts to unreplace the class.
1326 * Throws a BSConfigExcpt upon failure.
1327 *
1328 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
1329 */
1330
1331int BSUnreplaceClass::Unreplace(BSUniCodec &codecProcess, // in: codec for process codepage,
1332 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
1333 const
1334{
1335 int irc = 0;
1336
1337 CONVERT(&codecProcess, OldClassName);
1338 CONVERT(&codecProcess, NewClassName);
1339
1340 if (!WinReplaceObjectClass(pcszOldClassName,
1341 pcszNewClassName,
1342 FALSE))
1343 {
1344 if (pLogFile)
1345 pLogFile->Write("Error undoing WPS class replacement of \"%s\" with \"%s\"",
1346 pcszOldClassName,
1347 pcszNewClassName);
1348
1349 throw BSConfigExcpt(REGEXCPT_UNREPLACE, 0);
1350 }
1351
1352 // success:
1353 if (pLogFile)
1354 pLogFile->Write("Undid WPS class replacement of \"%s\" with \"%s\"",
1355 pcszOldClassName,
1356 pcszNewClassName);
1357
1358 return (irc);
1359}
1360
1361/*
1362 *@@ AddToUndoList:
1363 * static helper method to create BSUnreplaceClass
1364 * instances from the logger instance that was
1365 * previously used with BSRegisterClass::Register.
1366 *
1367 * The new BSUnreplaceClass objects are appended to
1368 * the specified list.
1369 *
1370 * See BSReplaceClass::Replace for an example usage.
1371 *
1372 * This returns the number of items created.
1373 */
1374
1375int BSUnreplaceClass::AddToUndoList(list<BSConfigBase*> &List,
1376 BSReplaceDoneLogger &logger)
1377{
1378 // the logger in this case has a simple list of PSZs with all
1379 // the class names that were registered, each of which is terminated
1380 // with a null character
1381
1382 PSZ pszLogStart = logger._pabLogString,
1383 pszLogThis = pszLogStart;
1384 int iCount = 0;
1385
1386 while (pszLogThis < pszLogStart + logger._cbLogString)
1387 {
1388 ULONG cbLogThis = strlen(pszLogThis);
1389 ustring ustr;
1390 ustr.assignUtf8(pszLogThis);
1391 List.push_back(new BSUnreplaceClass(ustr));
1392 // this calls the inherited BSReplaceClass
1393 // constructor, which will parse the thing
1394 pszLogThis += cbLogThis + 1; // go beyond null byte
1395 iCount++;
1396 }
1397
1398 return (iCount);
1399}
1400
1401/* ******************************************************************
1402 *
1403 * BSCreateWPSObject class
1404 *
1405 ********************************************************************/
1406
1407/*
1408 *@@ BSCreateWPSObject:
1409 * this constructor translates a CREATEOBJECT attribute (as used
1410 * in the PCK tag and stored in the database) into the BSCreateWPSObject
1411 * instance data.
1412 *
1413 * Syntax:
1414 *
1415 + CREATEOBJECT="[REPLACE] classname|title|location[|config]]"
1416 *
1417 * Throws:
1418 * -- BSConfigExcpt.
1419 *
1420 *@@changed V0.9.3 (2000-04-08) [umoeller]: "REPLACE" wasn't evaluated; fixed. Thanks, Cornelis Bockemhl.
1421 *@@changed V0.9.15 (2001-08-26) [umoeller]: added WPOEXCPT_INVALIDLOCATION checks
1422 *@@changed V0.9.19 (2002-04-14) [umoeller]: REPLACE was never detected, fixed
1423 */
1424
1425BSCreateWPSObject::BSCreateWPSObject(const ustring &ustrCreateObject)
1426 : BSConfigBase(CFGT_CREATEOBJECT, tBSCreateWPSObject)
1427{
1428 PCSZ pcszCreateObject = ustrCreateObject.GetBuffer();
1429
1430 // extract class name
1431 PCSZ pBegin;
1432 if (!(pBegin = pcszCreateObject))
1433 throw BSConfigExcpt(WPOEXCPT_NOCLASS, ustrCreateObject);
1434
1435 // if (!strcmp(pBegin, "REPLACE ")) BUZZZZ WRONG V0.9.19 (2002-04-14) [umoeller]
1436 if (!memcmp(pBegin, "REPLACE ", 8))
1437 {
1438 // added V0.9.3 (2000-04-08) [umoeller]
1439 _fReplace = TRUE;
1440 pBegin += 7;
1441 while (*pBegin == ' ')
1442 pBegin++;
1443 }
1444 else
1445 _fReplace = FALSE;
1446
1447 PSZ pEnd;
1448 if (pEnd = strchr(pBegin, '|'))
1449 {
1450 _ustrClassName.assignUtf8(pBegin, pEnd);
1451
1452 // extract title
1453 pBegin = pEnd + 1;
1454 if (pEnd = strchr(pBegin, '|'))
1455 {
1456 _ustrTitle.assignUtf8(pBegin, pEnd);
1457
1458 // extract location
1459 pBegin = pEnd + 1;
1460 if (pEnd = strchr(pBegin, '|'))
1461 {
1462 // yet another separator found: we then have a config parameter
1463 _ustrLocation.assignUtf8(pBegin, pEnd);
1464
1465 // extract config (rest of pszCreateObject)
1466 pBegin = pEnd + 1;
1467 _ustrSetupString.assignUtf8(pBegin);
1468 }
1469 else
1470 // no separator after "location" found:
1471 // duplicate whole string
1472 _ustrLocation.assignUtf8(pBegin);
1473
1474 if (!_ustrLocation())
1475 throw BSConfigExcpt(WPOEXCPT_NOLOCATION, ustrCreateObject);
1476
1477 // check if the location is correctly in <> brackets
1478 // V0.9.15 (2001-08-26) [umoeller]
1479 PCSZ pcszLocation = _ustrLocation.GetBuffer();
1480 if ( (pcszLocation[0] == '<')
1481 && (pcszLocation[_ustrLocation.length() - 1] != '>')
1482 )
1483 throw BSConfigExcpt(WPOEXCPT_INVALIDLOCATIONSTRING, ustrCreateObject);
1484 }
1485 else
1486 throw BSConfigExcpt(WPOEXCPT_NOTITLE, ustrCreateObject);
1487 }
1488 else
1489 throw BSConfigExcpt(WPOEXCPT_NOCLASS, ustrCreateObject);
1490}
1491
1492/*
1493 *@@ DescribeType:
1494 * describes the current manipulator type to the GUI.
1495 *
1496 *@@added V0.9.9 (2001-03-27) [umoeller]
1497 */
1498
1499const char* BSCreateWPSObject::DescribeType()
1500{
1501 return ("WPS object");
1502}
1503
1504/*
1505 *@@ DescribeData:
1506 * describes the current manipulator data to the GUI.
1507 *
1508 *@@added V0.9.9 (2001-03-27) [umoeller]
1509 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
1510 */
1511
1512ustring BSCreateWPSObject::DescribeData()
1513{
1514 ustring ustr = _ustrClassName;
1515 ustr.appendUtf8(" ");
1516 ustr += _ustrTitle;
1517 ustr.appendUtf8(" ");
1518 ustr += _ustrLocation;
1519 ustr.appendUtf8(" ");
1520 ustr += _ustrSetupString;
1521
1522 return (ustr);
1523}
1524
1525/*
1526 *@@ CreateObject:
1527 * this attempts to create the WPS object
1528 * with the instance data.
1529 *
1530 * Throws a BSConfigExcpt with WPOEXCPT_CREATE
1531 * if this fails.
1532 *
1533 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
1534 */
1535
1536void BSCreateWPSObject::CreateObject(BSUniCodec &codecProcess, // in: codec for process codepage,
1537 BSWPSObjectsDoneLogger *pLogger, // in: logger object to append items to (can be NULL)
1538 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
1539{
1540 CONVERT(&codecProcess, ClassName); // creates local string strXXX, PCSZ pcszXXX
1541 CONVERT(&codecProcess, Title); // creates local string strXXX, PCSZ pcszXXX
1542 CONVERT(&codecProcess, SetupString); // creates local string strXXX, PCSZ pcszXXX
1543 CONVERT(&codecProcess, Location); // creates local string strXXX, PCSZ pcszXXX
1544
1545 // check if the target folder exists, this is the most common problem
1546 // if WinCreateObject fails
1547 // V0.9.19 (2002-06-15) [umoeller]
1548
1549 if (!WinQueryObject(pcszLocation))
1550 {
1551 if (pLogFile)
1552 pLogFile->Write("Error creating WPS object \"%s\", class \"%s\", location \"%s\", setup \"%s\": Invalid target folder",
1553 pcszTitle,
1554 pcszClassName,
1555 pcszLocation,
1556 pcszSetupString);
1557
1558 throw BSConfigExcpt(WPOEXCPT_CREATE_BADLOCATION, _ustrTitle);
1559 }
1560
1561 if (!(_hobj = WinCreateObject(pcszClassName,
1562 pcszTitle,
1563 pcszSetupString,
1564 pcszLocation,
1565 (_fReplace)
1566 ? CO_REPLACEIFEXISTS
1567 : CO_UPDATEIFEXISTS)))
1568 {
1569 if (pLogFile)
1570 pLogFile->Write("Error creating WPS object \"%s\", class \"%s\", location \"%s\", setup \"%s\"",
1571 pcszTitle,
1572 pcszClassName,
1573 pcszLocation,
1574 pcszSetupString);
1575
1576 throw BSConfigExcpt(WPOEXCPT_CREATE, _ustrTitle);
1577 }
1578
1579 // V1.0.5 (2005-02-17) [pr]
1580 // We now save the object synchronously to prevent objects getting lost if we
1581 // restart the WPS imminently due to a class registration for example. @@fixes 634.
1582 WinSaveObject (_hobj, FALSE);
1583 if (pLogFile)
1584 {
1585 pLogFile->Write("Created WPS object \"%s\", class \"%s\", location \"%s\", setup \"%s\"",
1586 pcszTitle,
1587 pcszClassName,
1588 pcszLocation,
1589 pcszSetupString);
1590 pLogFile->Write("HOBJECT is 0x%lX", _hobj);
1591 }
1592
1593 // store the object ID in the logger
1594 PCSZ pObjectID;
1595 if ( // do we have a setup string at all?
1596 (pObjectID = _ustrSetupString.GetBuffer())
1597 // does it contain an object ID?
1598 && (pObjectID = strhistr(pObjectID,
1599 "OBJECTID=<"))
1600 )
1601 {
1602 PCSZ pBegin = pObjectID + 9; // points to '<' now
1603 PCSZ pEnd;
1604 if (pEnd = strchr(pBegin, '>'))
1605 {
1606 ustring ustrObjectID;
1607 ustrObjectID.assignUtf8(pBegin, pEnd + 1);
1608 if (pLogger)
1609 pLogger->Append(ustrObjectID);
1610 }
1611 }
1612}
1613
1614/* ******************************************************************
1615 *
1616 * BSDeleteWPSObject class
1617 *
1618 ********************************************************************/
1619
1620/*
1621 *@@ BSDeleteWPSObject:
1622 * the constructor, which in this case takes a simple
1623 * PSZ object ID as input.
1624 */
1625
1626BSDeleteWPSObject::BSDeleteWPSObject(const ustring &ustrID2Delete)
1627 : BSConfigBase(CFGT_DELETEOBJECT, tBSDeleteWPSObject)
1628{
1629 _ustrObjectID = ustrID2Delete;
1630}
1631
1632/*
1633 *@@ DescribeType:
1634 * describes the current manipulator type to the GUI.
1635 *
1636 *@@added V0.9.2 (2000-02-19) [umoeller]
1637 */
1638
1639const char* BSDeleteWPSObject::DescribeType()
1640{
1641 return ("WPS object deletion");
1642}
1643
1644/*
1645 *@@ DescribeData:
1646 * describes the current manipulator data to the GUI.
1647 *
1648 *@@added V0.9.2 (2000-02-19) [umoeller]
1649 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
1650 */
1651
1652ustring BSDeleteWPSObject::DescribeData()
1653{
1654 return (_ustrObjectID);
1655}
1656
1657/*
1658 *@@ Delete:
1659 * this attempts to delete the object.
1660 * Throws a BSConfigExcpt upon failure.
1661 *
1662 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
1663 */
1664
1665int BSDeleteWPSObject::Delete(BSUniCodec &codecProcess,
1666 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
1667 const
1668{
1669 int irc = 0;
1670
1671 CONVERT(&codecProcess, ObjectID); // creates local string strXXX, PCSZ pcszXXX
1672
1673 HOBJECT hobj;
1674 if (!(hobj = WinQueryObject(pcszObjectID)))
1675 {
1676 if (pLogFile)
1677 pLogFile->Write("Error deleting WPS object \"%s\"",
1678 pcszObjectID);
1679
1680 throw BSConfigExcpt(WPOEXCPT_DELETEOBJECT, 0);
1681 }
1682
1683 if (!WinDestroyObject(hobj))
1684 {
1685 if (pLogFile)
1686 pLogFile->Write("Error deleting WPS object \"%s\", HOBJECT was 0x%lX",
1687 pcszObjectID,
1688 hobj);
1689
1690 throw BSConfigExcpt(WPOEXCPT_DELETEOBJECT, 0);
1691 }
1692
1693 return (irc);
1694}
1695
1696/*
1697 *@@ AddToUndoList:
1698 * static helper method to create BSDeleteWPSObject
1699 * instances from the logger instance that was
1700 * previously used with BSRegisterClass::Register.
1701 *
1702 * The new BSDeleteWPSObject objects are appended to
1703 * the specified list.
1704 *
1705 * This returns the number of items created.
1706 */
1707
1708int BSDeleteWPSObject::AddToUndoList(list<BSConfigBase*> &List,
1709 BSWPSObjectsDoneLogger &logger)
1710{
1711 // the logger in this case has a simple list of PSZs with all
1712 // the object IDs that were created, each of which is terminated
1713 // with a null character
1714
1715 PSZ pszLogStart = logger._pabLogString,
1716 pszLogThis = pszLogStart;
1717 int iCount = 0;
1718
1719 while (pszLogThis < pszLogStart + logger._cbLogString)
1720 {
1721 ULONG cbLogThis = strlen(pszLogThis);
1722 ustring ustr;
1723 ustr.assignUtf8(pszLogThis);
1724 List.push_back(new BSDeleteWPSObject(ustr));
1725 pszLogThis += cbLogThis + 1; // go beyond null byte
1726 iCount++;
1727 }
1728
1729 return (iCount);
1730}
1731
1732/* ******************************************************************
1733 *
1734 * BSClearProfile class
1735 *
1736 ********************************************************************/
1737
1738/*
1739 *@@ DescribeType:
1740 * describes the current manipulator type to the GUI.
1741 *
1742 *@@added V0.9.2 (2000-02-19) [umoeller]
1743 */
1744
1745const char* BSProfileBase::DescribeType()
1746{
1747 return ("Profile data");
1748}
1749
1750/*
1751 *@@ DescribeData:
1752 * describes the current manipulator data to the GUI.
1753 *
1754 *@@added V0.9.2 (2000-02-19) [umoeller]
1755 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
1756 */
1757
1758ustring BSProfileBase::DescribeData()
1759{
1760 return (DescribePrfKey());
1761}
1762
1763/*
1764 *@@ DescribePrfKey:
1765 * returns a descriptive string describing the
1766 * member fields, a la "USER\app\key".
1767 *
1768 *@@added V0.9.1 (2000-02-12) [umoeller]
1769 */
1770
1771ustring BSProfileBase::DescribePrfKey() const
1772{
1773 ustring str(_ustrProfile);
1774 if (_ustrApplication())
1775 {
1776 str.appendUtf8("\\");
1777 str += _ustrApplication;
1778
1779 if (_ustrKey())
1780 {
1781 str.appendUtf8("\\");
1782 str += _ustrKey;
1783 }
1784 }
1785
1786 return (str);
1787}
1788
1789/*
1790 *@@ DescribePrfKey:
1791 *
1792 *@@added V0.9.18 (2002-03-08) [umoeller]
1793 */
1794
1795string BSProfileBase::DescribePrfKey(BSUniCodec &codecProcess) const
1796{
1797 string str(&codecProcess, DescribePrfKey());
1798 return str;
1799}
1800
1801/*
1802 *@@ BSClearProfile:
1803 *
1804 * Syntax:
1805 *
1806 + CLEARPROFILE="profile\application[\key]"
1807 *
1808 *@@added V0.9.1 (2000-02-07) [umoeller]
1809 *@@changed V1.0.5 (2005-03-06) [pr]: Improved parsing of profiles with path names. @@fixes 633
1810 */
1811
1812BSClearProfile::BSClearProfile(const ustring &ustrClearProfile)
1813 : BSProfileBase(CFGT_CLEARPROFILE, tBSClearProfile)
1814{
1815 PCSZ pcszClearProfile = ustrClearProfile.GetBuffer();
1816
1817 // get profile
1818 PCSZ pFirstBackslash;
1819 if (pFirstBackslash = strchr(pcszClearProfile, '\\'))
1820 {
1821 // extract profile
1822 _ustrProfile.assignUtf8(pcszClearProfile, pFirstBackslash);
1823
1824 if ( _ustrProfile.compareUtf8("USER")
1825 && _ustrProfile.compareUtf8("SYSTEM")
1826 )
1827 {
1828 while (pFirstBackslash)
1829 {
1830 ULONG cbProfile = pFirstBackslash - pcszClearProfile;
1831 if ( (cbProfile > 4)
1832 && (!strnicmp(pFirstBackslash - 4, ".INI", 4))
1833 )
1834 {
1835 _ustrProfile.assignUtf8(pcszClearProfile, pFirstBackslash);
1836 break;
1837 }
1838
1839 pFirstBackslash = strchr(pFirstBackslash + 1, '\\');
1840 }
1841 }
1842 }
1843
1844 if (pFirstBackslash)
1845 {
1846 // get application
1847 PCSZ pSecondBackslash;
1848 if (pSecondBackslash = strchr(pFirstBackslash + 2, '\\'))
1849 {
1850 // key specified:
1851 // extract application up to key
1852 _ustrApplication.assignUtf8(pFirstBackslash + 1, pSecondBackslash);
1853
1854 // extract key (all the rest)
1855 _ustrKey.assignUtf8(pSecondBackslash + 1);
1856 }
1857 else
1858 // key not specified:
1859 _ustrApplication.assignUtf8(pFirstBackslash + 1);
1860 }
1861
1862 if (!_ustrApplication())
1863 // any error:
1864 throw BSConfigExcpt(PRFEXCPT_SYNTAX, ustrClearProfile);
1865}
1866
1867/*
1868 *@@ Clear:
1869 * deletes the corresponding profile entry.
1870 *
1871 *@@added V0.9.5 (2000-08-26) [umoeller]
1872 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
1873 */
1874
1875int BSClearProfile::Clear(BSUniCodec &codecProcess, // in: codec for process codepage,
1876 HAB hab,
1877 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
1878 const
1879{
1880 BOOL fOK = FALSE;
1881 HINI hini = NULLHANDLE,
1882 hini2Close = NULLHANDLE;
1883
1884 if (!_ustrProfile.compareUtf8("USER"))
1885 hini = HINI_USER;
1886 else if (!_ustrProfile.compareUtf8("SYSTEM"))
1887 hini = HINI_SYSTEM;
1888 else
1889 {
1890 CONVERT(&codecProcess, Profile);
1891 hini = PrfOpenProfile(hab,
1892 pcszProfile);
1893 hini2Close = hini;
1894 }
1895
1896 if (hini)
1897 {
1898 CONVERT(&codecProcess, Application);
1899 CONVERT(&codecProcess, Key);
1900
1901 ULONG cb = 0;
1902 // does key exist?
1903 if (PrfQueryProfileSize(hini,
1904 pcszApplication,
1905 pcszKey, // can be NULL
1906 &cb))
1907 {
1908 // key exists:
1909 if (PrfWriteProfileString(hini,
1910 pcszApplication,
1911 pcszKey, // can be NULL
1912 NULL))
1913 fOK = TRUE;
1914 }
1915 else
1916 // key does not exist:
1917 fOK = TRUE;
1918 }
1919
1920 if (!fOK)
1921 {
1922 string strClear = DescribePrfKey(codecProcess);
1923 if (pLogFile)
1924 pLogFile->Write("Error deleting profile key \"%s\"",
1925 strClear.c_str());
1926
1927 // error:
1928 throw BSConfigExcpt(PRFEXCPT_PRFERROR, DescribePrfKey());
1929 }
1930
1931 if (pLogFile)
1932 {
1933 string strClear = DescribePrfKey(codecProcess);
1934 pLogFile->Write("Deleted profile key \"%s\"",
1935 strClear.c_str());
1936 }
1937
1938 return (1);
1939}
1940
1941/*
1942 *@@ AddToUndoList:
1943 * static helper method to create BSClearProfile
1944 * instances from the specified logger instance.
1945 * Note that this class method will be used both
1946 * for "CLEARPROFILE" tags specified in the script
1947 * as well as "WRITEPROFILE" tags which were actually
1948 * executed (written to a profile).
1949 *
1950 * The new BSClearProfile objects are appended to
1951 * the specified list.
1952 *
1953 * This returns the number of items created.
1954 */
1955
1956int BSClearProfile::AddToUndoList(list<BSConfigBase*> &List,
1957 BSClearPrfAttrsLogger &logger)
1958{
1959 // the logger in this case has a simple list of PSZs with all
1960 // the profile keys that were created, each of which is terminated
1961 // with a null character
1962
1963 PSZ pszLogStart = logger._pabLogString,
1964 pszLogThis = pszLogStart;
1965 int iCount = 0;
1966
1967 while (pszLogThis < pszLogStart + logger._cbLogString)
1968 {
1969 ULONG cbLogThis = strlen(pszLogThis);
1970 ustring ustr;
1971 ustr.assignUtf8(pszLogThis);
1972 List.push_back(new BSClearProfile(ustr));
1973 pszLogThis += cbLogThis + 1; // go beyond null byte
1974 iCount++;
1975 }
1976
1977 return (iCount);
1978}
1979
1980/* ******************************************************************
1981 *
1982 * BSWriteProfile class
1983 *
1984 ********************************************************************/
1985
1986/*
1987 *@@ BSWriteProfile:
1988 *
1989 * Syntax:
1990 *
1991 + WRITEPROFILE="profile\application\key|string"
1992 *
1993 *@@added V0.9.1 (2000-02-07) [umoeller]
1994 *@@changed V0.9.19 (2002-05-07) [umoeller]: added missing class init, now writeprofiles work again
1995 *@@changed V1.0.5 (2005-03-06) [pr]: Improved parsing of profiles with path names. @@fixes 633
1996 */
1997
1998BSWriteProfile::BSWriteProfile(const ustring &ustrWriteProfile)
1999 : BSProfileBase(CFGT_WRITEPROFILE, tBSWriteProfile)
2000{
2001 PCSZ pcszWriteProfile = ustrWriteProfile.GetBuffer();
2002
2003 // override class, this was still BSClearProfile due
2004 // to the constructor; this is also why writeprofiles
2005 // never worked V0.9.19 (2002-05-07) [umoeller]
2006 _Class = tBSWriteProfile;
2007
2008 PCSZ pSlash;
2009 if (pSlash = strchr(pcszWriteProfile, '|'))
2010 {
2011 // copy write data (after '|')
2012 _ustrWriteString.assignUtf8(pSlash + 1);
2013
2014 // get profile
2015 PCSZ pFirstBackslash;
2016 if (pFirstBackslash = strchr(pcszWriteProfile, '\\'))
2017 {
2018 // extract profile
2019 _ustrProfile.assignUtf8(pcszWriteProfile, pFirstBackslash);
2020
2021 if ( _ustrProfile.compareUtf8("USER")
2022 && _ustrProfile.compareUtf8("SYSTEM")
2023 )
2024 {
2025 while (pFirstBackslash)
2026 {
2027 ULONG cbProfile = pFirstBackslash - pcszWriteProfile;
2028 if ( (cbProfile > 4)
2029 && (!strnicmp(pFirstBackslash - 4, ".INI", 4))
2030 && (pFirstBackslash < pSlash)
2031 )
2032 {
2033 _ustrProfile.assignUtf8(pcszWriteProfile, pFirstBackslash);
2034 break;
2035 }
2036
2037 pFirstBackslash = strchr(pFirstBackslash + 1, '\\');
2038 }
2039 }
2040 }
2041
2042 if (pFirstBackslash)
2043 {
2044 // get application
2045 PCSZ pSecondBackslash;
2046 if (pSecondBackslash = strchr(pFirstBackslash + 2, '\\'))
2047 {
2048 // extract application
2049 _ustrApplication.assignUtf8(pFirstBackslash + 1, pSecondBackslash);
2050
2051 // extract key (up to '|')
2052 _ustrKey.assignUtf8(pSecondBackslash + 1, pSlash);
2053 }
2054 }
2055 }
2056
2057 if (!_ustrKey())
2058 // any error:
2059 throw BSConfigExcpt(PRFEXCPT_SYNTAX, ustrWriteProfile);
2060}
2061
2062/*
2063 *@@ Write:
2064 * this actually writes data to a profile, all
2065 * as specified by the member variables.
2066 *
2067 * Note that this stores the "done" stuff in
2068 * a BSClearPrfAttrsLogger for undoing later.
2069 *
2070 * Throws:
2071 * -- BSConfigExcpt.
2072 *
2073 *@@added V0.9.1 (2000-02-07) [umoeller]
2074 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
2075 */
2076
2077int BSWriteProfile::Write(BSUniCodec &codecProcess, // in: codec for process codepage,
2078 HAB hab, // in: anchor block (for PrfOpenProfile)
2079 BSClearPrfAttrsLogger *pLogger,
2080 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
2081 const
2082{
2083 BOOL fOK = FALSE;
2084 HINI hini = NULLHANDLE,
2085 hini2Close = NULLHANDLE;
2086
2087 if (!_ustrProfile.compareUtf8("USER"))
2088 hini = HINI_USER;
2089 else if (!_ustrProfile.compareUtf8("SYSTEM"))
2090 hini = HINI_SYSTEM;
2091 else
2092 {
2093 CONVERT(&codecProcess, Profile);
2094 if (!(hini = PrfOpenProfile(hab,
2095 pcszProfile)))
2096 throw BSConfigExcpt(PRFEXCPT_PRFOPENPROFILE, _ustrProfile);
2097
2098 hini2Close = hini;
2099 }
2100
2101 if (hini)
2102 {
2103 CONVERT(&codecProcess, Application);
2104 CONVERT(&codecProcess, Key);
2105 CONVERT(&codecProcess, WriteString);
2106 if (PrfWriteProfileString(hini,
2107 pcszApplication,
2108 pcszKey,
2109 pcszWriteString))
2110 {
2111 fOK = TRUE;
2112 }
2113 }
2114
2115 // recompose string for BSClearProfile
2116 ustring ustrClear = DescribePrfKey();
2117 string strClear = DescribePrfKey(codecProcess);
2118
2119 if (fOK)
2120 {
2121 if (pLogFile)
2122 pLogFile->Write("Wrote profile key \"%s\"",
2123 strClear.c_str());
2124
2125 if (pLogger)
2126 pLogger->Append(ustrClear);
2127 }
2128 else
2129 {
2130 // error:
2131 if (pLogFile)
2132 pLogFile->Write("Error writing profile key \"%s\"",
2133 strClear.c_str());
2134
2135 throw BSConfigExcpt(PRFEXCPT_PRFERROR, DescribePrfKey());
2136 }
2137
2138 return 1;
2139}
2140
2141/* ******************************************************************
2142 *
2143 * BSExecute class
2144 *
2145 ********************************************************************/
2146
2147/*
2148 *@@ BSExecute:
2149 * this constructor takes an EXECUTE attribute (of the PCK tag)
2150 * as input and sets up the instance data accordingly.
2151 *
2152 * Syntax:
2153 *
2154 + EXECUTE="[ context ] execfile params"
2155 *
2156 * Throws:
2157 * -- BSConfigExcpt.
2158 *
2159 *@@added V0.9.1 (2000-02-07) [umoeller]
2160 *@@changed V0.9.3 (2000-04-08) [umoeller]: "|" was a requirement, contradicting the docs; changed. Thanks, Cornelis Bockemhl.
2161 *@@changed V0.9.4 (2000-07-10) [umoeller]: _ulExecType wasn't always initialized; thanks Yuri Dario
2162 *@@changed V0.9.4 (2000-07-26) [umoeller]: now ORing the flags
2163 *@@changed V0.9.9 (2001-03-27) [umoeller]: fixed EXECUTE parameters
2164 */
2165
2166BSExecute::BSExecute(const ustring &ustrExecute,
2167 BSClassID &Class)
2168 : BSConfigBase(CFGT_EXECUTE, Class)
2169{
2170 BOOL fContextFound = FALSE,
2171 fContinue = TRUE;
2172 PCSZ pcszExecute = ustrExecute.GetBuffer();
2173 PCSZ pSearch = (PSZ)pcszExecute;
2174
2175 _ulExecType = 0;
2176 _arc = 0; // NO_ERROR
2177
2178 while (fContinue)
2179 {
2180 // skip leading spaces
2181 while ( (*pSearch)
2182 && (*pSearch == ' ')
2183 )
2184 pSearch++;
2185
2186 if (strncmp(pSearch, "CONFIGSYS", 9) == 0)
2187 {
2188 _ulExecType |= CFGT_CFGSYS;
2189 fContextFound = TRUE;
2190 pSearch += 9; // strlen("CONFIGSYS");
2191 }
2192 else if (strncmp(pSearch, "REGISTERCLASS", 13) == 0)
2193 {
2194 _ulExecType |= CFGT_REGISTERCLASS;
2195 fContextFound = TRUE;
2196 pSearch += 13; // strlen("REGISTERCLASS");
2197 }
2198 else if (strncmp(pSearch, "CREATEOBJECT", 12) == 0)
2199 {
2200 _ulExecType |= CFGT_CREATEOBJECT;
2201 fContextFound = TRUE;
2202 pSearch += 12; // strlen("CREATEOBJECT");
2203 }
2204 else
2205 // other: stop looping
2206 fContinue = FALSE;
2207 } // end while (fContinue)
2208
2209 if (!(*pSearch))
2210 throw BSConfigExcpt(EXEEXCPT_SYNTAX, ustrExecute);
2211
2212 // pSearch now points to the first non-space
2213 // character which does not introduces a keyword;
2214 // check if this is a "|" separator
2215
2216 if (*pSearch == '|')
2217 // skip '|'
2218 pSearch++;
2219
2220 // skip following spaces
2221 while ( (*pSearch)
2222 && (*pSearch == ' ')
2223 )
2224 pSearch++;
2225
2226 // NOW we finally point to the executable
2227
2228 if (!(*pSearch))
2229 throw BSConfigExcpt(EXEEXCPT_SYNTAX, ustrExecute);
2230
2231 // separate executable and parameters:
2232 // This can be separated by another '|',
2233 // or a space.
2234
2235 // NOTE: The '|' separator is used by the "resolved"
2236 // logger, from which BSDeExecute instances are
2237 // created on de-install. But the documentation states
2238 // spaces should be used... as long as a user doesn't
2239 // use spaces in the (unresolved) filename, we're fine.
2240 // For example we get:
2241 // 1) on install: $(1)\install.cmd PARAMETER
2242 // works nicely, and $(1) may contain spaces.
2243 // 2) Logger stores C:\PATH1 WITH SPACES\install.cmd|PARAMETER
2244 // in database.
2245 // 3) on deinstall, BSDeExecutes get created, and
2246 // we now have nice separation and get PARAMETER
2247 // properly separated.
2248 PCSZ p;
2249 if (!(p = strchr(pSearch, '|')))
2250 p = strchr(pSearch, ' ');
2251 if (p)
2252 {
2253 const char *pSep = p++; // V1.0.5 (2005-01-20) [pr]: @@fixes 624
2254 // we have a space --> parameters:
2255 while ( (*p)
2256 && (*p == ' ')
2257 )
2258 p++;
2259
2260 if (*p)
2261 {
2262 _ustrParams.assignUtf8(p);
2263 _ustrExecutable.assignUtf8(pSearch, pSep);
2264 }
2265 else
2266 _ustrExecutable.assignUtf8(pSearch);
2267 }
2268 else
2269 _ustrExecutable.assignUtf8(pSearch);
2270}
2271
2272/*
2273 *@@ DescribeType:
2274 * describes the current manipulator type to the GUI.
2275 *
2276 *@@added V0.9.9 (2001-03-27) [umoeller]
2277 */
2278
2279const char* BSExecute::DescribeType()
2280{
2281 return ("Execute program");
2282}
2283
2284/*
2285 *@@ DescribeData:
2286 * describes the current manipulator data to the GUI.
2287 *
2288 *@@added V0.9.9 (2001-03-27) [umoeller]
2289 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
2290 */
2291
2292ustring BSExecute::DescribeData()
2293{
2294 ustring ustr;
2295 ustr._printf("\"%s\"", _ustrExecutable.GetBuffer());
2296
2297 if (_ustrParams())
2298 {
2299 ustr.appendUtf8(" \"");
2300 ustr += _ustrParams;
2301 ustr.appendUtf8("\"");
2302 }
2303
2304 if (_ulExecType == 0)
2305 ustr.appendUtf8(" (no flags)");
2306 else
2307 {
2308 if (_ulExecType & CFGT_CFGSYS)
2309 ustr.appendUtf8(" CONFIGSYS");
2310 if (_ulExecType & CFGT_REGISTERCLASS)
2311 ustr.appendUtf8(" REGISTERCLASS");
2312 if (_ulExecType & CFGT_CREATEOBJECT)
2313 ustr.appendUtf8(" CREATEOBJECT");
2314 }
2315
2316 return (ustr);
2317}
2318
2319/*
2320 *@@ Execute:
2321 * executes the member program. This does not return
2322 * until the program terminates. As a result, do not
2323 * call this on a PM thread, because this gets blocked.
2324 *
2325 * Throws:
2326 * -- BSConfigExcpt.
2327 *
2328 *@@added V0.9.1 (2000-02-07) [umoeller]
2329 *@@changed V0.9.18 (2002-03-08) [umoeller]: added codec
2330 *@@changed V1.0.5 (2005-01-25) [pr]: Change directory to that of executable
2331 */
2332
2333int BSExecute::Execute(BSUniCodec &codecProcess, // in: codec for process codepage,
2334 BSFileLogger *pLogFile) // in: file logger (log file), can be NULL
2335{
2336 string strParams = "/c ";
2337 strParams.appendUtf8(&codecProcess, _ustrExecutable);
2338 if (_ustrParams())
2339 {
2340 strParams += " ";
2341 strParams.appendUtf8(&codecProcess, _ustrParams);
2342 }
2343
2344 // V1.0.5 (2005-01-26) [pr]: Change directory to that of executable. @@fixes 626.
2345 // We remove any leading quotes and just leave the directory name, excluding a
2346 // trailing slash, unless it is a root directory.
2347 string strDir;
2348 strDir.assignUtf8(&codecProcess, _ustrExecutable);
2349 strDir = strDir.substr(strDir.find_first_not_of("\"'"));
2350 size_type pos = strDir.rfind('\\', 0);
2351 if (pos != string::npos)
2352 {
2353 strDir.erase(pos);
2354 if (strDir.size() == 2 && strDir[1] == ':')
2355 strDir += '\\';
2356
2357 doshSetCurrentDir(strDir.c_str());
2358 }
2359
2360 ULONG sid = 0;
2361 PID pid = 0;
2362 if (_arc = doshQuickStartSession("cmd.exe",
2363 (PSZ)strParams.c_str(),
2364 SSF_TYPE_DEFAULT, // session type
2365 FALSE, // foreground
2366 0, // auto-close, visible
2367 TRUE, // wait
2368 &sid,
2369 &pid,
2370 NULL))
2371 {
2372 if (pLogFile)
2373 pLogFile->Write("Error %d executing \"CMD.EXE %s\"",
2374 _arc,
2375 strParams.c_str());
2376
2377 throw BSConfigExcpt(EXEEXCPT_DOSERROR, _arc);
2378 }
2379
2380 if (pLogFile)
2381 pLogFile->Write("Executed \"CMD.EXE %s\"",
2382 strParams.c_str());
2383
2384 return (0);
2385}
2386
2387/* ******************************************************************
2388 *
2389 * BSDeExecute class
2390 *
2391 ********************************************************************/
2392
2393/*
2394 *@@ BSDeExecute:
2395 * constructor.
2396 *
2397 *@@added V0.9.16 (2001-12-08) [umoeller]
2398 */
2399
2400BSDeExecute::BSDeExecute(const ustring &ustrDeExecute)
2401 : BSExecute(ustrDeExecute, tBSDeExecute)
2402{
2403};
2404
2405/*
2406 *@@ DescribeType:
2407 * describes the current manipulator type to the GUI.
2408 *
2409 *@@added V0.9.2 (2000-02-19) [umoeller]
2410 */
2411
2412const char* BSDeExecute::DescribeType()
2413{
2414 return ("External program for deinstall");
2415}
2416
2417/*
2418 *@@ AddToUndoList:
2419 * static helper method to create BSDeExecute instances from
2420 * the specified logger instance.
2421 *
2422 * The new BSKillProcess objects are appended to
2423 * the specified list.
2424 *
2425 * This returns the number of items created.
2426 *
2427 * This isn't really an "Undo" method like the other
2428 * static AddToUndoList methods (for the other classes),
2429 * but has been named like this for conformity. This
2430 * is during package de-install to create the same
2431 * BSDeExecute list which was initially created from
2432 * the script so that processes can be killed during
2433 * de-install as well.
2434 *
2435 *@@added V0.9.9 (2001-03-27) [umoeller]
2436 */
2437
2438int BSDeExecute::AddToUndoList(list <BSConfigBase*> &List,
2439 BSDeExecuteResolvedLogger &logger)
2440{
2441 PSZ pszLogStart = logger._pabLogString,
2442 pszLogThis = pszLogStart;
2443 int iCount = 0;
2444
2445 while (pszLogThis < pszLogStart + logger._cbLogString)
2446 {
2447 ULONG cbLogThis = strlen(pszLogThis);
2448 ustring ustr;
2449 ustr.assignUtf8(pszLogThis);
2450 List.push_back(new BSDeExecute(ustr));
2451 pszLogThis += cbLogThis + 1; // go beyond null byte
2452 iCount++;
2453 }
2454
2455 return (iCount);
2456}
2457
2458/* ******************************************************************
2459 *
2460 * BSKillProcess class
2461 *
2462 ********************************************************************/
2463
2464/*
2465 *@@ BSKillProcess:
2466 *
2467 * Syntax:
2468 *
2469 + KILLPROCESS="filename"
2470 *
2471 * Throws:
2472 * -- BSConfigExcpt.
2473 *
2474 *@@added V0.9.1 (2000-02-12) [umoeller]
2475 */
2476
2477BSKillProcess::BSKillProcess(const ustring &ustrKillProcess)
2478 : BSConfigBase(CFGT_KILLPROCESS, tBSKillProcess)
2479{
2480 if (!ustrKillProcess())
2481 throw BSConfigExcpt(KILLEXCPT_SYNTAX, ustrKillProcess);
2482
2483 _ustrKillProcess = ustrKillProcess;
2484}
2485
2486/*
2487 *@@ DescribeType:
2488 * describes the current manipulator type to the GUI.
2489 *
2490 *@@added V0.9.4 (2000-07-01) [umoeller]
2491 */
2492
2493const char* BSKillProcess::DescribeType()
2494{
2495 return ("Kill process");
2496}
2497
2498/*
2499 *@@ DescribeData:
2500 * describes the current manipulator data to the GUI.
2501 *
2502 *@@added V0.9.4 (2000-07-01) [umoeller]
2503 *@@changed V0.9.18 (2002-03-08) [umoeller]: now returning ustring
2504 */
2505
2506ustring BSKillProcess::DescribeData()
2507{
2508 return _ustrKillProcess;
2509}
2510
2511/*
2512 *@@ AddToUndoList:
2513 * static helper method to create BSKillProcess
2514 * instances from the specified logger instance.
2515 *
2516 * The new BSKillProcess objects are appended to
2517 * the specified list.
2518 *
2519 * This returns the number of items created.
2520 *
2521 * This isn't really an "Undo" method like the other
2522 * static AddToUndoList methods (for the other classes),
2523 * but has been named like this for conformity. This
2524 * is during package de-install to create the same
2525 * BSKillProcess list which was initially created from
2526 * the script so that processes can be killed during
2527 * de-install as well.
2528 *
2529 *@@added V0.9.4 (2000-07-01) [umoeller]
2530 */
2531
2532int BSKillProcess::AddToUndoList(list<BSConfigBase*> &List,
2533 BSKillProcessAttrsLogger &logger)
2534{
2535 // the logger in this case has a simple list of PSZs with all
2536 // the executable names that were created, each of which is
2537 // terminated with a null character
2538
2539 PSZ pszLogStart = logger._pabLogString,
2540 pszLogThis = pszLogStart;
2541 int iCount = 0;
2542
2543 while (pszLogThis < pszLogStart + logger._cbLogString)
2544 {
2545 ULONG cbLogThis = strlen(pszLogThis);
2546 ustring ustr;
2547 ustr.assignUtf8(pszLogThis);
2548 List.push_back(new BSKillProcess(ustr));
2549 pszLogThis += cbLogThis + 1; // go beyond null byte
2550 iCount++;
2551 }
2552
2553 return (iCount);
2554}
2555
2556
Note: See TracBrowser for help on using the repository browser.