xref: /AOO41X/main/desktop/os2/source/applauncher/launcher.cxx (revision 7b6b9ddb4b63a97ea0214b9472b5270bbf674949)
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 
68     // make sure we load DLL from our path only, so multiple instances/versions
69     // can be loaded.
70     DosSetExtLIBPATH( (PCSZ)"T", LIBPATHSTRICT);
71 
72 #if OSL_DEBUG_LEVEL > 0
73     rc = DosQueryExtLIBPATH( (PSZ)szLibpath, BEGIN_LIBPATH);
74     fprintf( stderr, "2 BeginLibPath: %s\n", szLibpath);
75 #endif
76 
77     // adjust exe name
78     _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
79     _makepath( szApplicationName, szDrive, szDir, OFFICE_IMAGE_NAME, (".bin") );
80 
81     // copy command line parameters
82     int i, len;
83     len = strlen(szApplicationName) + 1 + strlen( APPLICATION_SWITCH) + 1 + 1;
84     for( i=1; i<argc; i++)
85         len += strlen( argv[i]) + 1;
86 
87     char* pszCommandLine, *pszArgs;
88     pszCommandLine = (char*) calloc( 1, len);
89     strcpy( pszCommandLine, szApplicationName);
90     pszArgs = pszCommandLine + strlen(szApplicationName) + 1;
91     strcat( pszArgs, APPLICATION_SWITCH);
92     strcat( pszArgs, " ");
93     for( i=1; i<argc; i++) {
94         // add quotes if argument has spaces!
95         if (strchr( argv[i], ' '))
96             strcat( pszArgs, "\"");
97         strcat( pszArgs, argv[i]);
98         if (strchr( argv[i], ' '))
99             strcat( pszArgs, "\"");
100         strcat( pszArgs, " ");
101     }
102     pszArgs[ strlen( pszArgs) + 0] = 0;
103 
104     // execute
105     rc = DosExecPgm(szFail, sizeof(szFail),
106                    EXEC_SYNC, (PCSZ)pszCommandLine, (PCSZ)NULL, &result,
107                    (PCSZ)szApplicationName);
108     if (rc) {
109         char     szMessage[ _MAX_PATH*2];
110         sprintf( szMessage, "Execution failed! Contact technical support.\n\nReturn code: %d\nFailing module:%s\n", rc, szFail);
111         rc = WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
112                             (PSZ)szMessage,
113                             (PSZ)"Unable to start Apache OpenOffice!",
114                             0, MB_ERROR | MB_OK);
115         WinDestroyMsgQueue( hmq);
116         WinTerminate( hab);
117         exit(1);
118     }
119 
120     WinDestroyMsgQueue( hmq);
121     WinTerminate( hab);
122 
123     exit( result.codeResult);
124 }
125