Debian Codegraph @

mixguile/xmixguile_cmd_dispatcher.c

raw symbols SCIP
1 /* -*-c-*- -------------- xmixguile_cmd_dispatcher.c :
2 * Implementation of the functions declared in xmixguile_cmd_dispatcher.h
3 * ------------------------------------------------------------------
4 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2014 Free Software Foundation, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 *
21 */
22
23 #include <string.h>
24
25 #include <mixlib/mix.h>
26 #include <libguile.h>
27
28 #include "xmixguile_cmd_dispatcher.h"
29
30 /* cmd dispatcher for use within the scm commands */
31 static mixguile_cmd_dispatcher_t *dispatcher_;
32 static mix_vm_cmd_dispatcher_t *vm_dispatcher_;
33 static mix_vm_t *vm_;
34 static SCM mutex_;
35
36 /* register a NULL-terminated list of scm commands */
37 void
38 register_scm_commands_ (const scm_command_t *commands)
39 {
40 int k = 0;
41 g_return_if_fail (commands != NULL);
42 while (commands[k].name)
43 {
44 scm_c_define_gsubr (commands[k].name,
45 commands[k].argno,
46 commands[k].opt_argno,
47 commands[k].restp,
48 commands[k].func);
49 ++k;
50 }
51 }
52
53 /* register the mixvm cmd dispatcher to use with commands */
54 void
55 register_cmd_dispatcher_ (mixguile_cmd_dispatcher_t *dis)
56 {
57 g_return_if_fail (dis != NULL);
58 dispatcher_ = dis;
59 vm_dispatcher_ = mixguile_cmd_dispatcher_get_vm_dispatcher (dis);
60 vm_ = (mix_vm_t *) mix_vm_cmd_dispatcher_get_vm (vm_dispatcher_);
61 mutex_ = scm_make_mutex ();
62 }
63
64 /* commands */
65 static SCM
66 mixvm_cmd_ (SCM cmd, SCM arg)
67 {
68 char *com = NULL, *argu = NULL;
69
70 SCM_ASSERT (scm_is_string (cmd) || scm_is_symbol (cmd),
71 cmd, SCM_ARG1, "mixvm-cmd");
72 SCM_ASSERT (scm_is_string (arg) || scm_is_symbol (arg),
73 arg, SCM_ARG2, "mixvm-cmd");
74
75 scm_lock_mutex (mutex_);
76 com = scm_to_locale_string (cmd);
77 argu = scm_to_locale_string (arg);
78 (void) mix_vm_cmd_dispatcher_dispatch (vm_dispatcher_,
79 mix_vm_command_from_string (com),
80 argu);
81 g_free (com);
82 g_free (argu);
83
84 scm_unlock_mutex (mutex_);
85
86 return SCM_UNSPECIFIED;
87 }
88
89 static SCM
90 mixvm_status_ (void)
91 {
92 return scm_from_long (mix_vm_get_run_status (vm_));
93 }
94
95 static SCM
96 mix_last_result_ (void)
97 {
98 return scm_from_bool (mix_vm_cmd_dispatcher_get_last_result (vm_dispatcher_));
99 }
100
101 static long
102 word_to_long_ (mix_word_t word)
103 {
104 long result = mix_word_magnitude (word);
105 return mix_word_is_negative (word) ? -result : result;
106 }
107
108 static long
109 short_to_long_ (mix_short_t s)
110 {
111 long result = mix_short_magnitude (s);
112 return mix_short_is_negative (s) ? -result : result;
113 }
114
115 static SCM
116 mix_reg_ (SCM reg)
117 {
118 char *regis;
119 long val = MIX_WORD_MAX + 1;
120
121 SCM_ASSERT (scm_is_string (reg) || scm_is_symbol (reg),
122 reg, SCM_ARG1, "mix-reg");
123
124 scm_lock_mutex (mutex_);
125 if (SCM_SYMBOLP (reg)) reg = scm_symbol_to_string (reg);
126 regis = scm_to_locale_string (reg);
127 switch (regis[0])
128 {
129 case 'A':
130 val = word_to_long_ (mix_vm_get_rA (vm_)); break;
131 case 'X':
132 val = word_to_long_ (mix_vm_get_rX (vm_)); break;
133 case 'J':
134 val = short_to_long_ (mix_vm_get_rJ (vm_)); break;
135 case 'I':
136 {
137 int i = regis[1] - '0';
138 if (i > 0 && i < 7) val = short_to_long_ (mix_vm_get_rI (vm_, i));
139 }
140 break;
141 default:
142 break;
143 }
144 g_free (regis);
145
146 scm_unlock_mutex (mutex_);
147
148 SCM_ASSERT (val <= MIX_WORD_MAX, reg, SCM_ARG1, "mix-reg");
149
150 return scm_from_long (val);
151 }
152
153 static SCM
154 mix_set_reg_ (SCM reg, SCM value)
155 {
156 char *regis;
157 long val;
158 gboolean result = TRUE;
159
160 SCM_ASSERT (scm_is_string (reg) || scm_is_symbol (reg),
161 reg, SCM_ARG1, "mix-set-reg!");
162 SCM_ASSERT (scm_is_number (value), value, SCM_ARG2, "mix-set-reg!");
163
164 scm_lock_mutex (mutex_);
165 if (SCM_SYMBOLP (reg)) reg = scm_symbol_to_string (reg);
166 regis = scm_to_locale_string (reg);
167 val = scm_to_long (value);
168 switch (regis[0])
169 {
170 case 'A':
171 mix_vm_set_rA (vm_, mix_word_new (val)); break;
172 case 'X':
173 mix_vm_set_rX (vm_, mix_word_new (val)); break;
174 case 'J':
175 mix_vm_set_rJ (vm_, mix_short_new (val)); break;
176 case 'I':
177 {
178 int i = regis[1] - '0';
179 if (i > 0 && i < 7) mix_vm_set_rI (vm_, i, mix_short_new (val));
180 else result = FALSE;
181 }
182 break;
183 default:
184 result = FALSE; break;
185 }
186 g_free (regis);
187
188 scm_unlock_mutex (mutex_);
189
190 SCM_ASSERT (result, reg, SCM_ARG1, "mix-set-reg!");
191
192 return SCM_BOOL_T;
193 }
194
195 static SCM
196 mix_cell_ (SCM no)
197 {
198 int cell;
199 long result;
200
201 SCM_ASSERT (SCM_NUMBERP (no), no, SCM_ARG1, "mix-cell");
202 cell = scm_to_int (no);
203 SCM_ASSERT (cell < MIX_VM_CELL_NO, no, SCM_ARG1, "mix-cell");
204 result = word_to_long_ (mix_vm_get_addr_contents (vm_, cell));
205 return scm_from_long (result);
206 }
207
208 static SCM
209 mix_set_cell_ (SCM no, SCM val)
210 {
211 int cell;
212 long result;
213
214 SCM_ASSERT (SCM_NUMBERP (no), no, SCM_ARG1, "mix-set-cell!");
215 SCM_ASSERT (SCM_NUMBERP (val), no, SCM_ARG2, "mix-set-cell!");
216 cell = scm_to_int (no);
217 SCM_ASSERT (cell < MIX_VM_CELL_NO, no, SCM_ARG1, "mix-set-cell!");
218 result = scm_to_long (val);
219 mix_vm_set_addr_contents (vm_, cell, mix_word_new (result));
220 return SCM_BOOL_T;
221 }
222
223 static SCM
224 mix_over_ (void)
225 {
226 return scm_from_bool (mix_vm_get_overflow (vm_));
227 }
228
229 static SCM
230 mix_set_over_ (SCM over)
231 {
232 mix_vm_set_overflow (vm_, scm_to_bool (over));
233 return SCM_BOOL_T;
234 }
235
236 static SCM
237 mix_loc_ (void)
238 {
239 return scm_from_long (mix_vm_get_prog_count (vm_));
240 }
241
242 static SCM
243 mix_cmp_ (void)
244 {
245 gchar *result = NULL;
246 switch (mix_vm_get_cmpflag (vm_))
247 {
248 case mix_LESS: result = "L"; break;
249 case mix_EQ: result = "E"; break;
250 case mix_GREAT: result = "G"; break;
251 default: g_assert_not_reached ();
252 }
253 return scm_from_locale_symbol (result);
254 }
255
256 static SCM
257 mix_set_cmp_ (SCM value)
258 {
259 gchar *val = NULL;
260 mix_cmpflag_t result = -1;
261
262 SCM_ASSERT (scm_is_string (value) || scm_is_symbol (value),
263 value, SCM_ARG1, "mix-set-cmp!");
264
265 scm_lock_mutex (mutex_);
266 val = scm_to_locale_string (value);
267 if (strlen (val) == 1)
268 {
269 switch (val[0])
270 {
271 case 'L': result = mix_LESS; break;
272 case 'E': result = mix_EQ; break;
273 case 'G': result = mix_GREAT; break;
274 default: break;
275 }
276 }
277 g_free (val);
278 scm_unlock_mutex (mutex_);
279 SCM_ASSERT (result != -1, value, SCM_ARG1, "mix-set-cmp!");
280 mix_vm_set_cmpflag (vm_, result);
281 return SCM_BOOL_T;
282 }
283
284 static SCM
285 mix_src_name_ (void)
286 {
287 const gchar *path = mix_vm_cmd_dispatcher_get_src_file_path (vm_dispatcher_);
288 return scm_from_locale_string (path? g_path_get_basename (path) : "");
289 }
290
291 static SCM
292 mix_src_path_ (void)
293 {
294 const gchar *path = mix_vm_cmd_dispatcher_get_src_file_path (vm_dispatcher_);
295 return scm_from_locale_string (path? (char *)path : "");
296 }
297
298 static SCM
299 mix_prog_name_ (void)
300 {
301 const gchar *path = mix_vm_cmd_dispatcher_get_program_path (vm_dispatcher_);
302 return scm_from_locale_string (path? g_path_get_basename (path) : "");
303 }
304
305 static SCM
306 mix_prog_path_ (void)
307 {
308 const gchar *path = mix_vm_cmd_dispatcher_get_program_path (vm_dispatcher_);
309 return scm_from_locale_string (path? (char *)path : "");
310 }
311
312 static SCM
313 mix_ddir_ (void)
314 {
315 return scm_from_locale_string ((char *)mix_device_get_dir ());
316 }
317
318 static SCM
319 mix_uptime_ (void)
320 {
321 return scm_from_long (mix_vm_cmd_dispatcher_get_uptime (vm_dispatcher_));
322 }
323
324 static SCM
325 mix_progtime_ (void)
326 {
327 return scm_from_long (mix_vm_cmd_dispatcher_get_progtime (vm_dispatcher_));
328 }
329
330 static SCM
331 mix_laptime_ (void)
332 {
333 return scm_from_long (mix_vm_cmd_dispatcher_get_laptime (vm_dispatcher_));
334 }
335
336 static SCM
337 mix_src_line_ (SCM opt)
338 {
339 gulong no = 0;
340 const gchar *line = "";
341 if (opt != SCM_UNDEFINED)
342 {
343 SCM_ASSERT (SCM_NUMBERP (opt), opt, SCM_ARG1, "mix-src-line");
344 no = scm_to_ulong (opt);
345 }
346 else
347 no = mix_vm_cmd_dispatcher_get_src_file_lineno (vm_dispatcher_);
348
349 SCM_ASSERT (line >= 0, opt, SCM_ARG1, "mix-src-line");
350
351 if (no > 0)
352 line = mix_vm_cmd_dispatcher_get_src_file_line (vm_dispatcher_, no, FALSE);
353
354 return scm_from_locale_string ((char *)line);
355 }
356
357 static SCM
358 mix_src_line_no_ (void)
359 {
360 return
361 scm_from_long (mix_vm_cmd_dispatcher_get_src_file_lineno (vm_dispatcher_));
362 }
363
364 /* ----- hook functions ---- */
365
366 /* auxiliar arg list maker */
367 static SCM
368 make_arg_list_ (const gchar *arg)
369 {
370 gchar **arglist = g_strsplit (arg, " ", -1);
371 SCM argument = scm_list_n (SCM_UNDEFINED, SCM_EOL);
372 if (arglist && arglist[0])
373 {
374 int k = 0;
375 while (arglist[k])
376 argument = scm_cons (scm_from_locale_string (arglist[k++]), argument);
377 argument = scm_reverse (argument);
378 }
379 g_strfreev (arglist);
380 return argument;
381 }
382
383 /* command hook auxiliar functions and types */
384 /*
385 static SCM
386 hook_error_handler_ (void *data, SCM tag, SCM args){}
387 */
388 typedef struct
389 {
390 SCM function;
391 SCM args;
392 } hook_data_t;
393
394 static SCM
395 hook_catch_body_ (void *data)
396 {
397 hook_data_t *h = (hook_data_t *)data;
398 return scm_call_1 (h->function, h->args);
399 }
400
401 static void
402 scm_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg, gpointer data)
403 {
404 hook_data_t h;
405 h.function = (SCM) data;
406
407 g_assert (scm_is_true (scm_procedure_p (h.function)));
408
409 h.args = make_arg_list_ (arg);
410 g_assert (scm_is_true (scm_list_p (h.args)));
411
412 scm_internal_catch (SCM_BOOL_T, hook_catch_body_, &h,
413 scm_handle_by_message_noexit, dis);
414 }
415
416 /* global hook auxiliar functions and types */
417 typedef struct
418 {
419 SCM function;
420 SCM cmd;
421 SCM args;
422 } global_hook_data_t;
423
424 static SCM
425 global_hook_catch_body_ (void *data)
426 {
427 global_hook_data_t *h = (global_hook_data_t *)data;
428 return scm_call_2 (h->function, h->cmd, h->args);
429 }
430
431 static void
432 scm_global_hook_ (mix_vm_cmd_dispatcher_t *dis, mix_vm_command_t cmd,
433 const gchar *arg, gpointer data)
434 {
435 global_hook_data_t h;
436 h.function = (SCM) data;
437 h.cmd = scm_from_locale_string ((char *)mix_vm_command_to_string (cmd));
438 h.args = make_arg_list_ (arg);
439 scm_internal_catch (SCM_BOOL_T, global_hook_catch_body_, &h,
440 scm_handle_by_message_noexit, NULL);
441 }
442
443 static SCM
444 define_hook_procedure_ (SCM function)
445 {
446 enum {BUFF_SIZE = 128};
447 static gchar BUFFER[BUFF_SIZE];
448 static const gchar *PATTERN = "____mix__hook__%d____";
449 static int K = 0;
450 g_snprintf (BUFFER, BUFF_SIZE, PATTERN, K++);
451 /* scm_c__define (name, val) returns a pair: (symbol . symbol-value) */
452 return scm_cdr (scm_c_define ((char *)BUFFER, function));
453 }
454
455 static SCM
456 mix_add_hook_ (SCM cmd, SCM function, gboolean pre)
457 {
458 gchar *cmdstr = NULL;
459 mix_vm_command_t command;
460 const gchar *fun = pre? "mix-add-pre-hook" : "mix-add-post-hook";
461
462 SCM_ASSERT (scm_is_string (cmd) || scm_is_symbol (cmd), cmd, SCM_ARG1, fun);
463 SCM_ASSERT (scm_is_true (scm_procedure_p (function)), function, SCM_ARG2, fun);
464 scm_lock_mutex (mutex_);
465 cmdstr = scm_to_locale_string (cmd);
466 command = mix_vm_command_from_string (cmdstr);
467 g_free (cmdstr);
468 scm_unlock_mutex (mutex_);
469 SCM_ASSERT (command != MIX_CMD_INVALID, cmd, SCM_ARG1, fun);
470 scm_lock_mutex (mutex_);
471 if (pre)
472 mix_vm_cmd_dispatcher_pre_hook (vm_dispatcher_, command, scm_hook_,
473 (gpointer) define_hook_procedure_ (function));
474 else
475 mix_vm_cmd_dispatcher_post_hook (vm_dispatcher_, command, scm_hook_,
476 (gpointer) define_hook_procedure_ (function));
477 scm_unlock_mutex (mutex_);
478 return SCM_BOOL_T;
479 }
480
481 static SCM
482 mix_add_global_hook_ (SCM function, gboolean pre)
483 {
484 const gchar *fun =
485 pre? "mix-add-global-pre-hook" : "mix-add-global-post-hook";
486
487 SCM_ASSERT (scm_is_true (scm_procedure_p (function)), function, SCM_ARG1, fun);
488 scm_lock_mutex (mutex_);
489 if (pre)
490 mix_vm_cmd_dispatcher_global_pre_hook (vm_dispatcher_, scm_global_hook_,
491 (gpointer) define_hook_procedure_ (function));
492 else
493 mix_vm_cmd_dispatcher_global_post_hook (vm_dispatcher_, scm_global_hook_,
494 (gpointer) define_hook_procedure_ (function));
495 scm_unlock_mutex (mutex_);
496 return SCM_BOOL_T;
497 }
498
499 static SCM
500 mix_add_pre_hook_ (SCM cmd, SCM function)
501 {
502 return mix_add_hook_ (cmd, function, TRUE);
503 }
504
505 static SCM
506 mix_add_post_hook_ (SCM cmd, SCM function)
507 {
508 return mix_add_hook_ (cmd, function, FALSE);
509 }
510
511 static SCM
512 mix_add_global_pre_hook_ (SCM function)
513 {
514 return mix_add_global_hook_ (function, TRUE);
515 }
516
517 static SCM
518 mix_add_global_post_hook_ (SCM function)
519 {
520 return mix_add_global_hook_ (function, FALSE);
521 }
522
523 /* NULL-terminated list of available scm commands */
524 const scm_command_t DEFAULT_SCM_COMMANDS_[] = {
525 {"mixvm-cmd", mixvm_cmd_, 2, 0, 0},
526 {"mixvm-status", mixvm_status_, 0, 0, 0},
527 {"mix-last-result", mix_last_result_, 0, 0, 0},
528 {"mix-reg", mix_reg_, 1, 0, 0},
529 {"mix-set-reg!", mix_set_reg_, 2, 0, 0},
530 {"mix-cell", mix_cell_, 1, 0, 0},
531 {"mix-set-cell!", mix_set_cell_, 2, 0, 0},
532 {"mix-over", mix_over_, 0, 0, 0},
533 {"mix-loc", mix_loc_, 0, 0, 0},
534 {"mix-set-over!", mix_set_over_, 1, 0, 0},
535 {"mix-cmp", mix_cmp_, 0, 0, 0},
536 {"mix-up-time", mix_uptime_, 0, 0, 0},
537 {"mix-lap-time", mix_laptime_, 0, 0, 0},
538 {"mix-prog-time", mix_progtime_, 0, 0, 0},
539 {"mix-prog-name", mix_prog_name_, 0, 0, 0},
540 {"mix-prog-path", mix_prog_path_, 0, 0, 0},
541 {"mix-src-name", mix_src_name_, 0, 0, 0},
542 {"mix-src-path", mix_src_path_, 0, 0, 0},
543 {"mix-src-line-no", mix_src_line_no_, 0, 0, 0},
544 {"mix-src-line", mix_src_line_, 0, 1, 0},
545 {"mix-ddir", mix_ddir_, 0, 0, 0},
546 {"mix-set-cmp!", mix_set_cmp_, 1, 0, 0},
547 {"mix-add-pre-hook", mix_add_pre_hook_, 2, 0, 0},
548 {"mix-add-post-hook", mix_add_post_hook_, 2, 0, 0},
549 {"mix-add-global-pre-hook", mix_add_global_pre_hook_, 1, 0, 0},
550 {"mix-add-global-post-hook", mix_add_global_post_hook_, 1, 0, 0},
551 {NULL}
552 };