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

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

Rewrote winlist widget to use daemon.

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