1 /*
2 * shell_startup.c
3 */
4 #include "private.h"
5 #include <assert.h>
6
7 #include "lub/string.h"
8
9 /* Default hooks */
10 const char* clish_plugin_default_hook[] = {
11 NULL,
12 "clish_script@clish",
13 NULL,
14 "clish_hook_config@clish",
15 "clish_hook_log@clish"
16 };
17
18 /*----------------------------------------------------------- */
clish_shell_startup(clish_shell_t * this)19 int clish_shell_startup(clish_shell_t *this)
20 {
21 const char *banner;
22 clish_context_t context;
23
24 if (!this->startup) {
25 fprintf(stderr, "Error: Can't get valid STARTUP tag.\n");
26 return -1;
27 }
28
29 /* Prepare context */
30 clish_context_init(&context, this);
31 clish_context__set_cmd(&context, this->startup);
32 clish_context__set_action(&context,
33 clish_command__get_action(this->startup));
34
35 banner = clish_command__get_detail(this->startup);
36 if (banner)
37 tinyrl_printf(this->tinyrl, "%s\n", banner);
38
39 /* Call log initialize */
40 if (clish_shell__get_log(this))
41 clish_shell_exec_log(&context, NULL, 0);
42 /* Call startup script */
43 return clish_shell_execute(&context, NULL);
44 }
45
46 /*----------------------------------------------------------- */
clish_shell__set_startup_view(clish_shell_t * this,const char * viewname)47 void clish_shell__set_startup_view(clish_shell_t *this, const char *viewname)
48 {
49 clish_view_t *view;
50
51 assert(this);
52 assert(this->startup);
53 /* Search for the view */
54 view = clish_shell_find_view(this, viewname);
55 assert(view);
56 clish_command__force_viewname(this->startup, viewname);
57 }
58
59 /*----------------------------------------------------------- */
clish_shell__set_startup_viewid(clish_shell_t * this,const char * viewid)60 void clish_shell__set_startup_viewid(clish_shell_t *this, const char *viewid)
61 {
62 assert(this);
63 assert(this->startup);
64 clish_command__force_viewid(this->startup, viewid);
65 }
66
67 /*----------------------------------------------------------- */
clish_shell__set_default_shebang(clish_shell_t * this,const char * shebang)68 void clish_shell__set_default_shebang(clish_shell_t *this, const char *shebang)
69 {
70 assert(this);
71 lub_string_free(this->default_shebang);
72 this->default_shebang = lub_string_dup(shebang);
73 }
74
75 /*----------------------------------------------------------- */
clish_shell__get_default_shebang(const clish_shell_t * this)76 const char * clish_shell__get_default_shebang(const clish_shell_t *this)
77 {
78 assert(this);
79 return this->default_shebang;
80 }
81
82
83
84 /*-------------------------------------------------------- */
85 /* Resolve PTYPE for given PARAM.
86 */
resolve_ptype(clish_shell_t * this,clish_param_t * param)87 static clish_ptype_t * resolve_ptype(clish_shell_t *this, clish_param_t *param)
88 {
89 clish_ptype_t *ptype = NULL;
90
91 if (!this || !param)
92 return NULL;
93
94 /* Resolve PARAM's PTYPE */
95 ptype = clish_shell_find_ptype(this, clish_param__get_ptype_name(param));
96 if (!ptype) {
97 fprintf(stderr, "Error: Unresolved PTYPE \"%s\" in PARAM \"%s\"\n",
98 clish_param__get_ptype_name(param),
99 clish_param__get_name(param));
100 return NULL;
101 }
102 clish_param__set_ptype(param, ptype);
103 clish_param__set_ptype_name(param, NULL); /* Free some memory */
104
105 return ptype;
106 }
107
108 /*-------------------------------------------------------- */
109 /* Static recursive function to iterate parameters. Logically it's the
110 * part of clish_shell_prepare() function.
111 */
iterate_paramv(clish_shell_t * this,clish_paramv_t * paramv,clish_hook_access_fn_t * access_fn)112 static int iterate_paramv(clish_shell_t *this, clish_paramv_t *paramv,
113 clish_hook_access_fn_t *access_fn)
114 {
115 int i = 0;
116 clish_param_t *param;
117
118 while((param = clish_paramv__get_param(paramv, i))) {
119 clish_paramv_t *nested_paramv;
120
121 /* Resolve PARAM's PTYPE */
122 if (!resolve_ptype(this, param))
123 return -1;
124
125 /* Check access for PARAM */
126 if (access_fn && clish_param__get_access(param) &&
127 access_fn(this, clish_param__get_access(param))) {
128 #ifdef DEBUG
129 fprintf(stderr, "Warning: Access denied. Remove PARAM \"%s\"\n",
130 clish_param__get_name(param));
131 #endif
132 if (clish_paramv_remove(paramv, i) < 0) {
133 fprintf(stderr, "Error: Some system problem\n");
134 return -1;
135 }
136 clish_param_delete(param);
137 continue; /* Don't increment index */
138 }
139 /* Recursive iterate nested PARAMs */
140 nested_paramv = clish_param__get_paramv(param);
141 if (iterate_paramv(this, nested_paramv, access_fn) < 0)
142 return -1;
143 i++;
144 }
145
146 return 0;
147 }
148
149 /*-------------------------------------------------------- */
150 /* This function prepares schema for execution. It loads
151 * plugins, link unresolved symbols, then iterates all the
152 * objects and link them to each other, check access
153 * permissions. Don't execute clish_shell_startup() without this
154 * function.
155 */
clish_shell_prepare(clish_shell_t * this)156 int clish_shell_prepare(clish_shell_t *this)
157 {
158 clish_command_t *cmd;
159 clish_view_t *view;
160 clish_nspace_t *nspace;
161 lub_bintree_t *view_tree, *cmd_tree;
162 lub_list_t *nspace_tree;
163 lub_bintree_iterator_t cmd_iter, view_iter;
164 lub_list_node_t *nspace_iter;
165 clish_hook_access_fn_t *access_fn = NULL;
166 clish_paramv_t *paramv;
167 int i = 0;
168
169 /* Add statically linked plugins */
170 while (clish_plugin_builtin_list[i].name) {
171 clish_plugin_t *plugin;
172 plugin = clish_shell_find_create_plugin(this,
173 clish_plugin_builtin_list[i].name);
174 clish_plugin_add_init(plugin,
175 clish_plugin_builtin_list[i].init);
176 clish_plugin__set_builtin_flag(plugin, BOOL_TRUE);
177 i++;
178 }
179
180 /* Add default plugin to the list of plugins */
181 if (this->default_plugin) {
182 clish_shell_find_create_plugin(this, "clish");
183 /* Default hooks */
184 for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
185 if (this->hooks_use[i])
186 continue;
187 if (!clish_plugin_default_hook[i])
188 continue;
189 clish_sym__set_name(this->hooks[i],
190 clish_plugin_default_hook[i]);
191 }
192 }
193 /* Add default syms to unresolved table */
194 for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
195 if (clish_sym__get_name(this->hooks[i]))
196 lub_list_add(this->syms, this->hooks[i]);
197 }
198
199 /* Load plugins and link symbols */
200 if (clish_shell_load_plugins(this) < 0)
201 return -1;
202 if (clish_shell_link_plugins(this) < 0)
203 return -1;
204
205 access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
206
207 /* Iterate the VIEWs */
208 view_tree = &this->view_tree;
209 view = lub_bintree_findfirst(view_tree);
210 for (lub_bintree_iterator_init(&view_iter, view_tree, view);
211 view; view = lub_bintree_iterator_next(&view_iter)) {
212 /* Check access rights for the VIEW */
213 if (access_fn && clish_view__get_access(view) &&
214 access_fn(this, clish_view__get_access(view))) {
215 #ifdef DEBUG
216 fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
217 clish_view__get_name(view));
218 #endif
219 lub_bintree_remove(view_tree, view);
220 clish_view_delete(view);
221 continue;
222 }
223
224 /* Iterate the NAMESPACEs */
225 nspace_tree = clish_view__get_nspace_tree(view);
226 nspace_iter = lub_list__get_head(nspace_tree);
227 while(nspace_iter) {
228 clish_view_t *ref_view;
229 lub_list_node_t *old_nspace_iter;
230 nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
231 old_nspace_iter = nspace_iter;
232 nspace_iter = lub_list_node__get_next(nspace_iter);
233 /* Resolve NAMESPACEs and remove unresolved ones */
234 ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
235 if (!ref_view) {
236 #ifdef DEBUG
237 fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
238 clish_nspace__get_view_name(nspace), clish_view__get_name(view));
239 #endif
240 lub_list_del(nspace_tree, old_nspace_iter);
241 lub_list_node_free(old_nspace_iter);
242 clish_nspace_delete(nspace);
243 continue;
244 }
245 clish_nspace__set_view(nspace, ref_view);
246 clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
247 /* Check access rights for the NAMESPACE */
248 if (access_fn && (
249 /* Check NAMESPASE owned access */
250 (clish_nspace__get_access(nspace) && access_fn(this, clish_nspace__get_access(nspace)))
251 ||
252 /* Check referenced VIEW's access */
253 (clish_view__get_access(ref_view) && access_fn(this, clish_view__get_access(ref_view)))
254 )) {
255 #ifdef DEBUG
256 fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
257 clish_nspace__get_view_name(nspace), clish_view__get_name(view));
258 #endif
259 lub_list_del(nspace_tree, old_nspace_iter);
260 lub_list_node_free(old_nspace_iter);
261 clish_nspace_delete(nspace);
262 continue;
263 }
264 }
265
266 /* Iterate the COMMANDs */
267 cmd_tree = clish_view__get_command_tree(view);
268 cmd = lub_bintree_findfirst(cmd_tree);
269 for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
270 cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
271 int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
272 clish_param_t *args = NULL;
273
274 /* Check access rights for the COMMAND */
275 if (access_fn && clish_command__get_access(cmd) &&
276 access_fn(this, clish_command__get_access(cmd))) {
277 #ifdef DEBUG
278 fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
279 clish_command__get_name(cmd), clish_view__get_name(view));
280 #endif
281 lub_bintree_remove(cmd_tree, cmd);
282 clish_command_delete(cmd);
283 continue;
284 }
285
286 /* Resolve command aliases */
287 if (cmd_is_alias) {
288 clish_view_t *aview;
289 clish_command_t *cmdref;
290 const char *alias_view = clish_command__get_alias_view(cmd);
291 if (!alias_view)
292 aview = clish_command__get_pview(cmd);
293 else
294 aview = clish_shell_find_view(this, alias_view);
295 if (!aview /* Removed or broken VIEW */
296 ||
297 /* Removed or broken referenced COMMAND */
298 !(cmdref = clish_view_find_command(aview, clish_command__get_alias(cmd), BOOL_FALSE))
299 ) {
300 #ifdef DEBUG
301 fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
302 clish_command__get_name(cmd), clish_view__get_name(view));
303 #endif
304 lub_bintree_remove(cmd_tree, cmd);
305 clish_command_delete(cmd);
306 continue;
307 /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
308 clish_command__get_name(cmd));
309 return -1; */
310 /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
311 clish_command__get_name(cmd));
312 return -1; */
313 }
314 if (!clish_command_alias_to_link(cmd, cmdref)) {
315 fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
316 clish_command__get_name(cmd));
317 return -1;
318 }
319 /* Check access rights for newly constructed COMMAND.
320 Now the link has access filed from referenced command.
321 */
322 if (access_fn && clish_command__get_access(cmd) &&
323 access_fn(this, clish_command__get_access(cmd))) {
324 #ifdef DEBUG
325 fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
326 clish_command__get_name(cmd), clish_view__get_name(view));
327 #endif
328 lub_bintree_remove(cmd_tree, cmd);
329 clish_command_delete(cmd);
330 continue;
331 }
332 }
333 if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
334 continue;
335 /* Iterate PARAMeters */
336 paramv = clish_command__get_paramv(cmd);
337 if (iterate_paramv(this, paramv, access_fn) < 0)
338 return -1;
339 /* Resolve PTYPE for args */
340 if ((args = clish_command__get_args(cmd))) {
341 if (!resolve_ptype(this, args))
342 return -1;
343 }
344 }
345 }
346
347 return 0;
348 }
349
350 /*----------------------------------------------------------- */
351