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

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

Buncha fixes, plus Paul's screen wrap feature.

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