home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / CMDLINE.LIB < prev    next >
Text File  |  1996-02-05  |  4KB  |  116 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.    if( text[0]=='"' )
  35.      {
  36.        EndText = strchr(text+1,'"');
  37.        if( EndText==NULL ) EndText = text+strlen(text);
  38.        strncpy(lName,text+1,EndText-text);
  39.      } else {
  40.        EndText = text + strcspn(text,gWhiteSpace);
  41.        strncpy(lName,text,EndText-text+1);
  42.      }
  43.    strcpy(text,EndText);
  44.    return lName;
  45. }
  46.  
  47. GetTempOutputFileName()
  48. {
  49.    if ( !(lTemp = getenv("TEMP"))  &&  !(lTemp = getenv("TMP")) )
  50.       lTemp = ".";
  51.    lTemp = FullPath(lTemp);
  52.    if ( '\\' != lTemp[strlen(lTemp)-1] )
  53.       strcat(lTemp,"\\");
  54.    lNameSpec = lTemp + strlen(lTemp);
  55.    for ( i = 0; i < 10000; i++ ) {
  56.       sprintf(lSpec,"%sCNV%04X.TMP",lTemp,i);
  57.       if ( !Directory(lSpec) )
  58.          return lSpec;  
  59.    }
  60.    return False;
  61. }
  62.  
  63. ProcessCommandLine(pCmdLine,ExecuteFunction)
  64. {
  65.    lResult = -1; // assume failure
  66.    strcpy(CmdLine,pCmdLine);
  67.    
  68.    // remove all whitespace from beginning and end of CmdLine
  69.    CmdLine += strspn(CmdLine,gWhiteSpace);
  70.    lCmdEnd = CmdLine + strlen(CmdLine);
  71.    while ( CmdLine < lCmdEnd && strchr(gWhiteSpace,lCmdEnd[-1]) ) {
  72.       lCmdEnd--;
  73.       lCmdEnd[0] = 0;
  74.    }
  75.  
  76.    if ( lPipe = FindCharNotInQuotes(CmdLine,'|') ) {
  77.       // Pipe divides command into left and right sides
  78.       lPipe[0] = '\0';
  79.       lTempFile = GetTempOutputFileName();
  80.       freopen(lTempFile,"wt",stdout);
  81.       lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
  82.       fclose(stdout);
  83.       if ( -1 != lResult ) {
  84.          freopen(lTempFile,"rt",stdin);
  85.          lResult = ProcessCommandLine(lPipe+1,ExecuteFunction);
  86.          fclose(stdin);  
  87.       }
  88.       remove(lTempFile);   
  89.    } else if ( lRedirectIn = FindCharNotInQuotes(CmdLine,'<') ) {
  90.       lRedirectIn[0] = ' ';
  91.       if ( !(lFileIn = GetAndRemoveFileName(lRedirectIn))
  92.         || NULL == freopen(lFileIn,"rt",stdin) ) {
  93.             printf("Unable to open redirected input file!\a\n");
  94.       } else {
  95.          lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
  96.          fclose(stdin);
  97.       }
  98.    } else if ( lRedirectOut = FindCharNotInQuotes(CmdLine,'>') ) {
  99.       lRedirectOut[0] = ' ';
  100.       if ( lAppend = ( '>' == lRedirectOut[1] ) )
  101.          lRedirectOut[1] = ' ';
  102.       if ( !(lFileOut = GetAndRemoveFileName(lRedirectOut))
  103.         || NULL == freopen(lFileOut,lAppend?"at":"wt",stdout) ) {
  104.             printf("Unable to open redirected output file!\a\n");
  105.       } else {
  106.         lResult = ProcessCommandLine(CmdLine,ExecuteFunction);
  107.         fclose(stdout);
  108.       }        
  109.    } else {
  110.       // finally, execute the command
  111.       lResult = function(ExecuteFunction,CmdLine);
  112.    }
  113.    return lResult;
  114. }
  115.  
  116.