| Ivo Hinkelmann | 551eb4d | 2007-10-15 14:40:02 +0000 | [diff] [blame] | 1 | /* RCS $Id: macparse.c,v 1.3 2007-10-15 15:40:02 ihi Exp $ |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 2 | -- |
| 3 | -- SYNOPSIS |
| 4 | -- Parse a macro definition |
| 5 | -- |
| 6 | -- DESCRIPTION |
| 7 | -- This file contains the code that parses a macro definition |
| 8 | -- stored in a buffer. If the string in buffer is not a valid |
| 9 | -- macro definition the routie Parse_macro returns 0, otherwise it |
| 10 | -- returns 1 to indicate success. |
| 11 | -- |
| 12 | -- AUTHOR |
| 13 | -- Dennis Vadura, dvadura@dmake.wticorp.com |
| 14 | -- |
| 15 | -- WWW |
| 16 | -- http://dmake.wticorp.com/ |
| 17 | -- |
| 18 | -- COPYRIGHT |
| 19 | -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved. |
| 20 | -- |
| 21 | -- This program is NOT free software; you can redistribute it and/or |
| 22 | -- modify it under the terms of the Software License Agreement Provided |
| 23 | -- in the file <distribution-root>/readme/license.txt. |
| 24 | -- |
| 25 | -- LOG |
| 26 | -- Use cvs log to obtain detailed change logs. |
| 27 | */ |
| 28 | |
| 29 | #include "extern.h" |
| 30 | |
| 31 | PUBLIC int |
| 32 | Parse_macro( buffer, flag )/* |
| 33 | ============================= |
| 34 | Parse the string in buffer and define it as a macro if it is a valid macro. |
| 35 | Note especially the string .SETDIR= since it is an attribute, but looks a |
| 36 | lot like a macro definition. This would not be a problem if make used |
| 37 | white space as token separators, since this is not the case we must do |
| 38 | something about it. */ |
| 39 | char *buffer; |
| 40 | int flag; |
| 41 | { |
| 42 | char *result; /* temporary pointer for strings */ |
| 43 | TKSTR input; /* place to scan the buffer from */ |
| 44 | HASHPTR hv; /* pointer to hash table value */ |
| 45 | int operator; /* what macro operator do we have */ |
| 46 | char *tok1; /* temporary place to keep a token */ |
| 47 | char *tok2; /* temporary place to keep a token */ |
| Ivo Hinkelmann | 551eb4d | 2007-10-15 14:40:02 +0000 | [diff] [blame] | 48 | int toklen; /* length of a token */ |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 49 | |
| 50 | DB_ENTER( "Parse_macro" ); |
| 51 | |
| 52 | SET_TOKEN( &input, buffer ); |
| 53 | tok1 = Get_token( &input, "=+:*!?", 0 ); |
| 54 | |
| 55 | operator=Macro_op(tok1); |
| 56 | if( operator ) { |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 57 | CLEAR_TOKEN( &input ); |
| Ivo Hinkelmann | 551eb4d | 2007-10-15 14:40:02 +0000 | [diff] [blame] | 58 | Error( "Assignment without macro name: [%s].", buffer ); |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 59 | DB_RETURN( 1 ); |
| 60 | } |
| 61 | |
| 62 | tok1 = DmStrDup(tok1); |
| 63 | tok2 = Get_token( &input, "=+:*!?", 2 ); |
| 64 | if( !(operator = Macro_op(tok2)) || !strcmp(tok1,".SETDIR") ) { |
| 65 | CLEAR_TOKEN( &input ); |
| 66 | FREE(tok1); |
| 67 | DB_RETURN(0); |
| 68 | } |
| 69 | |
| 70 | tok2 = Expand(tok1); FREE(tok1); tok1 = tok2; |
| Ivo Hinkelmann | 551eb4d | 2007-10-15 14:40:02 +0000 | [diff] [blame] | 71 | if ( !(toklen = strlen(tok1)) ) { |
| 72 | Warning( "Empty macro name after expansion: [%s].", buffer ); |
| 73 | } |
| 74 | |
| 75 | /* Catch illegal single character macro names. */ |
| 76 | if ( toklen == 1 && strchr("{()}", tok1[0]) ) { |
| 77 | CLEAR_TOKEN( &input ); |
| 78 | Fatal( "Syntax error in macro assignment [%s]. The following characters cannot be used as single letter macro names: '{()}'.", buffer ); |
| 79 | } |
| 80 | |
| 81 | /* Catch ':' in macro names. */ |
| 82 | if ( strchr(tok1, ':') ) { |
| 83 | CLEAR_TOKEN( &input ); |
| 84 | Fatal( "Syntax error in macro assignment [%s]. The character ':' is not allowed in macro names.", buffer ); |
| 85 | } |
| 86 | |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 87 | tok2 = Get_token(&input, NIL( char ), FALSE); |
| 88 | |
| 89 | /* Make sure we can force the assignment. */ |
| 90 | if ( operator & M_OP_SI ) { |
| 91 | flag |= M_FORCE|M_MULTI; |
| 92 | operator &= ~M_OP_SI; |
| 93 | } |
| 94 | |
| 95 | switch( operator ) { |
| 96 | case M_OP_PLCL: |
| 97 | tok2 = Expand( tok2 ); |
| 98 | /* Fall thru */ |
| 99 | |
| 100 | case M_OP_PL: |
| 101 | /* Add to an existing macro, if it is not defined, though, then |
| 102 | * just define a new macro */ |
| 103 | |
| 104 | if( (hv = GET_MACRO(tok1)) == NIL(HASH) || hv->ht_value == NIL(char) ) |
| 105 | Def_macro( tok1, tok2, flag ); |
| 106 | else { |
| 107 | result = DmStrAdd( hv->ht_value, tok2, FALSE ); |
| 108 | Def_macro( tok1, result, flag ); |
| 109 | FREE( result ); |
| 110 | } |
| 111 | if( operator == M_OP_PLCL ) FREE(tok2); |
| 112 | break; |
| 113 | |
| 114 | case M_OP_DF: |
| Vladimir Glazounov | dcfd545 | 2006-09-25 08:40:09 +0000 | [diff] [blame] | 115 | /* *= */ |
| 116 | /* internal default macros or initialized empty macros set M_INIT. */ |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 117 | if( (hv = GET_MACRO(tok1)) != NIL(HASH) && !(hv->ht_flag & M_INIT) ) |
| 118 | break; |
| 119 | /* else FALLTHRU */ |
| 120 | |
| 121 | case M_OP_EQ: |
| 122 | Def_macro( tok1, tok2, flag ); |
| 123 | break; |
| 124 | |
| 125 | case M_OP_DFCL: |
| Vladimir Glazounov | dcfd545 | 2006-09-25 08:40:09 +0000 | [diff] [blame] | 126 | /* *:= */ |
| 127 | /* internal default macros or initialized empty macros set M_INIT. */ |
| Jens-Heiner Rechtien | b501a54 | 2000-09-22 14:33:37 +0000 | [diff] [blame] | 128 | if( (hv = GET_MACRO(tok1)) != NIL(HASH) && !(hv->ht_flag & M_INIT) ) |
| 129 | break; |
| 130 | /* else FALLTHRU */ |
| 131 | |
| 132 | case M_OP_CL: |
| 133 | tok2 = Expand( tok2 ); |
| 134 | Def_macro( tok1, tok2, M_EXPANDED | flag ); |
| 135 | FREE( tok2 ); |
| 136 | break; |
| 137 | |
| 138 | case M_OP_CM:{ |
| 139 | CELLPTR cp; |
| 140 | STRINGPTR sp; |
| 141 | |
| 142 | if (flag & M_PUSH) { |
| 143 | Error("Nested conditional definition [%s ?= %s] ignored", |
| 144 | tok1, tok2); |
| 145 | } |
| 146 | else { |
| 147 | cp = Def_cell(tok1); |
| 148 | if (cp->ce_flag & F_MULTI) { |
| 149 | LINKPTR lp; |
| 150 | for(lp=cp->ce_prq; lp->cl_next; lp=lp->cl_next); |
| 151 | cp = lp->cl_prq; |
| 152 | } |
| 153 | TALLOC(sp,1,STRING); |
| 154 | sp->st_string = DmStrDup(tok2); |
| 155 | sp->st_next = cp->ce_cond; |
| 156 | cp->ce_cond = sp; |
| 157 | |
| 158 | tok1 = NIL(char); |
| 159 | } |
| 160 | } |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (tok1) { |
| 165 | if ( LastMacName != NIL(char) ) |
| 166 | FREE( LastMacName ); |
| 167 | |
| 168 | LastMacName = tok1; |
| 169 | } |
| 170 | |
| 171 | DB_RETURN( 1 ); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | PUBLIC int |
| 177 | Macro_op( op )/* |
| 178 | ================ |
| 179 | Check the passed in op string and map it to one of the macro operators */ |
| 180 | char *op; |
| 181 | { |
| 182 | int ret = 0; |
| 183 | DB_ENTER( "macro_op" ); |
| 184 | |
| 185 | if ( *op == '!' ) { |
| 186 | ret = M_OP_SI; |
| 187 | op++; |
| 188 | } |
| 189 | |
| 190 | switch( *op ) { |
| 191 | case '=': ret |= M_OP_EQ; break; |
| 192 | case ':': ret |= M_OP_CL; op++; break; |
| 193 | |
| 194 | case '+': |
| 195 | op++; |
| 196 | if( *op == ':' ) { |
| 197 | ret |= M_OP_PLCL; |
| 198 | op++; |
| 199 | } |
| 200 | else { |
| 201 | ret |= M_OP_PL; |
| 202 | } |
| 203 | break; |
| 204 | |
| 205 | case '*': |
| 206 | op++; |
| 207 | if( *op == ':' ) { |
| 208 | ret |= M_OP_DFCL; |
| 209 | op++; |
| 210 | } |
| 211 | else { |
| 212 | ret |= M_OP_DF; |
| 213 | } |
| 214 | break; |
| 215 | |
| 216 | case '?': |
| 217 | ret |= M_OP_CM; |
| 218 | op++; |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | if( *op != '=' ) |
| 223 | ret = 0; |
| 224 | else { |
| 225 | op++; |
| 226 | |
| 227 | if( *op != '\0' ) |
| 228 | ret = 0; |
| 229 | } |
| 230 | |
| 231 | DB_RETURN( ret ); |
| 232 | } |