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

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

PageMage fixes et al.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 56.3 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 _Pmpf((__FUNCTION__ ": progt.progc: %d", pNewProgDetails->progt.progc));
996 _Pmpf((" progt.fbVisible: 0x%lX", pNewProgDetails->progt.fbVisible));
997 _Pmpf((" progt.pszTitle: \"%s\"", (pNewProgDetails->pszTitle) ? pNewProgDetails->pszTitle : "NULL"));
998 _Pmpf((" exec: \"%s\"", (pNewProgDetails->pszExecutable) ? pNewProgDetails->pszExecutable : "NULL"));
999 _Pmpf((" params: \"%s\"", (pNewProgDetails->pszParameters) ? pNewProgDetails->pszParameters : "NULL"));
1000 _Pmpf((" startup: \"%s\"", (pNewProgDetails->pszStartupDir) ? pNewProgDetails->pszStartupDir : "NULL"));
1001 _Pmpf((" pszIcon: \"%s\"", (pNewProgDetails->pszIcon) ? pNewProgDetails->pszIcon : "NULL"));
1002 _Pmpf((" environment: "));
1003 {
1004 PSZ pszThis = pNewProgDetails->pszEnvironment;
1005 while (pszThis && *pszThis)
1006 {
1007 _Pmpf((" \"%s\"", pszThis));
1008 pszThis += strlen(pszThis) + 1;
1009 }
1010 }
1011
1012 _Pmpf((" swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
1013 pNewProgDetails->swpInitial.fl,
1014 pNewProgDetails->swpInitial.x,
1015 pNewProgDetails->swpInitial.y,
1016 pNewProgDetails->swpInitial.cx,
1017 pNewProgDetails->swpInitial.cy));
1018 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
1019 pNewProgDetails->swpInitial.hwndInsertBehind,
1020 pNewProgDetails->swpInitial.hwnd,
1021 pNewProgDetails->swpInitial.ulReserved1,
1022 pNewProgDetails->swpInitial.ulReserved2));
1023
1024 if (!(*phapp = WinStartApp(hwndNotify,
1025 // receives WM_APPTERMINATENOTIFY
1026 pNewProgDetails,
1027 pNewProgDetails->pszParameters,
1028 NULL, // "reserved", PMREF says...
1029 SAF_INSTALLEDCMDLINE)))
1030 // we MUST use SAF_INSTALLEDCMDLINE
1031 // or no Win-OS/2 session will start...
1032 // whatever is going on here... Warp 4 FP11
1033
1034 // do not use SAF_STARTCHILDAPP, or the
1035 // app will be terminated automatically
1036 // when the WPS terminates!
1037 {
1038 // cannot start app:
1039 // _Pmpf((__FUNCTION__ ": WinStartApp failed"));
1040 arc = ERROR_FILE_NOT_FOUND;
1041 // unfortunately WinStartApp doesn't
1042 // return meaningful codes like DosStartSession, so
1043 // try to see what happened
1044 /*
1045 switch (ERRORIDERROR(WinGetLastError(0)))
1046 {
1047 case PMERR_DOS_ERROR: // (0x1200)
1048 {
1049 arc = ERROR_FILE_NOT_FOUND;
1050
1051 // this is probably the case where the module
1052 // couldn't be loaded, so try DosStartSession
1053 // to get a meaningful return code... note that
1054 // this cannot handle hwndNotify then
1055 RESULTCODES result;
1056 arc = DosExecPgm(pszFailingName,
1057 cbFailingName,
1058 EXEC_ASYNC,
1059 NULL, // ProgDetails.pszParameters,
1060 NULL, // ProgDetails.pszEnvironment,
1061 &result,
1062 ProgDetails.pszExecutable);
1063 ULONG sid, pid;
1064 STARTDATA SData;
1065 SData.Length = sizeof(STARTDATA);
1066 SData.Related = SSF_RELATED_CHILD; //INDEPENDENT;
1067 SData.FgBg = SSF_FGBG_FORE;
1068 SData.TraceOpt = SSF_TRACEOPT_NONE;
1069
1070 SData.PgmTitle = ProgDetails.pszTitle;
1071 SData.PgmName = ProgDetails.pszExecutable;
1072 SData.PgmInputs = ProgDetails.pszParameters;
1073
1074 SData.TermQ = NULL;
1075 SData.Environment = ProgDetails.pszEnvironment;
1076 SData.InheritOpt = SSF_INHERTOPT_PARENT; // ignored
1077 SData.SessionType = SSF_TYPE_DEFAULT;
1078 SData.IconFile = 0;
1079 SData.PgmHandle = 0;
1080
1081 SData.PgmControl = SSF_CONTROL_VISIBLE;
1082
1083 SData.InitXPos = 30;
1084 SData.InitYPos = 40;
1085 SData.InitXSize = 200;
1086 SData.InitYSize = 140;
1087 SData.Reserved = 0;
1088 SData.ObjectBuffer = pszFailingName;
1089 SData.ObjectBuffLen = cbFailingName;
1090
1091 arc = DosStartSession(&SData, &sid, &pid);
1092 }
1093 break;
1094
1095 case PMERR_INVALID_APPL: // (0x1530)
1096 // Attempted to start an application whose type is not
1097 // recognized by OS/2.
1098 arc = ERROR_INVALID_EXE_SIGNATURE;
1099 break;
1100
1101 case PMERR_INVALID_PARAMETERS: // (0x1208)
1102 // An application parameter value is invalid for
1103 // its converted PM type. For example: a 4-byte
1104 // value outside the range -32 768 to +32 767 cannot be
1105 // converted to a SHORT, and a negative number cannot
1106 // be converted to a ULONG or USHORT.
1107 arc = ERROR_INVALID_DATA;
1108 break;
1109
1110 case PMERR_STARTED_IN_BACKGROUND: // (0x1532)
1111 // The application started a new session in the
1112 // background.
1113 arc = ERROR_SMG_START_IN_BACKGROUND;
1114 break;
1115
1116 case PMERR_INVALID_WINDOW: // (0x1206)
1117 // The window specified with a Window List call
1118 // is not a valid frame window.
1119
1120 default:
1121 arc = ERROR_BAD_FORMAT;
1122 break;
1123 }
1124 */
1125 }
1126
1127 DosFreeMem(pNewProgDetails);
1128 }
1129 }
1130
1131 return (arc);
1132}
1133
1134/*
1135 *@@ appStartApp:
1136 * wrapper around WinStartApp which fixes the
1137 * specified PROGDETAILS to (hopefully) work
1138 * work with all executable types.
1139 *
1140 * This fixes the executable info to support:
1141 *
1142 * -- starting "*" executables (command prompts
1143 * for OS/2, DOS, Win-OS/2);
1144 *
1145 * -- starting ".CMD" and ".BAT" files as
1146 * PROGDETAILS.pszExecutable;
1147 *
1148 * -- starting apps which are not fully qualified
1149 * and therefore assumed to be on the PATH.
1150 *
1151 * Unless it is "*", PROGDETAILS.pszExecutable must
1152 * be a proper file name. The full path may be omitted
1153 * if it is on the PATH, but the extension (.EXE etc.)
1154 * must be given. You can use doshFindExecutable to
1155 * find executables if you don't know the extension.
1156 *
1157 * This also handles and merges special and default
1158 * environments for the app to be started. The
1159 * following should be respected:
1160 *
1161 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
1162 * is NULL, the new app inherits a default environment
1163 * from the shell.
1164 *
1165 * -- However, if you specify an environment, you _must_
1166 * specify a complete environment. This function
1167 * will not merge environments. Use
1168 * appSetEnvironmentVar to change environment
1169 * variables in a complete environment set.
1170 *
1171 * -- If PROGDETAILS specifies a Win-OS/2 session
1172 * and PROGDETAILS.pszEnvironment is empty,
1173 * this uses the default Win-OS/2 environment.
1174 * See appQueryDefaultWin31Environment.
1175 *
1176 * Even though this isn't clearly said in PMREF,
1177 * PROGDETAILS.swpInitial is important:
1178 *
1179 * -- To start a session minimized, set SWP_MINIMIZE.
1180 *
1181 * -- To start a VIO session with auto-close disabled,
1182 * set the half-documented SWP_NOAUTOCLOSE flag (0x8000)
1183 * This flag is now in the newer toolkit headers.
1184 *
1185 * In addition, this supports the following session
1186 * flags with ulFlags if PROG_DEFAULT is specified:
1187 *
1188 * -- APP_RUN_FULLSCREEN
1189 *
1190 * -- APP_RUN_ENHANCED
1191 *
1192 * -- APP_RUN_STANDARD
1193 *
1194 * -- APP_RUN_SEPARATE
1195 *
1196 * Since this calls WinStartApp in turn, this
1197 * requires a message queue on the calling thread.
1198 *
1199 * Note that this also does minimal checking on
1200 * the specified parameters so it can return something
1201 * more meaningful than FALSE like WinStartApp.
1202 * As a result, you get a DOS error code now (V0.9.16).
1203 *
1204 * Most importantly:
1205 *
1206 * -- ERROR_INVALID_THREADID: not running on thread 1.
1207 * Since this uses WinStartApp internally and
1208 * WinStartApp completely hangs the session manager
1209 * if a Win-OS/2 full-screen session is started from
1210 * a thread that is NOT thread 1, this will now fail
1211 * with this error for safety (V0.9.16).
1212 *
1213 * -- ERROR_INVALID_PARAMETER: pcProgDetails or
1214 * phapp is NULL; or PROGDETAILS.pszExecutable is NULL.
1215 *
1216 * -- ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND:
1217 * PROGDETAILS.pszExecutable and/or PROGDETAILS.pszStartupDir
1218 * are invalid.
1219 * A NULL PROGDETAILS.pszStartupDir is supported though.
1220 *
1221 * -- ERROR_NOT_ENOUGH_MEMORY
1222 *
1223 *@@added V0.9.6 (2000-10-16) [umoeller]
1224 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
1225 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
1226 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
1227 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
1228 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
1229 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
1230 *@@changed V0.9.14 (2001-08-23) [pr]: added session type options
1231 *@@changed V0.9.16 (2001-10-19) [umoeller]: added prototype to return APIRET
1232 *@@changed V0.9.16 (2001-10-19) [umoeller]: added thread-1 check
1233 *@@changed V0.9.16 (2001-12-06) [umoeller]: now using doshSearchPath for finding pszExecutable if not qualified
1234 *@@changed V0.9.16 (2002-01-04) [umoeller]: removed error report if startup directory was drive letter only
1235 *@@changed V0.9.16 (2002-01-04) [umoeller]: added more detailed error reports and *FailingName params
1236 *@@changed V0.9.18 (2002-02-13) [umoeller]: added CallWinStartApp to fix possible memory problems
1237 */
1238
1239APIRET appStartApp(HWND hwndNotify, // in: notify window or NULLHANDLE
1240 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
1241 ULONG ulFlags, // in: APP_RUN_* flags
1242 HAPP *phapp, // out: application handle if NO_ERROR is returned
1243 ULONG cbFailingName,
1244 PSZ pszFailingName)
1245{
1246 APIRET arc = NO_ERROR;
1247 PROGDETAILS ProgDetails;
1248
1249 if (pszFailingName)
1250 *pszFailingName = '\0';
1251
1252 if (!pcProgDetails || !phapp)
1253 return (ERROR_INVALID_PARAMETER);
1254
1255 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
1256 // pointers still point into old prog details buffer
1257 ProgDetails.Length = sizeof(PROGDETAILS);
1258 ProgDetails.progt.fbVisible = SHE_VISIBLE;
1259
1260 // all this only makes sense if this contains something...
1261 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
1262 if ( (!ProgDetails.pszExecutable)
1263 || (!(*(ProgDetails.pszExecutable)))
1264 )
1265 arc = ERROR_INVALID_PARAMETER;
1266 else if (doshMyTID() != 1) // V0.9.16 (2001-10-19) [umoeller]
1267 arc = ERROR_INVALID_THREADID;
1268 else
1269 {
1270 ULONG ulIsWinApp;
1271
1272 CHAR szFQExecutable[CCHMAXPATH];
1273
1274 XSTRING strParamsPatched;
1275 PSZ pszWinOS2Env = 0;
1276
1277 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
1278 // this wasn't a good idea... WPProgram stores stuff
1279 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
1280
1281 // duplicate parameters...
1282 // we need this for string manipulations below...
1283 if (ProgDetails.pszParameters)
1284 xstrInitCopy(&strParamsPatched,
1285 ProgDetails.pszParameters,
1286 100);
1287 else
1288 // no old params:
1289 xstrInit(&strParamsPatched, 100);
1290
1291 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
1292 // _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
1293 // _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
1294
1295 // program type fixups
1296 switch (ProgDetails.progt.progc) // that's a ULONG
1297 {
1298 case ((ULONG)-1): // we get that sometimes...
1299 case PROG_DEFAULT:
1300 {
1301 // V0.9.12 (2001-05-26) [umoeller]
1302 ULONG ulDosAppType;
1303 appQueryAppType(ProgDetails.pszExecutable,
1304 &ulDosAppType,
1305 &ProgDetails.progt.progc);
1306 }
1307 break;
1308 }
1309
1310 // set session type from option flags
1311 if (ulFlags & APP_RUN_FULLSCREEN)
1312 {
1313 if (ProgDetails.progt.progc == PROG_WINDOWABLEVIO)
1314 ProgDetails.progt.progc = PROG_FULLSCREEN;
1315
1316 if (ProgDetails.progt.progc == PROG_WINDOWEDVDM)
1317 ProgDetails.progt.progc = PROG_VDM;
1318 }
1319
1320 if (ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc))
1321 {
1322 if (ulFlags & APP_RUN_FULLSCREEN)
1323 ProgDetails.progt.progc = (ulFlags & APP_RUN_ENHANCED)
1324 ? PROG_31_ENH
1325 : PROG_31_STD;
1326 else
1327 {
1328 if (ulFlags & APP_RUN_STANDARD)
1329 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1330 ? PROG_31_STDSEAMLESSVDM
1331 : PROG_31_STDSEAMLESSCOMMON;
1332
1333 if (ulFlags & APP_RUN_ENHANCED)
1334 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1335 ? PROG_31_ENHSEAMLESSVDM
1336 : PROG_31_ENHSEAMLESSCOMMON;
1337 }
1338
1339 // re-run V0.9.16 (2001-10-19) [umoeller]
1340 ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc);
1341 }
1342
1343 /*
1344 * command lines fixups:
1345 *
1346 */
1347
1348 if (!strcmp(ProgDetails.pszExecutable, "*"))
1349 {
1350 /*
1351 * "*" for command sessions:
1352 *
1353 */
1354
1355 if (ulIsWinApp == 2)
1356 {
1357 // enhanced Win-OS/2 session:
1358 PSZ psz = NULL;
1359 if (strParamsPatched.ulLength)
1360 // "/3 " + existing params
1361 psz = strdup(strParamsPatched.psz);
1362
1363 xstrcpy(&strParamsPatched, "/3 ", 0);
1364
1365 if (psz)
1366 {
1367 xstrcat(&strParamsPatched, psz, 0);
1368 free(psz);
1369 }
1370 }
1371
1372 if (ulIsWinApp)
1373 {
1374 // cheat: WinStartApp doesn't support NULL
1375 // for Win-OS2 sessions, so manually start winos2.com
1376 ProgDetails.pszExecutable = "WINOS2.COM";
1377 // this is a DOS app, so fix this to DOS fullscreen
1378 ProgDetails.progt.progc = PROG_VDM;
1379 }
1380 else
1381 // for all other executable types
1382 // (including OS/2 and DOS sessions),
1383 // set pszExecutable to NULL; this will
1384 // have WinStartApp start a cmd shell
1385 ProgDetails.pszExecutable = NULL;
1386
1387 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
1388 else
1389 {
1390 // check if the executable is fully qualified; if so,
1391 // check if the executable file exists
1392 if ( (ProgDetails.pszExecutable[1] == ':')
1393 && (strchr(ProgDetails.pszExecutable, '\\'))
1394 )
1395 {
1396 ULONG ulAttr;
1397 if (!(arc = doshQueryPathAttr(ProgDetails.pszExecutable,
1398 &ulAttr)))
1399 {
1400 // make sure startup dir is really a directory
1401 if (ProgDetails.pszStartupDir)
1402 {
1403 // it is valid to specify a startup dir of "C:"
1404 if ( (strlen(ProgDetails.pszStartupDir) > 2)
1405 && (!(arc = doshQueryPathAttr(ProgDetails.pszStartupDir,
1406 &ulAttr)))
1407 && (!(ulAttr & FILE_DIRECTORY))
1408 )
1409 arc = ERROR_PATH_NOT_FOUND;
1410 }
1411 }
1412 }
1413 else
1414 {
1415 // _not_ fully qualified: look it up on the PATH then
1416 // V0.9.16 (2001-12-06) [umoeller]
1417 if (!(arc = doshSearchPath("PATH",
1418 ProgDetails.pszExecutable,
1419 szFQExecutable,
1420 sizeof(szFQExecutable))))
1421 // alright, found it:
1422 ProgDetails.pszExecutable = szFQExecutable;
1423 }
1424
1425 if (!arc)
1426 {
1427 PSZ pszExtension;
1428 switch (ProgDetails.progt.progc)
1429 {
1430 /*
1431 * .CMD files fixups
1432 *
1433 */
1434
1435 case PROG_FULLSCREEN: // OS/2 fullscreen
1436 case PROG_WINDOWABLEVIO: // OS/2 window
1437 {
1438 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
1439 && (!stricmp(pszExtension, "CMD"))
1440 )
1441 {
1442 CallBatchCorrectly(&ProgDetails,
1443 &strParamsPatched,
1444 "OS2_SHELL",
1445 "CMD.EXE");
1446 }
1447 break; }
1448
1449 case PROG_VDM: // DOS fullscreen
1450 case PROG_WINDOWEDVDM: // DOS window
1451 {
1452 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
1453 && (!stricmp(pszExtension, "BAT"))
1454 )
1455 {
1456 CallBatchCorrectly(&ProgDetails,
1457 &strParamsPatched,
1458 NULL,
1459 "COMMAND.COM");
1460 }
1461 break; }
1462 } // end switch (ProgDetails.progt.progc)
1463 }
1464 }
1465
1466 if (!arc)
1467 {
1468 if ( (ulIsWinApp)
1469 && ( (ProgDetails.pszEnvironment == NULL)
1470 || (!strlen(ProgDetails.pszEnvironment))
1471 )
1472 )
1473 {
1474 // this is a windoze app, and caller didn't bother
1475 // to give us an environment:
1476 // we MUST set one then, or we'll get the strangest
1477 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
1478
1479 DOSENVIRONMENT Env = {0};
1480
1481 // get standard WIN-OS/2 environment
1482 PSZ pszTemp = appQueryDefaultWin31Environment();
1483
1484 if (!(arc = appParseEnvironment(pszTemp,
1485 &Env)))
1486 {
1487 // now override KBD_CTRL_BYPASS=CTRL_ESC
1488 if ( (!(arc = appSetEnvironmentVar(&Env,
1489 "KBD_CTRL_BYPASS=CTRL_ESC",
1490 FALSE))) // add last
1491 && (!(arc = appConvertEnvironment(&Env,
1492 &pszWinOS2Env, // freed at bottom
1493 NULL)))
1494 )
1495 ProgDetails.pszEnvironment = pszWinOS2Env;
1496
1497 appFreeEnvironment(&Env);
1498 }
1499
1500 free(pszTemp);
1501 }
1502
1503 if (!arc)
1504 {
1505 if (!ProgDetails.pszTitle)
1506 ProgDetails.pszTitle = ProgDetails.pszExecutable;
1507
1508 ProgDetails.pszParameters = strParamsPatched.psz;
1509
1510 if (pszFailingName)
1511 strhncpy0(pszFailingName, ProgDetails.pszExecutable, cbFailingName);
1512
1513 arc = CallWinStartApp(phapp,
1514 hwndNotify,
1515 &ProgDetails,
1516 strParamsPatched.psz);
1517 }
1518 }
1519
1520 xstrClear(&strParamsPatched);
1521 if (pszWinOS2Env)
1522 free(pszWinOS2Env);
1523 } // end if (ProgDetails.pszExecutable)
1524
1525 // _Pmpf((__FUNCTION__ ": returning %d", arc));
1526
1527 return (arc);
1528}
1529
1530/*
1531 *@@ appWaitForApp:
1532 * waits for the specified application to terminate
1533 * and returns its exit code.
1534 *
1535 *@@added V0.9.9 (2001-03-07) [umoeller]
1536 */
1537
1538BOOL appWaitForApp(HWND hwndNotify, // in: notify window
1539 HAPP happ, // in: app to wait for
1540 PULONG pulExitCode) // out: exit code (ptr can be NULL)
1541{
1542 BOOL brc = FALSE;
1543
1544 if (happ)
1545 {
1546 // app started:
1547 // enter a modal message loop until we get the
1548 // WM_APPTERMINATENOTIFY for happ. Then we
1549 // know the app is done.
1550 HAB hab = WinQueryAnchorBlock(hwndNotify);
1551 QMSG qmsg;
1552 // ULONG ulXFixReturnCode = 0;
1553 while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
1554 {
1555 if ( (qmsg.msg == WM_APPTERMINATENOTIFY)
1556 && (qmsg.hwnd == hwndNotify)
1557 && (qmsg.mp1 == (MPARAM)happ)
1558 )
1559 {
1560 // xfix has terminated:
1561 // get xfix return code from mp2... this is:
1562 // -- 0: everything's OK, continue.
1563 // -- 1: handle section was rewritten, restart Desktop
1564 // now.
1565 if (pulExitCode)
1566 *pulExitCode = (ULONG)qmsg.mp2;
1567 brc = TRUE;
1568 // do not dispatch this
1569 break;
1570 }
1571
1572 WinDispatchMsg(hab, &qmsg);
1573 }
1574 }
1575
1576 return (brc);
1577}
1578
1579/*
1580 *@@ appQuickStartApp:
1581 * shortcut for simply starting an app and
1582 * waiting until it's finished.
1583 *
1584 * On errors, NULLHANDLE is returned.
1585 *
1586 * If pulReturnCode != NULL, it receives the
1587 * return code of the app.
1588 *
1589 *@@added V0.9.16 (2001-10-19) [umoeller]
1590 */
1591
1592HAPP appQuickStartApp(const char *pcszFile,
1593 ULONG ulProgType, // e.g. PROG_PM
1594 const char *pcszArgs,
1595 PULONG pulExitCode)
1596{
1597 PROGDETAILS pd = {0};
1598 HAPP happ,
1599 happReturn = NULLHANDLE;
1600 CHAR szDir[CCHMAXPATH] = "";
1601 PCSZ p;
1602 HWND hwndObject;
1603
1604 pd.Length = sizeof(pd);
1605 pd.progt.progc = ulProgType;
1606 pd.progt.fbVisible = SHE_VISIBLE;
1607 pd.pszExecutable = (PSZ)pcszFile;
1608 pd.pszParameters = (PSZ)pcszArgs;
1609 if (p = strrchr(pcszFile, '\\'))
1610 {
1611 strhncpy0(szDir,
1612 pcszFile,
1613 p - pcszFile);
1614 pd.pszStartupDir = szDir;
1615 }
1616
1617 if ( (hwndObject = winhCreateObjectWindow(WC_STATIC, NULL))
1618 && (!appStartApp(hwndObject,
1619 &pd,
1620 0,
1621 &happ,
1622 0,
1623 NULL))
1624 )
1625 {
1626 if (appWaitForApp(hwndObject,
1627 happ,
1628 pulExitCode))
1629 happReturn = happ;
1630 }
1631
1632 return (happReturn);
1633}
Note: See TracBrowser for help on using the repository browser.