diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/embryo/src/bin/embryo_cc_sclist.c | 293 |
1 files changed, 293 insertions, 0 deletions
diff --git a/libraries/embryo/src/bin/embryo_cc_sclist.c b/libraries/embryo/src/bin/embryo_cc_sclist.c new file mode 100644 index 0000000..94ebbb7 --- /dev/null +++ b/libraries/embryo/src/bin/embryo_cc_sclist.c | |||
@@ -0,0 +1,293 @@ | |||
1 | /* Small compiler - maintenance of various lists | ||
2 | * | ||
3 | * Name list (aliases) | ||
4 | * Include path list | ||
5 | * | ||
6 | * Copyright (c) ITB CompuPhase, 2001-2003 | ||
7 | * | ||
8 | * This software is provided "as-is", without any express or implied warranty. | ||
9 | * In no event will the authors be held liable for any damages arising from | ||
10 | * the use of this software. | ||
11 | * | ||
12 | * Permission is granted to anyone to use this software for any purpose, | ||
13 | * including commercial applications, and to alter it and redistribute it | ||
14 | * freely, subject to the following restrictions: | ||
15 | * | ||
16 | * 1. The origin of this software must not be misrepresented; you must not | ||
17 | * claim that you wrote the original software. If you use this software in | ||
18 | * a product, an acknowledgment in the product documentation would be | ||
19 | * appreciated but is not required. | ||
20 | * 2. Altered source versions must be plainly marked as such, and must not be | ||
21 | * misrepresented as being the original software. | ||
22 | * 3. This notice may not be removed or altered from any source distribution. | ||
23 | * | ||
24 | * Version: $Id: embryo_cc_sclist.c 52451 2010-09-19 03:00:12Z raster $ | ||
25 | */ | ||
26 | |||
27 | |||
28 | #ifdef HAVE_CONFIG_H | ||
29 | # include <config.h> | ||
30 | #endif | ||
31 | |||
32 | #include <assert.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <string.h> | ||
35 | #include "embryo_cc_sc.h" | ||
36 | |||
37 | static stringpair * | ||
38 | insert_stringpair(stringpair * root, char *first, char *second, int matchlength) | ||
39 | { | ||
40 | stringpair *cur, *pred; | ||
41 | |||
42 | assert(root != NULL); | ||
43 | assert(first != NULL); | ||
44 | assert(second != NULL); | ||
45 | /* create a new node, and check whether all is okay */ | ||
46 | if (!(cur = (stringpair *)malloc(sizeof(stringpair)))) | ||
47 | return NULL; | ||
48 | cur->first = strdup(first); | ||
49 | cur->second = strdup(second); | ||
50 | cur->matchlength = matchlength; | ||
51 | if (!cur->first || !cur->second) | ||
52 | { | ||
53 | if (cur->first) | ||
54 | free(cur->first); | ||
55 | if (cur->second) | ||
56 | free(cur->second); | ||
57 | free(cur); | ||
58 | return NULL; | ||
59 | } /* if */ | ||
60 | /* link the node to the tree, find the position */ | ||
61 | for (pred = root; pred->next && strcmp(pred->next->first, first) < 0; | ||
62 | pred = pred->next) | ||
63 | /* nothing */ ; | ||
64 | cur->next = pred->next; | ||
65 | pred->next = cur; | ||
66 | return cur; | ||
67 | } | ||
68 | |||
69 | static void | ||
70 | delete_stringpairtable(stringpair * root) | ||
71 | { | ||
72 | stringpair *cur, *next; | ||
73 | |||
74 | assert(root != NULL); | ||
75 | cur = root->next; | ||
76 | while (cur) | ||
77 | { | ||
78 | next = cur->next; | ||
79 | assert(cur->first != NULL); | ||
80 | assert(cur->second != NULL); | ||
81 | free(cur->first); | ||
82 | free(cur->second); | ||
83 | free(cur); | ||
84 | cur = next; | ||
85 | } /* while */ | ||
86 | memset(root, 0, sizeof(stringpair)); | ||
87 | } | ||
88 | |||
89 | static stringpair * | ||
90 | find_stringpair(stringpair * cur, char *first, int matchlength) | ||
91 | { | ||
92 | int result = 0; | ||
93 | |||
94 | assert(matchlength > 0); /* the function cannot handle zero-length comparison */ | ||
95 | assert(first != NULL); | ||
96 | while (cur && result <= 0) | ||
97 | { | ||
98 | result = (int)*cur->first - (int)*first; | ||
99 | if (result == 0 && matchlength == cur->matchlength) | ||
100 | { | ||
101 | result = strncmp(cur->first, first, matchlength); | ||
102 | if (result == 0) | ||
103 | return cur; | ||
104 | } /* if */ | ||
105 | cur = cur->next; | ||
106 | } /* while */ | ||
107 | return NULL; | ||
108 | } | ||
109 | |||
110 | static int | ||
111 | delete_stringpair(stringpair * root, stringpair * item) | ||
112 | { | ||
113 | stringpair *cur; | ||
114 | |||
115 | assert(root != NULL); | ||
116 | cur = root; | ||
117 | while (cur->next) | ||
118 | { | ||
119 | if (cur->next == item) | ||
120 | { | ||
121 | cur->next = item->next; /* unlink from list */ | ||
122 | assert(item->first != NULL); | ||
123 | assert(item->second != NULL); | ||
124 | free(item->first); | ||
125 | free(item->second); | ||
126 | free(item); | ||
127 | return TRUE; | ||
128 | } /* if */ | ||
129 | cur = cur->next; | ||
130 | } /* while */ | ||
131 | return FALSE; | ||
132 | } | ||
133 | |||
134 | /* ----- alias table --------------------------------------------- */ | ||
135 | static stringpair alias_tab = { NULL, NULL, NULL, 0 }; /* alias table */ | ||
136 | |||
137 | stringpair * | ||
138 | insert_alias(char *name, char *alias) | ||
139 | { | ||
140 | stringpair *cur; | ||
141 | |||
142 | assert(name != NULL); | ||
143 | assert(strlen(name) <= sNAMEMAX); | ||
144 | assert(alias != NULL); | ||
145 | assert(strlen(alias) <= sEXPMAX); | ||
146 | if (!(cur = insert_stringpair(&alias_tab, name, alias, strlen(name)))) | ||
147 | error(103); /* insufficient memory (fatal error) */ | ||
148 | return cur; | ||
149 | } | ||
150 | |||
151 | int | ||
152 | lookup_alias(char *target, char *name) | ||
153 | { | ||
154 | stringpair *cur = | ||
155 | find_stringpair(alias_tab.next, name, strlen(name)); | ||
156 | if (cur) | ||
157 | { | ||
158 | assert(strlen(cur->second) <= sEXPMAX); | ||
159 | strcpy(target, cur->second); | ||
160 | } /* if */ | ||
161 | return !!cur; | ||
162 | } | ||
163 | |||
164 | void | ||
165 | delete_aliastable(void) | ||
166 | { | ||
167 | delete_stringpairtable(&alias_tab); | ||
168 | } | ||
169 | |||
170 | /* ----- include paths list -------------------------------------- */ | ||
171 | static stringlist includepaths = { NULL, NULL }; /* directory list for include files */ | ||
172 | |||
173 | stringlist * | ||
174 | insert_path(char *path) | ||
175 | { | ||
176 | stringlist *cur; | ||
177 | |||
178 | assert(path != NULL); | ||
179 | if (!(cur = (stringlist *)malloc(sizeof(stringlist)))) | ||
180 | error(103); /* insufficient memory (fatal error) */ | ||
181 | if (!(cur->line = strdup(path))) | ||
182 | error(103); /* insufficient memory (fatal error) */ | ||
183 | cur->next = includepaths.next; | ||
184 | includepaths.next = cur; | ||
185 | return cur; | ||
186 | } | ||
187 | |||
188 | char * | ||
189 | get_path(int index) | ||
190 | { | ||
191 | stringlist *cur = includepaths.next; | ||
192 | |||
193 | while (cur && index-- > 0) | ||
194 | cur = cur->next; | ||
195 | if (cur) | ||
196 | { | ||
197 | assert(cur->line != NULL); | ||
198 | return cur->line; | ||
199 | } /* if */ | ||
200 | return NULL; | ||
201 | } | ||
202 | |||
203 | void | ||
204 | delete_pathtable(void) | ||
205 | { | ||
206 | stringlist *cur = includepaths.next, *next; | ||
207 | |||
208 | while (cur) | ||
209 | { | ||
210 | next = cur->next; | ||
211 | assert(cur->line != NULL); | ||
212 | free(cur->line); | ||
213 | free(cur); | ||
214 | cur = next; | ||
215 | } /* while */ | ||
216 | memset(&includepaths, 0, sizeof(stringlist)); | ||
217 | } | ||
218 | |||
219 | /* ----- text substitution patterns ------------------------------ */ | ||
220 | |||
221 | static stringpair substpair = { NULL, NULL, NULL, 0 }; /* list of substitution pairs */ | ||
222 | static stringpair *substindex['z' - 'A' + 1]; /* quick index to first character */ | ||
223 | |||
224 | static void | ||
225 | adjustindex(char c) | ||
226 | { | ||
227 | stringpair *cur; | ||
228 | |||
229 | assert((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'); | ||
230 | assert('A' < '_' && '_' < 'z'); | ||
231 | |||
232 | for (cur = substpair.next; cur && cur->first[0] != c; | ||
233 | cur = cur->next) | ||
234 | /* nothing */ ; | ||
235 | substindex[(int)c - 'A'] = cur; | ||
236 | } | ||
237 | |||
238 | stringpair * | ||
239 | insert_subst(char *pattern, char *substitution, int prefixlen) | ||
240 | { | ||
241 | stringpair *cur; | ||
242 | |||
243 | assert(pattern != NULL); | ||
244 | assert(substitution != NULL); | ||
245 | if (!(cur = insert_stringpair(&substpair, pattern, substitution, prefixlen))) | ||
246 | error(103); /* insufficient memory (fatal error) */ | ||
247 | adjustindex(*pattern); | ||
248 | return cur; | ||
249 | } | ||
250 | |||
251 | stringpair * | ||
252 | find_subst(char *name, int length) | ||
253 | { | ||
254 | stringpair *item; | ||
255 | |||
256 | assert(name != NULL); | ||
257 | assert(length > 0); | ||
258 | assert((*name >= 'A' && *name <= 'Z') || (*name >= 'a' && *name <= 'z') | ||
259 | || *name == '_'); | ||
260 | item = substindex[(int)*name - 'A']; | ||
261 | if (item) | ||
262 | item = find_stringpair(item, name, length); | ||
263 | return item; | ||
264 | } | ||
265 | |||
266 | int | ||
267 | delete_subst(char *name, int length) | ||
268 | { | ||
269 | stringpair *item; | ||
270 | |||
271 | assert(name != NULL); | ||
272 | assert(length > 0); | ||
273 | assert((*name >= 'A' && *name <= 'Z') || (*name >= 'a' && *name <= 'z') | ||
274 | || *name == '_'); | ||
275 | item = substindex[(int)*name - 'A']; | ||
276 | if (item) | ||
277 | item = find_stringpair(item, name, length); | ||
278 | if (!item) | ||
279 | return FALSE; | ||
280 | delete_stringpair(&substpair, item); | ||
281 | adjustindex(*name); | ||
282 | return TRUE; | ||
283 | } | ||
284 | |||
285 | void | ||
286 | delete_substtable(void) | ||
287 | { | ||
288 | int i; | ||
289 | |||
290 | delete_stringpairtable(&substpair); | ||
291 | for (i = 0; i < (int)(sizeof(substindex) / sizeof(substindex[0])); i++) | ||
292 | substindex[i] = NULL; | ||
293 | } | ||