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

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

Minor updates.

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