1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "launcher.hxx"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <malloc.h>
34 #include <process.h>
35 
36 int main( int argc, char* argv[])
37 {
38 	PPIB	pib;
39 	APIRET   rc;
40 	RESULTCODES result = {0};
41 	char     szFail[ _MAX_PATH];
42 
43 	HAB hab = WinInitialize( 0);
44 	HMQ hmq = WinCreateMsgQueue( hab, 0);
45 	ERRORID    erridErrorCode = 0;
46 	erridErrorCode = WinGetLastError(hab);
47 
48 	// Calculate application name
49 	CHAR	szLibpath[_MAX_PATH*2];
50 	CHAR	szApplicationName[_MAX_PATH];
51 	CHAR	szDrive[_MAX_PATH];
52 	CHAR	szDir[_MAX_PATH];
53 	CHAR	szFileName[_MAX_PATH];
54 	CHAR	szExt[_MAX_PATH];
55 
56 	// get executable fullpath
57 	DosGetInfoBlocks(NULL, &pib);
58 	DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
59 
60 	// adjust libpath
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 
75 	// adjust exe name
76 	_splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
77 	_makepath( szApplicationName, szDrive, szDir, OFFICE_IMAGE_NAME, (".bin") );
78 
79 	// copy command line parameters
80 	int i, len;
81 	len = strlen(szApplicationName) + 1 + strlen( APPLICATION_SWITCH) + 1 + 1;
82 	for( i=1; i<argc; i++)
83 		len += strlen( argv[i]) + 1;
84 
85 	char* pszCommandLine, *pszArgs;
86 	pszCommandLine = (char*) calloc( 1, len);
87 	strcpy( pszCommandLine, szApplicationName);
88 	pszArgs = pszCommandLine + strlen(szApplicationName) + 1;
89 	strcat( pszArgs, APPLICATION_SWITCH);
90 	strcat( pszArgs, " ");
91 	for( i=1; i<argc; i++) {
92 		// add quotes if argument has spaces!
93 		if (strchr( argv[i], ' '))
94 			strcat( pszArgs, "\"");
95 		strcat( pszArgs, argv[i]);
96 		if (strchr( argv[i], ' '))
97 			strcat( pszArgs, "\"");
98 		strcat( pszArgs, " ");
99 	}
100 	pszArgs[ strlen( pszArgs) + 0] = 0;
101 
102 	// execute
103 	rc = DosExecPgm(szFail, sizeof(szFail),
104                    EXEC_SYNC, (PCSZ)pszCommandLine, (PCSZ)NULL, &result,
105                    (PCSZ)szApplicationName);
106 	if (rc) {
107 		char     szMessage[ _MAX_PATH*2];
108 		sprintf( szMessage, "Execution failed! Contact technical support.\n\nReturn code: %d\nFailing module:%s\n", rc, szFail);
109 		rc = WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
110 							(PSZ)szMessage,
111 							(PSZ)"Unable to start OpenOffice.org!",
112 							0, MB_ERROR | MB_OK);
113 		WinDestroyMsgQueue( hmq);
114 		WinTerminate( hab);
115 		exit(1);
116 	}
117 
118 	WinDestroyMsgQueue( hmq);
119 	WinTerminate( hab);
120 
121 	exit( result.codeResult);
122 }
123