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_imagel {f lvar} {
60 if {$debug_rect} { debug "<< $l" }
63 proc must_gets_exactly {f expected} {
65 if {[string compare $expected $got]} { error "$expected $got ?" }
68 proc read_counted {f var} {
71 set val [read $f $count]
73 if {[string length $eol]} { error "$eol ?" }
74 debug "READ_COUNTED $count $var"
77 proc puts_counted {f dvar} {
79 set count [string length $d]
82 debug "PUTS_COUNTED $count $dvar"
86 global errorCode errorInfo
87 puts stderr "ERROR: $m\n[list $errorCode]\n$errorInfo\n";
91 #---------- display core ----------
100 proc init_widgets {} {
102 global csrh gotsh ctxh
104 if {[winfo exists .d]} return
106 frame .privacy -bd 2 -relief groove
107 pack .privacy -side top -padx 2 -pady 2 -fill x
111 frame .d -pady 2 -padx 2 -bg black -bd 2 -relief sunken
113 image create bitmap image/main
114 label .d.mi -image image/main -bd 0
116 frame .d.csr -bg black -height $csrh
117 frame .d.got -bg black -height $gotsh
118 frame .d.ctx -bg black
120 image create bitmap image/cursor -foreground white -background black \
122 {#define csr_width 11
123 #define csr_height 11
124 static unsigned char csr_bits[] = {
125 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
126 0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
130 label .d.csr.csr.l -image image/cursor -compound left -fg white -bg black
131 entry .d.csr.csr.e -bd 0
132 pack .d.csr.csr.l -side left
134 frame .selctx -bd 2 -relief groove
135 frame .d.mi.csr_0 -bg white -width 1
136 frame .d.mi.csr_1 -bg white -width 1
140 button .pe.ok -text OK
141 pack .pe.grid .pe.ok -side left
143 pack .d.mi .d.ctx -side top -anchor w
144 pack .d -fill x -padx 2 -pady 2
146 frame .help -bd 2 -relief groove
147 pack .help -pady 2 -padx 2
150 proc resize_widgets_core {} {
151 global mulcols mulrows csrh gotsh ctxh
152 global unk_l unk_contexts
154 foreach w {.d.csr .d.got .d.ctx} {
155 $w configure -width $mulcols
157 .d.csr configure -width [expr {$mulcols+150}]
159 eval destroy [winfo children .d.ctx]
166 if {![string compare $t $last_ht]} return
167 eval destroy [grid slaves .help]
168 set y 0; foreach l $t {
169 set x 0; foreach c $l {
170 set w .help.at${x}x${y}
172 grid $w -row $y -column $x -padx 5 -sticky w
180 proc bind_key {k proc} {
182 bind .d <Key-$k> $proc
183 set keybindings($k) [expr {!![string length $proc]}]
184 .d configure -takefocus 1
186 proc unbind_all_keys {} {
188 foreach k [array names keybindings] { bind_key $k {} }
189 .d configure -takefocus 0
192 #---------- database read and write common wrapper ----------
195 if {[gets $f l] < 0} { error "unexpected db eof" }
199 proc read_database {fn} {
200 global reqkind database database_fn
201 upvar #0 database_magic/$reqkind magic
202 catch { unset database }
205 if {![file exists $database_fn]} return
206 set f [open $database_fn r]
207 if {[string compare [db_getsl $f] $magic]} { error "$magic $reqkind ?" }
209 read_database_header/$reqkind $f
213 if {![string length $l1]} continue
214 if {[regexp {^\#} $l1]} continue
215 if {![string compare . $l1]} break
217 read_database_entry/$reqkind $f $l1
222 proc write_database {} {
223 global reqkind database_fn database
224 upvar #0 database_magic/$reqkind magic
226 set f [open $database_fn.new w]
229 write_database_header/$reqkind $f
232 foreach bm [array names database] {
233 lappend ol [format_database_entry/$reqkind $bm $database($bm)]
235 foreach o [lsort $ol] {
240 file rename -force $database_fn.new $database_fn
243 proc select_database {dbname_spec} {
245 set dbname $dbname_spec
246 read_database "./#local-$dbname#.txt"
249 proc do_database_update {im def} {
251 maybe_upload_entry $im $def
252 set database($im) $def
256 proc required/char {} {
257 global mulrows glyphsdone unk_l unk_r unk_contexts rows new_context
258 global all_contexts debug_rect
263 manyset [lrange $l 0 3] unk_l unk_r unk_contexts
264 set glyphsdone [lrange $l 3 end]
268 catch { unset all_contexts }
272 .d.mi.csr_$w configure -height $mulrows
275 foreach {min max context contexts got} $glyphsdone {
276 show_context maxh $min $context
277 foreach ctx $contexts { set all_contexts($ctx) 1 }
279 foreach ctx $unk_contexts { set all_contexts($ctx) 1 }
281 eval destroy [winfo children .selctx]
282 label .selctx.title -text \
283 {Select match context for altering dictionary:}
284 pack .selctx.title -side left
285 set new_context [lindex $unk_contexts 0]
287 set ci 0; foreach ctx [lsort [array names all_contexts]] {
288 set all_contexts($ctx) $ci
289 set selw .selctx.c$ci
291 radiobutton $selw -variable new_context -value $ctx -text $seltxt
292 pack $selw -side left
295 $selw configure -text "$seltxt."
296 label .selctx.warning -text {See README.charset.}
297 pack .selctx.warning -side left
299 show_context maxh $unk_l $unk_contexts
300 .d.ctx configure -height $maxh
302 pack .selctx -before .d -padx 2 -fill x
303 pack .d.csr -side top -before .d.mi -anchor w
304 pack .d.got .d.ctx -side top -after .d.mi -anchor w
305 pack configure .selctx -fill x
308 select_database char$rows
313 proc approve_showentry_xinfo/char {w def} {
314 set unic [string2unicodenames $def]
318 #========== PIXMAPS ==========
320 #---------- pixmap database read and write ----------
322 set database_magic/pixmap {# ypp-sc-tools pctb pixmaps v1}
324 proc read_database_header/pixmap {f} { }
325 proc read_database_entry/pixmap {f def} {
330 set p3 [db_getsl $f]; append im $p3 "\n"
331 if {[string compare $p3 P3]} { error "$p3 ?" }
333 set wh [db_getsl $f]; append im $wh "\n"; manyset $wh w h
334 set depth [db_getsl $f]; append im $depth "\n"
336 for {set y 0} {$y < $h} {incr y} {
337 set line [db_getsl $f]; append im $line "\n"
339 set database($im) $def
341 proc write_database_header/pixmap {f} { puts $f "" }
342 proc format_database_entry/pixmap {im def} {
346 #---------- pixmap display and input handling ----------
348 proc foreach_pixmap_col {var body} {
351 for {set col 0} {$col < [llength $alloptions]/3} {incr col} {
356 proc pixmap_select {ncol} {
358 debug "PIX SELECT $ncol [llength $alloptions]"
359 foreach_pixmap_col col {
360 if {$col==$ncol} continue
361 .pe.grid.l$col selection clear 0 end
365 proc pixmap_maybe_ok {} {
366 global alloptions pixmap_selcol pixmap_selrow
368 foreach_pixmap_col col {
369 set cs [.pe.grid.l$col curselection]
370 set lcs [llength $cs]
373 set pixmap_selcol $col
374 set pixmap_selrow [lindex $cs 0]
377 debug "MAYBE_OK YES col=$pixmap_selcol row=$pixmap_selrow."
378 .pe.ok configure -state normal -command pixmap_ok
380 .pe.ok configure -state disabled -command {}
384 global database ppm pixmap_selcol pixmap_selrow mainkind alloptions
387 foreach_pixmap_col col {
388 .pe.grid.l$col configure -state disabled
390 .pe.ok configure -state disabled
392 manyset [lrange $alloptions [expr {$pixmap_selcol*3}] end] \
394 manyset [lrange $rows [expr {$pixmap_selrow*2}] end] \
396 set result "$colname - $rowname"
397 debug "UPDATE PIXMAP AS >$result<"
399 do_database_update $ppm $result
404 proc required/pixmap {} {
405 global unk_what ppm mulcols alloptions
406 must_gets stdin unk_what
407 debug "GOT pixmap $unk_what"
410 must_gets_imagel stdin ppml
411 if {![string length $ppml]} break
412 append ppm $ppml "\n"
414 set data [exec pnmscale 2 << $ppm]
415 image create photo image/main -data $data
417 set alloptions [exec ./database-info-fetch $unk_what]
419 select_database pixmap
421 set mulcols [image width image/main]
422 set mulrows [image height image/main]
424 place forget .d.mi.csr_0
425 place forget .d.mi.csr_1
427 pack forget .selctx .d.csr .d.got
428 pack .pe -side top -before .d -pady 2
429 .d configure -takefocus 0
432 eval destroy [winfo children .pe.grid]
433 set col 0; foreach {colname coldesc rows} $alloptions {
434 debug "INIT $col $colname \"$coldesc\""
435 label .pe.grid.t$col -text $colname
436 listbox .pe.grid.l$col
437 foreach {rowname rowdesc} $rows {
438 debug "INIT $col $colname \"$coldesc\" $rowname \"$rowdesc\""
439 .pe.grid.l$col insert end $rowdesc
441 bind .pe.grid.l$col <<ListboxSelect>> [list pixmap_select $col]
442 grid .pe.grid.t$col -column $col -row 0
443 grid .pe.grid.l$col -column $col -row 1
449 {{Indicate the meaning of this image, and click OK.}}
453 proc approve_showentry_xinfo/pixmap {w def} {
454 label $w -image image/empty
457 #========== UPLOADS TO DICTIONARY SERVER ==========
459 proc upload_init {} {
460 global privacy_setting
462 set privacy_setting [upload_status]
464 label .privacy.warn -text " Privacy "
465 if {$privacy_setting} {
466 .privacy.warn configure -background yellow -foreground black
468 label .privacy.overall -text " Upload new dictionary entry:"
469 label .privacy.reference -text " See README.privacy."
471 pack .privacy.warn .privacy.overall -side left
473 foreach {setting string} {
476 2 {Yes, quoting my pirate name.}
478 radiobutton .privacy.o$setting -text $string \
479 -value $setting -variable privacy_setting
480 pack .privacy.o$setting -side left
481 if {$setting > $privacy_setting} {
482 .privacy.o$setting configure -state disabled
485 pack .privacy.reference -side left
487 if {!$privacy_setting} {
488 foreach w [winfo children .privacy] {
489 $w configure -state disabled
492 if {$privacy_setting} {
494 ::http::config -urlencoding utf-8
498 proc upload_status {} {
499 # returns 0, 1, 2 for none, anon, with pirate name
502 if {![info exists env(YPPSC_PCTB_DICT_SUBMIT)]} { debug a; return 0 }
503 if {![string compare 0 $env(YPPSC_PCTB_DICT_SUBMIT)]} { debug b; return 0 }
505 if {![info exists env(YPPSC_PIRATE)]} { return 1 }
506 if {![info exists env(YPPSC_OCEAN)]} { return 1 }
507 if {![string length $env(YPPSC_PIRATE)]} { return 1 }
508 if {![string length $env(YPPSC_OCEAN)]} { return 1 }
513 proc maybe_upload_entry {im def} {
514 global reqkind privacy_setting env dbname quiet
516 debug "DB-UPDATE PRIVACY $privacy_setting"
517 if {!$privacy_setting} return
519 debug "DB-UPDATE UPLOADING"
522 lappend pl dict $dbname
524 if {$privacy_setting>=2} {
525 set pirate [string totitle $env(YPPSC_PIRATE)]
526 set ocean [string totitle $env(YPPSC_OCEAN)]
527 debug "DB-UPDATE NON-ANON $ocean $pirate"
532 lappend pl entry [format_database_entry/$reqkind $im $def]
534 set url $env(YPPSC_PCTB_DICT_SUBMIT)
535 append url dictionary-update-receiver
537 set query [eval ::http::formatQuery $pl]
538 regsub -all {%0d} $query {} query
539 debug "DB-UPDATE QUERY $query"
541 if {[regexp {^\.?/} $url]} {
542 set cmd [list $url $query]
543 debug "SUBMIT CMD [string range $cmd 0 200]..."
544 set body [eval exec $cmd 2>@ stderr]
545 regsub {^Content-Type: text/plain.*\n\n} $body {} body
549 set req [::http::geturl $url -query $query]
552 "\nWARNING: Cannot do dictionary upload: $emsg\n"
556 debug "DB-UPDATE DONE $req $s(status) [array names s]"
557 set ncode [::http::ncode $req]
559 if {!(![string compare ok $s(status)] &&
560 ![string compare 200 $ncode])} {
561 set m "\nWARNING: Dictionary upload failed:"
562 foreach v {status http error posterror} {
563 if {[info exists s($v)]} { append m "\n $v: $s($v)" }
572 if {![string match {OK *} $body]} {
573 set m "\nWARNING: Dictionary upload went wrong:\n"
574 append m "\n " [join [split $body "\n"] "\n "]
581 "Uploaded $dbname `$def': $body"
585 #========== CHARACTER SET ==========
587 #---------- pgm input processor ----------
589 proc char_read_pgm {f} {
590 global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
591 global cols rows charkey
593 must_gets_imagel $f l
594 if {[string compare $l P2]} { error "magic $l ?" }
596 must_gets_imagel $f l
597 if {![regexp {^([1-9]\d{0,3}) ([1-9]\d{0,3}) ([1-9]\d?)$} \
598 $l dummy cols rows maxval]} { error "head $l ?" }
600 for {set depth 1} {$maxval != (1<<$depth)-1} {incr depth} {
601 if {$depth >= 16} { error "maxval $maxval ?" }
604 set chop_l [expr {$unk_l - 80}]
605 set chop_r [expr {$cols - $unk_l - 100}]
606 if {$chop_l<0} { set chop_l 0 }
607 if {$chop_r<0} { set chop_r 0 }
609 set unk_l [expr {$unk_l - $chop_l}]
610 set unk_r [expr {$unk_r - $chop_l}]
612 foreach {min max context contexts got} $glyphsdone {
614 [expr {$min-$chop_l}] \
615 [expr {$max-$chop_l}] \
616 $context $contexts $got
621 set cols [expr {$cols - $chop_l - $chop_r}]
622 debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
625 set mulcols [expr {$cols*$mul+$inter}]
626 set mulrows [expr {$rows*$mul+$inter}]
628 set o "P3\n$mulcols $mulrows 15\n"
630 for {set x 0} {$x<$cols} {incr x} { set charkey($x) {} }
633 set ointer [string repeat $ointer1 $inter]
634 set ointerl "[string repeat $ointer1 $mulcols]\n"
638 for {set y 0} {$y<$rows} {incr y} {
639 must_gets_imagel $f l
640 if {[llength $l] != $realcols} { error "realcols=$realcols $l ?" }
644 for {set x 0} {$x<$cols} {incr x} {
645 set realx [expr {$x + $chop_l}]
646 set c [lindex $l $realx]
647 append charkey($x) [format %x $c]
650 if {$x >= $unk_l && $x <= $unk_r} {
654 foreach {min max context contexts got} $glyphsdone {
656 if {$x >= $min && $x <= $max} {
657 set how [lindex {a b} $ab]
663 set c15 [expr {$c << (16-$depth)}]
664 set c15 [expr {$c15 | ($c15 >> $depth)}]
665 set c15 [expr {$c15 | ($c15 >> $depth*2)}]
666 set c15 [expr {$c15 >> 12}]
668 foreach rgb {r g b} { set $rgb {$c15} }
670 a { set r {$c15>>1 | 0x8} }
671 b { set b {$c15>>2 | 0xc} }
674 default { error "how $how ?" }
676 #debug "PIXEL $x,$y how=$how c=$c c15=$c15 $r $g $b"
679 foreach rgb {r g b} {
680 append opixel [format " %2d" [expr [set $rgb]]]
682 append ol [string repeat $opixel [expr {$mul-$inter}]]
686 append o [string repeat $ol [expr {$mul-$inter}]]
691 set data [exec pnmscale 1 << $o]
692 image create photo image/main -data $data
695 #---------- character set editor display ----------
697 proc show_context {maxhv x ctxs} {
701 if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
702 label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
703 place $w -x [expr {($x-1)*$mul}] -y 0
704 set wh [winfo reqheight $w]
705 if {$wh > $maxh} { set maxh $wh }
708 proc draw_glyphsdone {} {
709 global glyphsdone mul inter
710 eval destroy [winfo children .d.got]
711 foreach {min max context contexts got} $glyphsdone {
712 frame .d.got.m$min -bd 0 -background \#888
713 label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
714 pack .d.got.m$min.l -padx 1 -pady 1
715 place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
719 proc startup_cursor {} {
720 global cur_already cur_mode cur_0 cur_1 last_ht
721 global glyphsdone unk_l unk_r
723 set cur_already [expr {[llength $glyphsdone]/5-1}]
724 set cur_mode 1 ;# one of: 0 1 already text
727 set cur_1 [expr {$unk_r+1}]
732 #---------- character set runtime display and keystroke handling ----------
734 proc char_exactly_selctxts {contexts} {
736 foreach ctx [array names all_contexts] {
737 set ci $all_contexts($ctx)
738 set selw .selctx.c$ci
739 if {[lsearch -exact $contexts $ctx]>=0} {
744 $selw configure -state $state
748 proc recursor/0 {} { recursor//01 0 }
749 proc recursor/1 {} { recursor//01 1 }
750 proc recursor//01 {z1} {
751 global mul rhsmost_max cols glyphsdone cur_0 cur_1
754 .d.csr.csr.l configure -text "adjust [char_get_definition_context_actual]"
755 place .d.csr.csr -x [expr {$cur*$mul - 7}]
757 set okctxts [char_get_definition_contexts]
758 char_exactly_selctxts $okctxts
760 foreach ctx [lsort [array names all_contexts]] {
761 set key [string range $ctx 0 0]
762 if {[lsearch -exact $okctxts $ctx] >= 0} {
763 bind_key [string tolower $key] "
764 [list set new_context $ctx]
768 bind_key [string tolower $key] {}
770 lappend context_help $key
772 set context_help [list [join $context_help " "] \
773 {Set match context for new glyph.}]
775 bind_key space { othercursor }
776 bind_leftright_q cur_$z1 0 [expr {$cols-1}]
777 if {[llength $glyphsdone]} {
778 bind_key BackSpace { set cur_mode already; recursor }
780 bind_key BackSpace {}
783 char_start_define_text
786 {{<- ->} {move cursor, adjusting area to define}} \
787 {Space {switch to moving other cursor}} \
788 {Return {confirm location, enter letter(s)}} \
789 {Backspace {switch to correcting earlier ocr}} \
790 {Q {quit and abandon OCR run}} \
794 proc othercursor {} {
796 set cur_mode [expr {!$cur_mode}]
800 proc char_start_define_text {} {
801 global cur_0 cur_1 cur_mode
802 if {$cur_0 == $cur_1} return
803 set cdgdca [char_get_definition_context_actual]
804 if {![string length $cdgdca]} return
805 .d.csr.csr.e delete 0 end
807 .d.csr.csr.l configure -text "define $cdgdca:"
811 proc recursor/text {} {
815 {Return {confirm entry of new glyph}}
816 {Escape {abandon entry}}
819 pack .d.csr.csr.e -side left -padx 2
821 bind .d.csr.csr.e <Key-Return> {
822 set strq [.d.csr.csr.e get]
823 if {[string length $strq]} {
824 RETURN_RESULT DEFINE [list $strq]
827 bind .d.csr.csr.e <Key-Escape> {
829 pack forget .d.csr.csr.e
836 proc recursor/already {} {
838 global cur_already mul
839 global glyphsdone cur_already mul
841 char_exactly_selctxts [lindex $glyphsdone [expr {$cur_already*5+2}]]
843 .d.csr.csr.l configure -text {correct}
844 set rmax [lindex $glyphsdone [expr {$cur_already*5}]]
845 place .d.csr.csr -x [expr {$rmax*$mul-3}]
847 bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/5-1}]
848 bind_key space { bind_key Delete {}; set cur_mode 1; recursor }
850 RETURN_RESULT DELETE [lrange $glyphsdone \
851 [expr $cur_already*5] \
852 [expr $cur_already*5+2]]
855 {{<- ->} {move cursor, selecting glyph to correct}}
856 {Del {clear this glyph from the recognition database}}
857 {Space {switch to selecting area to define as new glyph}}
858 {Q {quit and abandon OCR run}}
862 proc bind_leftright_q {var min max} {
863 bind_key Left [list leftright $var $min $max -1]
864 bind_key Right [list leftright $var $min $max +1]
866 puts stderr "\nCharacter resolver quitting as you requested."
870 proc leftright {var min max inc} {
874 if {$vnew < $min || $vnew > $max} return
880 global csrh cur_mode cur_0 cur_1 mul
882 place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
887 #---------- character database read and write ----------
892 # <context> <ncharacters> <hex>...
896 # $database($context 0x<bits> 0x<bits>...) = $hex
898 set database_magic/char {# ypp-sc-tools pctb font v2}
900 proc read_database_header/char {f} {
903 if {$l+0 != $rows} { error "wrong h $l $rows ?" }
905 proc read_database_entry/char {f context} {
908 set strq [db_getsl $f]
911 if {![string length $l]} break
914 set database($bm) $strq
917 proc write_database_header/char {f} {
921 proc format_database_entry/char {bm strq} {
923 set o "[lindex $bm 0]\n$strq\n"
924 foreach x [lrange $bm 1 end] { append o "$x\n" }
928 proc dbkey {ctx l r} {
931 for {set x $l} {$x <= $r} {incr x} {
932 lappend bm $charkey($x)
937 proc char_get_definition_cursors {} {
939 if {$cur_0 <= $cur_1} {
940 set cl $cur_0; set cr $cur_1
942 set cl $cur_1; set cr $cur_0
945 debug "CGD CURSORS $cl $cr"
946 return [list $cl $cr]
949 proc char_get_definition_contexts {} {
950 global glyphsdone unk_l unk_contexts database
952 manyset [char_get_definition_cursors] c0 c1
955 set ncontexts $unk_contexts
957 foreach {l r context contexts got} $glyphsdone {
958 if {$l==$c0} { set ncontexts $contexts; break }
960 if {![info exists ncontexts]} {
964 debug "CGD CONTEXTS $ncontexts"
968 proc char_get_definition_context_actual {} {
970 set ncontexts [char_get_definition_contexts]
971 if {[llength $ncontexts]==1} {
972 set c [lindex $ncontexts 0]
973 } elseif {[lsearch -exact $ncontexts $new_context]>=0} {
978 debug "CDG CONTEXT ACTUAL $c FROM NEW $new_context ALLOW $ncontexts"
982 proc update_database/DEFINE {strq} {
983 manyset [char_get_definition_cursors] c0 c1
984 set c [char_get_definition_context_actual]
985 if {![string length $c]} {
986 error "Selected context is not one of the many possibilities."
989 set bm [dbkey $c $c0 $c1]
990 do_database_update $bm $strq
993 proc update_database/DELETE {l r ctxs} {
995 if {[llength $ctxs]!=1} { error "$ctxs ?" }
997 set bm [dbkey $ctx $l $r]
998 catch { unset database($bm) }
1003 proc RETURN_RESULT {how what} {
1006 place forget .d.csr.csr
1007 pack forget .d.csr.csr.e
1010 eval update_database/$how $what
1012 return_result_finish
1015 #========== server for approving updates ==========
1017 proc remote-serv-log {dict pirate caller file event} {
1018 global remoteserv_logf
1019 set t [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S %Z}]
1020 set s [format "%s %-6s %-31s %-31s %s %s\n" \
1021 $t $dict $pirate $caller [file tail $file] $event]
1022 puts -nonewline $remoteserv_logf $s
1025 proc remote-serv/list {} {
1027 foreach file [glob -nocomplain -type f -directory $dropdir _update.*.rdy] {
1033 puts_counted stdout d
1038 proc remote-serv/take {yesno file dict} {
1039 global dropdir dictdir rows reqkind database
1041 debug "TAKE [list $yesno $file $dict]"
1042 read_counted stdin pirate
1043 read_counted stdin caller
1044 read_counted stdin key
1045 read_counted stdin val
1047 must_gets_exactly stdin confirmed
1049 manyset [dict2_reqkind_rows $dict] reqkind rows
1052 read_database $dictdir/master-$dict.txt
1053 set database($key) $val
1059 remote-serv-log $dict $pirate $caller $file "$desc $reqkind $rows"
1060 file delete -force $file
1065 proc remote-serv/noop {} {
1069 set remoteserv_banner {ypp-sc-tools pctb remote-server v1}
1071 proc main/remoteserv {} {
1072 global argv dropdir remoteserv_banner remoteserv_logf dictdir
1073 manyset $argv dropdir dictdir
1074 puts $remoteserv_banner
1075 set remoteserv_logf [open $dropdir/_dict.log a]
1076 fconfigure $remoteserv_logf -buffering line
1079 if {[gets stdin l] < 0} break
1084 #========== updates approver ==========
1086 proc puts_server {s} {
1091 proc must_gets_server {lv} {
1098 proc must_gets_exactly_server {expected} {
1099 must_gets_server got
1100 if {[string compare $expected $got]} { error "$expected $got ?" }
1103 proc regsub-data {exp subspec args} {
1105 if {![eval regsub $args -- [list $exp $data $subspec data]]} {
1106 error "$exp >$data< ?"
1110 proc dict2_reqkind_rows {dict} {
1111 if {![string compare pixmap $dict]} {
1114 } elseif {[regexp {^(char)([1-9]\d*)$} $dict dummy reqkind rows]} {
1115 debug "DICT CHAR rqk=$reqkind r=$rows."
1116 return [list $reqkind $rows]
1122 proc chop_counted {var} {
1125 if {![regexp {^(0|[1-9]\d*)\n} $data dummy l]} { error "$data ?" }
1126 regsub-data {^.*\n} {} -line
1127 set val [string range $data 0 [expr {$l-1}]]
1128 set data [string range $data $l end]
1129 debug "CHOP_COUNTED $l $var"
1130 regsub-data {^\n} {}
1133 proc approve_decompose_data {specdata} {
1137 regsub-data {^ypp-sc-tools dictionary update v1\n} {}
1138 uplevel 1 chop_counted pirate
1139 uplevel 1 chop_counted caller
1140 uplevel 1 chop_counted dict
1141 uplevel 1 chop_counted ctx
1142 uplevel 1 chop_counted def
1143 uplevel 1 chop_counted image
1144 uplevel 1 chop_counted key
1145 uplevel 1 chop_counted val
1147 return [uplevel 1 {list $dict $def $image}]
1150 proc approve_compare {fd1 fd2} {
1151 manyset $fd1 file data; set sv1 [approve_decompose_data $data]
1152 manyset $fd2 file data; set sv2 [approve_decompose_data $data]
1153 return [string compare $sv1 $sv2]
1156 proc string2unicodenames {str} {
1157 return [exec perl -e {
1158 use Unicode::CharName qw(uname);
1160 foreach $_ (split //,$ARGV[0]) {
1161 print uname(ord),"\n" or die $!
1166 proc approve_showentry {ix file specdata} {
1167 global approve_ixes reqkind approve_entryhow
1169 approve_decompose_data $specdata
1174 label $wb-inf.who -text $pirate
1176 entry $wb-inf.file -font fixed -relief flat
1177 $wb-inf.file insert end [file tail $file]
1178 $wb-inf.file configure -state readonly -width -1
1180 pack $wb-inf.who $wb-inf.file -side top
1183 label $wb-def.scope -text "$dict $ctx"
1184 label $wb-def.def -text $def
1185 pack $wb-def.scope $wb-def.def -side bottom
1187 set ppm [exec pnmscale 2 << $image]
1188 image create photo approve/$ix -data $ppm
1189 label $wb-image -image approve/$ix -bd 2 -relief sunken
1191 manyset [dict2_reqkind_rows $dict] reqkind
1192 approve_showentry_xinfo/$reqkind $wb-xinfo $def
1195 label $wb-div -bd 1 -relief sunken -image image/empty
1196 grid configure $wb-div -columnspan 5 -sticky ew -padx 5
1199 frame $wb-act -bd 2 -relief groove
1200 set approve_entryhow($ix) approve
1201 foreach how {approve reject defer} {
1203 radiobutton $w -variable approve_entryhow($ix) \
1204 -text [string totitle $how] -value $how
1208 grid $wb-def $wb-image $wb-xinfo $wb-act $wb-inf -padx 3
1209 grid configure $wb-image -ipadx 3 -ipady 3 -sticky w
1211 lappend approve_ixes $ix
1214 proc approve_approve_reject_one {ix yesno} {
1215 global approve_list server
1216 manyset [lindex $approve_list $ix] file tdata
1217 approve_decompose_data $tdata
1218 puts_server "take $yesno $file $dict"
1219 puts_counted $server pirate
1220 puts_counted $server caller
1221 puts_counted $server key
1222 puts_counted $server val
1223 puts_server confirmed
1225 must_gets_exactly_server done
1228 proc approve_check_server {} {
1232 must_gets_exactly_server ok
1235 proc approve_confirm {} {
1236 global approve_ixes approve_entryhow
1237 approve_check_server
1238 foreach ix $approve_ixes {
1239 set how $approve_entryhow($ix)
1240 switch -exact $how {
1241 approve { approve_approve_reject_one $ix 1 }
1242 reject { approve_approve_reject_one $ix 0 }
1244 default { error $how? }
1250 proc approve_fetch_list {} {
1251 global server approve_list
1256 must_gets_server more
1257 switch -exact $more {
1260 default { error "$more ?" }
1262 must_gets_server file
1263 read_counted $server data
1264 lappend approve_list [list $file $data]
1267 if {![llength $approve_list]} { puts "Nothing (more) to approve."; exit 0 }
1269 set approve_list [lsort -command approve_compare $approve_list]
1273 proc main/approve {} {
1274 global argv server remoteserv_banner data approve_list approve_page
1275 global userhost directory dictdir debug
1277 if {[llength $argv] != 3} { error "wrong # args" }
1278 manyset $argv userhost directory dictdir
1279 debug "APPROVER FOR $userhost $directory $dictdir"
1281 set cmd [list tclsh $directory/dictionary-manager]
1282 if {$debug} { lappend cmd --debug-server }
1283 lappend cmd --remote-server-1 $directory $dictdir
1284 switch -glob $userhost {
1286 {* *} { set cmd $userhost }
1287 * { set cmd [concat [list ssh -o compression=yes $userhost] $cmd] }
1289 debug "APPROVER RUNS $cmd"
1291 lappend cmd 2>@ stderr
1292 set server [open |$cmd r+]
1293 must_gets_exactly_server $remoteserv_banner
1295 button .left -text {<<} -command {approve_show_page -1}
1296 button .right -text {>>} -command {approve_show_page +1}
1298 label .title -text {}
1299 frame .app -bd 2 -relief groove
1300 button .ok -text "Confirm" -command approve_confirm
1301 pack .title .app -side top
1302 pack .left -side left
1303 pack .right -side right
1304 pack .ok -side bottom
1306 image create bitmap image/empty
1312 proc approve_show_page {delta} {
1313 global approve_page approve_ixes approve_list userhost directory dictdir
1315 eval destroy [winfo children .app]
1319 incr approve_page $delta
1321 set ll [llength $approve_list]
1322 set npages [expr {($ll+$per_page-1)/$per_page}]
1323 if {$approve_page >= $npages} { incr approve_page -1 }
1325 set page_start [expr {$approve_page*$per_page}]
1326 set page_end [expr {$page_start+$per_page-1}]
1328 for {set ix $page_start} {$ix < $ll && $ix <= $page_end} {incr ix} {
1329 set fd [lindex $approve_list $ix]
1330 eval approve_showentry $ix $fd
1333 .title configure -text \
1334 "$userhost\n$directory => $dictdir\nPage [expr {$approve_page+1}]/$npages"
1336 .left configure -state disabled
1337 .right configure -state disabled
1338 if {$approve_page>0} { .left configure -state normal }
1339 if {$approve_page<$npages-1} { .right configure -state normal }
1342 #========== main program ==========
1344 proc return_result_start {} {
1345 helptext {{{ Processing }}}
1349 proc return_result_finish {} {
1354 proc main/default {} {
1355 puts stderr "Do not run this program directly."
1358 proc done/default {} {
1364 fileevent stdin readable {}
1365 fconfigure stdin -blocking yes
1367 if {[gets stdin reqkind]<0} {
1368 if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
1376 proc main/automatic {} {
1377 fconfigure stdin -blocking no
1378 fileevent stdin readable required
1380 proc done/automatic {} {
1381 exec sh -c {printf \\0 >&4}
1387 set mainkind default
1394 switch -exact -- $arg {
1395 {--quiet} { set quiet 1 }
1396 {--debug} { set debug 1 }
1397 {--debug-rect} { set debug_rect 1 }
1398 {--debug-server} { proc debug {m} { puts stderr "DICT-MGR-SVR $m" }}
1400 {--approve-updates} { set mainkind approve; break }
1401 {--automatic-1} { set mainkind automatic; break }
1402 {--remote-server-1} { set mainkind remoteserv; break }
1403 {--automatic*} - {--remote-server}
1404 { error "incompatible versions - install problem" }
1405 default { error "huh $argv ?" }
1409 proc debug {m} { puts stderr "DICT-MGR $m" }
1411 set argv [lrange $argv $ai end]