1f8e2c85aSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3f8e2c85aSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4f8e2c85aSAndrew Rist * or more contributor license agreements. See the NOTICE file 5f8e2c85aSAndrew Rist * distributed with this work for additional information 6f8e2c85aSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7f8e2c85aSAndrew Rist * to you under the Apache License, Version 2.0 (the 8f8e2c85aSAndrew Rist * "License"); you may not use this file except in compliance 9f8e2c85aSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11f8e2c85aSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13f8e2c85aSAndrew Rist * Unless required by applicable law or agreed to in writing, 14f8e2c85aSAndrew Rist * software distributed under the License is distributed on an 15f8e2c85aSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16f8e2c85aSAndrew Rist * KIND, either express or implied. See the License for the 17f8e2c85aSAndrew Rist * specific language governing permissions and limitations 18f8e2c85aSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20f8e2c85aSAndrew Rist *************************************************************/ 21f8e2c85aSAndrew Rist 22f8e2c85aSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_shell.hxx" 26cdf0e10cSrcweir #include <osl/diagnose.h> 27cdf0e10cSrcweir #include <osl/thread.h> 28cdf0e10cSrcweir #include <osl/process.h> 29cdf0e10cSrcweir #include <osl/file.hxx> 30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir #ifndef _RTL_URI_H_ 33cdf0e10cSrcweir #include <rtl/uri.hxx> 34cdf0e10cSrcweir #endif 35cdf0e10cSrcweir #include "shellexec.hxx" 36cdf0e10cSrcweir #include <com/sun/star/system/SystemShellExecuteFlags.hpp> 37cdf0e10cSrcweir 38cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp> 39cdf0e10cSrcweir #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp> 40cdf0e10cSrcweir #include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp> 41cdf0e10cSrcweir 42cdf0e10cSrcweir #include "uno/current_context.hxx" 43cdf0e10cSrcweir 44cdf0e10cSrcweir #include <string.h> 45cdf0e10cSrcweir #include <errno.h> 46cdf0e10cSrcweir #include <unistd.h> 47781fd7caSPedro Giffuni #ifdef OS2 48781fd7caSPedro Giffuni #include <process.h> 49781fd7caSPedro Giffuni #endif 50cdf0e10cSrcweir 51cdf0e10cSrcweir //------------------------------------------------------------------------ 52cdf0e10cSrcweir // namespace directives 53cdf0e10cSrcweir //------------------------------------------------------------------------ 54cdf0e10cSrcweir 55cdf0e10cSrcweir using com::sun::star::system::XSystemShellExecute; 56cdf0e10cSrcweir using com::sun::star::system::SystemShellExecuteException; 57cdf0e10cSrcweir 58cdf0e10cSrcweir using rtl::OString; 59cdf0e10cSrcweir using rtl::OUString; 60cdf0e10cSrcweir using rtl::OStringBuffer; 61cdf0e10cSrcweir using rtl::OUStringBuffer; 62cdf0e10cSrcweir using osl::FileBase; 63cdf0e10cSrcweir 64cdf0e10cSrcweir using namespace ::com::sun::star::uno; 65cdf0e10cSrcweir using namespace ::com::sun::star::lang; 66cdf0e10cSrcweir using namespace ::com::sun::star::system::SystemShellExecuteFlags; 67cdf0e10cSrcweir using namespace cppu; 68cdf0e10cSrcweir 69cdf0e10cSrcweir //------------------------------------------------------------------------ 70cdf0e10cSrcweir // defines 71cdf0e10cSrcweir //------------------------------------------------------------------------ 72cdf0e10cSrcweir 73cdf0e10cSrcweir #define SHELLEXEC_IMPL_NAME "com.sun.star.comp.system.SystemShellExecute2" 74cdf0e10cSrcweir 75cdf0e10cSrcweir //------------------------------------------------------------------------ 76cdf0e10cSrcweir // helper functions 77cdf0e10cSrcweir //------------------------------------------------------------------------ 78cdf0e10cSrcweir 79cdf0e10cSrcweir namespace // private 80cdf0e10cSrcweir { 81cdf0e10cSrcweir Sequence< OUString > SAL_CALL ShellExec_getSupportedServiceNames() 82cdf0e10cSrcweir { 83cdf0e10cSrcweir Sequence< OUString > aRet(1); 84cdf0e10cSrcweir aRet[0] = OUString::createFromAscii("com.sun.star.sys.shell.SystemShellExecute"); 85cdf0e10cSrcweir return aRet; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir void escapeForShell( rtl::OStringBuffer & rBuffer, const rtl::OString & rURL) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir sal_Int32 nmax = rURL.getLength(); 92cdf0e10cSrcweir for(sal_Int32 n=0; n < nmax; ++n) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir // escape every non alpha numeric characters (excluding a few "known good") by prepending a '\' 95cdf0e10cSrcweir sal_Char c = rURL[n]; 96cdf0e10cSrcweir #ifndef OS2 // YD shell does not support escaped chars 97cdf0e10cSrcweir if( ( c < 'A' || c > 'Z' ) && ( c < 'a' || c > 'z' ) && ( c < '0' || c > '9' ) && c != '/' && c != '.' ) 98cdf0e10cSrcweir rBuffer.append( '\\' ); 99cdf0e10cSrcweir #endif 100cdf0e10cSrcweir 101cdf0e10cSrcweir rBuffer.append( c ); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir } 104cdf0e10cSrcweir 105cdf0e10cSrcweir //----------------------------------------------------------------------------------------- 106cdf0e10cSrcweir // 107cdf0e10cSrcweir //----------------------------------------------------------------------------------------- 108cdf0e10cSrcweir 109cdf0e10cSrcweir ShellExec::ShellExec( const Reference< XComponentContext >& xContext ) : 110cdf0e10cSrcweir WeakImplHelper2< XSystemShellExecute, XServiceInfo >(), 111cdf0e10cSrcweir m_xContext(xContext) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir try { 114cdf0e10cSrcweir Reference< XCurrentContext > xCurrentContext(getCurrentContext()); 115cdf0e10cSrcweir 116cdf0e10cSrcweir if (xCurrentContext.is()) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir Any aValue = xCurrentContext->getValueByName( 119cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "system.desktop-environment" ) ) ); 120cdf0e10cSrcweir 121cdf0e10cSrcweir OUString aDesktopEnvironment; 122cdf0e10cSrcweir if (aValue >>= aDesktopEnvironment) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir m_aDesktopEnvironment = OUStringToOString(aDesktopEnvironment, RTL_TEXTENCODING_ASCII_US); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir } catch (RuntimeException e) { 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131cdf0e10cSrcweir //------------------------------------------------- 132cdf0e10cSrcweir // 133cdf0e10cSrcweir //------------------------------------------------- 134cdf0e10cSrcweir 135cdf0e10cSrcweir void SAL_CALL ShellExec::execute( const OUString& aCommand, const OUString& aParameter, sal_Int32 nFlags ) 136cdf0e10cSrcweir throw (IllegalArgumentException, SystemShellExecuteException, RuntimeException) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir OStringBuffer aBuffer, aLaunchBuffer; 139cdf0e10cSrcweir 140cdf0e10cSrcweir // DESKTOP_LAUNCH, see http://freedesktop.org/pipermail/xdg/2004-August/004489.html 141cdf0e10cSrcweir static const char *pDesktopLaunch = getenv( "DESKTOP_LAUNCH" ); 142cdf0e10cSrcweir 143*74cbd1f1SMatthias Seidel // Check whether aCommand contains a document url or not 144cdf0e10cSrcweir sal_Int32 nIndex = aCommand.indexOf( OUString( RTL_CONSTASCII_USTRINGPARAM(":/") ) ); 145cdf0e10cSrcweir 146cdf0e10cSrcweir if( nIndex > 0 || 0 == aCommand.compareToAscii("mailto:", 7) ) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir // It seems to be a url .. 149cdf0e10cSrcweir // We need to re-encode file urls because osl_getFileURLFromSystemPath converts 150cdf0e10cSrcweir // to UTF-8 before encoding non ascii characters, which is not what other apps 151cdf0e10cSrcweir // expect. 152cdf0e10cSrcweir OUString aURL( 153cdf0e10cSrcweir com::sun::star::uri::ExternalUriReferenceTranslator::create( 154cdf0e10cSrcweir m_xContext)->translateToExternal(aCommand)); 155cdf0e10cSrcweir if ( aURL.getLength() == 0 && aCommand.getLength() != 0 ) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir throw RuntimeException( 158cdf0e10cSrcweir (OUString( 159cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 160cdf0e10cSrcweir "Cannot translate URI reference to external format: ")) 161cdf0e10cSrcweir + aCommand), 162cdf0e10cSrcweir static_cast< cppu::OWeakObject * >(this)); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir #ifdef MACOSX 166cdf0e10cSrcweir aBuffer.append("open"); 167cdf0e10cSrcweir #else 168cdf0e10cSrcweir // The url launchers are expected to be in the $OOO_BASE_DIR/program 169cdf0e10cSrcweir // directory: 170cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::util::XMacroExpander > 171cdf0e10cSrcweir exp; 172cdf0e10cSrcweir if (!(m_xContext->getValueByName( 173cdf0e10cSrcweir rtl::OUString( 174cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 175cdf0e10cSrcweir "/singletons/com.sun.star.util.theMacroExpander"))) 176cdf0e10cSrcweir >>= exp) 177cdf0e10cSrcweir || !exp.is()) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir throw SystemShellExecuteException( 180cdf0e10cSrcweir rtl::OUString( 181cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 182cdf0e10cSrcweir "component context fails to supply singleton" 183cdf0e10cSrcweir " com.sun.star.util.theMacroExpander of type" 184cdf0e10cSrcweir " com.sun.star.util.XMacroExpander")), 185cdf0e10cSrcweir static_cast< XSystemShellExecute * >(this), ENOENT); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir OUString aProgramURL; 188cdf0e10cSrcweir try { 189cdf0e10cSrcweir aProgramURL = exp->expandMacros( 190cdf0e10cSrcweir rtl::OUString( 191cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/program/"))); 192cdf0e10cSrcweir } catch (com::sun::star::lang::IllegalArgumentException &) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir throw SystemShellExecuteException( 195cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM("Could not expand $OOO_BASE_DIR path")), 196cdf0e10cSrcweir static_cast < XSystemShellExecute * > (this), ENOENT ); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir OUString aProgram; 200cdf0e10cSrcweir if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram)) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir throw SystemShellExecuteException( 203cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM("Cound not convert executable path")), 204cdf0e10cSrcweir static_cast < XSystemShellExecute * > (this), ENOENT ); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir #ifdef OS2 208cdf0e10cSrcweir OStringBuffer aProg = OUStringToOString(aProgram, osl_getThreadTextEncoding()); 209cdf0e10cSrcweir aProg.append("open-url.exe"); 210cdf0e10cSrcweir OString aUrl = OUStringToOString(aURL, osl_getThreadTextEncoding()); 211cdf0e10cSrcweir if ( -1 == spawnl(P_NOWAIT, aProg.getStr(), aProg.getStr(), aUrl.getStr() , NULL) ) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir int nerr = errno; 214cdf0e10cSrcweir throw SystemShellExecuteException(OUString::createFromAscii( strerror( nerr ) ), 215cdf0e10cSrcweir static_cast < XSystemShellExecute * > (this), nerr ); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir return; 218cdf0e10cSrcweir #endif 219cdf0e10cSrcweir 220cdf0e10cSrcweir OString aTmp = OUStringToOString(aProgram, osl_getThreadTextEncoding()); 221cdf0e10cSrcweir escapeForShell(aBuffer, aTmp); 222cdf0e10cSrcweir 223cdf0e10cSrcweir #ifdef SOLARIS 224cdf0e10cSrcweir if ( m_aDesktopEnvironment.getLength() == 0 ) 225cdf0e10cSrcweir m_aDesktopEnvironment = OString("GNOME"); 226cdf0e10cSrcweir #endif 227cdf0e10cSrcweir 228cdf0e10cSrcweir // Respect the desktop environment - if there is an executable named 229cdf0e10cSrcweir // <desktop-environement-is>-open-url, pass the url to this one instead 230cdf0e10cSrcweir // of the default "open-url" script. 231cdf0e10cSrcweir if ( m_aDesktopEnvironment.getLength() > 0 ) 232cdf0e10cSrcweir { 233cdf0e10cSrcweir OString aDesktopEnvironment(m_aDesktopEnvironment.toAsciiLowerCase()); 234cdf0e10cSrcweir OStringBuffer aCopy(aTmp); 235cdf0e10cSrcweir 236cdf0e10cSrcweir aCopy.append(aDesktopEnvironment); 237cdf0e10cSrcweir aCopy.append("-open-url"); 238cdf0e10cSrcweir 239cdf0e10cSrcweir if ( 0 == access( aCopy.getStr(), X_OK) ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir aBuffer.append(aDesktopEnvironment); 242cdf0e10cSrcweir aBuffer.append("-"); 243cdf0e10cSrcweir 244cdf0e10cSrcweir /* CDE requires file urls to be decoded */ 245cdf0e10cSrcweir if ( m_aDesktopEnvironment.equals("CDE") && 0 == aURL.compareToAscii("file://", 7) ) 246cdf0e10cSrcweir { 247cdf0e10cSrcweir aURL = rtl::Uri::decode(aURL, rtl_UriDecodeWithCharset, osl_getThreadTextEncoding()); 248cdf0e10cSrcweir } 249cdf0e10cSrcweir } 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252cdf0e10cSrcweir aBuffer.append("open-url"); 253cdf0e10cSrcweir #endif 254cdf0e10cSrcweir aBuffer.append(" "); 255cdf0e10cSrcweir escapeForShell(aBuffer, OUStringToOString(aURL, osl_getThreadTextEncoding())); 256cdf0e10cSrcweir 257cdf0e10cSrcweir if ( pDesktopLaunch && *pDesktopLaunch ) 258cdf0e10cSrcweir { 259cdf0e10cSrcweir aLaunchBuffer.append( pDesktopLaunch ); 260cdf0e10cSrcweir aLaunchBuffer.append(" "); 261cdf0e10cSrcweir escapeForShell(aLaunchBuffer, OUStringToOString(aURL, osl_getThreadTextEncoding())); 262cdf0e10cSrcweir } 263cdf0e10cSrcweir } else { 264cdf0e10cSrcweir escapeForShell(aBuffer, OUStringToOString(aCommand, osl_getThreadTextEncoding())); 265cdf0e10cSrcweir aBuffer.append(" "); 266cdf0e10cSrcweir if( nFlags != 42 ) 267cdf0e10cSrcweir escapeForShell(aBuffer, OUStringToOString(aParameter, osl_getThreadTextEncoding())); 268cdf0e10cSrcweir else 269cdf0e10cSrcweir aBuffer.append(OUStringToOString(aParameter, osl_getThreadTextEncoding())); 270cdf0e10cSrcweir } 271cdf0e10cSrcweir 272cdf0e10cSrcweir // Prefer DESKTOP_LAUNCH when available 273cdf0e10cSrcweir if ( aLaunchBuffer.getLength() > 0 ) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir FILE *pLaunch = popen( aLaunchBuffer.makeStringAndClear().getStr(), "w" ); 276cdf0e10cSrcweir if ( pLaunch != NULL ) 277cdf0e10cSrcweir { 278cdf0e10cSrcweir if ( 0 == pclose( pLaunch ) ) 279cdf0e10cSrcweir return; 280cdf0e10cSrcweir } 281cdf0e10cSrcweir // Failed, do not try DESKTOP_LAUNCH any more 282cdf0e10cSrcweir pDesktopLaunch = NULL; 283cdf0e10cSrcweir } 284cdf0e10cSrcweir 285cdf0e10cSrcweir OString cmd = aBuffer.makeStringAndClear(); 286cdf0e10cSrcweir if ( 0 != pclose(popen(cmd.getStr(), "w")) ) 287cdf0e10cSrcweir { 288cdf0e10cSrcweir int nerr = errno; 289cdf0e10cSrcweir throw SystemShellExecuteException(OUString::createFromAscii( strerror( nerr ) ), 290cdf0e10cSrcweir static_cast < XSystemShellExecute * > (this), nerr ); 291cdf0e10cSrcweir } 292cdf0e10cSrcweir } 293cdf0e10cSrcweir 294cdf0e10cSrcweir 295cdf0e10cSrcweir // ------------------------------------------------- 296cdf0e10cSrcweir // XServiceInfo 297cdf0e10cSrcweir // ------------------------------------------------- 298cdf0e10cSrcweir 299cdf0e10cSrcweir OUString SAL_CALL ShellExec::getImplementationName( ) 300cdf0e10cSrcweir throw( RuntimeException ) 301cdf0e10cSrcweir { 302cdf0e10cSrcweir return OUString::createFromAscii( SHELLEXEC_IMPL_NAME ); 303cdf0e10cSrcweir } 304cdf0e10cSrcweir 305cdf0e10cSrcweir // ------------------------------------------------- 306cdf0e10cSrcweir // XServiceInfo 307cdf0e10cSrcweir // ------------------------------------------------- 308cdf0e10cSrcweir 309cdf0e10cSrcweir sal_Bool SAL_CALL ShellExec::supportsService( const OUString& ServiceName ) 310cdf0e10cSrcweir throw( RuntimeException ) 311cdf0e10cSrcweir { 312cdf0e10cSrcweir Sequence < OUString > SupportedServicesNames = ShellExec_getSupportedServiceNames(); 313cdf0e10cSrcweir 314cdf0e10cSrcweir for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; ) 315cdf0e10cSrcweir if (SupportedServicesNames[n].compareTo(ServiceName) == 0) 316cdf0e10cSrcweir return sal_True; 317cdf0e10cSrcweir 318cdf0e10cSrcweir return sal_False; 319cdf0e10cSrcweir } 320cdf0e10cSrcweir 321cdf0e10cSrcweir // ------------------------------------------------- 322cdf0e10cSrcweir // XServiceInfo 323cdf0e10cSrcweir // ------------------------------------------------- 324cdf0e10cSrcweir 325cdf0e10cSrcweir Sequence< OUString > SAL_CALL ShellExec::getSupportedServiceNames( ) 326cdf0e10cSrcweir throw( RuntimeException ) 327cdf0e10cSrcweir { 328cdf0e10cSrcweir return ShellExec_getSupportedServiceNames(); 329cdf0e10cSrcweir } 330cdf0e10cSrcweir 331