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

Last change on this file since 91 was 91, 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: 34.5 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 */
573
574APIRET appQueryAppType(const char *pcszExecutable,
575 PULONG pulDosAppType,
576 PULONG pulWinAppType)
577{
578 APIRET arc = DosQueryAppType((PSZ)pcszExecutable, pulDosAppType);
579 if (arc == NO_ERROR)
580 {
581 ULONG _ulDosAppType = *pulDosAppType;
582
583 if (_ulDosAppType == 0)
584 *pulWinAppType = PROG_FULLSCREEN;
585 else if (_ulDosAppType & 0x40)
586 *pulWinAppType = PROG_PDD;
587 else if (_ulDosAppType & 0x80)
588 *pulWinAppType = PROG_VDD;
589 else if ((_ulDosAppType & 0xF0) == 0x10)
590 // DLL bit set
591 *pulWinAppType = PROG_XWP_DLL;
592 else if (_ulDosAppType & 0x20)
593 // DOS bit set?
594 *pulWinAppType = PROG_WINDOWEDVDM;
595 else if ((_ulDosAppType & 0x0003) == 0x0003) // "Window-API" == PM
596 *pulWinAppType = PROG_PM;
597 else if ( ((_ulDosAppType & 0xFFFF) == 0x1000) // windows program (?!?)
598 || ((_ulDosAppType & 0xFFFF) == 0x0400) // windows program (?!?)
599 )
600 *pulWinAppType = PROG_31_ENH;
601 // *pulWinAppType = PROG_31_ENHSEAMLESSVDM;
602 else if ((_ulDosAppType & 0x03) == 0x02)
603 *pulWinAppType = PROG_WINDOWABLEVIO;
604 else if ((_ulDosAppType & 0x03) == 0x01)
605 *pulWinAppType = PROG_FULLSCREEN;
606 }
607
608 return (arc);
609}
610
611/*
612 *@@ appIsWindowsApp:
613 * checks the specified program category
614 * (PROGDETAILS.progt.progc) for whether
615 * it represents a Win-OS/2 application.
616 *
617 * Returns:
618 *
619 * -- 0: no windows app (it's VIO, OS/2
620 * or DOS fullscreen, or PM).
621 *
622 * -- 1: Win-OS/2 standard app.
623 *
624 * -- 2: Win-OS/2 enhanced-mode app.
625 *
626 *@@added V0.9.12 (2001-05-26) [umoeller]
627 */
628
629ULONG appIsWindowsApp(ULONG ulProgCategory)
630{
631 switch (ulProgCategory)
632 {
633 case PROG_31_ENHSEAMLESSVDM: // 17
634 case PROG_31_ENHSEAMLESSCOMMON: // 18
635 case PROG_31_ENH: // 19
636 return (2);
637
638#ifndef PROG_30_STD
639 #define PROG_30_STD (PROGCATEGORY)11
640#endif
641
642#ifndef PROG_30_STDSEAMLESSVDM
643 #define PROG_30_STDSEAMLESSVDM (PROGCATEGORY)13
644#endif
645
646 case PROG_WINDOW_REAL: // 10
647 case PROG_30_STD: // 11
648 case PROG_WINDOW_AUTO: // 12
649 case PROG_30_STDSEAMLESSVDM: // 13
650 case PROG_30_STDSEAMLESSCOMMON: // 14
651 case PROG_31_STDSEAMLESSVDM: // 15
652 case PROG_31_STDSEAMLESSCOMMON: // 16
653 case PROG_31_STD: // 20
654 return (1);
655 }
656
657 return (0);
658}
659
660/*
661 *@@ appQueryDefaultWin31Environment:
662 * returns the default Win-OS/2 3.1 environment
663 * from OS2.INI, which you can then merge with
664 * your process environment to be able to
665 * start Win-OS/2 sessions properly with
666 * appStartApp.
667 *
668 * Caller must free() the return value.
669 *
670 *@@added V0.9.12 (2001-05-26) [umoeller]
671 */
672
673PSZ appQueryDefaultWin31Environment(VOID)
674{
675 PSZ pszReturn = NULL;
676 ULONG ulSize = 0;
677 // get default environment (from Win-OS/2 settings object)
678 // from OS2.INI
679 PSZ pszDefEnv = prfhQueryProfileData(HINI_USER,
680 "WINOS2",
681 "PM_GlobalWindows31Settings",
682 &ulSize);
683 if (pszDefEnv)
684 {
685 if (pszReturn = (PSZ)malloc(ulSize + 2))
686 {
687 PSZ p;
688 memset(pszReturn, 0, ulSize + 2);
689 memcpy(pszReturn, pszDefEnv, ulSize);
690
691 for (p = pszReturn;
692 p < pszReturn + ulSize;
693 p++)
694 if (*p == ';')
695 *p = 0;
696
697 // okay.... now we got an OS/2-style environment
698 // with 0, 0, 00 strings
699 }
700
701 free(pszDefEnv);
702 }
703
704 return (pszReturn);
705}
706
707/*
708 *@@ appStartApp:
709 * wrapper around WinStartApp which fixes the
710 * specified PROGDETAILS to (hopefully) work
711 * work with all executable types.
712 *
713 * This fixes the executable info to support:
714 *
715 * -- starting "*" executables (command prompts
716 * for OS/2, DOS, Win-OS/2);
717 *
718 * -- starting ".CMD" and ".BAT" files as
719 * PROGDETAILS.pszExecutable.
720 *
721 * Unless it is "*", PROGDETAILS.pszExecutable must
722 * be a proper file name. The full path may be omitted
723 * if it is on the PATH, but the extension (.EXE etc.)
724 * must be given. You can use doshFindExecutable to
725 * find executables if you don't know the extension.
726 *
727 * This also handles and merges special and default
728 * environments for the app to be started. The
729 * following should be respected:
730 *
731 * -- As with WinStartApp, if PROGDETAILS.pszEnvironment
732 * is NULL, the new app inherits a default environment
733 * from the shell.
734 *
735 + -- However, if you specify an environment, you _must_
736 * specify a complemete environment. This function
737 * will not merge environments. Use
738 * appSetEnvironmentVar to change environment
739 * variables in a complete environment set.
740 *
741 * -- If PROGDETAILS specifies a Win-OS/2 session
742 * and PROGDETAILS.pszEnvironment is empty,
743 * this uses the default Win-OS/2 environment.
744 * See appQueryDefaultWin31Environment.
745 *
746 * Even though this isn't clearly said in PMREF,
747 * PROGDETAILS.swpInitial is important:
748 *
749 * -- To start a session minimized, set SWP_MINIMIZE.
750 *
751 * -- To start a VIO session auto-close disabled, set
752 * the half-documented SWP_NOAUTOCLOSE flag (0x8000)
753 * This flag is now in the newer toolkit headers.
754 *
755 * Since this calls WinStartApp in turn, this
756 * requires a message queue on the calling thread.
757 *
758 *@@added V0.9.6 (2000-10-16) [umoeller]
759 *@@changed V0.9.7 (2000-12-10) [umoeller]: PROGDETAILS.swpInitial no longer zeroed... this broke VIOs
760 *@@changed V0.9.7 (2000-12-17) [umoeller]: PROGDETAILS.pszEnvironment no longer zeroed
761 *@@changed V0.9.9 (2001-01-27) [umoeller]: crashed if PROGDETAILS.pszExecutable was NULL
762 *@@changed V0.9.12 (2001-05-26) [umoeller]: fixed PROG_DEFAULT
763 *@@changed V0.9.12 (2001-05-27) [umoeller]: moved from winh.c to apps.c
764 */
765
766HAPP appStartApp(HWND hwndNotify, // in: notify window (as with WinStartApp)
767 const PROGDETAILS *pcProgDetails) // in: program data
768{
769 HAPP happ = NULLHANDLE;
770 XSTRING strParamsPatched;
771 PROGDETAILS ProgDetails;
772
773 PSZ pszWinOS2Env = 0;
774
775 memcpy(&ProgDetails, pcProgDetails, sizeof(PROGDETAILS));
776 // pointers still point into old prog details buffer
777 ProgDetails.Length = sizeof(PROGDETAILS);
778 ProgDetails.progt.fbVisible = SHE_VISIBLE;
779 // ProgDetails.pszEnvironment = 0;
780
781 // all this only makes sense if this contains something...
782 // besides, this crashed on string comparisons V0.9.9 (2001-01-27) [umoeller]
783 if (ProgDetails.pszExecutable)
784 {
785 ULONG ulIsWinApp;
786
787 // memset(&ProgDetails.swpInitial, 0, sizeof(SWP));
788 // this wasn't a good idea... WPProgram stores stuff
789 // in here, such as the "minimize on startup" -> SWP_MINIMIZE
790
791 // duplicate parameters...
792 // we need this for string manipulations below...
793 if (ProgDetails.pszParameters)
794 xstrInitCopy(&strParamsPatched,
795 ProgDetails.pszParameters,
796 100);
797 else
798 // no old params:
799 xstrInit(&strParamsPatched, 100);
800
801 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
802 // _Pmpf((" pszTitle: %s", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : NULL));
803 // _Pmpf((" pszIcon: %s", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : NULL));
804
805 // program type fixups
806 switch (ProgDetails.progt.progc) // that's a ULONG
807 {
808 case ((ULONG)-1): // we get that sometimes...
809 case PROG_DEFAULT:
810 {
811 // V0.9.12 (2001-05-26) [umoeller]
812 ULONG ulDosAppType;
813 appQueryAppType(ProgDetails.pszExecutable,
814 &ulDosAppType,
815 &ProgDetails.progt.progc);
816 }
817 break;
818 }
819
820 ulIsWinApp = appIsWindowsApp(ProgDetails.progt.progc);
821
822 // now try again...
823
824 /*
825 * command lines fixups:
826 *
827 */
828
829 if (strcmp(ProgDetails.pszExecutable, "*") == 0)
830 {
831 /*
832 * "*" for command sessions:
833 *
834 */
835
836 if (ulIsWinApp == 2)
837 {
838 // enhanced Win-OS/2 session:
839 PSZ psz = NULL;
840 if (strParamsPatched.ulLength)
841 // "/3 " + existing params
842 psz = strdup(strParamsPatched.psz);
843
844 xstrcpy(&strParamsPatched, "/3 ", 0);
845
846 if (psz)
847 {
848 xstrcat(&strParamsPatched, psz, 0);
849 free(psz);
850 }
851 }
852
853 if (ulIsWinApp)
854 {
855 // cheat: WinStartApp doesn't support NULL
856 // for Win-OS2 sessions, so manually start winos2.com
857 ProgDetails.pszExecutable = "WINOS2.COM";
858 // this is a DOS app, so fix this to DOS fullscreen
859 ProgDetails.progt.progc = PROG_VDM;
860 }
861 else
862 // for all other executable types
863 // (including OS/2 and DOS sessions),
864 // set pszExecutable to NULL; this will
865 // have WinStartApp start a cmd shell
866 ProgDetails.pszExecutable = NULL;
867
868 } // end if (strcmp(pProgDetails->pszExecutable, "*") == 0)
869 else
870 switch (ProgDetails.progt.progc)
871 {
872 /*
873 * .CMD files fixups
874 *
875 */
876
877 case PROG_FULLSCREEN: // OS/2 fullscreen
878 case PROG_WINDOWABLEVIO: // OS/2 window
879 {
880 PSZ pszExtension = doshGetExtension(ProgDetails.pszExecutable);
881 if (pszExtension)
882 {
883 if (stricmp(pszExtension, "CMD") == 0)
884 {
885 CallBatchCorrectly(&ProgDetails,
886 &strParamsPatched,
887 "OS2_SHELL",
888 "CMD.EXE");
889 }
890 }
891 break; }
892
893 case PROG_VDM: // DOS fullscreen
894 case PROG_WINDOWEDVDM: // DOS window
895 {
896 PSZ pszExtension = doshGetExtension(ProgDetails.pszExecutable);
897 if (pszExtension)
898 {
899 if (stricmp(pszExtension, "BAT") == 0)
900 {
901 CallBatchCorrectly(&ProgDetails,
902 &strParamsPatched,
903 NULL,
904 "COMMAND.COM");
905 }
906 }
907 break; }
908 } // end switch (ProgDetails.progt.progc)
909
910 if ( (ulIsWinApp)
911 && ( (ProgDetails.pszEnvironment == NULL)
912 || (!strlen(ProgDetails.pszEnvironment))
913 )
914 )
915 {
916 // this is a windoze app, and caller didn't bother
917 // to give us an environment:
918 // we MUST set one then, or we'll get the strangest
919 // errors, up to system hangs. V0.9.12 (2001-05-26) [umoeller]
920
921 DOSENVIRONMENT Env = {0};
922
923 // get standard WIN-OS/2 environment
924 PSZ pszTemp = appQueryDefaultWin31Environment();
925
926 if (!appParseEnvironment(pszTemp,
927 &Env))
928 {
929 // now override KBD_CTRL_BYPASS=CTRL_ESC
930 appSetEnvironmentVar(&Env,
931 "KBD_CTRL_BYPASS=CTRL_ESC",
932 FALSE); // add last
933 appSetEnvironmentVar(&Env,
934 "KBD_ALTHOME_BYPASS=1",
935 FALSE); // add last
936 appSetEnvironmentVar(&Env,
937 "INT_DURING_IO=1",
938 FALSE); // add last
939 appSetEnvironmentVar(&Env,
940 "HW_TIMER=1",
941 FALSE); // add last
942 appSetEnvironmentVar(&Env,
943 "MOUSE_EXCLUSIVE_ACCESS=0",
944 FALSE); // add last
945 appSetEnvironmentVar(&Env,
946 "VIDEO_RETRACE_EMULATION=1",
947 FALSE); // add last
948 appSetEnvironmentVar(&Env,
949 "VIDEO_SWITCH_NOTIFICATION=1",
950 FALSE); // add last
951 appSetEnvironmentVar(&Env,
952 "VIDEO_8514A_XGA_IOTRAP=0",
953 FALSE); // add last
954
955 if (!appConvertEnvironment(&Env,
956 &pszWinOS2Env, // freed at bottom
957 NULL))
958 ProgDetails.pszEnvironment = pszWinOS2Env;
959
960 appFreeEnvironment(&Env);
961 }
962
963 free(pszTemp);
964 }
965
966 if (!ProgDetails.pszTitle)
967 ProgDetails.pszTitle = ProgDetails.pszExecutable;
968
969 ProgDetails.pszParameters = strParamsPatched.psz;
970
971 _Pmpf(("progt.progc: %d", ProgDetails.progt.progc));
972 _Pmpf(("progt.fbVisible: 0x%lX", ProgDetails.progt.fbVisible));
973 _Pmpf(("progt.pszTitle: \"%s\"", (ProgDetails.pszTitle) ? ProgDetails.pszTitle : "NULL"));
974 _Pmpf(("exec: \"%s\"", (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL"));
975 _Pmpf(("params: \"%s\"", (ProgDetails.pszParameters) ? ProgDetails.pszParameters : "NULL"));
976 _Pmpf(("startup: \"%s\"", (ProgDetails.pszStartupDir) ? ProgDetails.pszStartupDir : "NULL"));
977 _Pmpf(("pszIcon: \"%s\"", (ProgDetails.pszIcon) ? ProgDetails.pszIcon : "NULL"));
978 _Pmpf(("environment: "));
979 {
980 PSZ pszThis = ProgDetails.pszEnvironment;
981 while (pszThis && *pszThis)
982 {
983 _Pmpf((" \"%s\"", pszThis));
984 pszThis += strlen(pszThis) + 1;
985 }
986 }
987
988 _Pmpf(("swpInitial.fl = 0x%lX, x = %d, y = %d, cx = %d, cy = %d:",
989 ProgDetails.swpInitial.fl,
990 ProgDetails.swpInitial.x,
991 ProgDetails.swpInitial.y,
992 ProgDetails.swpInitial.cx,
993 ProgDetails.swpInitial.cy));
994 _Pmpf((" behind = %d, hwnd = %d, res1 = %d, res2 = %d",
995 ProgDetails.swpInitial.hwndInsertBehind,
996 ProgDetails.swpInitial.hwnd,
997 ProgDetails.swpInitial.ulReserved1,
998 ProgDetails.swpInitial.ulReserved2));
999
1000 /* if (WinMessageBox(HWND_DESKTOP,
1001 NULLHANDLE,
1002 (ProgDetails.pszExecutable) ? ProgDetails.pszExecutable : "NULL",
1003 "Start?",
1004 0,
1005 MB_YESNO | MB_MOVEABLE)
1006 == MBID_YES) */
1007 happ = WinStartApp(hwndNotify,
1008 // receives WM_APPTERMINATENOTIFY
1009 &ProgDetails,
1010 strParamsPatched.psz,
1011 NULL, // "reserved", PMREF says...
1012 SAF_INSTALLEDCMDLINE);
1013 // we MUST use SAF_INSTALLEDCMDLINE
1014 // or no Win-OS/2 session will start...
1015 // whatever is going on here... Warp 4 FP11
1016
1017 // do not use SAF_STARTCHILDAPP, or the
1018 // app will be terminated automatically
1019 // when the WPS terminates!
1020
1021 // _Pmpf((__FUNCTION__ ": got happ 0x%lX", happ));
1022
1023 xstrClear(&strParamsPatched);
1024 if (pszWinOS2Env)
1025 free(pszWinOS2Env);
1026 } // end if (ProgDetails.pszExecutable)
1027
1028 return (happ);
1029}
1030
1031
Note: See TracBrowser for help on using the repository browser.