home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / cmdline.lib < prev    next >
Encoding:
Text File  |  1995-10-06  |  3.5 KB  |  108 lines

  1. // CmdLine.lib - Functions to process a command line, handling
  2. // ver.1         redirection, spawning, piping, etc...
  3. //               This is likely used by the shell, but may
  4. //               be handled in any script containing an
  5. //               ExecuteCommand() instruction.
  6. //
  7. //**** int ProcessCommandLine(): Process line handle everything and
  8. //                               call ExecuteCommand with final statement
  9. // RETURN: result of command, -1 for error
  10.  
  11. gWhiteSpace = " \t";
  12.  
  13. FindCharNotInQuotes(string,c)
  14. {
  15.    for ( lFound = string; lFound = strchr(lFound,c); lFound++ ) {
  16.       // count quotes before this character
  17.       lFound[0] = 0;  // temporarily null-terminate
  18.       lQuoteCount = 0;
  19.       for ( lQuote = string; lQuote = strchr(lQuote,'\"'); lQuote++ )
  20.          lQuoteCount++;
  21.       lFound[0] = c;
  22.       if ( !(lQuoteCount & 1) )
  23.          break;
  24.    }
  25.    return lFound;
  26. }
  27.  
  28. GetAndRemoveFileName(text)  // remove next parameter from text
  29. {
  30.    // remove all whitespace from beginning of text
  31.    text += strspn(text,gWhiteSpace);
  32.    if ( !text[0] )
  33.       return NULL;
  34.    EndText = text + strcspn(text,gWhiteSpace);
  35.    strncpy(lName,text,EndText-text+1);
  36.    strcpy(text,EndText);
  37.    return lName;
  38. }
  39.  
  40. GetTempOutputFileName()
  41. {
  42.    if ( !(lTemp = getenv("TEMP"))  &&  !(lTemp = getenv("TMP")) )
  43.       lTemp = ".";
  44.    lTemp = FullPath(lTemp);
  45.    if ( '\\' != lTemp[strlen(lTemp)-1] )
  46.       strcat(lTemp,"\\");
  47.    lNameSpec = lTemp + strlen(lTemp);
  48.    for ( i = 0; i < 10000; i++ ) {
  49.       sprintf(lSpec,"%sCNV%04X.TMP",lTemp,i);
  50.       if ( !Directory(lSpec) )
  51.          return lSpec;  
  52.    }
  53.    return False;
  54. }
  55.  
  56. ProcessCommandLine(pCmdLine,ExecuteFunction)
  57. {
  58.    lResult = -1; // assume failure
  59.    strcpy(CmdLine,pCmdLine);
  60.    
  61.    // remove all whitespace from beginning and end of CmdLine
  62.    CmdLine += strspn(CmdLine,gWhiteSpace);
  63.    lCmdEnd = CmdLine + strlen(CmdLine);
  64.    while ( CmdLine < lCmdEnd && strchr(gWhiteSpace,lCmdEnd[-1]) ) {
  65.       lCmdEnd--;
  66.       lCmdEnd[0] = 0;
  67.    }
  68.  
  69.    if ( lPipe = FindCharNotInQuotes(CmdLine,'|') ) {
  70.       // Pipe divides command into left and right sides
  71.       lPipe[0] = '\0';
  72.       lTempFile = GetTempOutputFileName();
  73.       freopen(lTempFile,"wt",stdout);
  74.       lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
  75.       fclose(stdout);
  76.       if ( -1 != lResult ) {
  77.          freopen(lTempFile,"rt",stdin);
  78.          lResult = ProcessCommandLine(lPipe+1,ExecuteFunction);
  79.          fclose(stdin);  
  80.       }
  81.       remove(lTempFile);   
  82.    } else if ( lRedirectIn = FindCharNotInQuotes(CmdLine,'<') ) {
  83.       lRedirectIn[0] = ' ';
  84.       if ( !(lFileIn = GetAndRemoveFileName(lRedirectIn))
  85.         || NULL == freopen(lFileIn,"rt",stdin) ) {
  86.             printf("Unable to open redirected input file!\a\n");
  87.       } else {
  88.          lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
  89.          fclose(stdin);
  90.       }
  91.    } else if ( lRedirectOut = FindCharNotInQuotes(CmdLine,'>') ) {
  92.       lRedirectOut[0] = ' ';
  93.       if ( lAppend = ( '>' == lRedirectOut[1] ) )
  94.          lRedirectOut[1] = ' ';
  95.       if ( !(lFileOut = GetAndRemoveFileName(lRedirectOut))
  96.         || NULL == freopen(lFileOut,lAppend?"at":"wt",stdout) ) {
  97.             printf("Unable to open redirected output file!\a\n");
  98.       } else {
  99.         lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
  100.         fclose(stdout);
  101.       }        
  102.    } else {
  103.       // finally, execute the command
  104.       lResult = function(ExecuteFunction,CmdLine);
  105.    }
  106.    return lResult;
  107. }
  108.