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 	_splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
58 	char* basedir = strstr( szDir, "\\PROGRAM\\");
59 	if (basedir) *basedir = 0;
60  	sprintf( szLibpath, "\"%s%s\\URE\\BIN\";\"%s%s\\BASIS\\PROGRAM\";%BeginLIBPATH%",
61   		szDrive, szDir, szDrive, szDir);
62 	DosSetExtLIBPATH( (PCSZ)szLibpath, BEGIN_LIBPATH);
63 	// make sure we load DLL from our path only, so multiple instances/versions
64 	// can be loaded.
65 #if 0
66 	// YD this feature is not compatible with innowin b20,
67 	// java cannot load with this flag enabled
68 	DosSetExtLIBPATH( (PCSZ)"T", LIBPATHSTRICT);
69 #endif
70 
71 	// adjust exe name
72 	_splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
73 	_makepath( szApplicationName, szDrive, szDir, OFFICE_IMAGE_NAME, (".bin") );
74 
75 	// copy command line parameters
76 	int i, len;
77 	len = strlen(szApplicationName) + 1 + strlen( APPLICATION_SWITCH) + 1 + 1;
78 	for( i=1; i<argc; i++)
79 		len += strlen( argv[i]) + 1;
80 
81 	char* pszCommandLine, *pszArgs;
82 	pszCommandLine = (char*) calloc( 1, len);
83 	strcpy( pszCommandLine, szApplicationName);
84 	pszArgs = pszCommandLine + strlen(szApplicationName) + 1;
85 	strcat( pszArgs, APPLICATION_SWITCH);
86 	strcat( pszArgs, " ");
87 	for( i=1; i<argc; i++) {
88 		// add quotes if argument has spaces!
89 		if (strchr( argv[i], ' '))
90 			strcat( pszArgs, "\"");
91 		strcat( pszArgs, argv[i]);
92 		if (strchr( argv[i], ' '))
93 			strcat( pszArgs, "\"");
94 		strcat( pszArgs, " ");
95 	}
96 	pszArgs[ strlen( pszArgs) + 0] = 0;
97 
98 	// execute
99 	rc = DosExecPgm(szFail, sizeof(szFail),
100                    EXEC_SYNC, (PCSZ)pszCommandLine, (PCSZ)NULL, &result,
101                    (PCSZ)szApplicationName);
102 	if (rc) {
103 		char     szMessage[ _MAX_PATH*2];
104 		sprintf( szMessage, "Execution failed! Contact technical support.\n\nReturn code: %d\nFailing module:%s\n", rc, szFail);
105 		rc = WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
106 							(PSZ)szMessage,
107 							(PSZ)"Unable to start Apache OpenOffice!",
108 							0, MB_ERROR | MB_OK);
109 		WinDestroyMsgQueue( hmq);
110 		WinTerminate( hab);
111 		exit(1);
112 	}
113 
114 	WinDestroyMsgQueue( hmq);
115 	WinTerminate( hab);
116 
117 	exit( result.codeResult);
118 }
119