From 9d97119cb4f191b7fa25a2d4913dd8bb41069776 Mon Sep 17 00:00:00 2001
From: jaortega <jaortega>
Date: Wed, 14 Feb 2001 23:33:09 +0000
Subject: mixvm-gud interface added

---
 Makefile.am              |   2 +-
 configure.in             |   1 +
 misc/.cvsignore          |   2 +
 misc/Makefile.am         |  14 ++++
 misc/mixvm.el            | 159 ++++++++++++++++++++++++++++++++++++++++++++
 mixutils/mixvm.c         |  16 +++--
 mixutils/mixvm_command.c | 169 +++++++++++++++++++++++++++++++++--------------
 mixutils/mixvm_command.h |   7 +-
 mixutils/mixvm_loop.c    |   6 +-
 samples/primes.mixal     |   1 +
 10 files changed, 314 insertions(+), 63 deletions(-)
 create mode 100644 misc/.cvsignore
 create mode 100644 misc/Makefile.am
 create mode 100644 misc/mixvm.el

diff --git a/Makefile.am b/Makefile.am
index 7863fa3..ba12b01 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -11,5 +11,5 @@
 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 EXTRA_DIST = ABOUT-NLS  autogen.sh configure
-SUBDIRS = po intl doc mixlib mixutils mixmvc samples
+SUBDIRS = po intl doc mixlib mixutils mixmvc misc samples
 
diff --git a/configure.in b/configure.in
index 907190a..d69128e 100644
--- a/configure.in
+++ b/configure.in
@@ -51,6 +51,7 @@ mixlib/Makefile
 mixlib/testsuite/Makefile 
 mixutils/Makefile 
 mixmvc/Makefile
+misc/Makefile
 intl/Makefile 
 samples/Makefile
 po/Makefile.in 
