home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38A.ZIP / GETINP.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  14KB  |  528 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/getinp.c,v 1.1 1992/01/24 03:27:56 dvadura Exp $
  2. -- SYNOPSIS -- handle reading of input.
  3. -- 
  4. -- DESCRIPTION
  5. --    The code in this file reads the input from the specified stream
  6. --    into the provided buffer of size Buffer_size.  In doing so it deletes
  7. --    comments.  Comments are delimited by the #, and
  8. --    <nl> character sequences.  An exception is \# which
  9. --    is replaced by # in the input.  Line continuations are signalled
  10. --    at the end of a line and are recognized inside comments.
  11. --    The line continuation is always  <\><nl>.
  12. --
  13. --    If the file to read is NIL(FILE) then the Get_line routine returns the
  14. --    next rule from the builtin rule table if there is one.
  15. --
  16. -- AUTHOR
  17. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  18. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  19. --
  20. -- COPYRIGHT
  21. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  22. -- 
  23. --      This program is free software; you can redistribute it and/or
  24. --      modify it under the terms of the GNU General Public License
  25. --      (version 1), as published by the Free Software Foundation, and
  26. --      found in the file 'LICENSE' included with this distribution.
  27. -- 
  28. --      This program is distributed in the hope that it will be useful,
  29. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  30. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  31. --      GNU General Public License for more details.
  32. -- 
  33. --      You should have received a copy of the GNU General Public License
  34. --      along with this program;  if not, write to the Free Software
  35. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. --
  37. -- LOG
  38. --     $Log: getinp.c,v $
  39.  * Revision 1.1  1992/01/24  03:27:56  dvadura
  40.  * dmake Version 3.8, Initial revision
  41.  *
  42. */
  43.  
  44. #include "extern.h"
  45.  
  46. #define IS_WHITE(A)  ((A == ' ') || (A == '\t') || (A == '\n') || (A == '\r'))
  47. #define SCAN_WHITE(A) \
  48.     while( IS_WHITE(*A) ) A++;
  49.  
  50. static    int    _is_conditional ANSI((char*));
  51. static    int    _handle_conditional ANSI((int, TKSTRPTR));
  52.  
  53. static int  rule_ind = 0;    /* index of rule when reading Rule_tab     */
  54. static int  skip = FALSE;    /* if true the skip input         */
  55.  
  56.  
  57. PUBLIC int
  58. Get_line( buf, fil )/*
  59. ======================
  60.         Read a line of input from the file stripping
  61.         off comments.  The routine returns TRUE if EOF */
  62. char *buf;
  63. FILE *fil;
  64. {
  65.    extern   char **Rule_tab;
  66.    register char *p;
  67.    register char *c;
  68.    char      *q;
  69.    char             *buf_org;
  70.    static   int     ignore = FALSE;
  71.    int          cont   = FALSE;
  72.    int          pos    = 0;
  73.    int         res;
  74.  
  75.    DB_ENTER( "Get_line" );
  76.  
  77.    if( fil == NIL(FILE) ) {
  78.       /* Reading the internal rule table.  Set the rule_index to zero.
  79.        * This way ReadEnvironment works as expected every time. */
  80.  
  81.       while( (p = Rule_tab[ rule_ind++ ]) != NIL(char) )
  82.      /* The last test in this if '*p != '~', handles the environment
  83.       * passing conventions used by MKS to pass arguments.  We want to
  84.       * skip those environment entries. */
  85.      if( !Readenv || (Readenv && (strchr(p,'=') != NIL(char)) && *p!='~')){
  86.         strcpy( buf, p );
  87.  
  88.         DB_PRINT( "io", ("Returning [%s]", buf) );
  89.         DB_RETURN( FALSE );
  90.      }
  91.  
  92.       rule_ind = 0;
  93.  
  94.       DB_PRINT( "io", ("Done Ruletab") );
  95.       DB_RETURN( TRUE );
  96.    }
  97.  
  98.    buf_org = buf;
  99.  
  100. do_again:
  101.    do {
  102.       p = buf+pos;
  103.       if(feof( fil ) || (fgets( p, Buffer_size-pos, fil ) == NIL(char)))
  104.      DB_RETURN( TRUE );
  105.  
  106.       Line_number++;
  107.  
  108.       /* ignore input if ignore flag set and line ends in a continuation
  109.      character. */
  110.  
  111.       q = p+strlen(p)-2;
  112.       /* ignore each RETURN at the end of a line before any further
  113.        * processing */
  114.       if( q[0] == '\r' && q[1] == '\n' ) {
  115.      q[0] = '\n';
  116.      q[1] = '\0';
  117.      q--;
  118.       }
  119.       /* you also have to deal with END_OF_FILE chars to process raw
  120.        * DOS-Files. Normally they are the last chars in file, but after
  121.        * working on these file with vi, there is an additional NEWLINE
  122.        * after the last END_OF_FILE. So if the second last char in the
  123.        * actual line is END_OF_FILE, you can skip the last char. Then
  124.        * you can search the line back until you find no more END_OF_FILE
  125.        * and nuke each you found by string termination. */
  126.       if( q[0] == '\032' )
  127.      q--;
  128.       while( q[1] == '\032' ) {
  129.      q[1] = '\0';
  130.      q--;
  131.       }
  132.  
  133.       if( ignore ) {
  134.      if( q[0] != CONTINUATION_CHAR || q[1] != '\n' )  ignore = FALSE;
  135.      *p = '\0';
  136.      continue;
  137.       }
  138.  
  139.       c = Do_comment(p, &q, Group || (*buf == '\t'));
  140.       
  141.       /* Does the end of the line end in a continuation sequence? */
  142.       
  143.       if( (q[0] == CONTINUATION_CHAR) && (q[1] == '\n')) {
  144.      /* If the continuation was at the end of a comment then ignore the
  145.       * next input line, (or lines until we get one ending in just <nl>)
  146.       * else it's a continuation, so build the input line from several
  147.       * text lines on input.  The maximum size of this is governened by
  148.       * Buffer_size */
  149.      if( q != p && q[-1] == CONTINUATION_CHAR ) {
  150.         strcpy( q, q+1 );
  151.         q--;
  152.         cont = FALSE;
  153.      }
  154.      else if( c != NIL(char) )
  155.         ignore = TRUE;
  156.      else
  157.         cont   = TRUE;
  158.       }
  159.       else {
  160.      cont = FALSE;
  161.       }
  162.  
  163.       q    = ( c == NIL(char) ) ? q+2 : c;
  164.       pos += q-p;
  165.    }
  166.    while( (cont || !*buf) && (pos <= Buffer_size) );
  167.  
  168.    if( buf[ pos-1 ] == '\n' )
  169.       buf[ --pos ] = '\0';
  170.    else
  171.       if( pos == Buffer_size-1 )
  172.      Fatal( "Input line too long, increase MAXLINELENGTH" );
  173.  
  174.  
  175.    /* Now that we have the next line of input to make, we should check to
  176.     * see if it is a conditional expression.  If it is then process it,
  177.     * otherwise pass it on to the parser. */
  178.  
  179.    if( *(p = _strspn(buf, " \t\r\n")) == CONDSTART ) {
  180.       TKSTR token;
  181.  
  182.       SET_TOKEN( &token, p );
  183.  
  184.       p = Get_token( &token, "", FALSE );
  185.  
  186.       if( (res = _is_conditional( p )) )    /* ignore non control special */
  187.       {                        /* targets               */
  188.      res  = _handle_conditional( res, &token );
  189.      skip = TRUE;
  190.       }
  191.       else {
  192.      CLEAR_TOKEN( &token );
  193.      res  = TRUE;
  194.       }
  195.    }
  196.  
  197.    if( skip ) {
  198.       buf  = buf_org;        /* ignore line just read in */
  199.       pos  = 0;
  200.       skip = res;
  201.       goto do_again;
  202.    }
  203.  
  204.    DB_PRINT( "io", ("Returning [%s]", buf) );
  205.    DB_RETURN( FALSE );
  206. }
  207.  
  208.  
  209. PUBLIC char *
  210. Do_comment(str, pend, keep)/*
  211. =============================
  212.    Search the input string looking for comment chars.  If it contains
  213.    comment chars then NUKE the remainder of the line, if the comment
  214.    char is preceeded by \ then shift the remainder of the line left
  215.    by one char. */
  216. char *str;
  217. char **pend;
  218. int  keep;
  219. {
  220.    char *c = str;
  221.  
  222.    while( (c = strchr(c, COMMENT_CHAR)) != NIL(char) ) {
  223.       if( Comment || State == NORMAL_SCAN )
  224.      if( c != str && c[-1] == ESCAPE_CHAR ) {
  225.         strcpy( c-1, c );        /* copy it left, due to \# */
  226.         if( pend ) (*pend)--;    /* shift tail pointer left */
  227.      }
  228.      else {
  229.         if(    c == str
  230.             && c[1] == '!'
  231.             && Line_number == 1
  232.         && Nestlevel() == 1 ) {
  233.            int res;
  234.            char *cmnd;
  235.          
  236.            cmnd = Expand(c+2);
  237.            cmnd[strlen(cmnd)-1] = '\0';    /* strip last newline */
  238.            Current_target = Root;
  239.            Swap_on_exec = TRUE;
  240.            Wait_for_completion = TRUE;
  241.            Do_cmnd(cmnd, FALSE, TRUE, Current_target, FALSE, FALSE, TRUE);
  242.         }
  243.  
  244.         *c = '\0';               /* a true comment so break */
  245.         break;
  246.      }
  247.       else {
  248.          if( keep )
  249.         c = NIL(char);
  250.      else
  251.         *c = '\0';
  252.  
  253.      break;
  254.       }
  255.    }
  256.  
  257.    return(c);
  258. }
  259.       
  260.  
  261. PUBLIC char *
  262. Get_token( string, brk, anchor )/*
  263. ==================================
  264.     Return the next token in string.
  265.     Returns empty string when no more tokens in string.
  266.     brk is a list of chars that also cause breaks in addition to space and
  267.     tab, but are themselves returned as tokens.  if brk is NULL then the
  268.     remainder of the line is returned as a single token.
  269.     
  270.     anchor if 1, says break on chars in the brk list, but only if
  271.     the entire token begins with the first char of the brk list, if
  272.     0 then any char of brk will cause a break to occurr.
  273.     
  274.     If anchor is 2, then break only seeing the first char in the break
  275.     list allowing only chars in the break list to form the prefix. */
  276.  
  277. TKSTRPTR  string;
  278. char      *brk;
  279. int      anchor;
  280. {
  281.    register char *s;
  282.    register char *curp;
  283.    register char *t;
  284.    int           done = FALSE;
  285.    char          space[10];
  286.  
  287.    DB_ENTER( "Get_token" );
  288.  
  289.    s  = string->tk_str;              /* Get string parameters    */
  290.    *s = string->tk_cchar;          /* ... and strip leading w/s    */
  291.  
  292.    SCAN_WHITE( s );
  293.  
  294.    DB_PRINT( "tok", ("What's left [%s]", s) );
  295.  
  296.    if( !*s ) {
  297.       DB_PRINT( "tok", ("Returning NULL token") );
  298.       DB_RETURN( "" );
  299.    }
  300.  
  301.  
  302.    /* Build the space list.  space contains all those chars that may possibly
  303.     * cause breaks.  This includes the brk list as well as white space. */
  304.  
  305.    if( brk != NIL(char) ) {
  306.       strcpy( space, " \t\r\n" );
  307.       strcat( space, brk   );
  308.    }
  309.    else {
  310.       space[0] = 0xff;            /* a char we know will not show up      */
  311.       space[1] = 0;
  312.    }
  313.  
  314.  
  315.    /* Handle processing of quoted tokens.  Note that this is disabled if
  316.     * brk is equal to NIL */
  317.  
  318.    while( *s == '\"' && ((brk != NIL(char)) || !string->tk_quote) ) {
  319.       s++;
  320.       if( string->tk_quote ) {
  321.      curp = s-1;
  322.      do { curp = strchr( curp+1, '\"' ); }
  323.      while( (curp != NIL(char)) && (*(curp+1) == '\"'));
  324.  
  325.          if( curp == NIL(char) ) Fatal( "Unmatched quote in token" );
  326.      string->tk_quote = !string->tk_quote;
  327.  
  328.      /* Check for "" case, and if found ignore it */
  329.      if( curp == s ) continue;
  330.      goto found_token;
  331.       }
  332.       else
  333.      SCAN_WHITE( s );
  334.  
  335.       string->tk_quote = !string->tk_quote;
  336.    }
  337.    
  338.  
  339.    /* Check for a token break character at the beginning of the token.
  340.     * If found return the next set of break chars as a token. */
  341.  
  342.    if( anchor == 2 && brk != NIL(char) ) {
  343.       curp = s;
  344.       while( *curp && (strchr(brk,*curp)!=NIL(char)) && (*curp!=*brk) ) curp++;
  345.       done = (*brk == *curp++);
  346.    }
  347.    else if( (brk != NIL(char)) && (strchr( brk, *s ) != NIL(char)) ) {
  348.       curp = _strspn( s, brk );
  349.       done = (anchor == 0) ? TRUE :
  350.          ((anchor == 1)?(*s == *brk) : (*brk == curp[-1]));
  351.    }
  352.  
  353.  
  354.    /* Scan for the next token in the list and return it less the break char
  355.     * that was used to terminate the token.  It will possibly be returned in
  356.     * the next call to Get_token */
  357.  
  358.    if( !done ) {
  359.       SCAN_WHITE( s );
  360.  
  361.       t = s;
  362.       do {
  363.      done = TRUE;
  364.      curp = _strpbrk(t, space);
  365.      
  366.      if( anchor && *curp && !IS_WHITE( *curp ) )
  367.         if( ((anchor == 1)?*curp:_strspn(curp,brk)[-1]) != *brk ) {
  368.            t++;
  369.            done = FALSE;
  370.         }
  371.       }
  372.       while( !done );
  373.  
  374.       if( (curp == s) && (strchr(brk, *curp) != NIL(char)) ) curp++;
  375.    }
  376.  
  377. found_token:
  378.    string->tk_str   = curp;
  379.    string->tk_cchar = *curp;
  380.    *curp = '\0';
  381.  
  382.    DB_PRINT( "tok", ("Returning [%s]", s) );
  383.    DB_RETURN( s );
  384. }
  385.  
  386.  
  387. static int
  388. _is_conditional( tg )/*
  389. =======================
  390.     Look at tg and return it's value if it is a conditional identifier
  391.     otherwise return 0. */
  392. char *tg;
  393. {
  394.    DB_ENTER( "_is_conditional" );
  395.    
  396.    tg++;
  397.    switch( *tg ) {
  398.       case 'I': if( !strcmp( tg, "IF" )) DB_RETURN( ST_IF   ); break;
  399.       
  400.       case 'E':
  401.          if( !strcmp( tg, "END" ))     DB_RETURN( ST_END  );
  402.          else if( !strcmp( tg, "ENDIF")) DB_RETURN( ST_END  );
  403.          else if( !strcmp( tg, "ELSE" )) DB_RETURN( ST_ELSE );
  404.          else if( !strcmp( tg, "ELIF" )) DB_RETURN( ST_ELIF );
  405.      break;
  406.    }
  407.    
  408.    DB_RETURN( 0 );
  409. }
  410.  
  411.  
  412.  
  413. #define SEEN_END  0x00
  414. #define SEEN_IF   0x01
  415. #define SEEN_ELSE 0x02
  416. #define SEEN_ELIF 0x04
  417.  
  418. #define ACCEPT_IF   0x10
  419. #define ACCEPT_ELIF 0x20
  420.  
  421. static int
  422. _handle_conditional( opcode, tg )/*
  423. ===================================
  424.     Perform the necessary processing for .IF conditinal targets.
  425.     Someday this should be modified to do bracketted expressions ala
  426.     CPP... sigh */
  427. int      opcode;
  428. TKSTRPTR tg;
  429. {
  430.    static short    action[MAX_COND_DEPTH];
  431.    static char  ifcntl[MAX_COND_DEPTH];
  432.    char     *tok, *lhs, *rhs, *op, *expr;
  433.    int      result;
  434.  
  435.    DB_ENTER( "_handle_conditional" );
  436.  
  437.    switch( opcode ) {
  438.       case ST_ELIF:
  439.          if( !(ifcntl[Nest_level] & SEEN_IF) || (ifcntl[Nest_level]&SEEN_ELSE) )
  440.         Fatal(".ELIF without a preceeding .IF" );
  441.      /* FALLTHROUGH */
  442.  
  443.       case ST_IF:
  444.      if( opcode == ST_IF && (Nest_level+1) == MAX_COND_DEPTH )
  445.         Fatal( ".IF .ELSE ... .END nesting too deep" );
  446.  
  447.      If_expand = TRUE;
  448.      expr = Expand( Get_token( tg, NIL(char), FALSE ));
  449.      If_expand = FALSE;
  450.      lhs = _strspn( expr, " \t" );
  451.      if( !*lhs ) lhs = NIL(char);
  452.  
  453.      if( (op = _strstr( lhs, "==" )) == NIL(char) )
  454.         op = _strstr( lhs, "!=" );
  455.  
  456.      if( op == NIL(char) )
  457.         result = (lhs != NIL(char));
  458.      else {
  459.         op[1] = op[0];
  460.         if( lhs != op ) {
  461.            for( tok = op-1; (tok != lhs) && ((*tok == ' ')||(*tok == '\t'));
  462.                 tok-- );
  463.            tok[1] = '\0';
  464.         }
  465.         else
  466.            lhs = NIL(char);
  467.  
  468.         op++;
  469.         rhs = _strspn( op+1, " \t" );
  470.         if( !*rhs ) rhs = NIL(char);
  471.  
  472.         if( (rhs == NIL(char)) || (lhs == NIL(char)) )
  473.            result = (rhs == lhs) ? TRUE : FALSE;
  474.         else {
  475.            tok = rhs + strlen( rhs );
  476.            for( tok=tok-1; (tok != lhs) && ((*tok == ' ')||(*tok == '\t'));
  477.             tok--);
  478.            tok[1] = '\0';
  479.  
  480.            result = (strcmp( lhs, rhs ) == 0) ? TRUE : FALSE;
  481.         }
  482.  
  483.         if( *op == '!' ) result = !result;
  484.      }
  485.  
  486.      if( expr != NIL(char) ) FREE( expr );
  487.  
  488.      if( opcode == ST_IF ) {
  489.         Nest_level++;
  490.         action[Nest_level] = 1;
  491.      }
  492.      ifcntl[Nest_level] |= (opcode==ST_IF)?SEEN_IF:SEEN_ELIF;
  493.  
  494.      if( result ) {
  495.         if( !(ifcntl[Nest_level] & (ACCEPT_IF|ACCEPT_ELIF)) ) {
  496.            action[ Nest_level ] = action[ Nest_level-1 ];
  497.            ifcntl[Nest_level] |= (opcode==ST_IF)?ACCEPT_IF:ACCEPT_ELIF;
  498.         }
  499.         else
  500.            action[Nest_level] = 1;
  501.      }
  502.      else
  503.         action[Nest_level] = 1;
  504.      break;
  505.  
  506.       case ST_ELSE:
  507.      if( Nest_level <= 0 ) Fatal( ".ELSE without .IF" );
  508.      if( ifcntl[Nest_level] & SEEN_ELSE )
  509.         Fatal( "Missing .IF or .ELIF before .ELSE" );
  510.  
  511.      if( ifcntl[Nest_level] & (ACCEPT_IF|ACCEPT_ELIF) )
  512.         action[Nest_level] = 1;
  513.      else if( action[ Nest_level-1 ] != 1 )
  514.         action[ Nest_level ] ^= 0x1;    /* flip between 0 and 1    */
  515.  
  516.      ifcntl[Nest_level] |= SEEN_ELSE;
  517.      break;
  518.  
  519.       case ST_END:
  520.      ifcntl[Nest_level] = SEEN_END;
  521.      Nest_level--;
  522.      if( Nest_level < 0 ) Fatal( "Unmatched .END[IF]" );
  523.      break;
  524.    }
  525.  
  526.    DB_RETURN( action[ Nest_level ] );
  527. }
  528.