summaryrefslogtreecommitdiffhomepage
path: root/mixutils/mixvm_command.c
blob: d13e5accd496697564e033baa3a6c000784764f1 (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* -*-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>
#else
  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>
#include "mixvm_command.h"

/* The names of functions that actually do the manipulation. */
#define DEC_FUN(name) \
static int cmd_##name (char *arg)

DEC_FUN (help_);
DEC_FUN (shell_);
DEC_FUN (quit_);

/* A structure which contains information on the commands this program
   can understand. */
typedef struct {
  const char *name;		/* User printable name of the function. */
  Function *func;		/* Function to call to do the job. */
  const char *doc;		/* Documentation for this function.  */
  const char *usage;		/* Usage */
} COMMAND;
     
COMMAND commands[] = {
  { "help", cmd_help_, N_("Display this text"), "help [COMMAND]" },
  { "shell", cmd_shell_, N_("Execute shell command"), "shell COMMAND" },
  { "quit", cmd_quit_, N_("Quit the program"), "quit" },
  { (char *)NULL, (Function *)NULL, (char *)NULL }
};

#define LOCAL_COMANDS_NO_  ((sizeof (commands) / sizeof (commands[0])) - 1)
#define MIX_COMMANDS_NO_ MIX_CMD_INVALID+1
#define ALL_COMMANDS_NO_ LOCAL_COMANDS_NO_+MIX_COMMANDS_NO_

static const char *mix_commands_[ALL_COMMANDS_NO_] = {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 int list_index, len;
  const char *name;
     
  /* If this is a new word to complete, initialize now.  This includes
     saving the length of TEXT for efficiency, and initializing the index
     variable to 0. */
  if (!state)
    {
      list_index = 0;
      len = strlen (text);
    }
     
  /* Return the next name which partially matches from the command list. */
  while ((name = mix_commands_[list_index]) != NULL)
    {
      list_index++;
      if (strncmp (name, text, len) == 0)
	return (g_strdup (name));
    }

  /* If no names matched, then return NULL. */
  return ((char *)NULL);
}
#endif /* HAVE_LIBREADLINE */

/* command functions */
static COMMAND *
find_command_ (const char *name)
{
  int i;
     
  for (i = 0; commands[i].name; i++)
    if (strcmp (name, commands[i].name) == 0)
      return (&commands[i]);
     
  return ((COMMAND *)NULL);
}


/* mixvm dispatcher */
static mix_vm_cmd_dispatcher_t *dis_ = NULL;

/* 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_help_ (char *arg)
{
  int i;
  int printed = 0;
     
  for (i = 1; commands[i].name; i++)
    {
      if (!arg || !*arg)
	{
	  printf (_("%s\t\t%s.\n"), commands[i].name, _(commands[i].doc));
	  printed++;
	}
      else if (strcmp (arg, commands[i].name) == 0)
	{
	  printf (_("%s\t\t%s.\nUsage:\t\t%s\n"), commands[i].name, 
		  _(commands[i].doc), commands[i].usage);
	  printed++;
	}
    }
  if (!printed ||!arg || !*arg)
    mix_vm_cmd_dispatcher_dispatch (dis_, MIX_CMD_HELP, arg);
  return TRUE;
}

static int
cmd_quit_ (char *arg)
{
  puts ("Quitting ...");
  if ( dis_ ) mix_vm_cmd_dispatcher_delete (dis_);

  /* pek: anything needed here to make the marker disappear??? */
  return FALSE;
}


static int 
cmd_shell_ (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");
}

void
mixvm_cmd_init (mix_config_t *config, char *arg, gboolean use_emacs)
{
  int k;
  /* get local command names */
  for (k = 0; k < LOCAL_COMANDS_NO_; ++k)
    mix_commands_[k] = commands[k].name;
  /* get external command names */
  for (k = 0; k < MIX_CMD_INVALID; ++k)
    mix_commands_[k + LOCAL_COMANDS_NO_] = mix_vm_command_to_string (k);
  mix_commands_[ALL_COMMANDS_NO_ - 1] = NULL;

#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_);
  
  /* 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);
}

gboolean
mixvm_cmd_exec (char *line)
{
  int i;
  COMMAND *command;
  char *cmd, *arg;
  mix_vm_command_t mix_cmd;

  if (!line) return cmd_quit_(NULL);
  
  /* strip white  space */
  line = g_strstrip(line);
   
  /* Isolate the command word. */
  i = 0;
  while (line[i] && isspace (line[i]))
    i++;
  cmd = line + i;
     
  while (line[i] && !isspace (line[i]))
    i++;
     
  if (line[i])
    line[i++] = '\0';
     
  if (cmd == NULL || strlen (cmd) == 0)
    return TRUE;
  
  /* Get argument to command, if any. */
  while (isspace (line[i]))
    i++;
     
  arg = line + i;
     
  /* try to find local command */
  command = find_command_ (cmd);
  if (command)
    return ((*(command->func)) (arg));

  /* try to find mix command */
  mix_cmd = mix_vm_command_from_string (cmd);
  
  if (mix_cmd == MIX_CMD_INVALID)
    fprintf (stderr, _("%s: No such command. Try \'help\'\n"), cmd);
  else
    mix_vm_cmd_dispatcher_dispatch (dis_, mix_cmd, arg);
  
  return TRUE;
}