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