Mercurial > emacs
comparison lisp/net/net-utils.el @ 32123:2e9fda397ea2
* net/net-utils.el (nslookup-font-lock-keywords,
ftp-font-lock-keywords, smbclient-font-lock-keywords):
Only set if window-system is non-nil
(net-utils-run-program): Returns buffer.
(network-connection-reconnect): Added this function.
author | Peter Breton <pbreton@attbi.com> |
---|---|
date | Wed, 04 Oct 2000 05:43:37 +0000 |
parents | 716c9bd98063 |
children | b048135b76bc |
comparison
equal
deleted
inserted
replaced
32122:034d1bf7a606 | 32123:2e9fda397ea2 |
---|---|
1 ;;; net-utils.el --- Network functions | 1 ;;; net-utils.el --- Network functions |
2 | 2 |
3 ;; Author: Peter Breton <pbreton@cs.umb.edu> | 3 ;; Author: Peter Breton <pbreton@cs.umb.edu> |
4 ;; Created: Sun Mar 16 1997 | 4 ;; Created: Sun Mar 16 1997 |
5 ;; Keywords: network communications | 5 ;; Keywords: network communications |
6 ;; Time-stamp: <1999-11-13 10:19:01 pbreton> | 6 ;; Time-stamp: <2000-10-04 01:32:16 pbreton> |
7 | 7 |
8 ;; This file is part of GNU Emacs. | 8 ;; This file is part of GNU Emacs. |
9 | 9 |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | 10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
11 ;; it under the terms of the GNU General Public License as published by | 11 ;; it under the terms of the GNU General Public License as published by |
23 ;; Boston, MA 02111-1307, USA. | 23 ;; Boston, MA 02111-1307, USA. |
24 | 24 |
25 ;;; Commentary: | 25 ;;; Commentary: |
26 ;; | 26 ;; |
27 ;; There are three main areas of functionality: | 27 ;; There are three main areas of functionality: |
28 ;; | 28 ;; |
29 ;; * Wrap common network utility programs (ping, traceroute, netstat, | 29 ;; * Wrap common network utility programs (ping, traceroute, netstat, |
30 ;; nslookup, arp, route). Note that these wrappers are of the diagnostic | 30 ;; nslookup, arp, route). Note that these wrappers are of the diagnostic |
31 ;; functions of these programs only. | 31 ;; functions of these programs only. |
32 ;; | 32 ;; |
33 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois) | 33 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois) |
34 ;; | 34 ;; |
35 ;; * Support connections to HOST/PORT, generally for debugging and the like. | 35 ;; * Support connections to HOST/PORT, generally for debugging and the like. |
36 ;; In other words, for doing much the same thing as "telnet HOST PORT", and | 36 ;; In other words, for doing much the same thing as "telnet HOST PORT", and |
37 ;; then typing commands. | 37 ;; then typing commands. |
38 ;; | 38 ;; |
39 ;; PATHS | 39 ;; PATHS |
40 ;; | 40 ;; |
41 ;; On some systems, some of these programs are not in normal user path, | 41 ;; On some systems, some of these programs are not in normal user path, |
42 ;; but rather in /sbin, /usr/sbin, and so on. | 42 ;; but rather in /sbin, /usr/sbin, and so on. |
43 | 43 |
44 | 44 |
45 ;;; Code: | 45 ;;; Code: |
46 (eval-when-compile | 46 (eval-when-compile |
47 (require 'comint)) | 47 (require 'comint)) |
55 :prefix "net-utils-" | 55 :prefix "net-utils-" |
56 :group 'comm | 56 :group 'comm |
57 :version "20.3" | 57 :version "20.3" |
58 ) | 58 ) |
59 | 59 |
60 (defcustom net-utils-remove-ctl-m | 60 (defcustom net-utils-remove-ctl-m |
61 (member system-type (list 'windows-nt 'msdos)) | 61 (member system-type (list 'windows-nt 'msdos)) |
62 "If non-nil, remove control-Ms from output." | 62 "If non-nil, remove control-Ms from output." |
63 :group 'net-utils | 63 :group 'net-utils |
64 :type 'boolean | 64 :type 'boolean |
65 ) | 65 ) |
66 | 66 |
67 (defcustom traceroute-program | 67 (defcustom traceroute-program |
68 (if (eq system-type 'windows-nt) | 68 (if (eq system-type 'windows-nt) |
69 "tracert" | 69 "tracert" |
70 "traceroute") | 70 "traceroute") |
71 "Program to trace network hops to a destination." | 71 "Program to trace network hops to a destination." |
72 :group 'net-utils | 72 :group 'net-utils |
73 :type 'string | 73 :type 'string |
85 :type 'string | 85 :type 'string |
86 ) | 86 ) |
87 | 87 |
88 ;; On Linux and Irix, the system's ping program seems to send packets | 88 ;; On Linux and Irix, the system's ping program seems to send packets |
89 ;; indefinitely unless told otherwise | 89 ;; indefinitely unless told otherwise |
90 (defcustom ping-program-options | 90 (defcustom ping-program-options |
91 (and (memq system-type (list 'linux 'gnu/linux 'irix)) | 91 (and (memq system-type (list 'linux 'gnu/linux 'irix)) |
92 (list "-c" "4")) | 92 (list "-c" "4")) |
93 "Options for the ping program. | 93 "Options for the ping program. |
94 These options can be used to limit how many ICMP packets are emitted." | 94 These options can be used to limit how many ICMP packets are emitted." |
95 :group 'net-utils | 95 :group 'net-utils |
96 :type '(repeat string) | 96 :type '(repeat string) |
97 ) | 97 ) |
98 | 98 |
99 (defcustom ipconfig-program | 99 (defcustom ipconfig-program |
100 (if (eq system-type 'windows-nt) | 100 (if (eq system-type 'windows-nt) |
101 "ipconfig" | 101 "ipconfig" |
102 "ifconfig") | 102 "ifconfig") |
103 "Program to print network configuration information." | 103 "Program to print network configuration information." |
104 :group 'net-utils | 104 :group 'net-utils |
105 :type 'string | 105 :type 'string |
106 ) | 106 ) |
107 | 107 |
108 (defcustom ipconfig-program-options | 108 (defcustom ipconfig-program-options |
109 (list | 109 (list |
110 (if (eq system-type 'windows-nt) | 110 (if (eq system-type 'windows-nt) |
111 "/all" "-a")) | 111 "/all" "-a")) |
112 "Options for ipconfig-program." | 112 "Options for ipconfig-program." |
113 :group 'net-utils | 113 :group 'net-utils |
114 :type '(repeat string) | 114 :type '(repeat string) |
118 "Program to print network statistics." | 118 "Program to print network statistics." |
119 :group 'net-utils | 119 :group 'net-utils |
120 :type 'string | 120 :type 'string |
121 ) | 121 ) |
122 | 122 |
123 (defcustom netstat-program-options | 123 (defcustom netstat-program-options |
124 (list "-a") | 124 (list "-a") |
125 "Options for netstat-program." | 125 "Options for netstat-program." |
126 :group 'net-utils | 126 :group 'net-utils |
127 :type '(repeat string) | 127 :type '(repeat string) |
128 ) | 128 ) |
131 "Program to print IP to address translation tables." | 131 "Program to print IP to address translation tables." |
132 :group 'net-utils | 132 :group 'net-utils |
133 :type 'string | 133 :type 'string |
134 ) | 134 ) |
135 | 135 |
136 (defcustom arp-program-options | 136 (defcustom arp-program-options |
137 (list "-a") | 137 (list "-a") |
138 "Options for arp-program." | 138 "Options for arp-program." |
139 :group 'net-utils | 139 :group 'net-utils |
140 :type '(repeat string) | 140 :type '(repeat string) |
141 ) | 141 ) |
142 | 142 |
143 (defcustom route-program | 143 (defcustom route-program |
144 (if (eq system-type 'windows-nt) | 144 (if (eq system-type 'windows-nt) |
145 "route" | 145 "route" |
146 "netstat") | 146 "netstat") |
147 "Program to print routing tables." | 147 "Program to print routing tables." |
148 :group 'net-utils | 148 :group 'net-utils |
149 :type 'string | 149 :type 'string |
150 ) | 150 ) |
151 | 151 |
152 (defcustom route-program-options | 152 (defcustom route-program-options |
153 (if (eq system-type 'windows-nt) | 153 (if (eq system-type 'windows-nt) |
154 (list "print") | 154 (list "print") |
155 (list "-r")) | 155 (list "-r")) |
156 "Options for route-program." | 156 "Options for route-program." |
157 :group 'net-utils | 157 :group 'net-utils |
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
226 ;; Nslookup goodies | 226 ;; Nslookup goodies |
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
228 | 228 |
229 (defconst nslookup-font-lock-keywords | 229 (defconst nslookup-font-lock-keywords |
230 (progn | 230 (and window-system |
231 (require 'font-lock) | 231 (progn |
232 (list | 232 (require 'font-lock) |
233 (list nslookup-prompt-regexp 0 font-lock-reference-face) | 233 (list |
234 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face) | 234 (list nslookup-prompt-regexp 0 font-lock-reference-face) |
235 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>" | 235 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face) |
236 1 font-lock-keyword-face) | 236 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>" |
237 ;; Dotted quads | 237 1 font-lock-keyword-face) |
238 (list | 238 ;; Dotted quads |
239 (mapconcat 'identity | 239 (list |
240 (make-list 4 "[0-9]+") | 240 (mapconcat 'identity |
241 "\\.") | 241 (make-list 4 "[0-9]+") |
242 0 font-lock-variable-name-face) | 242 "\\.") |
243 ;; Host names | 243 0 font-lock-variable-name-face) |
244 (list | 244 ;; Host names |
245 (let ((host-expression "[-A-Za-z0-9]+")) | 245 (list |
246 (concat | 246 (let ((host-expression "[-A-Za-z0-9]+")) |
247 (mapconcat 'identity | 247 (concat |
248 (make-list 2 host-expression) | 248 (mapconcat 'identity |
249 "\\.") | 249 (make-list 2 host-expression) |
250 "\\(\\." host-expression "\\)*") | 250 "\\.") |
251 ) | 251 "\\(\\." host-expression "\\)*") |
252 0 font-lock-variable-name-face) | 252 ) |
253 )) | 253 0 font-lock-variable-name-face) |
254 "Expressions to font-lock for nslookup.") | 254 ))) |
255 "Expressions to font-lock for nslookup.") | |
255 | 256 |
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
257 ;; FTP goodies | 258 ;; FTP goodies |
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
259 | 260 |
260 (defconst ftp-font-lock-keywords | 261 (defconst ftp-font-lock-keywords |
261 (progn | 262 (and window-system |
262 (require 'font-lock) | 263 (progn |
263 (list | 264 (require 'font-lock) |
264 (list ftp-prompt-regexp 0 font-lock-reference-face)))) | 265 (list |
266 (list ftp-prompt-regexp 0 font-lock-reference-face))))) | |
265 | 267 |
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
267 ;; smbclient goodies | 269 ;; smbclient goodies |
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
269 | 271 |
270 (defconst smbclient-font-lock-keywords | 272 (defconst smbclient-font-lock-keywords |
271 (progn | 273 (and window-system |
272 (require 'font-lock) | 274 (progn |
273 (list | 275 (require 'font-lock) |
274 (list smbclient-prompt-regexp 0 font-lock-reference-face)))) | 276 (list |
277 (list smbclient-prompt-regexp 0 font-lock-reference-face))))) | |
275 | 278 |
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
277 ;; Utility functions | 280 ;; Utility functions |
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
279 | 282 |
309 (filtered-string output-string)) | 312 (filtered-string output-string)) |
310 (unwind-protect | 313 (unwind-protect |
311 (let ((moving)) | 314 (let ((moving)) |
312 (set-buffer (process-buffer process)) | 315 (set-buffer (process-buffer process)) |
313 (setq moving (= (point) (process-mark process))) | 316 (setq moving (= (point) (process-mark process))) |
314 | 317 |
315 (while (string-match "\r" filtered-string) | 318 (while (string-match "\r" filtered-string) |
316 (setq filtered-string | 319 (setq filtered-string |
317 (replace-match "" nil nil filtered-string))) | 320 (replace-match "" nil nil filtered-string))) |
318 | 321 |
319 (save-excursion | 322 (save-excursion |
321 (goto-char (process-mark process)) | 324 (goto-char (process-mark process)) |
322 (insert filtered-string) | 325 (insert filtered-string) |
323 (set-marker (process-mark process) (point))) | 326 (set-marker (process-mark process) (point))) |
324 (if moving (goto-char (process-mark process)))) | 327 (if moving (goto-char (process-mark process)))) |
325 (set-buffer old-buffer)))) | 328 (set-buffer old-buffer)))) |
326 | 329 |
327 (defmacro net-utils-run-program (name header program &rest args) | 330 (defmacro net-utils-run-program (name header program &rest args) |
328 "Run a network information program." | 331 "Run a network information program." |
329 ` (let ((buf (get-buffer-create (concat "*" ,name "*")))) | 332 ` (let ((buf (get-buffer-create (concat "*" ,name "*")))) |
330 (set-buffer buf) | 333 (set-buffer buf) |
331 (erase-buffer) | 334 (erase-buffer) |
332 (insert ,header "\n") | 335 (insert ,header "\n") |
333 (set-process-filter | 336 (set-process-filter |
334 (apply 'start-process ,name buf ,program ,@args) | 337 (apply 'start-process ,name buf ,program ,@args) |
335 'net-utils-remove-ctrl-m-filter) | 338 'net-utils-remove-ctrl-m-filter) |
336 (display-buffer buf))) | 339 (display-buffer buf) |
340 buf)) | |
337 | 341 |
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
339 ;; Wrappers for external network programs | 343 ;; Wrappers for external network programs |
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
341 | 345 |
342 ;;;###autoload | 346 ;;;###autoload |
343 (defun traceroute (target) | 347 (defun traceroute (target) |
344 "Run traceroute program for TARGET." | 348 "Run traceroute program for TARGET." |
345 (interactive "sTarget: ") | 349 (interactive "sTarget: ") |
346 (let ((options | 350 (let ((options |
347 (if traceroute-program-options | 351 (if traceroute-program-options |
348 (append traceroute-program-options (list target)) | 352 (append traceroute-program-options (list target)) |
349 (list target)))) | 353 (list target)))) |
350 (net-utils-run-program | 354 (net-utils-run-program |
351 (concat "Traceroute" " " target) | 355 (concat "Traceroute" " " target) |
355 ))) | 359 ))) |
356 | 360 |
357 ;;;###autoload | 361 ;;;###autoload |
358 (defun ping (host) | 362 (defun ping (host) |
359 "Ping HOST. | 363 "Ping HOST. |
360 If your system's ping continues until interrupted, you can try setting | 364 If your system's ping continues until interrupted, you can try setting |
361 `ping-program-options'." | 365 `ping-program-options'." |
362 (interactive | 366 (interactive |
363 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point)))) | 367 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point)))) |
364 (let ((options | 368 (let ((options |
365 (if ping-program-options | 369 (if ping-program-options |
366 (append ping-program-options (list host)) | 370 (append ping-program-options (list host)) |
367 (list host)))) | 371 (list host)))) |
368 (net-utils-run-program | 372 (net-utils-run-program |
369 (concat "Ping" " " host) | 373 (concat "Ping" " " host) |
383 ipconfig-program-options | 387 ipconfig-program-options |
384 )) | 388 )) |
385 | 389 |
386 ;; This is the normal name on most Unixes. | 390 ;; This is the normal name on most Unixes. |
387 ;;;###autoload | 391 ;;;###autoload |
388 (defalias 'ifconfig 'ipconfig) | 392 (defalias 'ifconfig 'ipconfig) |
389 | 393 |
390 ;;;###autoload | 394 ;;;###autoload |
391 (defun netstat () | 395 (defun netstat () |
392 "Run netstat program." | 396 "Run netstat program." |
393 (interactive) | 397 (interactive) |
433 ;;;###autoload | 437 ;;;###autoload |
434 (defun nslookup-host (host) | 438 (defun nslookup-host (host) |
435 "Lookup the DNS information for HOST." | 439 "Lookup the DNS information for HOST." |
436 (interactive | 440 (interactive |
437 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point)))) | 441 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point)))) |
438 (let ((options | 442 (let ((options |
439 (if nslookup-program-options | 443 (if nslookup-program-options |
440 (append nslookup-program-options (list host)) | 444 (append nslookup-program-options (list host)) |
441 (list host)))) | 445 (list host)))) |
442 (net-utils-run-program | 446 (net-utils-run-program |
443 "Nslookup" | 447 "Nslookup" |
460 'net-utils-remove-ctrl-m-filter) | 464 'net-utils-remove-ctrl-m-filter) |
461 (nslookup-mode) | 465 (nslookup-mode) |
462 ) | 466 ) |
463 | 467 |
464 ;; Using a derived mode gives us keymaps, hooks, etc. | 468 ;; Using a derived mode gives us keymaps, hooks, etc. |
465 (define-derived-mode | 469 (define-derived-mode |
466 nslookup-mode comint-mode "Nslookup" | 470 nslookup-mode comint-mode "Nslookup" |
467 "Major mode for interacting with the nslookup program." | 471 "Major mode for interacting with the nslookup program." |
468 (set | 472 (set |
469 (make-local-variable 'font-lock-defaults) | 473 (make-local-variable 'font-lock-defaults) |
470 '((nslookup-font-lock-keywords))) | 474 '((nslookup-font-lock-keywords))) |
471 (setq local-abbrev-table nslookup-mode-abbrev-table) | 475 (setq local-abbrev-table nslookup-mode-abbrev-table) |
472 (abbrev-mode t) | 476 (abbrev-mode t) |
473 (make-local-variable 'comint-prompt-regexp) | 477 (make-local-variable 'comint-prompt-regexp) |
493 "Run dig program." | 497 "Run dig program." |
494 (interactive | 498 (interactive |
495 (list | 499 (list |
496 (progn | 500 (progn |
497 (require 'ffap) | 501 (require 'ffap) |
498 (read-from-minibuffer | 502 (read-from-minibuffer |
499 "Lookup host: " | 503 "Lookup host: " |
500 (or (ffap-string-at-point 'machine) ""))))) | 504 (or (ffap-string-at-point 'machine) ""))))) |
501 (net-utils-run-program | 505 (net-utils-run-program |
502 "Dig" | 506 "Dig" |
503 (concat "** " | 507 (concat "** " |
504 (mapconcat 'identity | 508 (mapconcat 'identity |
505 (list "Dig" host dig-program) | 509 (list "Dig" host dig-program) |
506 " ** ")) | 510 " ** ")) |
507 dig-program | 511 dig-program |
508 (list host) | 512 (list host) |
509 )) | 513 )) |
510 | 514 |
511 ;; This is a lot less than ange-ftp, but much simpler. | 515 ;; This is a lot less than ange-ftp, but much simpler. |
512 ;;;###autoload | 516 ;;;###autoload |
513 (defun ftp (host) | 517 (defun ftp (host) |
514 "Run ftp program." | 518 "Run ftp program." |
515 (interactive | 519 (interactive |
516 (list | 520 (list |
517 (read-from-minibuffer | 521 (read-from-minibuffer |
518 "Ftp to Host: " (net-utils-machine-at-point)))) | 522 "Ftp to Host: " (net-utils-machine-at-point)))) |
519 (require 'comint) | 523 (require 'comint) |
520 (let ((buf (get-buffer-create (concat "*ftp [" host "]*")))) | 524 (let ((buf (get-buffer-create (concat "*ftp [" host "]*")))) |
521 (set-buffer buf) | 525 (set-buffer buf) |
522 (comint-mode) | 526 (comint-mode) |
526 (list host))) | 530 (list host))) |
527 (ftp-mode) | 531 (ftp-mode) |
528 (switch-to-buffer-other-window buf) | 532 (switch-to-buffer-other-window buf) |
529 )) | 533 )) |
530 | 534 |
531 (define-derived-mode | 535 (define-derived-mode |
532 ftp-mode comint-mode "FTP" | 536 ftp-mode comint-mode "FTP" |
533 "Major mode for interacting with the ftp program." | 537 "Major mode for interacting with the ftp program." |
534 | 538 |
535 (set | 539 (set |
536 (make-local-variable 'font-lock-defaults) | 540 (make-local-variable 'font-lock-defaults) |
537 '((ftp-font-lock-keywords))) | 541 '((ftp-font-lock-keywords))) |
538 | 542 |
539 (make-local-variable 'comint-prompt-regexp) | 543 (make-local-variable 'comint-prompt-regexp) |
540 (setq comint-prompt-regexp ftp-prompt-regexp) | 544 (setq comint-prompt-regexp ftp-prompt-regexp) |
541 | 545 |
542 (make-local-variable 'comint-input-autoexpand) | 546 (make-local-variable 'comint-input-autoexpand) |
543 (setq comint-input-autoexpand t) | 547 (setq comint-input-autoexpand t) |
544 | 548 |
545 ;; Already buffer local! | 549 ;; Already buffer local! |
546 (setq comint-output-filter-functions | 550 (setq comint-output-filter-functions |
547 (list 'comint-watch-for-password-prompt)) | 551 (list 'comint-watch-for-password-prompt)) |
548 | 552 |
549 (setq local-abbrev-table ftp-mode-abbrev-table) | 553 (setq local-abbrev-table ftp-mode-abbrev-table) |
550 (abbrev-mode t) | 554 (abbrev-mode t) |
551 ) | 555 ) |
552 | 556 |
553 (define-abbrev ftp-mode-abbrev-table "q" "quit") | 557 (define-abbrev ftp-mode-abbrev-table "q" "quit") |
558 ;; Occasionally useful | 562 ;; Occasionally useful |
559 (define-key ftp-mode-map "\t" 'comint-dynamic-complete) | 563 (define-key ftp-mode-map "\t" 'comint-dynamic-complete) |
560 | 564 |
561 (defun smbclient (host service) | 565 (defun smbclient (host service) |
562 "Connect to SERVICE on HOST via SMB." | 566 "Connect to SERVICE on HOST via SMB." |
563 (interactive | 567 (interactive |
564 (list | 568 (list |
565 (read-from-minibuffer | 569 (read-from-minibuffer |
566 "Connect to Host: " (net-utils-machine-at-point)) | 570 "Connect to Host: " (net-utils-machine-at-point)) |
567 (read-from-minibuffer "SMB Service: "))) | 571 (read-from-minibuffer "SMB Service: "))) |
568 (require 'comint) | 572 (require 'comint) |
569 (let* ((name (format "smbclient [%s\\%s]" host service)) | 573 (let* ((name (format "smbclient [%s\\%s]" host service)) |
570 (buf (get-buffer-create (concat "*" name "*"))) | 574 (buf (get-buffer-create (concat "*" name "*"))) |
579 (switch-to-buffer-other-window buf) | 583 (switch-to-buffer-other-window buf) |
580 )) | 584 )) |
581 | 585 |
582 (defun smbclient-list-shares (host) | 586 (defun smbclient-list-shares (host) |
583 "List services on HOST." | 587 "List services on HOST." |
584 (interactive | 588 (interactive |
585 (list | 589 (list |
586 (read-from-minibuffer | 590 (read-from-minibuffer |
587 "Connect to Host: " (net-utils-machine-at-point)) | 591 "Connect to Host: " (net-utils-machine-at-point)) |
588 )) | 592 )) |
589 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host)))) | 593 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host)))) |
590 (set-buffer buf) | 594 (set-buffer buf) |
591 (comint-mode) | 595 (comint-mode) |
592 (comint-exec | 596 (comint-exec |
593 buf | 597 buf |
594 "smbclient-list-shares" | 598 "smbclient-list-shares" |
595 smbclient-program | 599 smbclient-program |
596 nil | 600 nil |
597 (list "-L" host) | 601 (list "-L" host) |
598 ) | 602 ) |
599 (smbclient-mode) | 603 (smbclient-mode) |
600 (switch-to-buffer-other-window buf))) | 604 (switch-to-buffer-other-window buf))) |
601 | 605 |
602 (define-derived-mode | 606 (define-derived-mode |
603 smbclient-mode comint-mode "smbclient" | 607 smbclient-mode comint-mode "smbclient" |
604 "Major mode for interacting with the smbclient program." | 608 "Major mode for interacting with the smbclient program." |
605 | 609 |
606 (set | 610 (set |
607 (make-local-variable 'font-lock-defaults) | 611 (make-local-variable 'font-lock-defaults) |
608 '((smbclient-font-lock-keywords))) | 612 '((smbclient-font-lock-keywords))) |
609 | 613 |
610 (make-local-variable 'comint-prompt-regexp) | 614 (make-local-variable 'comint-prompt-regexp) |
611 (setq comint-prompt-regexp smbclient-prompt-regexp) | 615 (setq comint-prompt-regexp smbclient-prompt-regexp) |
612 | 616 |
613 (make-local-variable 'comint-input-autoexpand) | 617 (make-local-variable 'comint-input-autoexpand) |
614 (setq comint-input-autoexpand t) | 618 (setq comint-input-autoexpand t) |
615 | 619 |
616 ;; Already buffer local! | 620 ;; Already buffer local! |
617 (setq comint-output-filter-functions | 621 (setq comint-output-filter-functions |
618 (list 'comint-watch-for-password-prompt)) | 622 (list 'comint-watch-for-password-prompt)) |
619 | 623 |
620 (setq local-abbrev-table smbclient-mode-abbrev-table) | 624 (setq local-abbrev-table smbclient-mode-abbrev-table) |
621 (abbrev-mode t) | 625 (abbrev-mode t) |
622 ) | 626 ) |
623 | 627 |
624 (define-abbrev smbclient-mode-abbrev-table "q" "quit") | 628 (define-abbrev smbclient-mode-abbrev-table "q" "quit") |
628 ;; Network Connections | 632 ;; Network Connections |
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 633 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
630 | 634 |
631 ;; Full list is available at: | 635 ;; Full list is available at: |
632 ;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers | 636 ;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers |
633 (defvar network-connection-service-alist | 637 (defvar network-connection-service-alist |
634 (list | 638 (list |
635 (cons 'echo 7) | 639 (cons 'echo 7) |
636 (cons 'active-users 11) | 640 (cons 'active-users 11) |
637 (cons 'daytime 13) | 641 (cons 'daytime 13) |
638 (cons 'chargen 19) | 642 (cons 'chargen 19) |
657 ) | 661 ) |
658 "Alist of services and associated TCP port numbers. | 662 "Alist of services and associated TCP port numbers. |
659 This list in not complete.") | 663 This list in not complete.") |
660 | 664 |
661 ;; Workhorse macro | 665 ;; Workhorse macro |
662 (defmacro run-network-program (process-name host port | 666 (defmacro run-network-program (process-name host port |
663 &optional initial-string) | 667 &optional initial-string) |
664 ` | 668 ` |
665 (let ((tcp-connection) | 669 (let ((tcp-connection) |
666 (buf) | 670 (buf) |
667 ) | 671 ) |
668 (setq buf (get-buffer-create (concat "*" ,process-name "*"))) | 672 (setq buf (get-buffer-create (concat "*" ,process-name "*"))) |
669 (set-buffer buf) | 673 (set-buffer buf) |
670 (or | 674 (or |
671 (setq tcp-connection | 675 (setq tcp-connection |
672 (open-network-stream | 676 (open-network-stream |
673 ,process-name | 677 ,process-name |
674 buf | 678 buf |
675 ,host | 679 ,host |
676 ,port | 680 ,port |
677 )) | 681 )) |
678 (error "Could not open connection to %s" ,host)) | 682 (error "Could not open connection to %s" ,host)) |
679 (erase-buffer) | 683 (erase-buffer) |
680 (set-marker (process-mark tcp-connection) (point-min)) | 684 (set-marker (process-mark tcp-connection) (point-min)) |
681 (set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter) | 685 (set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter) |
682 (and ,initial-string | 686 (and ,initial-string |
683 (process-send-string tcp-connection | 687 (process-send-string tcp-connection |
684 (concat ,initial-string "\r\n"))) | 688 (concat ,initial-string "\r\n"))) |
685 (display-buffer buf))) | 689 (display-buffer buf))) |
686 | 690 |
687 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 691 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
688 ;; Simple protocols | 692 ;; Simple protocols |
721 found) | 725 found) |
722 (while (not (string-match (car regexps) host)) | 726 (while (not (string-match (car regexps) host)) |
723 (setq regexps (cdr regexps))) | 727 (setq regexps (cdr regexps))) |
724 (when regexps | 728 (when regexps |
725 (setq user-and-host user)) | 729 (setq user-and-host user)) |
726 (run-network-program | 730 (run-network-program |
727 process-name | 731 process-name |
728 host | 732 host |
729 (cdr (assoc 'finger network-connection-service-alist)) | 733 (cdr (assoc 'finger network-connection-service-alist)) |
730 user-and-host))) | 734 user-and-host))) |
731 | 735 |
732 (defcustom whois-server-name "rs.internic.net" | 736 (defcustom whois-server-name "rs.internic.net" |
733 "Default host name for the whois service." | 737 "Default host name for the whois service." |
802 (host | 806 (host |
803 (if arg | 807 (if arg |
804 (completing-read "Whois server name: " | 808 (completing-read "Whois server name: " |
805 whois-server-list nil nil "whois.") | 809 whois-server-list nil nil "whois.") |
806 server-name))) | 810 server-name))) |
807 (run-network-program | 811 (run-network-program |
808 "Whois" | 812 "Whois" |
809 host | 813 host |
810 (cdr (assoc 'whois network-connection-service-alist)) | 814 (cdr (assoc 'whois network-connection-service-alist)) |
811 search-string | 815 search-string |
812 ))) | 816 ))) |
826 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
827 ;;; General Network connection | 831 ;;; General Network connection |
828 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
829 | 833 |
830 ;; Using a derived mode gives us keymaps, hooks, etc. | 834 ;; Using a derived mode gives us keymaps, hooks, etc. |
831 (define-derived-mode | 835 (define-derived-mode |
832 network-connection-mode comint-mode "Network-Connection" | 836 network-connection-mode comint-mode "Network-Connection" |
833 "Major mode for interacting with the network-connection program." | 837 "Major mode for interacting with the network-connection program." |
834 ) | 838 ) |
835 | 839 |
836 (defun network-connection-mode-setup (host service) | 840 (defun network-connection-mode-setup (host service) |
837 (let ((network-abbrev-table | 841 (let ((network-abbrev-table |
838 (or | 842 (or |
839 (assoc service network-connection-service-abbrev-alist) | 843 (assoc service network-connection-service-abbrev-alist) |
840 (and (rassoc service network-connection-service-alist) | 844 (and (rassoc service network-connection-service-alist) |
841 (assoc | 845 (assoc |
842 (elt (rassoc service network-connection-service-alist) 0) | 846 (elt (rassoc service network-connection-service-alist) 0) |
843 network-connection-service-abbrev-alist))))) | 847 network-connection-service-abbrev-alist))))) |
844 (make-local-variable 'network-connection-host) | 848 (make-local-variable 'network-connection-host) |
845 (setq network-connection-host host) | 849 (setq network-connection-host host) |
846 (make-local-variable 'network-connection-service) | 850 (make-local-variable 'network-connection-service) |
847 (setq network-connection-service service) | 851 (setq network-connection-service service) |
848 (and network-abbrev-table | 852 (and network-abbrev-table |
849 (setq local-abbrev-table (cdr network-abbrev-table)) | 853 (setq local-abbrev-table (cdr network-abbrev-table)) |
850 (abbrev-mode t) | 854 (abbrev-mode t) |
851 ))) | 855 ))) |
852 | 856 |
853 ;;;###autoload | 857 ;;;###autoload |
854 (defun network-connection-to-service (host service) | 858 (defun network-connection-to-service (host service) |
855 "Open a network connection to SERVICE on HOST." | 859 "Open a network connection to SERVICE on HOST." |
856 (interactive | 860 (interactive |
857 (list | 861 (list |
858 (read-from-minibuffer "Host: " (net-utils-machine-at-point)) | 862 (read-from-minibuffer "Host: " (net-utils-machine-at-point)) |
859 (completing-read "Service: " | 863 (completing-read "Service: " |
860 (mapcar | 864 (mapcar |
861 (function | 865 (function |
862 (lambda (elt) | 866 (lambda (elt) |
863 (list (symbol-name (car elt))))) | 867 (list (symbol-name (car elt))))) |
864 network-connection-service-alist)))) | 868 network-connection-service-alist)))) |
865 (network-connection | 869 (network-connection |
866 host | 870 host |
867 (cdr (assoc (intern service) network-connection-service-alist))) | 871 (cdr (assoc (intern service) network-connection-service-alist))) |
868 ) | 872 ) |
869 | 873 |
870 ;;;###autoload | 874 ;;;###autoload |
871 (defun network-connection (host port) | 875 (defun network-connection (host port) |
880 (process-name (concat "Network Connection [" host " " service "]")) | 884 (process-name (concat "Network Connection [" host " " service "]")) |
881 (portnum (string-to-number service)) | 885 (portnum (string-to-number service)) |
882 (buf (get-buffer-create (concat "*" process-name "*"))) | 886 (buf (get-buffer-create (concat "*" process-name "*"))) |
883 ) | 887 ) |
884 (or (zerop portnum) (setq service portnum)) | 888 (or (zerop portnum) (setq service portnum)) |
885 (make-comint | 889 (make-comint |
886 process-name | 890 process-name |
887 (cons host service)) | 891 (cons host service)) |
888 (set-buffer buf) | 892 (set-buffer buf) |
889 (network-connection-mode) | 893 (network-connection-mode) |
890 (network-connection-mode-setup host service) | 894 (network-connection-mode-setup host service) |
891 (pop-to-buffer buf) | 895 (pop-to-buffer buf) |
892 )) | 896 )) |
893 | 897 |
898 (defun network-connection-reconnect () | |
899 "Reconnect a network connection, preserving the old input ring." | |
900 (interactive) | |
901 (let ((proc (get-buffer-process (current-buffer))) | |
902 (old-comint-input-ring comint-input-ring) | |
903 (host network-connection-host) | |
904 (service network-connection-service) | |
905 ) | |
906 (if (not (or (not proc) | |
907 (eq (process-status proc) 'closed))) | |
908 (message "Still connected") | |
909 (goto-char (point-max)) | |
910 (insert (format "Reopening connection to %s\n" host)) | |
911 (network-connection host | |
912 (if (numberp service) | |
913 service | |
914 (cdr (assoc service network-connection-service-alist)))) | |
915 (and old-comint-input-ring | |
916 (setq comint-input-ring old-comint-input-ring)) | |
917 ))) | |
918 | |
894 (provide 'net-utils) | 919 (provide 'net-utils) |
895 | 920 |
896 ;;; net-utils.el ends here | 921 ;;; net-utils.el ends here |