20#include <osl/thread.h>
21#include <osl/file.hxx>
22#include <rtl/strbuf.hxx>
26#include <com/sun/star/system/SystemShellExecuteException.hpp>
27#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
29#include <com/sun/star/lang/IllegalArgumentException.hpp>
30#include <com/sun/star/security/AccessControlException.hpp>
31#include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp>
32#include <com/sun/star/uri/UriReferenceFactory.hpp>
48using com::sun::star::system::XSystemShellExecute;
49using com::sun::star::system::SystemShellExecuteException;
53using namespace ::com::sun::star::system::SystemShellExecuteFlags;
59 void escapeForShell( OStringBuffer & rBuffer,
const OString & rURL)
61 sal_Int32 nmax = rURL.getLength();
62 for(sal_Int32 n=0;
n < nmax; ++
n)
66 if( ( c < 'A' || c >
'Z' ) && ( c < 'a' || c >
'z' ) && ( c < '0' || c >
'9' ) && c !=
'/' && c !=
'.' )
67 rBuffer.append(
'\\' );
80void SAL_CALL
ShellExec::execute(
const OUString& aCommand,
const OUString& aParameter, sal_Int32 nFlags )
83 OStringBuffer
aBuffer, aLaunchBuffer;
87 SAL_WARN(
"shell",
"Unusual - shell attempt to launch " <<
aCommand <<
" with params " << aParameter <<
" under lok");
92 static const char *pDesktopLaunch = getenv(
"DESKTOP_LAUNCH" );
95 css::uno::Reference< css::uri::XUriReference > uri(
97 if (uri.is() && uri->isAbsolute())
103 OUString
aURL = css::uri::ExternalUriReferenceTranslator::create(
108 "Cannot translate URI reference to external format: "
115 if (uri->getScheme().equalsIgnoreAsciiCase(
"file")) {
117 auto const e1 = osl::FileBase::getSystemPathFromFileURL(
aCommand, pathname);
118 if (e1 != osl::FileBase::E_None) {
119 throw css::lang::IllegalArgumentException(
120 (
"XSystemShellExecute.execute, getSystemPathFromFileURL <" +
aCommand
121 +
"> failed with " + OUString::number(e1)),
125 if (!pathname.convertToString(
126 &pathname8, RTL_TEXTENCODING_UTF8,
127 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
128 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
130 throw css::lang::IllegalArgumentException(
131 "XSystemShellExecute.execute, cannot convert \"" + pathname +
"\" to UTF-8", {},
135 auto const e2 = lstat(pathname8.getStr(), &st);
137 auto const e3 = errno;
138 SAL_INFO(
"shell",
"lstat(" << pathname8 <<
") failed with errno " << e3);
141 throw css::lang::IllegalArgumentException(
142 "XSystemShellExecute.execute, cannot process <" +
aCommand +
">", {}, 0);
143 }
else if (S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) {
145 }
else if ((nFlags & css::system::SystemShellExecuteFlags::URIS_ONLY) != 0
146 && (!S_ISREG(st.st_mode)
147 || (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0))
149 throw css::security::AccessControlException(
150 "XSystemShellExecute.execute, bad <" +
aCommand +
">", {}, {});
151 }
else if (pathname.endsWithIgnoreAsciiCase(
".class")
152 || pathname.endsWithIgnoreAsciiCase(
".dmg")
153 || pathname.endsWithIgnoreAsciiCase(
".fileloc")
154 || pathname.endsWithIgnoreAsciiCase(
".inetloc")
155 || pathname.endsWithIgnoreAsciiCase(
".ipa")
156 || pathname.endsWithIgnoreAsciiCase(
".jar")
157 || pathname.endsWithIgnoreAsciiCase(
".terminal"))
197 if ( pDesktopLaunch && *pDesktopLaunch )
199 aLaunchBuffer.append( pDesktopLaunch + OString::Concat(
" "));
202 }
else if ((nFlags & css::system::SystemShellExecuteFlags::URIS_ONLY) != 0)
204 throw css::lang::IllegalArgumentException(
205 "XSystemShellExecute.execute URIS_ONLY with non-absolute"
219 if ( !aLaunchBuffer.isEmpty() )
221 FILE *pLaunch = popen( aLaunchBuffer.makeStringAndClear().getStr(),
"w" );
222 if ( pLaunch !=
nullptr )
224 if ( 0 == pclose( pLaunch ) )
228 pDesktopLaunch =
nullptr;
238 FILE *pLaunch = popen(cmd.getStr(),
"w");
239 if ( pLaunch !=
nullptr )
241 if ( 0 == pclose( pLaunch ) )
246 throw SystemShellExecuteException(OUString::createFromAscii( strerror( nerr ) ),
247 static_cast < XSystemShellExecute *
> (
this), nerr );
251 css::uno::Reference< css::uri::XUriReference > uri(
253 if (!uri.is() || !uri->isAbsolute())
254 throw SystemShellExecuteException(
"Emscripten can just open absolute URIs.",
255 static_cast<XSystemShellExecute*
>(
this), 42);
256 if (!aParameter.isEmpty())
257 throw SystemShellExecuteException(
"Emscripten can't process parameters; encode in URI.",
258 static_cast<XSystemShellExecute*
>(
this), 42);
260 OUString sEscapedURI(rtl::Uri::encode(
aCommand, rtl_UriCharClassUric,
261 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8));
270 return "com.sun.star.comp.system.SystemShellExecute";
280 return {
"com.sun.star.system.SystemShellExecute" };
283extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
285 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const&)
287 return cppu::acquire(
new ShellExec(context));
Reference< XComponentContext > m_xContext
ShellExec(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual void SAL_CALL execute(const OUString &aCommand, const OUString &aParameter, sal_Int32 nFlags) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
bool parse(OUString const &uri, SourceProviderScannerData *data)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * shell_ShellExec_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void execute_browser(const char *sUrl)
Some of our templating stuff clashes with EM_ASM / MAIN_THREAD_EM_ASM:
std::unique_ptr< char[]> aBuffer