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