Mercurial > projects > pwm
annotate macro.c @ 27:722a45b4028b
Add define command for defining macros
Macros are parsed when they are defined with the D command and can
subsequently be used as arguments for other commands.
Handle out of memory errors directly in tok.c.
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Mon, 25 Sep 2017 21:21:25 +0200 |
parents | |
children |
rev | line source |
---|---|
27
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
1 /* |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
3 * |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
4 * Permission is hereby granted, free of charge, to any person obtaining |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
5 * a copy of this software and associated documentation files (the |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
6 * "Software"), to deal in the Software without restriction, including |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
7 * without limitation the rights to use, copy, modify, merge, publish, |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
8 * distribute, sublicense, and/or sell copies of the Software, and to |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
9 * permit persons to whom the Software is furnished to do so, subject to |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
10 * the following conditions: |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
11 * |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
13 * in all copies or substantial portions of the Software. |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
14 * |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
22 */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
23 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
24 #include "compat.h" |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
25 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
26 #include <ctype.h> |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
27 #ifdef HAVE_QUEUE_H |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
28 #include <sys/queue.h> |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
29 #endif /* HAVE_QUEUE_H */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
30 #include <stdlib.h> |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
31 #include <string.h> |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
32 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
33 #include "macro.h" |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
34 #include "tok.h" |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
35 #include "util.h" |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
36 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
37 TAILQ_HEAD(macro_head, macro_entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
38 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
39 struct macro_entry { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
40 TAILQ_ENTRY(macro_entry) entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
41 char *name; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
42 int argc; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
43 char **argv; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
44 }; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
45 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
46 static struct macro_entry * |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
47 match_macro(struct macro_head *head, const char *name) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
48 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
49 struct macro_entry *entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
50 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
51 TAILQ_FOREACH(entry, head, entry) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
52 if (strcmp(entry->name, name) == 0) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
53 return (entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
54 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
55 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
56 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
57 return (NULL); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
58 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
59 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
60 void |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
61 macro_init(struct macro_head **headp) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
62 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
63 struct macro_head *head; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
64 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
65 head = xmalloc(sizeof (struct macro_head)); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
66 TAILQ_INIT(head); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
67 *headp = head; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
68 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
69 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
70 static void |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
71 free_macro_entry(struct macro_entry *entry) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
72 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
73 int i; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
74 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
75 if (entry == NULL) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
76 return; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
77 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
78 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
79 free(entry->name); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
80 for (i = 0; i < entry->argc; i++) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
81 free(entry->argv[i]); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
82 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
83 free(entry->argv); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
84 free(entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
85 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
86 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
87 void |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
88 macro_destroy(struct macro_head *head) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
89 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
90 struct macro_entry *entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
91 struct macro_entry *tmp_entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
92 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
93 if (head == NULL) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
94 return; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
95 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
96 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
97 TAILQ_FOREACH_SAFE(entry, head, entry, tmp_entry) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
98 TAILQ_REMOVE(head, entry, entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
99 free_macro_entry(entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
100 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
101 free(head); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
102 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
103 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
104 enum macro_err |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
105 macro_parse(const char *name, size_t tokenc, union tok **tokenv, |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
106 struct macro_head *head, struct macro_entry **macro_entryp) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
107 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
108 const char *p = name; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
109 int argc; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
110 char **argv; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
111 struct macro_entry *entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
112 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
113 /* validate macro name */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
114 for (p = name; *p != '\0'; p++) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
115 /* |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
116 * macro names must contain only ASCII alphanumeric characters |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
117 * and underscores |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
118 */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
119 if (!isascii(*p) || (!isalnum(*p) && (*p != '_'))) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
120 return (MACRO_ERR_INVALID_NAME); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
121 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
122 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
123 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
124 /* expand macros */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
125 if (macro_expand_macros(head, tokenc, tokenv, &argc, &argv) != 0) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
126 return (MACRO_ERR_UNDEFINED_MACRO); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
127 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
128 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
129 /* create macro */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
130 entry = xmalloc(sizeof (struct macro_entry)); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
131 entry->name = xstrdup(name); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
132 entry->argc = argc; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
133 entry->argv = argv; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
134 *macro_entryp = entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
135 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
136 return (MACRO_ERR_OK); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
137 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
138 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
139 void |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
140 macro_add(struct macro_head *head, struct macro_entry *entry) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
141 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
142 struct macro_entry *old_entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
143 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
144 /* remove existing macro with the same name */ |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
145 old_entry = match_macro(head, entry->name); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
146 if (old_entry != NULL) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
147 TAILQ_REMOVE(head, old_entry, entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
148 free_macro_entry(old_entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
149 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
150 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
151 TAILQ_INSERT_TAIL(head, entry, entry); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
152 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
153 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
154 int |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
155 macro_expand_macros(struct macro_head *head, size_t tokenc, union tok **tokenv, |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
156 int *argcp, char ***argvp) |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
157 { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
158 int retval = -1; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
159 char **argv = NULL; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
160 int argc = 0; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
161 size_t argv_size = tokenc + 1; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
162 size_t i; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
163 struct macro_entry *entry; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
164 int j; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
165 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
166 argv = xmalloc(argv_size * sizeof (char *)); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
167 argv[0] = NULL; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
168 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
169 for (i = 0; i < tokenc; i++) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
170 switch (tokenv[i]->any.type) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
171 case TOK_ARG: |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
172 argv[argc++] = xstrdup(tokenv[i]->arg.value); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
173 argv[argc] = NULL; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
174 break; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
175 case TOK_MACRO: |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
176 entry = match_macro(head, tokenv[i]->macro.name); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
177 if (entry == NULL) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
178 goto out; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
179 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
180 argv_size += entry->argc; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
181 argv = xrealloc(argv, argv_size * sizeof (char *)); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
182 for (j = 0; j < entry->argc; j++) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
183 argv[argc++] = xstrdup(entry->argv[j]); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
184 argv[argc] = NULL; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
185 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
186 break; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
187 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
188 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
189 *argvp = argv; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
190 *argcp = argc; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
191 retval = 0; |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
192 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
193 out: |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
194 if (retval != 0) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
195 for (j = 0; j < argc; j++) { |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
196 free(argv[j]); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
197 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
198 free(argv); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
199 } |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
200 |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
201 return (retval); |
722a45b4028b
Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
202 } |