source: trunk/src/gui/common/epmenv.c@ 3408

Last change on this file since 3408 was 3408, checked in by Andreas Schnellbacher, 6 years ago
  • Removed unused _searchLoaderExecutable.
  • Property svn:keywords set to Date Revision Author HeadURL Id
File size: 30.9 KB
Line 
1/****************************** Module Header *******************************
2*
3* Module Name: epmenv.c
4*
5* Generic routine to load the NEPMD environment file for EPM and NEPMD
6* utilities
7*
8* Copyright (c) Netlabs EPM Distribution Project 2002
9*
10* $Id: epmenv.c 3408 2019-01-12 20:15:45Z aschn $
11*
12* ===========================================================================
13*
14* This file is part of the Netlabs EPM Distribution package and is free
15* software. You can redistribute it and/or modify it under the terms of the
16* GNU General Public License as published by the Free Software
17* Foundation, in version 2 as it comes in the "COPYING" file of the
18* Netlabs EPM Distribution. This library is distributed in the hope that it
19* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21* General Public License for more details.
22*
23****************************************************************************/
24
25#define INCL_DOS
26#define INCL_WIN
27#define INCL_ERRORS
28#include <os2.h>
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33
34#include "macros.h"
35#include "file.h"
36#include "nepmd.h"
37#include "instval.h"
38
39#include "epmenv.h"
40
41#define __APPNAMESHORT__ "NEPMD"
42
43// -----------------------------------------------------------------------------
44
45static APIRET _searchEpmExecutable( PSZ pszEpmExecutable, ULONG ulBuflen,
46 PSZ pszLoaderExecutable, ULONG ulLoaderBuflen)
47{
48 APIRET rc = NO_ERROR;
49 PPIB ppib;
50 PTIB ptib;
51 ULONG ulBootDrive;
52
53
54 BOOL fFound = FALSE;
55 PSZ pszPath = getenv( "PATH");
56 PSZ pszCopy = NULL;
57 PSZ pszDir;
58 CHAR szExecutable[ _MAX_PATH];
59
60 CHAR szThisModule[ _MAX_PATH];
61 CHAR szNepmdModule[ _MAX_PATH];
62 CHAR szInstalledModule[ _MAX_PATH];
63
64do
65 {
66 // check parms
67 if ((!pszEpmExecutable) ||
68 (!ulBuflen))
69 {
70 rc = ERROR_INVALID_PARAMETER;
71 break;
72 }
73
74 // check env
75 if (!pszPath)
76 {
77 rc = ERROR_ENVVAR_NOT_FOUND;
78 break;
79 }
80
81 // get name of own module
82 DosGetInfoBlocks( &ptib,&ppib);
83 DosQueryModuleName( ppib->pib_hmte, sizeof( szThisModule), szThisModule);
84
85 // get name of epm.exe in OS/2 directory
86 // this is used by installed NEPMD
87 DosQuerySysInfo( QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive, sizeof( ULONG));
88 sprintf( szInstalledModule, "%c:\\OS2\\EPM.EXE", (CHAR) ulBootDrive + 'A' - 1);
89
90 // get name of EPM.EXE in NEPMD path
91 memset( szNepmdModule, 0, sizeof( szNepmdModule));
92 rc = QueryInstValue( NEPMD_INSTVALUE_ROOTDIR, szNepmdModule, sizeof( szNepmdModule));
93 if (rc == NO_ERROR)
94 {
95 strcat( szNepmdModule, "\\"NEPMD_SUBPATH_BINBINDIR"\\epm.exe");
96 strupr( szNepmdModule);
97 }
98 else
99 {
100 // don't report error from here
101 rc = NO_ERROR;
102
103 // copy pathname of installed EPM fullname here in order
104 // not to break the filename comparison scheme below
105 strcpy( szNepmdModule, szInstalledModule);
106
107 }
108
109 // create copy to allow modification
110 pszCopy = strdup( pszPath);
111 if (!pszCopy)
112 {
113 rc = ERROR_NOT_ENOUGH_MEMORY;
114 break;
115 }
116
117 pszDir = strtok( pszCopy, ";");
118 while (pszDir)
119 {
120 // create fullname for entry to check
121 strcpy( szExecutable, pszDir);
122 if (*(pszDir + strlen( pszDir) - 1) != '\\')
123 strcat( szExecutable, "\\");
124 strcat( szExecutable, "epm.exe");
125 rc = DosQueryPathInfo( szExecutable, FIL_QUERYFULLNAME, szExecutable, sizeof( szExecutable));
126 strupr( szExecutable);
127 if (rc == NO_ERROR)
128 {
129 // file must exist
130 if (FileExists( szExecutable))
131 {
132 // process only modules not being the current one or of NEPMD bin directory
133 if ((strcmp( szExecutable, szThisModule)) &&
134 (strcmp( szExecutable, szNepmdModule)) &&
135 (strcmp( szExecutable, szInstalledModule)))
136 {
137 // executable found
138 fFound = TRUE;
139 break;
140 }
141 }
142 }
143 else
144 rc = NO_ERROR;
145
146 // next please
147 pszDir = strtok( NULL, ";");
148 }
149 if (!fFound)
150 {
151 rc = ERROR_FILE_NOT_FOUND;
152 break;
153 }
154
155 // hand over results
156 if (strlen( szExecutable) + 1 > ulBuflen)
157 {
158 rc= ERROR_BUFFER_OVERFLOW;
159 break;
160 }
161 strcpy( pszEpmExecutable, szExecutable);
162
163 if (strlen( szThisModule) + 1 > ulBuflen)
164 {
165 rc= ERROR_BUFFER_OVERFLOW;
166 break;
167 }
168 strcpy( pszLoaderExecutable, szThisModule);
169
170 } while (FALSE);
171
172// cleanup
173if (pszCopy) free( pszCopy);
174return rc;
175}
176
177// -----------------------------------------------------------------------------
178
179static APIRET _searchNepmdEnvironmentFiles( PSZ pszMainEnvFile, ULONG ulMainBuflen,
180 PSZ pszAddEnvFile, ULONG ulAddBuflen)
181{
182 APIRET rc = NO_ERROR;
183 BOOL fFound = FALSE;
184 PPIB ppib;
185 PTIB ptib;
186
187 CHAR szExecutablePath[ _MAX_PATH];
188 CHAR szBasename[ _MAX_PATH];
189 CHAR szRootDir[ _MAX_PATH];
190 CHAR szUserDir[ _MAX_PATH];
191 ULONG ulDataLen;
192 BOOL fRootDirFound = FALSE;
193 BOOL fUserDirFound = FALSE;
194 CHAR szCurrentPath[ _MAX_PATH];
195
196 CHAR szMainEnvFile[ _MAX_PATH];
197 CHAR szAddEnvFile[ _MAX_PATH];
198
199 CHAR szMessage[ 1024];
200
201static PSZ pszDefaultExecBaseName = "epm";
202static PSZ pszMyDefaultExecBaseName = "myepm";
203
204static PSZ pszNepmdExecDirMask = "%s\\"NEPMD_SUBPATH_BINBINDIR"\\%s"NEPMD_FILENAMEEXT_ENV;
205static PSZ pszUserExecDirMask = "%s\\"NEPMD_SUBPATH_USERBINDIR"\\%s"NEPMD_FILENAMEEXT_ENV;
206
207do
208 {
209 // check parms
210 if ((!pszMainEnvFile) ||
211 (!pszAddEnvFile) ||
212 (!ulMainBuflen) ||
213 (!ulAddBuflen))
214 {
215 rc = ERROR_INVALID_PARAMETER;
216 break;
217 }
218
219 // init vars
220 memset( pszMainEnvFile, 0, ulMainBuflen);
221 memset( pszAddEnvFile, 0, ulAddBuflen);
222
223 // get own filename to isolate basename of executable
224 DosGetInfoBlocks( &ptib,&ppib);
225 DosQueryModuleName( ppib->pib_hmte, sizeof( szExecutablePath), szExecutablePath);
226 strcpy( szBasename, strrchr( szExecutablePath, '\\') + 1);
227 strcpy( strrchr( szBasename, '.'), "");
228
229 // isolate path of executabe
230 strcpy( strrchr( szExecutablePath, '\\'), "");
231
232 // get NEPMD install directories
233 rc = QueryInstValue( NEPMD_INSTVALUE_ROOTDIR, szRootDir, sizeof( szRootDir));
234 fRootDirFound = (rc == NO_ERROR);
235 rc = QueryInstValue( NEPMD_INSTVALUE_USERDIR, szUserDir, sizeof( szUserDir));
236 fUserDirFound = (rc == NO_ERROR);
237
238 // ----- check for main env file loaded
239
240 do
241 {
242 // reset rc here
243 rc == NO_ERROR;
244
245 if (!fRootDirFound)
246 {
247 rc = ERROR_PATH_NOT_FOUND;
248 sprintf( szMessage,
249 "Fatal error: RootDir could not be determined.\n\n"
250 "NEPMD is not properly installed,"
251 " repeat the installation via WarpIN!\n\n"
252 "If that problem still persists, check"
253 " NEPMD -> RootDir in OS2.INI.\n\n");
254 SHOWFATALERROR( HWND_DESKTOP, szMessage);
255 break;
256 }
257
258 if (!fUserDirFound)
259 {
260 sprintf( szMessage,
261 "Fatal error: UserDir could not be determined.\n\n"
262 "NEPMD is not properly installed,"
263 " repeat the installation via WarpIN!\n\n"
264 "If that problem still persists, check"
265 " if your UserDir (e.g. NEPMD\\myepm)"
266 " exists and is writable.\n\n");
267 SHOWFATALERROR( HWND_DESKTOP, szMessage);
268 break;
269 }
270
271 // <nepmd_userdir>\bin\<exename>.env
272 sprintf( szMainEnvFile, pszUserExecDirMask, szUserDir, szBasename);
273 //DPRINTF(( "EPMENV: Search main env file: %s\n", szMainEnvFile));
274 if (fFound = FileExists( szMainEnvFile))
275 break;
276
277 // <nepmd_userdir>\bin\epm.env
278 sprintf( szMainEnvFile, pszUserExecDirMask, szUserDir, pszDefaultExecBaseName);
279 //DPRINTF(( "EPMENV: Search main env file: %s\n", szMainEnvFile));
280 if (fFound = FileExists( szMainEnvFile))
281 break;
282
283 // <executable_path>\<exename>.env
284 sprintf( szMainEnvFile, "%s\\%s"NEPMD_FILENAMEEXT_ENV, szExecutablePath, szBasename);
285 //DPRINTF(( "EPMENV: Search main env file: %s\n", szMainEnvFile));
286 if (fFound = FileExists( szMainEnvFile))
287 break;
288
289 // <nepmd_rootdir>\netlabs\bin\<exename>.env
290 sprintf( szMainEnvFile, pszNepmdExecDirMask, szRootDir, szBasename);
291 //DPRINTF(( "EPMENV: Search main env file: %s\n", szMainEnvFile));
292 if (fFound = FileExists( szMainEnvFile))
293 break;
294
295 // <nepmd_rootdir>\netlabs\bin\epm.env
296 sprintf( szMainEnvFile, pszNepmdExecDirMask, szRootDir, pszDefaultExecBaseName);
297 //DPRINTF(( "EPMENV: Search main env file: %s\n", szMainEnvFile));
298 if (fFound = FileExists( szMainEnvFile))
299 break;
300
301
302 } while (FALSE);
303
304 // delete filename if not found
305 if (!fFound)
306 szMainEnvFile[ 0] = 0;
307
308 // ----- check for additional env file loaded
309
310 do
311 {
312 // <nepmd_userdir>\bin\myepm.env
313 sprintf( szAddEnvFile, pszUserExecDirMask, szUserDir, pszMyDefaultExecBaseName);
314 //DPRINTF(( "EPMENV: Search additional env file: %s\n", szAddEnvFile));
315 if (fFound = FileExists( szAddEnvFile))
316 break;
317 } while (FALSE);
318
319
320 // delete filename if not found
321 if (!fFound)
322 szAddEnvFile[ 0] = 0;
323
324 // error if not found
325 if (!strlen( szMainEnvFile))
326 {
327 rc = ERROR_FILE_NOT_FOUND;
328 break;
329 }
330
331 // hand over result
332 if (strlen( szMainEnvFile) + 1 > ulMainBuflen)
333 {
334 rc = ERROR_BUFFER_OVERFLOW;
335 break;
336 }
337 if (strlen( szAddEnvFile) + 1 > ulAddBuflen)
338 {
339 rc = ERROR_BUFFER_OVERFLOW;
340 break;
341 }
342
343 strcpy( pszMainEnvFile, szMainEnvFile);
344 strcpy( pszAddEnvFile, szAddEnvFile);
345 //DPRINTF(( "EPMENV: Main envfile is: %s\n", strlen( pszMainEnvFile) ? pszMainEnvFile : "<none>"));
346 //DPRINTF(( "EPMENV: Add. envfile is: %s\n", strlen( pszAddEnvFile) ? pszAddEnvFile : "<none>"));
347
348 } while (FALSE);
349
350return rc;
351}
352
353// -----------------------------------------------------------------------------
354
355PSZ _queryExtLIBPATH( PSZ pBuffer, ULONG ulWhichPath)
356{
357 APIRET rc;
358 rc = DosQueryExtLIBPATH( pBuffer, ulWhichPath);
359 return ((rc == NO_ERROR) ? pBuffer : NULL);
360}
361
362// -----------------------------------------------------------------------------
363
364PSZ ExpandEnvVar( PSZ pszStr)
365{
366 PSZ pszResult = NULL;
367 PSZ pszNewValue;
368 PSZ pszStartPos;
369 PSZ pszEndPos;
370 PSZ pszLibpath = NULL;
371 PSZ pszVarValue;
372 PSZ pszNewResult;
373 ULONG ulNameLen;
374 ULONG ulNewResultLen;
375
376 CHAR szVarName[ 128];
377 // Several sources say DosQueryExtLIBPATH will never return
378 // more than 1024 bytes.
379 CHAR szLibPath[ 1025]; // +1 for safe measure
380static CHAR chDelimiter = '%';
381
382do
383 {
384 // check parms
385 if (!pszStr)
386 break;
387
388 // create a copy
389 pszResult = strdup( pszStr);
390 if (!pszResult)
391 break;
392
393 // maintain the copy
394 pszStartPos = strchr( pszResult, chDelimiter);
395 while (pszStartPos)
396 {
397 // find end
398 pszEndPos = strchr( pszStartPos + 1, chDelimiter);
399
400 // no end found, cut off to end of string
401 if (!pszEndPos)
402 {
403 *pszStartPos = 0;
404 break;
405 }
406 else
407 {
408 // isolate name
409 ulNameLen = pszEndPos - pszStartPos - 1;
410 memcpy( szVarName, pszStartPos + 1, ulNameLen);
411 szVarName[ ulNameLen] = 0;
412
413 // first of all, elimintate the variable
414 strcpy( pszStartPos, pszEndPos + 1);
415
416 // get value
417 if (!stricmp( szVarName, "beginlibpath"))
418 pszVarValue = _queryExtLIBPATH( szLibPath, BEGIN_LIBPATH);
419 else if (!stricmp( szVarName, "endlibpath"))
420 pszVarValue = _queryExtLIBPATH( szLibPath, END_LIBPATH);
421 else
422 pszVarValue = getenv( szVarName);
423
424 if (pszVarValue)
425 {
426 // embedd new value
427 pszNewResult = malloc( strlen( pszResult) + 1 + strlen( pszVarValue));
428 if (pszNewResult)
429 {
430 strcpy( pszNewResult, pszResult);
431 strcpy( pszNewResult + (pszStartPos - pszResult), pszVarValue);
432 strcat( pszNewResult, pszStartPos);
433 free( pszResult);
434 pszResult = pszNewResult;
435 }
436 else
437 {
438 // kick any result, as we are out of memory
439 free( pszResult);
440 pszResult = NULL;
441 break;
442 }
443 }
444 }
445
446 // next var please
447 pszStartPos = strchr( pszResult, chDelimiter);
448 }
449
450
451 } while (FALSE);
452
453return pszResult;
454}
455
456// -----------------------------------------------------------------------------
457// Expands env vars like _expandEnvVar, but also replaces %NEPMD_ROOTDIR% with
458// its value, specified as arg2.
459
460PSZ ExpandEnvVarAndRootDir( PSZ pszStr, PSZ pszRootDirValue)
461{
462 PSZ pszResult = NULL;
463 PSZ pszNewValue;
464 PSZ pszStartPos;
465 PSZ pszEndPos;
466 PSZ pszLibpath = NULL;
467 PSZ pszVarValue;
468 PSZ pszNewResult;
469 ULONG ulNameLen;
470 ULONG ulNewResultLen;
471
472 CHAR szVarName[ 128];
473 // Several sources say DosQueryExtLIBPATH will never return
474 // more than 1024 bytes.
475 CHAR szLibPath[ 1025]; // +1 for safe measure
476static CHAR chDelimiter = '%';
477
478do
479 {
480 // check parms
481 if (!pszStr)
482 break;
483 if (!pszRootDirValue)
484 break;
485
486 // create a copy
487 pszResult = strdup( pszStr);
488 if (!pszResult)
489 break;
490
491 // maintain the copy
492 pszStartPos = strchr( pszResult, chDelimiter);
493 while (pszStartPos)
494 {
495 // find end
496 pszEndPos = strchr( pszStartPos + 1, chDelimiter);
497
498 // no end found, cut off to end of string
499 if (!pszEndPos)
500 {
501 *pszStartPos = 0;
502 break;
503 }
504 else
505 {
506 // isolate name
507 ulNameLen = pszEndPos - pszStartPos - 1;
508 memcpy( szVarName, pszStartPos + 1, ulNameLen);
509 szVarName[ ulNameLen] = 0;
510
511 // first of all, elimintate the variable
512 strcpy( pszStartPos, pszEndPos + 1);
513
514 // get value
515 if (!stricmp( szVarName, "beginlibpath"))
516 pszVarValue = _queryExtLIBPATH( szLibPath, BEGIN_LIBPATH);
517 else if (!stricmp( szVarName, "endlibpath"))
518 pszVarValue = _queryExtLIBPATH( szLibPath, END_LIBPATH);
519 else if (!stricmp( szVarName, ENV_NEPMD_ROOTDIR))
520 pszVarValue = pszRootDirValue;
521 else
522 pszVarValue = getenv( szVarName);
523
524 if (pszVarValue)
525 {
526 // embedd new value
527 pszNewResult = malloc( strlen( pszResult) + 1 + strlen( pszVarValue));
528 if (pszNewResult)
529 {
530 strcpy( pszNewResult, pszResult);
531 strcpy( pszNewResult + (pszStartPos - pszResult), pszVarValue);
532 strcat( pszNewResult, pszStartPos);
533 free( pszResult);
534 pszResult = pszNewResult;
535 }
536 else
537 {
538 // kick any result, as we are out of memory
539 free( pszResult);
540 pszResult = NULL;
541 break;
542 }
543 }
544 }
545
546 // next var please
547 pszStartPos = strchr( pszResult, chDelimiter);
548 }
549
550
551 } while (FALSE);
552
553return pszResult;
554}
555
556// -----------------------------------------------------------------------------
557
558static PSZ _copyname( PSZ pszBuffer, PSZ pszCurrent, PSZ pszEntry)
559{
560 BOOL fFound = FALSE;
561 PSZ p;
562 ULONG ulNameLen;
563 CHAR szName[ 128];
564
565if (!pszEntry)
566 return pszCurrent;
567
568// copy name to allow proper check with strcmp
569// (strncmp does not work properly here, finds LIB in LIBPATH etc)
570p = strchr( pszEntry, '=');
571if (!p)
572 return pszCurrent;
573
574ulNameLen = p - pszEntry;
575strncpy( szName, pszEntry, ulNameLen);
576szName[ ulNameLen] = 0;
577
578// check if name is already included
579p = pszBuffer;
580while (*p)
581 {
582 if (!strcmp( p, szName))
583 {
584 fFound = TRUE;
585 break;
586 }
587 p = NEXTSTR( p);
588 }
589
590if (fFound)
591 {
592 // move whole block and thus eliminate old entry
593// DPRINTF(( "EPMENV: Var moved: %s\n", pszEntry));
594 memcpy( p, p + ulNameLen + 1, pszCurrent - p);
595 pszCurrent -= ulNameLen + 1;
596 }
597else
598 {
599// DPRINTF(( "EPMENV: Var added: %s\n", pszEntry));
600 }
601
602// copy current name to the end
603strcpy( pszCurrent, szName);
604return NEXTSTR( pszCurrent);
605}
606
607// -----------------------------------------------------------------------------
608
609#define ADDVAR(e) {ulEnvSize += strlen( e) + 1; \
610 pszName = _copyname( pszEnvNameList, pszName, e); \
611 putenv( e);}
612#define ADDVARX(e) {*pulEnvSize += strlen( e) + 1; \
613 *ppszName = _copyname( pszEnvNameList, *ppszName, e); \
614 putenv( e);}
615
616// -----------------------------------------------------------------------------
617
618static APIRET _readEnvFile( PSZ szEnvFile, PULONG pulEnvSize, PSZ *ppszName, PSZ pszEnvNameList)
619{
620 APIRET rc = NO_ERROR;
621 ULONG i;
622
623 FILESTATUS3 fs3;
624 ULONG ulFileSize;
625 PSZ pszData = NULL;
626 HFILE hfile = NULLHANDLE;
627 ULONG ulAction;
628 ULONG ulBytesRead;
629
630static PSZ pszDelimiters = "\r\n";
631 PSZ pszLine;
632 PSZ pszCopyLine;
633 PSZ pszUpCopyLine;
634 PSZ p;
635 ULONG ulNameLen = 0;
636 PSZ pszNewLine = NULL;
637 PSZ pszName = NULL;
638 PSZ pszValue = NULL;
639 PSZ pszNewValue = NULL;
640
641do
642 {
643 // get memory
644 rc = DosQueryPathInfo( szEnvFile, FIL_STANDARD, &fs3, sizeof( fs3));
645 if (rc != NO_ERROR)
646 break;
647 ulFileSize = fs3.cbFile;
648 pszData = malloc( ulFileSize + 1);
649 if (!pszData)
650 {
651 rc = ERROR_NOT_ENOUGH_MEMORY;
652 break;
653 }
654 memset( pszData, 0, ulFileSize + 1);
655
656
657 // read file
658 rc = DosOpen( szEnvFile, &hfile, &ulAction, 0, 0,
659 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
660 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
661 NULL);
662 if (rc != NO_ERROR)
663 break;
664
665 rc = DosRead( hfile, pszData, ulFileSize, &ulBytesRead);
666 if (rc != NO_ERROR)
667 break;
668 if (ulFileSize != ulBytesRead)
669 {
670 rc = ERROR_READ_FAULT;
671 break;
672 }
673
674 // go through all lines
675 pszLine = strtok( pszData, pszDelimiters);
676 while (pszLine)
677 {
678 do
679 {
680 // skip line without equal sign: no env set here
681 if (!strchr( pszLine, '='))
682 break;
683
684 // skip comment lines
685 if (*pszLine == ':')
686 break;
687
688 // init vars
689 ulNameLen = 0;
690 pszNewLine = NULL;
691 //pszName = NULL;
692 pszValue = NULL;
693 pszNewValue = NULL;
694
695 // create copies to allow modification
696 pszCopyLine = strdup( pszLine);
697 pszUpCopyLine = strdup( pszLine);
698 strupr( pszUpCopyLine);
699
700 // make env var name uppercase, otherwise env var names
701 // containing lowercase chars won't work
702 p = strchr( pszLine, '=');
703 if (p)
704 {
705 ulNameLen = p - pszLine;
706 // pszCopyLine
707 strncpy( pszCopyLine, pszUpCopyLine, ulNameLen);
708 // pszName
709 pszName = malloc( ulNameLen + 1);
710 memset( pszName, '\0', sizeof( pszName));
711 strncat( pszName, pszUpCopyLine, ulNameLen);
712 // pszValue
713 pszValue = p + 1;
714 }
715
716 // Filter out BEGINLIBPATH and ENDLIBPATH pseudo env vars.
717 // Support also EPMBEGINLIBPATH and EPMENDLIBPATH for compatibilty.
718 // Each dir segment must exist, otherwise DosSetExtLIBPATH would
719 // set Begin/EndLIBPATH to NULL. If successful, it automatically
720 // appends a ';' if not present.
721 if (!strcmp( pszName, "BEGINLIBPATH") || !strcmp( pszName, "EPMBEGINLIBPATH"))
722 {
723 pszNewValue = ExpandEnvVar( pszValue);
724 DosSetExtLIBPATH( pszNewValue, BEGIN_LIBPATH);
725 }
726 else if (!strcmp( pszName, "ENDLIBPATH") || !strcmp( pszName, "EPMENDLIBPATH"))
727 {
728 pszNewValue = ExpandEnvVar( pszValue);
729 DosSetExtLIBPATH( pszNewValue, END_LIBPATH);
730 }
731 // Handle other env vars
732 else
733 {
734
735 // expand env vars
736 pszNewLine = ExpandEnvVar( pszCopyLine);
737
738 // add line to env
739 if (pszNewLine)
740 {
741 //DPRINTF(( "EPMENV: Added: %s\n", pszNewLine));
742 ADDVARX( pszNewLine);
743 }
744 else
745 DPRINTF(( "EPMENV: ERROR: Cannot expand \"%s\"\n", pszLine));
746 }
747
748 // cleanup copies
749 if (pszCopyLine) free( pszCopyLine);
750 if (pszUpCopyLine) free( pszUpCopyLine);
751 if (pszName) free( pszName);
752
753 } while (FALSE);
754
755
756 // next please
757 pszLine = strtok( NULL, pszDelimiters);
758 }
759
760 } while (FALSE);
761
762
763// cleanup
764if (hfile) DosClose( hfile);
765if (pszData) free( pszData);
766return rc;
767}
768
769// -----------------------------------------------------------------------------
770// Extends the environment with internal NEPMD vars and parsed contents (by
771// _readEnvFile) of the .env file(s). The EPM executable is searched after that
772// first extension of the environment. After that, its env var is set and the
773// environment is extended a second time.
774// The LIBPATH is extended via EPMBEGINLIBPATH and EPMENDLIBPATH env vars,
775// almost like supported by cmd.exe.
776// If pszBuffer was supplied, it will be set to the value of the EPM
777// executable.
778
779APIRET GetExtendedEPMEnvironment( PSZ envv[], PSZ *ppszNewEnv, PSZ pszBuffer, ULONG ulBuflen)
780{
781 APIRET rc = NO_ERROR;
782 ULONG i = 0;
783
784 BOOL fEnvAlreadySet = 0;
785 CHAR szMainEnvFile[ _MAX_PATH];
786 CHAR szAddEnvFile[ _MAX_PATH];
787
788 CHAR szEpmExecutable[ _MAX_PATH];
789 CHAR szLoaderExecutable[ _MAX_PATH];
790 CHAR *pszPathVar;
791
792 CHAR szInstallVar[ _MAX_PATH + 30];
793 PSZ apszVar[ 10]; // 7 vars are used - increase size of array if more vars required!
794
795 PSZ *ppszEnv;
796 PSZ pszVar;
797 PSZ pszName;
798 PSZ pszValue;
799
800 ULONG ulEnvSize;
801 PSZ pszEnvNameList = NULL;
802 PSZ pszEnv = NULL;
803
804
805do
806 {
807 // init vars
808 memset( apszVar, 0, sizeof( apszVar));
809
810 // check parms
811 if ((!ppszNewEnv) ||
812 (!envv))
813 {
814 rc = ERROR_INVALID_PARAMETER;
815 break;
816 }
817
818 // default to make no changes
819 *ppszNewEnv = NULL;
820
821 // check if extended environment is already set
822 pszValue = getenv( ENV_NEPMD_ADDENVFILE);
823 if ((!pszValue) || (!*pszValue))
824 pszValue = getenv( ENV_NEPMD_MAINENVFILE);
825 if ((pszValue) && (*pszValue))
826 {
827 //DPRINTF(( "EPMENV: Skip environment extension, already set with: %s\n", pszValue));
828 fEnvAlreadySet = 1;
829 // can't break here, because now pszBuffer is set to EpmExecutable later
830 //break;
831 }
832
833 // ------- get name list ----------------------------
834
835 // get size of envnames provided
836 // for simplicity use env size as we add vars anyway
837
838 ppszEnv = envv;
839 ulEnvSize = 0;
840 while (*ppszEnv)
841 {
842 ulEnvSize += strlen( *ppszEnv) + 1;
843 ppszEnv++;
844 }
845 ulEnvSize += 1;
846
847 // get memory for name list
848 pszEnvNameList = malloc( ulEnvSize);
849 if (!pszEnvNameList)
850 {
851 rc = ERROR_NOT_ENOUGH_MEMORY;
852 break;
853 }
854 memset( pszEnvNameList , 0x0, ulEnvSize);
855
856 ppszEnv = envv;
857 pszName = pszEnvNameList;
858 while (*ppszEnv)
859 {
860 // copy var
861 pszName = _copyname( pszEnvNameList, pszName, *ppszEnv);
862
863 // copy next var
864 ppszEnv++;
865 }
866
867 if (fEnvAlreadySet == 0)
868 {
869
870 // ------- search environment files -----------------
871
872 // ignore errors!
873 _searchNepmdEnvironmentFiles( szMainEnvFile, sizeof( szMainEnvFile),
874 szAddEnvFile, sizeof( szAddEnvFile));
875
876 // ------- set internal env vars --------------------
877
878 // set internal var(s) first, use a different buffer for each
879 // as putvar only passes a pointer to the environment list
880
881 // --- > set environment variable for NEPMD install directory
882 memset( szInstallVar, 0, sizeof( szInstallVar));
883 sprintf( szInstallVar, "%s=", ENV_NEPMD_ROOTDIR);
884 rc = QueryInstValue( NEPMD_INSTVALUE_ROOTDIR, _EOS( szInstallVar), _EOSSIZE( szInstallVar));
885 //DPRINTF(( "QueryInstValue for NEPMD_INSTVALUE_ROOTDIR: rc = %u\n", rc ));
886 if (rc == NO_ERROR)
887 {
888 apszVar[ i] = strdup( szInstallVar);
889 ADDVAR( apszVar[ i]);
890 i += 1;
891 }
892 else
893 // don't report error from here
894 //rc = NO_ERROR;
895 break;
896
897 // --- > set environment variable for user directory
898 memset( szInstallVar, 0, sizeof( szInstallVar));
899 sprintf( szInstallVar, "%s=", ENV_NEPMD_USERDIR);
900 rc = QueryInstValue( NEPMD_INSTVALUE_USERDIR, _EOS( szInstallVar), _EOSSIZE( szInstallVar));
901 //DPRINTF(( "QueryInstValue for NEPMD_INSTVALUE_USERDIR: rc = %u\n", rc ));
902 if (rc == NO_ERROR)
903 {
904 apszVar[ i] = strdup( szInstallVar);
905 ADDVAR( apszVar[ i]);
906 i += 1;
907 }
908 else
909 break;
910
911 // --- > set environment variable for NEPMD language
912 memset( szInstallVar, 0, sizeof( szInstallVar));
913 sprintf( szInstallVar, "%s=", ENV_NEPMD_LANGUAGE);
914 QueryInstValue( NEPMD_INSTVALUE_LANGUAGE, _EOS( szInstallVar), _EOSSIZE( szInstallVar));
915 apszVar[ i] = strdup( szInstallVar);
916 ADDVAR( apszVar[ i]);
917 i += 1;
918
919 // --- > set environment variables for env files
920 if (strlen( szMainEnvFile))
921 {
922 memset( szInstallVar, 0, sizeof( szInstallVar));
923 sprintf( szInstallVar, "%s=%s", ENV_NEPMD_MAINENVFILE, szMainEnvFile);
924 apszVar[ i] = strdup( szInstallVar);
925 ADDVAR( apszVar[ i]);
926 i += 1;
927 }
928
929 if (strlen( szAddEnvFile))
930 {
931 memset( szInstallVar, 0, sizeof( szInstallVar));
932 sprintf( szInstallVar, "%s=%s", ENV_NEPMD_ADDENVFILE, szAddEnvFile);
933 apszVar[ i] = strdup( szInstallVar);
934 ADDVAR( apszVar[ i]);
935 i += 1;
936 }
937
938 // ------- read env files, set env vars and Begin/EndLIBPATH ---------
939
940 if (strlen( szMainEnvFile)) _readEnvFile( szMainEnvFile, &ulEnvSize, &pszName, pszEnvNameList);
941 if (strlen( szAddEnvFile)) _readEnvFile( szAddEnvFile, &ulEnvSize, &pszName, pszEnvNameList);
942
943 } // fEnvAlreadySet == 0
944
945 // ------- search EPM executable and get loader -----
946
947 // search EPM executable after PATH was extended
948 // the loader executable is here not required, if it was processed already before
949 szEpmExecutable[ 0] = 0;
950 szLoaderExecutable[ 0] = 0;
951 rc = _searchEpmExecutable( szEpmExecutable, sizeof( szEpmExecutable),
952 szLoaderExecutable, sizeof( szLoaderExecutable));
953
954 // hand over name of executable, if buffer supplied
955 if (pszBuffer)
956 {
957 // if executable is requested, react on error
958 if (rc != NO_ERROR)
959 break;
960
961 if (strlen( szEpmExecutable) + 1 > ulBuflen)
962 {
963 rc = ERROR_BUFFER_OVERFLOW;
964 break;
965 }
966 //DPRINTF(( "EPMENV: Found executable: %s\n", szEpmExecutable));
967 strcpy( pszBuffer, szEpmExecutable);
968 }
969
970 // ------- set internal env vars --------------------
971
972 // --- > set NEPMD_EPMEXECUTABLE
973 memset( szInstallVar, 0, sizeof( szInstallVar));
974 sprintf( szInstallVar, "%s=%s", ENV_NEPMD_EPMEXECUTABLE, szEpmExecutable);
975 apszVar[ i] = strdup( szInstallVar);
976 ADDVAR( apszVar[ i]);
977 //DPRINTF(( "EPMENV: Added: %s\n", apszVar[ i]));
978 i += 1;
979
980 // --- > set NEPMD_LOADEREXECUTABLE
981 memset( szInstallVar, 0, sizeof( szInstallVar));
982 sprintf( szInstallVar, "%s=%s", ENV_NEPMD_LOADEREXECUTABLE, szLoaderExecutable);
983 apszVar[ i] = strdup( szInstallVar);
984 ADDVAR( apszVar[ i]);
985 //DPRINTF(( "EPMENV: Added: %s\n", apszVar[ i]));
986 i += 1;
987
988 // ------- set environment --------------------------
989
990 pszEnv = malloc( ulEnvSize);
991 if (!pszEnv)
992 {
993 rc = ERROR_NOT_ENOUGH_MEMORY;
994 //DPRINTF(( "EPMENV: ERROR: Out of memory, rc = %u\n", rc));
995 break;
996 }
997 memset( pszEnv, 0x0, ulEnvSize);
998
999 // read env into memory block
1000 pszName = pszEnvNameList;
1001 pszVar = pszEnv;
1002 while (*pszName)
1003 {
1004 // copy var
1005 sprintf( pszVar, "%s=%s", pszName, getenv( pszName));
1006 //DPRINTF(( "EPMENV: Read into mem, pszVar = %s\n", pszVar));
1007
1008 // copy next var
1009 pszVar = NEXTSTR( pszVar);
1010 pszName = NEXTSTR( pszName);
1011 }
1012
1013 //for (i = 0; i < (sizeof( apszVar) / sizeof( PSZ)); i++)
1014 // {
1015 // if (apszVar[ i])
1016 // DPRINTF(( "EPMENV: %u: %s\n", i, apszVar[ i]));
1017 // }
1018
1019 // close name list
1020 *pszName = 0;
1021
1022 *pszVar = 0;
1023
1024 // hand over result
1025 *ppszNewEnv = pszEnv;
1026
1027 } while (FALSE);
1028
1029
1030// cleanup on error
1031if (rc)
1032 if (pszEnv) free( pszEnv);
1033
1034// cleanup
1035if (pszEnvNameList) free( pszEnvNameList);
1036for (i = 0; i < (sizeof( apszVar) / sizeof( PSZ)); i++)
1037 {
1038 if (apszVar[ i]) free( apszVar[ i]);
1039 }
1040return rc;
1041}
1042
Note: See TracBrowser for help on using the repository browser.