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

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/macparse.c,v 1.1 1992/01/24 03:27:52 dvadura Exp $
  2. -- SYNOPSIS -- parse a macro definition
  3. -- 
  4. -- DESCRIPTION
  5. --    This file contains the code that parses a macro definition
  6. --    stored in a buffer.  If the string in buffer is not a valid
  7. --    macro definition the routie Parse_macro returns 0, otherwise it
  8. --    returns 1 to indicate success.
  9. -- 
  10. -- AUTHOR
  11. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  12. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  13. --
  14. -- COPYRIGHT
  15. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  16. -- 
  17. --      This program is free software; you can redistribute it and/or
  18. --      modify it under the terms of the GNU General Public License
  19. --      (version 1), as published by the Free Software Foundation, and
  20. --      found in the file 'LICENSE' included with this distribution.
  21. -- 
  22. --      This program is distributed in the hope that it will be useful,
  23. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  24. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. --      GNU General Public License for more details.
  26. -- 
  27. --      You should have received a copy of the GNU General Public License
  28. --      along with this program;  if not, write to the Free Software
  29. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. --
  31. -- LOG
  32. --     $Log: macparse.c,v $
  33.  * Revision 1.1  1992/01/24  03:27:52  dvadura
  34.  * dmake Version 3.8, Initial revision
  35.  *
  36. */
  37.  
  38. #include "extern.h"
  39.  
  40. PUBLIC int
  41. Parse_macro( buffer, flag )/*
  42. =============================
  43.    Parse the string in buffer and define it as a macro if it is a valid macro.
  44.    Note especially the string .SETDIR= since it is an attribute, but looks a
  45.    lot like a macro definition.  This would not be a problem if make used
  46.    white space as token separators, since this is not the case we must do
  47.    something about it. */
  48. char *buffer;
  49. int  flag;
  50. {
  51.    register char *tok1;        /* temporary place to keep a token */
  52.    register char *tok2;        /* temporary place to keep a token */
  53.    char         *result;    /* temporary pointer for strings   */
  54.    TKSTR          input;        /* place to scan the buffer from   */
  55.    HASHPTR      hv;        /* pointer to hash table value       */
  56.    int          operator;    /* what macro operator do we have  */
  57.  
  58.    DB_ENTER( "Parse_macro" );
  59.  
  60.    SET_TOKEN( &input, buffer );
  61.    tok1 = Get_token( &input, "=+:*", 0 );
  62.  
  63.    if( Macro_op( tok1 ) ) {
  64.       Error( "No macro name" );
  65.       CLEAR_TOKEN( &input );
  66.       DB_RETURN( 1 );
  67.    }
  68.  
  69.    tok1 = _strdup( tok1 );
  70.    tok2 = Get_token( &input, "=+:*", 2 );
  71.    if( !(operator = Macro_op(tok2)) || 
  72.       (!strcmp(tok1, ".SETDIR") &&
  73.          ((operator != M_OP_CL) || (operator != M_OP_PLCL) ||
  74.           (operator != M_OP_DFCL))) ) {
  75.       CLEAR_TOKEN( &input );
  76.       FREE( tok1 );
  77.       DB_RETURN( 0 );
  78.    }
  79.  
  80.    tok2 = Expand(tok1); FREE(tok1); tok1 = tok2;
  81.    tok2 = Get_token( &input, NIL( char ), FALSE );
  82.  
  83.    switch( operator ) {
  84.       case M_OP_PLCL:
  85.       tok2 = Expand( tok2 );
  86.       /* Fall thru */
  87.  
  88.       case M_OP_PL:
  89.      /* Add to an existing macro, if it is not defined, though, then
  90.       * just define a new macro */
  91.  
  92.      if( (hv = GET_MACRO(tok1)) == NIL(HASH) || hv->ht_value == NIL(char) )
  93.         Def_macro( tok1, tok2, flag );
  94.      else {
  95.         result = _stradd( hv->ht_value, tok2, FALSE );
  96.         Def_macro( tok1, result, flag );
  97.         FREE( result );
  98.      }
  99.      if( operator == M_OP_PLCL ) FREE(tok2);
  100.      break;
  101.  
  102.       case M_OP_DF:
  103.       if( (hv = GET_MACRO(tok1)) != NIL(HASH) )
  104.         break;
  105.      /* else FALLTHRU */
  106.  
  107.       case M_OP_EQ:
  108.      Def_macro( tok1, tok2, flag );
  109.      break;
  110.  
  111.       case M_OP_DFCL:
  112.       if( (hv = GET_MACRO(tok1)) != NIL(HASH) )
  113.         break;
  114.      /* else FALLTHRU */
  115.  
  116.       case M_OP_CL:
  117.      /* If the macro we are assigning from is a single control
  118.       * macro with nothing else, then we propagate the M_MULTI
  119.       * flag to the macro we are assigning the value to so that
  120.       * the same macro can be used to do this over and over. */
  121.      If_multi = 0;
  122.      tok2 = Expand( tok2 );
  123.      Def_macro( tok1, tok2, M_EXPANDED | flag | If_multi );
  124.      FREE( tok2 );
  125.      break;
  126.    }
  127.         
  128.    FREE( tok1 );
  129.  
  130.    DB_RETURN( 1 );
  131. }
  132.  
  133.  
  134.  
  135. PUBLIC int
  136. Macro_op( op )/*
  137. ================
  138.    Check the passed in op string and map it to one of the macro operators */
  139. char *op;
  140. {
  141.    int ret = 0;
  142.  
  143.    DB_ENTER( "macro_op" );
  144.  
  145.    switch( *op ) {
  146.       case '=': ret = M_OP_EQ; break;
  147.       case ':': ret = M_OP_CL; op++; break;
  148.  
  149.       case '+':
  150.          ret = M_OP_PL; op++;
  151.          if( *op == ':' ) { ret = M_OP_PLCL; op++; }
  152.          break;
  153.  
  154.       case '*':
  155.          ret = M_OP_DF; op++;
  156.          if( *op == ':' ) { ret = M_OP_DFCL; op++; }
  157.          break;
  158.    }
  159.  
  160.    if( *op++ != '=' )
  161.       ret = 0;
  162.    else if( *op != '\0' )
  163.       ret = 0;
  164.  
  165.    DB_RETURN( ret );
  166. }
  167.  
  168.