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

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

Minor adjustments for new static handling.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 71.2 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 *@@changed V0.9.21 (2002-08-21) [umoeller]: now allowing for UNC
804 */
805
806STATIC APIRET CheckAndQualifyExecutable(PPROGDETAILS pDetails, // in/out: program details
807 PXSTRING pstrExecutablePatched) // in/out: buffer for q'fied exec (must be init'ed)
808{
809 APIRET arc = NO_ERROR;
810
811 ULONG ulAttr;
812 // check if the executable is fully qualified; if so,
813 // check if the executable file exists
814 if ( // allow UNC V0.9.21 (2002-08-21) [umoeller]
815 ( (pDetails->pszExecutable[0] == '\\')
816 && (pDetails->pszExecutable[1] == '\\')
817 )
818 || ( (pDetails->pszExecutable[1] == ':')
819 && (strchr(pDetails->pszExecutable, '\\'))
820 )
821 )
822 {
823 arc = doshQueryPathAttr(pDetails->pszExecutable,
824 &ulAttr);
825 }
826 else
827 {
828 // _not_ fully qualified: look it up on the PATH then
829 // V0.9.16 (2001-12-06) [umoeller]
830 CHAR szFQExecutable[CCHMAXPATH];
831 if (!(arc = doshSearchPath("PATH",
832 pDetails->pszExecutable,
833 szFQExecutable,
834 sizeof(szFQExecutable))))
835 {
836 // alright, found it:
837 xstrcpy(pstrExecutablePatched, szFQExecutable, 0);
838 pDetails->pszExecutable = pstrExecutablePatched->psz;
839 }
840 }
841
842 return arc;
843}
844
845/*
846 *@@ CallBatchCorrectly:
847 * fixes the specified PROGDETAILS for
848 * command files in the executable part
849 * by inserting /C XXX into the parameters
850 * and setting the executable to the fully
851 * qualified command interpreter specified
852 * by the given environment variable.
853 *
854 *@@added V0.9.6 (2000-10-16) [umoeller]
855 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING
856 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
857 *@@changed V0.9.20 (2002-07-03) [umoeller]: now always qualifying executable to fix broken BAT files
858 *@@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
859 */
860
861STATIC APIRET CallBatchCorrectly(PPROGDETAILS pProgDetails,
862 PXSTRING pstrExecutablePatched, // in/out: buffer for q'fied exec (must be init'ed)
863 PXSTRING pstrParams, // in/out: modified parameters (reallocated)
864 const char *pcszEnvVar, // in: env var spec'g command proc
865 // (e.g. "OS2_SHELL"); can be NULL
866 const char *pcszDefProc) // in: def't command proc (e.g. "CMD.EXE")
867{
868 APIRET arc = NO_ERROR;
869
870 // XXX.CMD file as executable:
871 // fix args to /C XXX.CMD
872
873 PSZ pszOldParams = NULL;
874 ULONG ulOldParamsLength = pstrParams->ulLength;
875 BOOL fQuotes = FALSE;
876
877 if (ulOldParamsLength)
878 // we have parameters already:
879 // make a backup... we'll append that later
880 pszOldParams = strdup(pstrParams->psz);
881
882 // set new params to "/C filename.cmd"
883 xstrcpy(pstrParams, "/C ", 0);
884
885 // if the path has spaces, or other invalid characters,
886 // include it in quotes V0.9.21 (2002-08-12) [umoeller]
887 if (fQuotes = !!strpbrk(pProgDetails->pszExecutable, " +&|="))
888 xstrcatc(pstrParams, '"');
889 // @@bugbug "=" still doesn't work
890
891 #ifdef DEBUG_PROGRAMSTART
892 _PmpfF(("fQuotes (parameters need quotes) is %d", fQuotes));
893 #endif
894
895 xstrcat(pstrParams,
896 pProgDetails->pszExecutable,
897 0);
898
899 if (fQuotes)
900 xstrcatc(pstrParams, '"'); // V0.9.21 (2002-08-12) [umoeller]
901
902 if (pszOldParams)
903 {
904 // .cmd had params:
905 // append space and old params
906 xstrcatc(pstrParams, ' ');
907 xstrcat(pstrParams,
908 pszOldParams,
909 ulOldParamsLength);
910 free(pszOldParams);
911 }
912
913 // set executable to $(OS2_SHELL)
914 pProgDetails->pszExecutable = NULL;
915 if (pcszEnvVar)
916 pProgDetails->pszExecutable = getenv(pcszEnvVar);
917 if (!pProgDetails->pszExecutable)
918 pProgDetails->pszExecutable = (PSZ)pcszDefProc;
919 // should be on PATH
920
921 // and make sure this is always qualified
922 // V0.9.20 (2002-07-03) [umoeller]
923 return CheckAndQualifyExecutable(pProgDetails,
924 pstrExecutablePatched);
925}
926
927/*
928 *@@ appQueryDefaultWin31Environment:
929 * returns the default Win-OS/2 3.1 environment
930 * from OS2.INI, which you can then merge with
931 * your process environment to be able to
932 * start Win-OS/2 sessions properly with
933 * appStartApp.
934 *
935 * Caller must free() the return value.
936 *
937 *@@added V0.9.12 (2001-05-26) [umoeller]
938 *@@changed V0.9.19 (2002-03-28) [umoeller]: now returning APIRET
939 */
940
941APIRET appQueryDefaultWin31Environment(PSZ *ppsz)
942{
943 APIRET arc = NO_ERROR;
944 PSZ pszReturn = NULL;
945 ULONG ulSize = 0;
946
947 // get default environment (from Win-OS/2 settings object) from OS2.INI
948 PSZ pszDefEnv;
949 if (pszDefEnv = prfhQueryProfileData(HINI_USER,
950 "WINOS2",
951 "PM_GlobalWindows31Settings",
952 &ulSize))
953 {
954 if (pszReturn = (PSZ)malloc(ulSize + 2))
955 {
956 PSZ p;
957 memset(pszReturn, 0, ulSize + 2);
958 memcpy(pszReturn, pszDefEnv, ulSize);
959
960 for (p = pszReturn;
961 p < pszReturn + ulSize;
962 p++)
963 if (*p == ';')
964 *p = 0;
965
966 // okay.... now we got an OS/2-style environment
967 // with 0, 0, 00 strings
968
969 *ppsz = pszReturn;
970 }
971 else
972 arc = ERROR_NOT_ENOUGH_MEMORY;
973
974 free(pszDefEnv);
975 }
976 else
977 arc = ERROR_BAD_ENVIRONMENT;
978
979 return arc;
980}
981
982/*
983 *@@ appBuildProgDetails:
984 * extracted code from appStartApp to fix the
985 * given PROGDETAILS data to support the typical
986 * WPS stuff and allocate a single block of
987 * shared memory containing all the data.
988 *
989 * This is now used by XWP's progOpenProgram
990 * directly as a temporary fix for all the
991 * session hangs.
992 *
993 * As input, this takes a PROGDETAILS structure,
994 * which is converted in various ways. In detail,
995 * this supports:
996 *
997 * -- starting "*" executables (command prompts
998 * for OS/2, DOS, Win-OS/2);
999 *
1000 * -- starting ".CMD" and ".BAT" files as
1001 * PROGDETAILS.pszExecutable; for those, we
1002 * convert the executable and parameters to
1003 * start CMD.EXE or COMMAND.COM with the "/C"
1004 * parameter instead;
1005 *
1006 * -- starting apps which are not fully qualified
1007 * and therefore assumed to be on the PATH
1008 * (for which doshSearchPath("PATH") is called).
1009 *
1010 * Unless it is "*", PROGDETAILS.pszExecutable must
1011 * be a proper file name. The full path may be omitted
1012 * if it is on the PATH, but the extension (.EXE etc.)
1013 * must be given. You can use doshFindExecutable to
1014 * find executables if you don't know the extension.
1015 *
1016 * This also handles and merges special and default
1017 * environments for the app to be started. The
1018 * following should be respected:
1019 *
1020 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
1021 * is NULL, the new app inherits the default environment
1022 * from the shell.
1023 *
1024 * -- However, if you specify an environment, you _must_
1025 * specify a complete environment. This function
1026 * will not merge environments. Use
1027 * appSetEnvironmentVar to change environment
1028 * variables in a complete environment set.
1029 *
1030 * -- If PROGDETAILS specifies a Win-OS/2 session
1031 * and PROGDETAILS.pszEnvironment is empty,
1032 * this uses the default Win-OS/2 environment
1033 * from OS2.INI. See appQueryDefaultWin31Environment.
1034 *
1035 * Even though this isn't clearly said in PMREF,
1036 * PROGDETAILS.swpInitial is important:
1037 *
1038 * -- To start a session minimized, set fl to SWP_MINIMIZE.
1039 *
1040 * -- To start a VIO session with auto-close disabled,
1041 * set the half-documented SWP_NOAUTOCLOSE flag (0x8000)
1042 * This flag is now in the newer toolkit headers.
1043 *
1044 * In addition, this supports the following session
1045 * flags with ulFlags if PROG_DEFAULT is specified:
1046 *
1047 * -- APP_RUN_FULLSCREEN: start a fullscreen session
1048 * for VIO, DOS, and Win-OS/2 programs. Otherwise
1049 * we start a windowed or (share) a seamless session.
1050 * Ignored if the program is PM.
1051 *
1052 * -- APP_RUN_ENHANCED: for Win-OS/2 sessions, use
1053 * enhanced mode.
1054 * Ignored if the program is not Win-OS/2.
1055 *
1056 * -- APP_RUN_STANDARD: for Win-OS/2 sessions, use
1057 * standard mode.
1058 * Ignored if the program is not Win-OS/2.
1059 *
1060 * -- APP_RUN_SEPARATE: for Win-OS/2 sessions, use
1061 * a separate session.
1062 * Ignored if the program is not Win-OS/2.
1063 *
1064 * If NO_ERROR is returned, *ppDetails receives a
1065 * new buffer of shared memory containing all the
1066 * data packed together.
1067 *
1068 * The shared memory is allocated unnamed and
1069 * with OBJ_GETTABLE. It is the responsibility
1070 * of the caller to call DosFreeMem on that buffer.
1071 *
1072 * Returns:
1073 *
1074 * -- NO_ERROR
1075 *
1076 * -- ERROR_INVALID_PARAMETER: pcProgDetails or
1077 * ppDetails is NULL; or PROGDETAILS.pszExecutable is NULL.
1078 *
1079 * -- ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND:
1080 * PROGDETAILS.pszExecutable and/or PROGDETAILS.pszStartupDir
1081 * are invalid.
1082 * A NULL PROGDETAILS.pszStartupDir is supported though.
1083 *
1084 * -- ERROR_BAD_FORMAT
1085 *
1086 * -- ERROR_BAD_ENVIRONMENT: environment is larger than 60.000 bytes.
1087 *
1088 * -- ERROR_NOT_ENOUGH_MEMORY
1089 *
1090 * plus the error codes from doshQueryPathAttr, doshSearchPath,
1091 * appParseEnvironment, appSetEnvironmentVar, and appConvertEnvironment.
1092 *
1093 *@@added V0.9.18 (2002-03-27) [umoeller]
1094 *@@changed V0.9.19 (2002-03-28) [umoeller]: now allocating contiguous buffer
1095 *@@changed V0.9.20 (2002-07-03) [umoeller]: fixed Win-OS/2 full screen breakage
1096 *@@changed V0.9.20 (2002-07-03) [umoeller]: fixed broken bat and cmd files when PROG_DEFAULT was set
1097 *@@changed V0.9.21 (2002-08-18) [umoeller]: fixed cmd and bat files that had "=" in their paths
1098 */
1099
1100APIRET appBuildProgDetails(PPROGDETAILS *ppDetails, // out: shared mem with fixed program spec (req.)
1101 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
1102 ULONG ulFlags) // in: APP_RUN_* flags or 0
1103{
1104 APIRET arc = NO_ERROR;
1105
1106 XSTRING strExecutablePatched,
1107 strParamsPatched;
1108 PSZ pszWinOS2Env = 0;
1109
1110 PROGDETAILS Details;
1111 ULONG ulIsWinApp;
1112
1113 // parameter checking extended V0.9.21 (2002-08-21) [umoeller]
1114 if ( (!pcProgDetails)
1115 || (!pcProgDetails->pszExecutable)
1116 || (!pcProgDetails->pszExecutable[0])
1117 || (!ppDetails)
1118 )
1119 return ERROR_INVALID_PARAMETER;
1120
1121 *ppDetails = NULL;
1122
1123 /*
1124 * part 1:
1125 * fix up the PROGDETAILS fields
1126 */
1127
1128 xstrInit(&strExecutablePatched, 0);
1129 xstrInit(&strParamsPatched, 0);
1130
1131 memcpy(&Details, pcProgDetails, sizeof(PROGDETAILS));
1132 // pointers still point into old prog details buffer
1133 Details.Length = sizeof(PROGDETAILS);
1134 Details.progt.fbVisible = SHE_VISIBLE;
1135
1136 // memset(&Details.swpInitial, 0, sizeof(SWP));
1137 // this wasn't a good idea... WPProgram stores stuff
1138 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
1139
1140 // duplicate parameters...
1141 // we need this for string manipulations below...
1142 if ( (Details.pszParameters)
1143 && (Details.pszParameters[0]) // V0.9.18
1144 )
1145 xstrcpy(&strParamsPatched,
1146 Details.pszParameters,
1147 0);
1148
1149 #ifdef DEBUG_PROGRAMSTART
1150 _PmpfF((" old progc: 0x%lX", pcProgDetails->progt.progc));
1151 _Pmpf((" pszTitle: %s", STRINGORNULL(Details.pszTitle)));
1152 _Pmpf((" pszExecutable: %s", STRINGORNULL(Details.pszExecutable)));
1153 _Pmpf((" pszParameters: %s", STRINGORNULL(Details.pszParameters)));
1154 _Pmpf((" pszIcon: %s", STRINGORNULL(Details.pszIcon)));
1155 #endif
1156
1157 // program type fixups
1158 switch (Details.progt.progc) // that's a ULONG
1159 {
1160 case ((ULONG)-1): // we get that sometimes...
1161 case PROG_DEFAULT:
1162 {
1163 // V0.9.12 (2001-05-26) [umoeller]
1164 ULONG ulDosAppType;
1165 appQueryAppType(Details.pszExecutable,
1166 &ulDosAppType,
1167 &Details.progt.progc);
1168 }
1169 break;
1170 }
1171
1172 // set session type from option flags
1173 if (ulFlags & APP_RUN_FULLSCREEN)
1174 {
1175 if (Details.progt.progc == PROG_WINDOWABLEVIO)
1176 Details.progt.progc = PROG_FULLSCREEN;
1177 else if (Details.progt.progc == PROG_WINDOWEDVDM)
1178 Details.progt.progc = PROG_VDM;
1179 }
1180
1181 if (ulIsWinApp = appIsWindowsApp(Details.progt.progc))
1182 {
1183 if (ulFlags & APP_RUN_FULLSCREEN)
1184 Details.progt.progc = (ulFlags & APP_RUN_ENHANCED)
1185 ? PROG_31_ENH
1186 : PROG_31_STD;
1187 else
1188 {
1189 if (ulFlags & APP_RUN_STANDARD)
1190 Details.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1191 ? PROG_31_STDSEAMLESSVDM
1192 : PROG_31_STDSEAMLESSCOMMON;
1193 else if (ulFlags & APP_RUN_ENHANCED)
1194 Details.progt.progc = (ulFlags & APP_RUN_SEPARATE)
1195 ? PROG_31_ENHSEAMLESSVDM
1196 : PROG_31_ENHSEAMLESSCOMMON;
1197 }
1198
1199 // re-run V0.9.16 (2001-10-19) [umoeller]
1200 ulIsWinApp = appIsWindowsApp(Details.progt.progc);
1201 }
1202
1203 /*
1204 * command lines fixups:
1205 *
1206 */
1207
1208 if (!strcmp(Details.pszExecutable, "*"))
1209 {
1210 /*
1211 * "*" for command sessions:
1212 *
1213 */
1214
1215 if (ulIsWinApp)
1216 {
1217 // cheat: WinStartApp doesn't support NULL
1218 // for Win-OS2 sessions, so manually start winos2.com
1219 Details.pszExecutable = "WINOS2.COM";
1220 // this is a DOS app, so fix this to DOS fullscreen
1221 Details.progt.progc = PROG_VDM;
1222
1223 if (ulIsWinApp == 2)
1224 {
1225 // enhanced Win-OS/2 session:
1226 PSZ psz = NULL;
1227 if (strParamsPatched.ulLength)
1228 // "/3 " + existing params
1229 psz = strdup(strParamsPatched.psz);
1230
1231 xstrcpy(&strParamsPatched, "/3 ", 0);
1232
1233 if (psz)
1234 {
1235 xstrcat(&strParamsPatched, psz, 0);
1236 free(psz);
1237 }
1238 }
1239 }
1240 else
1241 // for all other executable types
1242 // (including OS/2 and DOS sessions),
1243 // set pszExecutable to NULL; this will
1244 // have WinStartApp start a cmd shell
1245 Details.pszExecutable = NULL;
1246
1247 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
1248
1249 // else
1250
1251 // no, this else breaks the WINOS2.COM hack above... we
1252 // need to look for that on the PATH as well
1253 // V0.9.20 (2002-07-03) [umoeller]
1254 if (Details.pszExecutable)
1255 {
1256 // check the executable and look for it on the
1257 // PATH if necessary
1258 if (!(arc = CheckAndQualifyExecutable(&Details,
1259 &strExecutablePatched)))
1260 {
1261 PSZ pszExtension;
1262
1263 // make sure startup dir is really a directory
1264 // V0.9.20 (2002-07-03) [umoeller]: moved this down
1265 if (Details.pszStartupDir)
1266 {
1267 ULONG ulAttr;
1268 // it is valid to specify a startup dir of "C:"
1269 if ( (strlen(Details.pszStartupDir) > 2)
1270 && (!(arc = doshQueryPathAttr(Details.pszStartupDir,
1271 &ulAttr)))
1272 && (!(ulAttr & FILE_DIRECTORY))
1273 )
1274 arc = ERROR_PATH_NOT_FOUND;
1275 }
1276
1277// V0.9.21: this define is never set. I have thus completely
1278// disabled the batch hacks that we used to provide, that is
1279// we no longer change the "c:\path\batch.cmd" to "cmd.exe /c c:\path\batch.cmd"
1280// because it is perfectly valid to call WinStartApp with a
1281// batch file. The problem with my code was that cmd.exe has
1282// a weird bug in that if you give it something via /c that
1283// has an equals character (=) in its path, e.g. "c:\path=path\batch.cmd",
1284// the command parser apparently stops at the first "=" and
1285// reports "c:\path" not found or something. What a bitch.
1286#ifdef ENABLEBATCHHACKS
1287
1288 // we frequently get here for BAT and CMD files
1289 // with progtype == PROG_DEFAULT, so include
1290 // that in the check, or all BAT files will fail
1291 // V0.9.20 (2002-07-03) [umoeller]
1292
1293 switch (Details.progt.progc)
1294 {
1295 /*
1296 * .CMD files fixups
1297 *
1298 */
1299
1300 case PROG_DEFAULT: // V0.9.20 (2002-07-03) [umoeller]
1301 case PROG_FULLSCREEN: // OS/2 fullscreen
1302 case PROG_WINDOWABLEVIO: // OS/2 window
1303 {
1304 if ( (pszExtension = doshGetExtension(Details.pszExecutable))
1305 && (!stricmp(pszExtension, "CMD"))
1306 )
1307 {
1308 arc = CallBatchCorrectly(&Details,
1309 &strExecutablePatched,
1310 &strParamsPatched,
1311 "OS2_SHELL",
1312 "CMD.EXE");
1313 }
1314 }
1315 break;
1316 }
1317
1318 switch (Details.progt.progc)
1319 {
1320 case PROG_DEFAULT: // V0.9.20 (2002-07-03) [umoeller]
1321 case PROG_VDM: // DOS fullscreen
1322 case PROG_WINDOWEDVDM: // DOS window
1323 {
1324 if ( (pszExtension = doshGetExtension(Details.pszExecutable))
1325 && (!stricmp(pszExtension, "BAT"))
1326 )
1327 {
1328 arc = CallBatchCorrectly(&Details,
1329 &strExecutablePatched,
1330 &strParamsPatched,
1331 // there is no environment variable
1332 // for the DOS shell
1333 NULL,
1334 "COMMAND.COM");
1335 }
1336 }
1337 break;
1338 } // end switch (Details.progt.progc)
1339#endif // ENABLEBATCHHACKS
1340 }
1341 }
1342
1343 if (!arc)
1344 {
1345 if ( (ulIsWinApp)
1346 && ( (!(Details.pszEnvironment))
1347 || (!(*Details.pszEnvironment))
1348 )
1349 )
1350 {
1351 // this is a windoze app, and caller didn't bother
1352 // to give us an environment:
1353 // we MUST set one then, or we'll get the strangest
1354 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
1355
1356 DOSENVIRONMENT Env = {0};
1357
1358 // get standard WIN-OS/2 environment
1359 PSZ pszTemp;
1360 if (!(arc = appQueryDefaultWin31Environment(&pszTemp)))
1361 {
1362 if (!(arc = appParseEnvironment(pszTemp,
1363 &Env)))
1364 {
1365 // now override KBD_CTRL_BYPASS=CTRL_ESC
1366 if ( (!(arc = appSetEnvironmentVar(&Env,
1367 "KBD_CTRL_BYPASS=CTRL_ESC",
1368 FALSE))) // add last
1369 && (!(arc = appConvertEnvironment(&Env,
1370 &pszWinOS2Env, // freed at bottom
1371 NULL)))
1372 )
1373 Details.pszEnvironment = pszWinOS2Env;
1374
1375 appFreeEnvironment(&Env);
1376 }
1377
1378 free(pszTemp);
1379 }
1380 }
1381
1382 if (!arc)
1383 {
1384 // if no title is given, use the executable
1385 if (!Details.pszTitle)
1386 Details.pszTitle = Details.pszExecutable;
1387
1388 // make sure params have a leading space
1389 // V0.9.18 (2002-03-27) [umoeller]
1390 if (strParamsPatched.ulLength)
1391 {
1392 if (strParamsPatched.psz[0] != ' ')
1393 {
1394 XSTRING str2;
1395 xstrInit(&str2, 0);
1396 xstrcpy(&str2, " ", 1);
1397 xstrcats(&str2, &strParamsPatched);
1398 xstrcpys(&strParamsPatched, &str2);
1399 xstrClear(&str2);
1400 // we really need xstrInsert or something
1401 }
1402 Details.pszParameters = strParamsPatched.psz;
1403 }
1404 else
1405 // never pass null pointers
1406 Details.pszParameters = "";
1407
1408 // never pass null pointers
1409 if (!Details.pszIcon)
1410 Details.pszIcon = "";
1411
1412 // never pass null pointers
1413 if (!Details.pszStartupDir)
1414 Details.pszStartupDir = "";
1415
1416 }
1417 }
1418
1419 /*
1420 * part 2:
1421 * pack the fixed PROGDETAILS fields
1422 */
1423
1424 if (!arc)
1425 {
1426 ULONG cb,
1427 cbTitle,
1428 cbExecutable,
1429 cbParameters,
1430 cbStartupDir,
1431 cbIcon,
1432 cbEnvironment;
1433
1434 #ifdef DEBUG_PROGRAMSTART
1435 _PmpfF((" new progc: 0x%lX", pcProgDetails->progt.progc));
1436 _Pmpf((" pszTitle: %s", STRINGORNULL(Details.pszTitle)));
1437 _Pmpf((" pszExecutable: %s", STRINGORNULL(Details.pszExecutable)));
1438 _Pmpf((" pszParameters: %s", STRINGORNULL(Details.pszParameters)));
1439 _Pmpf((" pszIcon: %s", STRINGORNULL(Details.pszIcon)));
1440 #endif
1441
1442 // allocate a chunk of tiled memory from OS/2 to make sure
1443 // this is aligned on a 64K memory (backed up by a 16-bit
1444 // LDT selector); if it is not, and the environment
1445 // crosses segments, it gets truncated!!
1446 cb = sizeof(PROGDETAILS);
1447 if (cbTitle = strhSize(Details.pszTitle))
1448 cb += cbTitle;
1449
1450 if (cbExecutable = strhSize(Details.pszExecutable))
1451 cb += cbExecutable;
1452
1453 if (cbParameters = strhSize(Details.pszParameters))
1454 cb += cbParameters;
1455
1456 if (cbStartupDir = strhSize(Details.pszStartupDir))
1457 cb += cbStartupDir;
1458
1459 if (cbIcon = strhSize(Details.pszIcon))
1460 cb += cbIcon;
1461
1462 if (cbEnvironment = appQueryEnvironmentLen(Details.pszEnvironment))
1463 cb += cbEnvironment;
1464
1465 if (cb > 60000) // to be on the safe side
1466 arc = ERROR_BAD_ENVIRONMENT; // 10;
1467 else
1468 {
1469 PPROGDETAILS pNewProgDetails;
1470 // alright, allocate the shared memory now
1471 if (!(arc = DosAllocSharedMem((PVOID*)&pNewProgDetails,
1472 NULL,
1473 cb,
1474 PAG_COMMIT | OBJ_GETTABLE | OBJ_TILE | PAG_EXECUTE | PAG_READ | PAG_WRITE)))
1475 {
1476 // and copy stuff
1477 PBYTE pThis;
1478
1479 memset(pNewProgDetails, 0, cb);
1480
1481 pNewProgDetails->Length = sizeof(PROGDETAILS);
1482
1483 pNewProgDetails->progt.progc = Details.progt.progc;
1484
1485 pNewProgDetails->progt.fbVisible = Details.progt.fbVisible;
1486 memcpy(&pNewProgDetails->swpInitial, &Details.swpInitial, sizeof(SWP));
1487
1488 // start copying into buffer right after PROGDETAILS
1489 pThis = (PBYTE)(pNewProgDetails + 1);
1490
1491 // handy macro to avoid typos
1492 #define COPY(id) if (cb ## id) { \
1493 memcpy(pThis, Details.psz ## id, cb ## id); \
1494 pNewProgDetails->psz ## id = pThis; \
1495 pThis += cb ## id; }
1496
1497 COPY(Title);
1498 COPY(Executable);
1499 COPY(Parameters);
1500 COPY(StartupDir);
1501 COPY(Icon);
1502 COPY(Environment);
1503
1504 *ppDetails = pNewProgDetails;
1505 }
1506 }
1507 }
1508
1509 xstrClear(&strParamsPatched);
1510 xstrClear(&strExecutablePatched);
1511
1512 if (pszWinOS2Env)
1513 free(pszWinOS2Env);
1514
1515 return arc;
1516}
1517
1518/*
1519 *@@ CallDosStartSession:
1520 *
1521 *@@added V0.9.18 (2002-03-27) [umoeller]
1522 */
1523
1524STATIC APIRET CallDosStartSession(HAPP *phapp,
1525 const PROGDETAILS *pNewProgDetails, // in: program spec (req.)
1526 ULONG cbFailingName,
1527 PSZ pszFailingName)
1528{
1529 APIRET arc = NO_ERROR;
1530
1531 BOOL fCrit = FALSE,
1532 fResetDir = FALSE;
1533 CHAR szCurrentDir[CCHMAXPATH];
1534
1535 ULONG sid,
1536 pid;
1537 STARTDATA SData;
1538 SData.Length = sizeof(STARTDATA);
1539 SData.Related = SSF_RELATED_INDEPENDENT; // SSF_RELATED_CHILD;
1540 // per default, try to start this in the foreground
1541 SData.FgBg = SSF_FGBG_FORE;
1542 SData.TraceOpt = SSF_TRACEOPT_NONE;
1543
1544 SData.PgmTitle = pNewProgDetails->pszTitle;
1545 SData.PgmName = pNewProgDetails->pszExecutable;
1546 SData.PgmInputs = pNewProgDetails->pszParameters;
1547
1548 SData.TermQ = NULL;
1549 SData.Environment = pNewProgDetails->pszEnvironment;
1550 SData.InheritOpt = SSF_INHERTOPT_PARENT; // ignored
1551
1552 switch (pNewProgDetails->progt.progc)
1553 {
1554 case PROG_FULLSCREEN:
1555 SData.SessionType = SSF_TYPE_FULLSCREEN;
1556 break;
1557
1558 case PROG_WINDOWABLEVIO:
1559 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
1560 break;
1561
1562 case PROG_PM:
1563 SData.SessionType = SSF_TYPE_PM;
1564 SData.FgBg = SSF_FGBG_BACK; // otherwise we get ERROR_SMG_START_IN_BACKGROUND
1565 break;
1566
1567 case PROG_VDM:
1568 SData.SessionType = SSF_TYPE_VDM;
1569 break;
1570
1571 case PROG_WINDOWEDVDM:
1572 SData.SessionType = SSF_TYPE_WINDOWEDVDM;
1573 break;
1574
1575 default:
1576 SData.SessionType = SSF_TYPE_DEFAULT;
1577 }
1578
1579 SData.IconFile = 0;
1580 SData.PgmHandle = 0;
1581
1582 SData.PgmControl = 0;
1583
1584 if (pNewProgDetails->progt.fbVisible == SHE_VISIBLE)
1585 SData.PgmControl |= SSF_CONTROL_VISIBLE;
1586
1587 if (pNewProgDetails->swpInitial.fl & SWP_HIDE)
1588 SData.PgmControl |= SSF_CONTROL_INVISIBLE;
1589
1590 if (pNewProgDetails->swpInitial.fl & SWP_MAXIMIZE)
1591 SData.PgmControl |= SSF_CONTROL_MAXIMIZE;
1592 if (pNewProgDetails->swpInitial.fl & SWP_MINIMIZE)
1593 {
1594 SData.PgmControl |= SSF_CONTROL_MINIMIZE;
1595 // use background then
1596 SData.FgBg = SSF_FGBG_BACK;
1597 }
1598 if (pNewProgDetails->swpInitial.fl & SWP_MOVE)
1599 SData.PgmControl |= SSF_CONTROL_SETPOS;
1600 if (pNewProgDetails->swpInitial.fl & SWP_NOAUTOCLOSE)
1601 SData.PgmControl |= SSF_CONTROL_NOAUTOCLOSE;
1602
1603 SData.InitXPos = pNewProgDetails->swpInitial.x;
1604 SData.InitYPos = pNewProgDetails->swpInitial.y;
1605 SData.InitXSize = pNewProgDetails->swpInitial.cx;
1606 SData.InitYSize = pNewProgDetails->swpInitial.cy;
1607
1608 SData.Reserved = 0;
1609 SData.ObjectBuffer = pszFailingName;
1610 SData.ObjectBuffLen = cbFailingName;
1611
1612 // now, if a required module cannot be found,
1613 // DosStartSession still returns ERROR_FILE_NOT_FOUND
1614 // (2), but pszFailingName will be set to something
1615 // meaningful... so set it to a null string first
1616 // and we can then check if it has changed
1617 if (pszFailingName)
1618 *pszFailingName = '\0';
1619
1620 TRY_QUIET(excpt1)
1621 {
1622 if ( (pNewProgDetails->pszStartupDir)
1623 && (pNewProgDetails->pszStartupDir[0])
1624 )
1625 {
1626 fCrit = !DosEnterCritSec();
1627 if ( (!(arc = doshQueryCurrentDir(szCurrentDir)))
1628 && (!(arc = doshSetCurrentDir(pNewProgDetails->pszStartupDir)))
1629 )
1630 fResetDir = TRUE;
1631 }
1632
1633 if ( (!arc)
1634 && (!(arc = DosStartSession(&SData, &sid, &pid)))
1635 )
1636 {
1637 // app started:
1638 // compose HAPP from that
1639 *phapp = sid;
1640 }
1641 else if (pszFailingName && *pszFailingName)
1642 // DosStartSession has set this to something
1643 // other than NULL: then use error code 1804,
1644 // as cmd.exe does
1645 arc = 1804;
1646 }
1647 CATCH(excpt1)
1648 {
1649 arc = ERROR_PROTECTION_VIOLATION;
1650 } END_CATCH();
1651
1652 if (fResetDir)
1653 doshSetCurrentDir(szCurrentDir);
1654
1655 if (fCrit)
1656 DosExitCritSec();
1657
1658 #ifdef DEBUG_PROGRAMSTART
1659 _Pmpf((" DosStartSession returned %d, pszFailingName: \"%s\"",
1660 arc, pszFailingName));
1661 #endif
1662
1663 return arc;
1664}
1665
1666/*
1667 *@@ CallWinStartApp:
1668 * wrapper around WinStartApp which copies all the
1669 * parameters into a contiguous block of tiled memory.
1670 *
1671 * This might fix some of the problems with truncated
1672 * environments we were having because apparently the
1673 * WinStartApp thunking to 16-bit doesn't always work.
1674 *
1675 *@@added V0.9.18 (2002-02-13) [umoeller]
1676 *@@changed V0.9.18 (2002-03-27) [umoeller]: made failing modules work
1677 */
1678
1679STATIC APIRET CallWinStartApp(HAPP *phapp, // out: application handle if NO_ERROR is returned
1680 HWND hwndNotify, // in: notify window or NULLHANDLE
1681 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
1682 ULONG cbFailingName,
1683 PSZ pszFailingName)
1684{
1685 APIRET arc = NO_ERROR;
1686
1687 if (!pcProgDetails)
1688 return ERROR_INVALID_PARAMETER;
1689
1690 if (pszFailingName)
1691 *pszFailingName = '\0';
1692
1693 if (!(*phapp = WinStartApp(hwndNotify,
1694 // receives WM_APPTERMINATENOTIFY
1695 (PPROGDETAILS)pcProgDetails,
1696 pcProgDetails->pszParameters,
1697 NULL, // "reserved", PMREF says...
1698 SAF_INSTALLEDCMDLINE)))
1699 // we MUST use SAF_INSTALLEDCMDLINE
1700 // or no Win-OS/2 session will start...
1701 // whatever is going on here... Warp 4 FP11
1702
1703 // do not use SAF_STARTCHILDAPP, or the
1704 // app will be terminated automatically
1705 // when the calling process terminates!
1706 {
1707 // cannot start app:
1708 PERRINFO pei;
1709
1710 #ifdef DEBUG_PROGRAMSTART
1711 _Pmpf((__FUNCTION__ ": WinStartApp failed"));
1712 #endif
1713
1714 // unfortunately WinStartApp doesn't
1715 // return meaningful codes like DosStartSession, so
1716 // try to see what happened
1717
1718 if (pei = WinGetErrorInfo(0))
1719 {
1720 #ifdef DEBUG_PROGRAMSTART
1721 _Pmpf((" WinGetErrorInfo returned 0x%lX, errorid 0x%lX, %d",
1722 pei,
1723 pei->idError,
1724 ERRORIDERROR(pei->idError)));
1725 #endif
1726
1727 switch (ERRORIDERROR(pei->idError))
1728 {
1729 case PMERR_DOS_ERROR: // (0x1200)
1730 {
1731 /*
1732 PUSHORT pausMsgOfs = (PUSHORT)(((PBYTE)pei) + pei->offaoffszMsg);
1733 PULONG pulData = (PULONG)(((PBYTE)pei) + pei->offBinaryData);
1734 PSZ pszMsg = (PSZ)(((PBYTE)pei) + *pausMsgOfs);
1735
1736 CHAR szMsg[1000];
1737 sprintf(szMsg, "cDetail: %d\nmsg: %s\n*pul: %d",
1738 pei->cDetailLevel,
1739 pszMsg,
1740 *(pulData - 1));
1741
1742 WinMessageBox(HWND_DESKTOP,
1743 NULLHANDLE,
1744 szMsg,
1745 "Error",
1746 0,
1747 MB_OK | MB_MOVEABLE);
1748
1749 // Very helpful. The message is "UNK 1200 E",
1750 // where I assume "UNK" means "unknown", which is
1751 // exactly what I was trying to find out. Oh my.
1752 // And cDetailLevel is always 1, which isn't terribly
1753 // helpful either. V0.9.18 (2002-03-27) [umoeller]
1754 // WHO THE &%õ$ CREATED THESE APIS?
1755
1756 */
1757
1758 // this is probably the case where the module
1759 // couldn't be loaded, so try DosStartSession
1760 // to get a meaningful return code... note that
1761 // this cannot handle hwndNotify then
1762 /* arc = CallDosStartSession(phapp,
1763 pcProgDetails,
1764 cbFailingName,
1765 pszFailingName); */
1766 arc = ERROR_FILE_NOT_FOUND;
1767 }
1768 break;
1769
1770 case PMERR_INVALID_APPL: // (0x1530)
1771 // Attempted to start an application whose type is not
1772 // recognized by OS/2.
1773 // This we get also if the executable doesn't exist...
1774 // V0.9.18 (2002-03-27) [umoeller]
1775 // arc = ERROR_INVALID_EXE_SIGNATURE;
1776 arc = ERROR_FILE_NOT_FOUND;
1777 break;
1778
1779 case PMERR_INVALID_PARAMETERS: // (0x1208)
1780 // An application parameter value is invalid for
1781 // its converted PM type. For example: a 4-byte
1782 // value outside the range -32 768 to +32 767 cannot be
1783 // converted to a SHORT, and a negative number cannot
1784 // be converted to a ULONG or USHORT.
1785 arc = ERROR_INVALID_DATA;
1786 break;
1787
1788 case PMERR_STARTED_IN_BACKGROUND: // (0x1532)
1789 // The application started a new session in the
1790 // background.
1791 arc = ERROR_SMG_START_IN_BACKGROUND;
1792 break;
1793
1794 case PMERR_INVALID_WINDOW: // (0x1206)
1795 // The window specified with a Window List call
1796 // is not a valid frame window.
1797
1798 default:
1799 arc = ERROR_BAD_FORMAT;
1800 break;
1801 }
1802
1803 WinFreeErrorInfo(pei);
1804 }
1805 }
1806
1807 return arc;
1808}
1809
1810/*
1811 *@@ appStartApp:
1812 * wrapper around WinStartApp which fixes the
1813 * specified PROGDETAILS to (hopefully) work
1814 * work with all executable types.
1815 *
1816 * This first calls appBuildProgDetails (see
1817 * remarks there) and then calls WinStartApp.
1818 *
1819 * Since this calls WinStartApp in turn, this
1820 * requires a message queue on the calling thread.
1821 *
1822 * Note that this also does minimal checking on
1823 * the specified parameters so it can return something
1824 * more meaningful than FALSE like WinStartApp.
1825 * As a result, you get a DOS error code now (V0.9.16).
1826 *
1827 * Most importantly:
1828 *
1829 * -- ERROR_INVALID_THREADID: not running on thread 1.
1830 * See remarks below.
1831 *
1832 * -- ERROR_NOT_ENOUGH_MEMORY
1833 *
1834 * plus the many error codes from appBuildProgDetails,
1835 * which gets called in turn.
1836 *
1837 * <B>About enforcing thread 1</B>
1838 *
1839 * OK, after long, long debugging hours, I have found
1840 * that WinStartApp hangs the system in the following
1841 * cases hard:
1842 *
1843 * -- If a Win-OS/2 session is started and WinStartApp
1844 * is _not_ on thread 1. For this reason, we check
1845 * if the caller is trying to start a Win-OS/2
1846 * session and return ERROR_INVALID_THREADID if
1847 * this is not running on thread 1.
1848 *
1849 * -- By contrast, there are many situations where
1850 * calling WinStartApp from within the Workplace
1851 * process will hang the system, most notably
1852 * with VIO sessions. I have been unable to figure
1853 * out why this happens, so XWorkplace now uses
1854 * its daemon to call WinStartApp instead.
1855 *
1856 * As a word of wisdom, do not call this from
1857 * within the Workplace process. For some strange
1858 * reason though, the XWorkplace "Run" dialog
1859 * (which uses this) _does_ work. Whatever.
1860 *
1861 *@@added V0.9.6 (2000-10-16) [umoeller]
1862 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
1863 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
1864 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
1865 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
1866 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
1867 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
1868 *@@changed V0.9.14 (2001-08-23) [pr]: added session type options
1869 *@@changed V0.9.16 (2001-10-19) [umoeller]: added prototype to return APIRET
1870 *@@changed V0.9.16 (2001-10-19) [umoeller]: added thread-1 check
1871 *@@changed V0.9.16 (2001-12-06) [umoeller]: now using doshSearchPath for finding pszExecutable if not qualified
1872 *@@changed V0.9.16 (2002-01-04) [umoeller]: removed error report if startup directory was drive letter only
1873 *@@changed V0.9.16 (2002-01-04) [umoeller]: added more detailed error reports and *FailingName params
1874 *@@changed V0.9.18 (2002-02-13) [umoeller]: added CallWinStartApp to fix possible memory problems
1875 *@@changed V0.9.18 (2002-03-27) [umoeller]: no longer returning ERROR_INVALID_THREADID, except for Win-OS/2 sessions
1876 *@@changed V0.9.18 (2002-03-27) [umoeller]: extracted appBuildProgDetails
1877 *@@changed V0.9.19 (2002-03-28) [umoeller]: adjusted for new appBuildProgDetails
1878 */
1879
1880APIRET appStartApp(HWND hwndNotify, // in: notify window or NULLHANDLE
1881 const PROGDETAILS *pcProgDetails, // in: program spec (req.)
1882 ULONG ulFlags, // in: APP_RUN_* flags or 0
1883 HAPP *phapp, // out: application handle if NO_ERROR is returned
1884 ULONG cbFailingName,
1885 PSZ pszFailingName)
1886{
1887 APIRET arc;
1888
1889 PPROGDETAILS pDetails;
1890
1891 if (!phapp)
1892 return ERROR_INVALID_PARAMETER;
1893
1894 if (!(arc = appBuildProgDetails(&pDetails,
1895 pcProgDetails,
1896 ulFlags)))
1897 {
1898 if (pszFailingName)
1899 strhncpy0(pszFailingName, pDetails->pszExecutable, cbFailingName);
1900
1901 if ( (appIsWindowsApp(pDetails->progt.progc))
1902 && (doshMyTID() != 1) // V0.9.16 (2001-10-19) [umoeller]
1903 )
1904 arc = ERROR_INVALID_THREADID;
1905 else
1906 arc = CallWinStartApp(phapp,
1907 hwndNotify,
1908 pDetails,
1909 cbFailingName,
1910 pszFailingName);
1911
1912 DosFreeMem(pDetails);
1913
1914 } // end if (ProgDetails.pszExecutable)
1915
1916 #ifdef DEBUG_PROGRAMSTART
1917 _Pmpf((__FUNCTION__ ": returning %d", arc));
1918 #endif
1919
1920 return arc;
1921}
1922
1923/*
1924 *@@ appWaitForApp:
1925 * waits for the specified application to terminate
1926 * and returns its exit code.
1927 *
1928 *@@added V0.9.9 (2001-03-07) [umoeller]
1929 */
1930
1931BOOL appWaitForApp(HWND hwndNotify, // in: notify window
1932 HAPP happ, // in: app to wait for
1933 PULONG pulExitCode) // out: exit code (ptr can be NULL)
1934{
1935 BOOL brc = FALSE;
1936
1937 if (happ)
1938 {
1939 // app started:
1940 // enter a modal message loop until we get the
1941 // WM_APPTERMINATENOTIFY for happ. Then we
1942 // know the app is done.
1943 HAB hab = WinQueryAnchorBlock(hwndNotify);
1944 QMSG qmsg;
1945 // ULONG ulXFixReturnCode = 0;
1946 while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
1947 {
1948 if ( (qmsg.msg == WM_APPTERMINATENOTIFY)
1949 && (qmsg.hwnd == hwndNotify)
1950 && (qmsg.mp1 == (MPARAM)happ)
1951 )
1952 {
1953 // xfix has terminated:
1954 // get xfix return code from mp2... this is:
1955 // -- 0: everything's OK, continue.
1956 // -- 1: handle section was rewritten, restart Desktop
1957 // now.
1958 if (pulExitCode)
1959 *pulExitCode = (ULONG)qmsg.mp2;
1960 brc = TRUE;
1961 // do not dispatch this
1962 break;
1963 }
1964
1965 WinDispatchMsg(hab, &qmsg);
1966 }
1967 }
1968
1969 return brc;
1970}
1971
1972/*
1973 *@@ appQuickStartApp:
1974 * shortcut for simply starting an app.
1975 *
1976 * On errors, NULLHANDLE is returned.
1977 *
1978 * Only if pulExitCode != NULL, we wait for
1979 * the app to complete and return the
1980 * exit code.
1981 *
1982 *@@added V0.9.16 (2001-10-19) [umoeller]
1983 *@@changed V0.9.20 (2002-08-10) [umoeller]: fixed missing destroy window, made wait optional
1984 *@@changed V0.9.20 (2002-08-10) [umoeller]: added pcszWorkingDir
1985 *@@changed V0.9.21 (2002-08-18) [umoeller]: changed prototype to return APIRET
1986 */
1987
1988APIRET appQuickStartApp(const char *pcszFile,
1989 ULONG ulProgType, // e.g. PROG_PM
1990 const char *pcszArgs, // in: arguments (can be NULL)
1991 const char *pcszWorkingDir, // in: working dir (can be NULL)
1992 HAPP *phapp,
1993 PULONG pulExitCode) // out: exit code; if ptr is NULL, we don't wait
1994{
1995 APIRET arc = NO_ERROR;
1996 PROGDETAILS pd = {0};
1997 HAPP happReturn = NULLHANDLE;
1998 CHAR szDir[CCHMAXPATH] = "";
1999 PCSZ p;
2000 HWND hwndObject = NULLHANDLE;
2001
2002 pd.Length = sizeof(pd);
2003 pd.progt.progc = ulProgType;
2004 pd.progt.fbVisible = SHE_VISIBLE;
2005 pd.pszExecutable = (PSZ)pcszFile;
2006 pd.pszParameters = (PSZ)pcszArgs;
2007
2008 if ( (!(pd.pszStartupDir = (PSZ)pcszWorkingDir))
2009 && (p = strrchr(pcszFile, '\\'))
2010 )
2011 {
2012 strhncpy0(szDir,
2013 pcszFile,
2014 p - pcszFile);
2015 pd.pszStartupDir = szDir;
2016 }
2017
2018 if (pulExitCode)
2019 if (!(hwndObject = winhCreateObjectWindow(WC_STATIC, NULL)))
2020 arc = ERROR_NOT_ENOUGH_MEMORY;
2021
2022 if ( (!arc)
2023 && (!(arc = appStartApp(hwndObject,
2024 &pd,
2025 0,
2026 phapp,
2027 0,
2028 NULL)))
2029 )
2030 {
2031 if (pulExitCode)
2032 appWaitForApp(hwndObject,
2033 *phapp,
2034 pulExitCode);
2035 }
2036
2037 if (hwndObject)
2038 WinDestroyWindow(hwndObject); // was missing V0.9.20 (2002-08-10) [umoeller]
2039
2040 return arc;
2041}
2042
2043/*
2044 *@@ appOpenURL:
2045 * opens the system default browser with the given
2046 * URL.
2047 *
2048 * We return TRUE if appQuickStartApp succeeded with
2049 * that URL.
2050 *
2051 *@@added V0.9.20 (2002-08-10) [umoeller]
2052 *@@changed V0.9.21 (2002-08-21) [umoeller]: changed prototype to return browser
2053 */
2054
2055APIRET appOpenURL(PCSZ pcszURL, // in: URL to open
2056 PSZ pszAppStarted, // out: application that was started (req.)
2057 ULONG cbAppStarted) // in: size of that buffer
2058{
2059 APIRET arc = ERROR_NO_DATA;
2060
2061 CHAR szStartupDir[CCHMAXPATH];
2062 XSTRING strParameters;
2063
2064 if ( (!pcszURL)
2065 || (!pszAppStarted)
2066 || (!cbAppStarted)
2067 )
2068 return ERROR_INVALID_PARAMETER;
2069
2070 xstrInit(&strParameters, 0);
2071
2072 if (PrfQueryProfileString(HINI_USER,
2073 "WPURLDEFAULTSETTINGS",
2074 "DefaultBrowserExe",
2075 "NETSCAPE.EXE",
2076 pszAppStarted,
2077 cbAppStarted))
2078 {
2079 PSZ pszDefParams;
2080 HAPP happ;
2081
2082 if (pszDefParams = prfhQueryProfileData(HINI_USER,
2083 "WPURLDEFAULTSETTINGS",
2084 "DefaultParameters",
2085 NULL))
2086 {
2087 xstrcpy(&strParameters, pszDefParams, 0);
2088 xstrcatc(&strParameters, ' ');
2089 free(pszDefParams);
2090 }
2091
2092 xstrcat(&strParameters, pcszURL, 0);
2093
2094 PrfQueryProfileString(HINI_USER,
2095 "WPURLDEFAULTSETTINGS",
2096 "DefaultWorkingDir",
2097 "",
2098 szStartupDir,
2099 sizeof(szStartupDir));
2100
2101 arc = appQuickStartApp(pszAppStarted,
2102 PROG_DEFAULT,
2103 strParameters.psz,
2104 szStartupDir,
2105 &happ,
2106 NULL); // don't wait
2107 }
2108
2109 xstrClear(&strParameters);
2110
2111 return arc;
2112}
Note: See TracBrowser for help on using the repository browser.