;; regex to identify block starters
'ENI-HEAD (format "^\\s*#?\\s*(%s)" (join ENI-KEY "|"))
'ENI-COMMENT "^\\s*#"
+ 'PROC (if (exec (format "command -v %s" (or (env "EDITOR") "nano"))) ($it 0)
+ "/bin/nano")
+ 'SUDO (if (exec "command -v sudo") ($it 0) "")
)
(define (is-eni-key PAT S)
(setf PATH '(( 0 "/etc/network/interfaces" )) )
; Edit a file
-(define (edit-file I FILE SUDO)
- (let ((PROC (if SUDO "/usr/bin/sudo /bin/nano" "/bin/nano")))
- (wait-pid (process (format "%s -F -J 75 -I -l +%d %s" PROC (int I) FILE)))
- ))
+(define (edit-file I FILE)
+ (wait-pid (process (format "%s %s +%d %s" SUDO PROC (int I) FILE))))
(define (ensure-newline TXT)
(if (empty? TXT) "" (ends-with TXT "\n") TXT (string TXT "\n")))
(write-file TXT (string (join (0 B DATA) "\n" true)
(ensure-newline (read-file TXT))
(join (E DATA) "\n")))
- (exec (format "sudo mv %s %s" TXT FILE))
+ (exec (format "%s mv %s %s" SUDO TXT FILE))
))
(define (key-command-select I FILE) ; PATH
(letn ((BLOCK (find-block (- (int I) 1) FILE))
- (TMP "/tmp/enitool.conf")
+ (TMP "/tmp/enitool/tmp.conf")
(HEAD (BLOCK (- (BLOCK 1) (BLOCK 0) -3)))
(TAG (or (and (regex "^#?(\\w*) (.*)" HEAD 0) $1) "#"))
(VALUE $2))
(define (delete-block-maybe I FILE)
(let ((BLOCK (find-block (- (int I) 1) FILE))
- (TMP "/tmp/enitool.conf"))
+ (TMP "/tmp/enitool/tmp.conf"))
(when (= (3 BLOCK) '(""))
- (exec (format "sudo ed %s" FILE)
+ (exec (format "%s ed %s" SUDO FILE)
(format "%dd\nw\n" (+ 1 (BLOCK 0)))))))
(define (toggle-commenting I FILE)
(let ((BLOCK (find-block (- (int I) 1) FILE))
- (TMP "/tmp/enitool.conf"))
+ (TMP "/tmp/enitool/tmp.conf"))
(letn ((H (- (BLOCK 1) (BLOCK 0)))
(TXT (3 BLOCK))
(toggle (if (starts-with (TXT H) "#")
((member $2 '("KEY_RIGHT" "RETURN")) (key-command-select $1 FILE))
((= $2 "d") (delete-block-maybe $1 FILE))
((= $2 "#") (toggle-commenting $1 FILE))
- ((= $2 "e") (edit-file $1 FILE "sudo"))
+ ((= $2 "e") (edit-file $1 FILE SUDO))
)))
(define (iselect POS FILE)
(exec (format "iselect -n '%s' -t '%s' -a -P -K '-k#' -kd -ke -p %d < %s"
"enitool" FILE (int POS) FILE)))
+(change-dir "/etc/network")
+(wait-pid (process (format "%s mkdir -m 777 -p /tmp/enitool" SUDO)))
+
(while PATH
(let ((SEL (apply iselect (PATH 0))) (FILE (PATH 0 1)))
(if SEL (command-dispatch (SEL 0) FILE)