summaryrefslogtreecommitdiffhomepage
path: root/mixutils
diff options
context:
space:
mode:
authorJose Antonio Ortega Ruiz <jao@gnu.org>2001-03-22 03:01:01 +0000
committerJose Antonio Ortega Ruiz <jao@gnu.org>2001-03-22 03:01:01 +0000
commit1c345e559710ec6200f7d508629bd24457a20a80 (patch)
tree4ebccd7c4719cf56da7151af6659992bd1dbb276 /mixutils
parent50375f34b611281a3b05a37221e2baa143f5f5ca (diff)
downloadmdk-1c345e559710ec6200f7d508629bd24457a20a80.tar.gz
mdk-1c345e559710ec6200f7d508629bd24457a20a80.tar.bz2
initial import (sf 0.3beta)
Diffstat (limited to 'mixutils')
-rw-r--r--mixutils/.cvsignore6
-rw-r--r--mixutils/Makefile.am18
-rw-r--r--mixutils/mixasm.c126
-rw-r--r--mixutils/mixasm_comp.c72
-rw-r--r--mixutils/mixasm_comp.h35
-rw-r--r--mixutils/mixvm.c155
-rw-r--r--mixutils/mixvm_command.c367
-rw-r--r--mixutils/mixvm_command.h35
-rw-r--r--mixutils/mixvm_loop.c86
9 files changed, 900 insertions, 0 deletions
diff --git a/mixutils/.cvsignore b/mixutils/.cvsignore
new file mode 100644
index 0000000..349354d
--- /dev/null
+++ b/mixutils/.cvsignore
@@ -0,0 +1,6 @@
+.deps
+Makefile
+Makefile.in
+mixasm
+mixcmp
+mixvm
diff --git a/mixutils/Makefile.am b/mixutils/Makefile.am
new file mode 100644
index 0000000..2bd03be
--- /dev/null
+++ b/mixutils/Makefile.am
@@ -0,0 +1,18 @@
+## Process this file with automake to produce Makefile.in
+
+# Copyright (C) 2000 Free Software Foundation, Inc.
+#
+# This file is free software; as a special exception the author gives
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+INCLUDES = -I$(includedir)
+LDADD = $(top_builddir)/mixlib/libmix.a
+
+bin_PROGRAMS = mixasm mixvm
+mixasm_SOURCES = mixasm.c mixasm_comp.h mixasm_comp.c
+mixvm_SOURCES = mixvm.c mixvm_loop.c mixvm_command.h mixvm_command.c
diff --git a/mixutils/mixasm.c b/mixutils/mixasm.c
new file mode 100644
index 0000000..64f0f3d
--- /dev/null
+++ b/mixutils/mixasm.c
@@ -0,0 +1,126 @@
+/* -*-c-*- -------------- mixasm.c:
+ * Main function of mixasm, the mix assembler
+ * ------------------------------------------------------------------
+ * Copyright (C) 2000 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 <stdlib.h>
+#include <stdio.h>
+#include <getopt.h>
+
+#include "mixasm_comp.h"
+
+enum {
+ VER_OPT = 'v',
+ HELP_OPT = 'h',
+ USAGE_OPT = 'u',
+ OUT_OPT = 'o',
+ LIST_OPT = 'l',
+ DEBUG_OPT = 'g'
+};
+
+
+static struct option long_options_[] =
+{
+ {"version", no_argument, 0, VER_OPT},
+ {"help", no_argument, 0, HELP_OPT},
+ {"usage", no_argument, 0, USAGE_OPT},
+ {"output", required_argument, 0, OUT_OPT},
+ {"list", optional_argument, 0, VER_OPT},
+ {"debug", no_argument, 0, DEBUG_OPT},
+ {0, 0, 0, 0}
+};
+
+static const gchar *USAGE_ =
+N_("Usage: %s [-vhulg] [-o OUTPUT_FILE] [--version] [--help]\n"
+ "\t[--usage] [--debug] [--output=OUTPUT_FILE] [--list[=LIST_FILE]] file\n");
+
+
+int
+main (int argc, char **argv)
+{
+ int c;
+ const char *prog_name = argv[0];
+ const char *src = NULL, *out = NULL, *list = NULL;
+ gboolean use_list = FALSE, debug = FALSE;
+
+ setlocale (LC_ALL, "");
+ bindtextdomain (PACKAGE, LOCALEDIR);
+ textdomain (PACKAGE);
+
+ while (1)
+ {
+ c = getopt_long (argc, argv, "vhuo:lg", long_options_, (int*)0);
+
+ /* Detect the end of the options. */
+ if (c == -1)
+ break;
+
+ switch (c)
+ {
+ case HELP_OPT: case USAGE_OPT:
+ fprintf (stderr, _(USAGE_), prog_name);
+ return EXIT_SUCCESS;
+ case VER_OPT:
+ fprintf (stderr, _("%s %s, MIX compiler.\n"), prog_name, VERSION);
+ fprintf (stderr, MIX_GPL_LICENSE);
+ return EXIT_SUCCESS;
+ case OUT_OPT:
+ out = optarg;
+ break;
+ case LIST_OPT:
+ use_list = TRUE;
+ list = optarg;
+ break;
+ case DEBUG_OPT:
+ debug = TRUE;
+ break;
+ case '?':
+ /* getopt already handles the output of a warning message */
+ fprintf (stderr, _("(Try: %s -h)\n"), prog_name);
+ return EXIT_FAILURE;
+ default:
+ g_assert_not_reached ();
+ }
+ }
+
+ if ( optind == argc )
+ {
+ fprintf (stderr, _("*** Error: Missing source file.\n"));
+ return EXIT_FAILURE;
+ }
+ if ( optind < argc-1 )
+ {
+ fprintf (stderr, _("*** Error: Too many input files.\n"));
+ return EXIT_FAILURE;
+ }
+ src = argv[optind];
+
+
+ mix_init_lib ();
+
+ c = mix_asm_compile (src, out, use_list, list, debug);
+
+ mix_release_lib ();
+
+ return c;
+
+}
+
diff --git a/mixutils/mixasm_comp.c b/mixutils/mixasm_comp.c
new file mode 100644
index 0000000..87b38be
--- /dev/null
+++ b/mixutils/mixasm_comp.c
@@ -0,0 +1,72 @@
+/* -*-c-*- -------------- mixasm_comp.c :
+ * Implementation of the functions declared in mixasm_comp.h
+ * ------------------------------------------------------------------
+ * Copyright (C) 2000 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 <stdlib.h>
+#include <stdio.h>
+#include <mixlib/mix_parser.h>
+#include "mixasm_comp.h"
+
+int
+mix_asm_compile(const gchar *src, const gchar *out, gboolean use_list,
+ const gchar *list, gboolean debug)
+{
+ int result = EXIT_SUCCESS;
+ mix_parser_t *parser;
+ mix_parser_err_t error;
+
+ if ( (parser = mix_parser_new(src)) == NULL )
+ {
+ fprintf(stderr, _("*** Unable to open source file %s\n"), src);
+ return EXIT_FAILURE;
+ }
+ if ( mix_parser_compile(parser) == MIX_PERR_OK )
+ {
+ guint k;
+ if ( ( k = mix_parser_warning_count(parser) ) != 0 )
+ fprintf(stderr, _("(%d warning(s))\n"), k);
+ if ( (error = mix_parser_write_code(parser, out, debug)) != MIX_PERR_OK )
+ {
+ fprintf(stderr, _("*** Error writing output code file: %s\n"),
+ mix_parser_err_string(error));
+ result = EXIT_FAILURE;
+ }
+ else if ( use_list
+ && (error = mix_parser_write_listing(parser, list)) !=
+ MIX_PERR_OK)
+ {
+ fprintf(stderr, _("*** Error writing listing file: %s\n"),
+ mix_parser_err_string(error));
+ result = EXIT_FAILURE;
+ }
+ }
+ else
+ {
+ fprintf(stderr, _("(%d warning(s), %d error(s))\n"),
+ mix_parser_warning_count(parser), mix_parser_err_count(parser));
+ result = EXIT_FAILURE;
+ }
+
+ mix_parser_delete(parser);
+ return result;
+}
+
+
diff --git a/mixutils/mixasm_comp.h b/mixutils/mixasm_comp.h
new file mode 100644
index 0000000..0fef0dd
--- /dev/null
+++ b/mixutils/mixasm_comp.h
@@ -0,0 +1,35 @@
+/* -*-c-*- ---------------- mixasm_comp.h :
+ * Declarations of functions used to compile mix source files.
+ * ------------------------------------------------------------------
+ * Copyright (C) 2000 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.
+ *
+ */
+
+
+#ifndef MIXASM_COMP_H
+#define MIXASM_COMP_H
+
+#include <glib.h>
+
+extern int
+mix_asm_compile(const gchar *src, const gchar *out, gboolean use_list,
+ const gchar *list, gboolean debug);
+
+
+
+#endif /* MIXASM_COMP_H */
+
diff --git a/mixutils/mixvm.c b/mixutils/mixvm.c
new file mode 100644
index 0000000..eafc8a0
--- /dev/null
+++ b/mixutils/mixvm.c
@@ -0,0 +1,155 @@
+/* -*-c-*- -------------- mixvm.c :
+ * Main function for mixvm, the mix vm simulator
+ * ------------------------------------------------------------------
+ * Copyright (C) 2000 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 <mixlib/mix_vm.h>
+#include <mixlib/mix_vm_dump.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <getopt.h>
+
+extern void
+mix_vmloop (const gchar *code_file, gboolean use_emacs);
+
+static void
+mix_vmrun (const gchar *code_file, gboolean dump);
+
+enum {
+ VER_OPT = 'v',
+ HELP_OPT = 'h',
+ USAGE_OPT = 'u',
+ RUN_OPT = 'r',
+ DUMP_OPT = 'd',
+ EMACS_OPT = 'e', /* used by mixvm-gud only */
+};
+
+static const char *options_ = "vhurd"; /* no short opt for --emacs */
+
+static struct option long_options_[] =
+{
+ {"version", no_argument, 0, VER_OPT},
+ {"help", no_argument, 0, HELP_OPT},
+ {"usage", no_argument, 0, USAGE_OPT},
+ {"run", required_argument, 0, RUN_OPT},
+ {"dump", no_argument, 0, DUMP_OPT},
+ /* pek: yo! */
+ {"emacs", no_argument, 0, EMACS_OPT},
+ {0, 0, 0, 0}
+};
+
+static const gchar *USAGE_ =
+N_("Usage: %s [-vhurd] [--version] [--help] [--usage] [--run] [--dump] [MIX_FILE]\n");
+
+int
+main (int argc, char **argv)
+{
+ int c;
+ const char *prog_name = argv[0];
+ const char *in = NULL;
+ gboolean run = FALSE;
+ gboolean dump = FALSE;
+ gboolean emacs = FALSE;
+
+ setlocale (LC_ALL, "");
+ bindtextdomain (PACKAGE, LOCALEDIR);
+ textdomain (PACKAGE);
+
+ while (1)
+ {
+ c = getopt_long (argc, argv, options_, long_options_, (int*)0);
+
+ /* Detect the end of the options. */
+ if (c == -1)
+ break;
+
+ switch (c)
+ {
+ case HELP_OPT: case USAGE_OPT:
+ fprintf (stderr, _(USAGE_), prog_name);
+ return EXIT_SUCCESS;
+ case VER_OPT:
+ fprintf (stderr, _("%s %s, MIX Virtual Machine.\n"),
+ prog_name, VERSION);
+ fprintf (stderr, MIX_GPL_LICENSE);
+ return EXIT_SUCCESS;
+ case RUN_OPT:
+ in = optarg;
+ run = TRUE;
+ break;
+ case DUMP_OPT:
+ dump = TRUE;
+ break;
+ case '?':
+ /* getopt already handles the output of a warning message */
+ fprintf (stderr, _("(Try: %s -h)\n"), prog_name);
+ return EXIT_FAILURE;
+ case EMACS_OPT:
+ emacs = TRUE;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+ }
+
+ if ( optind < argc-1 )
+ {
+ fprintf (stderr, _("*** Error: Too many input files.\n"));
+ return EXIT_FAILURE;
+ }
+
+ if (!in) in = argv[optind];
+
+ mix_init_lib ();
+
+ if (run) mix_vmrun(in, dump);
+ else mix_vmloop (in, emacs);
+
+ mix_release_lib ();
+
+ return EXIT_SUCCESS;
+
+}
+
+
+static void
+mix_vmrun (const gchar *code_file, gboolean dump)
+{
+ mix_vm_t *vm = mix_vm_new ();
+ if (!mix_vm_load_file (vm, code_file)) {
+ fprintf (stderr, _("Error loading %s file\n"), code_file);
+ return;
+ }
+ mix_vm_run (vm);
+ printf (_("** Execution time: %ld\n"), mix_vm_get_uptime (vm));
+ if (dump) {
+ mix_dump_context_t *dc = mix_dump_context_new (MIX_DUMP_DEF_CHANNEL,
+ 0, 0,
+ MIX_DUMP_ALL_NOMEM);
+ mix_vm_dump (vm, dc);
+ mix_dump_context_delete (dc);
+ }
+ mix_vm_delete (vm);
+}
+
+
diff --git a/mixutils/mixvm_command.c b/mixutils/mixvm_command.c
new file mode 100644
index 0000000..e1c03bf
--- /dev/null
+++ b/mixutils/mixvm_command.c
@@ -0,0 +1,367 @@
+/* -*-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 (compile_);
+DEC_FUN (edit_);
+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" },
+ { "compile", cmd_compile_, N_("Compile a source file"), "compile FILENAME"},
+ { "edit", cmd_edit_, N_("Edit a source file"), "edit FILENAME"},
+ { "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_ (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 = 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_ (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 = 0; commands[i].name; i++)
+ {
+ if (!*arg || (strcmp (arg, commands[i].name) == 0))
+ {
+ printf (_("%s\t\t%s. Usage: %s\n"), commands[i].name,
+ _(commands[i].doc), commands[i].usage);
+ printed++;
+ }
+ }
+
+ if (printed > 1) printf ("\n");
+
+ for (i = LOCAL_COMANDS_NO_ + 1 /* skip help cmd */; mix_commands_[i]; i++)
+ {
+ if (!*arg || (strcmp (arg, mix_commands_[i]) == 0))
+ {
+ printf (_("%s\t\t%s. Usage: %s\n"), mix_commands_[i],
+ mix_vm_command_help (i - LOCAL_COMANDS_NO_),
+ mix_vm_command_usage (i - LOCAL_COMANDS_NO_));
+ printed++;
+ }
+ }
+
+ if (!printed) printf ("Command \'%s\' not found\n", 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;
+}
+
+static int
+cmd_compile_ (char *arg)
+{
+ gchar *line;
+
+ if ( strlen (arg) == 0 )
+ return cmd_help_ ("compile");
+
+ line = g_strconcat ("mixasm -g ", arg, NULL);
+ if ( system (line) == EXIT_SUCCESS )
+ fputs (_("Successful compilation\n"), stderr);
+ g_free (line);
+
+ return TRUE;
+}
+
+static int
+cmd_edit_ (char *arg)
+{
+ static const gchar * envars[] = { "MDK_EDITOR", "X_EDITOR", "EDITOR",
+ "VISUAL" };
+
+ static const guint s = sizeof (envars) / sizeof (envars[0]);
+ static const gchar *editor = NULL;
+
+ if ( strlen (arg) == 0 )
+ return cmd_help_ ("edit");
+
+ if (!editor)
+ {
+ int k;
+ for (k = 0; k < s; k++)
+ if ( (editor = getenv (envars[k])) != NULL ) break;
+ }
+ if (!editor)
+ {
+ int k;
+ fputs (_("Cannot find editor ("), stderr);
+ for (k = 0; k < s; k++)
+ fprintf (stderr, "%s ", envars[k]);
+ fputs (_("undefined)\n"), stderr);
+ }
+ else
+ {
+ gchar *line = g_strconcat (editor, " ", arg, NULL);
+ system (line);
+ g_free (line);
+ }
+
+ return TRUE;
+}
+
+
+
+/* external interface */
+void
+mixvm_cmd_init (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 (stdout, stderr);
+
+ if ( dis_ == NULL)
+ g_error (_("Failed initialisation (no memory resources)"));
+
+ /* 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;
+}
+
+
diff --git a/mixutils/mixvm_command.h b/mixutils/mixvm_command.h
new file mode 100644
index 0000000..9741164
--- /dev/null
+++ b/mixutils/mixvm_command.h
@@ -0,0 +1,35 @@
+/* -*-c-*- ---------------- mixvm_command.h :
+ * Declarations for commands accepted by the mix virtual machine
+ * ------------------------------------------------------------------
+ * 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.
+ *
+ */
+
+
+#ifndef MIXVM_COMMAND_H
+#define MIXVM_COMMAND_H
+
+#include <glib.h>
+
+extern void
+mixvm_cmd_init (char *arg, gboolean use_emacs);
+
+extern gboolean
+mixvm_cmd_exec (char *line);
+
+#endif /* MIXVM_COMMAND_H */
+
diff --git a/mixutils/mixvm_loop.c b/mixutils/mixvm_loop.c
new file mode 100644
index 0000000..a565832
--- /dev/null
+++ b/mixutils/mixvm_loop.c
@@ -0,0 +1,86 @@
+/* -*-c-*- -------------- mixvm_loop.c :
+ * Implementation of mix vm command loop.
+ * ------------------------------------------------------------------
+ * 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.
+ *
+ */
+
+
+#define _GNU_SOURCE 1
+#include <stdio.h>
+
+#include <mixlib/mix.h>
+#include "mixvm_command.h"
+
+#ifdef HAVE_LIBHISTORY
+# include <readline/history.h>
+#else
+# define add_history(x) ((void)0)
+#endif
+
+#ifdef HAVE_LIBREADLINE
+# include <readline/readline.h>
+#else /* !HAVE_LIBREADLINE */
+static char *
+readline (char *prompt)
+{
+ char *line = NULL;
+ size_t s = 0;
+
+ printf ("%s", prompt);
+ getline (&line, &s, stdin);
+ return line;
+}
+#endif /* HAVE_LIBREADLINE */
+
+/* A static variable for holding the line. */
+static char *line_read = (char *)NULL;
+static const char *PROMPT = N_("MIX > ");
+
+/* Read a string, and return a pointer to it. Returns NULL on EOF. */
+static char *
+rl_gets ()
+{
+ /* If the buffer has already been allocated, return the memory
+ to the free pool. */
+ if (line_read)
+ {
+ g_free (line_read);
+ line_read = (char *)NULL;
+ }
+
+ /* Get a line from the user. */
+ line_read = readline ((char *)PROMPT);
+
+ /* If the line has any text in it, save it on the history. */
+ if (line_read && *line_read)
+ add_history (line_read);
+
+ return (line_read);
+}
+
+
+/* The main command loop of the virtual machine */
+void
+mix_vmloop (const gchar *file, gboolean use_emacs)
+{
+ mixvm_cmd_init ((char *)file, use_emacs);
+ while ( mixvm_cmd_exec (rl_gets ()) )
+ ;
+}
+
+