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

Last change on this file since 106 was 105, checked in by umoeller, 24 years ago

Misc changes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 36.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-2001 Ulrich M”ller.
18 * This file is part of the "XWorkplace helpers" source package.
19 * This is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published
21 * by the Free Software Foundation, in version 2 as it comes in the
22 * "COPYING" file of the XWorkplace main distribution.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 */
28
29#define OS2EMX_PLAIN_CHAR
30 // this is needed for "os2emx.h"; if this is defined,
31 // emx will define PSZ as _signed_ char, otherwise
32 // as unsigned char
33
34#define INCL_DOSPROCESS
35#define INCL_DOSSESMGR
36#define INCL_DOSERRORS
37
38#define INCL_WINPROGRAMLIST // needed for PROGDETAILS, wppgm.h
39#include <os2.h>
40
41#include <stdio.h>
42
43#include "setup.h" // code generation and debugging options
44
45#include "helpers\apps.h"
46#include "helpers\dosh.h"
47#include "helpers\prfh.h"
48#include "helpers\stringh.h"
49#include "helpers\xstring.h"
50
51/*
52 *@@category: Helpers\PM helpers\Application helpers
53 */
54
55/* ******************************************************************
56 *
57 * Environment helpers
58 *
59 ********************************************************************/
60
61/*
62 *@@ appParseEnvironment:
63 * this takes one of those ugly environment strings
64 * as used by DosStartSession and WinStartApp (with
65 * lots of zero-terminated strings one after another
66 * and a duplicate zero byte as a terminator) as
67 * input and splits it into an array of separate
68 * strings in pEnv.
69 *
70 * The newly allocated strings are stored in in
71 * pEnv->papszVars. The array count is stored in
72 * pEnv->cVars.
73 *
74 * Each environment variable will be copied into
75 * one newly allocated string in the array. Use
76 * appFreeEnvironment to free the memory allocated
77 * by this function.
78 *
79 * Use the following code to browse thru the array:
80 +
81 + DOSENVIRONMENT Env = {0};
82 + if (appParseEnvironment(pszEnv,
83 + &Env)
84 + == NO_ERROR)
85 + {
86 + if (Env.papszVars)
87 + {
88 + PSZ *ppszThis = Env.papszVars;
89 + for (ul = 0;
90 + ul < Env.cVars;
91 + ul++)
92 + {
93 + PSZ pszThis = *ppszThis;
94 + // pszThis now has something like PATH=C:\TEMP
95 + // ...
96 + // next environment string
97 + ppszThis++;
98 + }
99 + }
100 + appFreeEnvironment(&Env);
101 + }
102 *
103 *@@added V0.9.4 (2000-08-02) [umoeller]
104 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
105 */
106
107APIRET appParseEnvironment(const char *pcszEnv,
108 PDOSENVIRONMENT pEnv)
109{
110 APIRET arc = NO_ERROR;
111 if (!pcszEnv)
112 arc = ERROR_INVALID_PARAMETER;
113 else
114 {
115 PSZ pszVarThis = (PSZ)pcszEnv;
116 ULONG cVars = 0;
117 // count strings
118 while (*pszVarThis)
119 {
120 cVars++;
121 pszVarThis += strlen(pszVarThis) + 1;
122 }
123
124 pEnv->cVars = cVars;
125 pEnv->papszVars = 0;
126
127 if (cVars)
128 {
129 PSZ *papsz = (PSZ*)malloc(sizeof(PSZ) * cVars);
130 if (!papsz)
131 arc = ERROR_NOT_ENOUGH_MEMORY;
132 else
133 {
134 PSZ *ppszTarget = papsz;
135 memset(papsz, 0, sizeof(PSZ) * cVars);
136 pszVarThis = (PSZ)pcszEnv;
137 while (*pszVarThis)
138 {
139 *ppszTarget = strdup(pszVarThis);
140 ppszTarget++;
141 pszVarThis += strlen(pszVarThis) + 1;
142 }
143
144 pEnv->papszVars = papsz;
145 }
146 }
147 }
148
149 return (arc);
150}
151
152/*
153 *@@ appGetEnvironment:
154 * calls appParseEnvironment for the current
155 * process environment, which is retrieved from
156 * the info blocks.
157 *
158 *@@added V0.9.4 (2000-07-19) [umoeller]
159 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
160 */
161
162APIRET appGetEnvironment(PDOSENVIRONMENT pEnv)
163{
164 APIRET arc = NO_ERROR;
165 if (!pEnv)
166 arc = ERROR_INVALID_PARAMETER;
167 else
168 {
169 PTIB ptib = 0;
170 PPIB ppib = 0;
171 arc = DosGetInfoBlocks(&ptib, &ppib);
172 if (arc == NO_ERROR)
173 {
174 PSZ pszEnv = ppib->pib_pchenv;
175 if (pszEnv)
176 arc = appParseEnvironment(pszEnv, pEnv);
177 else
178 arc = ERROR_BAD_ENVIRONMENT;
179 }
180 }
181
182 return (arc);
183}
184
185/*
186 *@@ appFindEnvironmentVar:
187 * returns the PSZ* in the pEnv->papszVars array
188 * which specifies the environment variable in pszVarName.
189 *
190 * With pszVarName, you can either specify the variable
191 * name only ("VARNAME") or a full environment string
192 * ("VARNAME=BLAH"). In any case, only the variable name
193 * is compared.
194 *
195 * Returns NULL if no such variable name was found in
196 * the array.
197 *
198 *@@added V0.9.4 (2000-07-19) [umoeller]
199 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
200 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
201 */
202
203PSZ* appFindEnvironmentVar(PDOSENVIRONMENT pEnv,
204 PSZ pszVarName)
205{
206 PSZ *ppszRet = 0;
207 if (pEnv)
208 {
209 if ((pEnv->papszVars) && (pszVarName))
210 {
211 PSZ *ppszThis = pEnv->papszVars;
212 // PSZ pszThis;
213 ULONG ul = 0;
214 ULONG ulVarNameLen = 0;
215
216 PSZ pszSearch = NULL; // receives "VARNAME="
217 PSZ pFirstEqual = strchr(pszVarName, '=');
218 if (pFirstEqual)
219 pszSearch = strhSubstr(pszVarName, pFirstEqual + 1);
220 else
221 {
222 ulVarNameLen = strlen(pszVarName);
223 pszSearch = (PSZ)malloc(ulVarNameLen + 2);
224 memcpy(pszSearch, pszVarName, ulVarNameLen);
225 *(pszSearch + ulVarNameLen) = '=';
226 *(pszSearch + ulVarNameLen + 1) = 0;
227 }
228
229 ulVarNameLen = strlen(pszSearch);
230
231 for (ul = 0;
232 ul < pEnv->cVars;
233 ul++)
234 {
235 if (strnicmp(*ppszThis, pszSearch, ulVarNameLen) == 0)
236 {
237 ppszRet = ppszThis;
238 break;
239 }
240
241 // next environment string
242 ppszThis++;
243 }
244
245 free(pszSearch); // was missing V0.9.12 (2001-05-21) [umoeller]
246 }
247 }
248
249 return (ppszRet);
250}
251
252/*
253 *@@ appSetEnvironmentVar:
254 * sets an environment variable in the specified
255 * environment, which must have been initialized
256 * using appGetEnvironment first.
257 *
258 * pszNewEnv must be a full environment string
259 * in the form "VARNAME=VALUE".
260 *
261 * If "VARNAME" has already been set to something
262 * in the string array in pEnv, that array item
263 * is replaced.
264 *
265 * OTOH, if "VARNAME" has not been set yet, a new
266 * item is added to the array, and pEnv->cVars is
267 * raised by one. In that case, fAddFirst determines
268 * whether the new array item is added to the front
269 * or the tail of the environment list.
270 *
271 *@@added V0.9.4 (2000-07-19) [umoeller]
272 *@@changed V0.9.7 (2000-12-17) [umoeller]: added fAddFirst
273 *@@changed V0.9.12 (2001-05-21) [umoeller]: fixed memory leak
274 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed crash if !fAddFirst
275 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
276 */
277
278APIRET appSetEnvironmentVar(PDOSENVIRONMENT pEnv,
279 PSZ pszNewEnv,
280 BOOL fAddFirst)
281{
282 APIRET arc = NO_ERROR;
283 if ((!pEnv) || (!pszNewEnv))
284 arc = ERROR_INVALID_PARAMETER;
285 else
286 {
287 if (!pEnv->papszVars)
288 {
289 // no variables set yet:
290 pEnv->papszVars = (PSZ*)malloc(sizeof(PSZ));
291 pEnv->cVars = 1;
292
293 *(pEnv->papszVars) = strdup(pszNewEnv);
294 }
295 else
296 {
297 PSZ *ppszEnvLine = appFindEnvironmentVar(pEnv, pszNewEnv);
298 if (ppszEnvLine)
299 {
300 // was set already: replace
301 free(*ppszEnvLine);
302 *ppszEnvLine = strdup(pszNewEnv);
303 if (!(*ppszEnvLine))
304 arc = ERROR_NOT_ENOUGH_MEMORY;
305 }
306 else
307 {
308 // not set already:
309 PSZ *ppszNew = NULL;
310
311 // allocate new array, with one new entry
312 // fixed V0.9.12 (2001-05-26) [umoeller], this crashed
313 PSZ *papszNew = (PSZ*)malloc(sizeof(PSZ) * (pEnv->cVars + 1));
314
315 if (!papszNew)
316 arc = ERROR_NOT_ENOUGH_MEMORY;
317 else
318 {
319 if (fAddFirst)
320 {
321 // add as first entry:
322 // overwrite first entry
323 ppszNew = papszNew;
324 // copy old entries
325 memcpy(papszNew + 1, // second new entry
326 pEnv->papszVars, // first old entry
327 sizeof(PSZ) * pEnv->cVars);
328 }
329 else
330 {
331 // append at the tail:
332 // overwrite last entry
333 ppszNew = papszNew + pEnv->cVars;
334 // copy old entries
335 memcpy(papszNew, // first new entry
336 pEnv->papszVars, // first old entry
337 sizeof(PSZ) * pEnv->cVars);
338 }
339
340 if (pEnv->papszVars)
341 free(pEnv->papszVars); // was missing V0.9.12 (2001-05-21) [umoeller]
342
343 pEnv->papszVars = papszNew;
344 pEnv->cVars++;
345 *ppszNew = strdup(pszNewEnv);
346 }
347 }
348 }
349 }
350
351 return (arc);
352}
353
354/*
355 *@@ appConvertEnvironment:
356 * converts an environment initialized by appGetEnvironment
357 * to the string format required by WinStartApp and DosExecPgm,
358 * that is, one memory block is allocated in *ppszEnv and all
359 * strings in pEnv->papszVars are copied to that block. Each
360 * string is terminated with a null character; the last string
361 * is terminated with two null characters.
362 *
363 * Use free() to free the memory block allocated by this
364 * function in *ppszEnv.
365 *
366 *@@added V0.9.4 (2000-07-19) [umoeller]
367 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
368 */
369
370APIRET appConvertEnvironment(PDOSENVIRONMENT pEnv,
371 PSZ *ppszEnv, // out: environment string
372 PULONG pulSize) // out: size of block allocated in *ppszEnv; ptr can be NULL
373{
374 APIRET arc = NO_ERROR;
375 if (!pEnv)
376 arc = ERROR_INVALID_PARAMETER;
377 else
378 {
379 if (!pEnv->papszVars)
380 arc = ERROR_INVALID_PARAMETER;
381 else
382 {
383 // count memory needed for all strings
384 ULONG cbNeeded = 0,
385 ul = 0;
386 PSZ *ppszThis = pEnv->papszVars;
387
388 for (ul = 0;
389 ul < pEnv->cVars;
390 ul++)
391 {
392 cbNeeded += strlen(*ppszThis) + 1; // length of string plus null terminator
393
394 // next environment string
395 ppszThis++;
396 }
397
398 cbNeeded++; // for another null terminator
399
400 *ppszEnv = (PSZ)malloc(cbNeeded);
401 if (!(*ppszEnv))
402 arc = ERROR_NOT_ENOUGH_MEMORY;
403 else
404 {
405 PSZ pTarget = *ppszEnv;
406 if (pulSize)
407 *pulSize = cbNeeded;
408 ppszThis = pEnv->papszVars;
409
410 // now copy each string
411 for (ul = 0;
412 ul < pEnv->cVars;
413 ul++)
414 {
415 PSZ pSource = *ppszThis;
416
417 while ((*pTarget++ = *pSource++))
418 ;
419
420 // *pTarget++ = 0; // append null terminator per string
421
422 // next environment string
423 ppszThis++;
424 }
425
426 *pTarget++ = 0; // append second null terminator
427 }
428 }
429 }
430
431 return (arc);
432}
433
434/*
435 *@@ appFreeEnvironment:
436 * frees memory allocated by appGetEnvironment.
437 *
438 *@@added V0.9.4 (2000-07-19) [umoeller]
439 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from dosh2.c to apps.c
440 */
441
442APIRET appFreeEnvironment(PDOSENVIRONMENT pEnv)
443{
444 APIRET arc = NO_ERROR;
445 if (!pEnv)
446 arc = ERROR_INVALID_PARAMETER;
447 else
448 {
449 if (!pEnv->papszVars)
450 arc = ERROR_INVALID_PARAMETER;
451 else
452 {
453 PSZ *ppszThis = pEnv->papszVars;
454 PSZ pszThis;
455 ULONG ul = 0;
456
457 for (ul = 0;
458 ul < pEnv->cVars;
459 ul++)
460 {
461 pszThis = *ppszThis;
462 free(pszThis);
463 // *ppszThis = NULL;
464 // next environment string
465 ppszThis++;
466 }
467
468 free(pEnv->papszVars);
469 pEnv->cVars = 0;
470 }
471 }
472
473 return (arc);
474}
475
476/* ******************************************************************
477 *
478 * Application start
479 *
480 ********************************************************************/
481
482/*
483 *@@ CallBatchCorrectly:
484 * fixes the specified PROGDETAILS for
485 * command files in the executable part
486 * by inserting /C XXX into the parameters
487 * and setting the executable according
488 * to an environment variable.
489 *
490 *@@added V0.9.6 (2000-10-16) [umoeller]
491 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING
492 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
493 */
494
495VOID CallBatchCorrectly(PPROGDETAILS pProgDetails,
496 PXSTRING pstrParams, // in/out: modified parameters (reallocated)
497 const char *pcszEnvVar, // in: env var spec'g command proc
498 // (e.g. "OS2_SHELL"); can be NULL
499 const char *pcszDefProc) // in: def't command proc (e.g. "CMD.EXE")
500{
501 // XXX.CMD file as executable:
502 // fix args to /C XXX.CMD
503
504 PSZ pszOldParams = NULL;
505 ULONG ulOldParamsLength = pstrParams->ulLength;
506 if (ulOldParamsLength)
507 // we have parameters already:
508 // make a backup... we'll append that later
509 pszOldParams = strdup(pstrParams->psz);
510
511 // set new params to "/C filename.cmd"
512 xstrcpy(pstrParams, "/C ", 0);
513 xstrcat(pstrParams,
514 pProgDetails->pszExecutable,
515 0);
516
517 if (pszOldParams)
518 {
519 // .cmd had params:
520 // append space and old params
521 xstrcatc(pstrParams, ' ');
522 xstrcat(pstrParams,
523 pszOldParams,
524 ulOldParamsLength);
525 free(pszOldParams);
526 }
527
528 // set executable to $(OS2_SHELL)
529 pProgDetails->pszExecutable = NULL;
530 if (pcszEnvVar)
531 pProgDetails->pszExecutable = getenv(pcszEnvVar);
532 if (!pProgDetails->pszExecutable)
533 pProgDetails->pszExecutable = (PSZ)pcszDefProc;
534 // should be on PATH
535}
536
537/*
538 *@@ appQueryAppType:
539 * returns the Control Program (Dos) and
540 * Win* PROG_* application types for the
541 * specified executable. Essentially, this
542 * is a wrapper around DosQueryAppType.
543 *
544 * pcszExecutable must be fully qualified.
545 * You can use doshFindExecutable to qualify
546 * it.
547 *
548 * This returns the APIRET of DosQueryAppType.
549 * If this is NO_ERROR; *pulDosAppType receives
550 * the app type of DosQueryAppType. In addition,
551 * *pulWinAppType is set to one of the following:
552 *
553 * -- PROG_FULLSCREEN
554 *
555 * -- PROG_PDD
556 *
557 * -- PROG_VDD
558 *
559 * -- PROG_XWP_DLL: new apptype defined in winh.h for
560 * dynamic link libraries.
561 *
562 * -- PROG_WINDOWEDVDM
563 *
564 * -- PROG_PM
565 *
566 * -- PROG_31_ENHSEAMLESSCOMMON
567 *
568 * -- PROG_WINDOWABLEVIO
569 *
570 *@@added V0.9.9 (2001-03-07) [umoeller]
571 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
572 *@@changed V0.9.14 (2001-08-07) [pr]: use FAPPTYP_* constants
573 */
574
575APIRET appQueryAppType(const char *pcszExecutable,
576 PULONG pulDosAppType,
577 PULONG pulWinAppType)
578{
579 APIRET arc = DosQueryAppType((PSZ)pcszExecutable, pulDosAppType);
580 if (arc == NO_ERROR)
581 {
582 ULONG _ulDosAppType = *pulDosAppType;
583
584 if (_ulDosAppType == 0)
585 *pulWinAppType = PROG_FULLSCREEN;
586 else if (_ulDosAppType & FAPPTYP_PHYSDRV) // 0x40
587 *pulWinAppType = PROG_PDD;
588 else if (_ulDosAppType & FAPPTYP_VIRTDRV) // 0x80)
589 *pulWinAppType = PROG_VDD;
590 else if ((_ulDosAppType & 0xF0) == FAPPTYP_DLL) // 0x10)
591 // DLL bit set
592 *pulWinAppType = PROG_XWP_DLL;
593 else if (_ulDosAppType & FAPPTYP_DOS) // 0x20)
594 // DOS bit set?
595 *pulWinAppType = PROG_WINDOWEDVDM;
596 else if ((_ulDosAppType & FAPPTYP_WINDOWAPI) == FAPPTYP_WINDOWAPI) // 0x0003) // "Window-API" == PM
597 *pulWinAppType = PROG_PM;
598 else if ( ((_ulDosAppType & 0xFFFF) == FAPPTYP_WINDOWSPROT31) // 0x1000) // windows program (?!?)
599 || ((_ulDosAppType & 0xFFFF) == FAPPTYP_WINDOWSPROT) // ) // windows program (?!?)
600 )
601 *pulWinAppType = PROG_31_ENHSEAMLESSCOMMON; // PROG_31_ENH;
602 // *pulWinAppType = PROG_31_ENHSEAMLESSVDM;
603 else if ((_ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_WINDOWCOMPAT) // 0x02)
604 *pulWinAppType = PROG_WINDOWABLEVIO;
605 else if ((_ulDosAppType & FAPPTYP_WINDOWAPI /* 0x03 */ ) == FAPPTYP_NOTWINDOWCOMPAT) // 0x01)
606 *pulWinAppType = PROG_FULLSCREEN;
607 }
608
609 return (arc);
610}
611
612/*
613 *@@ appIsWindowsApp:
614 * checks the specified program category
615 * (PROGDETAILS.progt.progc) for whether
616 * it represents a Win-OS/2 application.
617 *
618 * Returns:
619 *
620 * -- 0: no windows app (it's VIO, OS/2
621 * or DOS fullscreen, or PM).
622 *
623 * -- 1: Win-OS/2 standard app.
624 *
625 * -- 2: Win-OS/2 enhanced-mode app.
626 *
627 *@@added V0.9.12 (2001-05-26) [umoeller]
628 */
629
630ULONG appIsWindowsApp(ULONG ulProgCategory)
631{
632 switch (ulProgCategory)
633 {
634 case PROG_31_ENHSEAMLESSVDM: // 17
635 case PROG_31_ENHSEAMLESSCOMMON: // 18
636 case PROG_31_ENH: // 19
637 return (2);
638
639#ifndef PROG_30_STD
640 #define PROG_30_STD (PROGCATEGORY)11
641#endif
642
643#ifndef PROG_30_STDSEAMLESSVDM
644 #define PROG_30_STDSEAMLESSVDM (PROGCATEGORY)13
645#endif
646
647 case PROG_WINDOW_REAL: // 10
648 case PROG_30_STD: // 11
649 case PROG_WINDOW_AUTO: // 12
650 case PROG_30_STDSEAMLESSVDM: // 13
651 case PROG_30_STDSEAMLESSCOMMON: // 14
652 case PROG_31_STDSEAMLESSVDM: // 15
653 case PROG_31_STDSEAMLESSCOMMON: // 16
654 case PROG_31_STD: // 20
655 return (1);
656 }
657
658 return (0);
659}
660
661/*
662 *@@ appQueryDefaultWin31Environment:
663 * returns the default Win-OS/2 3.1 environment
664 * from OS2.INI, which you can then merge with
665 * your process environment to be able to
666 * start Win-OS/2 sessions properly with
667 * appStartApp.
668 *
669 * Caller must free() the return value.
670 *
671 *@@added V0.9.12 (2001-05-26) [umoeller]
672 */
673
674PSZ appQueryDefaultWin31Environment(VOID)
675{
676 PSZ pszReturn = NULL;
677 ULONG ulSize = 0;
678 // get default environment (from Win-OS/2 settings object)
679 // from OS2.INI
680 PSZ pszDefEnv = prfhQueryProfileData(HINI_USER,
681 "WINOS2",
682 "PM_GlobalWindows31Settings",
683 &ulSize);
684 if (pszDefEnv)
685 {
686 if (pszReturn = (PSZ)malloc(ulSize + 2))
687 {
688 PSZ p;
689 memset(pszReturn, 0, ulSize + 2);
690 memcpy(pszReturn, pszDefEnv, ulSize);
691
692 for (p = pszReturn;
693 p < pszReturn + ulSize;
694 p++)
695 if (*p == ';')
696 *p = 0;
697
698 // okay.... now we got an OS/2-style environment
699 // with 0, 0, 00 strings
700 }
701
702 free(pszDefEnv);
703 }
704
705 return (pszReturn);
706}
707
708/*
709 *@@ appStartApp:
710 * wrapper around WinStartApp which fixes the
711 * specified PROGDETAILS to (hopefully) work
712 * work with all executable types.
713 *
714 * This fixes the executable info to support:
715 *
716 * -- starting "*" executables (command prompts
717 * for OS/2, DOS, Win-OS/2);
718 *
719 * -- starting ".CMD" and ".BAT" files as
720 * PROGDETAILS.pszExecutable.
721 *
722 * Unless it is "*", PROGDETAILS.pszExecutable must
723 * be a proper file name. The full path may be omitted
724 * if it is on the PATH, but the extension (.EXE etc.)
725 * must be given. You can use doshFindExecutable to
726 * find executables if you don't know the extension.
727 *
728 * This also handles and merges special and default
729 * environments for the app to be started. The
730 * following should be respected:
731 *
732 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
733 * is NULL, the new app inherits a default environment
734 * from the shell.
735 *
736 + -- However, if you specify an environment, you _must_
737 * specify a complemete environment. This function
738 * will not merge environments. Use
739 * appSetEnvironmentVar to change environment
740 * variables in a complete environment set.
741 *
742 * -- If PROGDETAILS specifies a Win-OS/2 session
743 * and PROGDETAILS.pszEnvironment is empty,
744 * this uses the default Win-OS/2 environment.
745 * See appQueryDefaultWin31Environment.
746 *
747 * -- WARNING: Since this uses WinStartApp internally,
748 * and WinStartApp completely hangs PM if a Win-OS/2
749 * full-screen session is started from a thread that
750 * is NOT thread1, THIS SHOULD ONLY BE CALLED ON
751 * THREAD 1 of your application.
752 *
753 * Even though this isn't clearly said in PMREF,
754 * PROGDETAILS.swpInitial is important:
755 *
756 * -- To start a session minimized, set SWP_MINIMIZE.
757 *
758 * -- To start a VIO session auto-close disabled, set
759 * the half-documented SWP_NOAUTOCLOSE flag (0x8000)
760 * This flag is now in the newer toolkit headers.
761 *
762 * Since this calls WinStartApp in turn, this
763 * requires a message queue on the calling thread.
764 *
765 *@@added V0.9.6 (2000-10-16) [umoeller]
766 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
767 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
768 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
769 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
770 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
771 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
772 *@@changed V0.9.14 (2001-08-23) [pr]: added session type options
773 */
774
775HAPP appStartApp(HWND hwndNotify, // in: notify window (as with WinStartApp)
776 const PROGDETAILS *pcProgDetails, // in: program data
777 ULONG ulFlags) // in: session type options
778{
779 HAPP happ = NULLHANDLE;
780 XSTRING strParamsPatched;
781 PROGDETAILS ProgDetails;
782
783 PSZ pszWinOS2Env = 0;
784
785 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
786 // pointers still point into old prog details buffer
787 ProgDetails.Length = sizeof(PROGDETAILS);
788 ProgDetails.progt.fbVisible = SHE_VISIBLE;
789 // ProgDetails.pszEnvironment = 0;
790
791 // all this only makes sense if this contains something...
792 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
793 if (ProgDetails.pszExecutable)
794 {
795 ULONG ulIsWinApp;
796
797 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
798 // this wasn't a good idea... WPProgram stores stuff
799 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
800
801 // duplicate parameters...
802 // we need this for string manipulations below...
803 if (ProgDetails.pszParameters)
804 xstrInitCopy(&strParamsPatched,
805 ProgDetails.pszParameters,
806 100);
807 else
808 // no old params:
809 xstrInit(&strParamsPatched, 100);
810
811 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
812 // _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
813 // _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
814
815 // program type fixups
816 switch (ProgDetails.progt.progc) // that's a ULONG
817 {
818 case ((ULONG)-1): // we get that sometimes...
819 case PROG_DEFAULT:
820 {
821 // V0.9.12 (2001-05-26) [umoeller]
822 ULONG ulDosAppType;
823 appQueryAppType(ProgDetails.pszExecutable,
824 &ulDosAppType,
825 &ProgDetails.progt.progc);
826 }
827 break;
828 }
829
830 // Set session type from option flags
831 if (ulFlags & APP_RUN_FULLSCREEN)
832 {
833 if (ProgDetails.progt.progc == PROG_WINDOWABLEVIO)
834 ProgDetails.progt.progc = PROG_FULLSCREEN;
835
836 if (ProgDetails.progt.progc == PROG_WINDOWEDVDM)
837 ProgDetails.progt.progc = PROG_VDM;
838 }
839
840 if (ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc))
841 {
842 if (ulFlags & APP_RUN_FULLSCREEN)
843 ProgDetails.progt.progc = (ulFlags & APP_RUN_ENHANCED)
844 ? PROG_31_ENH
845 : PROG_31_STD;
846 else
847 {
848 if (ulFlags & APP_RUN_STANDARD)
849 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
850 ? PROG_31_STDSEAMLESSVDM
851 : PROG_31_STDSEAMLESSCOMMON;
852
853 if (ulFlags & APP_RUN_ENHANCED)
854 ProgDetails.progt.progc = (ulFlags & APP_RUN_SEPARATE)
855 ? PROG_31_ENHSEAMLESSVDM
856 : PROG_31_ENHSEAMLESSCOMMON;
857 }
858 }
859
860 /*
861 * command lines fixups:
862 *
863 */
864
865 if (!strcmp(ProgDetails.pszExecutable, "*"))
866 {
867 /*
868 * "*" for command sessions:
869 *
870 */
871
872 if (ulIsWinApp == 2)
873 {
874 // enhanced Win-OS/2 session:
875 PSZ psz = NULL;
876 if (strParamsPatched.ulLength)
877 // "/3 " + existing params
878 psz = strdup(strParamsPatched.psz);
879
880 xstrcpy(&strParamsPatched, "/3 ", 0);
881
882 if (psz)
883 {
884 xstrcat(&strParamsPatched, psz, 0);
885 free(psz);
886 }
887 }
888
889 if (ulIsWinApp)
890 {
891 // cheat: WinStartApp doesn't support NULL
892 // for Win-OS2 sessions, so manually start winos2.com
893 ProgDetails.pszExecutable = "WINOS2.COM";
894 // this is a DOS app, so fix this to DOS fullscreen
895 ProgDetails.progt.progc = PROG_VDM;
896 }
897 else
898 // for all other executable types
899 // (including OS/2 and DOS sessions),
900 // set pszExecutable to NULL; this will
901 // have WinStartApp start a cmd shell
902 ProgDetails.pszExecutable = NULL;
903
904 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
905 else
906 {
907 PSZ pszExtension;
908 switch (ProgDetails.progt.progc)
909 {
910 /*
911 * .CMD files fixups
912 *
913 */
914
915 case PROG_FULLSCREEN: // OS/2 fullscreen
916 case PROG_WINDOWABLEVIO: // OS/2 window
917 {
918 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
919 && (!stricmp(pszExtension, "CMD"))
920 )
921 {
922 CallBatchCorrectly(&ProgDetails,
923 &strParamsPatched,
924 "OS2_SHELL",
925 "CMD.EXE");
926 }
927 break; }
928
929 case PROG_VDM: // DOS fullscreen
930 case PROG_WINDOWEDVDM: // DOS window
931 {
932 if ( (pszExtension = doshGetExtension(ProgDetails.pszExecutable))
933 && (!stricmp(pszExtension, "BAT"))
934 )
935 {
936 CallBatchCorrectly(&ProgDetails,
937 &strParamsPatched,
938 NULL,
939 "COMMAND.COM");
940 }
941 break; }
942 } // end switch (ProgDetails.progt.progc)
943 }
944
945 if ( (ulIsWinApp)
946 && ( (ProgDetails.pszEnvironment == NULL)
947 || (!strlen(ProgDetails.pszEnvironment))
948 )
949 )
950 {
951 // this is a windoze app, and caller didn't bother
952 // to give us an environment:
953 // we MUST set one then, or we'll get the strangest
954 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
955
956 DOSENVIRONMENT Env = {0};
957
958 // get standard WIN-OS/2 environment
959 PSZ pszTemp = appQueryDefaultWin31Environment();
960
961 if (!appParseEnvironment(pszTemp,
962 &Env))
963 {
964 // now override KBD_CTRL_BYPASS=CTRL_ESC
965 appSetEnvironmentVar(&Env,
966 "KBD_CTRL_BYPASS=CTRL_ESC",
967 FALSE); // add last
968 /*
969 * These should be set by the default environment. It is
970 * not our business to override them really. V0.9.14
971 *
972 appSetEnvironmentVar(&Env,
973 "KBD_ALTHOME_BYPASS=1",
974 FALSE); // add last
975 appSetEnvironmentVar(&Env,
976 "INT_DURING_IO=1",
977 FALSE); // add last
978 appSetEnvironmentVar(&Env,
979 "HW_TIMER=1",
980 FALSE); // add last
981 appSetEnvironmentVar(&Env,
982 "MOUSE_EXCLUSIVE_ACCESS=0",
983 FALSE); // add last
984 appSetEnvironmentVar(&Env,
985 "VIDEO_RETRACE_EMULATION=1",
986 FALSE); // add last
987 appSetEnvironmentVar(&Env,
988 "VIDEO_SWITCH_NOTIFICATION=1",
989 FALSE); // add last
990 appSetEnvironmentVar(&Env,
991 "VIDEO_8514A_XGA_IOTRAP=0",
992 FALSE); // add last
993 */
994
995 if (!appConvertEnvironment(&Env,
996 &pszWinOS2Env, // freed at bottom
997 NULL))
998 ProgDetails.pszEnvironment = pszWinOS2Env;
999
1000 appFreeEnvironment(&Env);
1001 }
1002
1003 free(pszTemp);
1004 }
1005
1006 if (!ProgDetails.pszTitle)
1007 ProgDetails.pszTitle = ProgDetails.pszExecutable;
1008
1009 ProgDetails.pszParameters = strParamsPatched.psz;
1010
1011 /* _Pmpf(("progt.progc: %d", ProgDetails.progt.progc));
1012 _Pmpf(("progt.fbVisible: 0x%lX", ProgDetails.progt.fbVisible));
1013 _Pmpf(("progt.pszTitle: \"%s\"", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : "NULL"));
1014 _Pmpf(("exec: \"%s\"", (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL"));
1015 _Pmpf(("params: \"%s\"", (ProgDetails.pszParameters) ? ProgDetails.pszParameters : "NULL"));
1016 _Pmpf(("startup: \"%s\"", (ProgDetails.pszStartupDir) ? ProgDetails.pszStartupDir : "NULL"));
1017 _Pmpf(("pszIcon: \"%s\"", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : "NULL"));
1018 _Pmpf(("environment: "));
1019 {
1020 PSZ pszThis = ProgDetails.pszEnvironment;
1021 while (pszThis && *pszThis)
1022 {
1023 _Pmpf((" \"%s\"", pszThis));
1024 pszThis += strlen(pszThis) + 1;
1025 }
1026 }
1027
1028 _Pmpf(("swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
1029 ProgDetails.swpInitial.fl,
1030 ProgDetails.swpInitial.x,
1031 ProgDetails.swpInitial.y,
1032 ProgDetails.swpInitial.cx,
1033 ProgDetails.swpInitial.cy));
1034 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
1035 ProgDetails.swpInitial.hwndInsertBehind,
1036 ProgDetails.swpInitial.hwnd,
1037 ProgDetails.swpInitial.ulReserved1,
1038 ProgDetails.swpInitial.ulReserved2));
1039 */
1040
1041 /* if (WinMessageBox(HWND_DESKTOP,
1042 NULLHANDLE,
1043 (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL",
1044 "Start?",
1045 0,
1046 MB_YESNO | MB_MOVEABLE)
1047 == MBID_YES) */
1048 happ = WinStartApp(hwndNotify,
1049 // receives WM_APPTERMINATENOTIFY
1050 &ProgDetails,
1051 strParamsPatched.psz,
1052 NULL, // "reserved", PMREF says...
1053 SAF_INSTALLEDCMDLINE);
1054 // we MUST use SAF_INSTALLEDCMDLINE
1055 // or no Win-OS/2 session will start...
1056 // whatever is going on here... Warp 4 FP11
1057
1058 // do not use SAF_STARTCHILDAPP, or the
1059 // app will be terminated automatically
1060 // when the WPS terminates!
1061
1062 // _Pmpf((__FUNCTION__ ": got happ 0x%lX", happ));
1063
1064 xstrClear(&strParamsPatched);
1065 if (pszWinOS2Env)
1066 free(pszWinOS2Env);
1067 } // end if (ProgDetails.pszExecutable)
1068
1069 return (happ);
1070}
1071
1072
Note: See TracBrowser for help on using the repository browser.