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/pctb /home/ftp/users/ijackson/pctb
30 # ./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
32 # ./dictionary-manager --approve-updates '' . .
37 # run this without args
39 # one line which is a Tcl list for unk_{l,r} unk_contexts glyphsdone etc.
40 # the xpm in the format expected
41 # then expect child to exit 0, or write a single 0 byte to fd 4
42 # if it wrote a byte to fd 4, it can take another question
45 #---------- library routines ----------
47 proc manyset {list args} {
48 foreach val $list var $args {
54 proc must_gets {f lvar} {
56 if {[gets $f l] < 0} { error "huh?" }
59 proc must_gets_imagel {f lvar} {
63 if {$debug_rect} { debug "<< $l" }
66 proc must_gets_exactly {f expected} {
68 if {[string compare $expected $got]} { error "$expected $got ?" }
71 proc read_counted {f var} {
74 set val [read $f $count]
76 if {[string length $eol]} { error "$eol ?" }
77 debug "READ_COUNTED $count $var"
80 proc puts_counted {f dvar} {
82 set count [string length $d]
85 debug "PUTS_COUNTED $count $dvar"
89 global errorCode errorInfo
90 puts stderr "ERROR: $m\n[list $errorCode]\n$errorInfo\n";
94 #---------- display core ----------
103 proc init_widgets {} {
105 global csrh gotsh ctxh
107 if {[winfo exists .d]} return
109 frame .privacy -bd 2 -relief groove
110 pack .privacy -side top -padx 2 -pady 2 -fill x
114 frame .d -pady 2 -padx 2 -bg black -bd 2 -relief sunken
116 image create bitmap image/main
117 label .d.mi -image image/main -bd 0
119 frame .d.csr -bg black -height $csrh
120 frame .d.got -bg black -height $gotsh
121 frame .d.ctx -bg black
123 image create bitmap image/cursor -foreground white -background black \
125 {#define csr_width 11
126 #define csr_height 11
127 static unsigned char csr_bits[] = {
128 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
129 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
133 label .d.csr.csr.l -image image/cursor -compound left -fg white -bg black
134 entry .d.csr.csr.e -bd 0
135 pack .d.csr.csr.l -side left
137 frame .selctx -bd 2 -relief groove
138 frame .d.mi.csr_0 -bg white -width 1
139 frame .d.mi.csr_1 -bg white -width 1
143 button .pe.ok -text OK
144 pack .pe.grid .pe.ok -side left
146 pack .d.mi .d.ctx -side top -anchor w
147 pack .d -fill x -padx 2 -pady 2
149 frame .help -bd 2 -relief groove
150 pack .help -pady 2 -padx 2
153 proc resize_widgets_core {} {
154 global mulcols mulrows csrh gotsh ctxh
155 global unk_l unk_contexts
157 foreach w {.d.csr .d.got .d.ctx} {
158 $w configure -width $mulcols
160 .d.csr configure -width [expr {$mulcols+150}]
162 eval destroy [winfo children .d.ctx]
169 if {![string compare $t $last_ht]} return
170 eval destroy [grid slaves .help]
171 set y 0; foreach l $t {
172 set x 0; foreach c $l {
173 set w .help.at${x}x${y}
175 grid $w -row $y -column $x -padx 5 -sticky w
183 proc bind_key {k proc} {
185 bind .d <Key-$k> $proc
186 set keybindings($k) [expr {!![string length $proc]}]
187 .d configure -takefocus 1
189 proc unbind_all_keys {} {
191 foreach k [array names keybindings] { bind_key $k {} }
192 .d configure -takefocus 0
195 #---------- database read and write common wrapper ----------
198 if {[gets $f l] < 0} { error "unexpected db eof" }
202 proc read_database {fn} {
203 global reqkind database database_fn
204 upvar #0 database_magic/$reqkind magic
205 catch { unset database }
208 if {![file exists $database_fn]} return
209 set f [open $database_fn r]
210 if {[string compare [db_getsl $f] $magic]} { error "$magic $reqkind ?" }
212 read_database_header/$reqkind $f
216 if {![string length $l1]} continue
217 if {[regexp {^\#} $l1]} continue
218 if {![string compare . $l1]} break
220 read_database_entry/$reqkind $f $l1
225 proc write_database {} {
226 global reqkind database_fn database
227 upvar #0 database_magic/$reqkind magic
229 set f [open $database_fn.tmp w]
232 write_database_header/$reqkind $f
235 foreach bm [array names database] {
236 lappend ol [format_database_entry/$reqkind $bm $database($bm)]
238 foreach o [lsort $ol] {
243 file rename -force $database_fn.tmp $database_fn
246 proc select_database {dbname_spec} {
248 set dbname $dbname_spec
249 read_database "./#local-$dbname#.txt"
252 proc do_database_update {im def} {
254 maybe_upload_entry $im $def
255 set database($im) $def
259 proc required/char {} {
260 global mulrows glyphsdone unk_l unk_r unk_contexts rows new_context
261 global all_contexts debug_rect
266 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
267 set glyphsdone [lrange $l 3 end]
271 catch { unset all_contexts }
275 .d.mi.csr_$w configure -height $mulrows
278 foreach {min max context contexts got} $glyphsdone {
279 show_context maxh $min $context
280 foreach ctx $contexts { set all_contexts($ctx) 1 }
282 foreach ctx $unk_contexts { set all_contexts($ctx) 1 }
284 eval destroy [winfo children .selctx]
285 label .selctx.title -text \
286 {Select match context for altering dictionary:}
287 pack .selctx.title -side left
290 set ci 0; foreach ctx [lsort [array names all_contexts]] {
291 set all_contexts($ctx) $ci
292 set selw .selctx.c$ci
294 radiobutton $selw -variable new_context -value $ctx -text $seltxt
295 pack $selw -side left
298 $selw configure -text "$seltxt."
299 label .selctx.warning -text {See README.charset.}
300 pack .selctx.warning -side left
302 show_context maxh $unk_l $unk_contexts
303 .d.ctx configure -height $maxh
305 pack .selctx -before .d -padx 2 -fill x
306 pack .d.csr -side top -before .d.mi -anchor w
307 pack .d.got .d.ctx -side top -after .d.mi -anchor w
308 pack configure .selctx -fill x
311 select_database char$rows
316 proc approve_showentry_xinfo/char {w def} {
317 set unic [string2unicodenames $def]
321 #========== PIXMAPS ==========
323 #---------- pixmap database read and write ----------
325 set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1}
327 proc read_database_header/pixmap {f} { }
328 proc read_database_entry/pixmap {f def} {
333 set p3 [db_getsl $f]; append im $p3 "\n"
334 if {[string compare $p3 P3]} { error "$p3 ?" }
336 set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h
337 set depth [db_getsl $f]; append im $depth "\n"
339 for {set y 0} {$y < $h} {incr y} {
340 set line [db_getsl $f]; append im $line "\n"
342 set database($im) $def
344 proc write_database_header/pixmap {f} { puts $f "" }
345 proc format_database_entry/pixmap {im def} {
349 #---------- pixmap display and input handling ----------
351 proc foreach_pixmap_col {var body} {
354 for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
359 proc pixmap_select {ncol} {
361 debug "PIX SELECT $ncol [llength $alloptions]"
362 foreach_pixmap_col col {
363 if {$col==$ncol} continue
364 .pe.grid.l$col selection clear 0 end
368 proc pixmap_maybe_ok {} {
369 global alloptions pixmap_selcol pixmap_selrow
371 foreach_pixmap_col col {
372 set cs [.pe.grid.l$col curselection]
373 set lcs [llength $cs]
376 set pixmap_selcol $col
377 set pixmap_selrow [lindex $cs 0]
380 debug "MAYBE_OK YES col=$pixmap_selcol row=$pixmap_selrow."
381 .pe.ok configure -state normal -command pixmap_ok
383 .pe.ok configure -state disabled -command {}
387 global database ppm pixmap_selcol pixmap_selrow mainkind alloptions
390 foreach_pixmap_col col {
391 .pe.grid.l$col configure -state disabled
393 .pe.ok configure -state disabled
395 manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \
397 manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \
399 set result "$colname - $rowname"
400 debug "UPDATE PIXMAP AS >$result<"
402 do_database_update $ppm $result
407 proc required/pixmap {} {
408 global unk_what ppm mulcols alloptions
409 must_gets stdin unk_what
410 debug "GOT pixmap $unk_what"
413 must_gets_imagel stdin ppml
414 if {![string length $ppml]} break
415 append ppm $ppml "\n"
417 set data [exec pnmscale 2 << $ppm]
418 image create photo image/main -data $data
420 set alloptions [exec ./database-info-fetch $unk_what]
422 select_database pixmap
424 set mulcols [image width image/main]
425 set mulrows [image height image/main]
427 place forget .d.mi.csr_0
428 place forget .d.mi.csr_1
430 pack forget .selctx .d.csr .d.got
431 pack .pe -side top -before .d -pady 2
432 .d configure -takefocus 0
435 eval destroy [winfo children .pe.grid]
436 set col 0; foreach {colname coldesc rows} $alloptions {
437 debug "INIT $col $colname \"$coldesc\""
438 label .pe.grid.t$col -text $colname
439 listbox .pe.grid.l$col
440 foreach {rowname rowdesc} $rows {
441 debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
442 .pe.grid.l$col insert end $rowdesc
444 bind .pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
445 grid .pe.grid.t$col -column $col -row 0
446 grid .pe.grid.l$col -column $col -row 1
452 {{Indicate the meaning of this image, and click OK.}}
456 proc approve_showentry_xinfo/pixmap {w def} {
457 label $w -image image/empty
460 #========== UPLOADS TO DICTIONARY SERVER ==========
462 proc upload_init {} {
463 global privacy_setting
465 set privacy_setting [upload_status]
467 label .privacy.warn -text " Privacy "
468 if {$privacy_setting} {
469 .privacy.warn configure -background yellow -foreground black
471 label .privacy.overall -text " Upload new dictionary entry:"
472 label .privacy.reference -text " See README.privacy."
474 pack .privacy.warn .privacy.overall -side left
476 foreach {setting string} {
479 2 {Yes, quoting my pirate name.}
481 radiobutton .privacy.o$setting -text $string \
482 -value $setting -variable privacy_setting
483 pack .privacy.o$setting -side left
484 if {$setting > $privacy_setting} {
485 .privacy.o$setting configure -state disabled
488 pack .privacy.reference -side left
490 if {!$privacy_setting} {
491 foreach w [winfo children .privacy] {
492 $w configure -state disabled
495 if {$privacy_setting} {
497 ::http::config -urlencoding utf-8
501 proc upload_status {} {
502 # returns 0, 1, 2 for none, anon, with pirate name
505 if {![info exists env(YPPSC_PCTB_DICT_SUBMIT)]} { debug a; return 0 }
506 if {![string compare 0 $env(YPPSC_PCTB_DICT_SUBMIT)]} { debug b; return 0 }
508 if {![info exists env(YPPSC_PIRATE)]} { return 1 }
509 if {![info exists env(YPPSC_OCEAN)]} { return 1 }
510 if {![string length $env(YPPSC_PIRATE)]} { return 1 }
511 if {![string length $env(YPPSC_OCEAN)]} { return 1 }
516 proc maybe_upload_entry {im def} {
517 global reqkind privacy_setting env dbname quiet
519 debug "DB-UPDATE PRIVACY $privacy_setting"
520 if {!$privacy_setting} return
522 debug "DB-UPDATE UPLOADING"
525 lappend pl dict $dbname
527 if {$privacy_setting>=2} {
528 set pirate [string totitle $env(YPPSC_PIRATE)]
529 set ocean [string totitle $env(YPPSC_OCEAN)]
530 debug "DB-UPDATE NON-ANON $ocean $pirate"
535 lappend pl entry [format_database_entry/$reqkind $im $def]
537 set url $env(YPPSC_PCTB_DICT_SUBMIT)
538 append url dictionary-update-receiver
540 set query [eval ::http::formatQuery $pl]
541 regsub -all {%0d} $query {} query
542 debug "DB-UPDATE QUERY $query"
544 if {[regexp {^\.?/} $url]} {
545 set cmd [list $url $query]
546 debug "SUBMIT CMD [string range $cmd 0 200]..."
547 set body [eval exec $cmd 2>@ stderr]
548 regsub {^Content-Type: text/plain.*\n\n} $body {} body
552 set req [::http::geturl $url -query $query]
555 "\nWARNING: Cannot do dictionary upload: $emsg\n"
559 debug "DB-UPDATE DONE $req $s(status) [array names s]"
560 set ncode [::http::ncode $req]
562 if {!(![string compare ok $s(status)] &&
563 ![string compare 200 $ncode])} {
564 set m "\nWARNING: Dictionary upload failed:"
565 foreach v {status http error posterror} {
566 if {[info exists s($v)]} { append m "\n $v: $s($v)" }
575 if {![string match {OK *} $body]} {
576 set m "\nWARNING: Dictionary upload went wrong:\n"
577 append m "\n " [join [split $body "\n"] "\n "]
584 "Uploaded $dbname `$def': $body"
588 #========== CHARACTER SET ==========
590 #---------- pgm input processor ----------
592 proc char_read_pgm {f} {
593 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
594 global cols rows charkey
596 must_gets_imagel $f l
597 if {[string compare $l P2]} { error "magic $l ?" }
599 must_gets_imagel $f l
600 if {![regexp {^([1-9]\d{0,3}) ([1-9]\d{0,3}) ([1-9]\d?)$} \
601 $l dummy cols rows maxval]} { error "head $l ?" }
603 for {set depth 1} {$maxval != (1<<$depth)-1} {incr depth} {
604 if {$depth >= 16} { error "maxval $maxval ?" }
607 set chop_l [expr {$unk_l - 80}]
608 set chop_r [expr {$cols - $unk_l - 100}]
609 if {$chop_l<0} { set chop_l 0 }
610 if {$chop_r<0} { set chop_r 0 }
612 set unk_l [expr {$unk_l - $chop_l}]
613 set unk_r [expr {$unk_r - $chop_l}]
615 foreach {min max context contexts got} $glyphsdone {
617 [expr {$min-$chop_l}] \
618 [expr {$max-$chop_l}] \
619 $context $contexts $got
624 set cols [expr {$cols - $chop_l - $chop_r}]
625 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
628 set mulcols [expr {$cols*$mul+$inter}]
629 set mulrows [expr {$rows*$mul+$inter}]
631 set o "P3\n$mulcols $mulrows 15\n"
633 for {set x 0} {$x<$cols} {incr x} { set charkey($x) {} }
636 set ointer [string repeat $ointer1 $inter]
637 set ointerl "[string repeat $ointer1 $mulcols]\n"
641 for {set y 0} {$y<$rows} {incr y} {
642 must_gets_imagel $f l
643 if {[llength $l] != $realcols} { error "realcols=$realcols $l ?" }
647 for {set x 0} {$x<$cols} {incr x} {
648 set realx [expr {$x + $chop_l}]
649 set c [lindex $l $realx]
650 append charkey($x) [format %x $c]
653 if {$x >= $unk_l && $x <= $unk_r} {
657 foreach {min max context contexts got} $glyphsdone {
659 if {$x >= $min && $x <= $max} {
660 set how [lindex {a b} $ab]
666 set c15 [expr {$c << (16-$depth)}]
667 set c15 [expr {$c15 | ($c15 >> $depth)}]
668 set c15 [expr {$c15 | ($c15 >> $depth*2)}]
669 set c15 [expr {$c15 >> 12}]
671 foreach rgb {r g b} { set $rgb {$c15} }
673 a { set r {$c15>>1 | 0x8} }
674 b { set b {$c15>>2 | 0xc} }
677 default { error "how $how ?" }
679 #debug "PIXEL $x,$y how=$how c=$c c15=$c15 $r $g $b"
682 foreach rgb {r g b} {
683 append opixel [format " %2d" [expr [set $rgb]]]
685 append ol [string repeat $opixel [expr {$mul-$inter}]]
689 append o [string repeat $ol [expr {$mul-$inter}]]
694 set data [exec pnmscale 1 << $o]
695 image create photo image/main -data $data
698 #---------- character set editor display ----------
700 proc show_context {maxhv x ctxs} {
704 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
705 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
706 place $w -x [expr {($x-1)*$mul}] -y 0
707 set wh [winfo reqheight $w]
708 if {$wh > $maxh} { set maxh $wh }
711 proc draw_glyphsdone {} {
712 global glyphsdone mul inter
713 eval destroy [winfo children .d.got]
714 foreach {min max context contexts got} $glyphsdone {
715 frame .d.got.m$min -bd 0 -background \#888
716 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
717 pack .d.got.m$min.l -padx 1 -pady 1
718 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
722 proc startup_cursor {} {
723 global cur_already cur_mode cur_0 cur_1 last_ht
724 global glyphsdone unk_l unk_r
726 set cur_already [expr {[llength $glyphsdone]/5-1}]
727 set cur_mode 1 ;# one of: 0 1 already text
730 set cur_1 [expr {$unk_r+1}]
735 #---------- character set runtime display and keystroke handling ----------
737 proc char_exactly_selctxts {contexts} {
739 foreach ctx [array names all_contexts] {
740 set ci $all_contexts($ctx)
741 set selw .selctx.c$ci
742 if {[lsearch -exact $contexts $ctx]>=0} {
747 $selw configure -state $state
751 proc recursor/0 {} { recursor//01 0 }
752 proc recursor/1 {} { recursor//01 1 }
753 proc recursor//01 {z1} {
754 global mul rhsmost_max cols glyphsdone cur_0 cur_1
757 .d.csr.csr.l configure -text "adjust [char_get_definition_context_actual]"
758 place .d.csr.csr -x [expr {$cur*$mul - 7}]
760 set okctxts [char_get_definition_contexts]
761 char_exactly_selctxts $okctxts
763 foreach ctx [lsort [array names all_contexts]] {
764 set key [string range $ctx 0 0]
765 if {[lsearch -exact $okctxts $ctx] >= 0} {
766 bind_key [string tolower $key] "
767 [list set new_context $ctx]
771 bind_key [string tolower $key] {}
773 lappend context_help $key
775 set context_help [list [join $context_help " "] \
776 {Set match context for new glyph.}]
778 bind_key space { othercursor }
779 bind_leftright_q cur_$z1 0 [expr {$cols-1}]
780 if {[llength $glyphsdone]} {
781 bind_key BackSpace { set cur_mode already; recursor }
783 bind_key BackSpace {}
786 char_start_define_text
789 {{<- ->} {move cursor, adjusting area to define}} \
790 {Space {switch to moving other cursor}} \
791 {Return {confirm location, enter letter(s)}} \
792 {Backspace {switch to correcting earlier ocr}} \
793 {Q {quit and abandon OCR run}} \
797 proc othercursor {} {
799 set cur_mode [expr {!$cur_mode}]
803 proc char_start_define_text {} {
804 global cur_0 cur_1 cur_mode
805 if {$cur_0 == $cur_1} return
806 set cdgdca [char_get_definition_context_actual]
807 if {![string length $cdgdca]} return
808 .d.csr.csr.e delete 0 end
810 .d.csr.csr.l configure -text "define $cdgdca:"
814 proc recursor/text {} {
818 {Return {confirm entry of new glyph}}
819 {Escape {abandon entry}}
822 pack .d.csr.csr.e -side left -padx 2
824 bind .d.csr.csr.e <Key-Return> {
825 set strq [.d.csr.csr.e get]
826 if {[string length $strq]} {
827 RETURN_RESULT DEFINE [list $strq]
830 bind .d.csr.csr.e <Key-Escape> {
832 pack forget .d.csr.csr.e
839 proc recursor/already {} {
841 global cur_already mul
842 global glyphsdone cur_already mul
844 char_exactly_selctxts [lindex $glyphsdone [expr {$cur_already*5+2}]]
846 .d.csr.csr.l configure -text {correct}
847 set rmax [lindex $glyphsdone [expr {$cur_already*5}]]
848 place .d.csr.csr -x [expr {$rmax*$mul-3}]
850 bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/5-1}]
851 bind_key space { bind_key Delete {}; set cur_mode 1; recursor }
853 RETURN_RESULT DELETE [lrange $glyphsdone \
854 [expr $cur_already*5] \
855 [expr $cur_already*5+2]]
858 {{<- ->} {move cursor, selecting glyph to correct}}
859 {Del {clear this glyph from the recognition database}}
860 {Space {switch to selecting area to define as new glyph}}
861 {Q {quit and abandon OCR run}}
865 proc bind_leftright_q {var min max} {
866 bind_key Left [list leftright $var $min $max -1]
867 bind_key Right [list leftright $var $min $max +1]
869 puts stderr "\nCharacter resolver quitting as you requested."
873 proc leftright {var min max inc} {
877 if {$vnew < $min || $vnew > $max} return
883 global csrh cur_mode cur_0 cur_1 mul
885 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
890 #---------- character database read and write ----------
895 # <context> <ncharacters> <hex>...
899 # $database($context 0x<bits> 0x<bits>...) = $hex
901 set database_magic/char {# ypp-sc-tools pctb font v2}
903 proc read_database_header/char {f} {
906 if {$l+0 != $rows} { error "wrong h $l $rows ?" }
908 proc read_database_entry/char {f context} {
911 set strq [db_getsl $f]
914 if {![string length $l]} break
917 set database($bm) $strq
920 proc write_database_header/char {f} {
924 proc format_database_entry/char {bm strq} {
926 set o "[lindex $bm 0]\n$strq\n"
927 foreach x [lrange $bm 1 end] { append o "$x\n" }
931 proc dbkey {ctx l r} {
934 for {set x $l} {$x <= $r} {incr x} {
935 lappend bm $charkey($x)
940 proc char_get_definition_cursors {} {
942 if {$cur_0 <= $cur_1} {
943 set cl $cur_0; set cr $cur_1
945 set cl $cur_1; set cr $cur_0
948 debug "CGD CURSORS $cl $cr"
949 return [list $cl $cr]
952 proc char_get_definition_contexts {} {
953 global glyphsdone unk_l unk_contexts database
955 manyset [char_get_definition_cursors] c0 c1
958 set ncontexts $unk_contexts
960 foreach {l r context contexts got} $glyphsdone {
961 if {$l==$c0} { set ncontexts $contexts; break }
963 if {![info exists ncontexts]} {
967 debug "CGD CONTEXTS $ncontexts"
971 proc char_get_definition_context_actual {} {
973 set ncontexts [char_get_definition_contexts]
974 if {[llength $ncontexts]==1} {
975 set c [lindex $ncontexts 0]
976 } elseif {[lsearch -exact $ncontexts $new_context]>=0} {
981 debug "CDG CONTEXT ACTUAL $c FROM NEW $new_context ALLOW $ncontexts"
985 proc update_database/DEFINE {strq} {
986 manyset [char_get_definition_cursors] c0 c1
987 set c [char_get_definition_context_actual]
988 if {![string length $c]} {
989 error "Selected context is not one of the many possibilities."
992 set bm [dbkey $c $c0 $c1]
993 do_database_update $bm $strq
996 proc update_database/DELETE {l r ctxs} {
998 if {[llength $ctxs]!=1} { error "$ctxs ?" }
1000 set bm [dbkey $ctx $l $r]
1001 catch { unset database($bm) }
1006 proc RETURN_RESULT {how what} {
1009 place forget .d.csr.csr
1010 pack forget .d.csr.csr.e
1013 eval update_database/$how $what
1015 return_result_finish
1018 #========== server for approving updates ==========
1020 proc remote-serv-log {dict pirate caller file event} {
1021 global remoteserv_logf
1022 set t [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S %Z}]
1023 set s [format "%s %-6s %-31s %-31s %s %s\n" \
1024 $t $dict $pirate $caller [file tail $file] $event]
1025 puts -nonewline $remoteserv_logf $s
1028 proc remote-serv/list {} {
1030 foreach file [glob -nocomplain -type f -directory $dropdir _update.*.rdy] {
1036 puts_counted stdout d
1041 proc remote-serv/take {yesno file dict} {
1042 global dropdir dictdir rows reqkind database
1044 debug "TAKE [list $yesno $file $dict]"
1045 read_counted stdin pirate
1046 read_counted stdin caller
1047 read_counted stdin key
1048 read_counted stdin val
1050 must_gets_exactly stdin confirmed
1052 manyset [dict2_reqkind_rows $dict] reqkind rows
1055 read_database $dictdir/master-$dict.txt
1056 set database($key) $val
1062 remote-serv-log $dict $pirate $caller $file "$desc $reqkind $rows"
1063 file delete -force $file
1068 proc remote-serv/noop {} {
1072 set remoteserv_banner {ypp-sc-tools pctb remote-server v1}
1074 proc main/remoteserv {} {
1075 global argv dropdir remoteserv_banner remoteserv_logf dictdir
1076 manyset $argv dropdir dictdir
1077 puts $remoteserv_banner
1078 set remoteserv_logf [open $dropdir/_dict.log a]
1079 fconfigure $remoteserv_logf -buffering line
1082 if {[gets stdin l] < 0} break
1087 #========== updates approver ==========
1089 proc puts_server {s} {
1094 proc must_gets_server {lv} {
1101 proc must_gets_exactly_server {expected} {
1102 must_gets_server got
1103 if {[string compare $expected $got]} { error "$expected $got ?" }
1106 proc regsub-data {exp subspec args} {
1108 if {![eval regsub $args -- [list $exp $data $subspec data]]} {
1109 error "$exp >$data< ?"
1113 proc dict2_reqkind_rows {dict} {
1114 if {![string compare pixmap $dict]} {
1117 } elseif {[regexp {^(char)([1-9]\d*)$} $dict dummy reqkind rows]} {
1118 debug "DICT CHAR rqk=$reqkind r=$rows."
1119 return [list $reqkind $rows]
1125 proc chop_counted {var} {
1128 if {![regexp {^(0|[1-9]\d*)\n} $data dummy l]} { error "$data ?" }
1129 regsub-data {^.*\n} {} -line
1130 set val [string range $data 0 [expr {$l-1}]]
1131 set data [string range $data $l end]
1132 debug "CHOP_COUNTED $l $var"
1133 regsub-data {^\n} {}
1136 proc approve_decompose_data {specdata} {
1140 regsub-data {^ypp-sc-tools dictionary update v2\n} {}
1141 uplevel 1 chop_counted pirate
1142 uplevel 1 chop_counted caller
1143 uplevel 1 chop_counted dict
1144 uplevel 1 chop_counted ctx
1145 uplevel 1 chop_counted def
1146 uplevel 1 chop_counted image
1147 uplevel 1 chop_counted key
1148 uplevel 1 chop_counted val
1150 return [uplevel 1 {list $dict $def $image}]
1153 proc approve_compare {fd1 fd2} {
1154 manyset $fd1 file data; set sv1 [approve_decompose_data $data]
1155 manyset $fd2 file data; set sv2 [approve_decompose_data $data]
1156 return [string compare $sv1 $sv2]
1159 proc string2unicodenames {str} {
1160 return [exec perl -e {
1161 use Unicode::CharName qw(uname);
1163 foreach $_ (split //,$ARGV[0]) {
1164 print uname(ord),"\n" or die $!
1169 proc approve_showentry {ix file specdata} {
1170 global approve_ixes reqkind approve_entryhow
1172 approve_decompose_data $specdata
1177 label $wb-inf.who -text $pirate
1179 entry $wb-inf.file -font fixed -relief flat
1180 $wb-inf.file insert end [file tail $file]
1181 $wb-inf.file configure -state readonly -width -1
1183 pack $wb-inf.who $wb-inf.file -side top
1186 label $wb-def.scope -text "$dict $ctx"
1187 label $wb-def.def -text $def
1188 pack $wb-def.scope $wb-def.def -side bottom
1190 if {[regexp {^P2} $image]} {
1191 set image [exec pgmtoppm {#008-white} << $image | pnmnoraw]
1194 set image [exec pnmscale 2 << $image]
1196 image create photo approve/$ix -data $image
1197 label $wb-image -image approve/$ix -bd 2 -relief flat -bg black
1199 manyset [dict2_reqkind_rows $dict] reqkind
1200 approve_showentry_xinfo/$reqkind $wb-xinfo $def
1203 label $wb-div -bd 1 -relief sunken -image image/empty
1204 grid configure $wb-div -columnspan 5 -sticky ew -padx 5
1207 frame $wb-act -bd 2 -relief groove
1208 set approve_entryhow($ix) approve
1209 foreach how {approve reject defer} {
1211 radiobutton $w -variable approve_entryhow($ix) \
1212 -text [string totitle $how] -value $how
1216 grid $wb-def $wb-image $wb-xinfo $wb-act $wb-inf -padx 3
1217 grid configure $wb-image -ipadx 3 -ipady 3 -sticky w
1219 lappend approve_ixes $ix
1222 proc approve_approve_reject_one {ix yesno} {
1223 global approve_list server
1224 manyset [lindex $approve_list $ix] file tdata
1225 approve_decompose_data $tdata
1226 puts_server "take $yesno $file $dict"
1227 puts_counted $server pirate
1228 puts_counted $server caller
1229 puts_counted $server key
1230 puts_counted $server val
1231 puts_server confirmed
1233 must_gets_exactly_server done
1236 proc approve_check_server {} {
1240 must_gets_exactly_server ok
1243 proc approve_confirm {} {
1244 global approve_ixes approve_entryhow
1245 approve_check_server
1246 foreach ix $approve_ixes {
1247 set how $approve_entryhow($ix)
1248 switch -exact $how {
1249 approve { approve_approve_reject_one $ix 1 }
1250 reject { approve_approve_reject_one $ix 0 }
1252 default { error $how? }
1258 proc approve_fetch_list {} {
1259 global server approve_list
1264 must_gets_server more
1265 switch -exact $more {
1268 default { error "$more ?" }
1270 must_gets_server file
1271 read_counted $server data
1272 lappend approve_list [list $file $data]
1275 if {![llength $approve_list]} { puts "Nothing (more) to approve."; exit 0 }
1277 set approve_list [lsort -command approve_compare $approve_list]
1281 proc main/approve {} {
1282 global argv server remoteserv_banner data approve_list approve_page
1283 global userhost directory dictdir debug
1285 if {[llength $argv] != 3} { error "wrong # args" }
1286 manyset $argv userhost directory dictdir
1287 debug "APPROVER FOR $userhost $directory $dictdir"
1289 set cmd [list tclsh $directory/dictionary-manager]
1290 if {$debug} { lappend cmd --debug-server }
1291 lappend cmd --remote-server-1 $directory $dictdir
1292 switch -glob $userhost {
1294 {* *} { set cmd $userhost }
1295 * { set cmd [concat [list ssh -o compression=yes $userhost] $cmd] }
1297 debug "APPROVER RUNS $cmd"
1299 lappend cmd 2>@ stderr
1300 set server [open |$cmd r+]
1301 must_gets_exactly_server $remoteserv_banner
1303 button .left -text {<<} -command {approve_show_page -1}
1304 button .right -text {>>} -command {approve_show_page +1}
1306 label .title -text {}
1307 frame .app -bd 2 -relief groove
1308 button .ok -text "Confirm" -command approve_confirm
1309 pack .title .app -side top
1310 pack .left -side left
1311 pack .right -side right
1312 pack .ok -side bottom
1314 image create bitmap image/empty
1320 proc approve_show_page {delta} {
1321 global approve_page approve_ixes approve_list userhost directory dictdir
1323 eval destroy [winfo children .app]
1327 incr approve_page $delta
1329 set ll [llength $approve_list]
1330 set npages [expr {($ll+$per_page-1)/$per_page}]
1331 if {$approve_page >= $npages} { incr approve_page -1 }
1333 set page_start [expr {$approve_page*$per_page}]
1334 set page_end [expr {$page_start+$per_page-1}]
1336 for {set ix $page_start} {$ix < $ll && $ix <= $page_end} {incr ix} {
1337 set fd [lindex $approve_list $ix]
1338 eval approve_showentry $ix $fd
1341 .title configure -text \
1342 "$userhost\n$directory => $dictdir\nPage [expr {$approve_page+1}]/$npages"
1344 .left configure -state disabled
1345 .right configure -state disabled
1346 if {$approve_page>0} { .left configure -state normal }
1347 if {$approve_page<$npages-1} { .right configure -state normal }
1350 #========== main program ==========
1352 proc return_result_start {} {
1353 helptext {{{ Processing }}}
1357 proc return_result_finish {} {
1362 proc main/default {} {
1363 puts stderr "Do not run this program directly."
1366 proc done/default {} {
1372 fileevent stdin readable {}
1373 fconfigure stdin -blocking yes
1375 if {[gets stdin reqkind]<0} {
1376 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
1384 proc main/automatic {} {
1385 fconfigure stdin -blocking no
1386 fileevent stdin readable required
1388 proc done/automatic {} {
1389 exec sh -c {printf \\0 >&4}
1395 set mainkind default
1402 switch -exact -- $arg {
1403 {--quiet} { set quiet 1 }
1404 {--debug} { set debug 1 }
1405 {--debug-rect} { set debug_rect 1 }
1406 {--debug-server} { proc debug {m} { puts stderr "DICT-MGR-SVR $m" }}
1408 {--approve-updates} { set mainkind approve; break }
1409 {--automatic-1} { set mainkind automatic; break }
1410 {--remote-server-1} { set mainkind remoteserv; break }
1411 {--automatic*} - {--remote-server}
1412 { error "incompatible versions - install problem" }
1413 default { error "huh $argv ?" }
1417 proc debug {m} { puts stderr "DICT-MGR $m" }
1419 set argv [lrange $argv $ai end]