aboutsummaryrefslogtreecommitdiff
path: root/rex
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-08-22 18:56:47 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2013-08-22 18:56:47 +0300
commitefc83a85ac071ea5657d75703a4680bfbd428456 (patch)
treead60c86022b51bbcb610fbc637436b551968f005 /rex
parent84b7e3c8d5e72ca9fe39165654a3bacaadd92f11 (diff)
downloadrex-efc83a85ac071ea5657d75703a4680bfbd428456.tar.gz
rex-efc83a85ac071ea5657d75703a4680bfbd428456.tar.bz2
Improve the --edit functionality.
* rex (ispasswd): New function (mkdbview,svdbview): Use ispasswd to select keys that should be encrypted. (dbchanged): New function. (editdb): Allow for nonexisting db file. Do not call whatnow if the database hasn't changed. (main); Use $usrconfdir/db as a default database. Create the directory if it does not exist, if the user confirms.
Diffstat (limited to 'rex')
-rwxr-xr-xrex63
1 files changed, 51 insertions, 12 deletions
diff --git a/rex b/rex
index 89ececd..1d3fa6b 100755
--- a/rex
+++ b/rex
@@ -370,6 +370,16 @@ proc passdec {code} {
encoding convertfrom ebcdic [binary format H* $code]
}
+proc ispasswd {key} {
+ if {$key == "pass" ||
+ ([string last ":pass" $key] > 0 &&
+ [string last ":pass" $key] == [expr [string length $key] - 5])} {
+ return 1
+ } else {
+ return 0
+ }
+}
+
# Read rex database FILE into VAR
proc readdb {file var} {
upvar $var x
@@ -433,9 +443,7 @@ proc mkdbview {dbname tempfile dbvar} {
puts $fd "# You are editing file $dbname"
puts $fd [format "# %-30.30s\t%s" "Key" "Value"]
foreach key [lsort -command keycmp [array names db]] {
- if {$key == "pass" ||
- ([string last ":pass" $key] > 0 &&
- [string last ":pass" $key] == [expr [string length $key] - 5])} {
+ if [ispasswd $key] {
puts $fd [format "%-32.32s\t%s" $key [passdec $db($key)]]
} else {
puts $fd [format "%-32.32s\t%s" $key $db($key)]
@@ -449,9 +457,7 @@ proc svdbview {tempfile outfile} {
array set x {}
readdb $tempfile x
foreach key [array names x] {
- if {$key == "pass" ||
- ([string last ":pass" $key] > 0 &&
- [string last ":pass" $key] == [expr [string length $key] - 5])} {
+ if [ispasswd $key] {
set x($key) [passenc $x($key)]
}
}
@@ -479,6 +485,26 @@ proc whatnow {} {
}
}
+proc dbchanged {filename var} {
+ global env
+ upvar $var olddb
+
+ readdb $filename newdb
+ foreach newkey [lsort -command keycmp [array names newdb]] oldkey [lsort -command keycmp [array names olddb]] {
+ if {$oldkey != $newkey} {
+ return 1
+ }
+ if [ispasswd $newkey] {
+ set newdb($newkey) [passenc $newdb($newkey)]
+ }
+
+ if {$olddb($newkey) != $newdb($newkey)} {
+ return 1
+ }
+ }
+ return 0
+}
+
# Edit database file dbname. The file is formatted in a more or less
# human-readable way, stored in a temporary file and an editor is started
# on that file.
@@ -487,8 +513,10 @@ proc editdb {dbname} {
array set rexdb {}
- readdb $dbname rexdb
-
+ if [file exists $dbname] {
+ readdb $dbname rexdb
+ }
+
if [info exist env(VISUAL)] {
set ed $env(VISUAL)
} elseif [info exist env(EDITOR)] {
@@ -502,6 +530,10 @@ proc editdb {dbname} {
mkdbview $dbname $tempfile rexdb
while 1 {
exec $ed $tempfile <@stdin >@stdout 2>@stderr
+ if {![dbchanged $tempfile rexdb]} {
+ break
+ }
+
switch [whatnow] {
e continue
q break
@@ -1106,12 +1138,19 @@ set argc [expr $argc - $optind]
if [config_option editdb] {
if {$argc >= 1} {
set dbname [lindex $argv 0]
- } elseif [file readable "$usrconfdir/db"] {
- set dbname "$usrconfdir/db"
} else {
- puts stderr "$0: no db file to edit"
- exit 1
+ set dbname "$usrconfdir/db"
}
+
+ set dir [file dirname $dbname]
+
+ if {![file exists $dir]} {
+ if {![getyn "Directory $dir does not exist: create"]} {
+ exit 0;
+ }
+ file mkdir $dir
+ }
+
editdb $dbname
exit 0
}

Return to:

Send suggestions and report system problems to the System administrator.