1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <osl/diagnose.h>
23 #include "SysShExec.hxx"
24 #include <osl/file.hxx>
25 #include <sal/macros.h>
27 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
28 #include <com/sun/star/uri/UriReferenceFactory.hpp>
29 #include <cppuhelper/supportsservice.hxx>
31 #define WIN32_LEAN_AND_MEAN
33 #pragma warning(push, 1)
43 // namespace directives
46 using com::sun::star::uno::Reference
;
47 using com::sun::star::uno::RuntimeException
;
48 using com::sun::star::uno::Sequence
;
49 using com::sun::star::uno::XInterface
;
50 using com::sun::star::lang::EventObject
;
51 using com::sun::star::lang::XServiceInfo
;
52 using com::sun::star::lang::IllegalArgumentException
;
54 using com::sun::star::system::XSystemShellExecute
;
55 using com::sun::star::system::SystemShellExecuteException
;
57 using namespace ::com::sun::star::system::SystemShellExecuteFlags
;
60 #define SYSSHEXEC_IMPL_NAME "com.sun.star.sys.shell.SystemShellExecute"
68 Sequence
< OUString
> SAL_CALL
SysShExec_getSupportedServiceNames()
70 Sequence
< OUString
> aRet(1);
71 aRet
[0] = "com.sun.star.system.SystemShellExecute";
75 /* This is the error table that defines the mapping between OS error
76 codes and errno values */
79 unsigned long oscode
; /* OS return value */
80 int errnocode
; /* System V error code */
83 struct errentry errtable
[] = {
84 { ERROR_SUCCESS
, osl_File_E_None
}, /* 0 */
85 { ERROR_INVALID_FUNCTION
, osl_File_E_INVAL
}, /* 1 */
86 { ERROR_FILE_NOT_FOUND
, osl_File_E_NOENT
}, /* 2 */
87 { ERROR_PATH_NOT_FOUND
, osl_File_E_NOENT
}, /* 3 */
88 { ERROR_TOO_MANY_OPEN_FILES
, osl_File_E_MFILE
}, /* 4 */
89 { ERROR_ACCESS_DENIED
, osl_File_E_ACCES
}, /* 5 */
90 { ERROR_INVALID_HANDLE
, osl_File_E_BADF
}, /* 6 */
91 { ERROR_ARENA_TRASHED
, osl_File_E_NOMEM
}, /* 7 */
92 { ERROR_NOT_ENOUGH_MEMORY
, osl_File_E_NOMEM
}, /* 8 */
93 { ERROR_INVALID_BLOCK
, osl_File_E_NOMEM
}, /* 9 */
94 { ERROR_BAD_ENVIRONMENT
, osl_File_E_2BIG
}, /* 10 */
95 { ERROR_BAD_FORMAT
, osl_File_E_NOEXEC
}, /* 11 */
96 { ERROR_INVALID_ACCESS
, osl_File_E_INVAL
}, /* 12 */
97 { ERROR_INVALID_DATA
, osl_File_E_INVAL
}, /* 13 */
98 { ERROR_INVALID_DRIVE
, osl_File_E_NOENT
}, /* 15 */
99 { ERROR_CURRENT_DIRECTORY
, osl_File_E_ACCES
}, /* 16 */
100 { ERROR_NOT_SAME_DEVICE
, osl_File_E_XDEV
}, /* 17 */
101 { ERROR_NO_MORE_FILES
, osl_File_E_NOENT
}, /* 18 */
102 { ERROR_LOCK_VIOLATION
, osl_File_E_ACCES
}, /* 33 */
103 { ERROR_BAD_NETPATH
, osl_File_E_NOENT
}, /* 53 */
104 { ERROR_NETWORK_ACCESS_DENIED
, osl_File_E_ACCES
}, /* 65 */
105 { ERROR_BAD_NET_NAME
, osl_File_E_NOENT
}, /* 67 */
106 { ERROR_FILE_EXISTS
, osl_File_E_EXIST
}, /* 80 */
107 { ERROR_CANNOT_MAKE
, osl_File_E_ACCES
}, /* 82 */
108 { ERROR_FAIL_I24
, osl_File_E_ACCES
}, /* 83 */
109 { ERROR_INVALID_PARAMETER
, osl_File_E_INVAL
}, /* 87 */
110 { ERROR_NO_PROC_SLOTS
, osl_File_E_AGAIN
}, /* 89 */
111 { ERROR_DRIVE_LOCKED
, osl_File_E_ACCES
}, /* 108 */
112 { ERROR_BROKEN_PIPE
, osl_File_E_PIPE
}, /* 109 */
113 { ERROR_DISK_FULL
, osl_File_E_NOSPC
}, /* 112 */
114 { ERROR_INVALID_TARGET_HANDLE
, osl_File_E_BADF
}, /* 114 */
115 { ERROR_INVALID_HANDLE
, osl_File_E_INVAL
}, /* 124 */
116 { ERROR_WAIT_NO_CHILDREN
, osl_File_E_CHILD
}, /* 128 */
117 { ERROR_CHILD_NOT_COMPLETE
, osl_File_E_CHILD
}, /* 129 */
118 { ERROR_DIRECT_ACCESS_HANDLE
, osl_File_E_BADF
}, /* 130 */
119 { ERROR_NEGATIVE_SEEK
, osl_File_E_INVAL
}, /* 131 */
120 { ERROR_SEEK_ON_DEVICE
, osl_File_E_ACCES
}, /* 132 */
121 { ERROR_DIR_NOT_EMPTY
, osl_File_E_NOTEMPTY
}, /* 145 */
122 { ERROR_NOT_LOCKED
, osl_File_E_ACCES
}, /* 158 */
123 { ERROR_BAD_PATHNAME
, osl_File_E_NOENT
}, /* 161 */
124 { ERROR_MAX_THRDS_REACHED
, osl_File_E_AGAIN
}, /* 164 */
125 { ERROR_LOCK_FAILED
, osl_File_E_ACCES
}, /* 167 */
126 { ERROR_ALREADY_EXISTS
, osl_File_E_EXIST
}, /* 183 */
127 { ERROR_FILENAME_EXCED_RANGE
, osl_File_E_NOENT
}, /* 206 */
128 { ERROR_NESTING_NOT_ALLOWED
, osl_File_E_AGAIN
}, /* 215 */
129 { ERROR_NOT_ENOUGH_QUOTA
, osl_File_E_NOMEM
} /* 1816 */
132 /* size of the table */
133 #define ERRTABLESIZE (SAL_N_ELEMENTS(errtable))
135 /* The following two constants must be the minimum and maximum
136 values in the (contiguous) range of osl_File_E_xec Failure errors. */
137 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
138 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
140 /* These are the low and high value in the range of errors that are
142 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
143 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
146 /*******************************************************************************/
148 oslFileError
_mapError( DWORD dwError
)
152 /* check the table for the OS error code */
153 for ( i
= 0; i
< ERRTABLESIZE
; ++i
)
155 if ( dwError
== errtable
[i
].oscode
)
156 return (oslFileError
)errtable
[i
].errnocode
;
159 /* The error code wasn't in the table. We check for a range of */
160 /* osl_File_E_ACCES errors or exec failure errors (ENOEXEC). Otherwise */
161 /* osl_File_E_INVAL is returned. */
163 if ( dwError
>= MIN_EACCES_RANGE
&& dwError
<= MAX_EACCES_RANGE
)
164 return osl_File_E_ACCES
;
165 else if ( dwError
>= MIN_EXEC_ERROR
&& dwError
<= MAX_EXEC_ERROR
)
166 return osl_File_E_NOEXEC
;
168 return osl_File_E_INVAL
;
171 #define MapError( oserror ) _mapError( oserror )
173 #define E_UNKNOWN_EXEC_ERROR -1
177 bool is_system_path(const OUString
& path_or_uri
)
180 osl::FileBase::RC rc
= osl::FileBase::getFileURLFromSystemPath(path_or_uri
, url
);
181 return (rc
== osl::FileBase::E_None
);
185 // trying to identify a jump mark
188 const OUString
JUMP_MARK_HTM(".htm#");
189 const OUString
JUMP_MARK_HTML(".html#");
190 const sal_Unicode HASH_MARK
= (sal_Unicode
)'#';
192 bool has_jump_mark(const OUString
& system_path
, sal_Int32
* jmp_mark_start
= NULL
)
194 sal_Int32 jmp_mark
= std::max
<int>(
195 system_path
.lastIndexOf(JUMP_MARK_HTM
),
196 system_path
.lastIndexOf(JUMP_MARK_HTML
));
199 *jmp_mark_start
= jmp_mark
;
201 return (jmp_mark
> -1);
206 bool is_existing_file(const OUString
& file_name
)
208 OSL_ASSERT(is_system_path(file_name
));
213 osl::FileBase::RC rc
= osl::FileBase::getFileURLFromSystemPath(file_name
, file_url
);
215 if (osl::FileBase::E_None
== rc
)
217 osl::DirectoryItem dir_item
;
218 rc
= osl::DirectoryItem::get(file_url
, dir_item
);
219 exist
= (osl::FileBase::E_None
== rc
);
225 // Jump marks in file urls are illegal.
228 void remove_jump_mark(OUString
* p_command
)
230 OSL_PRECOND(p_command
, "invalid parameter");
233 if (has_jump_mark(*p_command
, &pos
))
235 const sal_Unicode
* p_jmp_mark
= p_command
->getStr() + pos
;
236 while (*p_jmp_mark
&& (*p_jmp_mark
!= HASH_MARK
))
239 *p_command
= OUString(p_command
->getStr(), p_jmp_mark
- p_command
->getStr());
247 CSysShExec::CSysShExec( const Reference
< css::uno::XComponentContext
>& xContext
) :
248 WeakComponentImplHelper2
< XSystemShellExecute
, XServiceInfo
>( m_aMutex
),
252 * As this service is declared thread-affine, it is ensured to be called from a
253 * dedicated thread, so initialize COM here.
255 * We need COM to be initialized for STA, but osl thread get initialized for MTA.
256 * Once this changed, we can remove the uninitialize call.
259 CoInitialize( NULL
);
264 void SAL_CALL
CSysShExec::execute( const OUString
& aCommand
, const OUString
& aParameter
, sal_Int32 nFlags
)
265 throw (IllegalArgumentException
, SystemShellExecuteException
, RuntimeException
)
267 // parameter checking
268 if (0 == aCommand
.getLength())
269 throw IllegalArgumentException(
271 static_cast< XSystemShellExecute
* >( this ),
274 if ((nFlags
& ~(NO_SYSTEM_ERROR_MESSAGE
| URIS_ONLY
)) != 0)
275 throw IllegalArgumentException(
276 "Invalid Flags specified",
277 static_cast< XSystemShellExecute
* >( this ),
280 if ((nFlags
& URIS_ONLY
) != 0)
282 css::uno::Reference
< css::uri::XUriReference
> uri(
283 css::uri::UriReferenceFactory::create(m_xContext
)->parse(aCommand
));
284 if (!(uri
.is() && uri
->isAbsolute()))
286 throw css::lang::IllegalArgumentException(
287 OUString("XSystemShellExecute.execute URIS_ONLY with"
288 " non-absolute URI reference ")
290 static_cast< cppu::OWeakObject
* >(this), 0);
294 /* #i4789#; jump mark detection on system paths
295 if the given command is a system path (not http or
296 other uri schemes) and seems to have a jump mark
297 and names no existing file (remember the jump mark
298 sign '#' is a valid file name character we remove
299 the jump mark, else ShellExecuteEx fails */
300 OUString
preprocessed_command(aCommand
);
301 if (is_system_path(preprocessed_command
))
303 if (has_jump_mark(preprocessed_command
) && !is_existing_file(preprocessed_command
))
304 remove_jump_mark(&preprocessed_command
);
306 /* Convert file uris to system paths */
309 OUString aSystemPath
;
310 if (::osl::FileBase::E_None
== ::osl::FileBase::getSystemPathFromFileURL(preprocessed_command
, aSystemPath
))
311 preprocessed_command
= aSystemPath
;
314 SHELLEXECUTEINFOW sei
;
315 ZeroMemory(&sei
, sizeof( sei
));
317 sei
.cbSize
= sizeof(sei
);
318 sei
.lpFile
= reinterpret_cast<LPCWSTR
>(preprocessed_command
.getStr());
319 sei
.lpParameters
= reinterpret_cast<LPCWSTR
>(aParameter
.getStr());
320 sei
.nShow
= SW_SHOWNORMAL
;
322 if (NO_SYSTEM_ERROR_MESSAGE
& nFlags
)
323 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
327 sal_Bool bRet
= ShellExecuteExW(&sei
) ? sal_True
: sal_False
;
329 if (!bRet
&& (nFlags
& NO_SYSTEM_ERROR_MESSAGE
))
331 // ShellExecuteEx fails to set an error code
332 // we return osl_File_E_INVAL
333 sal_Int32 psxErr
= GetLastError();
334 if (ERROR_SUCCESS
== psxErr
)
335 psxErr
= E_UNKNOWN_EXEC_ERROR
;
337 psxErr
= MapError(psxErr
);
339 throw SystemShellExecuteException(
340 "Error executing command",
341 static_cast< XSystemShellExecute
* >(this),
347 OUString SAL_CALL
CSysShExec::getImplementationName( )
348 throw( RuntimeException
)
350 return OUString(SYSSHEXEC_IMPL_NAME
);
354 sal_Bool SAL_CALL
CSysShExec::supportsService( const OUString
& ServiceName
)
355 throw( RuntimeException
)
357 return cppu::supportsService(this, ServiceName
);
361 Sequence
< OUString
> SAL_CALL
CSysShExec::getSupportedServiceNames( )
362 throw( RuntimeException
)
364 return SysShExec_getSupportedServiceNames();
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */