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

Last change on this file since 151 was 151, checked in by umoeller, 23 years ago

Misc fixes

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 56.7 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_DOSMODULEMGR
36#define INCL_DOSSESMGR
37#define INCL_DOSERRORS
38
39#define INCL_WINPROGRAMLIST // needed for PROGDETAILS, wppgm.h
40#define INCL_WINERRORS
41#define INCL_SHLERRORS
42#include <os2.h>
43
44#include <stdio.h>
45
46#include "setup.h" // code generation and debugging options
47
48#include "helpers\apps.h"
49#include "helpers\dosh.h"
50#include "helpers\prfh.h"
51#include "helpers\standards.h" // some standard macros
52#include "helpers\stringh.h"
53#include "helpers\winh.h"
54#include "helpers\xstring.h"
55
56/*
57 *@@category: Helpers\PM helpers\Application helpers
58 */
59
60/* ******************************************************************
61 *
62 * Environment helpers
63 *
64 ********************************************************************/
65
66/*
67 *@@ appQueryEnvironmentLen:
68 * returns the total length of the passed in environment
69 * string buffer, including the terminating two null bytes.
70 *
71 *@@added V0.9.16 (2002-01-09) [umoeller]
72 */
73
74ULONG appQueryEnvironmentLen(PCSZ pcszEnvironment)
75{
76 ULONG cbEnvironment = 0;
77 if (pcszEnvironment)
78 {
79 PCSZ pVarThis = pcszEnvironment;
80 // go thru the environment strings; last one has two null bytes
81 while (*pVarThis)
82 {
83 ULONG ulLenThis = strlen(pVarThis) + 1;
84 cbEnvironment += ulLenThis;
85 pVarThis += ulLenThis;
86 }
87
88 cbEnvironment++; // last null byte
89 }
90
91 return (cbEnvironment);
92}
93
94/*
95 *@@ appParseEnvironment:
96 * this takes one of those ugly environment strings
97 * as used by DosStartSession and WinStartApp (with
98 * lots of zero-terminated strings one after another
99 * and a duplicate zero byte as a terminator) as
100 * input and splits it into an array of separate
101 * strings in pEnv.
102 *
103 * The newly allocated strings are stored in in
104 * pEnv->papszVars. The array count is stored in
105 * pEnv->cVars.
106 *
107 * Each environment variable will be copied into
108 * one newly allocated string in the array. Use
109 * appFreeEnvironment to free the memory allocated
110 * by this function.
111 *
112 * Use the following code to browse thru the array:
113 +
114 + DOSENVIRONMENT Env = {0};
115 + if (appParseEnvironment(pszEnv,
116 + &Env)
117 + == NO_ERROR)
118 + {
119 + if (Env.papszVars)
120 + {
121 + PSZ *ppszThis = Env.papszVars;
122 + for (ul = 0;
123 + ul < Env.cVars;
124 + ul++)
125 + {
126 + PSZ pszThis = *ppszThis;
127 + // pszThis now has something like PATH=C:\TEMP
128 + // ...
129 + // next environment string
130 + ppszThis++;
131 + }
132 + }
133 + appFreeEnvironment(&Env);
134 + }
135 *
136 *@@added V0.9.4 (2000-08-02) [umoeller]
137 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
138 */
139
140APIRET appParseEnvironment(const char *pcszEnv,
141 PDOSENVIRONMENT pEnv) // out: new environment
142{
143 APIRET arc = NO_ERROR;
144 if (!pcszEnv)
145 arc = ERROR_INVALID_PARAMETER;
146 else
147 {
148 PSZ pszVarThis = (PSZ)pcszEnv;
149 ULONG cVars = 0;
150 // count strings
151 while (*pszVarThis)
152 {
153 cVars++;
154 pszVarThis += strlen(pszVarThis) + 1;
155 }
156
157 pEnv->cVars = 0;
158 pEnv->papszVars = 0;
159
160 if (cVars)
161 {
162 ULONG cbArray = sizeof(PSZ) * cVars;
163 PSZ *papsz;
164 if (!(papsz = (PSZ*)malloc(cbArray)))
165 arc = ERROR_NOT_ENOUGH_MEMORY;
166 else
167 {
168 PSZ *ppszTarget = papsz;
169 memset(papsz, 0, cbArray);
170 pszVarThis = (PSZ)pcszEnv;
171 while (*pszVarThis)
172 {
173 ULONG ulThisLen;
174 if (!(*ppszTarget = strhdup(pszVarThis, &ulThisLen)))
175 {
176 arc = ERROR_NOT_ENOUGH_MEMORY;
177 break;
178 }
179 (pEnv->cVars)++;
180 ppszTarget++;
181 pszVarThis += ulThisLen + 1;
182 }
183
184 pEnv->papszVars = papsz;
185 }
186 }
187 }
188
189 return (arc);
190}
191
192/*
193 *@@ appGetEnvironment:
194 * calls appParseEnvironment for the current
195 * process environment, which is retrieved from
196 * the info blocks.
197 *
198 * Returns:
199 *
200 * -- NO_ERROR:
201 *
202 * -- ERROR_INVALID_PARAMETER
203 *
204 * -- ERROR_BAD_ENVIRONMENT: no environment found in
205 * info blocks.
206 *
207 *@@added V0.9.4 (2000-07-19) [umoeller]
208 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
209 */
210
211APIRET appGetEnvironment(PDOSENVIRONMENT pEnv)
212{
213 APIRET arc = NO_ERROR;
214 if (!pEnv)
215 arc = ERROR_INVALID_PARAMETER;
216 else
217 {
218 PTIB ptib = 0;
219 PPIB ppib = 0;
220 arc = DosGetInfoBlocks(&ptib, &ppib);
221 if (arc == NO_ERROR)
222 {
223 PSZ pszEnv;
224 if (pszEnv = ppib->pib_pchenv)
225 arc = appParseEnvironment(pszEnv, pEnv);
226 else
227 arc = ERROR_BAD_ENVIRONMENT;
228 }
229 }
230
231 return (arc);
232}
233
234/*
235 *@@ appFindEnvironmentVar:
236 * returns the PSZ* in the pEnv->papszVars array
237 * which specifies the environment variable in pszVarName.
238 *
239 * With pszVarName, you can either specify the variable
240 * name only ("VARNAME") or a full environment string
241 * ("VARNAME=BLAH"). In any case, only the variable name
242 * is compared.
243 *
244 * Returns NULL if no such variable name was found in
245 * the array.
246 *
247 *@@added V0.9.4 (2000-07-19) [umoeller]
248 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
249 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
250 *@@changed V0.9.16 (2002-01-01) [umoeller]: removed extra heap allocation
251 */
252
253PSZ* appFindEnvironmentVar(PDOSENVIRONMENT pEnv,
254 PSZ pszVarName)
255{
256 PSZ *ppszRet = 0;
257
258 if ( (pEnv)
259 && (pEnv->papszVars)
260 && (pszVarName)
261 )
262 {
263 ULONG ul = 0;
264 ULONG ulVarNameLen = 0;
265
266 PSZ pFirstEqual;
267 // rewrote all the following for speed V0.9.16 (2002-01-01) [umoeller]
268 if (pFirstEqual = strchr(pszVarName, '='))
269 // VAR=VALUE
270 // ^ pFirstEqual
271 ulVarNameLen = pFirstEqual - pszVarName;
272 else
273 ulVarNameLen = strlen(pszVarName);
274
275 for (ul = 0;
276 ul < pEnv->cVars;
277 ul++)
278 {
279 PSZ pszThis = pEnv->papszVars[ul];
280 if (pFirstEqual = strchr(pszThis, '='))
281 {
282 ULONG ulLenThis = pFirstEqual - pszThis;
283 if ( (ulLenThis == ulVarNameLen)
284 && (!memicmp(pszThis,
285 pszVarName,
286 ulVarNameLen))
287 )
288 {
289 ppszRet = &pEnv->papszVars[ul];
290 break;
291 }
292 }
293 }
294 }
295
296 return (ppszRet);
297}
298
299/*
300 *@@ appSetEnvironmentVar:
301 * sets an environment variable in the specified
302 * environment, which must have been initialized
303 * using appGetEnvironment first.
304 *
305 * pszNewEnv must be a full environment string
306 * in the form "VARNAME=VALUE".
307 *
308 * If "VARNAME" has already been set to something
309 * in the string array in pEnv, that array item
310 * is replaced.
311 *
312 * OTOH, if "VARNAME" has not been set yet, a new
313 * item is added to the array, and pEnv->cVars is
314 * raised by one. In that case, fAddFirst determines
315 * whether the new array item is added to the front
316 * or the tail of the environment list.
317 *
318 *@@added V0.9.4 (2000-07-19) [umoeller]
319 *@@changed V0.9.7 (2000-12-17) [umoeller]: added fAddFirst
320 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
321 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed crash if !fAddFirst
322 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
323 */
324
325APIRET appSetEnvironmentVar(PDOSENVIRONMENT pEnv,
326 PSZ pszNewEnv,
327 BOOL fAddFirst)
328{
329 APIRET arc = NO_ERROR;
330 if ((!pEnv) || (!pszNewEnv))
331 arc = ERROR_INVALID_PARAMETER;
332 else
333 {
334 if (!pEnv->papszVars)
335 {
336 // no variables set yet:
337 pEnv->papszVars = (PSZ*)malloc(sizeof(PSZ));
338 pEnv->cVars = 1;
339
340 *(pEnv->papszVars) = strdup(pszNewEnv);
341 }
342 else
343 {
344 PSZ *ppszEnvLine;
345 if (ppszEnvLine = appFindEnvironmentVar(pEnv, pszNewEnv))
346 // was set already: replace
347 arc = strhStore(ppszEnvLine,
348 pszNewEnv,
349 NULL);
350 else
351 {
352 // not set already:
353 PSZ *ppszNew = NULL;
354
355 // allocate new array, with one new entry
356 // fixed V0.9.12 (2001-05-26) [umoeller], this crashed
357 PSZ *papszNew;
358
359 if (!(papszNew = (PSZ*)malloc(sizeof(PSZ) * (pEnv->cVars + 1))))
360 arc = ERROR_NOT_ENOUGH_MEMORY;
361 else
362 {
363 if (fAddFirst)
364 {
365 // add as first entry:
366 // overwrite first entry
367 ppszNew = papszNew;
368 // copy old entries
369 memcpy(papszNew + 1, // second new entry
370 pEnv->papszVars, // first old entry
371 sizeof(PSZ) * pEnv->cVars);
372 }
373 else
374 {
375 // append at the tail:
376 // overwrite last entry
377 ppszNew = papszNew + pEnv->cVars;
378 // copy old entries
379 memcpy(papszNew, // first new entry
380 pEnv->papszVars, // first old entry
381 sizeof(PSZ) * pEnv->cVars);
382 }
383
384 free(pEnv->papszVars); // was missing V0.9.12 (2001-05-21) [umoeller]
385 pEnv->papszVars = papszNew;
386 pEnv->cVars++;
387 *ppszNew = strdup(pszNewEnv);
388 }
389 }
390 }
391 }
392
393 return (arc);
394}
395
396/*
397 *@@ appConvertEnvironment:
398 * converts an environment initialized by appGetEnvironment
399 * to the string format required by WinStartApp and DosExecPgm,
400 * that is, one memory block is allocated in *ppszEnv and all
401 * strings in pEnv->papszVars are copied to that block. Each
402 * string is terminated with a null character; the last string
403 * is terminated with two null characters.
404 *
405 * Use free() to free the memory block allocated by this
406 * function in *ppszEnv.
407 *
408 *@@added V0.9.4 (2000-07-19) [umoeller]
409 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
410 */
411
412APIRET appConvertEnvironment(PDOSENVIRONMENT pEnv,
413 PSZ *ppszEnv, // out: environment string
414 PULONG pulSize) // out: size of block allocated in *ppszEnv; ptr can be NULL
415{
416 APIRET arc = NO_ERROR;
417 if ( (!pEnv)
418 || (!pEnv->papszVars)
419 )
420 arc = ERROR_INVALID_PARAMETER;
421 else
422 {
423 // count memory needed for all strings
424 ULONG cbNeeded = 0,
425 ul = 0;
426 PSZ *ppszThis = pEnv->papszVars;
427
428 for (ul = 0;
429 ul < pEnv->cVars;
430 ul++)
431 {
432 cbNeeded += strlen(*ppszThis) + 1; // length of string plus null terminator
433
434 // next environment string
435 ppszThis++;
436 }
437
438 cbNeeded++; // for another null terminator
439
440 if (!(*ppszEnv = (PSZ)malloc(cbNeeded)))
441 arc = ERROR_NOT_ENOUGH_MEMORY;
442 else
443 {
444 PSZ pTarget = *ppszEnv;
445 if (pulSize)
446 *pulSize = cbNeeded;
447 ppszThis = pEnv->papszVars;
448
449 // now copy each string
450 for (ul = 0;
451 ul < pEnv->cVars;
452 ul++)
453 {
454 PSZ pSource = *ppszThis;
455
456 while ((*pTarget++ = *pSource++))
457 ;
458
459 // *pTarget++ = 0; // append null terminator per string
460
461 // next environment string
462 ppszThis++;
463 }
464
465 *pTarget++ = 0; // append second null terminator
466 }
467 }
468
469 return (arc);
470}
471
472/*
473 *@@ appFreeEnvironment:
474 * frees memory allocated by appGetEnvironment.
475 *
476 *@@added V0.9.4 (2000-07-19) [umoeller]
477 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
478 */
479
480APIRET appFreeEnvironment(PDOSENVIRONMENT pEnv)
481{
482 APIRET arc = NO_ERROR;
483 if ( (!pEnv)
484 || (!pEnv->papszVars)
485 )
486 arc = ERROR_INVALID_PARAMETER;
487 else
488 {
489 PSZ *ppszThis = pEnv->papszVars;
490 PSZ pszThis;
491 ULONG ul = 0;
492
493 for (ul = 0;
494 ul < pEnv->cVars;
495 ul++)
496 {
497 pszThis = *ppszThis;
498 free(pszThis);
499 // *ppszThis = NULL;
500 // next environment string
501 ppszThis++;
502 }
503
504 free(pEnv->papszVars);
505 pEnv->cVars = 0;
506 }
507
508 return (arc);
509}
510
511/* ******************************************************************
512 *
513 * Application information
514 *
515 ********************************************************************/
516
517/*
518 *@@ appQueryAppType:
519 * returns the Control Program (Dos) and
520 * Win* PROG_* application types for the
521 * specified executable. Essentially, this
522 * is a wrapper around DosQueryAppType.
523 *
524 * pcszExecutable must be fully qualified.
525 * You can use doshFindExecutable to qualify
526 * it.
527 *
528 * This returns the APIRET of DosQueryAppType.
529 * If this is NO_ERROR; *pulDosAppType receives
530 * the app type of DosQueryAppType. In addition,
531 * *pulWinAppType is set to one of the following:
532 *
533 * -- PROG_FULLSCREEN
534 *
535 * -- PROG_PDD
536 *
537 * -- PROG_VDD
538 *
539 * -- PROG_DLL
540 *
541 * -- PROG_WINDOWEDVDM
542 *
543 * -- PROG_PM
544 *
545 * -- PROG_31_ENHSEAMLESSCOMMON
546 *
547 * -- PROG_WINDOWABLEVIO
548 *
549 * -- PROG_DEFAULT
550 *
551 *@@added V0.9.9 (2001-03-07) [umoeller]
552 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
553 *@@changed V0.9.14 (2001-08-07) [pr]: use FAPPTYP_* constants
554 *@@changed V0.9.16 (2001-12-08) [umoeller]: added checks for batch files, other optimizations
555 */
556
557APIRET appQueryAppType(const char *pcszExecutable,
558 PULONG pulDosAppType,
559 PULONG pulWinAppType)
560{
561 APIRET arc;
562
563/*
564 #define FAPPTYP_NOTSPEC 0x0000
565 #define FAPPTYP_NOTWINDOWCOMPAT 0x0001
566 #define FAPPTYP_WINDOWCOMPAT 0x0002
567 #define FAPPTYP_WINDOWAPI 0x0003
568 #define FAPPTYP_BOUND 0x0008
569 #define FAPPTYP_DLL 0x0010
570 #define FAPPTYP_DOS 0x0020
571 #define FAPPTYP_PHYSDRV 0x0040 // physical device driver
572 #define FAPPTYP_VIRTDRV 0x0080 // virtual device driver
573 #define FAPPTYP_PROTDLL 0x0100 // 'protected memory' dll
574 #define FAPPTYP_WINDOWSREAL 0x0200 // Windows real mode app
575 #define FAPPTYP_WINDOWSPROT 0x0400 // Windows protect mode app
576 #define FAPPTYP_WINDOWSPROT31 0x1000 // Windows 3.1 protect mode app
577 #define FAPPTYP_32BIT 0x4000
578*/
579
580 ULONG ulWinAppType = PROG_DEFAULT;
581
582 if (!(arc = DosQueryAppType((PSZ)pcszExecutable, pulDosAppType)))
583 {
584 // clear the 32-bit flag
585 // V0.9.16 (2001-12-08) [umoeller]
586 ULONG ulDosAppType = (*pulDosAppType) & ~FAPPTYP_32BIT,
587 ulLoAppType = ulDosAppType & 0xFFFF;
588
589 if (ulDosAppType & FAPPTYP_PHYSDRV) // 0x40
590 ulWinAppType = PROG_PDD;
591 else if (ulDosAppType & FAPPTYP_VIRTDRV) // 0x80
592 ulWinAppType = PROG_VDD;
593 else if ((ulDosAppType & 0xF0) == FAPPTYP_DLL) // 0x10
594 // DLL bit set
595 ulWinAppType = PROG_DLL;
596 else if (ulDosAppType & FAPPTYP_DOS) // 0x20
597 // DOS bit set?
598 ulWinAppType = PROG_WINDOWEDVDM;
599 else if ((ulDosAppType & FAPPTYP_WINDOWAPI) == FAPPTYP_WINDOWAPI) // 0x0003)
600 // "Window-API" == PM
601 ulWinAppType = PROG_PM;
602 else if (ulLoAppType == FAPPTYP_WINDOWSREAL)
603 ulWinAppType = PROG_31_ENHSEAMLESSCOMMON; // @@todo really?
604 else if ( (ulLoAppType == FAPPTYP_WINDOWSPROT31) // 0x1000) // windows program (?!?)
605 || (ulLoAppType == FAPPTYP_WINDOWSPROT) // ) // windows program (?!?)
606 )
607 ulWinAppType = PROG_31_ENHSEAMLESSCOMMON; // PROG_31_ENH;
608 else if ((ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_WINDOWCOMPAT) // 0x02)
609 ulWinAppType = PROG_WINDOWABLEVIO;
610 else if ((ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_NOTWINDOWCOMPAT) // 0x01)
611 ulWinAppType = PROG_FULLSCREEN;
612 }
613
614 if (ulWinAppType == PROG_DEFAULT)
615 {
616 // added checks for batch files V0.9.16 (2001-12-08) [umoeller]
617 PCSZ pcszExt;
618 if (pcszExt = doshGetExtension(pcszExecutable))
619 {
620 if (!stricmp(pcszExt, "BAT"))
621 {
622 ulWinAppType = PROG_WINDOWEDVDM;
623 arc = NO_ERROR;
624 }
625 else if (!stricmp(pcszExt, "CMD"))
626 {
627 ulWinAppType = PROG_WINDOWABLEVIO;
628 arc = NO_ERROR;
629 }
630 }
631 }
632
633 *pulWinAppType = ulWinAppType;
634
635 return (arc);
636}
637
638/*
639 *@@ PROGTYPESTRING:
640 *
641 *@@added V0.9.16 (2002-01-13) [umoeller]
642 */
643
644typedef struct _PROGTYPESTRING
645{
646 PROGCATEGORY progc;
647 PCSZ pcsz;
648} PROGTYPESTRING, *PPROGTYPESTRING;
649
650PROGTYPESTRING G_aProgTypes[] =
651 {
652 PROG_DEFAULT, "PROG_DEFAULT",
653 PROG_FULLSCREEN, "PROG_FULLSCREEN",
654 PROG_WINDOWABLEVIO, "PROG_WINDOWABLEVIO",
655 PROG_PM, "PROG_PM",
656 PROG_GROUP, "PROG_GROUP",
657 PROG_VDM, "PROG_VDM",
658 // same as PROG_REAL, "PROG_REAL",
659 PROG_WINDOWEDVDM, "PROG_WINDOWEDVDM",
660 PROG_DLL, "PROG_DLL",
661 PROG_PDD, "PROG_PDD",
662 PROG_VDD, "PROG_VDD",
663 PROG_WINDOW_REAL, "PROG_WINDOW_REAL",
664 PROG_30_STD, "PROG_30_STD",
665 // same as PROG_WINDOW_PROT, "PROG_WINDOW_PROT",
666 PROG_WINDOW_AUTO, "PROG_WINDOW_AUTO",
667 PROG_30_STDSEAMLESSVDM, "PROG_30_STDSEAMLESSVDM",
668 // same as PROG_SEAMLESSVDM, "PROG_SEAMLESSVDM",
669 PROG_30_STDSEAMLESSCOMMON, "PROG_30_STDSEAMLESSCOMMON",
670 // same as PROG_SEAMLESSCOMMON, "PROG_SEAMLESSCOMMON",
671 PROG_31_STDSEAMLESSVDM, "PROG_31_STDSEAMLESSVDM",
672 PROG_31_STDSEAMLESSCOMMON, "PROG_31_STDSEAMLESSCOMMON",
673 PROG_31_ENHSEAMLESSVDM, "PROG_31_ENHSEAMLESSVDM",
674 PROG_31_ENHSEAMLESSCOMMON, "PROG_31_ENHSEAMLESSCOMMON",
675 PROG_31_ENH, "PROG_31_ENH",
676 PROG_31_STD, "PROG_31_STD",
677
678// Warp 4 toolkit defines, whatever these were designed for...
679#ifndef PROG_DOS_GAME
680 #define PROG_DOS_GAME (PROGCATEGORY)21
681#endif
682#ifndef PROG_WIN_GAME
683 #define PROG_WIN_GAME (PROGCATEGORY)22
684#endif
685#ifndef PROG_DOS_MODE
686 #define PROG_DOS_MODE (PROGCATEGORY)23
687#endif
688
689 PROG_DOS_GAME, "PROG_DOS_GAME",
690 PROG_WIN_GAME, "PROG_WIN_GAME",
691 PROG_DOS_MODE, "PROG_DOS_MODE",
692
693 // added this V0.9.16 (2001-12-08) [umoeller]
694 PROG_WIN32, "PROG_WIN32"
695 };
696
697/*
698 *@@ appDescribeAppType:
699 * returns a "PROG_*" string for the given
700 * program type. Useful for WPProgram setup
701 * strings and such.
702 *
703 *@@added V0.9.16 (2001-10-06)
704 */
705
706PCSZ appDescribeAppType(PROGCATEGORY progc) // in: from PROGDETAILS.progc
707{
708 ULONG ul;
709 for (ul = 0;
710 ul < ARRAYITEMCOUNT(G_aProgTypes);
711 ul++)
712 {
713 if (G_aProgTypes[ul].progc == progc)
714 return (G_aProgTypes[ul].pcsz);
715 }
716
717 return NULL;
718}
719
720/*
721 *@@ appIsWindowsApp:
722 * checks the specified program category
723 * (PROGDETAILS.progt.progc) for whether
724 * it represents a Win-OS/2 application.
725 *
726 * Returns:
727 *
728 * -- 0: no windows app (it's VIO, OS/2
729 * or DOS fullscreen, or PM).
730 *
731 * -- 1: Win-OS/2 standard app.
732 *
733 * -- 2: Win-OS/2 enhanced-mode app.
734 *
735 *@@added V0.9.12 (2001-05-26) [umoeller]
736 */
737
738ULONG appIsWindowsApp(ULONG ulProgCategory)
739{
740 switch (ulProgCategory)
741 {
742 case PROG_31_ENHSEAMLESSVDM: // 17
743 case PROG_31_ENHSEAMLESSCOMMON: // 18
744 case PROG_31_ENH: // 19
745 return (2);
746
747#ifndef PROG_30_STD
748 #define PROG_30_STD (PROGCATEGORY)11
749#endif
750
751#ifndef PROG_30_STDSEAMLESSVDM
752 #define PROG_30_STDSEAMLESSVDM (PROGCATEGORY)13
753#endif
754
755 case PROG_WINDOW_REAL: // 10
756 case PROG_30_STD: // 11
757 case PROG_WINDOW_AUTO: // 12
758 case PROG_30_STDSEAMLESSVDM: // 13
759 case PROG_30_STDSEAMLESSCOMMON: // 14
760 case PROG_31_STDSEAMLESSVDM: // 15
761 case PROG_31_STDSEAMLESSCOMMON: // 16
762 case PROG_31_STD: // 20
763 return (1);
764 }
765
766 return (0);
767}
768
769/* ******************************************************************
770 *
771 * Application start
772 *
773 ********************************************************************/
774
775/*
776 *@@ CallBatchCorrectly:
777 * fixes the specified PROGDETAILS for
778 * command files in the executable part
779 * by inserting /C XXX into the parameters
780 * and setting the executable according
781 * to an environment variable.
782 *
783 *@@added V0.9.6 (2000-10-16) [umoeller]
784 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING
785 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
786 */
787
788static VOID CallBatchCorrectly(PPROGDETAILS pProgDetails,
789 PXSTRING pstrParams, // in/out: modified parameters (reallocated)
790 const char *pcszEnvVar, // in: env var spec'g command proc
791 // (e.g. "OS2_SHELL"); can be NULL
792 const char *pcszDefProc) // in: def't command proc (e.g. "CMD.EXE")
793{
794 // XXX.CMD file as executable:
795 // fix args to /C XXX.CMD
796
797 PSZ pszOldParams = NULL;
798 ULONG ulOldParamsLength = pstrParams->ulLength;
799 if (ulOldParamsLength)
800 // we have parameters already:
801 // make a backup... we'll append that later
802 pszOldParams = strdup(pstrParams->psz);
803
804 // set new params to "/C filename.cmd"
805 xstrcpy(pstrParams, "/C ", 0);
806 xstrcat(pstrParams,
807 pProgDetails->pszExecutable,
808 0);
809
810 if (pszOldParams)
811 {
812 // .cmd had params:
813 // append space and old params
814 xstrcatc(pstrParams, ' ');
815 xstrcat(pstrParams,
816 pszOldParams,
817 ulOldParamsLength);
818 free(pszOldParams);
819 }
820
821 // set executable to $(OS2_SHELL)
822 pProgDetails->pszExecutable = NULL;
823 if (pcszEnvVar)
824 pProgDetails->pszExecutable = getenv(pcszEnvVar);
825 if (!pProgDetails->pszExecutable)
826 pProgDetails->pszExecutable = (PSZ)pcszDefProc;
827 // should be on PATH
828}
829
830/*
831 *@@ appQueryDefaultWin31Environment:
832 * returns the default Win-OS/2 3.1 environment
833 * from OS2.INI, which you can then merge with
834 * your process environment to be able to
835 * start Win-OS/2 sessions properly with
836 * appStartApp.
837 *
838 * Caller must free() the return value.
839 *
840 *@@added V0.9.12 (2001-05-26) [umoeller]
841 */
842
843PSZ appQueryDefaultWin31Environment(VOID)
844{
845 PSZ pszReturn = NULL;
846 ULONG ulSize = 0;
847 // get default environment (from Win-OS/2 settings object)
848 // from OS2.INI
849 PSZ pszDefEnv = prfhQueryProfileData(HINI_USER,
850 "WINOS2",
851 "PM_GlobalWindows31Settings",
852 &ulSize);
853 if (pszDefEnv)
854 {
855 if (pszReturn = (PSZ)malloc(ulSize + 2))
856 {
857 PSZ p;
858 memset(pszReturn, 0, ulSize + 2);
859 memcpy(pszReturn, pszDefEnv, ulSize);
860
861 for (p = pszReturn;
862 p < pszReturn + ulSize;
863 p++)
864 if (*p == ';')
865 *p = 0;
866
867 // okay.... now we got an OS/2-style environment
868 // with 0, 0, 00 strings
869 }
870
871 free(pszDefEnv);
872 }
873
874 return (pszReturn);
875}
876
877/*
878 *@@ CallWinStartApp:
879 * wrapper around WinStartApp which copies all the
880 * parameters into a contiguous block of tiled memory.
881 *
882 * This might fix some of the problems with truncated
883 * environments we were having because apparently the
884 * WinStartApp thunking to 16-bit doesn't always work.
885 *
886 *@@added V0.9.18 (2002-02-13) [umoeller]
887 */
888
889static APIRET CallWinStartApp(HAPP *phapp, // out: application handle if NO_ERROR is returned
890 HWND hwndNotify, // in: notify window or NULLHANDLE
891 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
892 PCSZ pcszParamsPatched)
893{
894 ULONG cb,
895 cbTitle,
896 cbExecutable,
897 cbParameters,
898 cbStartupDir,
899 cbIcon,
900 cbEnvironment;
901
902 APIRET arc = NO_ERROR;
903
904 /*
905 if (WinMessageBox(HWND_DESKTOP,
906 NULLHANDLE,
907 (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL",
908 "Start?",
909 0,
910 MB_YESNO | MB_MOVEABLE)
911 != MBID_YES)
912 return (ERROR_INTERRUPT);
913 */
914
915 // allocate a chunk of tiled memory from OS/2 to make sure
916 // this is aligned on a 64K memory (backed up by a 16-bit
917 // LDT selector)
918 cb = sizeof(PROGDETAILS);
919 if (cbTitle = strhSize(pcProgDetails->pszTitle))
920 cb += cbTitle;
921 if (cbExecutable = strhSize(pcProgDetails->pszExecutable))
922 cb += cbExecutable;
923 if (cbParameters = strhSize(pcProgDetails->pszParameters))
924 cb += cbParameters;
925 if (cbStartupDir = strhSize(pcProgDetails->pszStartupDir))
926 cb += cbStartupDir;
927 if (cbIcon = strhSize(pcProgDetails->pszIcon))
928 cb += cbIcon;
929 if (cbEnvironment = appQueryEnvironmentLen(pcProgDetails->pszEnvironment))
930 cb += cbEnvironment;
931
932 if (cb > 60000) // to be on the safe side
933 arc = ERROR_BAD_ENVIRONMENT; // 10;
934 else
935 {
936 PPROGDETAILS pNewProgDetails;
937 if (!(arc = DosAllocMem((PVOID*)&pNewProgDetails,
938 cb,
939 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE)))
940 {
941 // alright, copy stuff
942 PBYTE pThis;
943
944 memset(pNewProgDetails, 0, sizeof(PROGDETAILS));
945
946 pNewProgDetails->Length = sizeof(PROGDETAILS);
947 pNewProgDetails->progt.progc = pcProgDetails->progt.progc;
948 pNewProgDetails->progt.fbVisible = pcProgDetails->progt.fbVisible;
949 memcpy(&pNewProgDetails->swpInitial, &pcProgDetails->swpInitial, sizeof(SWP));
950
951 pThis = (PBYTE)(pNewProgDetails + 1);
952
953 if (cbTitle)
954 {
955 memcpy(pThis, pcProgDetails->pszTitle, cbTitle);
956 pNewProgDetails->pszTitle = pThis;
957 pThis += cbTitle;
958 }
959
960 if (cbExecutable)
961 {
962 memcpy(pThis, pcProgDetails->pszExecutable, cbExecutable);
963 pNewProgDetails->pszExecutable = pThis;
964 pThis += cbExecutable;
965 }
966
967 if (cbParameters)
968 {
969 memcpy(pThis, pcProgDetails->pszParameters, cbParameters);
970 pNewProgDetails->pszParameters = pThis;
971 pThis += cbParameters;
972 }
973
974 if (cbStartupDir)
975 {
976 memcpy(pThis, pcProgDetails->pszStartupDir, cbStartupDir);
977 pNewProgDetails->pszStartupDir = pThis;
978 pThis += cbStartupDir;
979 }
980
981 if (cbIcon)
982 {
983 memcpy(pThis, pcProgDetails->pszIcon, cbIcon);
984 pNewProgDetails->pszIcon = pThis;
985 pThis += cbIcon;
986 }
987
988 if (cbEnvironment)
989 {
990 memcpy(pThis, pcProgDetails->pszEnvironment, cbEnvironment);
991 pNewProgDetails->pszEnvironment = pThis;
992 pThis += cbEnvironment;
993 }
994
995 #ifdef DEBUG_PROGRAMSTART
996 _Pmpf((__FUNCTION__ ": progt.progc: %d", pNewProgDetails->progt.progc));
997 _Pmpf((" progt.fbVisible: 0x%lX", pNewProgDetails->progt.fbVisible));
998 _Pmpf((" progt.pszTitle: \"%s\"", (pNewProgDetails->pszTitle) ? pNewProgDetails->pszTitle : "NULL"));
999 _Pmpf((" exec: \"%s\"", (pNewProgDetails->pszExecutable) ? pNewProgDetails->pszExecutable : "NULL"));
1000 _Pmpf((" params: \"%s\"", (pNewProgDetails->pszParameters) ? pNewProgDetails->pszParameters : "NULL"));
1001 _Pmpf((" startup: \"%s\"", (pNewProgDetails->pszStartupDir) ? pNewProgDetails->pszStartupDir : "NULL"));
1002 _Pmpf((" pszIcon: \"%s\"", (pNewProgDetails->pszIcon) ? pNewProgDetails->pszIcon : "NULL"));
1003 _Pmpf((" environment: "));
1004 {
1005 PSZ pszThis = pNewProgDetails->pszEnvironment;
1006 while (pszThis && *pszThis)
1007 {
1008 _Pmpf((" \"%s\"", pszThis));
1009 pszThis += strlen(pszThis) + 1;
1010 }
1011 }
1012
1013 _Pmpf((" swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
1014 pNewProgDetails->swpInitial.fl,
1015 pNewProgDetails->swpInitial.x,
1016 pNewProgDetails->swpInitial.y,
1017 pNewProgDetails->swpInitial.cx,
1018 pNewProgDetails->swpInitial.cy));
1019 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
1020 pNewProgDetails->swpInitial.hwndInsertBehind,
1021 pNewProgDetails->swpInitial.hwnd,
1022 pNewProgDetails->swpInitial.ulReserved1,
1023 pNewProgDetails->swpInitial.ulReserved2));
1024 #endif
1025
1026 if (!(*phapp = WinStartApp(hwndNotify,
1027 // receives WM_APPTERMINATENOTIFY
1028 pNewProgDetails,
1029 pNewProgDetails->pszParameters,
1030 NULL, // "reserved", PMREF says...
1031 SAF_INSTALLEDCMDLINE)))
1032 // we MUST use SAF_INSTALLEDCMDLINE
1033 // or no Win-OS/2 session will start...
1034 // whatever is going on here... Warp 4 FP11
1035
1036 // do not use SAF_STARTCHILDAPP, or the
1037 // app will be terminated automatically
1038 // when the WPS terminates!
1039 {
1040 // cannot start app:
1041 #ifdef DEBUG_PROGRAMSTART
1042 _Pmpf((__FUNCTION__ ": WinStartApp failed"));
1043 #endif
1044
1045 arc = ERROR_FILE_NOT_FOUND;
1046 // unfortunately WinStartApp doesn't
1047 // return meaningful codes like DosStartSession, so
1048 // try to see what happened
1049 /*
1050 switch (ERRORIDERROR(WinGetLastError(0)))
1051 {
1052 case PMERR_DOS_ERROR: // (0x1200)
1053 {
1054 arc = ERROR_FILE_NOT_FOUND;
1055
1056 // this is probably the case where the module
1057 // couldn't be loaded, so try DosStartSession
1058 // to get a meaningful return code... note that
1059 // this cannot handle hwndNotify then
1060 RESULTCODES result;
1061 arc = DosExecPgm(pszFailingName,
1062 cbFailingName,
1063 EXEC_ASYNC,
1064 NULL, // ProgDetails.pszParameters,
1065 NULL, // ProgDetails.pszEnvironment,
1066 &result,
1067 ProgDetails.pszExecutable);
1068 ULONG sid, pid;
1069 STARTDATA SData;
1070 SData.Length = sizeof(STARTDATA);
1071 SData.Related = SSF_RELATED_CHILD; //INDEPENDENT;
1072 SData.FgBg = SSF_FGBG_FORE;
1073 SData.TraceOpt = SSF_TRACEOPT_NONE;
1074
1075 SData.PgmTitle = ProgDetails.pszTitle;
1076 SData.PgmName = ProgDetails.pszExecutable;
1077 SData.PgmInputs = ProgDetails.pszParameters;
1078
1079 SData.TermQ = NULL;
1080 SData.Environment = ProgDetails.pszEnvironment;
1081 SData.InheritOpt = SSF_INHERTOPT_PARENT; // ignored
1082 SData.SessionType = SSF_TYPE_DEFAULT;
1083 SData.IconFile = 0;
1084 SData.PgmHandle = 0;
1085
1086 SData.PgmControl = SSF_CONTROL_VISIBLE;
1087
1088 SData.InitXPos = 30;
1089 SData.InitYPos = 40;
1090 SData.InitXSize = 200;
1091 SData.InitYSize = 140;
1092 SData.Reserved = 0;
1093 SData.ObjectBuffer = pszFailingName;
1094 SData.ObjectBuffLen = cbFailingName;
1095
1096 arc = DosStartSession(&SData, &sid, &pid);
1097 }
1098 break;
1099
1100 case PMERR_INVALID_APPL: // (0x1530)
1101 // Attempted to start an application whose type is not
1102 // recognized by OS/2.
1103 arc = ERROR_INVALID_EXE_SIGNATURE;
1104 break;
1105
1106 case PMERR_INVALID_PARAMETERS: // (0x1208)
1107 // An application parameter value is invalid for
1108 // its converted PM type. For example: a 4-byte
1109 // value outside the range -32 768 to +32 767 cannot be
1110 // converted to a SHORT, and a negative number cannot
1111 // be converted to a ULONG or USHORT.
1112 arc = ERROR_INVALID_DATA;
1113 break;
1114
1115 case PMERR_STARTED_IN_BACKGROUND: // (0x1532)
1116 // The application started a new session in the
1117 // background.
1118 arc = ERROR_SMG_START_IN_BACKGROUND;
1119 break;
1120
1121 case PMERR_INVALID_WINDOW: // (0x1206)
1122 // The window specified with a Window List call
1123 // is not a valid frame window.
1124
1125 default:
1126 arc = ERROR_BAD_FORMAT;
1127 break;
1128 }
1129 */
1130 }
1131
1132 DosFreeMem(pNewProgDetails);
1133 }
1134 }
1135
1136 return (arc);
1137}
1138
1139/*
1140 *@@ appStartApp:
1141 * wrapper around WinStartApp which fixes the
1142 * specified PROGDETAILS to (hopefully) work
1143 * work with all executable types.
1144 *
1145 * This fixes the executable info to support:
1146 *
1147 * -- starting "*" executables (command prompts
1148 * for OS/2, DOS, Win-OS/2);
1149 *
1150 * -- starting ".CMD" and ".BAT" files as
1151 * PROGDETAILS.pszExecutable;
1152 *
1153 * -- starting apps which are not fully qualified
1154 * and therefore assumed to be on the PATH.
1155 *
1156 * Unless it is "*", PROGDETAILS.pszExecutable must
1157 * be a proper file name. The full path may be omitted
1158 * if it is on the PATH, but the extension (.EXE etc.)
1159 * must be given. You can use doshFindExecutable to
1160 * find executables if you don't know the extension.
1161 *
1162 * This also handles and merges special and default
1163 * environments for the app to be started. The
1164 * following should be respected:
1165 *
1166 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
1167 * is NULL, the new app inherits a default environment
1168 * from the shell.
1169 *
1170 * -- However, if you specify an environment, you _must_
1171 * specify a complete environment. This function
1172 * will not merge environments. Use
1173 * appSetEnvironmentVar to change environment
1174 * variables in a complete environment set.
1175 *
1176 * -- If PROGDETAILS specifies a Win-OS/2 session
1177 * and PROGDETAILS.pszEnvironment is empty,
1178 * this uses the default Win-OS/2 environment.
1179 * See appQueryDefaultWin31Environment.
1180 *
1181 * Even though this isn't clearly said in PMREF,
1182 * PROGDETAILS.swpInitial is important:
1183 *
1184 * -- To start a session minimized, set SWP_MINIMIZE.
1185 *
1186 * -- To start a VIO session with auto-close disabled,
1187 * set the half-documented SWP_NOAUTOCLOSE flag (0x8000)
1188 * This flag is now in the newer toolkit headers.
1189 *
1190 * In addition, this supports the following session
1191 * flags with ulFlags if PROG_DEFAULT is specified:
1192 *
1193 * -- APP_RUN_FULLSCREEN
1194 *
1195 * -- APP_RUN_ENHANCED
1196 *
1197 * -- APP_RUN_STANDARD
1198 *
1199 * -- APP_RUN_SEPARATE
1200 *
1201 * Since this calls WinStartApp in turn, this
1202 * requires a message queue on the calling thread.
1203 *
1204 * Note that this also does minimal checking on
1205 * the specified parameters so it can return something
1206 * more meaningful than FALSE like WinStartApp.
1207 * As a result, you get a DOS error code now (V0.9.16).
1208 *
1209 * Most importantly:
1210 *
1211 * -- ERROR_INVALID_THREADID: not running on thread 1.
1212 * Since this uses WinStartApp internally and
1213 * WinStartApp completely hangs the session manager
1214 * if a Win-OS/2 full-screen session is started from
1215 * a thread that is NOT thread 1, this will now fail
1216 * with this error for safety (V0.9.16).
1217 *
1218 * -- ERROR_INVALID_PARAMETER: pcProgDetails or
1219 * phapp is NULL; or PROGDETAILS.pszExecutable is NULL.
1220 *
1221 * -- ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND:
1222 * PROGDETAILS.pszExecutable and/or PROGDETAILS.pszStartupDir
1223 * are invalid.
1224 * A NULL PROGDETAILS.pszStartupDir is supported though.
1225 *
1226 * -- ERROR_NOT_ENOUGH_MEMORY
1227 *
1228 *@@added V0.9.6 (2000-10-16) [umoeller]
1229 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
1230 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
1231 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
1232 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
1233 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
1234 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
1235 *@@changed V0.9.14 (2001-08-23) [pr]: added session type options
1236 *@@changed V0.9.16 (2001-10-19) [umoeller]: added prototype to return APIRET
1237 *@@changed V0.9.16 (2001-10-19) [umoeller]: added thread-1 check
1238 *@@changed V0.9.16 (2001-12-06) [umoeller]: now using doshSearchPath for finding pszExecutable if not qualified
1239 *@@changed V0.9.16 (2002-01-04) [umoeller]: removed error report if startup directory was drive letter only
1240 *@@changed V0.9.16 (2002-01-04) [umoeller]: added more detailed error reports and *FailingName params
1241 *@@changed V0.9.18 (2002-02-13) [umoeller]: added CallWinStartApp to fix possible memory problems
1242 */
1243
1244APIRET appStartApp(HWND hwndNotify, // in: notify window or NULLHANDLE
1245 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
1246 ULONG ulFlags, // in: APP_RUN_* flags
1247 HAPP *phapp, // out: application handle if NO_ERROR is returned
1248 ULONG cbFailingName,
1249 PSZ pszFailingName)
1250{
1251 APIRET arc = NO_ERROR;
1252 PROGDETAILS ProgDetails;
1253
1254 if (pszFailingName)
1255 *pszFailingName = '\0';
1256
1257 if (!pcProgDetails || !phapp)
1258 return (ERROR_INVALID_PARAMETER);
1259
1260 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
1261 // pointers still point into old prog details buffer
1262 ProgDetails.Length = sizeof(PROGDETAILS);
1263 ProgDetails.progt.fbVisible = SHE_VISIBLE;
1264
1265 // all this only makes sense if this contains something...
1266 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
1267 if ( (!ProgDetails.pszExecutable)
1268 || (!(*(ProgDetails.pszExecutable)))
1269 )
1270 arc = ERROR_INVALID_PARAMETER;
1271 else if (doshMyTID() != 1) // V0.9.16 (2001-10-19) [umoeller]
1272 arc = ERROR_INVALID_THREADID;
1273 else
1274 {
1275 ULONG ulIsWinApp;
1276
1277 CHAR szFQExecutable[CCHMAXPATH];
1278
1279 XSTRING strParamsPatched;
1280 PSZ pszWinOS2Env = 0;
1281
1282 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
1283 // this wasn't a good idea... WPProgram stores stuff
1284 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
1285
1286 // duplicate parameters...
1287 // we need this for string manipulations below...
1288 if (ProgDetails.pszParameters)
1289 xstrInitCopy(&strParamsPatched,
1290 ProgDetails.pszParameters,
1291 100);
1292 else
1293 // no old params:
1294 xstrInit(&strParamsPatched, 100);
1295
1296 #ifdef DEBUG_PROGRAMSTART
1297 _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
1298 _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
1299 _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
1300 #endif
1301
1302 // program type fixups
1303 switch (ProgDetails.progt.progc) // that's a ULONG
1304 {
1305 case ((ULONG)-1): // we get that sometimes...
1306 case PROG_DEFAULT:
1307 {
1308 // V0.9.12 (2001-05-26) [umoeller]
1309 ULONG ulDosAppType;
1310 appQueryAppType(ProgDetails.pszExecutable,
1311 &ulDosAppType,
1312 &ProgDetails.progt.progc);
1313 }
1314 break;
1315 }
1316
1317 // set session type from option flags
1318 if (ulFlags & APP_RUN_FULLSCREEN)
1319 {
1320 if (ProgDetails.progt.progc == PROG_WINDOWABLEVIO)
1321 ProgDetails.progt.progc = PROG_FULLSCREEN;
1322
1323 if (ProgDetails.progt.progc == PROG_WINDOWEDVDM)
1324 ProgDetails.progt.progc = PROG_VDM;
1325 }
1326
1327 if (ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc))
1328 {
1329 if (ulFlags & APP_RUN_FULLSCREEN)
1330 ProgDetails.progt.progc = (ulFlags & APP_RUN_ENHANCED)
1331 ? PROG_31_ENH
1332 : PROG_31_STD;
1333 else
1334 {
1335 if (ulFlags & APP_RUN_STANDARD)
1336 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1337 ? PROG_31_STDSEAMLESSVDM
1338 : PROG_31_STDSEAMLESSCOMMON;
1339
1340 if (ulFlags & APP_RUN_ENHANCED)
1341 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1342 ? PROG_31_ENHSEAMLESSVDM
1343 : PROG_31_ENHSEAMLESSCOMMON;
1344 }
1345
1346 // re-run V0.9.16 (2001-10-19) [umoeller]
1347 ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc);
1348 }
1349
1350 /*
1351 * command lines fixups:
1352 *
1353 */
1354
1355 if (!strcmp(ProgDetails.pszExecutable, "*"))
1356 {
1357 /*
1358 * "*" for command sessions:
1359 *
1360 */
1361
1362 if (ulIsWinApp == 2)
1363 {
1364 // enhanced Win-OS/2 session:
1365 PSZ psz = NULL;
1366 if (strParamsPatched.ulLength)
1367 // "/3 " + existing params
1368 psz = strdup(strParamsPatched.psz);
1369
1370 xstrcpy(&strParamsPatched, "/3 ", 0);
1371
1372 if (psz)
1373 {
1374 xstrcat(&strParamsPatched, psz, 0);
1375 free(psz);
1376 }
1377 }
1378
1379 if (ulIsWinApp)
1380 {
1381 // cheat: WinStartApp doesn't support NULL
1382 // for Win-OS2 sessions, so manually start winos2.com
1383 ProgDetails.pszExecutable = "WINOS2.COM";
1384 // this is a DOS app, so fix this to DOS fullscreen
1385 ProgDetails.progt.progc = PROG_VDM;
1386 }
1387 else
1388 // for all other executable types
1389 // (including OS/2 and DOS sessions),
1390 // set pszExecutable to NULL; this will
1391 // have WinStartApp start a cmd shell
1392 ProgDetails.pszExecutable = NULL;
1393
1394 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
1395 else
1396 {
1397 // check if the executable is fully qualified; if so,
1398 // check if the executable file exists
1399 if ( (ProgDetails.pszExecutable[1] == ':')
1400 && (strchr(ProgDetails.pszExecutable, '\\'))
1401 )
1402 {
1403 ULONG ulAttr;
1404 if (!(arc = doshQueryPathAttr(ProgDetails.pszExecutable,
1405 &ulAttr)))
1406 {
1407 // make sure startup dir is really a directory
1408 if (ProgDetails.pszStartupDir)
1409 {
1410 // it is valid to specify a startup dir of "C:"
1411 if ( (strlen(ProgDetails.pszStartupDir) > 2)
1412 && (!(arc = doshQueryPathAttr(ProgDetails.pszStartupDir,
1413 &ulAttr)))
1414 && (!(ulAttr & FILE_DIRECTORY))
1415 )
1416 arc = ERROR_PATH_NOT_FOUND;
1417 }
1418 }
1419 }
1420 else
1421 {
1422 // _not_ fully qualified: look it up on the PATH then
1423 // V0.9.16 (2001-12-06) [umoeller]
1424 if (!(arc = doshSearchPath("PATH",
1425 ProgDetails.pszExecutable,
1426 szFQExecutable,
1427 sizeof(szFQExecutable))))
1428 // alright, found it:
1429 ProgDetails.pszExecutable = szFQExecutable;
1430 }
1431
1432 if (!arc)
1433 {
1434 PSZ pszExtension;
1435 switch (ProgDetails.progt.progc)
1436 {
1437 /*
1438 * .CMD files fixups
1439 *
1440 */
1441
1442 case PROG_FULLSCREEN: // OS/2 fullscreen
1443 case PROG_WINDOWABLEVIO: // OS/2 window
1444 {
1445 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
1446 && (!stricmp(pszExtension, "CMD"))
1447 )
1448 {
1449 CallBatchCorrectly(&ProgDetails,
1450 &strParamsPatched,
1451 "OS2_SHELL",
1452 "CMD.EXE");
1453 }
1454 break; }
1455
1456 case PROG_VDM: // DOS fullscreen
1457 case PROG_WINDOWEDVDM: // DOS window
1458 {
1459 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
1460 && (!stricmp(pszExtension, "BAT"))
1461 )
1462 {
1463 CallBatchCorrectly(&ProgDetails,
1464 &strParamsPatched,
1465 NULL,
1466 "COMMAND.COM");
1467 }
1468 break; }
1469 } // end switch (ProgDetails.progt.progc)
1470 }
1471 }
1472
1473 if (!arc)
1474 {
1475 if ( (ulIsWinApp)
1476 && ( (ProgDetails.pszEnvironment == NULL)
1477 || (!strlen(ProgDetails.pszEnvironment))
1478 )
1479 )
1480 {
1481 // this is a windoze app, and caller didn't bother
1482 // to give us an environment:
1483 // we MUST set one then, or we'll get the strangest
1484 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
1485
1486 DOSENVIRONMENT Env = {0};
1487
1488 // get standard WIN-OS/2 environment
1489 PSZ pszTemp = appQueryDefaultWin31Environment();
1490
1491 if (!(arc = appParseEnvironment(pszTemp,
1492 &Env)))
1493 {
1494 // now override KBD_CTRL_BYPASS=CTRL_ESC
1495 if ( (!(arc = appSetEnvironmentVar(&Env,
1496 "KBD_CTRL_BYPASS=CTRL_ESC",
1497 FALSE))) // add last
1498 && (!(arc = appConvertEnvironment(&Env,
1499 &pszWinOS2Env, // freed at bottom
1500 NULL)))
1501 )
1502 ProgDetails.pszEnvironment = pszWinOS2Env;
1503
1504 appFreeEnvironment(&Env);
1505 }
1506
1507 free(pszTemp);
1508 }
1509
1510 if (!arc)
1511 {
1512 if (!ProgDetails.pszTitle)
1513 ProgDetails.pszTitle = ProgDetails.pszExecutable;
1514
1515 ProgDetails.pszParameters = strParamsPatched.psz;
1516
1517 if (pszFailingName)
1518 strhncpy0(pszFailingName, ProgDetails.pszExecutable, cbFailingName);
1519
1520 arc = CallWinStartApp(phapp,
1521 hwndNotify,
1522 &ProgDetails,
1523 strParamsPatched.psz);
1524 }
1525 }
1526
1527 xstrClear(&strParamsPatched);
1528 if (pszWinOS2Env)
1529 free(pszWinOS2Env);
1530 } // end if (ProgDetails.pszExecutable)
1531
1532 #ifdef DEBUG_PROGRAMSTART
1533 _Pmpf((__FUNCTION__ ": returning %d", arc));
1534 #endif
1535
1536 return (arc);
1537}
1538
1539/*
1540 *@@ appWaitForApp:
1541 * waits for the specified application to terminate
1542 * and returns its exit code.
1543 *
1544 *@@added V0.9.9 (2001-03-07) [umoeller]
1545 */
1546
1547BOOL appWaitForApp(HWND hwndNotify, // in: notify window
1548 HAPP happ, // in: app to wait for
1549 PULONG pulExitCode) // out: exit code (ptr can be NULL)
1550{
1551 BOOL brc = FALSE;
1552
1553 if (happ)
1554 {
1555 // app started:
1556 // enter a modal message loop until we get the
1557 // WM_APPTERMINATENOTIFY for happ. Then we
1558 // know the app is done.
1559 HAB hab = WinQueryAnchorBlock(hwndNotify);
1560 QMSG qmsg;
1561 // ULONG ulXFixReturnCode = 0;
1562 while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
1563 {
1564 if ( (qmsg.msg == WM_APPTERMINATENOTIFY)
1565 && (qmsg.hwnd == hwndNotify)
1566 && (qmsg.mp1 == (MPARAM)happ)
1567 )
1568 {
1569 // xfix has terminated:
1570 // get xfix return code from mp2... this is:
1571 // -- 0: everything's OK, continue.
1572 // -- 1: handle section was rewritten, restart Desktop
1573 // now.
1574 if (pulExitCode)
1575 *pulExitCode = (ULONG)qmsg.mp2;
1576 brc = TRUE;
1577 // do not dispatch this
1578 break;
1579 }
1580
1581 WinDispatchMsg(hab, &qmsg);
1582 }
1583 }
1584
1585 return (brc);
1586}
1587
1588/*
1589 *@@ appQuickStartApp:
1590 * shortcut for simply starting an app and
1591 * waiting until it's finished.
1592 *
1593 * On errors, NULLHANDLE is returned.
1594 *
1595 * If pulReturnCode != NULL, it receives the
1596 * return code of the app.
1597 *
1598 *@@added V0.9.16 (2001-10-19) [umoeller]
1599 */
1600
1601HAPP appQuickStartApp(const char *pcszFile,
1602 ULONG ulProgType, // e.g. PROG_PM
1603 const char *pcszArgs,
1604 PULONG pulExitCode)
1605{
1606 PROGDETAILS pd = {0};
1607 HAPP happ,
1608 happReturn = NULLHANDLE;
1609 CHAR szDir[CCHMAXPATH] = "";
1610 PCSZ p;
1611 HWND hwndObject;
1612
1613 pd.Length = sizeof(pd);
1614 pd.progt.progc = ulProgType;
1615 pd.progt.fbVisible = SHE_VISIBLE;
1616 pd.pszExecutable = (PSZ)pcszFile;
1617 pd.pszParameters = (PSZ)pcszArgs;
1618 if (p = strrchr(pcszFile, '\\'))
1619 {
1620 strhncpy0(szDir,
1621 pcszFile,
1622 p - pcszFile);
1623 pd.pszStartupDir = szDir;
1624 }
1625
1626 if ( (hwndObject = winhCreateObjectWindow(WC_STATIC, NULL))
1627 && (!appStartApp(hwndObject,
1628 &pd,
1629 0,
1630 &happ,
1631 0,
1632 NULL))
1633 )
1634 {
1635 if (appWaitForApp(hwndObject,
1636 happ,
1637 pulExitCode))
1638 happReturn = happ;
1639 }
1640
1641 return (happReturn);
1642}
Note: See TracBrowser for help on using the repository browser.