/************************************************************** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * *************************************************************/ #ifdef SCO #define _IOSTREAM_H #endif #include #include #include "soldep/command.hxx" #include #include #ifdef _MSC_VER #pragma warning (push,1) #endif #include #include #include #include #include #include #ifdef _MSC_VER #pragma warning (pop) #endif //#define MH_TEST2 1 // fuers direkte Testen #if defined(WNT) || defined(OS2) #ifdef _MSC_VER #pragma warning (push,1) #endif #include // for _SPAWN #ifdef _MSC_VER #pragma warning (pop) #endif #endif #ifdef UNX #include #include #if ( defined NETBSD ) || defined (FREEBSD) || defined (AIX) \ || defined (HPUX) || defined (MACOSX) #include #else #include #endif #define P_WAIT 1 // erstmal einen dummz #endif #if defined WNT #include #endif #if defined(WNT) || defined(OS2) #define cPathSeperator ';' #endif #ifdef UNX #define cPathSeperator ':' #endif /*****************************************************************************/ CommandLine::CommandLine(sal_Bool bWrite) /*****************************************************************************/ : bTmpWrite(bWrite) { CommandBuffer = new char [1]; if (CommandBuffer == NULL) { //cout << "Error: nospace" << endl; exit(0); } CommandBuffer[0] = '\0'; nArgc = 0; ppArgv = new char * [1]; ppArgv[0] = NULL; ComShell = new char [128]; char* pTemp = getenv("COMMAND_SHELL"); if(!pTemp) strcpy(ComShell,COMMAND_SHELL); else strcpy(ComShell,pTemp); strcpy(&ComShell[strlen(ComShell)]," -C "); } /*****************************************************************************/ CommandLine::CommandLine(const char *CommandString, sal_Bool bWrite) /*****************************************************************************/ : bTmpWrite(bWrite) { CommandBuffer = new char [1]; if (CommandBuffer == NULL) { //cout << "Error: nospace" << endl; exit(0); } nArgc = 0; ppArgv = new char * [1]; ppArgv[0] = NULL; ComShell = new char [128]; char* pTemp = getenv("COMMAND_SHELL"); if(!pTemp) strcpy(ComShell,COMMAND_SHELL); else strcpy(ComShell,pTemp); strcpy(&ComShell[strlen(ComShell)]," -C "); BuildCommand(CommandString); } /*****************************************************************************/ CommandLine::CommandLine(const CommandLine& CCommandLine, sal_Bool bWrite) /*****************************************************************************/ : bTmpWrite(bWrite) { CommandBuffer = new char [1]; if (CommandBuffer == NULL) { //cout << "Error: nospace" << endl; exit(0); } nArgc = 0; ppArgv = new char * [1]; ppArgv[0] = NULL; ComShell = new char [128]; char* pTemp = getenv("COMMAND_SHELL"); if(!pTemp) strcpy(ComShell,COMMAND_SHELL); else strcpy(ComShell,pTemp); strcpy(&ComShell[strlen(ComShell)]," -C "); BuildCommand(CCommandLine.CommandBuffer); } /*****************************************************************************/ CommandLine::~CommandLine() /*****************************************************************************/ { delete [] CommandBuffer; delete [] ComShell; //for (int i = 0; ppArgv[i] != '\0'; i++) { for (int i = 0; ppArgv[i] != 0; i++) { delete [] ppArgv[i]; } delete [] ppArgv; } /*****************************************************************************/ CommandLine& CommandLine::operator=(const CommandLine& CCommandLine) /*****************************************************************************/ { strcpy (CommandBuffer, CCommandLine.CommandBuffer); for (int i = 0; i != nArgc; i++) { delete [] ppArgv[i]; } delete [] ppArgv; ppArgv = new char * [1]; ppArgv[0] = NULL; BuildCommand(CommandBuffer); return *this; } /*****************************************************************************/ CommandLine& CommandLine::operator=(const char *CommandString) /*****************************************************************************/ { strcpy (CommandBuffer, CommandString); for (int i = 0; i != nArgc; i++) { delete [] ppArgv[i]; } delete [] ppArgv; ppArgv = new char * [1]; ppArgv[0] = NULL; BuildCommand(CommandBuffer); return *this; } /*****************************************************************************/ void CommandLine::Print() /*****************************************************************************/ { //cout << "******* start print *******" << endl; //cout << "nArgc = " << nArgc << endl; //cout << "CommandBuffer = " << CommandBuffer << endl; for (int i = 0; ppArgv[i] != NULL; i++) { //cout << "ppArgv[" << i << "] = " << ppArgv[i] << endl; } //cout << "******** end print ********" << endl; } /*****************************************************************************/ void CommandLine::BuildCommand(const char *CommandString) /*****************************************************************************/ { int index = 0, pos=0; char buffer[1024]; char WorkString[1024]; strcpy(WorkString,CommandString); //falls LogWindow -> in tmpfile schreiben if(bTmpWrite) { strcpy(&WorkString[strlen(WorkString)]," >&"); strcpy(&WorkString[strlen(WorkString)],getenv("TMP")); strcpy(&WorkString[strlen(WorkString)],TMPNAME); } // delete old memory and get some new memory for CommandBuffer delete [] CommandBuffer; CommandBuffer = new char [strlen(ComShell)+strlen(WorkString)+1]; if (CommandBuffer == NULL) { //cout << "Error: nospace" << endl; exit(0); } strcpy (CommandBuffer, ComShell); strcpy (&CommandBuffer[strlen(ComShell)], WorkString); CommandString = CommandBuffer; // get the number of tokens Strtokens(CommandString); // delete the space for the old CommandLine for (int i = 0; ppArgv[i] != 0; i++) { delete [] ppArgv[i]; } delete [] ppArgv; /* get space for the new command line */ ppArgv = (char **) new char * [nArgc+1]; if (ppArgv == NULL) { //cout << "Error: no space" << endl; exit(0); } // flush the white space while ( isspace(*CommandString) ) CommandString++; index = 0; // start the loop to build all the individual tokens while (*CommandString != '\0') { pos = 0; // copy the token until white space is found while ( !isspace(*CommandString) && *CommandString != '\0') { buffer[pos++] = *CommandString++; } buffer[pos] = '\0'; // get space for the individual tokens ppArgv[index] = (char *) new char [strlen(buffer)+1]; if (ppArgv[index] == NULL) { //cout << "Error: nospace" << endl; exit(0); } // copy the token strcpy (ppArgv[index++], buffer); // flush while space while ( isspace(*CommandString) ) CommandString++; } // finish by setting the las pointer to NULL ppArgv[nArgc]= NULL; } /*****************************************************************************/ void CommandLine::Strtokens(const char *CommandString) /*****************************************************************************/ { int count = 0; const char *temp; temp = CommandString; /* bypass white space */ while (isspace(*temp)) temp++; for (count=0; *temp != '\0'; count++) { /* continue until white space of string terminator is found */ while ((!isspace(*temp)) && (*temp != '\0')) temp++; /* bypass white space */ while (isspace(*temp)) temp++; } nArgc = count; } /*****************************************************************************/ CCommand::CCommand( ByteString &rString ) /*****************************************************************************/ { rString.SearchAndReplace( '\t', ' ' ); aCommand = rString.GetToken( 0, ' ' ); aCommandLine = Search( "PATH" ); #ifndef UNX aCommandLine += " /c "; #else aCommandLine += " -c "; #endif ByteString sCmd( rString.GetToken( 0, ' ' )); ByteString sParam( rString.Copy( sCmd.Len())); aCommandLine += Search( "PATH", sCmd ); aCommandLine += sParam; ImplInit(); } /*****************************************************************************/ CCommand::CCommand( const char *pChar ) /*****************************************************************************/ { ByteString aString = pChar; aString.SearchAndReplace( '\t', ' ' ); aCommand = aString.GetToken( 0, ' ' ); aCommandLine = Search( "PATH" ); #ifndef UNX aCommandLine += " /c "; #else aCommandLine += " -c "; #endif ByteString rString( pChar ); ByteString sCmd( rString.GetToken( 0, ' ' )); ByteString sParam( rString.Copy( sCmd.Len())); aCommandLine += Search( "PATH", sCmd ); aCommandLine += sParam; ImplInit(); } /*****************************************************************************/ void CCommand::ImplInit() /*****************************************************************************/ { char pTmpStr[255]; size_t *pPtr; char *pChar; int nVoid = sizeof( size_t * ); nArgc = aCommandLine.GetTokenCount(' '); sal_uIntPtr nLen = aCommandLine.Len(); ppArgv = (char **) new char[ (sal_uIntPtr)(nLen + nVoid * (nArgc +2) + nArgc ) ]; pChar = (char *) ppArgv + ( (1+nArgc) * nVoid ); pPtr = (size_t *) ppArgv; for ( xub_StrLen i=0; i