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/>
20 #include <stdlib.h> /* bsearch */
21 #include <string.h> /* strcmp */
23 #include "directives.h"
25 #define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
27 static int byte_handler(const char *args)
33 static int end_handler(const char *args)
39 static int endi_handler(const char *args)
45 static int endt_handler(const char *args)
51 static int fstr_handler(const char *args)
57 static int funct_handler(const char *args)
63 static int gstr_handler(const char *args)
69 static int gvar_handler(const char *args)
75 static int insert_handler(const char *args)
81 static int len_handler(const char *args)
87 static int new_handler(const char *args)
93 static int object_handler(const char *args)
99 static int prop_handler(const char *args)
105 static int str_handler(const char *args)
111 static int strl_handler(const char *args)
117 static int table_handler(const char *args)
123 static int vocbeg_handler(const char *args)
129 static int vocend_handler(const char *args)
135 static int word_handler(const char *args)
141 static int zword_handler(const char *args)
148 static Directive Directives[] = {
149 "BYTE", byte_handler,
151 "ENDI", endi_handler,
152 "ENDT", endt_handler,
153 "FSTR", fstr_handler,
154 "FUNCT", funct_handler,
155 "GSTR", gstr_handler,
156 "GVAR", gvar_handler,
157 "INSERT", insert_handler,
160 "OBJECT", object_handler,
161 "PROP", prop_handler,
163 "STRL", strl_handler,
164 "TABLE", table_handler,
165 "VOCBEG", vocbeg_handler,
166 "VOCEND", vocend_handler,
167 "WORD", word_handler,
168 "ZWORD", zword_handler
172 const char *contents;
176 static int namecmp(const void *key, const void *elem)
178 const Name *p = (Name *)key;
179 const Directive *d = (Directive*)elem;
181 int len1 = p->length;
182 int len2 = strlen(d->name);
184 int rc = memcmp(p->contents, elem, len1 < len2 ? len1 : len2);
185 return rc ? rc : (len1 - len2);
188 Directive_handler directive_lookup(const char *name, unsigned namelen)
190 Name n = { name, namelen };
191 Directive *p = (Directive*)bsearch(&n, Directives, ARRAY_SIZE(Directives), sizeof(Directive), namecmp);
192 return p ? p->handler : NULL;