X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;ds=sidebyside;f=enitool.lsp;h=af06741200aa4a374ef61b474b1e0f6446ffbbd0;hb=d0898f3ed152897f45f7f29a15ff1046531195c0;hp=0efd7977f4d7bdf60e2196794d539aeb63a7e58b;hpb=a2e70494b3f1df178e6992b219f35aa6ddd900b5;p=rrq%2Fnewlisp-ftw.git diff --git a/enitool.lsp b/enitool.lsp index 0efd797..af06741 100755 --- a/enitool.lsp +++ b/enitool.lsp @@ -11,7 +11,13 @@ # right-arrow or return = follow to sourced file, or edit the current block # left-arrow or q = go up or exit -;(signal 2 (fn (x) (exit))) +(signal 2 (fn (x) (exit))) + +(when (!= "0" (if (exec "id -u") ($it 0) "")) + (let ((SUDO (if (exec "command -v sudo") ($it 0) "/usr/bin/sudo")) + (ED (format "env EDITOR=%s" (or (env "EDITOR") "nano"))) ) + (wait-pid (process (join (flat (list SUDO ED (main-args))) " "))) + (exit 0))) (constant ;; all "block starters", including blank lines @@ -20,14 +26,15 @@ ;; regex to identify block starters 'ENI-HEAD (format "^\\s*#?\\s*(%s)" (join ENI-KEY "|")) 'ENI-COMMENT "^\\s*#" + 'EDITOR (or (env "EDITOR") "nano") + 'PROC (if (exec (format "command -v %s" EDITOR)) ($it 0) "/bin/nano") ) (define (is-eni-key PAT S) (when (regex PAT S 0) true)) (define (is-eni-comment S) - (is-eni-key ENI-COMMENT S) - ) + (is-eni-key ENI-COMMENT S)) (define (istrue? A B) (list A B) (= A B true)) @@ -37,7 +44,7 @@ ;; Pull out the block headed by the B line. If this head is a blank ;; line, then the block includes preceeding comment and the blank line -;; only. Otherwise it includes preceeding comment, head line and fthe +;; only. Otherwise it includes preceeding comment, head line and the ;; following mix of non-head lines and comment lines (i.e. up to next ;; head line). FROM is the line after the prior block, and it is moved ;; to end of this block. @@ -72,26 +79,23 @@ (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 +%d %s" PROC (int I) FILE)))) (define (ensure-newline TXT) (if (empty? TXT) "" (ends-with TXT "\n") TXT (string TXT "\n"))) (define (update-file B E TXT FILE) - ;;(println (list 'update-file B E TXT FILE)) (let ((DATA (parse (read-file FILE) "\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 "mv %s %s" 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)) @@ -107,14 +111,14 @@ (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 "ed -s %s" 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) "#") @@ -133,17 +137,19 @@ ((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)) ))) (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") +(make-dir "/tmp/enitool") + (while PATH - (let ((SEL (apply iselect (PATH 0))) (FILE (PATH 0 1))) - (if SEL (command-dispatch (SEL 0) FILE) - (pop PATH)) - )) + (if (apply iselect (PATH 0)) + (command-dispatch ($it 0) (PATH 0 1)) + (pop PATH))) (exit 0)