1 /* Small compiler - maintenance of various lists
6 * Copyright (c) ITB CompuPhase, 2001-2003
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.
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:
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.
35 #include "embryo_cc_sc.h"
38 insert_stringpair(stringpair * root, char *first, char *second, int matchlength)
40 stringpair *cur, *pred;
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))))
48 cur->first = strdup(first);
49 cur->second = strdup(second);
50 cur->matchlength = matchlength;
51 if (!cur->first || !cur->second)
60 /* link the node to the tree, find the position */
61 for (pred = root; pred->next && strcmp(pred->next->first, first) < 0;
64 cur->next = pred->next;
70 delete_stringpairtable(stringpair * root)
72 stringpair *cur, *next;
79 assert(cur->first != NULL);
80 assert(cur->second != NULL);
86 memset(root, 0, sizeof(stringpair));
90 find_stringpair(stringpair * cur, char *first, int matchlength)
94 assert(matchlength > 0); /* the function cannot handle zero-length comparison */
95 assert(first != NULL);
96 while (cur && result <= 0)
98 result = (int)*cur->first - (int)*first;
99 if (result == 0 && matchlength == cur->matchlength)
101 result = strncmp(cur->first, first, matchlength);
111 delete_stringpair(stringpair * root, stringpair * item)
115 assert(root != NULL);
119 if (cur->next == item)
121 cur->next = item->next; /* unlink from list */
122 assert(item->first != NULL);
123 assert(item->second != NULL);
134 /* ----- alias table --------------------------------------------- */
135 static stringpair alias_tab = { NULL, NULL, NULL, 0 }; /* alias table */
138 insert_alias(char *name, char *alias)
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) */
152 lookup_alias(char *target, char *name)
155 find_stringpair(alias_tab.next, name, strlen(name));
158 assert(strlen(cur->second) <= sEXPMAX);
159 strcpy(target, cur->second);
165 delete_aliastable(void)
167 delete_stringpairtable(&alias_tab);
170 /* ----- include paths list -------------------------------------- */
171 static stringlist includepaths = { NULL, NULL }; /* directory list for include files */
174 insert_path(char *path)
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;
191 stringlist *cur = includepaths.next;
193 while (cur && index-- > 0)
197 assert(cur->line != NULL);
204 delete_pathtable(void)
206 stringlist *cur = includepaths.next, *next;
211 assert(cur->line != NULL);
216 memset(&includepaths, 0, sizeof(stringlist));
219 /* ----- text substitution patterns ------------------------------ */
221 static stringpair substpair = { NULL, NULL, NULL, 0 }; /* list of substitution pairs */
222 static stringpair *substindex['z' - 'A' + 1]; /* quick index to first character */
229 assert((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_');
230 assert('A' < '_' && '_' < 'z');
232 for (cur = substpair.next; cur && cur->first[0] != c;
235 substindex[(int)c - 'A'] = cur;
239 insert_subst(char *pattern, char *substitution, int prefixlen)
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);
252 find_subst(char *name, int length)
256 assert(name != NULL);
258 assert((*name >= 'A' && *name <= 'Z') || (*name >= 'a' && *name <= 'z')
260 item = substindex[(int)*name - 'A'];
262 item = find_stringpair(item, name, length);
267 delete_subst(char *name, int length)
271 assert(name != NULL);
273 assert((*name >= 'A' && *name <= 'Z') || (*name >= 'a' && *name <= 'z')
275 item = substindex[(int)*name - 'A'];
277 item = find_stringpair(item, name, length);
280 delete_stringpair(&substpair, item);
286 delete_substtable(void)
290 delete_stringpairtable(&substpair);
291 for (i = 0; i < (int)(sizeof(substindex) / sizeof(substindex[0])); i++)
292 substindex[i] = NULL;