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