Line data Source code
1 : /* RCS $Id: macparse.c,v 1.3 2007-10-15 15:40:02 ihi Exp $
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 300340 : 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 */
48 : int toklen; /* length of a token */
49 :
50 : DB_ENTER( "Parse_macro" );
51 :
52 300340 : SET_TOKEN( &input, buffer );
53 300340 : tok1 = Get_token( &input, "=+:*!?", 0 );
54 :
55 300340 : operator=Macro_op(tok1);
56 300340 : if( operator ) {
57 0 : CLEAR_TOKEN( &input );
58 0 : Error( "Assignment without macro name: [%s].", buffer );
59 0 : DB_RETURN( 1 );
60 : }
61 :
62 300340 : tok1 = DmStrDup(tok1);
63 300340 : tok2 = Get_token( &input, "=+:*!?", 2 );
64 300340 : if( !(operator = Macro_op(tok2)) || !strcmp(tok1,".SETDIR") ) {
65 27336 : CLEAR_TOKEN( &input );
66 27336 : FREE(tok1);
67 27336 : DB_RETURN(0);
68 : }
69 :
70 273004 : tok2 = Expand(tok1); FREE(tok1); tok1 = tok2;
71 273004 : if ( !(toklen = strlen(tok1)) ) {
72 0 : Warning( "Empty macro name after expansion: [%s].", buffer );
73 : }
74 :
75 : /* Catch illegal single character macro names. */
76 273004 : if ( toklen == 1 && strchr("{()}", tok1[0]) ) {
77 0 : CLEAR_TOKEN( &input );
78 0 : 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 273004 : if ( strchr(tok1, ':') ) {
83 0 : CLEAR_TOKEN( &input );
84 0 : Fatal( "Syntax error in macro assignment [%s]. The character ':' is not allowed in macro names.", buffer );
85 : }
86 :
87 273004 : tok2 = Get_token(&input, NIL( char ), FALSE);
88 :
89 : /* Make sure we can force the assignment. */
90 273004 : if ( operator & M_OP_SI ) {
91 1798 : flag |= M_FORCE|M_MULTI;
92 1798 : operator &= ~M_OP_SI;
93 : }
94 :
95 273004 : switch( operator ) {
96 : case M_OP_PLCL:
97 890 : 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 14746 : if( (hv = GET_MACRO(tok1)) == NIL(HASH) || hv->ht_value == NIL(char) )
105 4202 : Def_macro( tok1, tok2, flag );
106 : else {
107 10544 : result = DmStrAdd( hv->ht_value, tok2, FALSE );
108 10544 : Def_macro( tok1, result, flag );
109 10544 : FREE( result );
110 : }
111 14746 : if( operator == M_OP_PLCL ) FREE(tok2);
112 14746 : break;
113 :
114 : case M_OP_DF:
115 : /* *= */
116 : /* internal default macros or initialized empty macros set M_INIT. */
117 17006 : if( (hv = GET_MACRO(tok1)) != NIL(HASH) && !(hv->ht_flag & M_INIT) )
118 5020 : break;
119 : /* else FALLTHRU */
120 :
121 : case M_OP_EQ:
122 237994 : Def_macro( tok1, tok2, flag );
123 237994 : break;
124 :
125 : case M_OP_DFCL:
126 : /* *:= */
127 : /* internal default macros or initialized empty macros set M_INIT. */
128 2926 : if( (hv = GET_MACRO(tok1)) != NIL(HASH) && !(hv->ht_flag & M_INIT) )
129 656 : break;
130 : /* else FALLTHRU */
131 :
132 : case M_OP_CL:
133 14588 : tok2 = Expand( tok2 );
134 14588 : Def_macro( tok1, tok2, M_EXPANDED | flag );
135 14588 : FREE( tok2 );
136 14588 : break;
137 :
138 : case M_OP_CM:{
139 : CELLPTR cp;
140 : STRINGPTR sp;
141 :
142 0 : if (flag & M_PUSH) {
143 0 : Error("Nested conditional definition [%s ?= %s] ignored",
144 : tok1, tok2);
145 : }
146 : else {
147 0 : cp = Def_cell(tok1);
148 0 : if (cp->ce_flag & F_MULTI) {
149 : LINKPTR lp;
150 0 : for(lp=cp->ce_prq; lp->cl_next; lp=lp->cl_next);
151 0 : cp = lp->cl_prq;
152 : }
153 0 : TALLOC(sp,1,STRING);
154 0 : sp->st_string = DmStrDup(tok2);
155 0 : sp->st_next = cp->ce_cond;
156 0 : cp->ce_cond = sp;
157 :
158 0 : tok1 = NIL(char);
159 : }
160 : }
161 0 : break;
162 : }
163 :
164 273004 : if (tok1) {
165 273004 : if ( LastMacName != NIL(char) )
166 272820 : FREE( LastMacName );
167 :
168 273004 : LastMacName = tok1;
169 : }
170 :
171 273004 : DB_RETURN( 1 );
172 : }
173 :
174 :
175 :
176 : PUBLIC int
177 600680 : 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 600680 : int ret = 0;
183 : DB_ENTER( "macro_op" );
184 :
185 600680 : if ( *op == '!' ) {
186 1798 : ret = M_OP_SI;
187 1798 : op++;
188 : }
189 :
190 600680 : switch( *op ) {
191 226008 : case '=': ret |= M_OP_EQ; break;
192 36168 : case ':': ret |= M_OP_CL; op++; break;
193 :
194 : case '+':
195 14746 : op++;
196 14746 : if( *op == ':' ) {
197 890 : ret |= M_OP_PLCL;
198 890 : op++;
199 : }
200 : else {
201 13856 : ret |= M_OP_PL;
202 : }
203 14746 : break;
204 :
205 : case '*':
206 19932 : op++;
207 19932 : if( *op == ':' ) {
208 2926 : ret |= M_OP_DFCL;
209 2926 : op++;
210 : }
211 : else {
212 17006 : ret |= M_OP_DF;
213 : }
214 19932 : break;
215 :
216 : case '?':
217 0 : ret |= M_OP_CM;
218 0 : op++;
219 0 : break;
220 : }
221 :
222 600680 : if( *op != '=' )
223 327676 : ret = 0;
224 : else {
225 273004 : op++;
226 :
227 273004 : if( *op != '\0' )
228 0 : ret = 0;
229 : }
230 :
231 600680 : DB_RETURN( ret );
232 : }
|