| 1 |
|
| /* ------------------------------------------------------------------------ |
| 2 |
|
| @NAME : macros.c |
| 3 |
|
| @DESCRIPTION: Macro (@string) tables. A table is a hash keyed on the |
| 4 |
|
| case-folded macro name (macro names are case-insensitive). |
| 5 |
|
| A table may have a parent: lookups that fail in a table fall |
| 6 |
|
| back to its parent, but definitions and deletions only ever |
| 7 |
|
| touch the table itself. This lets a file keep its @string |
| 8 |
|
| definitions private while still seeing the globally defined |
| 9 |
|
| macros (eg. month names). |
| 10 |
|
| -------------------------------------------------------------------------- */ |
| 11 |
|
| |
| 12 |
|
| #include <stdlib.h> |
| 13 |
|
| #include <string.h> |
| 14 |
|
| #include "bt_internal.h" |
| 15 |
|
| |
| 16 |
|
| typedef struct macro |
| 17 |
|
| { |
| 18 |
|
| char * key; /* case-folded name */ |
| 19 |
|
| char * text; /* may be NULL */ |
| 20 |
|
| unsigned long hash; |
| 21 |
|
| struct macro * next; |
| 22 |
|
| } macro; |
| 23 |
|
| |
| 24 |
|
| struct bt_macros |
| 25 |
|
| { |
| 26 |
|
| macro ** buckets; |
| 27 |
|
| size_t nbuckets; |
| 28 |
|
| size_t count; |
| 29 |
|
| bt_macros * parent; |
| 30 |
|
| }; |
| 31 |
|
| |
| 32 |
|
| static unsigned long |
| 33 |
304 |
| hash_key (const char *s) |
| 34 |
|
| { |
| 35 |
304 |
| unsigned long h = 5381; /* djb2 */ |
| 36 |
1246 |
▶ | while (*s) |
Branch: 1 branch
| Branch | True | False | Total |
| while (*s) |
942 |
304 |
1246 |
|
| 37 |
942 |
| h = h * 33 + (unsigned char) *s++; |
| 38 |
304 |
| return h; |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| bt_macros * |
| 42 |
19 |
| bt_macros_new (bt_macros *parent) |
| 43 |
|
| { |
| 44 |
19 |
| bt_macros * m = bt_xcalloc (1, sizeof (bt_macros)); |
| 45 |
|
| |
| 46 |
19 |
| m->nbuckets = 64; |
| 47 |
19 |
| m->buckets = bt_xcalloc (m->nbuckets, sizeof (macro *)); |
| 48 |
19 |
| m->parent = parent; |
| 49 |
19 |
| return m; |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
|
| void |
| 53 |
1 |
| bt_delete_all_macros (bt_macros *m) |
| 54 |
|
| { |
| 55 |
|
| size_t i; |
| 56 |
|
| |
| 57 |
65 |
▶ | for (i = 0; i < m->nbuckets; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m->nbuckets; i++) |
64 |
1 |
65 |
|
| 58 |
|
| { |
| 59 |
64 |
| macro * cur = m->buckets[i]; |
| 60 |
65 |
▶ | while (cur) |
Branch: 1 branch
| Branch | True | False | Total |
| while (cur) |
1 |
64 |
65 |
|
| 61 |
|
| { |
| 62 |
1 |
| macro * next = cur->next; |
| 63 |
1 |
| free (cur->key); |
| 64 |
1 |
| free (cur->text); |
| 65 |
1 |
| free (cur); |
| 66 |
1 |
| cur = next; |
| 67 |
|
| } |
| 68 |
64 |
| m->buckets[i] = NULL; |
| 69 |
|
| } |
| 70 |
1 |
| m->count = 0; |
| 71 |
1 |
| } |
| 72 |
|
| |
| 73 |
|
| void |
| 74 |
35 |
| bt_macros_free (bt_macros *m) |
| 75 |
|
| { |
| 76 |
35 |
▶ | if (!m) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) |
34 |
1 |
35 |
|
| 77 |
34 |
| return; |
| 78 |
1 |
| bt_delete_all_macros (m); |
| 79 |
1 |
| free (m->buckets); |
| 80 |
1 |
| free (m); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| static void |
| 84 |
229 |
| insert (bt_macros *m, char *key, unsigned long hash, const char *text) |
| 85 |
|
| { |
| 86 |
|
| macro * rec; |
| 87 |
|
| size_t b; |
| 88 |
|
| |
| 89 |
229 |
▶ | if (m->count >= m->nbuckets * 2) /* grow */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->count >= m->nbuckets * 2) /* grow */ |
0 |
229 |
229 |
|
| 90 |
|
| { |
| 91 |
0 |
| size_t newn = m->nbuckets * 4; |
| 92 |
0 |
| macro ** nb = bt_xcalloc (newn, sizeof (macro *)); |
| 93 |
|
| size_t i; |
| 94 |
|
| |
| 95 |
0 |
▶ | for (i = 0; i < m->nbuckets; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m->nbuckets; i++) |
0 |
0 |
0 |
|
| 96 |
|
| { |
| 97 |
0 |
| macro * cur = m->buckets[i]; |
| 98 |
0 |
▶ | while (cur) |
Branch: 1 branch
| Branch | True | False | Total |
| while (cur) |
0 |
0 |
0 |
|
| 99 |
|
| { |
| 100 |
0 |
| macro * next = cur->next; |
| 101 |
0 |
| cur->next = nb[cur->hash % newn]; |
| 102 |
0 |
| nb[cur->hash % newn] = cur; |
| 103 |
0 |
| cur = next; |
| 104 |
|
| } |
| 105 |
|
| } |
| 106 |
0 |
| free (m->buckets); |
| 107 |
0 |
| m->buckets = nb; |
| 108 |
0 |
| m->nbuckets = newn; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
229 |
| rec = bt_xmalloc (sizeof (macro)); |
| 112 |
229 |
| rec->key = key; |
| 113 |
229 |
| rec->text = bt_xstrdup (text); |
| 114 |
229 |
| rec->hash = hash; |
| 115 |
229 |
| b = hash % m->nbuckets; |
| 116 |
229 |
| rec->next = m->buckets[b]; |
| 117 |
229 |
| m->buckets[b] = rec; |
| 118 |
229 |
| m->count++; |
| 119 |
229 |
| } |
| 120 |
|
| |
| 121 |
|
| bt_macros * |
| 122 |
0 |
| bt_macros_clone (const bt_macros *m, bt_macros *parent) |
| 123 |
|
| { |
| 124 |
0 |
| bt_macros * c = bt_macros_new (parent); |
| 125 |
|
| size_t i; |
| 126 |
|
| |
| 127 |
0 |
▶ | for (i = 0; i < m->nbuckets; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m->nbuckets; i++) |
0 |
0 |
0 |
|
| 128 |
|
| { |
| 129 |
|
| macro * cur; |
| 130 |
0 |
▶ | for (cur = m->buckets[i]; cur; cur = cur->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (cur = m->buckets[i]; cur; cur = cur->next) |
0 |
0 |
0 |
|
| 131 |
0 |
| insert (c, bt_xstrdup (cur->key), cur->hash, cur->text); |
| 132 |
|
| } |
| 133 |
0 |
| return c; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| /* Find a macro in this table only; *prevp gets the link pointing to it */ |
| 137 |
|
| static macro * |
| 138 |
305 |
| find_local (bt_macros *m, const char *key, unsigned long hash, macro ***prevp) |
| 139 |
|
| { |
| 140 |
305 |
| macro ** link = &m->buckets[hash % m->nbuckets]; |
| 141 |
|
| |
| 142 |
313 |
▶ | while (*link) |
Branch: 1 branch
| Branch | True | False | Total |
| while (*link) |
56 |
257 |
313 |
|
| 143 |
|
| { |
| 144 |
56 |
▶ | if ((*link)->hash == hash && strcmp ((*link)->key, key) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((*link)->hash == hash && strcmp ((*link)->key, key) == 0) |
48 |
8 |
56 |
| if ((*link)->hash == hash && strcmp ((*link)->key, key) == 0) |
48 |
0 |
48 |
|
| 145 |
|
| { |
| 146 |
48 |
▶ | if (prevp) *prevp = link; |
Branch: 1 branch
| Branch | True | False | Total |
| if (prevp) *prevp = link; |
15 |
33 |
48 |
|
| 147 |
48 |
| return *link; |
| 148 |
|
| } |
| 149 |
8 |
| link = &(*link)->next; |
| 150 |
|
| } |
| 151 |
257 |
| return NULL; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
|
| static macro * |
| 155 |
60 |
| find (bt_macros *m, const char *name) |
| 156 |
|
| { |
| 157 |
60 |
| char * key = bt_uni_map_string (name, BT_CASE_FOLD); |
| 158 |
60 |
| unsigned long hash = hash_key (key); |
| 159 |
60 |
| macro * rec = NULL; |
| 160 |
|
| |
| 161 |
121 |
▶ | for (; m && !rec; m = m->parent) |
Branch: 2 branches
| Branch | True | False | Total |
| for (; m && !rec; m = m->parent) |
62 |
59 |
121 |
| for (; m && !rec; m = m->parent) |
61 |
1 |
62 |
|
| 162 |
61 |
| rec = find_local (m, key, hash, NULL); |
| 163 |
60 |
| free (key); |
| 164 |
60 |
| return rec; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| void |
| 168 |
240 |
| bt_add_macro_text (bt_macros *m, bt_errors *errs, |
| 169 |
|
| const char *name, const char *text, |
| 170 |
|
| const char *filename, int line) |
| 171 |
|
| { |
| 172 |
240 |
| char * key = bt_uni_map_string (name, BT_CASE_FOLD); |
| 173 |
240 |
| unsigned long hash = hash_key (key); |
| 174 |
|
| macro ** link; |
| 175 |
|
| macro * rec; |
| 176 |
|
| |
| 177 |
240 |
▶ | if ((rec = find_local (m, key, hash, &link))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rec = find_local (m, key, hash, &link))) |
11 |
229 |
240 |
|
| 178 |
|
| { |
| 179 |
11 |
| bt_report (errs, BTERR_CONTENT, filename, line, NULL, -1, |
| 180 |
|
| "overriding existing definition of macro \"%s\"", name); |
| 181 |
11 |
| free (rec->text); |
| 182 |
11 |
| rec->text = bt_xstrdup (text); |
| 183 |
11 |
| free (key); |
| 184 |
11 |
| return; |
| 185 |
|
| } |
| 186 |
229 |
| insert (m, key, hash, text); |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| /* Define a macro from a "name = value" AST node (from an @string entry). |
| 190 |
|
| * If the value has not already been post-processed as a macro value |
| 191 |
|
| * (macros expanded, substrings pasted, whitespace not collapsed) that is |
| 192 |
|
| * done here before the text is stored. */ |
| 193 |
|
| void |
| 194 |
21 |
| bt_add_macro_value (bt_macros *m, bt_errors *errs, |
| 195 |
|
| AST *assignment, btshort options) |
| 196 |
|
| { |
| 197 |
|
| AST * value; |
| 198 |
|
| char * text; |
| 199 |
|
| boolean free_text; |
| 200 |
|
| |
| 201 |
21 |
▶ | if (assignment == NULL || assignment->down == NULL) |
Branch: 2 branches
| Branch | True | False | Total |
| if (assignment == NULL || assignment->down == NULL) |
21 |
0 |
21 |
| if (assignment == NULL || assignment->down == NULL) |
0 |
21 |
21 |
|
| 202 |
0 |
| return; |
| 203 |
21 |
| value = assignment->down; |
| 204 |
|
| |
| 205 |
21 |
▶ | if ((options & BTO_STRINGMASK) != BTO_MACRO) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((options & BTO_STRINGMASK) != BTO_MACRO) |
0 |
21 |
21 |
|
| 206 |
|
| { |
| 207 |
0 |
| text = bt_postprocess_field (m, errs, assignment, BTO_MACRO, FALSE); |
| 208 |
0 |
| free_text = TRUE; |
| 209 |
|
| } |
| 210 |
|
| else |
| 211 |
|
| { |
| 212 |
21 |
▶ | if (value->nodetype != BTAST_STRING || value->right != NULL) |
Branch: 2 branches
| Branch | True | False | Total |
| if (value->nodetype != BTAST_STRING || value->right != NULL) |
21 |
0 |
21 |
| if (value->nodetype != BTAST_STRING || value->right != NULL) |
0 |
21 |
21 |
|
| 213 |
|
| { |
| 214 |
0 |
| bt_report (errs, BTERR_INTERNAL, NULL, -1, NULL, -1, |
| 215 |
|
| "add_macro: macro value was not correctly preprocessed"); |
| 216 |
0 |
| return; |
| 217 |
|
| } |
| 218 |
21 |
| text = value->text; |
| 219 |
21 |
| free_text = FALSE; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
21 |
| bt_add_macro_text (m, errs, assignment->text, text, |
| 223 |
|
| assignment->filename, assignment->line); |
| 224 |
21 |
▶ | if (free_text) |
Branch: 1 branch
| Branch | True | False | Total |
| if (free_text) |
0 |
21 |
21 |
|
| 225 |
0 |
| free (text); |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| void |
| 229 |
4 |
| bt_delete_macro (bt_macros *m, const char *name) |
| 230 |
|
| { |
| 231 |
4 |
| char * key = bt_uni_map_string (name, BT_CASE_FOLD); |
| 232 |
4 |
| unsigned long hash = hash_key (key); |
| 233 |
|
| macro ** link; |
| 234 |
|
| macro * rec; |
| 235 |
|
| |
| 236 |
4 |
▶ | if ((rec = find_local (m, key, hash, &link))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((rec = find_local (m, key, hash, &link))) |
4 |
0 |
4 |
|
| 237 |
|
| { |
| 238 |
4 |
| *link = rec->next; |
| 239 |
4 |
| free (rec->key); |
| 240 |
4 |
| free (rec->text); |
| 241 |
4 |
| free (rec); |
| 242 |
4 |
| m->count--; |
| 243 |
|
| } |
| 244 |
4 |
| free (key); |
| 245 |
4 |
| } |
| 246 |
|
| |
| 247 |
|
| int |
| 248 |
10 |
| bt_macro_length (bt_macros *m, const char *name) |
| 249 |
|
| { |
| 250 |
10 |
| macro * rec = find (m, name); |
| 251 |
10 |
▶ | return (rec && rec->text) ? (int) strlen (rec->text) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (rec && rec->text) ? (int) strlen (rec->text) : 0; |
5 |
5 |
10 |
| return (rec && rec->text) ? (int) strlen (rec->text) : 0; |
5 |
0 |
5 |
|
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
|
| const char * |
| 255 |
50 |
| bt_macro_text (bt_macros *m, bt_errors *errs, const char *name, |
| 256 |
|
| const char *filename, int line) |
| 257 |
|
| { |
| 258 |
50 |
| macro * rec = find (m, name); |
| 259 |
|
| |
| 260 |
50 |
▶ | if (!rec) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rec) |
22 |
28 |
50 |
|
| 261 |
|
| { |
| 262 |
22 |
| bt_report (errs, BTERR_CONTENT, filename, line, NULL, -1, |
| 263 |
|
| "undefined macro \"%s\"", name); |
| 264 |
22 |
| return NULL; |
| 265 |
|
| } |
| 266 |
28 |
| return rec->text; |
| 267 |
|
| } |