home *** CD-ROM | disk | FTP | other *** search
- // CmdLine.lib - Functions to process a command line, handling
- // ver.1 redirection, spawning, piping, etc...
- // This is likely used by the shell, but may
- // be handled in any script containing an
- // ExecuteCommand() instruction.
- //
- //**** int ProcessCommandLine(): Process line handle everything and
- // call ExecuteCommand with final statement
- // RETURN: result of command, -1 for error
-
- gWhiteSpace = " \t";
-
- FindCharNotInQuotes(string,c)
- {
- for ( lFound = string; lFound = strchr(lFound,c); lFound++ ) {
- // count quotes before this character
- lFound[0] = 0; // temporarily null-terminate
- lQuoteCount = 0;
- for ( lQuote = string; lQuote = strchr(lQuote,'\"'); lQuote++ )
- lQuoteCount++;
- lFound[0] = c;
- if ( !(lQuoteCount & 1) )
- break;
- }
- return lFound;
- }
-
- GetAndRemoveFileName(text) // remove next parameter from text
- {
- // remove all whitespace from beginning of text
- text += strspn(text,gWhiteSpace);
- if ( !text[0] )
- return NULL;
- EndText = text + strcspn(text,gWhiteSpace);
- strncpy(lName,text,EndText-text+1);
- strcpy(text,EndText);
- return lName;
- }
-
- GetTempOutputFileName()
- {
- if ( !(lTemp = getenv("TEMP")) && !(lTemp = getenv("TMP")) )
- lTemp = ".";
- lTemp = FullPath(lTemp);
- if ( '\\' != lTemp[strlen(lTemp)-1] )
- strcat(lTemp,"\\");
- lNameSpec = lTemp + strlen(lTemp);
- for ( i = 0; i < 10000; i++ ) {
- sprintf(lSpec,"%sCNV%04X.TMP",lTemp,i);
- if ( !Directory(lSpec) )
- return lSpec;
- }
- return False;
- }
-
- ProcessCommandLine(pCmdLine,ExecuteFunction)
- {
- lResult = -1; // assume failure
- strcpy(CmdLine,pCmdLine);
-
- // remove all whitespace from beginning and end of CmdLine
- CmdLine += strspn(CmdLine,gWhiteSpace);
- lCmdEnd = CmdLine + strlen(CmdLine);
- while ( CmdLine < lCmdEnd && strchr(gWhiteSpace,lCmdEnd[-1]) ) {
- lCmdEnd--;
- lCmdEnd[0] = 0;
- }
-
- if ( lPipe = FindCharNotInQuotes(CmdLine,'|') ) {
- // Pipe divides command into left and right sides
- lPipe[0] = '\0';
- lTempFile = GetTempOutputFileName();
- freopen(lTempFile,"wt",stdout);
- lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
- fclose(stdout);
- if ( -1 != lResult ) {
- freopen(lTempFile,"rt",stdin);
- lResult = ProcessCommandLine(lPipe+1,ExecuteFunction);
- fclose(stdin);
- }
- remove(lTempFile);
- } else if ( lRedirectIn = FindCharNotInQuotes(CmdLine,'<') ) {
- lRedirectIn[0] = ' ';
- if ( !(lFileIn = GetAndRemoveFileName(lRedirectIn))
- || NULL == freopen(lFileIn,"rt",stdin) ) {
- printf("Unable to open redirected input file!\a\n");
- } else {
- lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
- fclose(stdin);
- }
- } else if ( lRedirectOut = FindCharNotInQuotes(CmdLine,'>') ) {
- lRedirectOut[0] = ' ';
- if ( lAppend = ( '>' == lRedirectOut[1] ) )
- lRedirectOut[1] = ' ';
- if ( !(lFileOut = GetAndRemoveFileName(lRedirectOut))
- || NULL == freopen(lFileOut,lAppend?"at":"wt",stdout) ) {
- printf("Unable to open redirected output file!\a\n");
- } else {
- lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
- fclose(stdout);
- }
- } else {
- // finally, execute the command
- lResult = function(ExecuteFunction,CmdLine);
- }
- return lResult;
- }
-