1 /* -*-c-*- -------------- mixguile_cmd_dispatcher.c :
2  * Implementation of the functions declared in mixguile_cmd_dispatcher.h
3  * ------------------------------------------------------------------
4  * Copyright (C) 2001, 2007, 2009 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 02110-1301, USA.
19  *
20  */
21 
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 
27 #include <libguile.h>
28 #include "mixguile.h"
29 #include "xmixguile_cmd_dispatcher.h"
30 
31 #define SCM_CMD  "scm"
32 #define SCMF_CMD "scmf"
33 
34 /*local commands */
35 
eval_(void * code)36 static SCM eval_ (void *code)
37 {
38   scm_c_eval_string ((char *)code);
39   return SCM_BOOL_T;
40 }
41 
42 static gboolean
cmd_scm_(mix_vm_cmd_dispatcher_t * dis,const gchar * arg)43 cmd_scm_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg)
44 {
45   scm_c_catch (SCM_BOOL_T, eval_, (void*) arg,
46                scm_handle_by_message_noexit, NULL, NULL, NULL);
47   return TRUE;
48 }
49 
load_(void * path)50 static SCM load_ (void *path)
51 {
52   scm_c_primitive_load ((char *)path);
53   return SCM_BOOL_T;
54 }
55 
56 static gboolean
cmd_scmf_(mix_vm_cmd_dispatcher_t * dis,const gchar * arg)57 cmd_scmf_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg)
58 {
59   scm_c_catch (SCM_BOOL_T, load_, (void*) arg,
60                scm_handle_by_message_noexit, NULL, NULL, NULL);
61   return TRUE;
62 }
63 
64 static mix_vm_command_info_t commands_[] = {
65   { SCM_CMD, cmd_scm_, N_("Eval Scheme command using Guile"), "scm COMMAND"},
66   { SCMF_CMD, cmd_scmf_, N_("Eval Scheme file using Guile"), "scmf PATH"},
67   {NULL}
68 };
69 
70 /* create/destroy cmd dispatcher */
71 mixguile_cmd_dispatcher_t *
mixguile_cmd_dispatcher_new(mix_vm_cmd_dispatcher_t * dis)72 mixguile_cmd_dispatcher_new (mix_vm_cmd_dispatcher_t *dis)
73 {
74   static gboolean REGISTERED = FALSE;
75   mixguile_cmd_dispatcher_t *result = NULL;
76   int k = 0;
77 
78   g_return_val_if_fail (dis != NULL, NULL);
79 
80   if (!REGISTERED)
81     {
82       register_scm_commands_ (DEFAULT_SCM_COMMANDS_);
83       REGISTERED = TRUE;
84     }
85 
86   result = g_new (mixguile_cmd_dispatcher_t, 1);
87   result->dispatcher = dis;
88 
89   while (commands_[k].name)
90     {
91       mix_vm_cmd_dispatcher_register_new (dis, commands_ + k);
92       ++k;
93     }
94 
95   register_cmd_dispatcher_ (result);
96 
97   return result;
98 }
99 
100 void
mixguile_cmd_dispatcher_delete(mixguile_cmd_dispatcher_t * dis)101 mixguile_cmd_dispatcher_delete (mixguile_cmd_dispatcher_t *dis)
102 {
103   g_return_if_fail (dis != NULL);
104   mix_vm_cmd_dispatcher_delete (dis->dispatcher);
105 }
106 
107 /* get the underlying vm dispatcher */
108 mix_vm_cmd_dispatcher_t *
mixguile_cmd_dispatcher_get_vm_dispatcher(const mixguile_cmd_dispatcher_t * dis)109 mixguile_cmd_dispatcher_get_vm_dispatcher (const mixguile_cmd_dispatcher_t *dis)
110 {
111   g_return_val_if_fail (dis != NULL, NULL);
112   return dis->dispatcher;
113 }
114 
115 void
mixguile_cmd_dispatcher_interpret_file(mixguile_cmd_dispatcher_t * dis,const gchar * path)116 mixguile_cmd_dispatcher_interpret_file (mixguile_cmd_dispatcher_t *dis,
117 					const gchar *path)
118 {
119   g_return_if_fail (dis != NULL);
120   g_return_if_fail (path != NULL);
121   mix_vm_cmd_dispatcher_dispatch_split_text (dis->dispatcher,
122 					     SCMF_CMD, path);
123 }
124 
125 void
mixguile_cmd_dispatcher_interpret_command(mixguile_cmd_dispatcher_t * dis,const gchar * command)126 mixguile_cmd_dispatcher_interpret_command (mixguile_cmd_dispatcher_t *dis,
127 					   const gchar *command)
128 {
129   g_return_if_fail (dis != NULL);
130   g_return_if_fail (command != NULL);
131   mix_vm_cmd_dispatcher_dispatch_split_text (dis->dispatcher,
132 					     SCM_CMD, command);
133 }
134 
135