source: tags/initial/AISC/aisc_var_ref.c

Last change on this file was 2, checked in by oldcode, 23 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <malloc.h>
5#include "aisc.h"
6#include "aisc_proto.h"
7
8AD             *
9aisc_match(AD * var, char *varid, char *varct)
10{
11        if (varid) {
12                if (strcmp(var->key, varid)) {
13                        return 0;
14                }
15        }
16        if (varct) {
17                if (var->sub) {
18                        return 0;
19                }
20                if (strcmp(var->val, varct)) {
21                        return 0;
22                }
23        }
24        return var;
25}
26#define NEXT_ITEM(se,extended) if (extended) {\
27                        if (se->next_item) se = se->next_item;\
28                        else se=se->first_item->next_line;\
29                }else{\
30                        se = se->next_item;\
31                }
32
33AD             *
34aisc_find_var_hier(AD * cursor, char *str, int next, int extended, int goup)
35{
36        char           *slash;
37        AD             *found;
38        if (*str == '/') {
39                cursor = 0;
40                str++;
41        }
42        slash = strchr(str, '/');
43        found = 0;
44        while (slash) {
45                *(slash++) = 0;
46                found = aisc_find_var(cursor, str, next, extended, goup);
47                if (!found)
48                        return 0;
49                if (!found->sub) {
50                        return 0;
51                }
52                cursor = found->sub;
53                goup = 0;
54                extended = 1;
55                str = slash;
56                slash = strchr(str, '/');
57                next = 0;
58        }
59        found = aisc_find_var(cursor, str, next, extended, goup);
60        return found;
61
62}
63
64AD             *
65aisc_find_var(AD * cursor, char *str, int next, int extended, int goup)
66{                               /* if next = 1 search next entry else search
67                                 * all; if extended != 0 search in parallel
68                                 * sentences, if goup search whole upper
69                                 * hierarchy     */
70        AD             *se, *line;
71        AD             *found;
72        char           *varid, *varct;
73        char           *pp, *buf;
74        found = 0;
75
76        if (cursor) {
77                line = cursor;
78        } else {
79                line = gl->root;
80        }
81
82        pp = strchr(str, '.');
83        if (!pp) {
84                varid = strdup(str);
85                varct = 0;
86        } else {
87                buf = strdup(str);
88                pp = strchr(buf, '.');
89                *(pp++) = 0;
90                if ((!strcmp(buf, "*")) || (!buf[0])) {
91                        varid = 0;
92                } else {
93                        varid = strdup(buf);
94                }
95                if ((!strcmp(pp, "*")) || (!pp[0])) {
96                        varct = 0;
97                } else {
98                        varct = strdup(pp);
99                }
100                free(buf);
101        }
102        while (line && !found) {
103                if (next) {
104                        NEXT_ITEM(line, extended);
105                } else {
106                        if (extended) {
107                                line = line->first_item->first_line;
108                        } else {
109                                line = line->first_item;
110                        }
111                }
112                for (se = line; se;) {
113                        if ( (found = aisc_match(se, varid, varct))) {
114                                break;
115                        }
116                        NEXT_ITEM(se, extended);
117                }
118                if (goup) {
119                        line = line->first_item->first_line->father;
120                } else {
121                        line = 0;
122                }
123        }
124        if (varid)
125                free(varid);
126        if (varct)
127                free(varct);
128        return found;
129}
130
131
132char           *
133get_var_string(char *var)
134/* Berechnet zu einem Ausdruch der Form $(IDENT:fdg|"sdfg") das Ergebnis */
135{
136        AD             *cur;
137        char           *doppelpunkt;
138        char           *bar;
139        char           *nextdp;
140        char           *finds;
141        int             len;
142        int             findl;
143        int             replacel;
144        int             offset;
145        int             use_path;
146        char           *in, *buf1, *buf2;
147
148
149
150        READ_SPACES(var);
151        use_path = 0;
152        while (var[0] == '&') {
153                use_path++;
154                var++;
155                READ_SPACES(var);
156        }
157        doppelpunkt = strchr(var, ':');
158        if (doppelpunkt)
159                *(doppelpunkt++) = 0;
160        bar = strchr(var, '|');
161        if (bar)
162                *(bar++) = 0;
163        in = read_hash_local(var,0);
164        if (in) {
165                in = strdup(in);
166        } else {
167                cur = aisc_find_var_hier(gl->cursor, var, 0, 0, 1);
168                if (!cur) {
169                        if (!bar) {
170                                if (gl->pc->command == IF) {
171                                        return 0;
172                                }
173                                fprintf(stderr, "%s: ", var);
174                                print_error("Ident not found");
175                                return 0;
176                        }
177                        return strdup(bar);
178                }
179                if (use_path) {
180                        in = strdup(cur->key);
181                        if (use_path == 1) {
182                        } else {
183                                while (cur->first_item->first_line->father) {
184                                        cur = cur->first_item->first_line->father;
185                                        len = strlen(in) + strlen(cur->key);
186                                        buf1 = (char *) calloc(sizeof(char), len + 2);
187                                        sprintf(buf1, "%s/%s", cur->key, in);
188                                        free(in);
189                                        in = buf1;
190                                }
191
192                        }
193                } else {
194                        if (cur->sub) {
195                                fprintf(stderr, "%s: ", var);
196                                print_error("Ident is a Hierarchical Type");
197                                return 0;
198                        } else {
199                                if (cur->val) {
200                                        in = strdup(cur->val);
201                                }else{
202                                        in = strdup("");
203                                }
204                        }
205                }
206        }
207        if (!doppelpunkt)
208                return in;
209        len = strlen(in);
210        while (doppelpunkt) {
211                nextdp = strchr(doppelpunkt, ':');
212                if (nextdp)
213                        *(nextdp++) = 0;
214                if (!doppelpunkt[0]) {
215                        print_error("Ident Replacement is missing an '='");
216                        return 0;
217                }
218
219                bar = strchr(doppelpunkt+1, '=');
220                if (bar) {
221                        *(bar++) = 0;
222                } else {
223                        print_error("Ident Replacement is missing an '='");
224                        return 0;
225                }
226                findl = strlen(doppelpunkt);
227                replacel = strlen(bar);
228                for (finds = strstr(in, doppelpunkt); finds; finds = strstr(buf2, doppelpunkt)) {
229                        len += replacel - findl;
230                        buf1 = (char *) calloc(sizeof(char), len + 1);
231                        offset = finds - in;
232                        memcpy(buf1, in, offset);
233                        memcpy(buf1 + offset, bar, replacel);
234                        buf2 = buf1 + offset + replacel;
235                        memcpy(buf2, in + offset + findl, len - replacel - offset);
236                        free(in);
237                        in = buf1;
238                        buf2 = in + offset + replacel;
239                }
240                doppelpunkt = nextdp;
241        }
242        return in;
243}
Note: See TracBrowser for help on using the repository browser.