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])))
29 static int byte_handler(const char *args)
35 static int end_handler(const char *args)
41 static int endi_handler(const char *args)
47 static int endt_handler(const char *args)
53 static int fstr_handler(const char *args)
59 static int funct_handler(const char *args)
65 static int gstr_handler(const char *args)
71 static int gvar_handler(const char *args)
77 static int insert_handler(const char *args)
83 static int len_handler(const char *args)
89 static int new_handler(const char *args)
95 static int object_handler(const char *args)
101 static int prop_handler(const char *args)
107 static int str_handler(const char *args)
113 static int strl_handler(const char *args)
119 static int table_handler(const char *args)
125 static int vocbeg_handler(const char *args)
131 static int vocend_handler(const char *args)
137 static int word_handler(const char *args)
143 static int zword_handler(const char *args)
150 static Directive Directives[] = {
151 "BYTE", byte_handler,
153 "ENDI", endi_handler,
154 "ENDT", endt_handler,
155 "FSTR", fstr_handler,
156 "FUNCT", funct_handler,
157 "GSTR", gstr_handler,
158 "GVAR", gvar_handler,
159 "INSERT", insert_handler,
162 "OBJECT", object_handler,
163 "PROP", prop_handler,
165 "STRL", strl_handler,
166 "TABLE", table_handler,
167 "VOCBEG", vocbeg_handler,
168 "VOCEND", vocend_handler,
169 "WORD", word_handler,
170 "ZWORD", zword_handler
174 const char *contents;
178 static int namecmp(const void *key, const void *elem)
180 const Name *p = (Name *)key;
181 const Directive *d = (Directive*)elem;
183 int len1 = p->length;
184 int len2 = strlen(d->name);
186 int rc = memcmp(p->contents, elem, len1 < len2 ? len1 : len2);
187 return rc ? rc : (len1 - len2);
190 Directive_handler directive_lookup(const char *name, unsigned namelen)
192 Name n = { name, namelen };
193 Directive *p = (Directive*)bsearch(&n, Directives, ARRAY_SIZE(Directives), sizeof(Directive), namecmp);
194 return p ? p->handler : NULL;