3 # Helper tool to browse and edit the ifupdown configuration
4 # uses iselect, ed, nano and sudo
6 # Extra iselect commands:
7 # # = toggle commenting of configuration block
8 # d = delete empty line
9 # e = edit the whole file
11 # right-arrow or return = follow to sourced file, or edit the current block
12 # left-arrow or q = go up or exit
14 ;(signal 2 (fn (x) (exit)))
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) " ")))))
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 "|"))
28 'EDITOR (or (env "EDITOR") "nano")
29 'PROC (if (exec (format "command -v %s" EDITOR)) ($it 0) "/bin/nano")
32 (define (is-eni-key PAT S)
33 (when (regex PAT S 0) true))
35 (define (is-eni-comment S)
36 (is-eni-key ENI-COMMENT S))
39 (list A B) (= A B true))
41 (define (eni-starters) ; DATA
42 (flat (ref-all ENI-HEAD DATA is-eni-key)))
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)))))
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"))
60 (setf LASTENI (map sub-divide-DATA BEG (1 BEG)))
61 ;;(map println LASTENI)
66 (define (add-selector X)
67 (cons (format "<s:%d>%s" (X 0) (X 1)) (2 X)))
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)))
74 ############################################################
75 ### Interactive actions
77 ## PATH holds interactive state as a stack of [pos file]
78 (setf PATH '(( 0 "/etc/network/interfaces" )) )
81 (define (edit-file I FILE)
82 (wait-pid (process (format "%s +%d %s" PROC (int I) FILE))))
84 (define (ensure-newline TXT)
85 (if (empty? TXT) "" (ends-with TXT "\n") TXT (string TXT "\n")))
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))
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) "#"))
102 ("source" (push (list 0 VALUE) PATH))
103 (true (write-file TMP (join (3 BLOCK) "\n" true))
104 (let ((F (file-info TMP 6)))
106 (when (!= F (file-info TMP 6))
107 (update-file (BLOCK 0) (BLOCK 2) TMP FILE)))
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)))))))
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)))
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)
132 (define (command-dispatch CMD FILE)
133 (when (regex "([^:]+):([^:]+):(\\S*)\\s*(.*)" CMD 0)
134 (setf (PATH 0 0) (int $1))
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))
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)))
146 (change-dir "/etc/network")
147 (make-dir "/tmp/enitool")
150 (if (apply iselect (PATH 0))
151 (command-dispatch ($it 0) (PATH 0 1))