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

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

Misc changes.

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