xref: /trunk/main/desktop/os2/source/applauncher/launcher.cxx (revision 055eca97e13cd9ab7f4ec8f50a399c535c159ce6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "launcher.hxx"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <malloc.h>
30 #include <process.h>
31 
32 int main( int argc, char* argv[])
33 {
34     PPIB    pib;
35     APIRET   rc;
36     RESULTCODES result = {0};
37     char     szFail[ _MAX_PATH];
38 
39     HAB hab = WinInitialize( 0);
40     HMQ hmq = WinCreateMsgQueue( hab, 0);
41     ERRORID    erridErrorCode = 0;
42     erridErrorCode = WinGetLastError(hab);
43 
44     // Calculate application name
45     CHAR    szLibpath[_MAX_PATH*2];
46     CHAR    szApplicationName[_MAX_PATH];
47     CHAR    szDrive[_MAX_PATH];
48     CHAR    szDir[_MAX_PATH];
49     CHAR    szFileName[_MAX_PATH];
50     CHAR    szExt[_MAX_PATH];
51 
52     // get executable fullpath
53     DosGetInfoBlocks(NULL, &pib);
54     DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
55 
56     // adjust libpath
57 #if OSL_DEBUG_LEVEL > 0
58     rc = DosQueryExtLIBPATH( (PSZ)szLibpath, BEGIN_LIBPATH);
59     fprintf( stderr, "1 BeginLibPath: %s\n", szLibpath);
60 #endif
61     _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
62     char* basedir = strstr( szDir, "\\PROGRAM\\");
63     if (basedir) *basedir = 0;
64     sprintf( szLibpath, "\"%s%s\\URE\\BIN\";\"%s%s\\BASIS\\PROGRAM\";%%BeginLIBPATH%%;",
65         szDrive, szDir, szDrive, szDir);
66     DosSetExtLIBPATH( (PCSZ)szLibpath, BEGIN_LIBPATH);
67     // make sure we load DLL from our path only, so multiple instances/versions
68     // can be loaded.
69 #if 0
70     // YD this feature is not compatible with innowin b20,
71     // java cannot load with this flag enabled
72     DosSetExtLIBPATH( (PCSZ)"T", LIBPATHSTRICT);
73 #endif
74 #if OSL_DEBUG_LEVEL > 0
75     rc = DosQueryExtLIBPATH( (PSZ)szLibpath, BEGIN_LIBPATH);
76     fprintf( stderr, "2 BeginLibPath: %s\n", szLibpath);
77 #endif
78 
79     // adjust exe name
80     _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
81     _makepath( szApplicationName, szDrive, szDir, OFFICE_IMAGE_NAME, (".bin") );
82 
83     // copy command line parameters
84     int i, len;
85     len = strlen(szApplicationName) + 1 + strlen( APPLICATION_SWITCH) + 1 + 1;
86     for( i=1; i<argc; i++)
87         len += strlen( argv[i]) + 1;
88 
89     char* pszCommandLine, *pszArgs;
90     pszCommandLine = (char*) calloc( 1, len);
91     strcpy( pszCommandLine, szApplicationName);
92     pszArgs = pszCommandLine + strlen(szApplicationName) + 1;
93     strcat( pszArgs, APPLICATION_SWITCH);
94     strcat( pszArgs, " ");
95     for( i=1; i<argc; i++) {
96         // add quotes if argument has spaces!
97         if (strchr( argv[i], ' '))
98             strcat( pszArgs, "\"");
99         strcat( pszArgs, argv[i]);
100         if (strchr( argv[i], ' '))
101             strcat( pszArgs, "\"");
102         strcat( pszArgs, " ");
103     }
104     pszArgs[ strlen( pszArgs) + 0] = 0;
105 
106     // execute
107     rc = DosExecPgm(szFail, sizeof(szFail),
108                    EXEC_SYNC, (PCSZ)pszCommandLine, (PCSZ)NULL, &result,
109                    (PCSZ)szApplicationName);
110     if (rc) {
111         char     szMessage[ _MAX_PATH*2];
112         sprintf( szMessage, "Execution failed! Contact technical support.\n\nReturn code: %d\nFailing module:%s\n", rc, szFail);
113         rc = WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
114                             (PSZ)szMessage,
115                             (PSZ)"Unable to start Apache OpenOffice!",
116                             0, MB_ERROR | MB_OK);
117         WinDestroyMsgQueue( hmq);
118         WinTerminate( hab);
119         exit(1);
120     }
121 
122     WinDestroyMsgQueue( hmq);
123     WinTerminate( hab);
124 
125     exit( result.codeResult);
126 }
127