source: trunk/src/helpers/apps.c@ 103

Last change on this file since 103 was 101, checked in by umoeller, 24 years ago

Misc updates.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 36.4 KB
Line 
1
2/*
3 *@@sourcefile apps.c:
4 * contains program helpers (environments, application start).
5 *
6 * This file is new with V0.9.12 and contains functions
7 * previously in winh.c and dosh2.c.
8 *
9 * Note: Version numbering in this file relates to XWorkplace version
10 * numbering.
11 *
12 *@@header "helpers\apps.h"
13 *@@added V0.9.12 (2001-05-26) [umoeller]
14 */
15
16/*
17 * Copyright (C) 1997-2001 Ulrich M”ller.
18 * This file is part of the "XWorkplace helpers" source package.
19 * This is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published
21 * by the Free Software Foundation, in version 2 as it comes in the
22 * "COPYING" file of the XWorkplace main distribution.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 */
28
29#define OS2EMX_PLAIN_CHAR
30 // this is needed for "os2emx.h"; if this is defined,
31 // emx will define PSZ as _signed_ char, otherwise
32 // as unsigned char
33
34#define INCL_DOSPROCESS
35#define INCL_DOSSESMGR
36#define INCL_DOSERRORS
37
38#define INCL_WINPROGRAMLIST // needed for PROGDETAILS, wppgm.h
39#include <os2.h>
40
41#include <stdio.h>
42
43#include "setup.h" // code generation and debugging options
44
45#include "helpers\apps.h"
46#include "helpers\dosh.h"
47#include "helpers\prfh.h"
48#include "helpers\stringh.h"
49#include "helpers\xstring.h"
50
51/*
52 *@@category: Helpers\PM helpers\Application helpers
53 */
54
55/* ******************************************************************
56 *
57 * Environment helpers
58 *
59 ********************************************************************/
60
61/*
62 *@@ appParseEnvironment:
63 * this takes one of those ugly environment strings
64 * as used by DosStartSession and WinStartApp (with
65 * lots of zero-terminated strings one after another
66 * and a duplicate zero byte as a terminator) as
67 * input and splits it into an array of separate
68 * strings in pEnv.
69 *
70 * The newly allocated strings are stored in in
71 * pEnv->papszVars. The array count is stored in
72 * pEnv->cVars.
73 *
74 * Each environment variable will be copied into
75 * one newly allocated string in the array. Use
76 * appFreeEnvironment to free the memory allocated
77 * by this function.
78 *
79 * Use the following code to browse thru the array:
80 +
81 + DOSENVIRONMENT Env = {0};
82 + if (appParseEnvironment(pszEnv,
83 + &Env)
84 + == NO_ERROR)
85 + {
86 + if (Env.papszVars)
87 + {
88 + PSZ *ppszThis = Env.papszVars;
89 + for (ul = 0;
90 + ul < Env.cVars;
91 + ul++)
92 + {
93 + PSZ pszThis = *ppszThis;
94 + // pszThis now has something like PATH=C:\TEMP
95 + // ...
96 + // next environment string
97 + ppszThis++;
98 + }
99 + }
100 + appFreeEnvironment(&Env);
101 + }
102 *
103 *@@added V0.9.4 (2000-08-02) [umoeller]
104 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
105 */
106
107APIRET appParseEnvironment(const char *pcszEnv,
108 PDOSENVIRONMENT pEnv)
109{
110 APIRET arc = NO_ERROR;
111 if (!pcszEnv)
112 arc = ERROR_INVALID_PARAMETER;
113 else
114 {
115 PSZ pszVarThis = (PSZ)pcszEnv;
116 ULONG cVars = 0;
117 // count strings
118 while (*pszVarThis)
119 {
120 cVars++;
121 pszVarThis += strlen(pszVarThis) + 1;
122 }
123
124 pEnv->cVars = cVars;
125 pEnv->papszVars = 0;
126
127 if (cVars)
128 {
129 PSZ *papsz = (PSZ*)malloc(sizeof(PSZ) * cVars);
130 if (!papsz)
131 arc = ERROR_NOT_ENOUGH_MEMORY;
132 else
133 {
134 PSZ *ppszTarget = papsz;
135 memset(papsz, 0, sizeof(PSZ) * cVars);
136 pszVarThis = (PSZ)pcszEnv;
137 while (*pszVarThis)
138 {
139 *ppszTarget = strdup(pszVarThis);
140 ppszTarget++;
141 pszVarThis += strlen(pszVarThis) + 1;
142 }
143
144 pEnv->papszVars = papsz;
145 }
146 }
147 }
148
149 return (arc);
150}
151
152/*
153 *@@ appGetEnvironment:
154 * calls appParseEnvironment for the current
155 * process environment, which is retrieved from
156 * the info blocks.
157 *
158 *@@added V0.9.4 (2000-07-19) [umoeller]
159 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
160 */
161
162APIRET appGetEnvironment(PDOSENVIRONMENT pEnv)
163{
164 APIRET arc = NO_ERROR;
165 if (!pEnv)
166 arc = ERROR_INVALID_PARAMETER;
167 else
168 {
169 PTIB ptib = 0;
170 PPIB ppib = 0;
171 arc = DosGetInfoBlocks(&ptib, &ppib);
172 if (arc == NO_ERROR)
173 {
174 PSZ pszEnv = ppib->pib_pchenv;
175 if (pszEnv)
176 arc = appParseEnvironment(pszEnv, pEnv);
177 else
178 arc = ERROR_BAD_ENVIRONMENT;
179 }
180 }
181
182 return (arc);
183}
184
185/*
186 *@@ appFindEnvironmentVar:
187 * returns the PSZ* in the pEnv->papszVars array
188 * which specifies the environment variable in pszVarName.
189 *
190 * With pszVarName, you can either specify the variable
191 * name only ("VARNAME") or a full environment string
192 * ("VARNAME=BLAH"). In any case, only the variable name
193 * is compared.
194 *
195 * Returns NULL if no such variable name was found in
196 * the array.
197 *
198 *@@added V0.9.4 (2000-07-19) [umoeller]
199 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
200 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
201 */
202
203PSZ* appFindEnvironmentVar(PDOSENVIRONMENT pEnv,
204 PSZ pszVarName)
205{
206 PSZ *ppszRet = 0;
207 if (pEnv)
208 {
209 if ((pEnv->papszVars) && (pszVarName))
210 {
211 PSZ *ppszThis = pEnv->papszVars;
212 // PSZ pszThis;
213 ULONG ul = 0;
214 ULONG ulVarNameLen = 0;
215
216 PSZ pszSearch = NULL; // receives "VARNAME="
217 PSZ pFirstEqual = strchr(pszVarName, '=');
218 if (pFirstEqual)
219 pszSearch = strhSubstr(pszVarName, pFirstEqual + 1);
220 else
221 {
222 ulVarNameLen = strlen(pszVarName);
223 pszSearch = (PSZ)malloc(ulVarNameLen + 2);
224 memcpy(pszSearch, pszVarName, ulVarNameLen);
225 *(pszSearch + ulVarNameLen) = '=';
226 *(pszSearch + ulVarNameLen + 1) = 0;
227 }
228
229 ulVarNameLen = strlen(pszSearch);
230
231 for (ul = 0;
232 ul < pEnv->cVars;
233 ul++)
234 {
235 if (strnicmp(*ppszThis, pszSearch, ulVarNameLen) == 0)
236 {
237 ppszRet = ppszThis;
238 break;
239 }
240
241 // next environment string
242 ppszThis++;
243 }
244
245 free(pszSearch); // was missing V0.9.12 (2001-05-21) [umoeller]
246 }
247 }
248
249 return (ppszRet);
250}
251
252/*
253 *@@ appSetEnvironmentVar:
254 * sets an environment variable in the specified
255 * environment, which must have been initialized
256 * using appGetEnvironment first.
257 *
258 * pszNewEnv must be a full environment string
259 * in the form "VARNAME=VALUE".
260 *
261 * If "VARNAME" has already been set to something
262 * in the string array in pEnv, that array item
263 * is replaced.
264 *
265 * OTOH, if "VARNAME" has not been set yet, a new
266 * item is added to the array, and pEnv->cVars is
267 * raised by one. In that case, fAddFirst determines
268 * whether the new array item is added to the front
269 * or the tail of the environment list.
270 *
271 *@@added V0.9.4 (2000-07-19) [umoeller]
272 *@@changed V0.9.7 (2000-12-17) [umoeller]: added fAddFirst
273 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
274 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed crash if !fAddFirst
275 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
276 */
277
278APIRET appSetEnvironmentVar(PDOSENVIRONMENT pEnv,
279 PSZ pszNewEnv,
280 BOOL fAddFirst)
281{
282 APIRET arc = NO_ERROR;
283 if ((!pEnv) || (!pszNewEnv))
284 arc = ERROR_INVALID_PARAMETER;
285 else
286 {
287 if (!pEnv->papszVars)
288 {
289 // no variables set yet:
290 pEnv->papszVars = (PSZ*)malloc(sizeof(PSZ));
291 pEnv->cVars = 1;
292
293 *(pEnv->papszVars) = strdup(pszNewEnv);
294 }
295 else
296 {
297 PSZ *ppszEnvLine = appFindEnvironmentVar(pEnv, pszNewEnv);
298 if (ppszEnvLine)
299 {
300 // was set already: replace
301 free(*ppszEnvLine);
302 *ppszEnvLine = strdup(pszNewEnv);
303 if (!(*ppszEnvLine))
304 arc = ERROR_NOT_ENOUGH_MEMORY;
305 }
306 else
307 {
308 // not set already:
309 PSZ *ppszNew = NULL;
310
311 // allocate new array, with one new entry
312 // fixed V0.9.12 (2001-05-26) [umoeller], this crashed
313 PSZ *papszNew = (PSZ*)malloc(sizeof(PSZ) * (pEnv->cVars + 1));
314
315 if (!papszNew)
316 arc = ERROR_NOT_ENOUGH_MEMORY;
317 else
318 {
319 if (fAddFirst)
320 {
321 // add as first entry:
322 // overwrite first entry
323 ppszNew = papszNew;
324 // copy old entries
325 memcpy(papszNew + 1, // second new entry
326 pEnv->papszVars, // first old entry
327 sizeof(PSZ) * pEnv->cVars);
328 }
329 else
330 {
331 // append at the tail:
332 // overwrite last entry
333 ppszNew = papszNew + pEnv->cVars;
334 // copy old entries
335 memcpy(papszNew, // first new entry
336 pEnv->papszVars, // first old entry
337 sizeof(PSZ) * pEnv->cVars);
338 }
339
340 if (pEnv->papszVars)
341 free(pEnv->papszVars); // was missing V0.9.12 (2001-05-21) [umoeller]
342
343 pEnv->papszVars = papszNew;
344 pEnv->cVars++;
345 *ppszNew = strdup(pszNewEnv);
346 }
347 }
348 }
349 }
350
351 return (arc);
352}
353
354/*
355 *@@ appConvertEnvironment:
356 * converts an environment initialized by appGetEnvironment
357 * to the string format required by WinStartApp and DosExecPgm,
358 * that is, one memory block is allocated in *ppszEnv and all
359 * strings in pEnv->papszVars are copied to that block. Each
360 * string is terminated with a null character; the last string
361 * is terminated with two null characters.
362 *
363 * Use free() to free the memory block allocated by this
364 * function in *ppszEnv.
365 *
366 *@@added V0.9.4 (2000-07-19) [umoeller]
367 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
368 */
369
370APIRET appConvertEnvironment(PDOSENVIRONMENT pEnv,
371 PSZ *ppszEnv, // out: environment string
372 PULONG pulSize) // out: size of block allocated in *ppszEnv; ptr can be NULL
373{
374 APIRET arc = NO_ERROR;
375 if (!pEnv)
376 arc = ERROR_INVALID_PARAMETER;
377 else
378 {
379 if (!pEnv->papszVars)
380 arc = ERROR_INVALID_PARAMETER;
381 else
382 {
383 // count memory needed for all strings
384 ULONG cbNeeded = 0,
385 ul = 0;
386 PSZ *ppszThis = pEnv->papszVars;
387
388 for (ul = 0;
389 ul < pEnv->cVars;
390 ul++)
391 {
392 cbNeeded += strlen(*ppszThis) + 1; // length of string plus null terminator
393
394 // next environment string
395 ppszThis++;
396 }
397
398 cbNeeded++; // for another null terminator
399
400 *ppszEnv = (PSZ)malloc(cbNeeded);
401 if (!(*ppszEnv))
402 arc = ERROR_NOT_ENOUGH_MEMORY;
403 else
404 {
405 PSZ pTarget = *ppszEnv;
406 if (pulSize)
407 *pulSize = cbNeeded;
408 ppszThis = pEnv->papszVars;
409
410 // now copy each string
411 for (ul = 0;
412 ul < pEnv->cVars;
413 ul++)
414 {
415 PSZ pSource = *ppszThis;
416
417 while ((*pTarget++ = *pSource++))
418 ;
419
420 // *pTarget++ = 0; // append null terminator per string
421
422 // next environment string
423 ppszThis++;
424 }
425
426 *pTarget++ = 0; // append second null terminator
427 }
428 }
429 }
430
431 return (arc);
432}
433
434/*
435 *@@ appFreeEnvironment:
436 * frees memory allocated by appGetEnvironment.
437 *
438 *@@added V0.9.4 (2000-07-19) [umoeller]
439 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
440 */
441
442APIRET appFreeEnvironment(PDOSENVIRONMENT pEnv)
443{
444 APIRET arc = NO_ERROR;
445 if (!pEnv)
446 arc = ERROR_INVALID_PARAMETER;
447 else
448 {
449 if (!pEnv->papszVars)
450 arc = ERROR_INVALID_PARAMETER;
451 else
452 {
453 PSZ *ppszThis = pEnv->papszVars;
454 PSZ pszThis;
455 ULONG ul = 0;
456
457 for (ul = 0;
458 ul < pEnv->cVars;
459 ul++)
460 {
461 pszThis = *ppszThis;
462 free(pszThis);
463 // *ppszThis = NULL;
464 // next environment string
465 ppszThis++;
466 }
467
468 free(pEnv->papszVars);
469 pEnv->cVars = 0;
470 }
471 }
472
473 return (arc);
474}
475
476/* ******************************************************************
477 *
478 * Application start
479 *
480 ********************************************************************/
481
482/*
483 *@@ CallBatchCorrectly:
484 * fixes the specified PROGDETAILS for
485 * command files in the executable part
486 * by inserting /C XXX into the parameters
487 * and setting the executable according
488 * to an environment variable.
489 *
490 *@@added V0.9.6 (2000-10-16) [umoeller]
491 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING
492 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
493 */
494
495VOID CallBatchCorrectly(PPROGDETAILS pProgDetails,
496 PXSTRING pstrParams, // in/out: modified parameters (reallocated)
497 const char *pcszEnvVar, // in: env var spec'g command proc
498 // (e.g. "OS2_SHELL"); can be NULL
499 const char *pcszDefProc) // in: def't command proc (e.g. "CMD.EXE")
500{
501 // XXX.CMD file as executable:
502 // fix args to /C XXX.CMD
503
504 PSZ pszOldParams = NULL;
505 ULONG ulOldParamsLength = pstrParams->ulLength;
506 if (ulOldParamsLength)
507 // we have parameters already:
508 // make a backup... we'll append that later
509 pszOldParams = strdup(pstrParams->psz);
510
511 // set new params to "/C filename.cmd"
512 xstrcpy(pstrParams, "/C ", 0);
513 xstrcat(pstrParams,
514 pProgDetails->pszExecutable,
515 0);
516
517 if (pszOldParams)
518 {
519 // .cmd had params:
520 // append space and old params
521 xstrcatc(pstrParams, ' ');
522 xstrcat(pstrParams,
523 pszOldParams,
524 ulOldParamsLength);
525 free(pszOldParams);
526 }
527
528 // set executable to $(OS2_SHELL)
529 pProgDetails->pszExecutable = NULL;
530 if (pcszEnvVar)
531 pProgDetails->pszExecutable = getenv(pcszEnvVar);
532 if (!pProgDetails->pszExecutable)
533 pProgDetails->pszExecutable = (PSZ)pcszDefProc;
534 // should be on PATH
535}
536
537/*
538 *@@ appQueryAppType:
539 * returns the Control Program (Dos) and
540 * Win* PROG_* application types for the
541 * specified executable. Essentially, this
542 * is a wrapper around DosQueryAppType.
543 *
544 * pcszExecutable must be fully qualified.
545 * You can use doshFindExecutable to qualify
546 * it.
547 *
548 * This returns the APIRET of DosQueryAppType.
549 * If this is NO_ERROR; *pulDosAppType receives
550 * the app type of DosQueryAppType. In addition,
551 * *pulWinAppType is set to one of the following:
552 *
553 * -- PROG_FULLSCREEN
554 *
555 * -- PROG_PDD
556 *
557 * -- PROG_VDD
558 *
559 * -- PROG_XWP_DLL: new apptype defined in winh.h for
560 * dynamic link libraries.
561 *
562 * -- PROG_WINDOWEDVDM
563 *
564 * -- PROG_PM
565 *
566 * -- PROG_31_ENHSEAMLESSCOMMON
567 *
568 * -- PROG_WINDOWABLEVIO
569 *
570 *@@added V0.9.9 (2001-03-07) [umoeller]
571 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
572 *@@changed V0.9.14 (2001-08-07) [pr]: use FAPPTYP_* constants
573 */
574
575APIRET appQueryAppType(const char *pcszExecutable,
576 PULONG pulDosAppType,
577 PULONG pulWinAppType)
578{
579 APIRET arc = DosQueryAppType((PSZ)pcszExecutable, pulDosAppType);
580 if (arc == NO_ERROR)
581 {
582 ULONG _ulDosAppType = *pulDosAppType;
583
584 if (_ulDosAppType == 0)
585 *pulWinAppType = PROG_FULLSCREEN;
586 else if (_ulDosAppType & FAPPTYP_PHYSDRV) // 0x40
587 *pulWinAppType = PROG_PDD;
588 else if (_ulDosAppType & FAPPTYP_VIRTDRV) // 0x80)
589 *pulWinAppType = PROG_VDD;
590 else if ((_ulDosAppType & 0xF0) == FAPPTYP_DLL) // 0x10)
591 // DLL bit set
592 *pulWinAppType = PROG_XWP_DLL;
593 else if (_ulDosAppType & FAPPTYP_DOS) // 0x20)
594 // DOS bit set?
595 *pulWinAppType = PROG_WINDOWEDVDM;
596 else if ((_ulDosAppType & FAPPTYP_WINDOWAPI) == FAPPTYP_WINDOWAPI) // 0x0003) // "Window-API" == PM
597 *pulWinAppType = PROG_PM;
598 else if ( ((_ulDosAppType & 0xFFFF) == FAPPTYP_WINDOWSPROT31) // 0x1000) // windows program (?!?)
599 || ((_ulDosAppType & 0xFFFF) == FAPPTYP_WINDOWSPROT) // ) // windows program (?!?)
600 )
601 *pulWinAppType = PROG_31_ENHSEAMLESSCOMMON; // PROG_31_ENH;
602 // *pulWinAppType = PROG_31_ENHSEAMLESSVDM;
603 else if ((_ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_WINDOWCOMPAT) // 0x02)
604 *pulWinAppType = PROG_WINDOWABLEVIO;
605 else if ((_ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_NOTWINDOWCOMPAT) // 0x01)
606 *pulWinAppType = PROG_FULLSCREEN;
607 }
608
609 return (arc);
610}
611
612/*
613 *@@ appIsWindowsApp:
614 * checks the specified program category
615 * (PROGDETAILS.progt.progc) for whether
616 * it represents a Win-OS/2 application.
617 *
618 * Returns:
619 *
620 * -- 0: no windows app (it's VIO, OS/2
621 * or DOS fullscreen, or PM).
622 *
623 * -- 1: Win-OS/2 standard app.
624 *
625 * -- 2: Win-OS/2 enhanced-mode app.
626 *
627 *@@added V0.9.12 (2001-05-26) [umoeller]
628 */
629
630ULONG appIsWindowsApp(ULONG ulProgCategory)
631{
632 switch (ulProgCategory)
633 {
634 case PROG_31_ENHSEAMLESSVDM: // 17
635 case PROG_31_ENHSEAMLESSCOMMON: // 18
636 case PROG_31_ENH: // 19
637 return (2);
638
639#ifndef PROG_30_STD
640 #define PROG_30_STD (PROGCATEGORY)11
641#endif
642
643#ifndef PROG_30_STDSEAMLESSVDM
644 #define PROG_30_STDSEAMLESSVDM (PROGCATEGORY)13
645#endif
646
647 case PROG_WINDOW_REAL: // 10
648 case PROG_30_STD: // 11
649 case PROG_WINDOW_AUTO: // 12
650 case PROG_30_STDSEAMLESSVDM: // 13
651 case PROG_30_STDSEAMLESSCOMMON: // 14
652 case PROG_31_STDSEAMLESSVDM: // 15
653 case PROG_31_STDSEAMLESSCOMMON: // 16
654 case PROG_31_STD: // 20
655 return (1);
656 }
657
658 return (0);
659}
660
661/*
662 *@@ appQueryDefaultWin31Environment:
663 * returns the default Win-OS/2 3.1 environment
664 * from OS2.INI, which you can then merge with
665 * your process environment to be able to
666 * start Win-OS/2 sessions properly with
667 * appStartApp.
668 *
669 * Caller must free() the return value.
670 *
671 *@@added V0.9.12 (2001-05-26) [umoeller]
672 */
673
674PSZ appQueryDefaultWin31Environment(VOID)
675{
676 PSZ pszReturn = NULL;
677 ULONG ulSize = 0;
678 // get default environment (from Win-OS/2 settings object)
679 // from OS2.INI
680 PSZ pszDefEnv = prfhQueryProfileData(HINI_USER,
681 "WINOS2",
682 "PM_GlobalWindows31Settings",
683 &ulSize);
684 if (pszDefEnv)
685 {
686 if (pszReturn = (PSZ)malloc(ulSize + 2))
687 {
688 PSZ p;
689 memset(pszReturn, 0, ulSize + 2);
690 memcpy(pszReturn, pszDefEnv, ulSize);
691
692 for (p = pszReturn;
693 p < pszReturn + ulSize;
694 p++)
695 if (*p == ';')
696 *p = 0;
697
698 // okay.... now we got an OS/2-style environment
699 // with 0, 0, 00 strings
700 }
701
702 free(pszDefEnv);
703 }
704
705 return (pszReturn);
706}
707
708/*
709 *@@ appStartApp:
710 * wrapper around WinStartApp which fixes the
711 * specified PROGDETAILS to (hopefully) work
712 * work with all executable types.
713 *
714 * This fixes the executable info to support:
715 *
716 * -- starting "*" executables (command prompts
717 * for OS/2, DOS, Win-OS/2);
718 *
719 * -- starting ".CMD" and ".BAT" files as
720 * PROGDETAILS.pszExecutable.
721 *
722 * Unless it is "*", PROGDETAILS.pszExecutable must
723 * be a proper file name. The full path may be omitted
724 * if it is on the PATH, but the extension (.EXE etc.)
725 * must be given. You can use doshFindExecutable to
726 * find executables if you don't know the extension.
727 *
728 * This also handles and merges special and default
729 * environments for the app to be started. The
730 * following should be respected:
731 *
732 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
733 * is NULL, the new app inherits a default environment
734 * from the shell.
735 *
736 + -- However, if you specify an environment, you _must_
737 * specify a complemete environment. This function
738 * will not merge environments. Use
739 * appSetEnvironmentVar to change environment
740 * variables in a complete environment set.
741 *
742 * -- If PROGDETAILS specifies a Win-OS/2 session
743 * and PROGDETAILS.pszEnvironment is empty,
744 * this uses the default Win-OS/2 environment.
745 * See appQueryDefaultWin31Environment.
746 *
747 * Even though this isn't clearly said in PMREF,
748 * PROGDETAILS.swpInitial is important:
749 *
750 * -- To start a session minimized, set SWP_MINIMIZE.
751 *
752 * -- To start a VIO session auto-close disabled, set
753 * the half-documented SWP_NOAUTOCLOSE flag (0x8000)
754 * This flag is now in the newer toolkit headers.
755 *
756 * Since this calls WinStartApp in turn, this
757 * requires a message queue on the calling thread.
758 *
759 *@@added V0.9.6 (2000-10-16) [umoeller]
760 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
761 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
762 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
763 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
764 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
765 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
766 *@@changed V0.9.14 (2001-08-23) [pr]: added session type options
767 */
768
769HAPP appStartApp(HWND hwndNotify, // in: notify window (as with WinStartApp)
770 const PROGDETAILS *pcProgDetails, // in: program data
771 ULONG ulFlags) // in: session type options
772{
773 HAPP happ = NULLHANDLE;
774 XSTRING strParamsPatched;
775 PROGDETAILS ProgDetails;
776
777 PSZ pszWinOS2Env = 0;
778
779 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
780 // pointers still point into old prog details buffer
781 ProgDetails.Length = sizeof(PROGDETAILS);
782 ProgDetails.progt.fbVisible = SHE_VISIBLE;
783 // ProgDetails.pszEnvironment = 0;
784
785 // all this only makes sense if this contains something...
786 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
787 if (ProgDetails.pszExecutable)
788 {
789 ULONG ulIsWinApp;
790
791 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
792 // this wasn't a good idea... WPProgram stores stuff
793 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
794
795 // duplicate parameters...
796 // we need this for string manipulations below...
797 if (ProgDetails.pszParameters)
798 xstrInitCopy(&strParamsPatched,
799 ProgDetails.pszParameters,
800 100);
801 else
802 // no old params:
803 xstrInit(&strParamsPatched, 100);
804
805 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
806 // _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
807 // _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
808
809 // program type fixups
810 switch (ProgDetails.progt.progc) // that's a ULONG
811 {
812 case ((ULONG)-1): // we get that sometimes...
813 case PROG_DEFAULT:
814 {
815 // V0.9.12 (2001-05-26) [umoeller]
816 ULONG ulDosAppType;
817 appQueryAppType(ProgDetails.pszExecutable,
818 &ulDosAppType,
819 &ProgDetails.progt.progc);
820 }
821 break;
822 }
823
824 // Set session type from option flags
825 if (ulFlags & APP_RUN_FULLSCREEN)
826 {
827 if (ProgDetails.progt.progc == PROG_WINDOWABLEVIO)
828 ProgDetails.progt.progc = PROG_FULLSCREEN;
829
830 if (ProgDetails.progt.progc == PROG_WINDOWEDVDM)
831 ProgDetails.progt.progc = PROG_VDM;
832 }
833
834 if (ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc))
835 {
836 if (ulFlags & APP_RUN_FULLSCREEN)
837 ProgDetails.progt.progc = (ulFlags & APP_RUN_ENHANCED)
838 ? PROG_31_ENH
839 : PROG_31_STD;
840 else
841 {
842 if (ulFlags & APP_RUN_STANDARD)
843 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
844 ? PROG_31_STDSEAMLESSVDM
845 : PROG_31_STDSEAMLESSCOMMON;
846
847 if (ulFlags & APP_RUN_ENHANCED)
848 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
849 ? PROG_31_ENHSEAMLESSVDM
850 : PROG_31_ENHSEAMLESSCOMMON;
851 }
852 }
853
854 /*
855 * command lines fixups:
856 *
857 */
858
859 if (!strcmp(ProgDetails.pszExecutable, "*"))
860 {
861 /*
862 * "*" for command sessions:
863 *
864 */
865
866 if (ulIsWinApp == 2)
867 {
868 // enhanced Win-OS/2 session:
869 PSZ psz = NULL;
870 if (strParamsPatched.ulLength)
871 // "/3 " + existing params
872 psz = strdup(strParamsPatched.psz);
873
874 xstrcpy(&strParamsPatched, "/3 ", 0);
875
876 if (psz)
877 {
878 xstrcat(&strParamsPatched, psz, 0);
879 free(psz);
880 }
881 }
882
883 if (ulIsWinApp)
884 {
885 // cheat: WinStartApp doesn't support NULL
886 // for Win-OS2 sessions, so manually start winos2.com
887 ProgDetails.pszExecutable = "WINOS2.COM";
888 // this is a DOS app, so fix this to DOS fullscreen
889 ProgDetails.progt.progc = PROG_VDM;
890 }
891 else
892 // for all other executable types
893 // (including OS/2 and DOS sessions),
894 // set pszExecutable to NULL; this will
895 // have WinStartApp start a cmd shell
896 ProgDetails.pszExecutable = NULL;
897
898 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
899 else
900 {
901 PSZ pszExtension;
902 switch (ProgDetails.progt.progc)
903 {
904 /*
905 * .CMD files fixups
906 *
907 */
908
909 case PROG_FULLSCREEN: // OS/2 fullscreen
910 case PROG_WINDOWABLEVIO: // OS/2 window
911 {
912 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
913 && (!stricmp(pszExtension, "CMD"))
914 )
915 {
916 CallBatchCorrectly(&ProgDetails,
917 &strParamsPatched,
918 "OS2_SHELL",
919 "CMD.EXE");
920 }
921 break; }
922
923 case PROG_VDM: // DOS fullscreen
924 case PROG_WINDOWEDVDM: // DOS window
925 {
926 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
927 && (!stricmp(pszExtension, "BAT"))
928 )
929 {
930 CallBatchCorrectly(&ProgDetails,
931 &strParamsPatched,
932 NULL,
933 "COMMAND.COM");
934 }
935 break; }
936 } // end switch (ProgDetails.progt.progc)
937 }
938
939 if ( (ulIsWinApp)
940 && ( (ProgDetails.pszEnvironment == NULL)
941 || (!strlen(ProgDetails.pszEnvironment))
942 )
943 )
944 {
945 // this is a windoze app, and caller didn't bother
946 // to give us an environment:
947 // we MUST set one then, or we'll get the strangest
948 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
949
950 DOSENVIRONMENT Env = {0};
951
952 // get standard WIN-OS/2 environment
953 PSZ pszTemp = appQueryDefaultWin31Environment();
954
955 if (!appParseEnvironment(pszTemp,
956 &Env))
957 {
958 // now override KBD_CTRL_BYPASS=CTRL_ESC
959 appSetEnvironmentVar(&Env,
960 "KBD_CTRL_BYPASS=CTRL_ESC",
961 FALSE); // add last
962 /*
963 * These should be set by the default environment. It is
964 * not our business to override them really. V0.9.14
965 *
966 appSetEnvironmentVar(&Env,
967 "KBD_ALTHOME_BYPASS=1",
968 FALSE); // add last
969 appSetEnvironmentVar(&Env,
970 "INT_DURING_IO=1",
971 FALSE); // add last
972 appSetEnvironmentVar(&Env,
973 "HW_TIMER=1",
974 FALSE); // add last
975 appSetEnvironmentVar(&Env,
976 "MOUSE_EXCLUSIVE_ACCESS=0",
977 FALSE); // add last
978 appSetEnvironmentVar(&Env,
979 "VIDEO_RETRACE_EMULATION=1",
980 FALSE); // add last
981 appSetEnvironmentVar(&Env,
982 "VIDEO_SWITCH_NOTIFICATION=1",
983 FALSE); // add last
984 appSetEnvironmentVar(&Env,
985 "VIDEO_8514A_XGA_IOTRAP=0",
986 FALSE); // add last
987 */
988
989 if (!appConvertEnvironment(&Env,
990 &pszWinOS2Env, // freed at bottom
991 NULL))
992 ProgDetails.pszEnvironment = pszWinOS2Env;
993
994 appFreeEnvironment(&Env);
995 }
996
997 free(pszTemp);
998 }
999
1000 if (!ProgDetails.pszTitle)
1001 ProgDetails.pszTitle = ProgDetails.pszExecutable;
1002
1003 ProgDetails.pszParameters = strParamsPatched.psz;
1004
1005 /* _Pmpf(("progt.progc: %d", ProgDetails.progt.progc));
1006 _Pmpf(("progt.fbVisible: 0x%lX", ProgDetails.progt.fbVisible));
1007 _Pmpf(("progt.pszTitle: \"%s\"", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : "NULL"));
1008 _Pmpf(("exec: \"%s\"", (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL"));
1009 _Pmpf(("params: \"%s\"", (ProgDetails.pszParameters) ? ProgDetails.pszParameters : "NULL"));
1010 _Pmpf(("startup: \"%s\"", (ProgDetails.pszStartupDir) ? ProgDetails.pszStartupDir : "NULL"));
1011 _Pmpf(("pszIcon: \"%s\"", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : "NULL"));
1012 _Pmpf(("environment: "));
1013 {
1014 PSZ pszThis = ProgDetails.pszEnvironment;
1015 while (pszThis && *pszThis)
1016 {
1017 _Pmpf((" \"%s\"", pszThis));
1018 pszThis += strlen(pszThis) + 1;
1019 }
1020 }
1021
1022 _Pmpf(("swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
1023 ProgDetails.swpInitial.fl,
1024 ProgDetails.swpInitial.x,
1025 ProgDetails.swpInitial.y,
1026 ProgDetails.swpInitial.cx,
1027 ProgDetails.swpInitial.cy));
1028 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
1029 ProgDetails.swpInitial.hwndInsertBehind,
1030 ProgDetails.swpInitial.hwnd,
1031 ProgDetails.swpInitial.ulReserved1,
1032 ProgDetails.swpInitial.ulReserved2));
1033 */
1034
1035 /* if (WinMessageBox(HWND_DESKTOP,
1036 NULLHANDLE,
1037 (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL",
1038 "Start?",
1039 0,
1040 MB_YESNO | MB_MOVEABLE)
1041 == MBID_YES) */
1042 happ = WinStartApp(hwndNotify,
1043 // receives WM_APPTERMINATENOTIFY
1044 &ProgDetails,
1045 strParamsPatched.psz,
1046 NULL, // "reserved", PMREF says...
1047 SAF_INSTALLEDCMDLINE);
1048 // we MUST use SAF_INSTALLEDCMDLINE
1049 // or no Win-OS/2 session will start...
1050 // whatever is going on here... Warp 4 FP11
1051
1052 // do not use SAF_STARTCHILDAPP, or the
1053 // app will be terminated automatically
1054 // when the WPS terminates!
1055
1056 // _Pmpf((__FUNCTION__ ": got happ 0x%lX", happ));
1057
1058 xstrClear(&strParamsPatched);
1059 if (pszWinOS2Env)
1060 free(pszWinOS2Env);
1061 } // end if (ProgDetails.pszExecutable)
1062
1063 return (happ);
1064}
1065
1066
Note: See TracBrowser for help on using the repository browser.