chiark / gitweb /
database updates seem to work
[ypp-sc-tools.db-test.git] / pctb / show-thing.tcl
index 4537e6af63b8242cd7afca15aebd1cad341f4e69..0b0b8f553616109d21da2d96a99f70bf33809a1f 100755 (executable)
@@ -1,4 +1,13 @@
-#!/usr/bin/tk
+#!/usr/bin/wish
+
+# usage:
+#  run show-thing without args
+#  then on stdin write
+#     one line which is a Tcl list for glyphsdone
+#     the xpm in the format expected
+#  then expect child to raise SIGSTOP or exit 0 or exit nonzero
+#  if child raised SIGSTOP, check database was updated
+
 
 proc manyset {list args} {
     foreach val $list var $args {
@@ -7,39 +16,76 @@ proc manyset {list args} {
     }
 }
 
-set foolist {
-    7 11 1 M
-    13 17 0 a
-    19 23 0 n
-}
-set unk_l 25
-set unk_r 29
-set unk_context 0
-
 
+#---------- display core ----------
 
 set mul 6
 set inter 1
-set rhsmost_max -1
-
-set f [open text.xpm]
-set o {}
-set y -3
-while {[gets $f l] >= 0} {
-    if {![regexp {^"(.*)",$} $l dummy l]} {
-       append o "$l\n"
-       continue
-    }
-    if {$y==-3} {
-       manyset $l cols rows colours cpp
-       #assert {$colours==2}
-       #assert {$cpp==1}
-       set mulcols [expr {$cols*$mul+$inter}]
-       set mulrows [expr {$rows*$mul+$inter}]
-       append o "\"$mulcols $mulrows 9 1\",\n"
-       for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
-    } elseif {$y==-2} { # first pixel
-       append o \
+
+set gotsh 20
+set csrh 20
+
+proc init_widgets {} {
+    global csrh gotsh
+    
+    frame .d
+
+    image create bitmap image/main
+    label .d.mi -image image/main -borderwidth 0
+
+    frame .d.csr -bg black -height $csrh
+    frame .d.got -bg black -height $gotsh
+
+    image create bitmap image/cursor -data \
+{#define csr_width 11
+#define csr_height 11
+static unsigned char csr_bits[] = {
+   0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
+   0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
+}
+
+    frame .d.csr.csr
+    label .d.csr.csr.l -image image/cursor -compound left
+    entry .d.csr.csr.e -bd 0
+    pack .d.csr.csr.l -side left
+
+    frame .d.csr_0 -bg white -width 1
+    frame .d.csr_1 -bg white -width 1
+
+    place .d.csr -x 0 -y 0
+    place .d.mi -x 0 -y $csrh
+    pack .d
+
+    frame .help
+    pack .help
+}
+
+
+#---------- xpm input processor ----------
+
+proc read_xpm {f} {
+    global glyphsdone mul inter rhsmost_max unk_l unk_r gotsh csrh
+    global cols rows wordmap
+    
+    set o {}
+    set y -3
+    while 1 {
+       if {[gets $f l] < 0} { error "huh? "}
+       if {![regexp {^"(.*)",$} $l dummy l]} {
+           append o "$l\n"
+           if {[regexp {^\}\;$} $l]} break
+           continue
+       }
+       if {$y==-3} {
+           manyset $l cols rows colours cpp
+           #assert {$colours==2}
+           #assert {$cpp==1}
+           set mulcols [expr {$cols*$mul+$inter}]
+           set mulrows [expr {$rows*$mul+$inter}]
+           append o "\"$mulcols $mulrows 9 1\",\n"
+           for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
+       } elseif {$y==-2} { # first pixel
+           append o \
 "\"+ c #111\",
 \"a c #800\",
 \"A c #fcc\",
@@ -49,103 +95,91 @@ while {[gets $f l] >= 0} {
 \"U c #ff0\",
 \"q c #000\",
 \"Q c #ff0\",\n"
-    } elseif {$y==-1} { # 2nd pixel but we've already printed ours
-    } else {
-       set ybit [expr {1<<$y}]
-       set x 0
-        set ol "\"+"
-        set olh $ol
-       foreach c [split $l ""] {
-           set how "u"
-           if {$x >= $unk_l && $x <= $unk_r} {
-               set how q
-           } else {
-               set ab 0
-               foreach {min max context got} $foolist {
-                   set rhsmost_max $max
-                   if {$x >= $min && $x <= $max} {
-                       set how [lindex {a b} $ab]
-                       break
+       } elseif {$y==-1} { # 2nd pixel but we've already printed ours
+       } else {
+           set ybit [expr {1<<$y}]
+           set x 0
+           set ol "\"+"
+           set olh $ol
+           foreach c [split $l ""] {
+               set how "u"
+               if {$x >= $unk_l && $x <= $unk_r} {
+                   set how q
+               } else {
+                   set ab 0
+                   foreach {min max context got} $glyphsdone {
+                       set rhsmost_max $max
+                       if {$x >= $min && $x <= $max} {
+                           set how [lindex {a b} $ab]
+                           break
+                       }
+                       set ab [expr {!$ab}]
                    }
-                   set ab [expr {!$ab}]
                }
-           }
-           switch -exact $c {
-               " " { set p $how }
-               "o" {
-                   set p [string toupper $how]
-                   incr wordmap($x) $ybit
+               switch -exact $c {
+                   " " { set p $how }
+                   "o" {
+                       set p [string toupper $how]
+                       incr wordmap($x) $ybit
+                   }
+                   default { error "$c ?" }
                }
-               default { error "$c ?" }
-           }
-           append ol "[string repeat $p [expr {$mul-$inter}]][
+               append ol "[string repeat $p [expr {$mul-$inter}]][
                          string repeat + $inter]"
-           append olh [string repeat + $mul]
-           incr x
+               append olh [string repeat + $mul]
+               incr x
+           }
+           set ole "\",\n"
+           append ol $ole
+           append olh $ole
+           set olhn [string repeat $olh $inter]
+           if {!$y} { append o $olhn }
+           append o [string repeat $ol [expr {$mul-1}]]
+           append o $olhn
        }
-        set ole "\",\n"
-       append ol $ole
-       append olh $ole
-       set olhn [string repeat $olh $inter]
-        if {!$y} { append o $olhn }
-        append o [string repeat $ol [expr {$mul-1}]]
-       append o $olhn
+       incr y
     }
-    incr y
+    set data [exec xpmtoppm << $o]
+    image create photo image/main -data $data
+    
+    foreach w {.d .d.csr .d.got} {
+       $w configure -width $mulcols
+    }
+    .d configure -height [expr {$csrh+$mulrows+$gotsh}]
+    foreach w {0 1} {
+       .d.csr_$w configure -height $mulrows
+    }
+    place .d.got -x 0 -y [expr {$csrh+$mulrows}]
 }
 
-#puts $o
-
-set xpm [exec xpmtoppm << $o]
 
-set gotsh 20
-set csrh 20
+#---------- per-invocation display ----------
 
-frame .d -width $mulcols -height [expr {$csrh+$mulrows+$gotsh}]
-
-set mi [image create photo -data $xpm]
-label .d.mi -image $mi -borderwidth 0
-
-frame .d.csr -bg black -width $mulcols -height $csrh
-frame .d.got -bg black -width $mulcols -height $gotsh
-
-foreach {min max context got} $foolist {
-    frame .d.got.m$min -bd 0 -background \#888
-    label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
-    pack .d.got.m$min.l -padx 1 -pady 1
-    place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
+proc draw_glyphsdone {} {
+    global glyphsdone mul inter
+    eval destroy [winfo children .d.got]
+    foreach {min max context got} $glyphsdone {
+       frame .d.got.m$min -bd 0 -background \#888
+       label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
+       pack .d.got.m$min.l -padx 1 -pady 1
+       place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
+    }
 }
 
-set imcsr [image create bitmap -data \
-{#define csr_width 11
-#define csr_height 11
-static unsigned char csr_bits[] = {
-   0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
-   0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
-}]
-
-frame .d.csr.csr
-label .d.csr.csr.l -image $imcsr -compound left
-entry .d.csr.csr.e -bd 0
-pack .d.csr.csr.l -side left
-
-frame .d.csr_0 -bg white -height $mulrows -width 1
-frame .d.csr_1 -bg white -height $mulrows -width 1
-
-place .d.csr -x 0 -y 0
-place .d.mi -x 0 -y $csrh
-place .d.got -x 0 -y [expr {$csrh+$mulrows}]
-pack .d
+proc startup_cursor {} {
+    global cur_already cur_mode cur_0 cur_1 last_ht
+    global glyphsdone unk_l unk_r
+    
+    set cur_already [expr {[llength $glyphsdone]/4-1}]
+    set cur_mode 1 ;# one of:   0 1 already text
 
-frame .help
-pack .help
+    set cur_0 $unk_l
+    set cur_1 [expr {$unk_r+1}]
+    set last_ht {}
+}
 
-set cur_already [expr {[llength $foolist]/4-1}]
-set cur_mode 1 ;# one of:   0 1 already text
 
-set cur_0 $unk_l
-set cur_1 [expr {$unk_r+1}]
-set last_ht {}
+#---------- runtime display and keystroke handling ----------
 
 proc helptext {t} {
     global last_ht
@@ -166,13 +200,13 @@ proc helptext {t} {
 proc recursor/0 {} { recursor//01 0 }
 proc recursor/1 {} { recursor//01 1 }
 proc recursor//01 {z1} {
-    global mul rhsmost_max cols foolist
+    global mul rhsmost_max cols glyphsdone
     upvar #0 cur_$z1 cur
     .d.csr.csr.l configure -text {adjust}
     place .d.csr.csr -x [expr {$cur*$mul - 7}]
     bind_key space { othercursor }
     bind_leftright cur_$z1 0 [expr {$cols-1}]
-    if {[llength $foolist]} {
+    if {[llength $glyphsdone]} {
        bind_key Tab { set cur_mode already; recursor }
     } else {
        bind_key Tab {}
@@ -221,20 +255,20 @@ proc recursor/text {} {
 
 proc recursor/already {} {
     global mul
-    global foolist
+    global glyphsdone
     global cur_already mul
-    global foolist cur_already mul
+    global glyphsdone cur_already mul
     .d.csr.csr.l configure -text {correct}
-    set rmax [lindex $foolist [expr {$cur_already*4}]]
+    set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
     place .d.csr.csr -x [expr {$rmax*$mul-3}]
     bind_key Return {}
     bind_key space {}
-    bind_leftright cur_already 0 [expr {[llength $foolist]/4-1}]
+    bind_leftright cur_already 0 [expr {[llength $glyphsdone]/4-1}]
     bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
     bind_key Delete {
-       RETURN_RESULT DELETE [lrange $foolist \
+       RETURN_RESULT DELETE [lrange $glyphsdone \
                                  [expr $cur_already*4] \
-                                 [expr $cur_already*4+1]]
+                                 [expr $cur_already*4+2]]
     }
     helptext {
        {{<- ->}   {move cursor, selecting glyph to correct}}
@@ -275,6 +309,8 @@ proc recursor {} {
 }
 
 
+#---------- database read and write ----------
+
 # database format:
 # series of glyphs:
 #   <context> <ncharacters> <hex>...
@@ -287,11 +323,10 @@ proc read_database {} {
     global database
     set f [open database r]
     while {[gets $f l] >= 0} {
-       if {![regexp {^(\w+) (\d+) ([0-9a-f]{2}+)$} $l \
+       if {![regexp {^(\w+) (\d+) ((?:[0-9a-f]{2})+)$} $l \
                  dummy context strl strh]} {
            error "bad syntax"
        }
-        binary scan $strw h* strh
        if {[string length $strh] != $strl*2} { error "$strh $strl" }
        gets $f l; set width [format %d $l]
        set bm $context
@@ -300,6 +335,7 @@ proc read_database {} {
        }
        set database($bm) $strh
     }
+    close $f
 }
 
 proc write_database {} {
@@ -314,32 +350,49 @@ proc write_database {} {
        foreach x [lrange $bm 1 end] { append o "$x\n" }
        lappend ol $o
     }
+    set f [open database.new w]
     foreach o [lsort $ol] {
-       puts -nonewline $o
+       puts -nonewline $f $o
     }
+    close $f
+    file rename -force database.new database
+}
+
+proc dbkey {ctx l r} {
+    global wordmap
+    set bm $ctx
+    for {set x $l} {$x <= $r} {incr x} {
+       lappend bm [format %x $wordmap($x)]
+    }
+    return $bm
 }
 
 proc update_database/DEFINE {c0 c1 strh} {
-    global foolist unk_l unk_context wordmap database
+    global glyphsdone unk_l unk_context wordmap database
     if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
     if {$c0 == $unk_l} {
        set ncontext $unk_context
     } else {
-       foreach {l r context got} $foolist {
+       foreach {l r context got} $glyphsdone {
            if {$l==$c0} { set ncontext $context; break }
        }
-       if {![exists ncontext]} {
+       if {![info exists ncontext]} {
            puts stderr "must start at letter LHS!"
            return
        }
     }
-    set bm $ncontext
-    for {set x $c0} {$x < $c1} {incr x} {
-       lappend bm [format %x $wordmap($x)]
-    }
+    incr c1 -1
+    set bm [dbkey $ncontext $c0 $c1]
     set database($bm) $strh
     write_database
 }
+
+proc update_database/DELETE {l r ctx} {
+    global database
+    set bm [dbkey $ctx $l $r]
+    unset database($bm)
+    write_database
+}
     
 
 proc RETURN_RESULT {how what} {
@@ -354,5 +407,27 @@ proc RETURN_RESULT {how what} {
 
 #    bind . <Key-space> {}
 
+proc test_main {} {
+    global glyphsdone unk_l unk_r unk_context
+
+    set glyphsdone {
+       7 11 1 M
+       13 17 0 a
+       19 23 0 n
+    }
+    set unk_l 25
+    set unk_r 29
+    set unk_context 0
+
+    set f [open text.xpm]
+    read_xpm $f
+    close $f
+
+    draw_glyphsdone
+    startup_cursor
+}
+
 read_database
+init_widgets
+test_main
 recursor