omit args/newline printing if there are none
[mala.git] / engine / action.c
bloba6fead0f9e2ba443f3c00de50e37cce93186b576
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 "mala_types.h"
20 #include "strings.h"
21 #include "stringlist.h"
22 #include "actiondesc.h"
23 #include "action.h"
27 MalaAction
28 mala_action_new (MalaString name,
29 void * data,
30 MalaParserFunc parser,
31 MalaDataFactory factory,
32 MalaAction parent)
34 MalaAction self;
36 self = malloc (sizeof (mala_action));
37 if (!self)
38 return NULL;
40 self->name = name; /* weak-reference, not refcounted */
41 self->parser = parser;
42 self->factory = factory;
43 self->data = data;
44 self->desc = NULL;
46 LIST_INIT (&self->childs);
48 self->parent = parent;
49 if (parent)
50 LIST_INSERT_HEAD (&self->childs, self, cldnode);
52 return self;
55 MalaAction
56 mala_action_new_actioninit (MalaActionInit init, MalaStringBucket bucket)
58 MalaAction self;
59 MalaString name;
60 void * data = NULL;
61 MalaDataFactory factory = init->factory;
63 name = mala_string_new (init->command, bucket);
64 if (!name)
65 return NULL;
67 if (factory)
69 data = factory ( &data, (char *)init->data, bucket);
70 if (!data)
71 goto ealloc;
73 else
74 data = init->data;
76 self = mala_action_new (name, data,
77 init->parser, factory,
78 mala_action_get_cstr (bucket, init->parent));
79 if (!self)
80 goto ealloc_action;
82 mala_string_free (name);
84 return self;
86 ealloc_action:
87 factory (&data, NULL, NULL);
88 ealloc:
89 mala_string_free (name);
90 return NULL;
93 MalaAction
94 mala_action_get_cstr (MalaStringBucket bucket, const char * parent)
96 return parent
97 ? mala_actiondesc_top (mala_string_user_get (mala_string_find (parent, bucket)))
98 : NULL;
103 mala_action_attach (MalaAction self)
105 MalaActionDesc desc;
107 desc = mala_actiondesc_ensure (self->name);
108 if (!desc)
109 return MALA_EALLOC;
111 mala_actiondesc_push_action (desc, self);
112 return MALA_SUCCESS;
116 mala_action_execute (MalaAction self, MalaStringListNode_ref pptr, MalaEngine eng)
118 return self->parser ? self->parser (eng, pptr, self->data) : MALA_EPARSER;
124 #if 0 /*TODO*/
127 t_uchar *
128 mala_action_getname (MalaAction self)
130 return self->name;
134 mala_action_attach (MalaAction self, MalaAction parent)
136 if (!llist_is_empty (&self->cldnode))
137 return MALA_EACTIONUSED;
139 llist_add_tail (&parent->childs, &self->cldnode);
140 return MALA_SUCCESS;
143 void
144 mala_action_detach (MalaAction self)
146 llist_unlink (&self->cldnode);
149 MalaParserFunc
150 mala_action_getparser (MalaAction self)
152 return self->parser;
155 void *
156 mala_action_getdata (MalaAction self)
158 return self->data;
160 #endif /*TODO*/
163 void
164 mala_action_free (MalaAction self)
166 MalaAction child;
168 if (!self)
169 return;
171 MALA_ASSERT(self->name);
172 // detach from parent
173 if (self->parent)
175 self->parent = NULL;
176 LIST_REMOVE (self, cldnode);
179 // delete all childs
180 while ((child = self->childs.lh_first) != NULL)
182 LIST_REMOVE (child, cldnode);
183 mala_action_free (child);
186 if (self->factory && self->data)
188 self->factory (&self->data, NULL, NULL);
189 self->data = NULL;
192 LIST_REMOVE(self, stknode);
194 /*ok, refcounting has some strange effects*/
195 if (!self->name->bucket->destroying && self->desc->actions.lh_first)
196 mala_actiondesc_free (self->desc);
198 free (self);
206 // Local Variables:
207 // mode: C
208 // c-file-style: "gnu"
209 // End:
210 // arch-tag: 5b3b4c3c-09f4-4f2e-a737-1ea6c5df59e9
211 // end_of_file