xref: /aoo4110/main/shell/source/unix/misc/open-url.c (revision b1cdbd2c)
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 <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <process.h>
29 #include <time.h>
30 
31 #define INCL_DOS
32 #define INCL_DOSERRORS
33 #define INCL_PM
34 #include <os2.h>
35 
36 // OOo uses popen() to start us, so we cannot show PM dialogs.
37 // log message to disk.
logMessage(char * msg)38 void logMessage( char* msg)
39 {
40     PPIB	pib;
41     CHAR	szApplicationName[_MAX_PATH];
42     CHAR	szDrive[_MAX_PATH];
43     CHAR	szDir[_MAX_PATH];
44     CHAR	szFileName[_MAX_PATH];
45     CHAR	szExt[_MAX_PATH];
46     FILE*	log;
47     time_t	timeOfDay;
48     struct tm* localTime;
49 
50     // get executable fullpath
51     DosGetInfoBlocks(NULL, &pib);
52     DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
53     _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
54     // log name
55     _makepath( szApplicationName, szDrive, szDir, szFileName, (".LOG") );
56     log = fopen( szApplicationName, "a");
57     if (!log)
58 	return;
59     time( &timeOfDay);
60     localTime = localtime( &timeOfDay);
61     fprintf( log, "%04d/%02d/%02d %02d:%02d:%02d %s\n",
62 	localTime->tm_year+1900, localTime->tm_mon+1, localTime->tm_mday,
63 	localTime->tm_hour, localTime->tm_min, localTime->tm_sec, msg);
64     fclose( log);
65 }
66 
67 // dump comand line arguments
dumpArgs(int argc,char * argv[])68 void dumpArgs( int argc, char *argv[] )
69 {
70     int	i;
71 
72     logMessage( "Start of command line arguments dump:");
73     for( i=0; i<argc; i++)
74 	logMessage( argv[i]);
75 }
76 
77 /*
78  * The intended use of this tool is to pass the argument to
79  * the default URL exe.
80  */
main(int argc,char * argv[])81 int main(int argc, char *argv[] )
82 {
83     APIRET	rc;
84     RESULTCODES result = {0};
85     char 		szAppFromINI[_MAX_PATH];
86     char 		szDirFromINI[_MAX_PATH];
87     char 		szCmdLine[1024];
88     char     	szFail[ _MAX_PATH];
89     ULONG 	ulSID;
90     PID 		pid;
91 
92     // check parameters
93     if (argc != 2)
94     {
95 	logMessage( "Usage: open-url <url>");
96 	dumpArgs( argc, argv);
97         return -1;
98     }
99 
100     // check configuration
101     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
102                           "DefaultBrowserExe", "",
103                           szAppFromINI, sizeof(szAppFromINI));
104     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
105                           "DefaultWorkingDir", "",
106                           szDirFromINI, sizeof(szDirFromINI));
107     if (*szAppFromINI == 0 || *szDirFromINI == 0)
108     {
109 	logMessage( "Unable to find default url handler in USER.INI; exiting.");
110 	dumpArgs( argc, argv);
111         return -1;
112     }
113 
114     // get default parameter list
115     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
116                           "DefaultParameters", "",
117                           szCmdLine, sizeof(szCmdLine));
118     strcat( szCmdLine, " ");
119     strcat( szCmdLine, argv[1]);
120 
121     // change default directory
122     _chdir( szDirFromINI);
123 
124     // start default handler
125     STARTDATA   SData;
126     CHAR        szObjBuf[CCHMAXPATH];
127 
128     SData.Length  = sizeof(STARTDATA);
129     SData.Related = SSF_RELATED_INDEPENDENT;
130     SData.FgBg    = (1) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
131     SData.TraceOpt = SSF_TRACEOPT_NONE;
132 
133     SData.PgmTitle = (PSZ)szAppFromINI;
134 
135     SData.PgmName = (PSZ)szAppFromINI;
136     SData.PgmInputs = (PSZ)szCmdLine;
137 
138     SData.TermQ = NULL;
139     SData.Environment = 0;
140     SData.InheritOpt = SSF_INHERTOPT_PARENT;
141     SData.SessionType = SSF_TYPE_PM;
142     SData.IconFile = 0;
143     SData.PgmHandle = 0;
144 
145     SData.PgmControl = SSF_CONTROL_VISIBLE;
146 
147     SData.InitXPos  = 30;
148     SData.InitYPos  = 40;
149     SData.InitXSize = 200;
150     SData.InitYSize = 140;
151     SData.Reserved = 0;
152     SData.ObjectBuffer  = szFail;
153     SData.ObjectBuffLen = (ULONG)sizeof(szFail);
154 
155     rc = DosStartSession( &SData, &ulSID, &pid);
156     // show error dialog in case of problems
157     if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND) {
158 	    char     szMessage[ _MAX_PATH*2];
159 	    sprintf( szMessage, "Execution failed! rc: %d, failing module:%s", rc, szFail);
160 	    logMessage( szMessage);
161 	    dumpArgs( argc, argv);
162 	    return -1;
163     }
164 
165     // ok
166     return 0;
167 }
168 
169