xref: /aoo42x/main/shell/source/unix/misc/open-url.c (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <stdlib.h>
29*cdf0e10cSrcweir #include <stdio.h>
30*cdf0e10cSrcweir #include <string.h>
31*cdf0e10cSrcweir #include <unistd.h>
32*cdf0e10cSrcweir #include <process.h>
33*cdf0e10cSrcweir #include <time.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #define INCL_DOS
36*cdf0e10cSrcweir #define INCL_DOSERRORS
37*cdf0e10cSrcweir #define INCL_PM
38*cdf0e10cSrcweir #include <os2.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // OOo uses popen() to start us, so we cannot show PM dialogs.
41*cdf0e10cSrcweir // log message to disk.
42*cdf0e10cSrcweir void logMessage( char* msg)
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir     PPIB	pib;
45*cdf0e10cSrcweir     CHAR	szApplicationName[_MAX_PATH];
46*cdf0e10cSrcweir     CHAR	szDrive[_MAX_PATH];
47*cdf0e10cSrcweir     CHAR	szDir[_MAX_PATH];
48*cdf0e10cSrcweir     CHAR	szFileName[_MAX_PATH];
49*cdf0e10cSrcweir     CHAR	szExt[_MAX_PATH];
50*cdf0e10cSrcweir     FILE*	log;
51*cdf0e10cSrcweir     time_t	timeOfDay;
52*cdf0e10cSrcweir     struct tm* localTime;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     // get executable fullpath
55*cdf0e10cSrcweir     DosGetInfoBlocks(NULL, &pib);
56*cdf0e10cSrcweir     DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
57*cdf0e10cSrcweir     _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
58*cdf0e10cSrcweir     // log name
59*cdf0e10cSrcweir     _makepath( szApplicationName, szDrive, szDir, szFileName, (".LOG") );
60*cdf0e10cSrcweir     log = fopen( szApplicationName, "a");
61*cdf0e10cSrcweir     if (!log)
62*cdf0e10cSrcweir 	return;
63*cdf0e10cSrcweir     time( &timeOfDay);
64*cdf0e10cSrcweir     localTime = localtime( &timeOfDay);
65*cdf0e10cSrcweir     fprintf( log, "%04d/%02d/%02d %02d:%02d:%02d %s\n",
66*cdf0e10cSrcweir 	localTime->tm_year+1900, localTime->tm_mon+1, localTime->tm_mday,
67*cdf0e10cSrcweir 	localTime->tm_hour, localTime->tm_min, localTime->tm_sec, msg);
68*cdf0e10cSrcweir     fclose( log);
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir // dump comand line arguments
72*cdf0e10cSrcweir void dumpArgs( int argc, char *argv[] )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     int	i;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     logMessage( "Start of command line arguments dump:");
77*cdf0e10cSrcweir     for( i=0; i<argc; i++)
78*cdf0e10cSrcweir 	logMessage( argv[i]);
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir /*
82*cdf0e10cSrcweir  * The intended use of this tool is to pass the argument to
83*cdf0e10cSrcweir  * the default URL exe.
84*cdf0e10cSrcweir  */
85*cdf0e10cSrcweir int main(int argc, char *argv[] )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     APIRET	rc;
88*cdf0e10cSrcweir     RESULTCODES result = {0};
89*cdf0e10cSrcweir     char 		szAppFromINI[_MAX_PATH];
90*cdf0e10cSrcweir     char 		szDirFromINI[_MAX_PATH];
91*cdf0e10cSrcweir     char 		szCmdLine[1024];
92*cdf0e10cSrcweir     char     	szFail[ _MAX_PATH];
93*cdf0e10cSrcweir     ULONG 	ulSID;
94*cdf0e10cSrcweir     PID 		pid;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     // check parameters
97*cdf0e10cSrcweir     if (argc != 2)
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir 	logMessage( "Usage: open-url <url>");
100*cdf0e10cSrcweir 	dumpArgs( argc, argv);
101*cdf0e10cSrcweir         return -1;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     // check configuration
105*cdf0e10cSrcweir     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
106*cdf0e10cSrcweir                           "DefaultBrowserExe", "",
107*cdf0e10cSrcweir                           szAppFromINI, sizeof(szAppFromINI));
108*cdf0e10cSrcweir     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
109*cdf0e10cSrcweir                           "DefaultWorkingDir", "",
110*cdf0e10cSrcweir                           szDirFromINI, sizeof(szDirFromINI));
111*cdf0e10cSrcweir     if (*szAppFromINI == 0 || *szDirFromINI == 0)
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir 	logMessage( "Unable to find default url handler in USER.INI; exiting.");
114*cdf0e10cSrcweir 	dumpArgs( argc, argv);
115*cdf0e10cSrcweir         return -1;
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     // get default parameter list
119*cdf0e10cSrcweir     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
120*cdf0e10cSrcweir                           "DefaultParameters", "",
121*cdf0e10cSrcweir                           szCmdLine, sizeof(szCmdLine));
122*cdf0e10cSrcweir     strcat( szCmdLine, " ");
123*cdf0e10cSrcweir     strcat( szCmdLine, argv[1]);
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     // change default directory
126*cdf0e10cSrcweir     _chdir( szDirFromINI);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     // start default handler
129*cdf0e10cSrcweir     STARTDATA   SData;
130*cdf0e10cSrcweir     CHAR        szObjBuf[CCHMAXPATH];
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     SData.Length  = sizeof(STARTDATA);
133*cdf0e10cSrcweir     SData.Related = SSF_RELATED_INDEPENDENT;
134*cdf0e10cSrcweir     SData.FgBg    = (1) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
135*cdf0e10cSrcweir     SData.TraceOpt = SSF_TRACEOPT_NONE;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     SData.PgmTitle = (PSZ)szAppFromINI;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     SData.PgmName = (PSZ)szAppFromINI;
140*cdf0e10cSrcweir     SData.PgmInputs = (PSZ)szCmdLine;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     SData.TermQ = NULL;
143*cdf0e10cSrcweir     SData.Environment = 0;
144*cdf0e10cSrcweir     SData.InheritOpt = SSF_INHERTOPT_PARENT;
145*cdf0e10cSrcweir     SData.SessionType = SSF_TYPE_PM;
146*cdf0e10cSrcweir     SData.IconFile = 0;
147*cdf0e10cSrcweir     SData.PgmHandle = 0;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     SData.PgmControl = SSF_CONTROL_VISIBLE;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     SData.InitXPos  = 30;
152*cdf0e10cSrcweir     SData.InitYPos  = 40;
153*cdf0e10cSrcweir     SData.InitXSize = 200;
154*cdf0e10cSrcweir     SData.InitYSize = 140;
155*cdf0e10cSrcweir     SData.Reserved = 0;
156*cdf0e10cSrcweir     SData.ObjectBuffer  = szFail;
157*cdf0e10cSrcweir     SData.ObjectBuffLen = (ULONG)sizeof(szFail);
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     rc = DosStartSession( &SData, &ulSID, &pid);
160*cdf0e10cSrcweir     // show error dialog in case of problems
161*cdf0e10cSrcweir     if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND) {
162*cdf0e10cSrcweir 	    char     szMessage[ _MAX_PATH*2];
163*cdf0e10cSrcweir 	    sprintf( szMessage, "Execution failed! rc: %d, failing module:%s", rc, szFail);
164*cdf0e10cSrcweir 	    logMessage( szMessage);
165*cdf0e10cSrcweir 	    dumpArgs( argc, argv);
166*cdf0e10cSrcweir 	    return -1;
167*cdf0e10cSrcweir     }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     // ok
170*cdf0e10cSrcweir     return 0;
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173