1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SysShExec.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
37 #include <osl/diagnose.h>
38 #include "SysShExec.hxx"
39 #include <osl/file.hxx>
41 #ifndef _COM_SUN_STAR_SYS_SHELL_SYSTEMSHELLEXECUTEFLAGS_HPP_
42 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
45 #define WIN32_LEAN_AND_MEAN
47 #pragma warning(push, 1)
56 //------------------------------------------------------------------------
57 // namespace directives
58 //------------------------------------------------------------------------
60 using com::sun::star::uno::Reference
;
61 using com::sun::star::uno::RuntimeException
;
62 using com::sun::star::uno::Sequence
;
63 using com::sun::star::uno::XInterface
;
64 using com::sun::star::lang::EventObject
;
65 using com::sun::star::lang::XServiceInfo
;
66 using com::sun::star::lang::IllegalArgumentException
;
69 using com::sun::star::system::XSystemShellExecute
;
70 using com::sun::star::system::SystemShellExecuteException
;
72 using namespace ::com::sun::star::system::SystemShellExecuteFlags
;
75 //------------------------------------------------------------------------
77 //------------------------------------------------------------------------
79 #define SYSSHEXEC_IMPL_NAME "com.sun.star.sys.shell.SystemShellExecute"
81 //------------------------------------------------------------------------
83 //------------------------------------------------------------------------
87 Sequence
< OUString
> SAL_CALL
SysShExec_getSupportedServiceNames()
89 Sequence
< OUString
> aRet(1);
90 aRet
[0] = OUString::createFromAscii("com.sun.star.sys.shell.SystemShellExecute");
94 /* This is the error table that defines the mapping between OS error
95 codes and errno values */
98 unsigned long oscode
; /* OS return value */
99 int errnocode
; /* System V error code */
102 struct errentry errtable
[] = {
103 { ERROR_SUCCESS
, osl_File_E_None
}, /* 0 */
104 { ERROR_INVALID_FUNCTION
, osl_File_E_INVAL
}, /* 1 */
105 { ERROR_FILE_NOT_FOUND
, osl_File_E_NOENT
}, /* 2 */
106 { ERROR_PATH_NOT_FOUND
, osl_File_E_NOENT
}, /* 3 */
107 { ERROR_TOO_MANY_OPEN_FILES
, osl_File_E_MFILE
}, /* 4 */
108 { ERROR_ACCESS_DENIED
, osl_File_E_ACCES
}, /* 5 */
109 { ERROR_INVALID_HANDLE
, osl_File_E_BADF
}, /* 6 */
110 { ERROR_ARENA_TRASHED
, osl_File_E_NOMEM
}, /* 7 */
111 { ERROR_NOT_ENOUGH_MEMORY
, osl_File_E_NOMEM
}, /* 8 */
112 { ERROR_INVALID_BLOCK
, osl_File_E_NOMEM
}, /* 9 */
113 { ERROR_BAD_ENVIRONMENT
, osl_File_E_2BIG
}, /* 10 */
114 { ERROR_BAD_FORMAT
, osl_File_E_NOEXEC
}, /* 11 */
115 { ERROR_INVALID_ACCESS
, osl_File_E_INVAL
}, /* 12 */
116 { ERROR_INVALID_DATA
, osl_File_E_INVAL
}, /* 13 */
117 { ERROR_INVALID_DRIVE
, osl_File_E_NOENT
}, /* 15 */
118 { ERROR_CURRENT_DIRECTORY
, osl_File_E_ACCES
}, /* 16 */
119 { ERROR_NOT_SAME_DEVICE
, osl_File_E_XDEV
}, /* 17 */
120 { ERROR_NO_MORE_FILES
, osl_File_E_NOENT
}, /* 18 */
121 { ERROR_LOCK_VIOLATION
, osl_File_E_ACCES
}, /* 33 */
122 { ERROR_BAD_NETPATH
, osl_File_E_NOENT
}, /* 53 */
123 { ERROR_NETWORK_ACCESS_DENIED
, osl_File_E_ACCES
}, /* 65 */
124 { ERROR_BAD_NET_NAME
, osl_File_E_NOENT
}, /* 67 */
125 { ERROR_FILE_EXISTS
, osl_File_E_EXIST
}, /* 80 */
126 { ERROR_CANNOT_MAKE
, osl_File_E_ACCES
}, /* 82 */
127 { ERROR_FAIL_I24
, osl_File_E_ACCES
}, /* 83 */
128 { ERROR_INVALID_PARAMETER
, osl_File_E_INVAL
}, /* 87 */
129 { ERROR_NO_PROC_SLOTS
, osl_File_E_AGAIN
}, /* 89 */
130 { ERROR_DRIVE_LOCKED
, osl_File_E_ACCES
}, /* 108 */
131 { ERROR_BROKEN_PIPE
, osl_File_E_PIPE
}, /* 109 */
132 { ERROR_DISK_FULL
, osl_File_E_NOSPC
}, /* 112 */
133 { ERROR_INVALID_TARGET_HANDLE
, osl_File_E_BADF
}, /* 114 */
134 { ERROR_INVALID_HANDLE
, osl_File_E_INVAL
}, /* 124 */
135 { ERROR_WAIT_NO_CHILDREN
, osl_File_E_CHILD
}, /* 128 */
136 { ERROR_CHILD_NOT_COMPLETE
, osl_File_E_CHILD
}, /* 129 */
137 { ERROR_DIRECT_ACCESS_HANDLE
, osl_File_E_BADF
}, /* 130 */
138 { ERROR_NEGATIVE_SEEK
, osl_File_E_INVAL
}, /* 131 */
139 { ERROR_SEEK_ON_DEVICE
, osl_File_E_ACCES
}, /* 132 */
140 { ERROR_DIR_NOT_EMPTY
, osl_File_E_NOTEMPTY
}, /* 145 */
141 { ERROR_NOT_LOCKED
, osl_File_E_ACCES
}, /* 158 */
142 { ERROR_BAD_PATHNAME
, osl_File_E_NOENT
}, /* 161 */
143 { ERROR_MAX_THRDS_REACHED
, osl_File_E_AGAIN
}, /* 164 */
144 { ERROR_LOCK_FAILED
, osl_File_E_ACCES
}, /* 167 */
145 { ERROR_ALREADY_EXISTS
, osl_File_E_EXIST
}, /* 183 */
146 { ERROR_FILENAME_EXCED_RANGE
, osl_File_E_NOENT
}, /* 206 */
147 { ERROR_NESTING_NOT_ALLOWED
, osl_File_E_AGAIN
}, /* 215 */
148 { ERROR_NOT_ENOUGH_QUOTA
, osl_File_E_NOMEM
} /* 1816 */
151 /* size of the table */
152 #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
154 /* The following two constants must be the minimum and maximum
155 values in the (contiguous) range of osl_File_E_xec Failure errors. */
156 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
157 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
159 /* These are the low and high value in the range of errors that are
161 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
162 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
165 /*******************************************************************************/
167 oslFileError
_mapError( DWORD dwError
)
171 /* check the table for the OS error code */
172 for ( i
= 0; i
< ERRTABLESIZE
; ++i
)
174 if ( dwError
== errtable
[i
].oscode
)
175 return (oslFileError
)errtable
[i
].errnocode
;
178 /* The error code wasn't in the table. We check for a range of */
179 /* osl_File_E_ACCES errors or exec failure errors (ENOEXEC). Otherwise */
180 /* osl_File_E_INVAL is returned. */
182 if ( dwError
>= MIN_EACCES_RANGE
&& dwError
<= MAX_EACCES_RANGE
)
183 return osl_File_E_ACCES
;
184 else if ( dwError
>= MIN_EXEC_ERROR
&& dwError
<= MAX_EXEC_ERROR
)
185 return osl_File_E_NOEXEC
;
187 return osl_File_E_INVAL
;
190 #define MapError( oserror ) _mapError( oserror )
192 #define E_UNKNOWN_EXEC_ERROR -1
194 //-----------------------------------------
195 //-----------------------------------------
197 bool is_system_path(const OUString
& path_or_uri
)
200 osl::FileBase::RC rc
= osl::FileBase::getFileURLFromSystemPath(path_or_uri
, url
);
201 return (rc
== osl::FileBase::E_None
);
204 //-----------------------------------------
205 // trying to identify a jump mark
206 //-----------------------------------------
208 const OUString JUMP_MARK_HTM
= OUString::createFromAscii(".htm#");
209 const OUString JUMP_MARK_HTML
= OUString::createFromAscii(".html#");
210 const sal_Unicode HASH_MARK
= (sal_Unicode
)'#';
212 bool has_jump_mark(const OUString
& system_path
, sal_Int32
* jmp_mark_start
= NULL
)
214 sal_Int32 jmp_mark
= std::max
<int>(
215 system_path
.lastIndexOf(JUMP_MARK_HTM
),
216 system_path
.lastIndexOf(JUMP_MARK_HTML
));
219 *jmp_mark_start
= jmp_mark
;
221 return (jmp_mark
> -1);
224 //-----------------------------------------
225 //-----------------------------------------
227 bool is_existing_file(const OUString
& file_name
)
229 OSL_ASSERT(is_system_path(file_name
));
234 osl::FileBase::RC rc
= osl::FileBase::getFileURLFromSystemPath(file_name
, file_url
);
236 if (osl::FileBase::E_None
== rc
)
238 osl::DirectoryItem dir_item
;
239 rc
= osl::DirectoryItem::get(file_url
, dir_item
);
240 exist
= (osl::FileBase::E_None
== rc
);
245 //-------------------------------------------------
246 // Jump marks in file urls are illegal.
247 //-------------------------------------------------
249 void remove_jump_mark(OUString
* p_command
)
251 OSL_PRECOND(p_command
, "invalid parameter");
254 if (has_jump_mark(*p_command
, &pos
))
256 const sal_Unicode
* p_jmp_mark
= p_command
->getStr() + pos
;
257 while (*p_jmp_mark
&& (*p_jmp_mark
!= HASH_MARK
))
260 *p_command
= OUString(p_command
->getStr(), p_jmp_mark
- p_command
->getStr());
266 //-----------------------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------------------
270 CSysShExec::CSysShExec( ) :
271 WeakComponentImplHelper2
< XSystemShellExecute
, XServiceInfo
>( m_aMutex
)
274 * As this service is declared thread-affine, it is ensured to be called from a
275 * dedicated thread, so initialize COM here.
277 * We need COM to be initialized for STA, but osl thread get initialized for MTA.
278 * Once this changed, we can remove the uninitialize call.
281 CoInitialize( NULL
);
284 //-------------------------------------------------
286 //-------------------------------------------------
288 void SAL_CALL
CSysShExec::execute( const OUString
& aCommand
, const OUString
& aParameter
, sal_Int32 nFlags
)
289 throw (IllegalArgumentException
, SystemShellExecuteException
, RuntimeException
)
291 // parameter checking
292 if (0 == aCommand
.getLength())
293 throw IllegalArgumentException(
294 OUString::createFromAscii( "Empty command" ),
295 static_cast< XSystemShellExecute
* >( this ),
298 if (!(nFlags
>= DEFAULTS
&& nFlags
<= NO_SYSTEM_ERROR_MESSAGE
))
299 throw IllegalArgumentException(
300 OUString::createFromAscii( "Invalid Flags specified" ),
301 static_cast< XSystemShellExecute
* >( this ),
304 /* #i4789#; jump mark detection on system paths
305 if the given command is a system path (not http or
306 other uri schemes) and seems to have a jump mark
307 and names no existing file (remeber the jump mark
308 sign '#' is a valid file name character we remove
309 the jump mark, else ShellExecuteEx fails */
310 OUString
preprocessed_command(aCommand
);
311 if (is_system_path(preprocessed_command
))
313 if (has_jump_mark(preprocessed_command
) && !is_existing_file(preprocessed_command
))
314 remove_jump_mark(&preprocessed_command
);
316 /* Convert file uris to system paths */
319 OUString aSystemPath
;
320 if (::osl::FileBase::E_None
== ::osl::FileBase::getSystemPathFromFileURL(preprocessed_command
, aSystemPath
))
321 preprocessed_command
= aSystemPath
;
324 SHELLEXECUTEINFOW sei
;
325 ZeroMemory(&sei
, sizeof( sei
));
327 sei
.cbSize
= sizeof(sei
);
328 sei
.lpFile
= reinterpret_cast<LPCWSTR
>(preprocessed_command
.getStr());
329 sei
.lpParameters
= reinterpret_cast<LPCWSTR
>(aParameter
.getStr());
330 sei
.nShow
= SW_SHOWNORMAL
;
332 if (NO_SYSTEM_ERROR_MESSAGE
& nFlags
)
333 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
337 sal_Bool bRet
= ShellExecuteExW(&sei
) ? sal_True
: sal_False
;
339 if (!bRet
&& (nFlags
& NO_SYSTEM_ERROR_MESSAGE
))
341 // ShellExecuteEx fails to set an error code
342 // we return osl_File_E_INVAL
343 sal_Int32 psxErr
= GetLastError();
344 if (ERROR_SUCCESS
== psxErr
)
345 psxErr
= E_UNKNOWN_EXEC_ERROR
;
347 psxErr
= MapError(psxErr
);
349 throw SystemShellExecuteException(
350 OUString::createFromAscii("Error executing command"),
351 static_cast< XSystemShellExecute
* >(this),
356 // -------------------------------------------------
358 // -------------------------------------------------
360 OUString SAL_CALL
CSysShExec::getImplementationName( )
361 throw( RuntimeException
)
363 return OUString::createFromAscii( SYSSHEXEC_IMPL_NAME
);
366 // -------------------------------------------------
368 // -------------------------------------------------
370 sal_Bool SAL_CALL
CSysShExec::supportsService( const OUString
& ServiceName
)
371 throw( RuntimeException
)
373 Sequence
< OUString
> SupportedServicesNames
= SysShExec_getSupportedServiceNames();
375 for ( sal_Int32 n
= SupportedServicesNames
.getLength(); n
--; )
376 if (SupportedServicesNames
[n
].compareTo(ServiceName
) == 0)
382 // -------------------------------------------------
384 // -------------------------------------------------
386 Sequence
< OUString
> SAL_CALL
CSysShExec::getSupportedServiceNames( )
387 throw( RuntimeException
)
389 return SysShExec_getSupportedServiceNames();