merge the formfield patch from ooo-build
[ooovba.git] / vos / source / process.cxx
blobe0908b3de1518277c537cb7cc2a2bac6585c2bc1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: process.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <cstdarg>
33 #include <vector>
34 #include <rtl/ustring.hxx>
35 #include <rtl/instance.hxx>
37 #include "vos/process.hxx"
38 #include "vos/diagnose.hxx"
39 #include <osl/file.hxx>
41 #define MAX_RESOURCES 100
42 #define MAX_ARGS 100
43 #define MAX_ENVIROMENTS 100
45 using namespace vos;
47 /////////////////////////////////////////////////////////////////////////////
48 /// Argument
50 OArgumentList::OArgumentList() :
51 n_Args(0),
52 m_aVec(0)
54 // empty
57 OArgumentList::OArgumentList( sal_uInt32 nArgs, const ::rtl::OUString* aArgument1, ... ) :
58 n_Args( nArgs )
60 m_aVec = new rtl_uString* [nArgs];
61 std::va_list pArgs;
62 sal_uInt32 i = 0;
63 const rtl::OUString* aArgument;
65 va_start ( pArgs, aArgument1 );
66 aArgument = aArgument1;
68 while( true ) {
69 m_aVec[i] = aArgument->pData;
70 rtl_uString_acquire( m_aVec[i++] );
71 if( i < n_Args )
72 aArgument = va_arg( pArgs,rtl::OUString* );
73 else
74 break;
76 va_end( pArgs );
80 OArgumentList::OArgumentList( const rtl::OUString aArgumentList[], sal_uInt32 nArgs ) :
81 n_Args( nArgs )
83 m_aVec = new rtl_uString* [n_Args];
84 for( sal_uInt32 i = 0; i < n_Args; ++ i ) {
85 m_aVec[i] = aArgumentList[i].pData;
86 rtl_uString_acquire( m_aVec[i] );
90 OArgumentList::OArgumentList( const OArgumentList& rOther ) : n_Args( rOther.n_Args )
92 m_aVec = new rtl_uString* [n_Args];
94 sal_uInt32 i;
95 for ( i = 0; i < n_Args; ++i )
97 m_aVec[i] = rOther.m_aVec[i];
98 rtl_uString_acquire( m_aVec[i] );
102 OArgumentList& OArgumentList::operator=( const OArgumentList& rOther )
104 if ( this != &rOther )
107 // delete the old one
108 sal_uInt32 i;
109 for ( i = 0; i < n_Args; ++i )
110 rtl_uString_release( m_aVec[i] );
112 delete [] m_aVec;
115 n_Args = rOther.n_Args;
116 m_aVec = new rtl_uString* [n_Args];
117 for( i = 0; i < n_Args; ++i )
119 m_aVec[i] = rOther.m_aVec[i];
120 rtl_uString_acquire( m_aVec[i] );
124 return *this;
127 OArgumentList::~OArgumentList( )
129 for( sal_uInt32 i = 0; i < n_Args; ++i ) rtl_uString_release( m_aVec[i] );
130 delete[] m_aVec;
134 ////////////////////////////////////////////////////////////////////////////////
135 /// Environment
137 OEnvironment::OEnvironment() :
138 n_Vars( 0 ),
139 m_aVec( 0 )
143 OEnvironment::OEnvironment( sal_Int32 nVars, const ::rtl::OUString* aArgument1, ... ) :
144 n_Vars( nVars )
146 m_aVec = new rtl_uString* [nVars];
147 std::va_list pArgs;
148 sal_Int32 i = 0;
149 const rtl::OUString* aArgument;
151 va_start ( pArgs, aArgument1 );
152 aArgument = aArgument1;
154 while( true ) {
155 m_aVec[i] = aArgument->pData;
156 rtl_uString_acquire( m_aVec[i++] );
157 if( i < n_Vars )
158 aArgument = va_arg( pArgs,rtl::OUString* );
159 else
160 break;
162 va_end( pArgs );
166 OEnvironment::OEnvironment( const ::rtl::OUString aVariableList[], sal_Int32 nVars ) :
167 n_Vars( nVars )
169 m_aVec = new rtl_uString* [n_Vars];
170 for( sal_Int32 i = 0; i < n_Vars; ++ i ) {
171 m_aVec[i] = aVariableList[i].pData;
172 rtl_uString_acquire( m_aVec[i] );
176 OEnvironment::OEnvironment( const OEnvironment& rOther ) : n_Vars( rOther.n_Vars )
178 m_aVec = new rtl_uString* [n_Vars];
180 sal_Int32 i;
181 for ( i = 0; i < n_Vars; ++i )
183 m_aVec[i] = rOther.m_aVec[i];
184 rtl_uString_acquire( m_aVec[i] );
188 OEnvironment& OEnvironment::operator=( const OEnvironment& rOther )
190 if ( this != &rOther )
192 sal_Int32 i;
193 for ( i = 0; i < n_Vars; ++i )
194 rtl_uString_release( m_aVec[i] );
196 delete [] m_aVec;
198 n_Vars = rOther.n_Vars;
199 m_aVec = new rtl_uString* [n_Vars];
200 for ( i = 0; i < n_Vars; ++i )
202 m_aVec[i] = rOther.m_aVec[i];
203 rtl_uString_acquire( m_aVec[i] );
207 return *this;
210 OEnvironment::~OEnvironment()
212 for( sal_Int32 i = 0; i < n_Vars; ++i ) rtl_uString_release( m_aVec[i] );
213 delete[] m_aVec;
216 /////////////////////////////////////////////////////////////////////////////
217 // Process
220 VOS_IMPLEMENT_CLASSINFO(
221 VOS_CLASSNAME(OProcess, vos),
222 VOS_NAMESPACE(OProcess, vos),
223 VOS_NAMESPACE(OObject, vos), 0);
226 OProcess::OProcess( ) :
227 m_strImageName( ),
228 m_strDirectory(),
229 m_Process(0)
234 OProcess::OProcess( const rtl::OUString& strImageName ) :
235 m_strImageName( strImageName ),
236 m_strDirectory(),
237 m_Process(0)
239 // empty
243 OProcess::OProcess(const rtl::OUString& strImageName, const rtl::OUString& strWorkingDirectory) :
244 m_strImageName( strImageName ),
245 m_strDirectory( strWorkingDirectory ),
246 m_Process(0)
248 // empty
252 OProcess::~OProcess()
254 osl_freeProcessHandle(m_Process);
257 OProcess* OProcess::getProcess(TProcessIdentifier Identifier)
259 oslProcess hProcess = osl_getProcess(Identifier);
261 if (hProcess)
263 OProcess* pProcess = new OProcess( );
265 pProcess->m_Process = hProcess;
267 return pProcess;
270 return 0;
274 OProcess::TProcessError OProcess::execute(TProcessOption Options,
275 const OArgumentList& aArgumentList,
276 const OEnvironment& aEnvironment )
278 return ((TProcessError)osl_executeProcess(m_strImageName.pData,
279 aArgumentList.m_aVec,
280 aArgumentList.n_Args,
281 Options,
283 m_strDirectory.pData,
284 aEnvironment.m_aVec,
285 aEnvironment.n_Vars,
286 &m_Process));
290 OProcess::TProcessError OProcess::execute( TProcessOption Options,
291 const OSecurity &Security,
292 const OArgumentList& aArgumentList,
293 const OEnvironment& aEnvironment )
295 return ((TProcessError)osl_executeProcess(m_strImageName.pData,
296 aArgumentList.m_aVec,
297 aArgumentList.n_Args,
298 Options,
299 Security,
300 m_strDirectory.pData,
301 aEnvironment.m_aVec,
302 aEnvironment.n_Vars,
303 &m_Process));
307 OProcess::TProcessError OProcess::terminate()
309 return (TProcessError)osl_terminateProcess(m_Process);
312 OProcess::TProcessError OProcess::getInfo(TProcessData Data, TProcessInfo* pInfo) const
314 return (TProcessError)osl_getProcessInfo(m_Process, Data, pInfo);
317 OProcess::TProcessError OProcess::getCurrentInfo(TProcessData Data, TProcessInfo* pInfo)
319 return (TProcessError)osl_getProcessInfo(0, Data, pInfo);
322 OProcess::TProcessError OProcess::join()
324 return (TProcessError)osl_joinProcess(m_Process);
329 OProcess::TProcessError OProcess::searchPath(const sal_Char* Name, sal_Char *Buffer, sal_uInt32 Max,
330 const sal_Char* Path, sal_Char Separator)
332 return (TProcessError)osl_searchPath(Name, Path, Separator, Buffer, Max);
336 /////////////////////////////////////////////////////////////////////////////
337 // StartupInfo
339 VOS_IMPLEMENT_CLASSINFO(
340 VOS_CLASSNAME(OStartupInfo, vos),
341 VOS_NAMESPACE(OStartupInfo, vos),
342 VOS_NAMESPACE(OObject, vos), 0);
344 OStartupInfo::OStartupInfo()
348 OStartupInfo::~OStartupInfo()
352 OStartupInfo::TStartupError OStartupInfo::getExecutableFile(
353 rtl::OUString& strImageName ) const
355 return (TStartupError) osl_getExecutableFile( &strImageName.pData );
359 OStartupInfo::TStartupError OStartupInfo::getCommandArg(sal_uInt32 nArg, rtl::OUString& strCommandArg)
361 return ( TStartupError ) osl_getCommandArg( nArg,&strCommandArg.pData );
364 sal_uInt32 OStartupInfo::getCommandArgCount()
366 return osl_getCommandArgCount();
369 OStartupInfo::TStartupError OStartupInfo::getEnvironment(const rtl::OUString& strVar,
370 rtl::OUString& strValue)
372 return ( TStartupError ) osl_getEnvironment( strVar.pData, &strValue.pData );
377 /////////////////////////////////////////////////////////////////////////////
379 // OExtCommandLineImpl
382 namespace vos
385 class OExtCommandLineImpl
387 void init();
389 ::std::vector< ::rtl::OUString > aExtArgVector;
390 sal_uInt32 m_nArgCount;
392 public:
394 OExtCommandLineImpl();
395 ~OExtCommandLineImpl();
397 sal_uInt32 SAL_CALL getCommandArgCount();
399 sal_Bool SAL_CALL getCommandArg(sal_uInt32 nArg, ::rtl::OUString& strCommandArg);
404 OExtCommandLineImpl::OExtCommandLineImpl()
405 : m_nArgCount(0)
407 init();
410 OExtCommandLineImpl::~OExtCommandLineImpl()
416 sal_uInt32 SAL_CALL OExtCommandLineImpl::getCommandArgCount()
418 return m_nArgCount;
422 sal_Bool SAL_CALL OExtCommandLineImpl::getCommandArg(sal_uInt32 nArg, ::rtl::OUString& strCommandArg)
424 if ( nArg >= m_nArgCount )
426 return sal_False;
429 strCommandArg = aExtArgVector[nArg];
431 return sal_True;
435 void OExtCommandLineImpl::init()
437 OStartupInfo aStartInfo;
438 sal_uInt32 nIndex=0;
439 sal_uInt32 nArgs = aStartInfo.getCommandArgCount();
441 for ( nIndex = 0 ; nIndex < nArgs ; ++nIndex )
443 ::rtl::OUString aString;
444 aStartInfo.getCommandArg(nIndex,aString);
446 if ( aString[0] == (sal_Unicode) '@' )
448 ::rtl::OUString aFileName = aString.copy(1);
449 ::osl::File aFile(aFileName);
450 ::rtl::ByteSequence aSeq;
452 ::osl::FileBase::RC aErr = aFile.open(OpenFlag_Read);
454 if ( aErr != ::osl::FileBase::E_None )
456 break;
461 aErr = aFile.readLine(aSeq);
462 if ( aSeq.getLength() != 0 )
464 ::rtl::OUString newString((sal_Char*)aSeq.getArray(), aSeq.getLength(), RTL_TEXTENCODING_ASCII_US);
465 aExtArgVector.push_back( newString );
466 m_nArgCount++;
469 while ( aErr == ::osl::FileBase::E_None && aSeq.getLength() > 0 );
471 aFile.close();
472 aFile.remove(aFileName);
474 else
476 aExtArgVector.push_back( aString );
477 m_nArgCount++;
484 /////////////////////////////////////////////////////////////////////////////
486 // OExtCommandLine
489 namespace
491 struct lclMutex : public rtl::Static< NAMESPACE_VOS(OMutex), lclMutex > {};
494 OExtCommandLineImpl* OExtCommandLine::pExtImpl=0;
497 VOS_IMPLEMENT_CLASSINFO(
498 VOS_CLASSNAME(OExtCommandLine, vos),
499 VOS_NAMESPACE(OExtCommandLine, vos),
500 VOS_NAMESPACE(OObject, vos), 0);
502 OExtCommandLine::OExtCommandLine()
504 OGuard Guard(lclMutex::get());
506 if ( pExtImpl == NULL )
508 pExtImpl = new OExtCommandLineImpl;
512 OExtCommandLine::~OExtCommandLine()
518 sal_uInt32 SAL_CALL OExtCommandLine::getCommandArgCount()
520 return pExtImpl->getCommandArgCount();
524 sal_Bool SAL_CALL OExtCommandLine::getCommandArg(sal_uInt32 nArg, ::rtl::OUString& strCommandArg)
526 return pExtImpl->getCommandArg(nArg,strCommandArg);