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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* -*-c-*- -------------- mixvm_command.c :
* Implementation of the functions declared in mixvm_command.h
* ------------------------------------------------------------------
* Copyright (C) 2000, 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 <mixlib/mix.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <mixlib/mix.h>
#ifdef HAVE_LIBREADLINE
# include <readline/readline.h>
# include <readline/history.h>
# ifndef HAVE_RL_COMPLETION_MATCHES /* old versions of rl don't use rl_ */
# define rl_completion_matches completion_matches
# endif
#else /* ! HAVE_LIBREADLINE */
typedef int Function ();
#endif /* HAVE_LIBREADLINE */
#include <mixlib/mix_vm.h>
#include <mixlib/mix_vm_dump.h>
#include <mixlib/mix_eval.h>
#include <mixlib/mix_src_file.h>
#include <mixlib/mix_vm_command.h>
#ifdef MAKE_GUILE
# include <mixguile/mixguile.h>
static gboolean
try_guile_ (char *line)
{
if (line[0] == '(')
{
if (line[strlen (line) -1] != ')') return FALSE;
mixguile_interpret_command (line);
return TRUE;
}
return FALSE;
}
#else /* !MAKE_GUILE */
# define try_guile_(ignored) FALSE
#endif /* MAKE_GUILE */
#include "mixvm_command.h"
/* mixvm dispatcher */
static mix_vm_cmd_dispatcher_t *dis_ = NULL;
/* The names of functions that actually do the manipulation. */
#define DEC_FUN(name) \
static gboolean cmd_##name (mix_vm_cmd_dispatcher_t *dis, const char *arg)
DEC_FUN (shell_);
DEC_FUN (quit_);
mix_vm_command_info_t commands[] = {
{ "shell", cmd_shell_, N_("Execute shell command"), "shell COMMAND" },
{ "quit", cmd_quit_, N_("Quit the program"), "quit" },
{ (char *)NULL, (Function *)NULL, (char *)NULL }
};
#ifdef HAVE_LIBREADLINE
/* readline functions */
static char *
mixvm_cmd_generator_ (const char *text, int state);
/* Attempt to complete on the contents of TEXT. START and END bound the
region of rl_line_buffer that contains the word to complete. TEXT is
the word to complete. We can use the entire contents of rl_line_buffer
in case we want to do some simple parsing. Return the array of matches,
or NULL if there aren't any. */
static char **
mixvm_cmd_completion_ (char *text, int start, int end)
{
char **matches;
matches = (char **)NULL;
/* If this word is at the start of the line, then it is a command
to complete. Otherwise it is the name of a file in the current
directory. */
if (start == 0)
matches = rl_completion_matches (text, mixvm_cmd_generator_);
return (matches);
}
/* Generator function for command completion. STATE lets us know whether
to start from scratch; without any state (i.e. STATE == 0), then we
start at the top of the list. */
static char *
mixvm_cmd_generator_ (const char *text, int state)
{
static const GList *comp = NULL;
char *prefix = NULL;
char *name = NULL;
/* If this is a new word to complete, initialize now. */
if (!state)
{
if (prefix) g_free (prefix);
comp = mix_vm_cmd_dispatcher_complete (dis_, text, &prefix);
}
/* Return the next name which partially matches from the command list. */
if (comp)
{
name = g_strdup ((const gchar *)comp->data);
comp = comp->next;
}
return name;
}
#endif /* HAVE_LIBREADLINE */
/* emacs interface */
static void
emacs_output_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg, gpointer data)
{
/* pek: probably bad that we snag the src w/every emacs_output_,
however when multiple files are supported then this will
have to be done each time (but the info will be snagged
from elsewhere...) */
const mix_vm_t *vm = mix_vm_cmd_dispatcher_get_vm (dis);
const mix_src_file_t *src = mix_vm_get_src_file (vm);
const gchar *path = mix_src_file_get_path (src);
mix_address_t loc = mix_vm_get_prog_count (vm);
guint lineno = mix_vm_get_address_lineno (vm, loc);
printf ("\032\032mixvm:%s%s:%d\n", path, MIX_SRC_DEFEXT, lineno);
return;
}
static int
cmd_quit_ (mix_vm_cmd_dispatcher_t *dis, const char *arg)
{
puts ("Quitting ...");
if ( dis_ ) mix_vm_cmd_dispatcher_delete (dis_);
exit (0);
/* pek: anything needed here to make the marker disappear??? */
return FALSE;
}
static int
cmd_shell_ (mix_vm_cmd_dispatcher_t *dis, const char *arg)
{
system (arg);
return TRUE;
}
/* external interface */
static void
init_dis_ (mix_vm_cmd_dispatcher_t *dis)
{
static const gchar * envars[] = { "MDK_EDITOR", "X_EDITOR", "EDITOR",
"VISUAL" };
static const guint s = sizeof (envars) / sizeof (envars[0]);
static const gchar *editor = NULL;
gchar *edit = NULL;
if (!editor)
{
int k;
for (k = 0; k < s; k++)
if ( (editor = getenv (envars[k])) != NULL ) break;
}
if (!editor) editor = "vi";
edit = g_strconcat (editor, " %s", NULL);
mix_vm_cmd_dispatcher_set_editor (dis, edit);
g_free (edit);
mix_vm_cmd_dispatcher_set_assembler (dis, "mixasm -g %s");
}
mix_vm_cmd_dispatcher_t *
mixvm_cmd_init (mix_config_t *config, char *arg, gboolean use_emacs)
{
int k;
#ifdef HAVE_LIBREADLINE
/* Tell the completer that we want a crack first. */
rl_attempted_completion_function = (CPPFunction *)mixvm_cmd_completion_;
#endif /* HAVE_LIBREADLINE */
/* initialise the dispatcher */
dis_ = mix_vm_cmd_dispatcher_new_with_config (stdout, stderr, config);
if ( dis_ == NULL)
g_error (_("Failed initialisation (no memory resources)"));
init_dis_ (dis_);
/* add local commands */
k = 0;
while (commands[k].name)
{
mix_vm_cmd_dispatcher_register_new (dis_, commands + k);
++k;
}
/* install post hook for emacs interaction */
if (use_emacs)
{
mix_vm_cmd_dispatcher_post_hook (dis_, MIX_CMD_LOAD, emacs_output_, NULL);
mix_vm_cmd_dispatcher_post_hook (dis_, MIX_CMD_RUN, emacs_output_, NULL);
mix_vm_cmd_dispatcher_post_hook (dis_, MIX_CMD_NEXT, emacs_output_, NULL);
}
if (arg)
mix_vm_cmd_dispatcher_dispatch (dis_, MIX_CMD_LOAD, arg);
return dis_;
}
gboolean
mixvm_cmd_exec (char *line)
{
if (!line) return cmd_quit_(dis_, NULL);
/* strip white space */
line = g_strstrip(line);
if (try_guile_ (line)) return TRUE;
(void)mix_vm_cmd_dispatcher_dispatch_text (dis_, line);
return TRUE;
}
|