bump product version to 4.1.6.2
[LibreOffice.git] / shell / source / win32 / SysShExec.cxx
blobb08160764b9518bd28a038e877835bab45c7e102
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/diagnose.h>
21 #include "SysShExec.hxx"
22 #include <osl/file.hxx>
23 #include <sal/macros.h>
25 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
26 #include <com/sun/star/uri/UriReferenceFactory.hpp>
28 #define WIN32_LEAN_AND_MEAN
29 #if defined _MSC_VER
30 #pragma warning(push, 1)
31 #endif
32 #include <windows.h>
33 #include <shellapi.h>
34 #include <objbase.h>
35 #if defined _MSC_VER
36 #pragma warning(pop)
37 #endif
39 //------------------------------------------------------------------------
40 // namespace directives
41 //------------------------------------------------------------------------
43 using com::sun::star::uno::Reference;
44 using com::sun::star::uno::RuntimeException;
45 using com::sun::star::uno::Sequence;
46 using com::sun::star::uno::XInterface;
47 using com::sun::star::lang::EventObject;
48 using com::sun::star::lang::XServiceInfo;
49 using com::sun::star::lang::IllegalArgumentException;
50 using osl::Mutex;
51 using com::sun::star::system::XSystemShellExecute;
52 using com::sun::star::system::SystemShellExecuteException;
54 using namespace ::com::sun::star::system::SystemShellExecuteFlags;
55 using namespace cppu;
57 #define SYSSHEXEC_IMPL_NAME "com.sun.star.sys.shell.SystemShellExecute"
59 //------------------------------------------------------------------------
60 // helper functions
61 //------------------------------------------------------------------------
63 namespace // private
65 Sequence< OUString > SAL_CALL SysShExec_getSupportedServiceNames()
67 Sequence< OUString > aRet(1);
68 aRet[0] = OUString("com.sun.star.sys.shell.SystemShellExecute");
69 return aRet;
72 /* This is the error table that defines the mapping between OS error
73 codes and errno values */
75 struct errentry {
76 unsigned long oscode; /* OS return value */
77 int errnocode; /* System V error code */
80 struct errentry errtable[] = {
81 { ERROR_SUCCESS, osl_File_E_None }, /* 0 */
82 { ERROR_INVALID_FUNCTION, osl_File_E_INVAL }, /* 1 */
83 { ERROR_FILE_NOT_FOUND, osl_File_E_NOENT }, /* 2 */
84 { ERROR_PATH_NOT_FOUND, osl_File_E_NOENT }, /* 3 */
85 { ERROR_TOO_MANY_OPEN_FILES, osl_File_E_MFILE }, /* 4 */
86 { ERROR_ACCESS_DENIED, osl_File_E_ACCES }, /* 5 */
87 { ERROR_INVALID_HANDLE, osl_File_E_BADF }, /* 6 */
88 { ERROR_ARENA_TRASHED, osl_File_E_NOMEM }, /* 7 */
89 { ERROR_NOT_ENOUGH_MEMORY, osl_File_E_NOMEM }, /* 8 */
90 { ERROR_INVALID_BLOCK, osl_File_E_NOMEM }, /* 9 */
91 { ERROR_BAD_ENVIRONMENT, osl_File_E_2BIG }, /* 10 */
92 { ERROR_BAD_FORMAT, osl_File_E_NOEXEC }, /* 11 */
93 { ERROR_INVALID_ACCESS, osl_File_E_INVAL }, /* 12 */
94 { ERROR_INVALID_DATA, osl_File_E_INVAL }, /* 13 */
95 { ERROR_INVALID_DRIVE, osl_File_E_NOENT }, /* 15 */
96 { ERROR_CURRENT_DIRECTORY, osl_File_E_ACCES }, /* 16 */
97 { ERROR_NOT_SAME_DEVICE, osl_File_E_XDEV }, /* 17 */
98 { ERROR_NO_MORE_FILES, osl_File_E_NOENT }, /* 18 */
99 { ERROR_LOCK_VIOLATION, osl_File_E_ACCES }, /* 33 */
100 { ERROR_BAD_NETPATH, osl_File_E_NOENT }, /* 53 */
101 { ERROR_NETWORK_ACCESS_DENIED, osl_File_E_ACCES }, /* 65 */
102 { ERROR_BAD_NET_NAME, osl_File_E_NOENT }, /* 67 */
103 { ERROR_FILE_EXISTS, osl_File_E_EXIST }, /* 80 */
104 { ERROR_CANNOT_MAKE, osl_File_E_ACCES }, /* 82 */
105 { ERROR_FAIL_I24, osl_File_E_ACCES }, /* 83 */
106 { ERROR_INVALID_PARAMETER, osl_File_E_INVAL }, /* 87 */
107 { ERROR_NO_PROC_SLOTS, osl_File_E_AGAIN }, /* 89 */
108 { ERROR_DRIVE_LOCKED, osl_File_E_ACCES }, /* 108 */
109 { ERROR_BROKEN_PIPE, osl_File_E_PIPE }, /* 109 */
110 { ERROR_DISK_FULL, osl_File_E_NOSPC }, /* 112 */
111 { ERROR_INVALID_TARGET_HANDLE, osl_File_E_BADF }, /* 114 */
112 { ERROR_INVALID_HANDLE, osl_File_E_INVAL }, /* 124 */
113 { ERROR_WAIT_NO_CHILDREN, osl_File_E_CHILD }, /* 128 */
114 { ERROR_CHILD_NOT_COMPLETE, osl_File_E_CHILD }, /* 129 */
115 { ERROR_DIRECT_ACCESS_HANDLE, osl_File_E_BADF }, /* 130 */
116 { ERROR_NEGATIVE_SEEK, osl_File_E_INVAL }, /* 131 */
117 { ERROR_SEEK_ON_DEVICE, osl_File_E_ACCES }, /* 132 */
118 { ERROR_DIR_NOT_EMPTY, osl_File_E_NOTEMPTY }, /* 145 */
119 { ERROR_NOT_LOCKED, osl_File_E_ACCES }, /* 158 */
120 { ERROR_BAD_PATHNAME, osl_File_E_NOENT }, /* 161 */
121 { ERROR_MAX_THRDS_REACHED, osl_File_E_AGAIN }, /* 164 */
122 { ERROR_LOCK_FAILED, osl_File_E_ACCES }, /* 167 */
123 { ERROR_ALREADY_EXISTS, osl_File_E_EXIST }, /* 183 */
124 { ERROR_FILENAME_EXCED_RANGE, osl_File_E_NOENT }, /* 206 */
125 { ERROR_NESTING_NOT_ALLOWED, osl_File_E_AGAIN }, /* 215 */
126 { ERROR_NOT_ENOUGH_QUOTA, osl_File_E_NOMEM } /* 1816 */
129 /* size of the table */
130 #define ERRTABLESIZE (SAL_N_ELEMENTS(errtable))
132 /* The following two constants must be the minimum and maximum
133 values in the (contiguous) range of osl_File_E_xec Failure errors. */
134 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
135 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
137 /* These are the low and high value in the range of errors that are
138 access violations */
139 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
140 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
143 /*******************************************************************************/
145 oslFileError _mapError( DWORD dwError )
147 unsigned i;
149 /* check the table for the OS error code */
150 for ( i = 0; i < ERRTABLESIZE; ++i )
152 if ( dwError == errtable[i].oscode )
153 return (oslFileError)errtable[i].errnocode;
156 /* The error code wasn't in the table. We check for a range of */
157 /* osl_File_E_ACCES errors or exec failure errors (ENOEXEC). Otherwise */
158 /* osl_File_E_INVAL is returned. */
160 if ( dwError >= MIN_EACCES_RANGE && dwError <= MAX_EACCES_RANGE)
161 return osl_File_E_ACCES;
162 else if ( dwError >= MIN_EXEC_ERROR && dwError <= MAX_EXEC_ERROR)
163 return osl_File_E_NOEXEC;
164 else
165 return osl_File_E_INVAL;
168 #define MapError( oserror ) _mapError( oserror )
170 #define E_UNKNOWN_EXEC_ERROR -1
172 //-----------------------------------------
174 bool is_system_path(const OUString& path_or_uri)
176 OUString url;
177 osl::FileBase::RC rc = osl::FileBase::getFileURLFromSystemPath(path_or_uri, url);
178 return (rc == osl::FileBase::E_None);
181 //-----------------------------------------
182 // trying to identify a jump mark
183 //-----------------------------------------
185 const OUString JUMP_MARK_HTM(".htm#");
186 const OUString JUMP_MARK_HTML(".html#");
187 const sal_Unicode HASH_MARK = (sal_Unicode)'#';
189 bool has_jump_mark(const OUString& system_path, sal_Int32* jmp_mark_start = NULL)
191 sal_Int32 jmp_mark = std::max<int>(
192 system_path.lastIndexOf(JUMP_MARK_HTM),
193 system_path.lastIndexOf(JUMP_MARK_HTML));
195 if (jmp_mark_start)
196 *jmp_mark_start = jmp_mark;
198 return (jmp_mark > -1);
201 //-----------------------------------------
203 bool is_existing_file(const OUString& file_name)
205 OSL_ASSERT(is_system_path(file_name));
207 bool exist = false;
209 OUString file_url;
210 osl::FileBase::RC rc = osl::FileBase::getFileURLFromSystemPath(file_name, file_url);
212 if (osl::FileBase::E_None == rc)
214 osl::DirectoryItem dir_item;
215 rc = osl::DirectoryItem::get(file_url, dir_item);
216 exist = (osl::FileBase::E_None == rc);
218 return exist;
221 //-------------------------------------------------
222 // Jump marks in file urls are illegal.
223 //-------------------------------------------------
225 void remove_jump_mark(OUString* p_command)
227 OSL_PRECOND(p_command, "invalid parameter");
229 sal_Int32 pos;
230 if (has_jump_mark(*p_command, &pos))
232 const sal_Unicode* p_jmp_mark = p_command->getStr() + pos;
233 while (*p_jmp_mark && (*p_jmp_mark != HASH_MARK))
234 p_jmp_mark++;
236 *p_command = OUString(p_command->getStr(), p_jmp_mark - p_command->getStr());
240 } // end namespace
242 //-----------------------------------------------------------------------------------------
244 CSysShExec::CSysShExec( const Reference< css::uno::XComponentContext >& xContext ) :
245 WeakComponentImplHelper2< XSystemShellExecute, XServiceInfo >( m_aMutex ),
246 m_xContext(xContext)
249 * As this service is declared thread-affine, it is ensured to be called from a
250 * dedicated thread, so initialize COM here.
252 * We need COM to be initialized for STA, but osl thread get initialized for MTA.
253 * Once this changed, we can remove the uninitialize call.
255 CoUninitialize();
256 CoInitialize( NULL );
259 //-------------------------------------------------
261 void SAL_CALL CSysShExec::execute( const OUString& aCommand, const OUString& aParameter, sal_Int32 nFlags )
262 throw (IllegalArgumentException, SystemShellExecuteException, RuntimeException)
264 // parameter checking
265 if (0 == aCommand.getLength())
266 throw IllegalArgumentException(
267 OUString("Empty command"),
268 static_cast< XSystemShellExecute* >( this ),
269 1 );
271 if ((nFlags & ~(NO_SYSTEM_ERROR_MESSAGE | URIS_ONLY)) != 0)
272 throw IllegalArgumentException(
273 OUString("Invalid Flags specified"),
274 static_cast< XSystemShellExecute* >( this ),
275 3 );
277 if ((nFlags & URIS_ONLY) != 0)
279 css::uno::Reference< css::uri::XUriReference > uri(
280 css::uri::UriReferenceFactory::create(m_xContext)->parse(aCommand));
281 if (!(uri.is() && uri->isAbsolute()))
283 throw css::lang::IllegalArgumentException(
284 (OUString(
285 "XSystemShellExecute.execute URIS_ONLY with"
286 " non-absolute URI reference ")
287 + aCommand),
288 static_cast< cppu::OWeakObject * >(this), 0);
292 /* #i4789#; jump mark detection on system paths
293 if the given command is a system path (not http or
294 other uri schemes) and seems to have a jump mark
295 and names no existing file (remember the jump mark
296 sign '#' is a valid file name character we remove
297 the jump mark, else ShellExecuteEx fails */
298 OUString preprocessed_command(aCommand);
299 if (is_system_path(preprocessed_command))
301 if (has_jump_mark(preprocessed_command) && !is_existing_file(preprocessed_command))
302 remove_jump_mark(&preprocessed_command);
304 /* Convert file uris to system paths */
305 else
307 OUString aSystemPath;
308 if (::osl::FileBase::E_None == ::osl::FileBase::getSystemPathFromFileURL(preprocessed_command, aSystemPath))
309 preprocessed_command = aSystemPath;
312 SHELLEXECUTEINFOW sei;
313 ZeroMemory(&sei, sizeof( sei));
315 sei.cbSize = sizeof(sei);
316 sei.lpFile = reinterpret_cast<LPCWSTR>(preprocessed_command.getStr());
317 sei.lpParameters = reinterpret_cast<LPCWSTR>(aParameter.getStr());
318 sei.nShow = SW_SHOWNORMAL;
320 if (NO_SYSTEM_ERROR_MESSAGE & nFlags)
321 sei.fMask = SEE_MASK_FLAG_NO_UI;
323 SetLastError( 0 );
325 sal_Bool bRet = ShellExecuteExW(&sei) ? sal_True : sal_False;
327 if (!bRet && (nFlags & NO_SYSTEM_ERROR_MESSAGE))
329 // ShellExecuteEx fails to set an error code
330 // we return osl_File_E_INVAL
331 sal_Int32 psxErr = GetLastError();
332 if (ERROR_SUCCESS == psxErr)
333 psxErr = E_UNKNOWN_EXEC_ERROR;
334 else
335 psxErr = MapError(psxErr);
337 throw SystemShellExecuteException(
338 OUString("Error executing command"),
339 static_cast< XSystemShellExecute* >(this),
340 psxErr);
344 // -------------------------------------------------
345 // XServiceInfo
346 // -------------------------------------------------
348 OUString SAL_CALL CSysShExec::getImplementationName( )
349 throw( RuntimeException )
351 return OUString(SYSSHEXEC_IMPL_NAME );
354 // -------------------------------------------------
355 // XServiceInfo
356 // -------------------------------------------------
358 sal_Bool SAL_CALL CSysShExec::supportsService( const OUString& ServiceName )
359 throw( RuntimeException )
361 Sequence < OUString > SupportedServicesNames = SysShExec_getSupportedServiceNames();
363 for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
364 if ( SupportedServicesNames[n] == ServiceName )
365 return sal_True;
367 return sal_False;
370 // -------------------------------------------------
371 // XServiceInfo
372 // -------------------------------------------------
374 Sequence< OUString > SAL_CALL CSysShExec::getSupportedServiceNames( )
375 throw( RuntimeException )
377 return SysShExec_getSupportedServiceNames();
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */