1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* -*-c-*- -------------- mixguile_cmd_dispatcher.c :
* Implementation of the functions declared in mixguile_cmd_dispatcher.h
* ------------------------------------------------------------------
* Last change: Time-stamp: "01/08/22 02:29:34 jao"
* ------------------------------------------------------------------
* Copyright (C) 2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <guile/gh.h>
#include "mixguile.h"
#include "xmixguile_cmd_dispatcher.h"
#define SCM_CMD "scm"
#define SCMF_CMD "scmf"
/*local commands */
static gboolean
cmd_scm_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg)
{
(void) gh_eval_str_with_catch ((char *)arg, scm_handle_by_message_noexit);
return TRUE;
}
static gboolean
cmd_scmf_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg)
{
(void) gh_eval_file_with_catch ((char *)arg, scm_handle_by_message_noexit);
return TRUE;
}
static mix_vm_command_info_t commands_[] = {
{ SCM_CMD, cmd_scm_, N_("Eval Scheme command using Guile"), "scm COMMAND"},
{ SCMF_CMD, cmd_scmf_, N_("Eval Scheme file using Guile"), "scm PATH"},
{NULL}
};
/* create/destroy cmd dispatcher */
static void
make_pipe_ (mixguile_cmd_dispatcher_t *dis)
{
int fildes[2], r;
FILE *out;
r = pipe (fildes);
g_return_if_fail (r == 0);
out = fdopen (fildes[1], "w");
g_return_if_fail (out != NULL);
r = fcntl (fildes[0], F_GETFL, 0);
g_return_if_fail (r != -1);
r = fcntl (fildes[0], F_SETFL, r | O_NONBLOCK);
g_return_if_fail (r != -1);
dis->guile_out = out;
dis->fildes[0] = fildes[0];
dis->fildes[1] = fildes[1];
}
mixguile_cmd_dispatcher_t *
mixguile_cmd_dispatcher_new (mix_vm_cmd_dispatcher_t *dis)
{
static gboolean REGISTERED = FALSE;
mixguile_cmd_dispatcher_t *result = NULL;
int k = 0;
g_return_val_if_fail (dis != NULL, NULL);
if (!REGISTERED)
{
register_scm_commands_ (DEFAULT_SCM_COMMANDS_);
REGISTERED = TRUE;
}
result = g_new (mixguile_cmd_dispatcher_t, 1);
result->dispatcher = dis;
result->err = result->out = NULL;
result->result = NULL;
result->fildes[0] = result->fildes[1] = -1;
result->guile_out = NULL;
while (commands_[k].name)
{
mix_vm_cmd_dispatcher_register_new (dis, commands_ + k);
++k;
}
register_cmd_dispatcher_ (result);
return result;
}
void
mixguile_cmd_dispatcher_delete (mixguile_cmd_dispatcher_t *dis)
{
g_return_if_fail (dis != NULL);
if (dis->guile_out)
{
fclose (dis->guile_out);
close (dis->fildes[0]);
close (dis->fildes[1]);
}
mix_vm_cmd_dispatcher_delete (dis->dispatcher);
}
/* get the underlying vm dispatcher */
mix_vm_cmd_dispatcher_t *
mixguile_cmd_dispatcher_get_vm_dispatcher (const mixguile_cmd_dispatcher_t *dis)
{
g_return_val_if_fail (dis != NULL, NULL);
return dis->dispatcher;
}
/* get the result string of last executed command */
const gchar *
mixguile_cmd_dispatcher_last_result (mixguile_cmd_dispatcher_t *dis)
{
enum {BLKSIZE = 256};
static gchar BUFFER[BLKSIZE + 1];
ssize_t k;
gchar *tmp = NULL;
g_return_val_if_fail (dis != NULL, NULL);
if (!dis->guile_out) return NULL;
if (dis->result) g_free (dis->result);
dis->result = NULL;
fflush (dis->guile_out);
while ((k = read (dis->fildes[0], BUFFER, BLKSIZE)) != 0)
{
if (k == -1 && errno != EINTR) break;
if (k != -1)
{
tmp = dis->result;
BUFFER[k] = '\0';
dis->result = g_strconcat (tmp ? tmp : "", BUFFER, NULL);
g_free (tmp);
}
}
if (dis->result && dis->result[strlen (dis->result) - 1] == '\n')
dis->result[strlen (dis->result) - 1] = '\0';
return dis->result;
}
/* prepare dispatcher for repl */
static void
prepare_dispatcher_ (mixguile_cmd_dispatcher_t *dis)
{
if (!dis->guile_out) make_pipe_ (dis);
dis->out = mix_vm_cmd_dispatcher_set_out_stream (dis->dispatcher,
dis->guile_out);
dis->err = mix_vm_cmd_dispatcher_set_error_stream (dis->dispatcher,
dis->guile_out);
}
void
mixguile_cmd_dispatcher_prepare (mixguile_cmd_dispatcher_t *dis)
{
g_return_if_fail (dis != NULL);
prepare_dispatcher_ (dis);
}
/* interpret commands from file or string
static void
reset_dispatcher_ (mixguile_cmd_dispatcher_t *dis)
{
(void) mix_vm_cmd_dispatcher_set_out_stream (dis->dispatcher, dis->out);
(void) mix_vm_cmd_dispatcher_set_error_stream (dis->dispatcher, dis->err);
}
*/
void
mixguile_cmd_dispatcher_interpret_file (mixguile_cmd_dispatcher_t *dis,
const gchar *path)
{
g_return_if_fail (dis != NULL);
g_return_if_fail (path != NULL);
mix_vm_cmd_dispatcher_dispatch_split_text (dis->dispatcher,
SCMF_CMD, path);
}
void
mixguile_cmd_dispatcher_interpret_command (mixguile_cmd_dispatcher_t *dis,
const gchar *command)
{
g_return_if_fail (dis != NULL);
g_return_if_fail (command != NULL);
mix_vm_cmd_dispatcher_dispatch_split_text (dis->dispatcher,
SCM_CMD, command);
}
|