underscore expansion
[mala.git] / engine / action.c
blobd12ea6083d8f103b48503a7f8b6d7eab09f30d63
1 /*
2 action.c - MaLa actions implementation
4 Copyright (C) 2004, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
19 #include <assert.h>
20 #include "mala_types.h"
21 #include "strings.h"
22 #include "stringlist.h"
23 #include "actiondesc.h"
24 #include "action.h"
27 int
28 mala_null_parser (MalaEngine eng, void* list, void * data)
30 (void) eng;
31 (void) list;
32 (void) data;
33 return MALA_ENOACTION;
36 MalaAction
37 mala_action_new (MalaString name, void * data,
38 MalaParserFunc parser, MalaDataDestructor destructor)
40 MalaAction self;
42 self = malloc (sizeof (mala_action));
43 if (!self)
44 return NULL;
46 self->name = name; /* weak-reference, not refcounted */
47 self->parser = parser;
48 self->destructor = destructor;
49 self->data = data;
50 self->desc = NULL;
52 LIST_INIT (&self->childs);
53 self->parent = NULL;
55 return self;
58 MalaAction
59 mala_action_new_actioninit (MalaActionInit init, MalaStringBucket bucket)
61 MalaAction self;
62 MalaString name;
63 void * data = NULL;
64 MalaDataDestructor dtor = init->destructor;
66 name = mala_string_new_bucket (init->command, bucket);
67 if (!name)
68 return NULL;
70 if (init->parser == mala_substitute_parser)
72 data = mala_string_new_bucket ((const char *)init->data, bucket);
73 if (!data)
74 goto ealloc;
76 dtor = (MalaDataDestructor) mala_string_free;
78 else if (init->parser == mala_expand_parser)
80 data = mala_stringlist_new_cstrs_bucket ((const char **)init->data, -1, bucket);
81 if (!data)
82 goto ealloc;
84 dtor = (MalaDataDestructor) mala_stringlist_free;
86 else if (init->parser == mala_macro_parser)
88 data = mala_stringlist_new_cstrs_bucket ((const char **)init->data, -1, bucket);
89 if (!data)
90 goto ealloc;
92 dtor = (MalaDataDestructor) mala_stringlist_free;
94 else
95 data = init->data;
97 self = mala_action_new (name, data, init->parser, dtor);
98 if (!self)
99 goto ealloc_action;
101 mala_string_free (name);
103 return self;
105 ealloc_action:
106 dtor (data);
107 ealloc:
108 mala_string_free (name);
109 return 0;
112 MalaAction
113 mala_action_new_brief (MalaActionInit init, MalaAction parent, MalaStringBucket bucket)
115 MalaAction self;
116 MalaString name;
117 MalaStringList cmds;
119 MALA_DEBUG("start");
121 name = mala_string_new_cat_bucket ("--BRIEF.", init->command, bucket);
122 if (!name)
123 return NULL;
125 MALA_DEBUG("catenated");
127 cmds = mala_stringlist_new_cstrs_bucket ((const char *[])
128 {"--PRINT",(const char *) init->brief,0}, 2, bucket);
129 if (!cmds)
131 MALA_DEBUG("stringlist FAILED");
132 goto ealloc_cmds;
134 MALA_DEBUG("stringlist created");
136 self = mala_action_new (name, cmds, mala_expand_parser, (MalaDataDestructor) mala_stringlist_free);
137 if (!self)
138 goto ealloc_action;
140 self->parent = parent;
141 LIST_INSERT_HEAD (&self->childs, self, cldnode);
143 mala_string_free (name);
145 return self;
147 ealloc_action:
148 mala_stringlist_free (cmds);
149 ealloc_cmds:
150 mala_string_free (name);
151 return NULL;
154 MalaAction
155 mala_action_new_help (MalaActionInit init, MalaAction parent, MalaStringBucket bucket)
157 MalaAction self;
158 MalaString name;
159 MalaStringList cmds;
161 MALA_DEBUG("start");
164 name = mala_string_new_cat_bucket ("--HELP.", init->command, bucket);
165 if (!name)
166 return NULL;
168 MALA_DEBUG("catenated");
170 cmds = mala_stringlist_new_cstrs_bucket ((const char **)init->help, -1, bucket);
171 if (!cmds)
173 MALA_DEBUG("stringlist FAILED");
174 goto ealloc_cmds;
176 MALA_DEBUG("stringlist created");
178 self = mala_action_new (name, cmds, mala_expand_parser, (MalaDataDestructor) mala_stringlist_free);
179 if (!self)
180 goto ealloc_action;
182 self->parent = parent;
183 LIST_INSERT_HEAD (&self->childs, self, cldnode);
185 mala_string_free (name);
187 return self;
189 ealloc_action:
190 mala_stringlist_free (cmds);
191 ealloc_cmds:
192 mala_string_free (name);
193 return NULL;
198 mala_action_attach (MalaAction self)
200 MalaActionDesc desc;
202 desc = mala_actiondesc_ensure (self->name);
203 if (!desc)
204 return MALA_EALLOC;
206 mala_actiondesc_push_action (desc, self);
207 return MALA_SUCCESS;
211 mala_action_execute (MalaAction self, MalaStringListNode pptr, MalaEngine eng)
213 return MALA_LITERAL;
214 (void) eng;
215 (void) pptr;
216 (void) self;
217 //return self->parser ? self->parser (pptr, self->data, eng) : MALA_EPARSER;
223 #if 0 /*TODO*/
226 t_uchar *
227 mala_action_getname (MalaAction self)
229 return self->name;
233 mala_action_attach (MalaAction self, MalaAction parent)
235 if (!llist_is_empty (&self->cldnode))
236 return MALA_EACTIONUSED;
238 llist_add_tail (&parent->childs, &self->cldnode);
239 return MALA_SUCCESS;
242 void
243 mala_action_detach (MalaAction self)
245 llist_unlink (&self->cldnode);
248 MalaParserFunc
249 mala_action_getparser (MalaAction self)
251 return self->parser;
254 void *
255 mala_action_getdata (MalaAction self)
257 return self->data;
259 #endif /*TODO*/
262 void
263 mala_action_free (MalaAction self)
265 MalaAction child;
267 assert(self);
268 assert(self->name);
270 // detach from parent
271 if (self->parent)
273 self->parent = NULL;
274 LIST_REMOVE (self, cldnode);
277 // delete all childs
278 while ((child = self->childs.lh_first) != NULL)
280 LIST_REMOVE (child, cldnode);
281 mala_action_free (child);
284 if (self->destructor && self->data)
286 self->destructor (self->data);
287 self->data = NULL;
290 LIST_REMOVE(self, stknode);
292 /*ok, refcounting has some strange effects*/
293 if (!self->name->bucket->destroying && self->desc->actions.lh_first != NULL)
294 mala_actiondesc_free (self->desc);
296 free (self);
304 // Local Variables:
305 // mode: C
306 // c-file-style: "gnu"
307 // End:
308 // arch-tag: 5b3b4c3c-09f4-4f2e-a737-1ea6c5df59e9
309 // end_of_file