use upfront sudo
[rrq/newlisp-ftw.git] / enitool.lsp
1 #!/usr/bin/newlisp
2 #
3 # Helper tool to browse and edit the ifupdown configuration
4 # uses iselect, ed, nano and sudo
5 #
6 # Extra iselect commands:
7 # # = toggle commenting of configuration block
8 # d = delete empty line
9 # e = edit the whole file
10 #
11 # right-arrow or return = follow to sourced file, or edit the current block
12 # left-arrow or q = go up or exit
13
14 ;(signal 2 (fn (x) (exit)))
15
16 (when (!= "0" (if (exec "id -u") ($it 0) ""))
17   (let ((SUDO (if (exec "command -v sudo") ($it 0) "/usr/bin/sudo")))
18     (wait-pid (process (join (cons SUDO (main-args) " ")))))
19   (exit 0))
20
21 (constant
22  ;; all "block starters", including blank lines
23  'ENI-KEY '( "iface" "mapping" "auto" "allow-\\w*" "rename"
24              "source" "source-directory" "$")
25  ;; regex to identify block starters
26  'ENI-HEAD (format "^\\s*#?\\s*(%s)" (join ENI-KEY "|"))
27  'ENI-COMMENT "^\\s*#"
28  'EDITOR (or (env "EDITOR") "nano")
29  'PROC (if (exec (format "command -v %s" EDITOR)) ($it 0) "/bin/nano")
30  )
31
32 (define (is-eni-key PAT S)
33   (when (regex PAT S 0) true))
34
35 (define (is-eni-comment S)
36   (is-eni-key ENI-COMMENT S))
37
38 (define (istrue? A B)
39   (list A B) (= A B true))
40
41 (define (eni-starters) ; DATA
42   (flat (ref-all ENI-HEAD DATA is-eni-key)))
43
44 ;; Pull out the block headed by the B line. If this head is a blank
45 ;; line, then the block includes preceeding comment and the blank line
46 ;; only. Otherwise it includes preceeding comment, head line and the
47 ;; following mix of non-head lines and comment lines (i.e. up to next
48 ;; head line). FROM is the line after the prior block, and it is moved
49 ;; to end of this block.
50 (define (sub-divide-DATA B (E (length DATA))) ; DATA FROM
51   (let ((SLICE (fn (F B E) (append (list F B E) (slice DATA F (- E F))))))
52     (SLICE FROM B (set 'FROM (if (empty? (DATA B)) (+ B 1) E)))))
53
54 ;; Read the "interfaces file" and split up into its "blocks"
55 ;; <block> <commentline>* <headline> ( <otherline> | <commentline> )*
56 (define (read-eni NAME)
57   (letn ((DATA (parse (read-file NAME) "\n"))
58          (BEG (eni-starters))
59          (FROM 0))
60     (setf LASTENI (map sub-divide-DATA BEG (1 BEG)))
61     ;;(map println LASTENI)
62     ;;(read-line)
63     LASTENI
64     ))
65
66 (define (add-selector X)
67   (cons (format "<s:%d>%s" (X 0) (X 1)) (2 X)))
68
69 ;; Find the definition block spanning line I in file FILE
70 (define (find-block I FILE)
71   ;;(println (list 'find-block I FILE))
72   (exists (fn (B) (< I (B 2))) (read-eni FILE)))
73
74 ############################################################
75 ### Interactive actions
76
77 ## PATH holds interactive state as a stack of [pos file]
78 (setf PATH '(( 0 "/etc/network/interfaces" )) )
79
80 ; Edit a file
81 (define (edit-file I FILE)
82   (wait-pid (process (format "%s +%d %s" PROC (int I) FILE))))
83
84 (define (ensure-newline TXT)
85   (if (empty? TXT) "" (ends-with TXT "\n") TXT (string TXT "\n")))
86
87 (define (update-file B E TXT FILE)
88   (let ((DATA (parse (read-file FILE) "\n")))
89     (write-file TXT (string (join (0 B DATA) "\n" true)
90                             (ensure-newline (read-file TXT))
91                             (join (E DATA) "\n")))
92     (exec (format "mv %s %s" TXT FILE))
93     ))
94
95 (define (key-command-select I FILE) ; PATH
96   (letn ((BLOCK (find-block (- (int I) 1) FILE))
97          (TMP "/tmp/enitool/tmp.conf")
98          (HEAD (BLOCK (- (BLOCK 1) (BLOCK 0) -3)))
99          (TAG (or (and (regex "^#?(\\w*) (.*)" HEAD 0) $1) "#"))
100          (VALUE $2))
101     (case TAG
102       ("source" (push (list 0 VALUE) PATH))
103       (true (write-file TMP (join (3 BLOCK) "\n" true))
104             (let ((F (file-info TMP 6)))
105               (edit-file 1 TMP)
106               (when (!= F (file-info TMP 6))
107                 (update-file (BLOCK 0) (BLOCK 2) TMP FILE)))
108             ))
109     ))
110
111 (define (delete-block-maybe I FILE)
112   (let ((BLOCK (find-block (- (int I) 1) FILE))
113         (TMP "/tmp/enitool/tmp.conf"))
114     (when (= (3 BLOCK) '(""))
115       (exec (format "ed -s %s" FILE)
116             (format "%dd\nw\n" (+ 1 (BLOCK 0)))))))
117
118 (define (toggle-commenting I FILE)
119   (let ((BLOCK (find-block (- (int I) 1) FILE))
120         (TMP "/tmp/enitool/tmp.conf"))
121     (letn ((H (- (BLOCK 1) (BLOCK 0)))
122            (TXT (3 BLOCK))
123            (toggle (if (starts-with (TXT H) "#")
124                        (fn (X) (if (starts-with X "#") (1 X) X))
125                      (fn (X) (string "#" X)))))
126       (write-file TMP (ensure-newline
127                        (string (join (0 H TXT) "\n" true)
128                                (join (map toggle (H TXT)) "\n"))))
129       (update-file (BLOCK 0) (BLOCK 2) TMP FILE)
130       )))
131
132 (define (command-dispatch CMD FILE)
133   (when (regex "([^:]+):([^:]+):(\\S*)\\s*(.*)" CMD 0)
134     (setf (PATH 0 0) (int $1))
135     (cond
136      ((member $2 '("KEY_RIGHT" "RETURN")) (key-command-select $1 FILE))
137      ((= $2 "d") (delete-block-maybe $1 FILE))
138      ((= $2 "#") (toggle-commenting $1 FILE))
139      ((= $2 "e") (edit-file $1 FILE))
140      )))
141
142 (define (iselect POS FILE)
143   (exec (format "iselect -n '%s' -t '%s' -a -P -K '-k#' -kd -ke -p %d < %s"
144                 "enitool" FILE (int POS) FILE)))
145
146 (change-dir "/etc/network")
147 (wait-pid (process (format "mkdir -m 777 -p /tmp/enitool")))
148
149 (while PATH
150   (if (apply iselect (PATH 0))
151       (command-dispatch ($it 0) (PATH 0 1))
152     (pop PATH)))
153
154 (exit 0)