xref: /AOO41X/main/desktop/os2/source/applauncher/launcher.cxx (revision fe22d2cfc602815794415026f1317bd625db6f83)
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 
main(int argc,char * argv[])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    szApplicationName[_MAX_PATH];
46 
47     // get executable fullpath
48     DosGetInfoBlocks(NULL, &pib);
49     DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
50 
51     // adjust libpath
52 #if OSL_DEBUG_LEVEL > 0
53     CHAR    szLibpath[_MAX_PATH*2];
54     rc = DosQueryExtLIBPATH( (PSZ)szLibpath, BEGIN_LIBPATH);
55     fprintf( stderr, "1 BeginLibPath: %s\n", szLibpath);
56 #endif
57     char* basedir = strrchr( szApplicationName, '\\');
58     if (basedir) *basedir = 0;
59     DosSetExtLIBPATH( (PCSZ)szApplicationName, BEGIN_LIBPATH);
60 
61     // make sure we load DLL from our path only, so multiple instances/versions
62     // can be loaded.
63     DosSetExtLIBPATH( (PCSZ)"T", LIBPATHSTRICT);
64 
65 #if OSL_DEBUG_LEVEL > 0
66     rc = DosQueryExtLIBPATH( (PSZ)szLibpath, BEGIN_LIBPATH);
67     fprintf( stderr, "2 BeginLibPath: %s\n", szLibpath);
68 #endif
69 
70     // adjust exe name
71     strcat( szApplicationName, "\\" OFFICE_IMAGE_NAME ".bin");
72 
73     // copy command line parameters
74     int i, len;
75     len = strlen(szApplicationName) + 1 + strlen( APPLICATION_SWITCH) + 1 + 1;
76     for( i=1; i<argc; i++)
77         len += strlen( argv[i]) + 1;
78 
79     char* pszCommandLine, *pszArgs;
80     pszCommandLine = (char*) calloc( 1, len);
81     strcpy( pszCommandLine, szApplicationName);
82     pszArgs = pszCommandLine + strlen(szApplicationName) + 1;
83     strcat( pszArgs, APPLICATION_SWITCH);
84     strcat( pszArgs, " ");
85     for( i=1; i<argc; i++) {
86         // add quotes if argument has spaces!
87         if (strchr( argv[i], ' '))
88             strcat( pszArgs, "\"");
89         strcat( pszArgs, argv[i]);
90         if (strchr( argv[i], ' '))
91             strcat( pszArgs, "\"");
92         strcat( pszArgs, " ");
93     }
94     pszArgs[ strlen( pszArgs) + 0] = 0;
95 
96     // execute
97     rc = DosExecPgm(szFail, sizeof(szFail),
98                    EXEC_SYNC, (PCSZ)pszCommandLine, (PCSZ)NULL, &result,
99                    (PCSZ)szApplicationName);
100     if (rc) {
101         char     szMessage[ _MAX_PATH*2];
102         sprintf( szMessage, "Execution failed! Contact technical support.\n\nReturn code: %d\nFailing module:%s\n", rc, szFail);
103         rc = WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
104                             (PSZ)szMessage,
105                             (PSZ)"Unable to start Apache OpenOffice!",
106                             0, MB_ERROR | MB_OK);
107         WinDestroyMsgQueue( hmq);
108         WinTerminate( hab);
109         exit(1);
110     }
111 
112     WinDestroyMsgQueue( hmq);
113     WinTerminate( hab);
114 
115     exit( result.codeResult);
116 }
117