| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /** @file lib/macros.h |
| 20 | * @brief Macro expansion |
| 21 | */ |
| 22 | |
| 23 | #ifndef MACROS_H |
| 24 | #define MACROS_H |
| 25 | |
| 26 | struct sink; |
| 27 | |
| 28 | /** @brief One node in a macro expansion parse tree */ |
| 29 | struct mx_node { |
| 30 | /** @brief Next element or NULL at end of list */ |
| 31 | struct mx_node *next; |
| 32 | |
| 33 | /** @brief Node type, @ref MX_TEXT or @ref MX_EXPANSION. */ |
| 34 | int type; |
| 35 | |
| 36 | /** @brief Filename containing this node */ |
| 37 | const char *filename; |
| 38 | |
| 39 | /** @brief Line number at start of this node */ |
| 40 | int line; |
| 41 | |
| 42 | /** @brief Plain text (if @p type is @ref MX_TEXT) */ |
| 43 | const char *text; |
| 44 | |
| 45 | /** @brief Expansion name (if @p type is @ref MX_EXPANSION) */ |
| 46 | const char *name; |
| 47 | |
| 48 | /** @brief Argument count (if @p type is @ref MX_EXPANSION) */ |
| 49 | int nargs; |
| 50 | |
| 51 | /** @brief Argument values, parsed recursively (or NULL if @p nargs is 0) */ |
| 52 | const struct mx_node **args; |
| 53 | }; |
| 54 | |
| 55 | /** @brief Text node */ |
| 56 | #define MX_TEXT 0 |
| 57 | |
| 58 | /** @brief Expansion node */ |
| 59 | #define MX_EXPANSION 1 |
| 60 | |
| 61 | const struct mx_node *mx_parse(const char *filename, |
| 62 | int line, |
| 63 | const char *input, |
| 64 | const char *end); |
| 65 | char *mx_dump(const struct mx_node *m); |
| 66 | |
| 67 | |
| 68 | /** @brief Callback for simple expansions |
| 69 | * @param nargs Number of arguments |
| 70 | * @param args Pointer to array of arguments |
| 71 | * @param output Where to send output |
| 72 | * @param u User data |
| 73 | * @return 0 on success, non-zero on error |
| 74 | */ |
| 75 | typedef int mx_simple_callback(int nargs, |
| 76 | char **args, |
| 77 | struct sink *output, |
| 78 | void *u); |
| 79 | |
| 80 | /** @brief Callback for magic expansions |
| 81 | * @param nargs Number of arguments |
| 82 | * @param args Pointer to array of arguments |
| 83 | * @param output Where to send output |
| 84 | * @param u User data |
| 85 | * @return 0 on success, non-zero on error |
| 86 | */ |
| 87 | typedef int mx_magic_callback(int nargs, |
| 88 | const struct mx_node **args, |
| 89 | struct sink *output, |
| 90 | void *u); |
| 91 | |
| 92 | void mx_register(const char *name, |
| 93 | int min, |
| 94 | int max, |
| 95 | mx_simple_callback *callback); |
| 96 | void mx_register_magic(const char *name, |
| 97 | int min, |
| 98 | int max, |
| 99 | mx_magic_callback *callback); |
| 100 | int mx_register_macro(const char *name, |
| 101 | int nargs, |
| 102 | char **args, |
| 103 | const struct mx_node *definition); |
| 104 | |
| 105 | void mx_register_builtin(void); |
| 106 | void mx_search_path(const char *s); |
| 107 | char *mx_find(const char *name, int report); |
| 108 | |
| 109 | int mx_expand_file(const char *path, |
| 110 | struct sink *output, |
| 111 | void *u); |
| 112 | int mx_expand(const struct mx_node *m, |
| 113 | struct sink *output, |
| 114 | void *u); |
| 115 | int mx_expandstr(const struct mx_node *m, |
| 116 | char **sp, |
| 117 | void *u, |
| 118 | const char *what); |
| 119 | const struct mx_node *mx_rewrite(const struct mx_node *definition, |
| 120 | hash *h); |
| 121 | const struct mx_node *mx_rewritel(const struct mx_node *m, |
| 122 | ...); |
| 123 | |
| 124 | int mx_str2bool(const char *s); |
| 125 | const char *mx_bool2str(int n); |
| 126 | int mx_bool_result(struct sink *output, int result); |
| 127 | |
| 128 | #endif /* MACROS_H */ |
| 129 | |
| 130 | |
| 131 | /* |
| 132 | Local Variables: |
| 133 | c-basic-offset:2 |
| 134 | comment-column:40 |
| 135 | fill-column:79 |
| 136 | indent-tabs-mode:nil |
| 137 | End: |
| 138 | */ |