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