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.
28 # ./dictionary-manager --approve-updates ijackson@login.chiark.greenend.org.uk /home/ijackson/things/ypp-sc-tools.pctb-dict-test/pctb /home/ftp/users/ijackson/pctb/test
29 # ./dictionary-manager --approve-updates ijackson@login.chiark.greenend.org.uk /home/ijackson/things/ypp-sc-tools.pctb-dict/pctb /home/ftp/users/ijackson/pctb
34 # run this without args
36 # one line which is a Tcl list for unk_{l,r} unk_contexts glyphsdone etc.
37 # the xpm in the format expected
38 # then expect child to exit 0, or write a single 0 byte to fd 4
39 # if it wrote a byte to fd 4, it can take another question
42 #---------- library routines ----------
44 proc manyset {list args} {
45 foreach val $list var $args {
51 proc must_gets {f lvar} {
53 if {[gets $f l] < 0} { error "huh?" }
56 proc must_gets_exactly {f expected} {
58 if {[string compare $expected $got]} { error "$expected $got ?" }
61 proc read_counted {f var} {
64 set val [read $f $count]
66 if {[string length $eol]} { error "$eol ?" }
67 debug "READ_COUNTED $count $var"
70 proc puts_counted {f dvar} {
72 set count [string length $d]
75 debug "PUTS_COUNTED $count $dvar"
79 global errorCode errorInfo
80 puts stderr "ERROR: $m\n[list $errorCode]\n$errorInfo\n";
84 #---------- display core ----------
93 proc init_widgets {} {
95 global csrh gotsh ctxh
97 if {[winfo exists .d]} return
99 frame .privacy -bd 2 -relief groove
100 pack .privacy -side top -padx 2 -pady 2 -fill x
104 frame .d -bd 2 -relief groove -pady 2 -padx 2
106 image create bitmap image/main
107 label .d.mi -image image/main -bd 0
109 frame .d.csr -bg black -height $csrh
110 frame .d.got -bg black -height $gotsh
111 frame .d.ctx -bg black
113 image create bitmap image/cursor -data \
114 {#define csr_width 11
115 #define csr_height 11
116 static unsigned char csr_bits[] = {
117 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
118 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
122 label .d.csr.csr.l -image image/cursor -compound left
123 entry .d.csr.csr.e -bd 0
124 pack .d.csr.csr.l -side left
126 frame .d.selctx -bd 2 -relief groove
127 frame .d.mi.csr_0 -bg white -width 1
128 frame .d.mi.csr_1 -bg white -width 1
132 button .d.pe.ok -text OK
133 pack .d.pe.grid .d.pe.ok -side left
135 pack .d.mi .d.ctx -side top
136 pack .d -fill x -padx 2 -pady 2
138 frame .help -bd 2 -relief groove
139 pack .help -pady 2 -padx 2
142 proc resize_widgets_core {} {
143 global mulcols mulrows csrh gotsh ctxh
144 global unk_l unk_contexts
146 foreach w {.d.csr .d.got .d.ctx} {
147 $w configure -width $mulcols
150 eval destroy [winfo children .d.ctx]
157 if {![string compare $t $last_ht]} return
158 eval destroy [grid slaves .help]
159 set y 0; foreach l $t {
160 set x 0; foreach c $l {
161 set w .help.at${x}x${y}
163 grid $w -row $y -column $x -padx 5 -sticky w
171 proc bind_key {k proc} {
173 bind .d <Key-$k> $proc
174 set keybindings($k) [expr {!![string length $proc]}]
175 .d configure -takefocus 1
177 proc unbind_all_keys {} {
179 foreach k [array names keybindings] { bind_key $k {} }
180 .d configure -takefocus 0
183 #---------- database read and write common wrapper ----------
186 if {[gets $f l] < 0} { error "unexpected db eof" }
190 proc read_database {fn} {
191 global reqkind database database_fn
192 upvar #0 database_magic/$reqkind magic
193 catch { unset database }
196 if {![file exists $database_fn]} return
197 set f [open $database_fn r]
198 if {[string compare [db_getsl $f] $magic]} { error "$l $reqkind ?" }
200 read_database_header/$reqkind $f
204 if {![string length $l1]} continue
205 if {[regexp {^\#} $l1]} continue
206 if {![string compare . $l1]} break
208 read_database_entry/$reqkind $f $l1
213 proc write_database {} {
214 global reqkind database_fn database
215 upvar #0 database_magic/$reqkind magic
217 set f [open $database_fn.new w]
220 write_database_header/$reqkind $f
223 foreach bm [array names database] {
224 lappend ol [format_database_entry/$reqkind $bm $database($bm)]
226 foreach o [lsort $ol] {
231 file rename -force $database_fn.new $database_fn
234 proc select_database {dbname_spec} {
236 set dbname $dbname_spec
237 read_database "./#local-$dbname#.txt"
240 proc do_database_update {im def} {
242 maybe_upload_entry $im $def
243 set database($im) $def
247 proc required/char {} {
248 global mulrows glyphsdone unk_l unk_r unk_contexts rows new_context
253 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
254 set glyphsdone [lrange $l 3 end]
259 catch { unset all_contexts }
263 .d.mi.csr_$w configure -height $mulrows
266 foreach {min max context contexts got} $glyphsdone {
267 show_context maxh $min $context
268 foreach ctx $contexts { set all_contexts($ctx) 1 }
270 foreach ctx $unk_contexts { set all_contexts($ctx) 1 }
272 eval destroy [winfo children .d.selctx]
273 label .d.selctx.title -text \
274 {Select match context for altering dictionary:}
275 pack .d.selctx.title -side left
276 set new_context [lindex $unk_contexts 0]
278 set ci 0; foreach ctx [lsort [array names all_contexts]] {
279 set all_contexts($ctx) $ci
280 set selw .d.selctx.c$ci
282 radiobutton $selw -variable new_context -value $ctx -text $seltxt
283 pack $selw -side left
286 $selw configure -text "$seltxt."
287 label .d.selctx.warning -text {See README.charset.}
288 pack .d.selctx.warning -side left
290 show_context maxh $unk_l $unk_contexts
291 .d.ctx configure -height $maxh
293 pack .d.selctx .d.csr -side top -before .d.mi
294 pack .d.got .d.ctx -side top -after .d.mi
295 pack configure .d.selctx -fill x
298 select_database char$rows
303 proc approve_showentry_xinfo/char {w def} {
304 set unic [string2unicodenames $def]
308 #========== PIXMAPS ==========
310 #---------- pixmap database read and write ----------
312 set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1}
314 proc read_database_header/pixmap {f} { }
315 proc read_database_entry/pixmap {f def} {
320 set p3 [db_getsl $f]; append im $p3 "\n"
321 if {[string compare $p3 P3]} { error "$p3 ?" }
323 set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h
324 set depth [db_getsl $f]; append im $depth "\n"
326 for {set y 0} {$y < $h} {incr y} {
327 set line [db_getsl $f]; append im $line "\n"
329 set database($im) $def
331 proc write_database_header/pixmap {f} { puts $f "" }
332 proc format_database_entry/pixmap {im def} {
336 #---------- pixmap display and input handling ----------
338 proc foreach_pixmap_col {var body} {
341 for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
346 proc pixmap_select {ncol} {
348 debug "PIX SELECT $ncol [llength $alloptions]"
349 foreach_pixmap_col col {
350 if {$col==$ncol} continue
351 .d.pe.grid.l$col selection clear 0 end
355 proc pixmap_maybe_ok {} {
356 global alloptions pixmap_selcol pixmap_selrow
358 foreach_pixmap_col col {
359 set cs [.d.pe.grid.l$col curselection]
360 set lcs [llength $cs]
363 set pixmap_selcol $col
364 set pixmap_selrow [lindex $cs 0]
367 debug "MAYBE_OK YES col=$pixmap_selcol row=$pixmap_selrow."
368 .d.pe.ok configure -state normal -command pixmap_ok
370 .d.pe.ok configure -state disabled -command {}
374 global database ppm pixmap_selcol pixmap_selrow mainkind alloptions
377 foreach_pixmap_col col {
378 .d.pe.grid.l$col configure -state disabled
380 .d.pe.ok configure -state disabled
382 manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \
384 manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \
386 set result "$colname - $rowname"
387 debug "UPDATE PIXMAP AS >$result<"
389 do_database_update $ppm $result
394 proc required/pixmap {} {
395 global unk_what ppm mulcols alloptions
396 must_gets stdin unk_what
397 debug "GOT pixmap $unk_what"
401 if {![string length $ppml]} break
402 append ppm $ppml "\n"
404 set data [exec pnmscale 2 << $ppm]
405 image create photo image/main -data $data
407 set alloptions [exec ./database-info-fetch $unk_what]
409 select_database pixmap
411 set mulcols [image width image/main]
412 set mulrows [image height image/main]
414 place forget .d.mi.csr_0
415 place forget .d.mi.csr_1
417 pack forget .d.selctx .d.csr .d.got
418 pack .d.pe -side top -before .d.mi -pady 2
419 .d configure -takefocus 0
422 eval destroy [winfo children .d.pe.grid]
423 set col 0; foreach {colname coldesc rows} $alloptions {
424 debug "INIT $col $colname \"$coldesc\""
425 label .d.pe.grid.t$col -text $colname
426 listbox .d.pe.grid.l$col
427 foreach {rowname rowdesc} $rows {
428 debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
429 .d.pe.grid.l$col insert end $rowdesc
431 bind .d.pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
432 grid .d.pe.grid.t$col -column $col -row 0
433 grid .d.pe.grid.l$col -column $col -row 1
439 {{Indicate the meaning of this image, and click OK.}}
443 proc approve_showentry_xinfo/pixmap {w def} {
444 label $w -image image/empty
447 #========== UPLOADS TO DICTIONARY SERVER ==========
449 proc upload_init {} {
450 global privacy_setting
452 set privacy_setting [upload_status]
454 label .privacy.warn -text " Privacy "
455 if {$privacy_setting} {
456 .privacy.warn configure -background yellow -foreground black
458 label .privacy.overall -text " Upload new dictionary entry:"
459 label .privacy.reference -text " See README.privacy."
461 pack .privacy.warn .privacy.overall -side left
463 foreach {setting string} {
466 2 {Yes, quoting my pirate name.}
468 radiobutton .privacy.o$setting -text $string \
469 -value $setting -variable privacy_setting
470 pack .privacy.o$setting -side left
471 if {$setting > $privacy_setting} {
472 .privacy.o$setting configure -state disabled
475 pack .privacy.reference -side left
477 if {!$privacy_setting} {
478 foreach w [winfo children .privacy] {
479 $w configure -state disabled
482 if {$privacy_setting} {
484 ::http::config -urlencoding utf-8
488 proc upload_status {} {
489 # returns 0, 1, 2 for none, anon, with pirate name
492 if {![info exists env(YPPSC_PCTB_DICT_SUBMIT)]} { debug a; return 0 }
493 if {![string compare 0 $env(YPPSC_PCTB_DICT_SUBMIT)]} { debug b; return 0 }
495 if {![info exists env(YPPSC_PIRATE)]} { return 1 }
496 if {![info exists env(YPPSC_OCEAN)]} { return 1 }
497 if {![string length $env(YPPSC_PIRATE)]} { return 1 }
498 if {![string length $env(YPPSC_OCEAN)]} { return 1 }
503 proc maybe_upload_entry {im def} {
504 global reqkind privacy_setting env dbname quiet
506 debug "DB-UPDATE PRIVACY $privacy_setting"
507 if {!$privacy_setting} return
509 debug "DB-UPDATE UPLOADING"
512 lappend pl dict $dbname
514 if {$privacy_setting>=2} {
515 set pirate [string totitle $env(YPPSC_PIRATE)]
516 set ocean [string totitle $env(YPPSC_OCEAN)]
517 debug "DB-UPDATE NON-ANON $ocean $pirate"
522 lappend pl entry [format_database_entry/$reqkind $im $def]
524 set url $env(YPPSC_PCTB_DICT_SUBMIT)
525 append url dictionary-update-receiver
527 set query [eval ::http::formatQuery $pl]
528 regsub -all {%0d} $query {} query
529 debug "DB-UPDATE QUERY $query"
531 if {[regexp {^\.?/} $url]} {
532 set cmd [list $url $query]
533 debug "SUBMIT CMD [string range $cmd 0 200]..."
534 set body [eval exec $cmd 2>@ stderr]
535 regsub {^Content-Type: text/plain.*\n\n} $body {} body
539 set req [::http::geturl $url -query $query]
542 "\nWARNING: Cannot do dictionary upload: $emsg\n"
546 debug "DB-UPDATE DONE $req $s(status) [array names s]"
547 set ncode [::http::ncode $req]
549 if {!(![string compare ok $s(status)] &&
550 ![string compare 200 $ncode])} {
551 set m "\nWARNING: Dictionary upload failed:"
552 foreach v {status http error posterror} {
553 if {[info exists s($v)]} { append m "\n $v: $s($v)" }
562 if {![string match {OK *} $body]} {
563 set m "\nWARNING: Dictionary upload went wrong:\n"
564 append m "\n " [join [split $body "\n"] "\n "]
571 "Uploaded $dbname dictionary entry `$def': $body"
575 #========== CHARACTER SET ==========
577 #---------- xpm input processor ----------
579 proc char_read_xpm {f} {
580 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
581 global cols rows wordmap
587 if {![regexp {^"(.*)",$} $l dummy l]} {
589 if {[regexp {^\}\;$} $l]} break
593 manyset $l cols rows colours cpp
594 if {$colours!=2 || $cpp!=1} { error "$l ?" }
596 set chop_l [expr {$unk_l - 80}]
597 set chop_r [expr {$cols - $unk_l - 100}]
598 if {$chop_l<0} { set chop_l 0 }
600 set unk_l [expr {$unk_l - $chop_l}]
601 set unk_r [expr {$unk_r - $chop_l}]
603 foreach {min max context contexts got} $glyphsdone {
605 [expr {$min-$chop_l}] \
606 [expr {$max-$chop_l}] \
607 $context $contexts $got
612 set cols [expr {$cols - $chop_l - $chop_r}]
613 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
616 set mulcols [expr {$cols*$mul+$inter}]
617 set mulrows [expr {$rows*$mul+$inter}]
618 append o "\"$mulcols $mulrows 9 1\",\n"
619 for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
620 } elseif {$y==-2} { # first pixel
631 } elseif {$y==-1} { # 2nd pixel but we've already printed ours
633 set ybit [expr {1<<$y}]
638 set l [string range $l $chop_l end-$chop_r]
640 set l [string range $l $chop_l end]
641 append l [string repeat " " [expr -$chop_r]]
643 foreach c [split $l ""] {
645 if {$x >= $unk_l && $x <= $unk_r} {
649 foreach {min max context contexts got} $glyphsdone {
651 if {$x >= $min && $x <= $max} {
652 set how [lindex {a b} $ab]
661 set p [string toupper $how]
662 incr wordmap($x) $ybit
664 default { error "$c ?" }
666 append ol "[string repeat $p [expr {$mul-$inter}]][
667 string repeat + $inter]"
668 append olh [string repeat + $mul]
674 set olhn [string repeat $olh $inter]
675 if {!$y} { append o $olhn }
676 append o [string repeat $ol [expr {$mul-1}]]
681 set data [exec xpmtoppm << $o]
682 image create photo image/main -data $data
685 #---------- character set editor display ----------
687 proc show_context {maxhv x ctxs} {
691 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
692 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
693 place $w -x [expr {($x-1)*$mul}] -y 0
694 set wh [winfo reqheight $w]
695 if {$wh > $maxh} { set maxh $wh }
698 proc draw_glyphsdone {} {
699 global glyphsdone mul inter
700 eval destroy [winfo children .d.got]
701 foreach {min max context contexts got} $glyphsdone {
702 frame .d.got.m$min -bd 0 -background \#888
703 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
704 pack .d.got.m$min.l -padx 1 -pady 1
705 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
709 proc startup_cursor {} {
710 global cur_already cur_mode cur_0 cur_1 last_ht
711 global glyphsdone unk_l unk_r
713 set cur_already [expr {[llength $glyphsdone]/5-1}]
714 set cur_mode 1 ;# one of: 0 1 already text
717 set cur_1 [expr {$unk_r+1}]
722 #---------- character set runtime display and keystroke handling ----------
724 proc char_exactly_selctxts {contexts} {
726 foreach ctx [array names all_contexts] {
727 set ci $all_contexts($ctx)
728 set selw .d.selctx.c$ci
729 if {[lsearch -exact $contexts $ctx]>=0} {
734 $selw configure -state $state
738 proc recursor/0 {} { recursor//01 0 }
739 proc recursor/1 {} { recursor//01 1 }
740 proc recursor//01 {z1} {
741 global mul rhsmost_max cols glyphsdone cur_0 cur_1
744 .d.csr.csr.l configure -text "adjust [char_get_definition_context_actual]"
745 place .d.csr.csr -x [expr {$cur*$mul - 7}]
747 set okctxts [char_get_definition_contexts]
748 char_exactly_selctxts $okctxts
750 foreach ctx [lsort [array names all_contexts]] {
751 set key [string range $ctx 0 0]
752 if {[lsearch -exact $okctxts $ctx] >= 0} {
753 bind_key [string tolower $key] "
754 [list set new_context $ctx]
758 bind_key [string tolower $key] {}
760 lappend context_help $key
762 set context_help [list [join $context_help " "] \
763 {Set match context for new glyph.}]
765 bind_key space { othercursor }
766 bind_leftright_q cur_$z1 0 [expr {$cols-1}]
767 if {[llength $glyphsdone]} {
768 bind_key BackSpace { set cur_mode already; recursor }
770 bind_key BackSpace {}
773 char_start_define_text
776 {{<- ->} {move cursor, adjusting area to define}} \
777 {Space {switch to moving other cursor}} \
778 {Return {confirm location, enter letter(s)}} \
779 {Backspace {switch to correcting earlier ocr}} \
780 {Q {quit and abandon OCR run}} \
784 proc othercursor {} {
786 set cur_mode [expr {!$cur_mode}]
790 proc char_start_define_text {} {
791 global cur_0 cur_1 cur_mode
792 if {$cur_0 == $cur_1} return
793 set cdgdca [char_get_definition_context_actual]
794 if {![string length $cdgdca]} return
795 .d.csr.csr.e delete 0 end
797 .d.csr.csr.l configure -text "define $cdgdca:"
801 proc recursor/text {} {
805 {Return {confirm entry of new glyph}}
806 {Escape {abandon entry}}
809 pack .d.csr.csr.e -side left
811 bind .d.csr.csr.e <Key-Return> {
812 set strq [.d.csr.csr.e get]
813 if {[string length $strq]} {
814 RETURN_RESULT DEFINE [list $strq]
817 bind .d.csr.csr.e <Key-Escape> {
819 pack forget .d.csr.csr.e
826 proc recursor/already {} {
828 global cur_already mul
829 global glyphsdone cur_already mul
831 char_exactly_selctxts [lindex $glyphsdone [expr {$cur_already*5+2}]]
833 .d.csr.csr.l configure -text {correct}
834 set rmax [lindex $glyphsdone [expr {$cur_already*5}]]
835 place .d.csr.csr -x [expr {$rmax*$mul-3}]
837 bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/5-1}]
838 bind_key space { bind_key Delete {}; set cur_mode 1; recursor }
840 RETURN_RESULT DELETE [lrange $glyphsdone \
841 [expr $cur_already*5] \
842 [expr $cur_already*5+2]]
845 {{<- ->} {move cursor, selecting glyph to correct}}
846 {Del {clear this glyph from the recognition database}}
847 {Space {switch to selecting area to define as new glyph}}
848 {Q {quit and abandon OCR run}}
852 proc bind_leftright_q {var min max} {
853 bind_key Left [list leftright $var $min $max -1]
854 bind_key Right [list leftright $var $min $max +1]
856 puts stderr "\nCharacter resolver quitting as you requested."
860 proc leftright {var min max inc} {
864 if {$vnew < $min || $vnew > $max} return
870 global csrh cur_mode cur_0 cur_1 mul
872 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
877 #---------- character database read and write ----------
882 # <context> <ncharacters> <hex>...
886 # $database($context 0x<bits> 0x<bits>...) = $hex
888 set database_magic/char {# ypp-sc-tools pctb font v1}
890 proc read_database_header/char {f} {
892 if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
894 proc read_database_entry/char {f context} {
897 set strq [db_getsl $f]
900 if {![string length $l]} break
901 lappend bm [format %x 0x$l]
903 set database($bm) $strq
906 proc write_database_header/char {f} {
910 proc format_database_entry/char {bm strq} {
912 set o "[lindex $bm 0]\n$strq\n"
913 foreach x [lrange $bm 1 end] { append o "$x\n" }
917 proc dbkey {ctx l r} {
920 for {set x $l} {$x <= $r} {incr x} {
921 lappend bm [format %x $wordmap($x)]
926 proc char_get_definition_cursors {} {
928 if {$cur_0 <= $cur_1} {
929 set cl $cur_0; set cr $cur_1
931 set cl $cur_1; set cr $cur_0
934 debug "CGD CURSORS $cl $cr"
935 return [list $cl $cr]
938 proc char_get_definition_contexts {} {
939 global glyphsdone unk_l unk_contexts wordmap database
941 manyset [char_get_definition_cursors] c0 c1
944 set ncontexts $unk_contexts
946 foreach {l r context contexts got} $glyphsdone {
947 if {$l==$c0} { set ncontexts $contexts; break }
949 if {![info exists ncontexts]} {
953 debug "CGD CONTEXTS $ncontexts"
957 proc char_get_definition_context_actual {} {
959 set ncontexts [char_get_definition_contexts]
960 if {[llength $ncontexts]==1} {
961 set c [lindex $ncontexts 0]
962 } elseif {[lsearch -exact $ncontexts $new_context]>=0} {
967 debug "CDG CONTEXT ACTUAL $c FROM NEW $new_context ALLOW $ncontexts"
971 proc update_database/DEFINE {strq} {
972 manyset [char_get_definition_cursors] c0 c1
973 set c [char_get_definition_context_actual]
974 if {![string length $c]} {
975 error "Selected context is not one of the many possibilities."
978 set bm [dbkey $c $c0 $c1]
979 do_database_update $bm $strq
982 proc update_database/DELETE {l r ctxs} {
984 if {[llength $ctxs]!=1} { error "$ctxs ?" }
986 set bm [dbkey $ctx $l $r]
987 catch { unset database($bm) }
992 proc RETURN_RESULT {how what} {
995 place forget .d.csr.csr
996 pack forget .d.csr.csr.e
999 eval update_database/$how $what
1001 return_result_finish
1004 #========== server for approving updates ==========
1006 proc remote-serv-log {dict pirate caller file event} {
1007 global remoteserv_logf
1008 set t [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S %Z}]
1009 set s [format "%s %-6s %-31s %-31s %s %s\n" \
1010 $t $dict $pirate $caller [file tail $file] $event]
1011 puts -nonewline $remoteserv_logf $s
1014 proc remote-serv/list {} {
1016 foreach file [glob -nocomplain -type f -directory $dropdir _update.*.rdy] {
1022 puts_counted stdout d
1027 proc remote-serv/take {yesno file dict} {
1028 global dropdir dictdir rows reqkind database
1030 debug "TAKE [list $yesno $file $dict]"
1031 read_counted stdin pirate
1032 read_counted stdin caller
1033 read_counted stdin key
1034 read_counted stdin val
1036 must_gets_exactly stdin confirmed
1038 manyset [dict2_reqkind_rows reqkind rows]
1041 read_database $dictdir/master-$dict.txt
1042 set database($key) $val
1048 remote-serv-log $dict $pirate $caller $file "$desc $reqkind $rows"
1049 file delete -force $file
1054 proc remote-serv/noop {} {
1058 set remoteserv_banner {ypp-sc-tools pctb remote-server v1}
1060 proc main/remoteserv {} {
1061 global argv dropdir remoteserv_banner remoteserv_logf dictdir
1062 manyset $argv dropdir dictdir
1063 puts $remoteserv_banner
1064 set remoteserv_logf [open $dropdir/_dict.log a]
1065 fconfigure $remoteserv_logf -buffering line
1068 if {[gets stdin l] < 0} break
1073 #========== updates approver ==========
1075 proc puts_server {s} {
1080 proc must_gets_server {lv} {
1087 proc must_gets_exactly_server {expected} {
1088 must_gets_server got
1089 if {[string compare $expected $got]} { error "$expected $got ?" }
1092 proc regsub-data {exp subspec args} {
1094 if {![eval regsub $args -- [list $exp $data $subspec data]]} {
1095 error "$exp >$data< ?"
1099 proc dict2_reqkind_rows {dict} {
1100 if {![string compare pixmap $dict]} {
1103 } elseif {[regexp {^(char)([1-9]\d*)$} $dict dummy reqkind rows]} {
1104 debug "DICT CHAR rqk=$reqkind r=$rows."
1105 return [list $reqkind rows]
1111 proc chop_counted {var} {
1114 if {![regexp {^(0|[1-9]\d*)\n} $data dummy l]} { error "$data ?" }
1115 regsub-data {^.*\n} {} -line
1116 set val [string range $data 0 [expr {$l-1}]]
1117 set data [string range $data $l end]
1118 debug "CHOP_COUNTED $l $var"
1119 regsub-data {^\n} {}
1122 proc approve_decompose_data {specdata} {
1126 regsub-data {^ypp-sc-tools dictionary update v1\n} {}
1127 uplevel 1 chop_counted pirate
1128 uplevel 1 chop_counted caller
1129 uplevel 1 chop_counted dict
1130 uplevel 1 chop_counted ctx
1131 uplevel 1 chop_counted def
1132 uplevel 1 chop_counted image
1133 uplevel 1 chop_counted key
1134 uplevel 1 chop_counted val
1136 return [uplevel 1 {list $dict $def $image}]
1139 proc approve_compare {fd1 fd2} {
1140 manyset $fd1 file data; set sv1 [approve_decompose_data $data]
1141 manyset $fd2 file data; set sv2 [approve_decompose_data $data]
1142 return [string compare $sv1 $sv2]
1145 proc string2unicodenames {str} {
1146 return [exec perl -e {
1147 use Unicode::CharName qw(uname);
1149 foreach $_ (split //,$ARGV[0]) {
1150 print uname(ord),"\n" or die $!
1155 proc approve_showentry {ix file specdata} {
1156 global approve_ixes reqkind
1158 approve_decompose_data $specdata
1163 label $wb-inf.who -text $pirate
1165 entry $wb-inf.file -font fixed -relief flat
1166 $wb-inf.file insert end [file tail $file]
1167 $wb-inf.file configure -state readonly -width -1
1169 pack $wb-inf.who $wb-inf.file -side top
1172 label $wb-def.scope -text "$dict $ctx"
1173 label $wb-def.def -text $def
1174 pack $wb-def.scope $wb-def.def -side bottom
1176 set ppm [exec pnmscale 2 << $image]
1177 image create photo approve/$ix -data $ppm
1178 label $wb-image -image approve/$ix -bd 2 -relief sunken
1180 manyset [dict2_reqkind_rows $dict] reqkind
1181 approve_showentry_xinfo/$reqkind $wb-xinfo $def
1184 label $wb-div -bd 1 -relief sunken -image image/empty
1185 grid configure $wb-div -columnspan 5 -sticky ew -padx 5
1189 button $wb-act.rej -text Reject -command [list approve_reject $ix]
1192 grid $wb-def $wb-image $wb-xinfo $wb-act $wb-inf -padx 3
1193 grid configure $wb-image -ipadx 3 -ipady 3 -sticky w
1195 lappend approve_ixes $ix
1198 proc approve_approve_reject_one {ix yesno} {
1199 global approve_list server
1200 manyset [lindex $approve_list $ix] file tdata
1201 approve_decompose_data $tdata
1202 puts_server "take $yesno $file $dict"
1203 puts_counted $server pirate
1204 puts_counted $server caller
1205 puts_counted $server key
1206 puts_counted $server val
1207 puts_server confirmed
1209 must_gets_exactly_server done
1212 proc approve_check_server {} {
1216 must_gets_exactly_server ok
1219 proc approve_reject {ix} {
1220 approve_check_server
1221 approve_approve_reject_one $ix 0
1225 proc approve_these {} {
1227 approve_check_server
1228 foreach ix $approve_ixes { approve_approve_reject_one $ix 1 }
1232 proc approve_fetch_list {} {
1233 global server approve_list
1238 must_gets_server more
1239 switch -exact $more {
1242 default { error "$more ?" }
1244 must_gets_server file
1245 read_counted $server data
1246 lappend approve_list [list $file $data]
1249 if {![llength $approve_list]} { puts "Nothing (more) to approve."; exit 0 }
1251 set approve_list [lsort -command approve_compare $approve_list]
1255 proc main/approve {} {
1256 global argv server remoteserv_banner data approve_list approve_page
1257 global userhost directory dictdir debug
1259 if {[llength $argv] != 3} { error "wrong # args" }
1260 manyset $argv userhost directory dictdir
1261 debug "APPROVER FOR $userhost $directory $dictdir"
1263 set cmd [list tclsh $directory/dictionary-manager]
1264 if {$debug} { lappend cmd --debug-server }
1265 lappend cmd --remote-server-1 $directory $dictdir
1266 switch -glob $userhost {
1268 {* *} { set cmd $userhost }
1269 * { set cmd [concat [list ssh $userhost] $cmd] }
1271 debug "APPROVER RUNS $cmd"
1273 lappend cmd 2>@ stderr
1274 set server [open |$cmd r+]
1275 must_gets_exactly_server $remoteserv_banner
1277 button .left -text {<<} -command {approve_show_page -1}
1278 button .right -text {>>} -command {approve_show_page +1}
1280 label .title -text {}
1281 frame .app -bd 2 -relief groove
1282 button .ok -text "Approve These" -command approve_these
1283 pack .title .app -side top
1284 pack .left -side left
1285 pack .right -side right
1286 pack .ok -side bottom
1288 image create bitmap image/empty
1294 proc approve_show_page {delta} {
1295 global approve_page approve_ixes approve_list userhost directory dictdir
1297 eval destroy [winfo children .app]
1301 incr approve_page $delta
1303 set ll [llength $approve_list]
1304 set npages [expr {($ll+$per_page-1)/$per_page}]
1305 if {$approve_page >= $npages} { incr approve_page -1 }
1307 set page_start [expr {$approve_page*$per_page}]
1308 set page_end [expr {$page_start+$per_page-1}]
1310 for {set ix $page_start} {$ix < $ll && $ix <= $page_end} {incr ix} {
1311 set fd [lindex $approve_list $ix]
1312 eval approve_showentry $ix $fd
1315 .title configure -text \
1316 "$userhost\n$directory => $dictdir\nPage [expr {$approve_page+1}]/$npages"
1318 .left configure -state disabled
1319 .right configure -state disabled
1320 if {$approve_page>0} { .left configure -state normal }
1321 if {$approve_page<$npages-1} { .right configure -state normal }
1324 #========== main program ==========
1326 proc return_result_start {} {
1327 helptext {{{ Processing }}}
1331 proc return_result_finish {} {
1336 proc main/default {} {
1337 puts stderr "Do not run this program directly."
1340 proc done/default {} {
1346 fileevent stdin readable {}
1347 fconfigure stdin -blocking yes
1349 if {[gets stdin reqkind]<0} {
1350 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
1358 proc main/automatic {} {
1359 fconfigure stdin -blocking no
1360 fileevent stdin readable required
1362 proc done/automatic {} {
1363 exec sh -c {printf \\0 >&4}
1369 set mainkind default
1375 switch -exact -- $arg {
1376 {--quiet} { set quiet 1 }
1377 {--debug} { set debug 1 }
1378 {--debug-server} { proc debug {m} { puts stderr "DICT-MGR-SVR $m" }}
1380 {--approve-updates} { set mainkind approve; break }
1381 {--automatic-1} { set mainkind automatic; break }
1382 {--remote-server-1} { set mainkind remoteserv; break }
1383 {--automatic*} - {--remote-server}
1384 { error "incompatible versions - install problem" }
1385 default { error "huh $argv ?" }
1389 proc debug {m} { puts stderr "DICT-MGR $m" }
1391 set argv [lrange $argv $ai end]