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

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

Lafaix and Ratcliffe updates.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 35.1 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_ENH
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 * Even though this isn't clearly said in PMREF,
748 * PROGDETAILS.swpInitial is important:
749 *
750 * -- To start a session minimized, set SWP_MINIMIZE.
751 *
752 * -- To start a VIO session auto-close disabled, set
753 * the half-documented SWP_NOAUTOCLOSE flag (0x8000)
754 * This flag is now in the newer toolkit headers.
755 *
756 * Since this calls WinStartApp in turn, this
757 * requires a message queue on the calling thread.
758 *
759 *@@added V0.9.6 (2000-10-16) [umoeller]
760 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
761 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
762 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
763 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
764 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
765 *@@changed V0.9.14 (2001-08-07) [pr]: removed some env. strings for Win. apps.
766 */
767
768HAPP appStartApp(HWND hwndNotify, // in: notify window (as with WinStartApp)
769 const PROGDETAILS *pcProgDetails) // in: program data
770{
771 HAPP happ = NULLHANDLE;
772 XSTRING strParamsPatched;
773 PROGDETAILS ProgDetails;
774
775 PSZ pszWinOS2Env = 0;
776
777 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
778 // pointers still point into old prog details buffer
779 ProgDetails.Length = sizeof(PROGDETAILS);
780 ProgDetails.progt.fbVisible = SHE_VISIBLE;
781 // ProgDetails.pszEnvironment = 0;
782
783 // all this only makes sense if this contains something...
784 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
785 if (ProgDetails.pszExecutable)
786 {
787 ULONG ulIsWinApp;
788
789 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
790 // this wasn't a good idea... WPProgram stores stuff
791 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
792
793 // duplicate parameters...
794 // we need this for string manipulations below...
795 if (ProgDetails.pszParameters)
796 xstrInitCopy(&strParamsPatched,
797 ProgDetails.pszParameters,
798 100);
799 else
800 // no old params:
801 xstrInit(&strParamsPatched, 100);
802
803 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
804 // _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
805 // _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
806
807 // program type fixups
808 switch (ProgDetails.progt.progc) // that's a ULONG
809 {
810 case ((ULONG)-1): // we get that sometimes...
811 case PROG_DEFAULT:
812 {
813 // V0.9.12 (2001-05-26) [umoeller]
814 ULONG ulDosAppType;
815 appQueryAppType(ProgDetails.pszExecutable,
816 &ulDosAppType,
817 &ProgDetails.progt.progc);
818 }
819 break;
820 }
821
822 ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc);
823
824 // now try again...
825
826 /*
827 * command lines fixups:
828 *
829 */
830
831 if (strcmp(ProgDetails.pszExecutable, "*") == 0)
832 {
833 /*
834 * "*" for command sessions:
835 *
836 */
837
838 if (ulIsWinApp == 2)
839 {
840 // enhanced Win-OS/2 session:
841 PSZ psz = NULL;
842 if (strParamsPatched.ulLength)
843 // "/3 " + existing params
844 psz = strdup(strParamsPatched.psz);
845
846 xstrcpy(&strParamsPatched, "/3 ", 0);
847
848 if (psz)
849 {
850 xstrcat(&strParamsPatched, psz, 0);
851 free(psz);
852 }
853 }
854
855 if (ulIsWinApp)
856 {
857 // cheat: WinStartApp doesn't support NULL
858 // for Win-OS2 sessions, so manually start winos2.com
859 ProgDetails.pszExecutable = "WINOS2.COM";
860 // this is a DOS app, so fix this to DOS fullscreen
861 ProgDetails.progt.progc = PROG_VDM;
862 }
863 else
864 // for all other executable types
865 // (including OS/2 and DOS sessions),
866 // set pszExecutable to NULL; this will
867 // have WinStartApp start a cmd shell
868 ProgDetails.pszExecutable = NULL;
869
870 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
871 else
872 switch (ProgDetails.progt.progc)
873 {
874 /*
875 * .CMD files fixups
876 *
877 */
878
879 case PROG_FULLSCREEN: // OS/2 fullscreen
880 case PROG_WINDOWABLEVIO: // OS/2 window
881 {
882 PSZ pszExtension = doshGetExtension(ProgDetails.pszExecutable);
883 if (pszExtension)
884 {
885 if (stricmp(pszExtension, "CMD") == 0)
886 {
887 CallBatchCorrectly(&ProgDetails,
888 &strParamsPatched,
889 "OS2_SHELL",
890 "CMD.EXE");
891 }
892 }
893 break; }
894
895 case PROG_VDM: // DOS fullscreen
896 case PROG_WINDOWEDVDM: // DOS window
897 {
898 PSZ pszExtension = doshGetExtension(ProgDetails.pszExecutable);
899 if (pszExtension)
900 {
901 if (stricmp(pszExtension, "BAT") == 0)
902 {
903 CallBatchCorrectly(&ProgDetails,
904 &strParamsPatched,
905 NULL,
906 "COMMAND.COM");
907 }
908 }
909 break; }
910 } // end switch (ProgDetails.progt.progc)
911
912 if ( (ulIsWinApp)
913 && ( (ProgDetails.pszEnvironment == NULL)
914 || (!strlen(ProgDetails.pszEnvironment))
915 )
916 )
917 {
918 // this is a windoze app, and caller didn't bother
919 // to give us an environment:
920 // we MUST set one then, or we'll get the strangest
921 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
922
923 DOSENVIRONMENT Env = {0};
924
925 // get standard WIN-OS/2 environment
926 PSZ pszTemp = appQueryDefaultWin31Environment();
927
928 if (!appParseEnvironment(pszTemp,
929 &Env))
930 {
931 // now override KBD_CTRL_BYPASS=CTRL_ESC
932 appSetEnvironmentVar(&Env,
933 "KBD_CTRL_BYPASS=CTRL_ESC",
934 FALSE); // add last
935 /*
936 * These should be set by the default environment. It is
937 * not our business to override them really. V0.9.14
938 *
939 appSetEnvironmentVar(&Env,
940 "KBD_ALTHOME_BYPASS=1",
941 FALSE); // add last
942 appSetEnvironmentVar(&Env,
943 "INT_DURING_IO=1",
944 FALSE); // add last
945 appSetEnvironmentVar(&Env,
946 "HW_TIMER=1",
947 FALSE); // add last
948 appSetEnvironmentVar(&Env,
949 "MOUSE_EXCLUSIVE_ACCESS=0",
950 FALSE); // add last
951 appSetEnvironmentVar(&Env,
952 "VIDEO_RETRACE_EMULATION=1",
953 FALSE); // add last
954 appSetEnvironmentVar(&Env,
955 "VIDEO_SWITCH_NOTIFICATION=1",
956 FALSE); // add last
957 appSetEnvironmentVar(&Env,
958 "VIDEO_8514A_XGA_IOTRAP=0",
959 FALSE); // add last
960 */
961
962 if (!appConvertEnvironment(&Env,
963 &pszWinOS2Env, // freed at bottom
964 NULL))
965 ProgDetails.pszEnvironment = pszWinOS2Env;
966
967 appFreeEnvironment(&Env);
968 }
969
970 free(pszTemp);
971 }
972
973 if (!ProgDetails.pszTitle)
974 ProgDetails.pszTitle = ProgDetails.pszExecutable;
975
976 ProgDetails.pszParameters = strParamsPatched.psz;
977
978 _Pmpf(("progt.progc: %d", ProgDetails.progt.progc));
979 _Pmpf(("progt.fbVisible: 0x%lX", ProgDetails.progt.fbVisible));
980 _Pmpf(("progt.pszTitle: \"%s\"", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : "NULL"));
981 _Pmpf(("exec: \"%s\"", (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL"));
982 _Pmpf(("params: \"%s\"", (ProgDetails.pszParameters) ? ProgDetails.pszParameters : "NULL"));
983 _Pmpf(("startup: \"%s\"", (ProgDetails.pszStartupDir) ? ProgDetails.pszStartupDir : "NULL"));
984 _Pmpf(("pszIcon: \"%s\"", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : "NULL"));
985 _Pmpf(("environment: "));
986 {
987 PSZ pszThis = ProgDetails.pszEnvironment;
988 while (pszThis && *pszThis)
989 {
990 _Pmpf((" \"%s\"", pszThis));
991 pszThis += strlen(pszThis) + 1;
992 }
993 }
994
995 _Pmpf(("swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
996 ProgDetails.swpInitial.fl,
997 ProgDetails.swpInitial.x,
998 ProgDetails.swpInitial.y,
999 ProgDetails.swpInitial.cx,
1000 ProgDetails.swpInitial.cy));
1001 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
1002 ProgDetails.swpInitial.hwndInsertBehind,
1003 ProgDetails.swpInitial.hwnd,
1004 ProgDetails.swpInitial.ulReserved1,
1005 ProgDetails.swpInitial.ulReserved2));
1006
1007 /* if (WinMessageBox(HWND_DESKTOP,
1008 NULLHANDLE,
1009 (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL",
1010 "Start?",
1011 0,
1012 MB_YESNO | MB_MOVEABLE)
1013 == MBID_YES) */
1014 happ = WinStartApp(hwndNotify,
1015 // receives WM_APPTERMINATENOTIFY
1016 &ProgDetails,
1017 strParamsPatched.psz,
1018 NULL, // "reserved", PMREF says...
1019 SAF_INSTALLEDCMDLINE);
1020 // we MUST use SAF_INSTALLEDCMDLINE
1021 // or no Win-OS/2 session will start...
1022 // whatever is going on here... Warp 4 FP11
1023
1024 // do not use SAF_STARTCHILDAPP, or the
1025 // app will be terminated automatically
1026 // when the WPS terminates!
1027
1028 // _Pmpf((__FUNCTION__ ": got happ 0x%lX", happ));
1029
1030 xstrClear(&strParamsPatched);
1031 if (pszWinOS2Env)
1032 free(pszWinOS2Env);
1033 } // end if (ProgDetails.pszExecutable)
1034
1035 return (happ);
1036}
1037
1038
Note: See TracBrowser for help on using the repository browser.