2 * directives.c -- part of ZilUtils/ZilAsm
4 * Copyright (C) 2016 Jason Self <j@jxself.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
19 * SPDX-License-Identifier: AGPL-3.0-or-later
22 #include <stdlib.h> /* bsearch */
23 #include <string.h> /* strcmp */
25 #include "directives.h"
27 #define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
30 byte_handler (const char *args)
37 end_handler (const char *args)
44 endi_handler (const char *args)
51 endt_handler (const char *args)
58 fstr_handler (const char *args)
65 funct_handler (const char *args)
72 gstr_handler (const char *args)
79 gvar_handler (const char *args)
86 insert_handler (const char *args)
93 len_handler (const char *args)
100 new_handler (const char *args)
107 object_handler (const char *args)
114 prop_handler (const char *args)
121 str_handler (const char *args)
128 strl_handler (const char *args)
135 table_handler (const char *args)
142 vocbeg_handler (const char *args)
149 vocend_handler (const char *args)
156 word_handler (const char *args)
163 zword_handler (const char *args)
170 static Directive Directives[] = {
171 "BYTE", byte_handler,
173 "ENDI", endi_handler,
174 "ENDT", endt_handler,
175 "FSTR", fstr_handler,
176 "FUNCT", funct_handler,
177 "GSTR", gstr_handler,
178 "GVAR", gvar_handler,
179 "INSERT", insert_handler,
182 "OBJECT", object_handler,
183 "PROP", prop_handler,
185 "STRL", strl_handler,
186 "TABLE", table_handler,
187 "VOCBEG", vocbeg_handler,
188 "VOCEND", vocend_handler,
189 "WORD", word_handler,
190 "ZWORD", zword_handler
195 const char *contents;
200 namecmp (const void *key, const void *elem)
202 const Name *p = (Name *) key;
203 const Directive *d = (Directive *) elem;
205 int len1 = p->length;
206 int len2 = strlen (d->name);
208 int rc = memcmp (p->contents, elem, len1 < len2 ? len1 : len2);
209 return rc ? rc : (len1 - len2);
213 directive_lookup (const char *name, unsigned namelen)
215 Name n = { name, namelen };
217 (Directive *) bsearch (&n, Directives, ARRAY_SIZE (Directives),
218 sizeof (Directive), namecmp);
219 return p ? p->handler : NULL;