1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: process.cxx,v $
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 ************************************************************************/
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
43 #define MAX_ENVIROMENTS 100
47 /////////////////////////////////////////////////////////////////////////////
50 OArgumentList::OArgumentList() :
57 OArgumentList::OArgumentList( sal_uInt32 nArgs
, const ::rtl::OUString
* aArgument1
, ... ) :
60 m_aVec
= new rtl_uString
* [nArgs
];
63 const rtl::OUString
* aArgument
;
65 va_start ( pArgs
, aArgument1
);
66 aArgument
= aArgument1
;
69 m_aVec
[i
] = aArgument
->pData
;
70 rtl_uString_acquire( m_aVec
[i
++] );
72 aArgument
= va_arg( pArgs
,rtl::OUString
* );
80 OArgumentList::OArgumentList( const rtl::OUString aArgumentList
[], sal_uInt32 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
];
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
109 for ( i
= 0; i
< n_Args
; ++i
)
110 rtl_uString_release( m_aVec
[i
] );
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
] );
127 OArgumentList::~OArgumentList( )
129 for( sal_uInt32 i
= 0; i
< n_Args
; ++i
) rtl_uString_release( m_aVec
[i
] );
134 ////////////////////////////////////////////////////////////////////////////////
137 OEnvironment::OEnvironment() :
143 OEnvironment::OEnvironment( sal_Int32 nVars
, const ::rtl::OUString
* aArgument1
, ... ) :
146 m_aVec
= new rtl_uString
* [nVars
];
149 const rtl::OUString
* aArgument
;
151 va_start ( pArgs
, aArgument1
);
152 aArgument
= aArgument1
;
155 m_aVec
[i
] = aArgument
->pData
;
156 rtl_uString_acquire( m_aVec
[i
++] );
158 aArgument
= va_arg( pArgs
,rtl::OUString
* );
166 OEnvironment::OEnvironment( const ::rtl::OUString aVariableList
[], sal_Int32 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
];
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
)
193 for ( i
= 0; i
< n_Vars
; ++i
)
194 rtl_uString_release( m_aVec
[i
] );
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
] );
210 OEnvironment::~OEnvironment()
212 for( sal_Int32 i
= 0; i
< n_Vars
; ++i
) rtl_uString_release( m_aVec
[i
] );
216 /////////////////////////////////////////////////////////////////////////////
220 VOS_IMPLEMENT_CLASSINFO(
221 VOS_CLASSNAME(OProcess
, vos
),
222 VOS_NAMESPACE(OProcess
, vos
),
223 VOS_NAMESPACE(OObject
, vos
), 0);
226 OProcess::OProcess( ) :
234 OProcess::OProcess( const rtl::OUString
& strImageName
) :
235 m_strImageName( strImageName
),
243 OProcess::OProcess(const rtl::OUString
& strImageName
, const rtl::OUString
& strWorkingDirectory
) :
244 m_strImageName( strImageName
),
245 m_strDirectory( strWorkingDirectory
),
252 OProcess::~OProcess()
254 osl_freeProcessHandle(m_Process
);
257 OProcess
* OProcess::getProcess(TProcessIdentifier Identifier
)
259 oslProcess hProcess
= osl_getProcess(Identifier
);
263 OProcess
* pProcess
= new OProcess( );
265 pProcess
->m_Process
= hProcess
;
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
,
283 m_strDirectory
.pData
,
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
,
300 m_strDirectory
.pData
,
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 /////////////////////////////////////////////////////////////////////////////
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
385 class OExtCommandLineImpl
389 ::std::vector
< ::rtl::OUString
> aExtArgVector
;
390 sal_uInt32 m_nArgCount
;
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()
410 OExtCommandLineImpl::~OExtCommandLineImpl()
416 sal_uInt32 SAL_CALL
OExtCommandLineImpl::getCommandArgCount()
422 sal_Bool SAL_CALL
OExtCommandLineImpl::getCommandArg(sal_uInt32 nArg
, ::rtl::OUString
& strCommandArg
)
424 if ( nArg
>= m_nArgCount
)
429 strCommandArg
= aExtArgVector
[nArg
];
435 void OExtCommandLineImpl::init()
437 OStartupInfo aStartInfo
;
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
)
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
);
469 while ( aErr
== ::osl::FileBase::E_None
&& aSeq
.getLength() > 0 );
472 aFile
.remove(aFileName
);
476 aExtArgVector
.push_back( aString
);
484 /////////////////////////////////////////////////////////////////////////////
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
);