;;; net-utils.el --- network functions
-;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-;; 2005, 2006, 2007 Free Software Foundation, Inc.
+;; Copyright (C) 1998-2011 Free Software Foundation, Inc.
;; Author: Peter Breton <pbreton@cs.umb.edu>
;; Created: Sun Mar 16 1997
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs 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 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
-(eval-when-compile
- (require 'comint))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customization Variables
:group 'comm
:version "20.3")
-(defcustom net-utils-remove-ctl-m
- (member system-type (list 'windows-nt 'msdos))
+(defcustom net-utils-remove-ctl-m (memq system-type '(windows-nt msdos))
"If non-nil, remove control-Ms from output."
:group 'net-utils
:type 'boolean)
;; On GNU/Linux and Irix, the system's ping program seems to send packets
;; indefinitely unless told otherwise
(defcustom ping-program-options
- (and (memq system-type (list 'linux 'gnu/linux 'irix))
+ (and (memq system-type '(gnu/linux irix))
(list "-c" "4"))
"Options for the ping program.
These options can be used to limit how many ICMP packets are emitted."
:group 'net-utils
:type '(repeat string))
-(defcustom ipconfig-program
+(define-obsolete-variable-alias 'ipconfig-program 'ifconfig-program "22.2")
+
+(defcustom ifconfig-program
(if (eq system-type 'windows-nt)
"ipconfig"
"ifconfig")
:group 'net-utils
:type 'string)
-(defcustom ipconfig-program-options
+(define-obsolete-variable-alias 'ipconfig-program-options
+ 'ifconfig-program-options "22.2")
+
+(defcustom ifconfig-program-options
(list
(if (eq system-type 'windows-nt)
"/all" "-a"))
- "Options for ipconfig-program."
+ "Options for the ifconfig program."
:group 'net-utils
:type '(repeat string))
-(defcustom netstat-program "netstat"
+(defcustom iwconfig-program "iwconfig"
+ "Program to print wireless network configuration information."
+ :group 'net-utils
+ :type 'string
+ :version "23.1")
+
+(defcustom iwconfig-program-options nil
+ "Options for the iwconfig program."
+ :group 'net-utils
+ :type '(repeat string)
+ :version "23.1")
+
+(defcustom netstat-program "netstat"
"Program to print network statistics."
:group 'net-utils
:type 'string)
(defcustom netstat-program-options
(list "-a")
- "Options for netstat-program."
+ "Options for the netstat program."
:group 'net-utils
:type '(repeat string))
-(defcustom arp-program "arp"
+(defcustom arp-program "arp"
"Program to print IP to address translation tables."
:group 'net-utils
:type 'string)
(defcustom arp-program-options
(list "-a")
- "Options for arp-program."
+ "Options for the arp program."
:group 'net-utils
:type '(repeat string))
(if (eq system-type 'windows-nt)
(list "print")
(list "-r"))
- "Options for route-program."
+ "Options for the route program."
:group 'net-utils
:type '(repeat string))
-(defcustom nslookup-program "nslookup"
+(defcustom nslookup-program "nslookup"
"Program to interactively query DNS information."
:group 'net-utils
:type 'string)
-(defcustom nslookup-program-options nil
- "List of options to pass to the nslookup program."
+(defcustom nslookup-program-options nil
+ "Options for the nslookup program."
:group 'net-utils
:type '(repeat string))
:group 'net-utils
:type 'regexp)
-(defcustom dig-program "dig"
+(defcustom dig-program "dig"
"Program to query DNS information."
:group 'net-utils
:type 'string)
(defcustom ftp-program "ftp"
- "Progam to run to do FTP transfers."
+ "Program to run to do FTP transfers."
:group 'net-utils
:type 'string)
(defcustom ftp-program-options nil
- "List of options to pass to the FTP program."
+ "Options for the ftp program."
:group 'net-utils
:type '(repeat string))
:type 'string)
(defcustom smbclient-program-options nil
- "List of options to pass to the smbclient program."
+ "Options for the smbclient program."
:group 'net-utils
:type '(repeat string))
:group 'net-utils
:type 'regexp)
-(defcustom dns-lookup-program "host"
+(defcustom dns-lookup-program "host"
"Program to interactively query DNS information."
:group 'net-utils
- :type 'string
- )
+ :type 'string)
-(defcustom dns-lookup-program-options nil
- "List of options to pass to the dns-lookup program."
+(defcustom dns-lookup-program-options nil
+ "Options for the dns-lookup program."
:group 'net-utils
- :type '(repeat string)
- )
+ :type '(repeat string))
;; Internal variables
(defvar network-connection-service nil)
;; Nslookup goodies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(defconst nslookup-font-lock-keywords
- (progn
- (defvar font-lock-type-face)
- (defvar font-lock-keyword-face)
- (defvar font-lock-variable-name-face)
- (require 'font-lock)
- (list
- (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face)
- (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
- 1 font-lock-keyword-face)
- ;; Dotted quads
- (list
- (mapconcat 'identity
- (make-list 4 "[0-9]+")
- "\\.")
- 0 font-lock-variable-name-face)
- ;; Host names
- (list
- (let ((host-expression "[-A-Za-z0-9]+"))
- (concat
- (mapconcat 'identity
- (make-list 2 host-expression)
- "\\.")
- "\\(\\." host-expression "\\)*"))
- 0 font-lock-variable-name-face)))
+(defvar nslookup-font-lock-keywords
+ (list
+ (list "^[A-Za-z0-9 _]+:" 0 'font-lock-type-face)
+ (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
+ 1 'font-lock-keyword-face)
+ ;; Dotted quads
+ (list
+ (mapconcat 'identity
+ (make-list 4 "[0-9]+")
+ "\\.")
+ 0 'font-lock-variable-name-face)
+ ;; Host names
+ (list
+ (let ((host-expression "[-A-Za-z0-9]+"))
+ (concat
+ (mapconcat 'identity
+ (make-list 2 host-expression)
+ "\\.")
+ "\\(\\." host-expression "\\)*"))
+ 0 'font-lock-variable-name-face))
"Expressions to font-lock for nslookup.")
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; General network utilities mode
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defvar net-utils-font-lock-keywords
+ (list
+ ;; Dotted quads
+ (list
+ (mapconcat 'identity (make-list 4 "[0-9]+") "\\.")
+ 0 'font-lock-variable-name-face)
+ ;; Simple rfc4291 addresses
+ (list (concat
+ "\\( \\([[:xdigit:]]+\\(:\\|::\\)\\)+[[:xdigit:]]+\\)"
+ "\\|"
+ "\\(::[[:xdigit:]]+\\)")
+ 0 'font-lock-variable-name-face)
+ ;; Host names
+ (list
+ (let ((host-expression "[-A-Za-z0-9]+"))
+ (concat
+ (mapconcat 'identity (make-list 2 host-expression) "\\.")
+ "\\(\\." host-expression "\\)*"))
+ 0 'font-lock-variable-name-face))
+ "Expressions to font-lock for general network utilities.")
+
+(define-derived-mode net-utils-mode special-mode "NetworkUtil"
+ "Major mode for interacting with an external network utility."
+ (set (make-local-variable 'font-lock-defaults)
+ '((net-utils-font-lock-keywords))))
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Utility functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(skip-chars-backward ":;.,!?" pt)
(point)))))
-
(defun net-utils-remove-ctrl-m-filter (process output-string)
"Remove trailing control Ms."
(let ((old-buffer (current-buffer))
(unwind-protect
(let ((moving))
(set-buffer (process-buffer process))
- (setq moving (= (point) (process-mark process)))
-
- (while (string-match "\r" filtered-string)
- (setq filtered-string
- (replace-match "" nil nil filtered-string)))
-
- (save-excursion
- ;; Insert the text, moving the process-marker.
- (goto-char (process-mark process))
- (insert filtered-string)
- (set-marker (process-mark process) (point)))
+ (let ((inhibit-read-only t))
+ (setq moving (= (point) (process-mark process)))
+
+ (while (string-match "\r" filtered-string)
+ (setq filtered-string
+ (replace-match "" nil nil filtered-string)))
+
+ (save-excursion
+ ;; Insert the text, moving the process-marker.
+ (goto-char (process-mark process))
+ (insert filtered-string)
+ (set-marker (process-mark process) (point))))
(if moving (goto-char (process-mark process))))
(set-buffer old-buffer))))
-(defmacro net-utils-run-program (name header program &rest args)
+(defun net-utils-run-program (name header program args)
"Run a network information program."
- ` (let ((buf (get-buffer-create (concat "*" ,name "*"))))
- (set-buffer buf)
- (erase-buffer)
- (insert ,header "\n")
- (set-process-filter
- (apply 'start-process ,name buf ,program ,@args)
- 'net-utils-remove-ctrl-m-filter)
- (display-buffer buf)
- buf))
+ (let ((buf (get-buffer-create (concat "*" name "*"))))
+ (set-buffer buf)
+ (erase-buffer)
+ (insert header "\n")
+ (set-process-filter
+ (apply 'start-process name buf program args)
+ 'net-utils-remove-ctrl-m-filter)
+ (display-buffer buf)
+ buf))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; General network utilities (diagnostic)
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defun net-utils-run-simple (buffer-name program-name args)
+ "Run a network utility for diagnostic output only."
+ (interactive)
+ (when (get-buffer buffer-name)
+ (kill-buffer buffer-name))
+ (get-buffer-create buffer-name)
+ (with-current-buffer buffer-name
+ (net-utils-mode)
+ (set-process-filter
+ (apply 'start-process (format "%s" program-name)
+ buffer-name program-name args)
+ 'net-utils-remove-ctrl-m-filter)
+ (goto-char (point-min)))
+ (display-buffer buffer-name))
+
+;;;###autoload
+(defun ifconfig ()
+ "Run ifconfig and display diagnostic output."
+ (interactive)
+ (net-utils-run-simple
+ (format "*%s*" ifconfig-program)
+ ifconfig-program
+ ifconfig-program-options))
+
+(defalias 'ipconfig 'ifconfig)
+
+;;;###autoload
+(defun iwconfig ()
+ "Run iwconfig and display diagnostic output."
+ (interactive)
+ (net-utils-run-simple
+ (format "*%s*" iwconfig-program)
+ iwconfig-program
+ iwconfig-program-options))
+
+;;;###autoload
+(defun netstat ()
+ "Run netstat and display diagnostic output."
+ (interactive)
+ (net-utils-run-simple
+ (format "*%s*" netstat-program)
+ netstat-program
+ netstat-program-options))
+
+;;;###autoload
+(defun arp ()
+ "Run arp and display diagnostic output."
+ (interactive)
+ (net-utils-run-simple
+ (format "*%s*" arp-program)
+ arp-program
+ arp-program-options))
+
+;;;###autoload
+(defun route ()
+ "Run route and display diagnostic output."
+ (interactive)
+ (net-utils-run-simple
+ (format "*%s*" route-program)
+ route-program
+ route-program-options))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Wrappers for external network programs
ping-program
options)))
-;;;###autoload
-(defun ipconfig ()
- "Run ipconfig program."
- (interactive)
- (net-utils-run-program
- "Ipconfig"
- (concat "** Ipconfig ** " ipconfig-program " ** ")
- ipconfig-program
- ipconfig-program-options))
-
-;; This is the normal name on most Unixes.
-;;;###autoload
-(defalias 'ifconfig 'ipconfig)
-
-;;;###autoload
-(defun netstat ()
- "Run netstat program."
- (interactive)
- (net-utils-run-program
- "Netstat"
- (concat "** Netstat ** " netstat-program " ** ")
- netstat-program
- netstat-program-options))
-
-;;;###autoload
-(defun arp ()
- "Run the arp program."
- (interactive)
- (net-utils-run-program
- "Arp"
- (concat "** Arp ** " arp-program " ** ")
- arp-program
- arp-program-options))
-
-;;;###autoload
-(defun route ()
- "Run the route program."
- (interactive)
- (net-utils-run-program
- "Route"
- (concat "** Route ** " route-program " ** ")
- route-program
- route-program-options))
-
;; FIXME -- Needs to be a process filter
;; (defun netstat-with-filter (filter)
;; "Run netstat program."
(defun nslookup ()
"Run nslookup program."
(interactive)
- (require 'comint)
- (comint-run nslookup-program)
+ (switch-to-buffer (make-comint "nslookup" nslookup-program))
(nslookup-mode))
+(defvar comint-prompt-regexp)
+(defvar comint-input-autoexpand)
+
+(autoload 'comint-mode "comint" nil t)
+
+(defvar nslookup-mode-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map "\t" 'comint-dynamic-complete)
+ map))
+
;; Using a derived mode gives us keymaps, hooks, etc.
(define-derived-mode nslookup-mode comint-mode "Nslookup"
"Major mode for interacting with the nslookup program."
(setq comint-prompt-regexp nslookup-prompt-regexp)
(setq comint-input-autoexpand t))
-(define-key nslookup-mode-map "\t" 'comint-dynamic-complete)
-
;;;###autoload
(defun dns-lookup-host (host)
"Lookup the DNS information for HOST (name or IP address)."
(list "DNS Lookup" host dns-lookup-program)
" ** "))
dns-lookup-program
- options
- )))
+ options)))
+
+(autoload 'ffap-string-at-point "ffap")
;;;###autoload
(defun run-dig (host)
"Run dig program."
(interactive
(list
- (progn
- (require 'ffap)
- (read-from-minibuffer
- "Lookup host: "
- (with-no-warnings
- (or (ffap-string-at-point 'machine) ""))))))
+ (read-from-minibuffer "Lookup host: "
+ (or (ffap-string-at-point 'machine) ""))))
(net-utils-run-program
"Dig"
(concat "** "
dig-program
(list host)))
+(autoload 'comint-exec "comint")
+
;; This is a lot less than ange-ftp, but much simpler.
;;;###autoload
(defun ftp (host)
(list
(read-from-minibuffer
"Ftp to Host: " (net-utils-machine-at-point))))
- (require 'comint)
(let ((buf (get-buffer-create (concat "*ftp [" host "]*"))))
(set-buffer buf)
(ftp-mode)
(list host)))
(pop-to-buffer buf)))
+(defvar ftp-mode-map
+ (let ((map (make-sparse-keymap)))
+ ;; Occasionally useful
+ (define-key map "\t" 'comint-dynamic-complete)
+ map))
+
(define-derived-mode ftp-mode comint-mode "FTP"
"Major mode for interacting with the ftp program."
(setq comint-prompt-regexp ftp-prompt-regexp)
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
nil t)))
-;; Occasionally useful
-(define-key ftp-mode-map "\t" 'comint-dynamic-complete)
-
(defun smbclient (host service)
"Connect to SERVICE on HOST via SMB."
(interactive
(read-from-minibuffer
"Connect to Host: " (net-utils-machine-at-point))
(read-from-minibuffer "SMB Service: ")))
- (require 'comint)
(let* ((name (format "smbclient [%s\\%s]" host service))
(buf (get-buffer-create (concat "*" name "*")))
(service-name (concat "\\\\" host "\\" service)))
"Alist of services and associated TCP port numbers.
This list is not complete.")
-;; Workhorse macro
-(defmacro run-network-program (process-name host port
- &optional initial-string)
- `(let ((tcp-connection)
- (buf))
- (setq buf (get-buffer-create (concat "*" ,process-name "*")))
+;; Workhorse routine
+(defun run-network-program (process-name host port &optional initial-string)
+ (let ((tcp-connection)
+ (buf))
+ (setq buf (get-buffer-create (concat "*" process-name "*")))
(set-buffer buf)
(or
(setq tcp-connection
- (open-network-stream
- ,process-name
- buf
- ,host
- ,port))
- (error "Could not open connection to %s" ,host))
+ (open-network-stream process-name buf host port))
+ (error "Could not open connection to %s" host))
(erase-buffer)
(set-marker (process-mark tcp-connection) (point-min))
(set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter)
- (and ,initial-string
+ (and initial-string
(process-send-string tcp-connection
- (concat ,initial-string "\r\n")))
+ (concat initial-string "\r\n")))
(display-buffer buf)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun network-service-connection (host service)
"Open a network connection to SERVICE on HOST."
- (require 'comint)
(let* ((process-name (concat "Network Connection [" host " " service "]"))
(portnum (string-to-number service))
(buf (get-buffer-create (concat "*" process-name "*"))))
(network-connection-mode-setup host service)
(pop-to-buffer buf)))
+(defvar comint-input-ring)
+
(defun network-connection-reconnect ()
"Reconnect a network connection, preserving the old input ring."
(interactive)
(provide 'net-utils)
-;;; arch-tag: 97119e91-9edb-4376-838b-bf7058fa1314
;;; net-utils.el ends here