diff --git a/misc/.cvsignore b/misc/.cvsignore
new file mode 100644
index 0000000..282522d
--- /dev/null
+++ b/misc/.cvsignore
@@ -0,0 +1,2 @@
+Makefile
+Makefile.in
diff --git a/misc/Makefile.am b/misc/Makefile.am
new file mode 100644
index 0000000..bf21fc6
--- /dev/null
+++ b/misc/Makefile.am
@@ -0,0 +1,14 @@
+## Process this file with automake to produce Makefile.in
+
+# Copyright (C) 2001 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.
+
+EXTRA_DIST = mixvm.el
+
diff --git a/misc/mixvm.el b/misc/mixvm.el
new file mode 100644
index 0000000..0a5e474
--- /dev/null
+++ b/misc/mixvm.el
@@ -0,0 +1,159 @@
+;;; mixvm.el --- mdk's mixvm / Emacs gud interaction
+;; Copyright (C) 2001 Philip Ellis King
+     
+;; Author: Philip Ellis King <pking@pdq.net>
+;; Maintainer: Philip Ellis King <pking@pdq.net>
+;; Created: 12 Feb 2001
+;; Version: 0.2
+;; Keywords: tools
+     
+
+;;; Commentary:
+;; mixvm.el provides an interface between mdk's mixvm and Emacs,
+;; via gud.  Place this file in your load-path, optionally adding
+;; the following line to your .emacs file:
+;; (autoload 'mixvm "mixvm" "mixvm/gud interaction" t)
+;; Initiate a mdk/gud session with the command mixvm, gud will
+;; reflect the current line in the source file buffer.
+
+;; (mixvm.el is based on a study of gdb, perldb, and pdb as found
+;; in gud.el, and rubydb3x.el distributed with the source code to
+;; the Ruby language.
+
+;;; Change Log:
+;; Version 0.2
+;; Initial release
+
+
+;;; Code:
+(require 'gud)
+(provide 'mixvm)
+
+;;; History of argument lists passed to mixvm.
+(defvar gud-mixvm-history nil)
+
+;; rubydb3x provided good examples of xemacs/emacs
+;; compatibility (not interested at the moment)
+(defun gud-mixvm-massage-args (file args)
+  (cons "--emacs" args))
+
+;; There's no guarantee that Emacs will hand the filter the entire
+;; marker at once; it could be broken up across several strings.  We
+;; might even receive a big chunk with several markers in it.  If we
+;; receive a chunk of text which looks like it might contain the
+;; beginning of a marker, we save it here between calls to the
+;; filter.
+(defvar gud-mixvm-marker-acc "")
+(make-variable-buffer-local 'gud-mixvm-marker-acc)
+
+(defun gud-mixvm-marker-filter (string)
+  (setq gud-mixvm-marker-acc (concat gud-mixvm-marker-acc string))
+  (let ((output ""))
+
+    ;; Process all the complete markers in this chunk.
+    (while (string-match "\032\032mixvm:\\([^:]+\\):\\([0-9]+\\)"
+                         gud-mixvm-marker-acc)
+      (setq
+
+       ;; Extract the frame position from the marker.
+       gud-last-frame
+       (cons (substring gud-mixvm-marker-acc (match-beginning 1) (match-end 1))
+             (string-to-int (substring gud-mixvm-marker-acc
+                                       (match-beginning 2)
+                                       (match-end 2))))
+
+       ;; Append any text before the marker to the output we're going
+       ;; to return - we don't include the marker in this text.
+       output (concat output
+                      (substring gud-mixvm-marker-acc 0 (match-beginning 0)))
+
+       ;; Set the accumulator to the remaining text.
+       gud-mixvm-marker-acc (substring gud-mixvm-marker-acc (match-end 0))))
+
+    ;; Does the remaining text look like it might end with the
+    ;; beginning of another marker?  If it does, then keep it in
+    ;; gud-mixvm-marker-acc until we receive the rest of it.  Since we
+    ;; know the full marker regexp above failed, it's pretty simple to
+    ;; test for marker starts.
+    ;; (note: \\' matches the end of the string (Perl's '$'))
+    (if (string-match "\032.*\\'" gud-mixvm-marker-acc)
+        (progn
+          ;; Everything before the potential marker start can be output.
+          (setq output (concat output (substring gud-mixvm-marker-acc
+                                                 0 (match-beginning 0))))
+
+          ;; Everything after, we save, to combine with later input.
+          (setq gud-mixvm-marker-acc
+                (substring gud-mixvm-marker-acc (match-beginning 0))))
+
+      (setq output (concat output gud-mixvm-marker-acc)
+            gud-mixvm-marker-acc ""))
+
+    output))
+
+;; See gdb for more comprehensive example
+;; pek: it bugs me that this is run for EVERY interactive
+;; mixvm command, should we cache some info somewhere?
+(defun gud-mixvm-find-file (file)
+  (save-excursion
+    ;; pek: concat'ing .mixal to the end of file is bad!
+    ;; mixvm is truncating the name of the actual file,
+    ;; though, so this will work in the meantime...
+    (let* ((buf (find-file-noselect file)))
+      (set-buffer buf)
+      (gud-make-debug-menu)
+      buf)))
+
+
+(defvar mixvm-minibuffer-local-map nil
+  "Keymap for minibuffer prompting of mixvm startup command.")
+(if mixvm-minibuffer-local-map
+    ()
+  (setq mixvm-minibuffer-local-map (copy-keymap minibuffer-local-map))
+  (define-key
+    mixvm-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
+
+
+(defcustom gud-mixvm-command-name "mixvm"
+  "File name for executing the mixvm debugger.
+This should be an executable on your path, or an absolute file name."
+  :type 'string
+  :group 'gud)
+
+
+;;;###autoload
+(defun mixvm (command-line)
+  "Run mixvm on program FILE in buffer `*gud-FILE*'.
+The directory containing FILE becomes the initial working directory
+and source-file directory for your debugger."
+  (interactive
+   (list (read-from-minibuffer "Run mixvm (like this): "
+                               (if (consp gud-mixvm-history)
+                                   (car gud-mixvm-history)
+                                 (concat gud-mixvm-command-name " "))
+                               mixvm-minibuffer-local-map nil
+                               '(gud-mixvm-history . 1))))
+
+  (gud-common-init command-line 'gud-mixvm-massage-args
+                   'gud-mixvm-marker-filter 'gud-mixvm-find-file)
+
+;   (gud-def gud-break  "break %l"     "\C-b" "Set breakpoint at current line.")
+;   (gud-def gud-remove "clear %l"     "\C-d" "Remove breakpoint at current line")
+;   (gud-def gud-step   "step"         "\C-s" "Step one source line with display.")
+;   (gud-def gud-next   "next"         "\C-n" "Step one line (skip functions).")
+;   (gud-def gud-cont   "continue"     "\C-r" "Continue with display.")
+;   (gud-def gud-finish "return"       "\C-f" "Finish executing current function.")
+;   (gud-def gud-up     "up"           "<" "Up one stack frame.")
+;   (gud-def gud-down   "down"         ">" "Down one stack frame.")
+;   (gud-def gud-print  "p %e"         "\C-p" "Evaluate Python expression at point.")
+;   ;; Is this right?
+;   (gud-def gud-statement "! %e"      "\C-e" "Execute Python statement at point.")
+
+;   (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
+;   (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
+;   (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
+  (setq comint-prompt-regexp "^MIX > *")
+  (set (make-local-variable 'paragraph-start) comint-prompt-regexp)
+  (run-hooks 'mixvm-mode-hook))
+
+;;; mixvm.el ends here
diff --git a/mixutils/mixvm.c b/mixutils/mixvm.c
index edf7ac5..4c28557 100644
--- a/mixutils/mixvm.c
+++ b/mixutils/mixvm.c
@@ -30,7 +30,7 @@
 #include <getopt.h>
 
 extern void
-mix_vmloop (const gchar *code_file);
+mix_vmloop (const gchar *code_file, gboolean use_emacs);
 
 static void
 mix_vmrun (const gchar *code_file, gboolean dump);
@@ -40,10 +40,11 @@ enum {
   HELP_OPT = 'h',
   USAGE_OPT = 'u',
   RUN_OPT = 'r',
-  DUMP_OPT = 'd'
+  DUMP_OPT = 'd',
+  EMACS_OPT = 'e',	/* used by mixvm-gud only */
 };
 
-static const char *options_ = "vhurd";
+static const char *options_ = "vhurd"; /* no short opt for --emacs */
 
 static struct option long_options_[] =
 {
@@ -52,13 +53,14 @@ static struct option long_options_[] =
   {"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)
 {
@@ -67,6 +69,7 @@ main (int argc, char **argv)
   const char *in = NULL;
   gboolean run = FALSE;
   gboolean dump = FALSE;
+  gboolean emacs = FALSE;
       
   setlocale (LC_ALL, "");
   bindtextdomain (PACKAGE, LOCALEDIR);
@@ -101,6 +104,9 @@ main (int argc, char **argv)
 	  /* 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 ();
 	}
@@ -117,7 +123,7 @@ main (int argc, char **argv)
   mix_init_lib ();
   
   if (run) mix_vmrun(in, dump);
-  else mix_vmloop (in);
+  else mix_vmloop (in, emacs);
 
   mix_release_lib ();
   
diff --git a/mixutils/mixvm_command.c b/mixutils/mixvm_command.c
index 3fba0eb..ca72c51 100644
--- a/mixutils/mixvm_command.c
+++ b/mixutils/mixvm_command.c
@@ -1,7 +1,7 @@
 /* -*-c-*- -------------- mixvm_command.c :
  * Implementation of the functions declared in mixvm_command.h
  * ------------------------------------------------------------------
- * Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.org>
+ * 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
@@ -19,7 +19,6 @@
  *  
  */
 
-
 #include <mixlib/mix.h>
 
 #include <stdio.h>
@@ -32,6 +31,7 @@
 #include <mixlib/mix_vm.h>
 #include <mixlib/mix_vm_dump.h>
 #include <mixlib/mix_eval.h>
+#include <mixlib/mix_src_file.h>
 #include "mixvm_command.h"
 
 /* The names of functions that actually do the manipulation. */
@@ -177,7 +177,6 @@ mixvm_cmd_generator_ (char *text, int state)
   return ((char *)NULL);
 }
 
-
 /* command functions */
 static COMMAND *
 find_command_ (const char *name)
@@ -202,7 +201,96 @@ static gboolean is_tracing_ = FALSE;
 /* uptime */
 static mix_time_t uptime_ = 0;
 static mix_time_t prog_time_ = 0;
+/* emacs interface */
+static gboolean emacs_ = FALSE;
+
+
+/* auxiliar methods */
+/* emacs interface */
+static void
+emacs_output_ (void);
+
+/* trace current instruction */
+static void
+trace_ (void);
+
+/* run a program tracing executed instructions */
+static int
+run_and_trace_ (void);
+
+/* print time statistics */
+static void
+print_time_ (void);
+
+
+static void
+emacs_output_ (void)
+{
+  /* 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_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;
+}
+
+/* trace current instruction */
+static void
+trace_ (void)
+{
+  enum {BUFFER_LEN = 128};
+  static gchar STRINS[BUFFER_LEN];
+  
+  const mix_src_file_t *file = mix_vm_get_src_file (vm_);
+  const gchar *line = "\n";
+  mix_address_t loc = mix_vm_get_prog_count (vm_);
+  mix_word_t ins = mix_vm_get_addr_contents (vm_, loc);
+  mix_ins_t fins;
+  mix_word_to_ins_uncheck (ins, fins);
+  mix_ins_to_string_in_buffer (&fins, STRINS, BUFFER_LEN);
+  
+  if (file != NULL)
+    {
+      gulong b = mix_vm_get_break_lineno (vm_);
+      if (b > 0) line = mix_src_file_get_line (file, b);
+    }
+  
+  printf ("%d: [%s]\t%s", (gint)loc, STRINS, line);
+}
+
+/* run a program tracing executed instructions */
+static int
+run_and_trace_ (void)
+{
+  int k = MIX_VM_OK;
+  if (!is_tracing_)
+    return mix_vm_run (vm_);
+  else while (k == MIX_VM_OK)
+    {
+      trace_ ();
+      k = mix_vm_exec_next (vm_);
+    }
+  return k;
+}
+
+/* print time statistics */
+static void
+print_time_ (void)
+{
+  mix_time_t lapse = mix_vm_get_uptime(vm_) - uptime_;
+  uptime_ += lapse;
+  prog_time_ += lapse;
+  printf(_("Elapsed time: %ld /Total program time: %ld (Total uptime: %ld)\n"),
+	 lapse, prog_time_, uptime_);
+}
+
 
+/* commands */
 
 static int
 cmd_help_ (char *arg)
@@ -272,56 +360,14 @@ cmd_load_ (char *arg)
   
   prog_time_ = 0;
   
-  return TRUE;
-}
-
-/* trace current instruction */
-static void
-trace_ (void)
-{
-  enum {BUFFER_LEN = 128};
-  static gchar STRINS[BUFFER_LEN];
-  
-  const mix_src_file_t *file = mix_vm_get_src_file (vm_);
-  const gchar *line = "\n";
-  mix_address_t loc = mix_vm_get_prog_count (vm_);
-  mix_word_t ins = mix_vm_get_addr_contents (vm_, loc);
-  mix_ins_t fins;
-  mix_word_to_ins_uncheck (ins, fins);
-  mix_ins_to_string_in_buffer (&fins, STRINS, BUFFER_LEN);
-  
-  if (file != NULL)
+  if (emacs_)
     {
-      gulong b = mix_vm_get_break_lineno (vm_);
-      if (b > 0) line = mix_src_file_get_line (file, b);
+      emacs_output_();
     }
-  
-  printf ("%d: [%s]\t%s", (gint)loc, STRINS, line);
-}
 
-static int
-run_and_trace_ (void)
-{
-  int k = MIX_VM_OK;
-  if (!is_tracing_)
-    return mix_vm_run (vm_);
-  else while (k == MIX_VM_OK)
-    {
-      trace_ ();
-      k = mix_vm_exec_next (vm_);
-    }
-  return k;
+  return TRUE;
 }
 
-static void
-print_time_ (void)
-{
-  mix_time_t lapse = mix_vm_get_uptime(vm_) - uptime_;
-  uptime_ += lapse;
-  prog_time_ += lapse;
-  printf(_("Elapsed time: %ld /Total program time: %ld (Total uptime: %ld)\n"),
-	 lapse, prog_time_, uptime_);
-}
 
 static int
 cmd_run_ (char *arg)
@@ -360,6 +406,12 @@ cmd_run_ (char *arg)
       break;
     }
   print_time_ ();
+
+  if (emacs_)
+    {
+      emacs_output_();
+    }
+
   return TRUE;
 }
 
@@ -406,6 +458,12 @@ cmd_next_ (char *arg)
 	}
     }
   print_time_ ();
+
+  if (emacs_)
+    {
+      emacs_output_();
+    }
+
   return TRUE;
 }
 
@@ -415,6 +473,8 @@ cmd_quit_ (char *arg)
   puts ("Quitting ...");
   if ( vm_ ) mix_vm_delete (vm_);
   if ( dc_ ) mix_dump_context_delete (dc_);
+
+  /* pek: anything needed here to make the marker disappear??? */
   return FALSE;
 }
 
@@ -1022,10 +1082,12 @@ cmd_troff_ (char *arg)
 
 /* external interface */
 void
-mixvm_cmd_init (char *arg)
+mixvm_cmd_init (char *arg, gboolean use_emacs)
 {
   /* Tell the completer that we want a crack first. */
   rl_attempted_completion_function = (CPPFunction *)mixvm_cmd_completion_;
+  /* set the emacs flag */
+  emacs_ = use_emacs;
   /* initialise the vm */
   vm_ = mix_vm_new ();
   dc_ = mix_dump_context_new (MIX_DUMP_DEF_CHANNEL, 
@@ -1080,6 +1142,15 @@ mixvm_cmd_exec (char *line)
      
   word = line + i;
      
+  /* pek note: as far as I know, only load, run,
+     next, and quit can affect where gud will
+     displays the "marker".  I am toying with the
+     idea of adding some logic before and after
+     the *(command->func) that determines if
+     either the source file or line number have
+     changed.  If another command is added later,
+     then the logic won't have to be inserted... */
+
   /* Call the function. */
   return ((*(command->func)) (word));
 }
diff --git a/mixutils/mixvm_command.h b/mixutils/mixvm_command.h
index 663aa41..9741164 100644
--- a/mixutils/mixvm_command.h
+++ b/mixutils/mixvm_command.h
@@ -1,7 +1,7 @@
 /* -*-c-*- ---------------- mixvm_command.h :
  * Declarations for commands accepted by the mix virtual machine
  * ------------------------------------------------------------------
- * Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.org>
+ * 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
@@ -26,13 +26,10 @@
 #include <glib.h>
 
 extern void
-mixvm_cmd_init (char *arg);
+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
index 309c492..8a571d8 100644
--- a/mixutils/mixvm_loop.c
+++ b/mixutils/mixvm_loop.c
@@ -1,7 +1,7 @@
 /* -*-c-*- -------------- mixvm_loop.c :
  * Implementation of mix vm command loop.
  * ------------------------------------------------------------------
- * Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.org>
+ * 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
@@ -56,9 +56,9 @@ rl_gets ()
 
 /* The main command loop of the virtual machine  */
 void
-mix_vmloop (const gchar *file)
+mix_vmloop (const gchar *file, gboolean use_emacs)
 {
-  mixvm_cmd_init ((char *)file);
+  mixvm_cmd_init ((char *)file, use_emacs);
   while ( mixvm_cmd_exec (rl_gets ()) ) 
     ;
 }
diff --git a/samples/primes.mixal b/samples/primes.mixal
index d2de8c0..d8e6eef 100644
--- a/samples/primes.mixal
+++ b/samples/primes.mixal
@@ -51,3 +51,4 @@ TITLE	ALF	"FIRST"
 	CON	BUF0+10
 	END	START
 
+                             
\ No newline at end of file
-- 
cgit v1.2.3