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