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"
21 #include "stringlist.h"
22 #include "actiondesc.h"
28 mala_action_new (MalaString name
,
30 MalaParserFunc parser
,
31 MalaDataFactory factory
,
36 self
= malloc (sizeof (mala_action
));
40 self
->name
= name
; /* weak-reference, not refcounted */
41 self
->parser
= parser
;
42 self
->factory
= factory
;
46 LIST_INIT (&self
->childs
);
48 self
->parent
= parent
;
50 LIST_INSERT_HEAD (&self
->childs
, self
, cldnode
);
56 mala_action_new_actioninit (MalaActionInit init
, MalaStringBucket bucket
)
61 MalaDataFactory factory
= init
->factory
;
63 name
= mala_string_new (init
->command
, bucket
);
69 data
= factory ( &data
, (char *)init
->data
, bucket
);
76 self
= mala_action_new (name
, data
,
77 init
->parser
, factory
,
78 mala_action_get_cstr (bucket
, init
->parent
));
82 mala_string_free (name
);
87 factory (&data
, NULL
, NULL
);
89 mala_string_free (name
);
94 mala_action_get_cstr (MalaStringBucket bucket
, const char * parent
)
97 ? mala_actiondesc_top (mala_string_user_get (mala_string_find (parent
, bucket
)))
103 mala_action_attach (MalaAction self
)
107 desc
= mala_actiondesc_ensure (self
->name
);
111 mala_actiondesc_push_action (desc
, self
);
116 mala_action_execute (MalaAction self
, MalaStringListNode_ref pptr
, MalaEngine eng
)
118 return self
->parser
? self
->parser (eng
, pptr
, self
->data
) : MALA_EPARSER
;
128 mala_action_getname (MalaAction self
)
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
);
144 mala_action_detach (MalaAction self
)
146 llist_unlink (&self
->cldnode
);
150 mala_action_getparser (MalaAction self
)
156 mala_action_getdata (MalaAction self
)
164 mala_action_free (MalaAction self
)
171 MALA_ASSERT(self
->name
);
172 // detach from parent
176 LIST_REMOVE (self
, cldnode
);
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
);
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
);
208 // c-file-style: "gnu"
210 // arch-tag: 5b3b4c3c-09f4-4f2e-a737-1ea6c5df59e9