From 5c6e55f60ddc65ffbc9000902719fadf4b44b83e Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 9 Jul 2014 06:29:41 -0400 Subject: [PATCH] DRY/fix debugger invocation code. This time, mostly for remake and gdb --- realgud/debugger/bashdb/core.el | 6 ++-- realgud/debugger/gdb/core.el | 28 +++++++++++------- realgud/debugger/gdb/gdb.el | 49 ++++++++++++------------------- realgud/debugger/kshdb/core.el | 10 +++++-- realgud/debugger/remake/core.el | 16 ++++++++-- realgud/debugger/remake/remake.el | 42 +++++--------------------- realgud/debugger/trepan/core.el | 6 ++++ test/gcd.sh | 25 ++++++++++++++++ test/test-bashdb.el | 13 ++++++-- test/test-gub-core.el | 1 + test/test-remake-core.el | 11 +++++-- 11 files changed, 121 insertions(+), 86 deletions(-) create mode 100755 test/gcd.sh diff --git a/realgud/debugger/bashdb/core.el b/realgud/debugger/bashdb/core.el index 99ac3c71c..463238376 100644 --- a/realgud/debugger/bashdb/core.el +++ b/realgud/debugger/bashdb/core.el @@ -7,6 +7,7 @@ "realgud-") (require-relative-list '("init") "realgud:bashdb-") +(declare-function realgud:expand-file-name-if-exists 'realgud-core) (declare-function realgud-parse-command-arg 'realgud-core) (declare-function realgud-query-cmdline 'realgud-core) (declare-function realgud-suggest-invocation 'realgud-core) @@ -133,7 +134,8 @@ Note that path elements have been expanded via `expand-file-name'. ((member arg '("--library" "-l")) (setq arg (pop args)) (nconc debugger-args - (list arg (expand-file-name (pop args))))) + (list arg (realgud:expand-file-name-if-exists + (pop args))))) ;; Other options with arguments. ((string-match "^-" arg) (setq pair (realgud-parse-command-arg @@ -141,7 +143,7 @@ Note that path elements have been expanded via `expand-file-name'. (nconc debugger-args (car pair)) (setq args (cadr pair))) ;; Anything else must be the script to debug. - (t (setq script-name (expand-file-name arg)) + (t (setq script-name (realgud:expand-file-name-if-exists arg)) (setq script-args (cons script-name (cdr args)))) ))) (list interpreter-args debugger-args script-args annotate-p)))) diff --git a/realgud/debugger/gdb/core.el b/realgud/debugger/gdb/core.el index 99aa8e8fd..65ebd9a1c 100644 --- a/realgud/debugger/gdb/core.el +++ b/realgud/debugger/gdb/core.el @@ -7,6 +7,7 @@ "../../common/lang") "realgud-") +(declare-function realgud:expand-file-name-if-exists 'realgud-core) (declare-function realgud-lang-mode? 'realgud-lang) (declare-function realgud-parse-command-arg 'realgud-core) (declare-function realgud-query-cmdline 'realgud-core) @@ -34,21 +35,22 @@ (defun realgud:gdb-parse-cmd-args (orig-args) "Parse command line ARGS for the annotate level and name of script to debug. -ARGS should contain a tokenized list of the command line to run. +ORIG_ARGS should contain a tokenized list of the command line to run. We return the a list containing -- the name of the debugger given (e.g. gdb) and its arguments - a list of strings -- the script name and its arguments - list of strings -- whether the annotate or emacs option was given ('-A', '--annotate' or '--emacs) - a boolean +* the name of the debugger given (e.g. gdb) and its arguments - a list of strings +* nil (a placehoder in other routines of this ilk for a debugger +* the script name and its arguments - list of strings +* whether the annotate or emacs option was given ('-A', '--annotate' or '--emacs) - a boolean For example for the following input (map 'list 'symbol-name - '(gdb --tty /dev/pts/1 --emacs ./gcd.py a b)) + '(gdb --tty /dev/pts/1 -cd ~ --emacs ./gcd.py a b)) we might return: - ((gdb --tty /dev/pts/1 --emacs) (./gcd.py a b) 't) + ((\"gdb\" \"--tty\" \"/dev/pts/1\" \"-cd\" \"home/rocky\' \"--emacs\") nil \"(/tmp/gcd.py a b\") 't\") -NOTE: the above should have each item listed in quotes. +Note that path elements have been expanded via `expand-file-name'. " ;; Parse the following kind of pattern: @@ -74,8 +76,8 @@ NOTE: the above should have each item listed in quotes. (annotate-p nil)) (if (not (and args)) - ;; Got nothing: return '(nil nil nil) - (list debugger-args script-args annotate-p) + ;; Got nothing: return '(nil nil nil nil) + (list debugger-args nil script-args annotate-p) ;; else (progn @@ -102,6 +104,12 @@ NOTE: the above should have each item listed in quotes. ((string-match "^--annotate=[0-9]" arg) (nconc debugger-args (list (pop args) (pop args)) ) (setq annotate-p t)) + ;; path-argument ooptions + ((member arg '("-cd" )) + (setq arg (pop args)) + (nconc debugger-args + (list arg (realgud:expand-file-name-if-exists + (pop args))))) ;; Options with arguments. ((string-match "^-" arg) (setq pair (realgud-parse-command-arg @@ -112,7 +120,7 @@ NOTE: the above should have each item listed in quotes. (t (setq script-name arg) (setq script-args args)) ))) - (list debugger-args script-args annotate-p))))) + (list debugger-args nil script-args annotate-p))))) (defvar realgud:gdb-command-name) (defun realgud:gdb-suggest-invocation (debugger-name) diff --git a/realgud/debugger/gdb/gdb.el b/realgud/debugger/gdb/gdb.el index e8868fe60..2302706c5 100644 --- a/realgud/debugger/gdb/gdb.el +++ b/realgud/debugger/gdb/gdb.el @@ -25,21 +25,22 @@ This should be an executable on your path, or an absolute file name." :type 'string :group 'realgud:gdb) -(declare-function gdb-track-mode (bool)) +(declare-function realgud:gdb-track-mode 'realgud:gdb-track-mode) (declare-function realgud-command 'realgud:gdb-core) (declare-function realgud:gdb-parse-cmd-args 'realgud:gdb-core) (declare-function realgud:gdb-query-cmdline 'realgud:gdb-core) -(declare-function realgud-run-process 'realgud-core) +(declare-function realgud:run-process 'realgud-core) ;; ------------------------------------------------------------------- ;; The end. ;; +(require 'list-utils) ;;;###autoload -(defun realgud:gdb (&optional opt-command-line no-reset) +(defun realgud:gdb (&optional opt-cmd-line no-reset) "Invoke the gdb debugger and start the Emacs user interface. -OPT-COMMAND-LINE is treated like a shell string; arguments are +OPT-CMD-LINE is treated like a shell string; arguments are tokenized by `split-string-and-unquote'. Normally, command buffers are reused when the same debugger is @@ -52,13 +53,16 @@ fringe and marginal icons. " (interactive) - (let* ((cmd-str (or opt-command-line (realgud:gdb-query-cmdline "gdb"))) + (let* ((cmd-str (or opt-cmd-line (realgud:gdb-query-cmdline "gdb"))) (cmd-args (split-string-and-unquote cmd-str)) (parsed-args (realgud:gdb-parse-cmd-args cmd-args)) - (script-args (cdr cmd-args)) - (script-name (expand-file-name (car script-args))) - (cmd-buf (realgud-run-process realgud:gdb-command-name (car script-args) cmd-args - 'realgud:gdb-track-mode nil)) + (script-args (caddr parsed-args)) + (script-name (car script-args)) + (parsed-cmd-args + (remove-if 'nil (list-utils-flatten parsed-args))) + (cmd-buf (realgud:run-process realgud:gdb-command-name + script-name parsed-cmd-args + 'realgud:gdb-track-mode-hook nil)) ) (if cmd-buf (with-current-buffer cmd-buf @@ -66,27 +70,10 @@ fringe and marginal icons. ) ) ) - - ;; ;; Parse the command line and pick out the script name and whether - ;; ;; --annotate has been set. - - ;; (condition-case nil - ;; (setq cmd-buf - ;; (apply 'realgud-exec-shell "gdb" (car script-args) - ;; (car cmd-args) nil - ;; (cons script-name (cddr cmd-args)))) - ;; (error nil)) - ;; ;; FIXME: Is there probably is a way to remove the - ;; ;; below test and combine in condition-case? - ;; (let ((process (get-buffer-process cmd-buf))) - ;; (if (and process (eq 'run (process-status process))) - ;; (progn - ;; (switch-to-buffer cmd-buf) - ;; (realgud:gdb-track-mode 't) - ;; (realgud-cmdbuf-info-cmd-args= cmd-args) - ;; ) - ;; (message "Error running gdb command")) - ;; ) - ) + ) (provide-me "realgud-") + +;; Local Variables: +;; byte-compile-warnings: (not cl-functions) +;; End: diff --git a/realgud/debugger/kshdb/core.el b/realgud/debugger/kshdb/core.el index bc73cc86d..853cf461e 100644 --- a/realgud/debugger/kshdb/core.el +++ b/realgud/debugger/kshdb/core.el @@ -5,6 +5,11 @@ (require-relative-list '("../../common/track" "../../common/core") "realgud-") (require-relative-list '("init") "realgud:kshdb-") +(declare-function realgud:expand-file-name-if-exists 'realgud-core) +(declare-function realgud-parse-command-arg 'realgud-core) +(declare-function realgud-query-cmdline 'realgud-core) +(declare-function realgud-suggest-invocation 'realgud-core) + ;; FIXME: I think the following could be generalized and moved to ;; realgud-... probably via a macro. (defvar kshdb-minibuffer-history nil @@ -24,6 +29,7 @@ 'kshdb-minibuffer-history opt-debugger)) +;;; FIXME: DRY this with other *-parse-cmd-args routines (defun kshdb-parse-cmd-args (orig-args) "Parse command line ARGS for the annotate level and name of script to debug. @@ -121,8 +127,8 @@ NOTE: the above should have each item listed in quotes. (nconc debugger-args (car pair)) (setq args (cadr pair))) ;; Anything else must be the script to debug. - (t (setq script-name arg) - (setq script-args args)) + (t (setq script-name (realgud:expand-file-name-if-exists arg)) + (setq script-args (cons script-name (cdr args)))) ))) (list interpreter-args debugger-args script-args annotate-p)))) diff --git a/realgud/debugger/remake/core.el b/realgud/debugger/remake/core.el index 9eed834fd..1f51c1645 100644 --- a/realgud/debugger/remake/core.el +++ b/realgud/debugger/remake/core.el @@ -2,9 +2,20 @@ (eval-when-compile (require 'cl)) (require 'load-relative) -(require-relative-list '("../../common/track" "../../common/core") "realgud-") +(require-relative-list '("../../common/track" "../../common/core") + "realgud-") +(require-relative-list '("../../common/buffer/command") + "realgud-buffer-") (require-relative-list '("init") "realgud:remake-") +(declare-function realgud:expand-file-name-if-exists 'realgud-core) +(declare-function realgud-parse-command-arg 'realgud-core) +(declare-function realgud-query-cmdline 'realgud-core) +(declare-function realgud-suggest-invocation 'realgud-core) +(declare-function realgud-cmdbuf-command-string + 'realgud-buffer-command) +(declare-function realgud-cmdbuf-debugger-name + 'realgud-buffer-command) ;; FIXME: I think the following could be generalized and moved to ;; realgud-... probably via a macro. (defvar remake-minibuffer-history nil @@ -83,7 +94,8 @@ we might return: ((member arg '("--file" "--makefile" "-f")) (setq remake-args (nconc remake-args (list arg))) - (setq makefile-name (pop args)) + (setq makefile-name (realgud:expand-file-name-if-exists + (pop args))) (setq remake-args (nconc remake-args (list (format "%s" makefile-name))))) diff --git a/realgud/debugger/remake/remake.el b/realgud/debugger/remake/remake.el index 32fd81976..7aad9d816 100644 --- a/realgud/debugger/remake/remake.el +++ b/realgud/debugger/remake/remake.el @@ -16,7 +16,7 @@ (declare-function remake-query-cmdline 'realgud:remake-core) (declare-function remake-parse-cmd-args 'realgud:remake-core) -(declare-function realgud:run-process 'realgud:run) +(declare-function realgud:run-debugger 'realgud:run) ;; ------------------------------------------------------------------- ;; User definable variables @@ -31,40 +31,14 @@ This should be an executable on your path, or an absolute file name." :group 'remake) ;;;###autoload -(defun realgud:remake-fn (&optional opt-cmd-line no-reset) +(defun realgud:remake (&optional opt-cmd-line no-reset) "See `realgud:remake' for details" - - (let* ((cmd-str (or opt-cmd-line (remake-query-cmdline "remake"))) - (cmd-args (split-string-and-unquote cmd-str)) - (parsed-args (remake-parse-cmd-args cmd-args)) - (remake-program (car parsed-args)) - (makefile-name (or (cadr parsed-args) "Makefile")) - (makefile-args (caddr parsed-args)) - (cmd-buf)) - (realgud:run-process "remake" makefile-name - (cons remake-program makefile-args) - 'remake-track-mode no-reset) - - ;; ;; Parse the command line and pick out the script name and whether - ;; ;; --annotate has been set. - - ;; (condition-case nil - ;; (setq cmd-buf - ;; (apply 'realgud-exec-shell "remake" makefile-name - ;; remake-program no-reset makefile-args)) - ;; (error nil)) - ;; ;; FIXME: Is there probably is a way to remove the - ;; ;; below test and combine in condition-case? - ;; (let ((process (get-buffer-process cmd-buf))) - ;; (if (and process (eq 'run (process-status process))) - ;; (progn - ;; (switch-to-buffer cmd-buf) - ;; (remake-track-mode 't) - ;; (realgud-cmdbuf-info-cmd-args= cmd-args) - ;; ) - ;; (message "Error running remake command")) - ;; ) - ) + (interactive) + (realgud:run-debugger "remake" + 'remake-query-cmdline + 'remake-parse-cmd-args + 'remake-track-mode-hook + opt-cmd-line no-reset) ) (defalias 'remake 'realgud:remake) diff --git a/realgud/debugger/trepan/core.el b/realgud/debugger/trepan/core.el index 3d51ab538..b843cc0df 100644 --- a/realgud/debugger/trepan/core.el +++ b/realgud/debugger/trepan/core.el @@ -129,6 +129,12 @@ Note that the script name path has been expanded via `expand-file-name'. ((string-match "^--annotate=[0-9]" arg) (nconc debugger-args (list (pop args)) ) (setq annotate-p t)) + ;; path-argument options + ((member arg '("--include" "-I" "--require" "-I")) + (setq arg (pop args)) + (nconc debugger-args + (list arg (realgud:expand-file-name-if-exists + (pop args))))) ;; Options with arguments. ((string-match "^-" arg) (setq pair (realgud-parse-command-arg diff --git a/test/gcd.sh b/test/gcd.sh new file mode 100755 index 000000000..497f33bc1 --- /dev/null +++ b/test/gcd.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Greatest Common Divisor in POSIX shell using Euclid's algorithm. On +# return, variable gcd_value is set and is the gcd of parameters $1 +# and $2. The GCD of a negative number is the same as the GCD of its +# absolute value, since a negative number is -1 times its positive +# value. Negative numbers are set when there is an error; -1 is set +# when the wrong number of parameters are given. +gcd() { + typeset -i a=$1 + typeset -i b=$2 + if (( a > b )) ; then + a=$b + b=$1 + fi + if (( a == 1 || (b-a) == 0)) ; then + gcd_value=$a + return 0 + fi + typeset -i c + ((c=b-a)) + gcd $c $a +} + +gcd $1 $2 +echo $gcd_value diff --git a/test/test-bashdb.el b/test/test-bashdb.el index 92b0c3261..5c33b63c9 100644 --- a/test/test-bashdb.el +++ b/test/test-bashdb.el @@ -1,5 +1,8 @@ (require 'test-simple) (load-file "../realgud/debugger/bashdb/bashdb.el") +(load-file "../realgud/common/core.el") + +(declare-function realgud:expand-file-name-if-exists 'realgud-core) (eval-when-compile (defvar test:run-process-save)) @@ -18,7 +21,8 @@ (format "%s %s %s %S" debugger-name script-filename cmd-args track-mode-func)) (assert-equal "bashdb" debugger-name "debugger name gets passed") - (let ((expanded-name (expand-file-name "./gcd.sh"))) + (let ((expanded-name + (realgud:expand-file-name-if-exists "./gcd.sh"))) (assert-equal expanded-name script-filename "file name check") (assert-equal (list "-l" (expand-file-name ".") expanded-name "3" "5") (cdr cmd-args) "command args listified") @@ -26,9 +30,12 @@ )) (note "bashdb-parse-cmd-args") -(assert-equal (list nil '("bashdb") (list (expand-file-name "foo")) nil) +(assert-equal (list nil '("bashdb") + (list (realgud:expand-file-name-if-exists "foo")) nil) (bashdb-parse-cmd-args '("bashdb" "foo"))) -(assert-equal (list nil '("bashdb") (list (expand-file-name "program.sh") "foo") nil) +(assert-equal (list nil '("bashdb") + (list (realgud:expand-file-name-if-exists "program.sh") + "foo") nil) (bashdb-parse-cmd-args '("bashdb" "program.sh" "foo"))) diff --git a/test/test-gub-core.el b/test/test-gub-core.el index b9d77236a..8560d4a94 100644 --- a/test/test-gub-core.el +++ b/test/test-gub-core.el @@ -1,6 +1,7 @@ (require 'test-simple) (load-file "../realgud/debugger/gub/core.el") +(declare-function __FILE__ 'require-relative) (declare-function gub-parse-cmd-args 'realgud-gub-core) (test-simple-start) diff --git a/test/test-remake-core.el b/test/test-remake-core.el index 877ac6453..0a2022787 100644 --- a/test/test-remake-core.el +++ b/test/test-remake-core.el @@ -1,11 +1,18 @@ (require 'test-simple) (load-file "../realgud/debugger/remake/core.el") +(declare-function __FILE__ 'require-relative) +(declare-function remake-parse-cmd-args 'realgud-remake-core) +(declare-function remake-suggest-Makefile 'realgud-remake-core) +(declare-function remake-suggest-file-priority 'realgud-remake-core) + (test-simple-start) -(assert-equal '("remake" "Makefile" ("-X" "-f" "Makefile")) +(assert-equal (list "remake" (expand-file-name "Makefile") + (list "-X" "-f" (expand-file-name "Makefile"))) (remake-parse-cmd-args - '("remake" "-X" "-f" "Makefile")) "remake-parse-cmd-args") + '("remake" "-X" "-f" "Makefile")) + "remake-parse-cmd-args") (assert-equal "Makefile" (remake-suggest-Makefile) "remake-suggest-Makefile") -- 2.39.2