3 # helper program for OCR in PCTB upload client
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission. This program is not endorsed or
25 # sponsored by Three Rings.
30 # run this without args
32 # one line which is a Tcl list for unk_{l,r} unk_contexts glyphsdone etc.
33 # the xpm in the format expected
34 # then expect child to exit 0, or write a single 0 byte to fd 4
35 # if it wrote a byte to fd 4, it can take another question
38 #---------- library routines ----------
40 proc manyset {list args} {
41 foreach val $list var $args {
47 proc must_gets {f lvar} {
49 if {[gets $f l] < 0} { error "huh?" }
52 proc must_gets_exactly {f expected} {
54 if {[string compare $expected $got]} { error "$expected $got ?" }
57 proc read_counted {f var} {
60 set val [read $f $count]
62 if {[string length $eol]} { error "$eol ?" }
63 debug "READ_COUNTED $count $var"
66 proc puts_counted {f dvar} {
68 set count [string length $d]
71 debug "PUTS_COUNTED $count $dvar"
75 global errorCode errorInfo
76 puts stderr "ERROR: $m\n[list $errorCode]\n$errorInfo\n";
80 #---------- display core ----------
89 proc init_widgets {} {
91 global csrh gotsh ctxh
93 if {[winfo exists .d]} return
95 frame .privacy -bd 2 -relief groove
96 pack .privacy -side top -padx 2 -pady 2 -fill x
100 frame .d -bd 2 -relief groove -pady 2 -padx 2
102 image create bitmap image/main
103 label .d.mi -image image/main -borderwidth 0
105 frame .d.csr -bg black -height $csrh
106 frame .d.got -bg black -height $gotsh
107 frame .d.ctx -bg black
109 image create bitmap image/cursor -data \
110 {#define csr_width 11
111 #define csr_height 11
112 static unsigned char csr_bits[] = {
113 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
114 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
118 label .d.csr.csr.l -image image/cursor -compound left
119 entry .d.csr.csr.e -bd 0
120 pack .d.csr.csr.l -side left
122 frame .d.mi.csr_0 -bg white -width 1
123 frame .d.mi.csr_1 -bg white -width 1
127 button .d.pe.ok -text OK
128 pack .d.pe.grid .d.pe.ok -side left
130 pack .d.mi .d.ctx -side top
131 pack .d -fill x -padx 2 -pady 2
133 frame .help -bd 2 -relief groove
134 pack .help -pady 2 -padx 2
137 proc resize_widgets_core {} {
138 global mulcols mulrows csrh gotsh ctxh glyphsdone
139 global unk_l unk_contexts
141 foreach w {.d.csr .d.got .d.ctx} {
142 $w configure -width $mulcols
145 eval destroy [winfo children .d.ctx]
152 if {![string compare $t $last_ht]} return
153 eval destroy [grid slaves .help]
154 set y 0; foreach l $t {
155 set x 0; foreach c $l {
156 set w .help.at${x}x${y}
158 grid $w -row $y -column $x -padx 5 -sticky w
166 proc bind_key {k proc} {
168 bind .d <Key-$k> $proc
169 set keybindings($k) [expr {!![string length $proc]}]
170 .d configure -takefocus 1
172 proc unbind_all_keys {} {
174 foreach k [array names keybindings] { bind_key $k {} }
175 .d configure -takefocus 0
178 #---------- database read and write common wrapper ----------
181 if {[gets $f l] < 0} { error "unexpected db eof" }
185 proc read_database {fn} {
186 global reqkind database database_fn
187 upvar #0 database_magic/$reqkind magic
188 catch { unset database }
191 if {![file exists $database_fn]} return
192 set f [open $database_fn r]
193 if {[string compare [db_getsl $f] $magic]} { error "$l $reqkind ?" }
195 read_database_header/$reqkind $f
199 if {![string length $l1]} continue
200 if {[regexp {^\#} $l1]} continue
201 if {![string compare . $l1]} break
203 read_database_entry/$reqkind $f $l1
208 proc write_database {} {
209 global reqkind database_fn database
210 upvar #0 database_magic/$reqkind magic
212 set f [open $database_fn.new w]
215 write_database_header/$reqkind $f
218 foreach bm [array names database] {
219 lappend ol [format_database_entry/$reqkind $bm $database($bm)]
221 foreach o [lsort $ol] {
226 file rename -force $database_fn.new $database_fn
229 proc select_database {dbname_spec} {
231 set dbname $dbname_spec
232 read_database "./#local-$dbname#.txt"
235 proc do_database_update {im def} {
237 maybe_upload_entry $im $def
238 set database($im) $def
242 proc required/char {} {
243 global mulrows glyphsdone unk_l unk_r unk_contexts rows
247 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
248 set glyphsdone [lrange $l 3 end]
255 .d.mi.csr_$w configure -height $mulrows
258 foreach {min max contexts got} $glyphsdone {
259 show_context maxh $min $contexts
261 show_context maxh $unk_l $unk_contexts
262 .d.ctx configure -height $maxh
264 pack .d.csr -side top -before .d.mi
265 pack .d.got .d.ctx -side top -after .d.mi
268 select_database char$rows
273 #========== PIXMAPS ==========
275 #---------- pixmap database read and write ----------
277 set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1}
279 proc read_database_header/pixmap {f} { }
280 proc read_database_entry/pixmap {f def} {
285 set p3 [db_getsl $f]; append im $p3 "\n"
286 if {[string compare $p3 P3]} { error "$p3 ?" }
288 set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h
289 set depth [db_getsl $f]; append im $depth "\n"
291 for {set y 0} {$y < $h} {incr y} {
292 set line [db_getsl $f]; append im $line "\n"
294 set database($im) $def
296 proc write_database_header/pixmap {f} { puts $f "" }
297 proc format_database_entry/pixmap {im def} {
301 #---------- pixmap display and input handling ----------
303 proc foreach_pixmap_col {var body} {
306 for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
311 proc pixmap_select {ncol} {
313 debug "PIX SELECT $ncol [llength $alloptions]"
314 foreach_pixmap_col col {
315 if {$col==$ncol} continue
316 .d.pe.grid.l$col selection clear 0 end
320 proc pixmap_maybe_ok {} {
321 global alloptions pixmap_selcol pixmap_selrow
323 foreach_pixmap_col col {
324 set cs [.d.pe.grid.l$col curselection]
325 set lcs [llength $cs]
328 set pixmap_selcol $col
329 set pixmap_selrow [lindex $cs 0]
332 debug "MAYBE_OK YES col=$pixmap_selcol row=$pixmap_selrow."
333 .d.pe.ok configure -state normal -command pixmap_ok
335 .d.pe.ok configure -state disabled -command {}
339 global database ppm pixmap_selcol pixmap_selrow mainkind alloptions
340 foreach_pixmap_col col {
341 .d.pe.grid.l$col configure -state disabled
343 .d.pe.ok configure -state disabled
344 helptext {{{ Processing }}}
345 manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \
347 manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \
349 set result "$colname - $rowname"
350 debug "UPDATE PIXMAP AS >$result<"
352 do_database_update $ppm $result
356 proc required/pixmap {} {
357 global unk_what ppm mulcols alloptions
358 must_gets stdin unk_what
359 debug "GOT pixmap $unk_what"
363 if {![string length $ppml]} break
364 append ppm $ppml "\n"
366 set data [exec pnmscale 2 << $ppm]
367 image create photo image/main -data $data
369 set alloptions [exec ./dictionary-pixmap-options $unk_what]
371 select_database pixmap
373 set mulcols [image width image/main]
374 set mulrows [image height image/main]
376 place forget .d.mi.csr_0
377 place forget .d.mi.csr_1
379 pack forget .d.csr .d.got
380 pack .d.pe -side top -before .d.mi -pady 2
381 .d configure -takefocus 0
384 eval destroy [winfo children .d.pe.grid]
385 set col 0; foreach {colname coldesc rows} $alloptions {
386 debug "INIT $col $colname \"$coldesc\""
387 label .d.pe.grid.t$col -text $colname
388 listbox .d.pe.grid.l$col
389 foreach {rowname rowdesc} $rows {
390 debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
391 .d.pe.grid.l$col insert end $rowdesc
393 bind .d.pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
394 grid .d.pe.grid.t$col -column $col -row 0
395 grid .d.pe.grid.l$col -column $col -row 1
401 {{Indicate the meaning of this image, and click OK.}}
405 #========== UPLOADS TO DICTIONARY SERVER ==========
407 proc upload_init {} {
408 global privacy_setting
410 set privacy_setting [upload_status]
412 label .privacy.warn -text " Privacy "
413 if {$privacy_setting} {
414 .privacy.warn configure -background yellow -foreground black
416 label .privacy.overall -text " Upload new dictionary entry:"
417 label .privacy.reference -text " See README.privacy."
419 pack .privacy.warn .privacy.overall -side left
421 foreach {setting string} {
424 2 {Yes, quoting my pirate name.}
426 radiobutton .privacy.o$setting -text $string \
427 -value $setting -variable privacy_setting
428 pack .privacy.o$setting -side left
429 if {$setting > $privacy_setting} {
430 .privacy.o$setting configure -state disabled
433 pack .privacy.reference -side left
435 if {!$privacy_setting} {
436 foreach w [winfo children .privacy] {
437 $w configure -state disabled
440 if {$privacy_setting} {
442 ::http::config -urlencoding utf-8
446 proc upload_status {} {
447 # returns 0, 1, 2 for none, anon, with pirate name
450 if {![info exists env(YPPSC_PCTB_DICT_SUBMIT)]} { debug a; return 0 }
451 if {![string compare 0 $env(YPPSC_PCTB_DICT_SUBMIT)]} { debug b; return 0 }
453 if {![info exists env(YPPSC_PIRATE)]} { return 1 }
454 if {![info exists env(YPPSC_OCEAN)]} { return 1 }
455 if {![string length $env(YPPSC_PIRATE)]} { return 1 }
456 if {![string length $env(YPPSC_OCEAN)]} { return 1 }
461 proc maybe_upload_entry {im def} {
462 global reqkind privacy_setting env dbname quiet
464 debug "DB-UPDATE PRIVACY $privacy_setting"
465 if {!$privacy_setting} return
467 debug "DB-UPDATE UPLOADING"
470 lappend pl dict $dbname
472 if {$privacy_setting>=2} {
473 set pirate [string totitle $env(YPPSC_PIRATE)]
474 set ocean [string totitle $env(YPPSC_OCEAN)]
475 debug "DB-UPDATE NON-ANON $ocean $pirate"
480 lappend pl entry [format_database_entry/$reqkind $im $def]
482 set url $env(YPPSC_PCTB_DICT_SUBMIT)
483 append url dictionary-update-receiver
485 set query [eval ::http::formatQuery $pl]
486 regsub -all {%0d} $query {} query
487 debug "DB-UPDATE QUERY [string range $query 0 200]..."
489 if {[regexp {^\.?/} $url]} {
490 set cmd [list $url $query]
491 debug "SUBMIT CMD [string range $cmd 0 200]..."
492 set body [eval exec $cmd 2>@ stderr]
493 regsub {^Content-Type: text/plain.*\n\n} $body {} body
497 set req [::http::geturl $url -query $query]
500 "\nWARNING: Cannot do dictionary upload: $emsg\n"
504 debug "DB-UPDATE DONE $req $s(status) [array names s]"
505 set ncode [::http::ncode $req]
507 if {!(![string compare ok $s(status)] &&
508 ![string compare 200 $ncode])} {
509 set m "\nWARNING: Dictionary upload failed:"
510 foreach v {status http error posterror} {
511 if {[info exists s($v)]} { append m "\n $v: $s($v)" }
520 if {![string match {OK *} $body]} {
521 set m "\nWARNING: Dictionary upload went wrong:\n"
522 append m "\n " [join [split $body "\n"] "\n "]
529 "Uploaded $dbname dictionary entry `$def': $body"
533 #========== CHARACTER SET ==========
535 #---------- xpm input processor ----------
537 proc char_read_xpm {f} {
538 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
539 global cols rows wordmap
545 if {![regexp {^"(.*)",$} $l dummy l]} {
547 if {[regexp {^\}\;$} $l]} break
551 manyset $l cols rows colours cpp
552 if {$colours!=2 || $cpp!=1} { error "$l ?" }
554 set chop_l [expr {$unk_l - 80}]
555 set chop_r [expr {$cols - $unk_l - 100}]
556 if {$chop_l<0} { set chop_l 0 }
558 set unk_l [expr {$unk_l - $chop_l}]
559 set unk_r [expr {$unk_r - $chop_l}]
561 foreach {min max contexts got} $glyphsdone {
563 [expr {$min-$chop_l}] \
564 [expr {$max-$chop_l}] \
570 set cols [expr {$cols - $chop_l - $chop_r}]
571 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
574 set mulcols [expr {$cols*$mul+$inter}]
575 set mulrows [expr {$rows*$mul+$inter}]
576 append o "\"$mulcols $mulrows 9 1\",\n"
577 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
578 } elseif {$y==-2} { # first pixel
589 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
591 set ybit [expr {1<<$y}]
596 set l [string range $l $chop_l end-$chop_r]
598 set l [string range $l $chop_l end]
599 append l [string repeat " " [expr -$chop_r]]
601 foreach c [split $l ""] {
603 if {$x >= $unk_l && $x <= $unk_r} {
607 foreach {min max contexts got} $glyphsdone {
609 if {$x >= $min && $x <= $max} {
610 set how [lindex {a b} $ab]
619 set p [string toupper $how]
620 incr wordmap($x) $ybit
622 default { error "$c ?" }
624 append ol "[string repeat $p [expr {$mul-$inter}]][
625 string repeat + $inter]"
626 append olh [string repeat + $mul]
632 set olhn [string repeat $olh $inter]
633 if {!$y} { append o $olhn }
634 append o [string repeat $ol [expr {$mul-1}]]
639 set data [exec xpmtoppm << $o]
640 image create photo image/main -data $data
643 #---------- character set editor display ----------
645 proc show_context {maxhv x ctxs} {
649 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
650 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
651 place $w -x [expr {($x-1)*$mul}] -y 0
652 set wh [winfo reqheight $w]
653 if {$wh > $maxh} { set maxh $wh }
656 proc draw_glyphsdone {} {
657 global glyphsdone mul inter
658 eval destroy [winfo children .d.got]
659 foreach {min max contexts got} $glyphsdone {
660 frame .d.got.m$min -bd 0 -background \#888
661 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
662 pack .d.got.m$min.l -padx 1 -pady 1
663 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
667 proc startup_cursor {} {
668 global cur_already cur_mode cur_0 cur_1 last_ht
669 global glyphsdone unk_l unk_r
671 set cur_already [expr {[llength $glyphsdone]/4-1}]
672 set cur_mode 1 ;# one of: 0 1 already text
675 set cur_1 [expr {$unk_r+1}]
680 #---------- character set runtime display and keystroke handling ----------
682 proc recursor/0 {} { recursor//01 0 }
683 proc recursor/1 {} { recursor//01 1 }
684 proc recursor//01 {z1} {
685 global mul rhsmost_max cols glyphsdone
687 .d.csr.csr.l configure -text {adjust}
688 place .d.csr.csr -x [expr {$cur*$mul - 7}]
689 bind_key space { othercursor }
690 bind_leftright_q cur_$z1 0 [expr {$cols-1}]
691 if {[llength $glyphsdone]} {
692 bind_key BackSpace { set cur_mode already; recursor }
694 bind_key BackSpace {}
697 if {$cur_0 != $cur_1} {
698 .d.csr.csr.e delete 0 end
704 {{<- ->} {move cursor, adjusting area to define}}
705 {Space {switch to moving other cursor}}
706 {Return {confirm location, enter letter(s)}}
707 {Backspace {switch to correcting earlier ocr}}
708 {Q {quit and abandon OCR run}}
711 proc othercursor {} {
713 set cur_mode [expr {!$cur_mode}]
717 proc recursor/text {} {
719 {Return {confirm entry of new glyph}}
720 {Escape {abandon entry}}
723 .d.csr.csr.l configure -text {define:}
724 pack .d.csr.csr.e -side left
726 bind .d.csr.csr.e <Key-Return> {
727 set strq [.d.csr.csr.e get]
728 if {[regexp -line {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
729 RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
732 bind .d.csr.csr.e <Key-Escape> {
734 pack forget .d.csr.csr.e
741 proc recursor/already {} {
744 global cur_already mul
745 global glyphsdone cur_already mul
746 .d.csr.csr.l configure -text {correct}
747 set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
748 place .d.csr.csr -x [expr {$rmax*$mul-3}]
750 bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/4-1}]
751 bind_key space { bind_key Delete {}; set cur_mode 1; recursor }
753 RETURN_RESULT DELETE [lrange $glyphsdone \
754 [expr $cur_already*4] \
755 [expr $cur_already*4+2]]
758 {{<- ->} {move cursor, selecting glyph to correct}}
759 {Del {clear this glyph from the recognition database}}
760 {Space {switch to selecting area to define as new glyph}}
761 {Q {quit and abandon OCR run}}
765 proc bind_leftright_q {var min max} {
766 bind_key Left [list leftright $var $min $max -1]
767 bind_key Right [list leftright $var $min $max +1]
769 puts stderr "\nCharacter resolver quitting as you requested."
773 proc leftright {var min max inc} {
777 if {$vnew < $min || $vnew > $max} return
783 global csrh cur_mode cur_0 cur_1 mul
785 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
790 #---------- character database read and write ----------
795 # <context> <ncharacters> <hex>...
799 # $database($context 0x<bits> 0x<bits>...) = $hex
801 set database_magic/char {# ypp-sc-tools pctb font v1}
803 proc read_database_header/char {f} {
805 if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
807 proc read_database_entry/char {f context} {
810 set strq [db_getsl $f]
813 if {![string length $l]} break
814 lappend bm [format %x 0x$l]
816 set database($bm) $strq
819 proc write_database_header/char {f} {
823 proc format_database_entry/char {bm strq} {
825 set o "[lindex $bm 0]\n$strq\n"
826 foreach x [lrange $bm 1 end] { append o "$x\n" }
830 proc dbkey {ctx l r} {
833 for {set x $l} {$x <= $r} {incr x} {
834 lappend bm [format %x $wordmap($x)]
839 proc update_database/DEFINE {c0 c1 strq} {
840 global glyphsdone unk_l unk_contexts wordmap database
841 if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
843 set ncontexts $unk_contexts
845 foreach {l r contexts got} $glyphsdone {
846 if {$l==$c0} { set ncontexts $contexts; break }
848 if {![info exists ncontexts]} {
849 puts stderr "must start at letter LHS!"
854 foreach c $ncontexts {
855 set bm [dbkey $c $c0 $c1]
856 do_database_update $bm $strq
860 proc update_database/DELETE {l r ctxs} {
863 set bm [dbkey $ctx $l $r]
864 catch { unset database($bm) }
869 proc RETURN_RESULT {how what} {
871 place forget .d.csr.csr
872 pack forget .d.csr.csr.e
873 helptext {{{ Processing }}}
877 eval update_database/$how $what
881 #========== server for approving updates ==========
883 proc remote-serv-log {dict pirate caller file event} {
884 global remoteserv_logf
885 set t [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S %Z}]
886 set s [format "%s %-6s %-31s %-31s %s %s\n" \
887 $t $dict $pirate $caller [file tail $file] $event]
888 puts -nonewline $remoteserv_logf $s
891 proc remote-serv/list {} {
893 foreach file [glob -nocomplain -type f -directory $dropdir _update.*.rdy] {
899 puts_counted stdout d
904 proc remote-serv/take {yesno file dict} {
905 global dropdir dictdir rows reqkind database
907 debug "TAKE [list $yesno $file $dict]"
908 read_counted stdin pirate
909 read_counted stdin caller
910 read_counted stdin key
911 read_counted stdin val
913 must_gets_exactly stdin confirmed
915 if {![string compare pixmap $dict]} {
918 } elseif {[regexp {^(char)([1-9]\d*)$} $dict dummy reqkind rows]} {
919 debug "DICT CHAR rqk=$reqkind r=$rows."
925 read_database $dictdir/master-$dict.txt
926 set database($key) $val
932 remote-serv-log $dict $pirate $caller $file "$desc $reqkind $rows"
933 file delete -force $file
938 proc remote-serv/noop {} {
942 set remoteserv_banner {ypp-sc-tools pctb remote-server v1}
944 proc main/remoteserv {} {
945 global argv dropdir remoteserv_banner remoteserv_logf dictdir
946 manyset $argv dropdir dictdir
947 puts $remoteserv_banner
948 set remoteserv_logf [open $dropdir/_dict.log a]
949 fconfigure $remoteserv_logf -buffering line
952 if {[gets stdin l] < 0} break
957 #========== updates approver ==========
959 proc puts_server {s} {
964 proc must_gets_server {lv} {
971 proc must_gets_exactly_server {expected} {
973 if {[string compare $expected $got]} { error "$expected $got ?" }
976 proc regsub-data {exp subspec args} {
978 if {![eval regsub $args -- [list $exp $data $subspec data]]} {
979 error "$exp >$data< ?"
983 proc chop_counted {var} {
986 if {![regexp {^(0|[1-9]\d*)\n} $data dummy l]} { error "$data ?" }
987 regsub-data {^.*\n} {} -line
988 set val [string range $data 0 [expr {$l-1}]]
989 set data [string range $data $l end]
990 debug "CHOP_COUNTED $l $var"
994 proc approve_decompose_data {specdata} {
998 regsub-data {^ypp-sc-tools dictionary update v1\n} {}
999 uplevel 1 chop_counted pirate
1000 uplevel 1 chop_counted caller
1001 uplevel 1 chop_counted dict
1002 uplevel 1 chop_counted ctx
1003 uplevel 1 chop_counted def
1004 uplevel 1 chop_counted image
1005 uplevel 1 chop_counted key
1006 uplevel 1 chop_counted val
1008 return [uplevel 1 {list $dict $def $image}]
1011 proc approve_compare {fd1 fd2} {
1012 manyset $fd1 file data; set sv1 [approve_decompose_data $data]
1013 manyset $fd2 file data; set sv2 [approve_decompose_data $data]
1014 return [string compare $sv1 $sv2]
1017 proc approve_showentry {ix file specdata} {
1020 approve_decompose_data $specdata
1025 label $wb-inf.who -text $pirate
1027 entry $wb-inf.file -font fixed -relief flat
1028 $wb-inf.file insert end [file tail $file]
1029 $wb-inf.file configure -state readonly -width -1
1031 pack $wb-inf.who $wb-inf.file -side top
1034 label $wb-def.scope -text "$dict $ctx"
1035 label $wb-def.def -text $def
1036 pack $wb-def.scope $wb-def.def -side bottom
1038 set ppm [exec pnmscale 2 << $image]
1039 image create photo approve/$ix -data $ppm
1040 label $wb-image -image approve/$ix -bd 2 -relief sunken
1043 button $wb-act.rej -text Reject -command [list approve_reject $ix]
1046 grid $wb-def $wb-image $wb-act $wb-inf -padx 3
1047 grid configure $wb-image -ipadx 3 -ipady 3 -sticky w
1049 lappend approve_ixes $ix
1052 proc approve_approve_reject_one {ix yesno} {
1053 global approve_list server
1054 manyset [lindex $approve_list $ix] file tdata
1055 approve_decompose_data $tdata
1056 puts_server "take $yesno $file $dict"
1057 puts_counted $server pirate
1058 puts_counted $server caller
1059 puts_counted $server key
1060 puts_counted $server val
1061 puts_server confirmed
1063 must_gets_exactly_server done
1066 proc approve_check_server {} {
1070 must_gets_exactly_server ok
1073 proc approve_reject {ix} {
1074 approve_check_server
1075 approve_approve_reject_one $ix 0
1079 proc approve_these {} {
1081 approve_check_server
1082 foreach ix $approve_ixes { approve_approve_reject_one $ix 1 }
1086 proc approve_fetch_list {} {
1087 global server approve_list
1092 must_gets_server more
1093 switch -exact $more {
1096 default { error "$more ?" }
1098 must_gets_server file
1099 read_counted $server data
1100 lappend approve_list [list $file $data]
1103 if {![llength $approve_list]} { puts "Nothing (more) to approve."; exit 0 }
1105 set approve_list [lsort -command approve_compare $approve_list]
1109 proc main/approve {} {
1110 global argv server remoteserv_banner data approve_list approve_page
1111 global userhost directory dictdir debug
1113 if {[llength $argv] != 3} { error "wrong # args" }
1114 manyset $argv userhost directory dictdir
1115 set cmd [list tclsh $directory/dictionary-manager]
1116 if {$debug} { lappend cmd --debug-server }
1117 lappend cmd --remote-server-1 $directory $dictdir
1118 switch -glob $userhost {
1120 {* *} { set cmd $userhost }
1121 * { set cmd [append [list ssh $userhost] $cmd] }
1123 lappend cmd 2>@ stderr
1124 set server [open |$cmd r+]
1125 must_gets_exactly_server $remoteserv_banner
1127 button .left -text {<<} -command {approve_show_page -1}
1128 button .right -text {>>} -command {approve_show_page +1}
1130 label .title -text {}
1131 frame .app -bd 2 -relief groove
1132 button .ok -text "Approve These" -command approve_these
1133 pack .title .app -side top
1134 pack .left -side left
1135 pack .right -side right
1136 pack .ok -side bottom
1142 proc approve_show_page {delta} {
1143 global approve_page approve_ixes approve_list userhost directory dictdir
1145 eval destroy [winfo children .app]
1149 incr approve_page $delta
1151 set ll [llength $approve_list]
1152 set npages [expr {($ll+$per_page-1)/$per_page}]
1153 if {$approve_page >= $npages} { incr approve_page -1 }
1155 set page_start [expr {$approve_page*$per_page}]
1156 set page_end [expr {$page_start+$per_page-1}]
1158 for {set ix $page_start} {$ix < $ll && $ix <= $page_end} {incr ix} {
1159 set fd [lindex $approve_list $ix]
1160 eval approve_showentry $ix $fd
1163 .title configure -text \
1164 "$userhost\n$directory => $dictdir\nPage [expr {$approve_page+1}]/$npages"
1166 .left configure -state disabled
1167 .right configure -state disabled
1168 if {$approve_page>0} { .left configure -state normal }
1169 if {$approve_page<$npages-1} { .right configure -state normal }
1172 #========== main program ==========
1174 proc main/default {} {
1175 puts stderr "Do not run this program directly."
1178 proc done/default {} {
1184 fileevent stdin readable {}
1185 fconfigure stdin -blocking yes
1187 if {[gets stdin reqkind]<0} {
1188 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
1196 proc main/automatic {} {
1197 fconfigure stdin -blocking no
1198 fileevent stdin readable required
1200 proc done/automatic {} {
1201 exec sh -c {printf \\0 >&4}
1207 set mainkind default
1213 switch -exact -- $arg {
1214 {--quiet} { set quiet 1 }
1215 {--debug} { set debug 1 }
1216 {--debug-server} { proc debug {m} { puts stderr "DICT-MGR-SVR $m" }}
1218 {--approve-updates} { set mainkind approve; break }
1219 {--automatic-1} { set mainkind automatic; break }
1220 {--remote-server-1} { set mainkind remoteserv; break }
1221 {--automatic*} - {--remote-server}
1222 { error "incompatible versions - install problem" }
1223 default { error "huh $argv ?" }
1227 proc debug {m} { puts stderr "DICT-MGR $m" }
1229 set argv [lrange $argv $ai end]