-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroot-cmd.el
123 lines (100 loc) · 3.78 KB
/
root-cmd.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;;; root-cmd.el --- execute command and get the output with root privilege
;; Copyright (C) 2013 zxsu
;; Author: zxsu <[email protected]>
;; Keywords:
;; 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 3 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'comint)
(defvar root-cmd-proc-name "*root-proc*"
"Default root cmd proc name.")
(defvar root-cmd-buffer-name "*root-proc*"
"Default root cmd buffer name.")
(defvar root-cmd-output-filter-hooks '(root-cmd-output-keep-hook root-cmd-password-prompt-hook)
"Function hooks used to process output.")
(defvar root-cmd-command nil
"record the command.")
(defvar root-cmd-output-keep nil
"keep cmd output.")
(defun root-cmd-send-password (prompt)
"send password to process"
;(message "begin root-cmd-send-password")
(let ((proc (get-process (or root-cmd-proc-name "*root-proc*"))))
(if (processp proc)
(let ((str (read-passwd prompt)))
(process-send-string proc (concat str "\n"))
(sit-for 1)
(when (and (stringp root-cmd-command) (> (string-width root-cmd-command) 0))
(process-send-string proc (concat root-cmd-command "\n"))
(setq root-cmd-command nil))
))))
(defun root-cmd-output-keep-hook (string)
"keep all cmd output."
;(message "root-cmd-output-keep-hook: %s" string)
(setq root-cmd-output-keep (cons string root-cmd-output-keep)))
(defun root-cmd-password-prompt-hook (string)
"prompt for password input"
(when (string-match comint-password-prompt-regexp string)
(when (string-match "^[ \n\r\t\v\f\b\a]+" string)
(setq string (replace-match "" t t string)))
(root-cmd-send-password string)))
(defun root-cmd-filter (proc string)
"filter"
(let ((buffer (get-buffer-create root-cmd-buffer-name)))
(with-current-buffer buffer
(insert string)
)
(run-hook-with-args 'root-cmd-output-filter-hooks string))
)
(defun root-cmd-sentinel (process event)
"root process sentinel"
(message "Process %s received event %s." process event)
)
;;;###autoload
(defun root-cmd (command)
"output command as root process."
;; (interactive)
(let* ((name (or root-cmd-proc-name "*root-proc*"))
(buffer (get-buffer-create (or root-cmd-buffer-name "*root-proc*")))
(proc (get-process (or root-cmd-proc-name "*root-proc*")))
)
(setq root-cmd-output-keep nil)
(when (processp proc)
(root-cmd-stop)
(sit-for 1)
;(delete-process proc)
)
(setq proc (start-file-process name buffer shell-file-name "-i"))
(setq root-cmd-command command)
(set-process-filter proc 'root-cmd-filter)
(set-process-sentinel proc 'root-cmd-sentinel)
(process-send-string proc "su\n")
))
;;;###autoload
(defun root-cmd-stop ()
"Stop root cmd process."
(interactive)
(let ((proc (get-process (or root-cmd-proc-name "*root-proc*")))
(buffer (get-buffer (or root-cmd-buffer-name "*root-proc*"))))
(if (processp proc)
(delete-process proc))
(if (bufferp buffer)
(kill-buffer buffer))))
;;;###autoload
(defun root-cmd-output ()
"return the cmd output."
(interactive)
(if (> (length root-cmd-output-keep) 2)
(nth 1 root-cmd-output-keep)))
(provide 'root-cmd)
;;; root-cmd.el ends here