xref: /AOO41X/main/soldep/bootstrp/command.cxx (revision d9e04f7d457e8d4292f3a8dc854ee7656a676fbb)
1*d9e04f7dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d9e04f7dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d9e04f7dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d9e04f7dSAndrew Rist  * distributed with this work for additional information
6*d9e04f7dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d9e04f7dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d9e04f7dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d9e04f7dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d9e04f7dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d9e04f7dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d9e04f7dSAndrew Rist  * software distributed under the License is distributed on an
15*d9e04f7dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d9e04f7dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d9e04f7dSAndrew Rist  * specific language governing permissions and limitations
18*d9e04f7dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d9e04f7dSAndrew Rist  *************************************************************/
21*d9e04f7dSAndrew Rist 
22*d9e04f7dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifdef SCO
25cdf0e10cSrcweir #define _IOSTREAM_H
26cdf0e10cSrcweir #endif
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <tools/fsys.hxx>
29cdf0e10cSrcweir #include <tools/stream.hxx>
30cdf0e10cSrcweir #include "soldep/command.hxx"
31cdf0e10cSrcweir #include <tools/debug.hxx>
32cdf0e10cSrcweir #include <soldep/appdef.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #ifdef _MSC_VER
35cdf0e10cSrcweir #pragma warning (push,1)
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <iostream>
39cdf0e10cSrcweir #include <string.h>
40cdf0e10cSrcweir #include <stdlib.h>
41cdf0e10cSrcweir #include <stdio.h>
42cdf0e10cSrcweir #include <ctype.h>
43cdf0e10cSrcweir #include <errno.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #ifdef _MSC_VER
46cdf0e10cSrcweir #pragma warning (pop)
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //#define MH_TEST2	1			// fuers direkte Testen
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
52cdf0e10cSrcweir #ifdef _MSC_VER
53cdf0e10cSrcweir #pragma warning (push,1)
54cdf0e10cSrcweir #endif
55cdf0e10cSrcweir #include <process.h>    // for _SPAWN
56cdf0e10cSrcweir #ifdef _MSC_VER
57cdf0e10cSrcweir #pragma warning (pop)
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir #endif
60cdf0e10cSrcweir #ifdef UNX
61cdf0e10cSrcweir #include <sys/types.h>
62cdf0e10cSrcweir #include <unistd.h>
63cdf0e10cSrcweir #if ( defined NETBSD ) || defined (FREEBSD) || defined (AIX) \
64cdf0e10cSrcweir     || defined (HPUX) || defined (MACOSX)
65cdf0e10cSrcweir #include <sys/wait.h>
66cdf0e10cSrcweir #else
67cdf0e10cSrcweir #include <wait.h>
68cdf0e10cSrcweir #endif
69cdf0e10cSrcweir #define P_WAIT 1 		// erstmal einen dummz
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir 
72cdf0e10cSrcweir #if defined WNT
73cdf0e10cSrcweir #include <tools/svwin.h>
74cdf0e10cSrcweir #endif
75cdf0e10cSrcweir 
76cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
77cdf0e10cSrcweir #define  	cPathSeperator ';'
78cdf0e10cSrcweir #endif
79cdf0e10cSrcweir #ifdef UNX
80cdf0e10cSrcweir #define	 	cPathSeperator	':'
81cdf0e10cSrcweir #endif
82cdf0e10cSrcweir 
83cdf0e10cSrcweir /*****************************************************************************/
CommandLine(sal_Bool bWrite)84cdf0e10cSrcweir CommandLine::CommandLine(sal_Bool bWrite)
85cdf0e10cSrcweir /*****************************************************************************/
86cdf0e10cSrcweir 				: bTmpWrite(bWrite)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	CommandBuffer = new char [1];
89cdf0e10cSrcweir 	if (CommandBuffer == NULL) {
90cdf0e10cSrcweir 		//cout << "Error: nospace" << endl;
91cdf0e10cSrcweir 		exit(0);
92cdf0e10cSrcweir 	}
93cdf0e10cSrcweir 	CommandBuffer[0] = '\0';
94cdf0e10cSrcweir 	nArgc = 0;
95cdf0e10cSrcweir 	ppArgv = new char * [1];
96cdf0e10cSrcweir 	ppArgv[0] = NULL;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	ComShell = new char [128];
99cdf0e10cSrcweir 	char* pTemp = getenv("COMMAND_SHELL");
100cdf0e10cSrcweir 	if(!pTemp)
101cdf0e10cSrcweir 		strcpy(ComShell,COMMAND_SHELL);
102cdf0e10cSrcweir 	else
103cdf0e10cSrcweir 		strcpy(ComShell,pTemp);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	strcpy(&ComShell[strlen(ComShell)]," -C ");
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir /*****************************************************************************/
CommandLine(const char * CommandString,sal_Bool bWrite)109cdf0e10cSrcweir CommandLine::CommandLine(const char *CommandString, sal_Bool bWrite)
110cdf0e10cSrcweir /*****************************************************************************/
111cdf0e10cSrcweir 				: bTmpWrite(bWrite)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	CommandBuffer = new char [1];
114cdf0e10cSrcweir 	if (CommandBuffer == NULL) {
115cdf0e10cSrcweir 		//cout << "Error: nospace" << endl;
116cdf0e10cSrcweir 		exit(0);
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir 	nArgc = 0;
119cdf0e10cSrcweir 	ppArgv = new char * [1];
120cdf0e10cSrcweir 	ppArgv[0] = NULL;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	ComShell = new char [128];
123cdf0e10cSrcweir 	char* pTemp = getenv("COMMAND_SHELL");
124cdf0e10cSrcweir 	if(!pTemp)
125cdf0e10cSrcweir 		strcpy(ComShell,COMMAND_SHELL);
126cdf0e10cSrcweir 	else
127cdf0e10cSrcweir 		strcpy(ComShell,pTemp);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	strcpy(&ComShell[strlen(ComShell)]," -C ");
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	BuildCommand(CommandString);
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir /*****************************************************************************/
CommandLine(const CommandLine & CCommandLine,sal_Bool bWrite)135cdf0e10cSrcweir CommandLine::CommandLine(const CommandLine& CCommandLine, sal_Bool bWrite)
136cdf0e10cSrcweir /*****************************************************************************/
137cdf0e10cSrcweir 				: bTmpWrite(bWrite)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	CommandBuffer = new char [1];
140cdf0e10cSrcweir 	if (CommandBuffer == NULL) {
141cdf0e10cSrcweir 		//cout << "Error: nospace" << endl;
142cdf0e10cSrcweir 		exit(0);
143cdf0e10cSrcweir 	}
144cdf0e10cSrcweir 	nArgc = 0;
145cdf0e10cSrcweir 	ppArgv = new char * [1];
146cdf0e10cSrcweir 	ppArgv[0] = NULL;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	ComShell = new char [128];
149cdf0e10cSrcweir 	char* pTemp = getenv("COMMAND_SHELL");
150cdf0e10cSrcweir 	if(!pTemp)
151cdf0e10cSrcweir 		strcpy(ComShell,COMMAND_SHELL);
152cdf0e10cSrcweir 	else
153cdf0e10cSrcweir 		strcpy(ComShell,pTemp);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	strcpy(&ComShell[strlen(ComShell)]," -C ");
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	BuildCommand(CCommandLine.CommandBuffer);
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir /*****************************************************************************/
~CommandLine()161cdf0e10cSrcweir CommandLine::~CommandLine()
162cdf0e10cSrcweir /*****************************************************************************/
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	delete [] CommandBuffer;
165cdf0e10cSrcweir 	delete [] ComShell;
166cdf0e10cSrcweir 	//for (int i = 0; ppArgv[i] != '\0'; i++) {
167cdf0e10cSrcweir 	for (int i = 0; ppArgv[i] != 0; i++) {
168cdf0e10cSrcweir 		delete [] ppArgv[i];
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir 	delete [] ppArgv;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir /*****************************************************************************/
operator =(const CommandLine & CCommandLine)175cdf0e10cSrcweir CommandLine& CommandLine::operator=(const CommandLine& CCommandLine)
176cdf0e10cSrcweir /*****************************************************************************/
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	strcpy (CommandBuffer, CCommandLine.CommandBuffer);
179cdf0e10cSrcweir 	for (int i = 0; i != nArgc; i++) {
180cdf0e10cSrcweir 		delete [] ppArgv[i];
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 	delete [] ppArgv;
183cdf0e10cSrcweir         ppArgv = new char * [1];
184cdf0e10cSrcweir         ppArgv[0] = NULL;
185cdf0e10cSrcweir 	BuildCommand(CommandBuffer);
186cdf0e10cSrcweir 	return *this;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir /*****************************************************************************/
operator =(const char * CommandString)190cdf0e10cSrcweir CommandLine& CommandLine::operator=(const char *CommandString)
191cdf0e10cSrcweir /*****************************************************************************/
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	strcpy (CommandBuffer, CommandString);
194cdf0e10cSrcweir 	for (int i = 0; i != nArgc; i++) {
195cdf0e10cSrcweir 		delete [] ppArgv[i];
196cdf0e10cSrcweir 	}
197cdf0e10cSrcweir 	delete [] ppArgv;
198cdf0e10cSrcweir         ppArgv = new char * [1];
199cdf0e10cSrcweir         ppArgv[0] = NULL;
200cdf0e10cSrcweir 	BuildCommand(CommandBuffer);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	return *this;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir /*****************************************************************************/
Print()206cdf0e10cSrcweir void CommandLine::Print()
207cdf0e10cSrcweir /*****************************************************************************/
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	//cout << "******* start print *******" << endl;
210cdf0e10cSrcweir 	//cout << "nArgc = " << nArgc << endl;
211cdf0e10cSrcweir 	//cout << "CommandBuffer = " << CommandBuffer << endl;
212cdf0e10cSrcweir 	for (int i = 0; ppArgv[i] != NULL; i++) {
213cdf0e10cSrcweir 		//cout << "ppArgv[" << i << "] = " << ppArgv[i] << endl;
214cdf0e10cSrcweir 	}
215cdf0e10cSrcweir 	//cout << "******** end print ********" << endl;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir /*****************************************************************************/
BuildCommand(const char * CommandString)219cdf0e10cSrcweir void CommandLine::BuildCommand(const char *CommandString)
220cdf0e10cSrcweir /*****************************************************************************/
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	int index = 0, pos=0;
223cdf0e10cSrcweir 	char buffer[1024];
224cdf0e10cSrcweir 	char WorkString[1024];
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	strcpy(WorkString,CommandString);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	//falls LogWindow -> in tmpfile schreiben
229cdf0e10cSrcweir 	if(bTmpWrite)
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		strcpy(&WorkString[strlen(WorkString)]," >&");
232cdf0e10cSrcweir 		strcpy(&WorkString[strlen(WorkString)],getenv("TMP"));
233cdf0e10cSrcweir 		strcpy(&WorkString[strlen(WorkString)],TMPNAME);
234cdf0e10cSrcweir 	}
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 	// delete old memory and get some new memory for CommandBuffer
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	delete [] CommandBuffer;
239cdf0e10cSrcweir 	CommandBuffer =  new char [strlen(ComShell)+strlen(WorkString)+1];
240cdf0e10cSrcweir 	if (CommandBuffer == NULL) {
241cdf0e10cSrcweir 		//cout << "Error: nospace" << endl;
242cdf0e10cSrcweir 		exit(0);
243cdf0e10cSrcweir 	}
244cdf0e10cSrcweir 	strcpy (CommandBuffer, ComShell);
245cdf0e10cSrcweir 	strcpy (&CommandBuffer[strlen(ComShell)], WorkString);
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 	CommandString = CommandBuffer;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	// get the number of tokens
250cdf0e10cSrcweir 	Strtokens(CommandString);
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	// delete the space for the old CommandLine
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 	for (int i = 0; ppArgv[i] != 0; i++) {
255cdf0e10cSrcweir 		delete [] ppArgv[i];
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir 	delete [] ppArgv;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	/* get space for the new command line */
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	ppArgv = (char **) new char * [nArgc+1];
262cdf0e10cSrcweir 	if (ppArgv == NULL) {
263cdf0e10cSrcweir 		//cout << "Error: no space" << endl;
264cdf0e10cSrcweir 		exit(0);
265cdf0e10cSrcweir 	}
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	// flush the white space
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	while ( isspace(*CommandString) )
270cdf0e10cSrcweir 		CommandString++;
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	index = 0;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 	// start the loop to build all the individual tokens
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	while (*CommandString != '\0') {
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 		pos = 0;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 		// copy the token until white space is found
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 		while ( !isspace(*CommandString) && *CommandString != '\0') {
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 			buffer[pos++] = *CommandString++;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 		}
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 		buffer[pos] = '\0';
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 		// get space for the individual tokens
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 		ppArgv[index] = (char *) new char [strlen(buffer)+1];
293cdf0e10cSrcweir 		if (ppArgv[index] == NULL) {
294cdf0e10cSrcweir 			//cout << "Error: nospace" << endl;
295cdf0e10cSrcweir 			exit(0);
296cdf0e10cSrcweir 		}
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 		// copy the token
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 		strcpy (ppArgv[index++], buffer);
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 		// flush while space
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 		while ( isspace(*CommandString) )
305cdf0e10cSrcweir 			CommandString++;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	}
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 	// finish by setting the las pointer to NULL
310cdf0e10cSrcweir 	ppArgv[nArgc]= NULL;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir /*****************************************************************************/
Strtokens(const char * CommandString)315cdf0e10cSrcweir void CommandLine::Strtokens(const char *CommandString)
316cdf0e10cSrcweir /*****************************************************************************/
317cdf0e10cSrcweir {
318cdf0e10cSrcweir 	int count = 0;
319cdf0e10cSrcweir 	const char *temp;
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 	temp = CommandString;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 	/* bypass white space */
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 	while (isspace(*temp)) temp++;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 	for (count=0; *temp != '\0'; count++) {
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 		/* continue until white space of string terminator is found */
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 		while ((!isspace(*temp)) && (*temp != '\0')) temp++;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 		/* bypass white space */
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 		while (isspace(*temp)) temp++;
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 	}
338cdf0e10cSrcweir 	nArgc = count;
339cdf0e10cSrcweir }
340cdf0e10cSrcweir 
341cdf0e10cSrcweir /*****************************************************************************/
CCommand(ByteString & rString)342cdf0e10cSrcweir CCommand::CCommand( ByteString &rString )
343cdf0e10cSrcweir /*****************************************************************************/
344cdf0e10cSrcweir {
345cdf0e10cSrcweir 	rString.SearchAndReplace( '\t', ' ' );
346cdf0e10cSrcweir 	aCommand = rString.GetToken( 0, ' ' );
347cdf0e10cSrcweir 	aCommandLine = Search( "PATH" );
348cdf0e10cSrcweir #ifndef UNX
349cdf0e10cSrcweir 	aCommandLine += " /c ";
350cdf0e10cSrcweir #else
351cdf0e10cSrcweir 	aCommandLine += " -c ";
352cdf0e10cSrcweir #endif
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 	ByteString sCmd( rString.GetToken( 0, ' ' ));
355cdf0e10cSrcweir 	ByteString sParam( rString.Copy( sCmd.Len()));
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 	aCommandLine += Search( "PATH", sCmd );
358cdf0e10cSrcweir 	aCommandLine += sParam;
359cdf0e10cSrcweir 
360cdf0e10cSrcweir 	ImplInit();
361cdf0e10cSrcweir }
362cdf0e10cSrcweir 
363cdf0e10cSrcweir /*****************************************************************************/
CCommand(const char * pChar)364cdf0e10cSrcweir CCommand::CCommand( const char *pChar )
365cdf0e10cSrcweir /*****************************************************************************/
366cdf0e10cSrcweir {
367cdf0e10cSrcweir 	ByteString aString = pChar;
368cdf0e10cSrcweir 	aString.SearchAndReplace( '\t', ' ' );
369cdf0e10cSrcweir 	aCommand = aString.GetToken( 0, ' ' );
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 	aCommandLine = Search( "PATH" );
372cdf0e10cSrcweir #ifndef UNX
373cdf0e10cSrcweir 	aCommandLine += " /c ";
374cdf0e10cSrcweir #else
375cdf0e10cSrcweir 	aCommandLine += " -c ";
376cdf0e10cSrcweir #endif
377cdf0e10cSrcweir 	ByteString rString( pChar );
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	ByteString sCmd( rString.GetToken( 0, ' ' ));
380cdf0e10cSrcweir 	ByteString sParam( rString.Copy( sCmd.Len()));
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 	aCommandLine += Search( "PATH", sCmd );
383cdf0e10cSrcweir 	aCommandLine += sParam;
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 	ImplInit();
386cdf0e10cSrcweir }
387cdf0e10cSrcweir 
388cdf0e10cSrcweir /*****************************************************************************/
ImplInit()389cdf0e10cSrcweir void CCommand::ImplInit()
390cdf0e10cSrcweir /*****************************************************************************/
391cdf0e10cSrcweir {
392cdf0e10cSrcweir 	char pTmpStr[255];
393cdf0e10cSrcweir 	size_t *pPtr;
394cdf0e10cSrcweir 	char *pChar;
395cdf0e10cSrcweir 	int nVoid = sizeof( size_t * );
396cdf0e10cSrcweir 	nArgc = aCommandLine.GetTokenCount(' ');
397cdf0e10cSrcweir 	sal_uIntPtr nLen = aCommandLine.Len();
398cdf0e10cSrcweir 
399cdf0e10cSrcweir 	ppArgv = (char **) new char[ (sal_uIntPtr)(nLen + nVoid * (nArgc +2) + nArgc ) ];
400cdf0e10cSrcweir 	pChar = (char *) ppArgv + ( (1+nArgc) * nVoid );
401cdf0e10cSrcweir 	pPtr = (size_t *) ppArgv;
402cdf0e10cSrcweir 	for ( xub_StrLen i=0; i<nArgc; i++ )
403cdf0e10cSrcweir 	{
404cdf0e10cSrcweir 		(void) strcpy( pTmpStr, aCommandLine.GetToken(i, ' ' ).GetBuffer() );
405cdf0e10cSrcweir 		size_t nStrLen = strlen( pTmpStr ) + 1;
406cdf0e10cSrcweir 		strcpy( pChar, pTmpStr );
407cdf0e10cSrcweir 		*pPtr = (sal_uIntPtr) pChar;
408cdf0e10cSrcweir 		pChar += nStrLen;
409cdf0e10cSrcweir 		pPtr += 1;
410cdf0e10cSrcweir #ifdef UNX
411cdf0e10cSrcweir 		if ( i == 1 )
412cdf0e10cSrcweir 		{
413cdf0e10cSrcweir 			sal_uInt16 nWo = aCommandLine.Search("csh -c ");
414cdf0e10cSrcweir 			if (nWo != STRING_NOTFOUND)
415cdf0e10cSrcweir 				aCommandLine.Erase(0, nWo + 7);
416cdf0e10cSrcweir 			else
417cdf0e10cSrcweir 				aCommandLine.Erase(0, 16);
418cdf0e10cSrcweir 			i = nArgc;
419cdf0e10cSrcweir 			strcpy( pChar, aCommandLine.GetBuffer() );
420cdf0e10cSrcweir 			*pPtr = (sal_uIntPtr) pChar;
421cdf0e10cSrcweir 			pPtr += 1;
422cdf0e10cSrcweir 		}
423cdf0e10cSrcweir #endif
424cdf0e10cSrcweir 	}
425cdf0e10cSrcweir 	*pPtr = 0;
426cdf0e10cSrcweir }
427cdf0e10cSrcweir 
428cdf0e10cSrcweir /*****************************************************************************/
operator int()429cdf0e10cSrcweir CCommand::operator int()
430cdf0e10cSrcweir /*****************************************************************************/
431cdf0e10cSrcweir {
432cdf0e10cSrcweir 	int nRet;
433cdf0e10cSrcweir #if defined WNT
434cdf0e10cSrcweir 	nRet = _spawnv( P_WAIT, ppArgv[0], (const char **) ppArgv );
435cdf0e10cSrcweir #elif defined OS2
436cdf0e10cSrcweir 	nRet = _spawnv( P_WAIT, ppArgv[0], ppArgv );
437cdf0e10cSrcweir #elif defined UNX
438cdf0e10cSrcweir 	//fprintf( stderr, "CComand : operator (int) not implemented\n");
439cdf0e10cSrcweir 	// **** Unix Implementierung ***************
440cdf0e10cSrcweir 	pid_t pid;
441cdf0e10cSrcweir 
442cdf0e10cSrcweir 	if (( pid = fork()) < 0 )
443cdf0e10cSrcweir 	{
444cdf0e10cSrcweir 		DBG_ASSERT( sal_False, "fork error" );
445cdf0e10cSrcweir 	}
446cdf0e10cSrcweir 	else if ( pid == 0 )
447cdf0e10cSrcweir 	{
448cdf0e10cSrcweir 		if ( execv( ppArgv[0], (char * const *) ppArgv ) < 0 )
449cdf0e10cSrcweir 		{
450cdf0e10cSrcweir 			DBG_ASSERT( sal_False, "execv failed" );
451cdf0e10cSrcweir 		}
452cdf0e10cSrcweir 	}
453cdf0e10cSrcweir 	//fprintf( stderr, "parent: %s %s\n", ppArgv[0] , ppArgv[1] );
454cdf0e10cSrcweir 	if ( (nRet = waitpid( pid, NULL, 0 ) < 0) )
455cdf0e10cSrcweir 	{
456cdf0e10cSrcweir 		DBG_ASSERT( sal_False, "wait error" );
457cdf0e10cSrcweir 	}
458cdf0e10cSrcweir #endif
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 	switch ( errno )
461cdf0e10cSrcweir 	{
462cdf0e10cSrcweir 		case	E2BIG :
463cdf0e10cSrcweir 					nError = COMMAND_TOOBIG;
464cdf0e10cSrcweir 					break;
465cdf0e10cSrcweir 		case	EINVAL :
466cdf0e10cSrcweir 					nError = COMMAND_INVALID;
467cdf0e10cSrcweir 					break;
468cdf0e10cSrcweir 		case	ENOENT:
469cdf0e10cSrcweir 					nError = COMMAND_NOTFOUND;
470cdf0e10cSrcweir 					break;
471cdf0e10cSrcweir 		case 	ENOEXEC :
472cdf0e10cSrcweir 					nError = COMMAND_NOEXEC;
473cdf0e10cSrcweir 					break;
474cdf0e10cSrcweir 		case	ENOMEM :
475cdf0e10cSrcweir 					nError = COMMAND_NOMEM;
476cdf0e10cSrcweir 					break;
477cdf0e10cSrcweir 		default:
478cdf0e10cSrcweir 				nError = COMMAND_UNKNOWN;
479cdf0e10cSrcweir 	}
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 	if ( nRet )
482cdf0e10cSrcweir 		fprintf( stderr, "Program returned with errros\n");
483cdf0e10cSrcweir 	return nRet;
484cdf0e10cSrcweir }
485cdf0e10cSrcweir 
486cdf0e10cSrcweir /*****************************************************************************/
Search(ByteString aEnv,ByteString sItem)487cdf0e10cSrcweir ByteString CCommand::Search(ByteString aEnv, ByteString sItem)
488cdf0e10cSrcweir /*****************************************************************************/
489cdf0e10cSrcweir {
490cdf0e10cSrcweir 	// default wird eine Shell im Path gesucht,
491cdf0e10cSrcweir 	// wenn aber compsec gestzt ist holen wir uns die
492cdf0e10cSrcweir 	// Shell von dort
493cdf0e10cSrcweir 	if ( sItem.Equals( COMMAND_SHELL ))
494cdf0e10cSrcweir 	{
495cdf0e10cSrcweir 		ByteString aComspec = GetEnv( "COMSPEC" );
496cdf0e10cSrcweir 		if ( !aComspec.Equals(""))
497cdf0e10cSrcweir 			return aComspec;
498cdf0e10cSrcweir 	}
499cdf0e10cSrcweir 
500cdf0e10cSrcweir 	DirEntry aItem( String( sItem, RTL_TEXTENCODING_ASCII_US ));
501cdf0e10cSrcweir 	if ( aItem.Exists())
502cdf0e10cSrcweir 		return sItem;
503cdf0e10cSrcweir 
504cdf0e10cSrcweir 	ByteString aEntry, sReturn;
505cdf0e10cSrcweir 	ByteString sEnv( aEnv );
506cdf0e10cSrcweir 	ByteString sEnvironment = GetEnv( sEnv.GetBuffer());
507cdf0e10cSrcweir 	xub_StrLen nCount = sEnvironment.GetTokenCount( cPathSeperator );
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 	sal_Bool bFound = sal_False;
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 	for ( xub_StrLen i=0; i<nCount && !bFound; i++ )
512cdf0e10cSrcweir 	{
513cdf0e10cSrcweir 		aEntry = sEnvironment.GetToken(i, cPathSeperator );
514cdf0e10cSrcweir #ifndef UNX
515cdf0e10cSrcweir 		aEntry += '\\';
516cdf0e10cSrcweir #else
517cdf0e10cSrcweir 		aEntry += '/';
518cdf0e10cSrcweir #endif
519cdf0e10cSrcweir 		aEntry += sItem;
520cdf0e10cSrcweir 
521cdf0e10cSrcweir 		String sEntry( aEntry, RTL_TEXTENCODING_ASCII_US );
522cdf0e10cSrcweir 		DirEntry aDirEntry( sEntry );
523cdf0e10cSrcweir 		aDirEntry.ToAbs();
524cdf0e10cSrcweir 		if ( aDirEntry.Exists()) {
525cdf0e10cSrcweir 			sReturn = aEntry;
526cdf0e10cSrcweir 			bFound = sal_True;
527cdf0e10cSrcweir 		}
528cdf0e10cSrcweir 	}
529cdf0e10cSrcweir 	if ( !bFound )
530cdf0e10cSrcweir 	{
531cdf0e10cSrcweir 		sEnv = sEnv.ToUpperAscii();
532cdf0e10cSrcweir 		ByteString sEnvironment2 = GetEnv(sEnv.GetBuffer() );
533cdf0e10cSrcweir 		xub_StrLen nCount2 = sEnvironment2.GetTokenCount( cPathSeperator );
534cdf0e10cSrcweir 		for ( xub_StrLen i=0; i<nCount2 && !bFound; i++ )
535cdf0e10cSrcweir 		{
536cdf0e10cSrcweir 			aEntry = sEnvironment2.GetToken(i, cPathSeperator );
537cdf0e10cSrcweir #ifndef UNX
538cdf0e10cSrcweir 			aEntry += '\\';
539cdf0e10cSrcweir #else
540cdf0e10cSrcweir 			aEntry += '/';
541cdf0e10cSrcweir #endif
542cdf0e10cSrcweir 			aEntry += sItem;
543cdf0e10cSrcweir 
544cdf0e10cSrcweir 			String sEntry( aEntry, RTL_TEXTENCODING_ASCII_US );
545cdf0e10cSrcweir 			DirEntry aDirEntry( sEntry );
546cdf0e10cSrcweir 			aDirEntry.ToAbs();
547cdf0e10cSrcweir 			if ( aDirEntry.Exists()) {
548cdf0e10cSrcweir 				sReturn = aEntry;
549cdf0e10cSrcweir 				bFound = sal_True;
550cdf0e10cSrcweir 			}
551cdf0e10cSrcweir 		}
552cdf0e10cSrcweir 	}
553cdf0e10cSrcweir 
554cdf0e10cSrcweir 	if ( sReturn.Equals( "" ))
555cdf0e10cSrcweir 		sReturn = sItem;
556cdf0e10cSrcweir 
557cdf0e10cSrcweir 	return sReturn;
558cdf0e10cSrcweir }
559cdf0e10cSrcweir 
560cdf0e10cSrcweir /*****************************************************************************/
CCommandd(ByteString & rString,CommandBits nBits)561cdf0e10cSrcweir CCommandd::CCommandd( ByteString &rString, CommandBits nBits )
562cdf0e10cSrcweir /*****************************************************************************/
563cdf0e10cSrcweir 				: CCommand( rString ),
564cdf0e10cSrcweir 				nFlag( nBits )
565cdf0e10cSrcweir {
566cdf0e10cSrcweir }
567cdf0e10cSrcweir 
568cdf0e10cSrcweir 
569cdf0e10cSrcweir /*****************************************************************************/
CCommandd(const char * pChar,CommandBits nBits)570cdf0e10cSrcweir CCommandd::CCommandd( const char *pChar, CommandBits nBits )
571cdf0e10cSrcweir /*****************************************************************************/
572cdf0e10cSrcweir 				: CCommand( pChar ),
573cdf0e10cSrcweir 				nFlag( nBits )
574cdf0e10cSrcweir {
575cdf0e10cSrcweir }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir /*****************************************************************************/
operator int()578cdf0e10cSrcweir CCommandd::operator int()
579cdf0e10cSrcweir /*****************************************************************************/
580cdf0e10cSrcweir {
581cdf0e10cSrcweir 	int nRet = 0;
582cdf0e10cSrcweir 
583cdf0e10cSrcweir #ifdef WNT
584cdf0e10cSrcweir 	LPCTSTR lpApplicationName = NULL;
585cdf0e10cSrcweir 	LPTSTR lpCommandLine = (char *) GetCommandLine_().GetBuffer();
586cdf0e10cSrcweir 	LPSECURITY_ATTRIBUTES lpProcessAttributes = NULL;
587cdf0e10cSrcweir 	LPSECURITY_ATTRIBUTES lpThreadAttributes = NULL;
588cdf0e10cSrcweir 	sal_Bool bInheritHandles = sal_True;
589cdf0e10cSrcweir 
590cdf0e10cSrcweir 	// wie wuenschen wir denn gestartet zu werden ??
591cdf0e10cSrcweir 	DWORD dwCreationFlags;
592cdf0e10cSrcweir 
593cdf0e10cSrcweir 	if ( nFlag & COMMAND_EXECUTE_START )
594cdf0e10cSrcweir 		dwCreationFlags = DETACHED_PROCESS;
595cdf0e10cSrcweir 	else
596cdf0e10cSrcweir 		dwCreationFlags = CREATE_NEW_CONSOLE;
597cdf0e10cSrcweir 
598cdf0e10cSrcweir 	// wir erben vom Vaterprozess
599cdf0e10cSrcweir 	LPVOID lpEnvironment = NULL;
600cdf0e10cSrcweir 
601cdf0e10cSrcweir 	// das exe im Pfad suchen
602cdf0e10cSrcweir 	LPCTSTR lpCurrentDirectory = NULL;
603cdf0e10cSrcweir 
604cdf0e10cSrcweir 	// in dieser Struktur bekommen wir die erzeugte Processinfo
605cdf0e10cSrcweir 	// zurueck
606cdf0e10cSrcweir 	PROCESS_INFORMATION aProcessInformation;
607cdf0e10cSrcweir 
608cdf0e10cSrcweir 	// weiteres Startupinfo anlegen
609cdf0e10cSrcweir 	STARTUPINFO aStartupInfo;
610cdf0e10cSrcweir 
611cdf0e10cSrcweir 	aStartupInfo.cb = sizeof( STARTUPINFO );
612cdf0e10cSrcweir 	aStartupInfo.lpReserved = NULL;
613cdf0e10cSrcweir 	aStartupInfo.lpDesktop = NULL;
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 	// das Fenster bekommt den Namen des Exes
616cdf0e10cSrcweir 	aStartupInfo.lpTitle = NULL;
617cdf0e10cSrcweir 	aStartupInfo.dwX = 100;
618cdf0e10cSrcweir 	aStartupInfo.dwY = 100;
619cdf0e10cSrcweir 	//aStartupInfo.dwXSize = 400;
620cdf0e10cSrcweir 	//aStartupInfo.dwYSize = 400;
621cdf0e10cSrcweir 	aStartupInfo.dwXCountChars = 40;
622cdf0e10cSrcweir 	aStartupInfo.dwYCountChars = 40;
623cdf0e10cSrcweir 
624cdf0e10cSrcweir 	// Farben setzen
625cdf0e10cSrcweir 	aStartupInfo.dwFillAttribute = FOREGROUND_RED | BACKGROUND_RED |
626cdf0e10cSrcweir 								BACKGROUND_BLUE | BACKGROUND_GREEN;
627cdf0e10cSrcweir 
628cdf0e10cSrcweir //	aStartupInfo.dwFlags = STARTF_USESTDHANDLES;
629cdf0e10cSrcweir 	//aStartupInfo.wShowWindow = SW_NORMAL; //SW_SHOWDEFAULT;
630cdf0e10cSrcweir 	//aStartupInfo.wShowWindow = SW_HIDE; //SW_SHOWNOACTIVATE;
631cdf0e10cSrcweir 	aStartupInfo.wShowWindow = SW_SHOWNOACTIVATE;
632cdf0e10cSrcweir 	aStartupInfo.cbReserved2 = NULL;
633cdf0e10cSrcweir 	aStartupInfo.lpReserved2 = NULL;
634cdf0e10cSrcweir 	//aStartupInfo.hStdInput = stdin;
635cdf0e10cSrcweir 	//aStartupInfo.hStdOutput = stdout;
636cdf0e10cSrcweir 	//aStartupInfo.hStdError = stderr;
637cdf0e10cSrcweir 
638cdf0e10cSrcweir 	if ( nFlag & COMMAND_EXECUTE_HIDDEN )
639cdf0e10cSrcweir 	{
640cdf0e10cSrcweir 		aStartupInfo.wShowWindow = SW_HIDE;
641cdf0e10cSrcweir 		aStartupInfo.dwFlags = aStartupInfo.dwFlags | STARTF_USESHOWWINDOW;
642cdf0e10cSrcweir 	}
643cdf0e10cSrcweir 
644cdf0e10cSrcweir 	bool bProcess = CreateProcess( lpApplicationName,
645cdf0e10cSrcweir 						lpCommandLine, lpProcessAttributes,
646cdf0e10cSrcweir 						lpThreadAttributes, bInheritHandles,
647cdf0e10cSrcweir 						dwCreationFlags, lpEnvironment, lpCurrentDirectory,
648cdf0e10cSrcweir 						&aStartupInfo, &aProcessInformation );
649cdf0e10cSrcweir 
650cdf0e10cSrcweir 	LPVOID lpMsgBuf;
651cdf0e10cSrcweir 
652cdf0e10cSrcweir 	if ( bProcess )
653cdf0e10cSrcweir 	{
654cdf0e10cSrcweir 		FormatMessage(
655cdf0e10cSrcweir 			    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
656cdf0e10cSrcweir 			    NULL,
657cdf0e10cSrcweir 			    GetLastError(),
658cdf0e10cSrcweir 			    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
659cdf0e10cSrcweir 			    (LPTSTR) &lpMsgBuf,
660cdf0e10cSrcweir 			    0,
661cdf0e10cSrcweir 			    NULL );
662cdf0e10cSrcweir 
663cdf0e10cSrcweir 		ByteString aErrorString = (char *) lpMsgBuf;
664cdf0e10cSrcweir 
665cdf0e10cSrcweir 		if ( nFlag & COMMAND_EXECUTE_WAIT )
666cdf0e10cSrcweir 		{
667cdf0e10cSrcweir 			DWORD aProcessState = STILL_ACTIVE;
668cdf0e10cSrcweir 			while(aProcessState == STILL_ACTIVE)
669cdf0e10cSrcweir 			{
670cdf0e10cSrcweir 				GetExitCodeProcess(aProcessInformation.hProcess,&aProcessState);
671cdf0e10cSrcweir 			}
672cdf0e10cSrcweir 		}
673cdf0e10cSrcweir 	}
674cdf0e10cSrcweir 	else
675cdf0e10cSrcweir 		fprintf( stderr, "Can not start Process !" );
676cdf0e10cSrcweir 
677cdf0e10cSrcweir #endif
678cdf0e10cSrcweir 	return nRet;
679cdf0e10cSrcweir }
680