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

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

Dialog formatter rewrite.

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