2 # the next line restarts using tclsh \
6 # (c) Index Data 1996-1998
7 # See the file LICENSE for details.
8 # Sebastian Hammer, Adam Dickmeiss
11 # Revision 1.1 1999-06-08 10:10:16 adam
12 # New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
14 # Revision 1.8 1999/04/20 10:37:04 adam
15 # Updated for ODR - added name parameter.
17 # Revision 1.7 1998/04/03 14:44:20 adam
20 # Revision 1.6 1998/04/03 13:21:17 adam
23 # Revision 1.5 1998/04/03 12:48:17 adam
24 # Fixed bug: missed handling of constructed tags for CHOICE.
26 # Revision 1.4 1998/03/31 15:47:45 adam
27 # First compiled ASN.1 code for YAZ.
29 # Revision 1.3 1998/03/23 17:13:20 adam
30 # Implemented SET OF and ENUM. The Compiler now eats ILL (ISO10161) and
33 # Revision 1.2 1997/10/07 10:31:01 adam
34 # Added facility to specify tag type (CONTEXT, APPLICATION, ...).
36 # Revision 1.1.1.1 1996/10/31 14:04:40 adam
37 # First version of the compiler for YAZ.
43 # Syntax for the ASN.1 supported:
46 # module -> name skip DEFINITIONS ::= mbody END
47 # mbody -> EXPORTS { nlist }
48 # | IMPORTS { imlist }
52 # type -> SEQUENCE { sqlist }
63 # sqlist -> sqlist , name tmt opt
65 # chlist -> chlist , name tmt
67 # enlist -> enlist , name (n)
69 # imlist -> nlist FROM name
70 # imlist nlist FROM name
73 # mod -> IMPLICIT | EXPLICIT | e
74 # tag -> [tagtype n] | [n] | e
77 # name identifier/token
79 # skip one token skipped
81 # tagtype APPLICATION, CONTEXT, etc.
83 # lex: moves input file pointer and returns type of token.
84 # The globals $type and $val are set. $val holds name if token
85 # is normal identifier name.
86 # sets global var type to one of:
89 # \} right curly brace
98 while {![string length $inf(str)]} {
100 set inf(cnt) [gets $inf(inf) inf(str)]
105 lappend inf(asn,$inf(asndef)) $inf(str)
106 set l [string first -- $inf(str)]
109 set inf(str) [string range $inf(str) 0 $l]
111 set inf(str) [string trim $inf(str)]
113 set s [string index $inf(str) 0]
123 \[ { regexp {^\[[ ]*(.+)[ ]*\]} $inf(str) s val }
124 : { regexp {^::=} $inf(str) s }
126 regexp "^\[^,\t :\{\}();\]+" $inf(str) s
131 set off [string length $s]
132 set inf(str) [string trim [string range $inf(str) $off end]]
136 # lex-expect: move pointer and expect token $t
137 proc lex-expect {t} {
140 if {[string compare $t $type]} {
141 asnError "Got $type '$val', expected $t"
145 # lex-name-move: see if token is $name; moves pointer and returns
146 # 1 if it is; returns 0 otherwise.
147 proc lex-name-move {name} {
149 if {![string compare $type n] && ![string compare $val $name]} {
156 # asnError: Report error and die
157 proc asnError {msg} {
160 puts "Error in line $inf(lineno) in module $inf(module)"
166 # asnWarning: Report warning and return
167 proc asnWarning {msg} {
170 puts "Warning in line $inf(lineno) in module $inf(module)"
174 # asnEnum: parses enumerated list - { name1 (n), name2 (n), ... }
175 # Uses $name as prefix. If there really is a list, $lx holds the C
176 # preprocessor definitions on return; otherwise lx isn't set.
177 proc asnEnum {name lx} {
180 if {[string compare $type \{]} return
183 set pq [asnName $name]
184 set id [lindex $pq 0]
187 lappend l "#define $inf(dprefix)$id $val"
190 if {[string compare $type ,]} break
192 if {[string compare $type \}]} {
193 asnError "Missing \} in enum list got $type '$val'"
198 # asnMod: parses tag and modifier.
199 # $xtag and $ximplicit holds tag and implicit-indication on return.
200 # $xtag is empty if no tag was specified. $ximplicit is 1 on implicit
201 # tagging; 0 otherwise.
202 proc asnMod {xtag ximplicit xtagtype} {
206 upvar $ximplicit implicit
207 upvar $xtagtype tagtype
211 if {![string compare $type \[]} {
212 if {[regexp {^([a-zA-Z]+)[ ]+([0-9]+)$} $val x tagtype tag]} {
213 set tagtype ODR_$tagtype
214 } elseif {[regexp {^([0-9]+)$} $val x tag]} {
215 set tagtype ODR_CONTEXT
217 asnError "bad tag specification: $val"
221 set implicit $inf(implicit-tags)
222 if {![string compare $type n]} {
223 if {![string compare $val EXPLICIT]} {
226 } elseif {![string compare $val IMPLICIT]} {
233 # asnName: moves pointer and expects name. Returns C-validated name.
234 proc asnName {name} {
237 if {[info exists inf(membermap,$inf(module),$name,$val)]} {
238 set nval $inf(membermap,$inf(module),$name,$val)
240 puts " mapping member $name,$val to $nval"
245 if {![string match {[A-Z]*} $val]} {
249 return [join [split $nval -] _]
252 # asnOptional: parses optional modifier. Returns 1 if OPTIONAL was
253 # specified; 0 otherwise.
254 proc asnOptional {} {
256 if {[lex-name-move OPTIONAL]} {
258 } elseif {[lex-name-move DEFAULT]} {
265 # asnSizeConstraint: parses the optional SizeConstraint.
266 # Currently not used for anything.
267 proc asnSizeConstraint {} {
269 if {[lex-name-move SIZE]} {
274 # asnSubtypeSpec: parses the SubtypeSpec ...
275 # Currently not used for anything. We now it's balanced however, i.e.
277 proc asnSubtypeSpec {} {
280 if {[string compare $type "("]} {
286 if {![string compare $type "("]} {
288 } elseif {![string compare $type ")"]} {
295 # asnType: parses ASN.1 type.
296 # On entry $name should hold the name we are currently defining.
297 # Returns type indicator:
298 # SequenceOf SEQUENCE OF
303 # Simple Basic types.
304 # In this casecalling procedure's $tname variable is a list holding:
305 # {C-Function C-Type} if the type is IMPORTed or ODR defined.
307 # {C-Function C-Type 1} if the type should be defined in this module
308 proc asnType {name} {
313 if {[string compare $type n]} {
314 asnError "Expects type specifier, but got $type"
321 if {[lex-name-move OF]} {
331 if {[lex-name-move OF]} {
344 if {[string length [info commands asnBasic$v]]} {
345 set tname [asnBasic$v]
347 if {[info exists inf(map,$inf(module),$v)]} {
348 set v $inf(map,$inf(module),$v)
350 if {[info exists inf(imports,$v)]} {
351 set tname $inf(imports,$v)
353 set w [join [split $v -] _]
354 set tname [list $inf(fprefix)$w $inf(vprefix)$w 1]
357 if {[lex-name-move DEFINED]} {
358 if {[lex-name-move BY]} {
366 proc mapName {name} {
368 if {[info exists inf(map,$inf(module),$name)]} {
369 set name $inf(map,$inf(module),$name)
371 puts -nonewline " $name ($inf(lineno))"
372 puts " mapping to $name"
376 puts " $name ($inf(lineno))"
382 # asnDef: parses type definition (top-level) and generates C code
383 # On entry $name holds the type we are defining.
387 set name [mapName $name]
388 if {[info exist inf(defined,$inf(fprefix)$name)]} {
389 incr inf(definedl,$name)
390 if {$inf(verbose) > 1} {
391 puts "set map($inf(module),$name) $name$inf(definedl,$name)"
394 set inf(definedl,$name) 0
396 set mname [join [split $name -] _]
397 asnMod tag implicit tagtype
398 set t [asnType $mname]
399 asnSub $mname $t $tname $tag $implicit $tagtype
403 # asnSub: parses type and generates C-code
405 # $name holds the type we are defining.
406 # $t is the type returned by the asnType procedure.
407 # $tname is the $tname set by the asnType procedure.
408 # $tag is the tag as returned by asnMod
409 # $implicit is the implicit indicator as returned by asnMod
410 proc asnSub {name t tname tag implicit tagtype} {
414 set defname defined,$inf(fprefix)$name
415 if {[info exist inf($defname)]} {
416 asnWarning "$name already defined in line $inf($defname)"
419 set inf($defname) $inf(lineno)
421 Sequence { set l [asnSequence $name $tag $implicit $tagtype] }
422 SequenceOf { set l [asnOf $name $tag $implicit $tagtype 0] }
423 SetOf { set l [asnOf $name $tag $implicit $tagtype 1] }
424 Choice { set l [asnChoice $name $tag $implicit $tagtype] }
425 Simple { set l [asnSimple $name $tname $tag $implicit $tagtype] }
426 default { asnError "switch asnType case not handled" }
431 puts $file(outc) "int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name)"
433 puts $file(outc) [lindex $l 0]
436 set fdef "$inf(cprefix)int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name);"
439 set decl "typedef [lindex $l 1] $inf(vprefix)$name;"
440 if {![string compare [lindex $tname 2] 1]} {
441 if {![info exist inf(defined,[lindex $tname 0])]} {
445 set inf(var,$inf(nodef)) [join [lindex $l 2] \n]
449 set decl "typedef struct $inf(vprefix)$name $inf(vprefix)$name;"
450 set inf(var,$inf(nodef)) "[lindex $l 1];"
456 puts $file(outh) $decl
457 puts $file(outh) $fdef
458 asnForwardTypes $name
460 lappend inf(forward,code,[lindex $tname 0]) {} $decl $fdef
461 lappend inf(forward,ref,[lindex $tname 0]) $name
465 proc asnForwardTypes {name} {
468 if {![info exists inf(forward,code,$inf(fprefix)$name)]} {
471 foreach r $inf(forward,code,$inf(fprefix)$name) {
474 unset inf(forward,code,$inf(fprefix)$name)
476 while {[info exists inf(forward,ref,$inf(fprefix)$name)]} {
477 set n $inf(forward,ref,$inf(fprefix)$name)
478 set m [lrange $n 1 end]
480 set inf(forward,ref,$inf(fprefix)$name) $m
482 unset inf(forward,ref,$inf(fprefix)$name)
484 asnForwardTypes [lindex $n 0]
488 # asnSimple: parses simple type definition and generates C code
490 # $name is the name we are defining
491 # $tname is the tname as returned by asnType
492 # $tag is the tag as returned by asnMod
493 # $implicit is the implicit indicator as returned by asnMod
496 # Note: Doesn't take care of enum lists yet.
497 proc asnSimple {name tname tag implicit tagtype} {
500 set j "[lindex $tname 1] "
502 if {[info exists inf(unionmap,$inf(module),$name)]} {
503 set uName $inf(unionmap,$inf(module),$name)
509 if {![string length $tag]} {
510 set l "\treturn [lindex $tname 0] (o, p, opt, name);"
511 } elseif {$implicit} {
513 "\treturn odr_implicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);"
516 "\treturn odr_explicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);" \
518 if {[info exists jj]} {
519 return [list $l $j $jj]
525 # asnSequence: parses "SEQUENCE { s-list }" and generates C code.
527 # $name is the type we are defining
532 proc asnSequence {name tag implicit tagtype} {
535 lappend j "struct $inf(vprefix)$name \{"
538 if {![string length $tag]} {
539 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), name))"
540 lappend l "\t\treturn opt && odr_ok (o);"
541 } elseif {$implicit} {
542 lappend l "\tif (!odr_implicit_settag (o, $tagtype, $tag) ||"
543 lappend l "\t\t!odr_sequence_begin (o, p, sizeof(**p), name))"
544 lappend l "\t\treturn opt && odr_ok(o);"
546 lappend l "\tif (!odr_constructed_begin (o, p, $tagtype, $tag, name)"
547 lappend l "\t\treturn opt && odr_ok(o);"
548 lappend l "\tif (o->direction == ODR_DECODE)"
549 lappend l "\t\t*p = odr_malloc (o, sizeof(**p));"
551 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), 0))"
553 lappend l "\t\t*p = 0;"
554 lappend l "\t\treturn 0;"
559 set p [lindex [asnName $name] 0]
560 asnMod ltag limplicit ltagtype
564 if {[info exists inf(unionmap,$inf(module),$name,$p)]} {
565 set uName $inf(unionmap,$inf(module),$name,$p)
568 if {![string compare $t Simple]} {
569 if {[string compare $uName { }]} {
575 set opt [asnOptional]
576 if {![string length $ltag]} {
577 lappend l "\t\t[lindex $tname 0](o, &(*p)->$p, $opt, \"$p\") &&"
578 } elseif {$limplicit} {
579 lappend l "\t\todr_implicit_tag (o, [lindex $tname 0],"
580 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
582 lappend l "\t\todr_explicit_tag (o, [lindex $tname 0],"
583 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
585 set dec "\t[lindex $tname 1] *$p;"
586 } elseif {![string compare $t SequenceOf] && [string length $uName] &&\
587 (![string length $ltag] || $limplicit)} {
590 if {[llength $uName] < 2} {
591 set uName [list num_$p $p]
593 if {[string length $ltag]} {
597 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
602 set tmpa "odr_sequence_of(o, (Odr_fun) [lindex $tname 0], &(*p)->$p,"
603 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
604 lappend j "\tint [lindex $uName 0];"
605 set dec "\t[lindex $tname 1] **[lindex $uName 1];"
608 set subName [mapName ${name}_$level]
609 asnSub $subName $u {} {} 0 {}
611 set tmpa "odr_sequence_of(o, (Odr_fun) $inf(fprefix)$subName, &(*p)->$p,"
612 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
613 lappend j "\tint [lindex $uName 0];"
614 set dec "\t$inf(vprefix)$subName **[lindex $uName 1];"
618 set opt [asnOptional]
620 lappend l "\t\t($tmpa"
621 lappend l "\t\t $tmpb || odr_ok(o)) &&"
623 lappend l "\t\t$tmpa"
624 lappend l "\t\t $tmpb &&"
626 } elseif {!$nchoice && ![string compare $t Choice] && \
627 [string length $uName]} {
628 if {[llength $uName] < 3} {
629 set uName [list which u $name]
632 lappend j "\tint [lindex $uName 0];"
633 lappend j "\tunion \{"
634 lappend v "\tstatic Odr_arm arm\[\] = \{"
635 asnArm $name [lindex $uName 2] v j
637 set dec "\t\} [lindex $uName 1];"
638 set opt [asnOptional]
641 if {[string length $ltag]} {
643 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
645 asnWarning "optional handling missing in CHOICE in SEQUENCE"
646 asnWarning " set unionmap($inf(module),$name,$p) to {}"
654 lappend l "\t\t${la}odr_constructed_begin (o, &(*p)->[lindex $uName 1], $ltagtype, $ltag, \"$p\") &&"
659 set ob " || odr_ok(o))"
662 lappend l "\t\t${oa}odr_choice (o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], 0)${ob} &&"
663 if {[string length $ltag]} {
666 set lb ") || odr_ok(o))"
670 lappend l "\t\todr_constructed_end (o)${lb} &&"
674 set subName [mapName ${name}_$level]
675 asnSub $subName $t {} {} 0 {}
676 set opt [asnOptional]
677 if {![string length $ltag]} {
678 lappend l "\t\t$inf(fprefix)${subName} (o, &(*p)->$p, $opt, \"$p\") &&"
679 } elseif {$limplicit} {
680 lappend l "\t\todr_implicit_tag (o, $inf(fprefix)${subName},"
681 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
683 lappend l "\t\todr_explicit_tag (o, $inf(fprefix)${subName},"
684 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
686 set dec "\t$inf(vprefix)${subName} *$p;"
690 lappend j "$dec /* OPT */"
694 if {[string compare $type ,]} break
697 if {[string length $tag] && !$implicit} {
698 lappend l "\t\todr_sequence_end (o) &&"
699 lappend l "\t\todr_constructed_end (o);"
701 lappend l "\t\todr_sequence_end (o);"
703 if {[string compare $type \}]} {
704 asnError "Missing \} got $type '$val'"
707 if {[info exists v]} {
710 return [list [join $l \n] [join $j \n]]
713 # asnOf: parses "SEQUENCE/SET OF type" and generates C code.
715 # $name is the type we are defining
720 proc asnOf {name tag implicit tagtype isset} {
726 set func odr_sequence_of
729 if {[info exists inf(unionmap,$inf(module),$name)]} {
730 set numName $inf(unionmap,$inf(module),$name)
732 set numName {num elements}
735 lappend j "struct $inf(vprefix)$name \{"
736 lappend j "\tint [lindex $numName 0];"
738 lappend l "\tif (!odr_initmember (o, p, sizeof(**p)))"
739 lappend l "\t\treturn opt && odr_ok(o);"
740 if {[string length $tag]} {
742 lappend l "\todr_implicit_settag (o, $tagtype, $tag);"
744 asnWarning "Constructed SEQUENCE/SET OF not handled"
747 set t [asnType $name]
751 lappend l "\tif ($func (o, (Odr_fun) [lindex $tname 0], &(*p)->[lindex $numName 1],"
752 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
753 lappend j "\t[lindex $tname 1] **[lindex $numName 1];"
756 set subName [mapName ${name}_s]
757 lappend l "\tif ($func (o, (Odr_fun) $inf(fprefix)$subName, &(*p)->[lindex $numName 1],"
758 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
759 lappend j "\t$inf(vprefix)$subName **[lindex $numName 1];"
760 asnSub $subName $t {} {} 0 {}
764 lappend l "\t\treturn 1;"
765 lappend l "\t*p = 0;"
766 lappend l "\treturn opt && odr_ok(o);"
767 return [list [join $l \n] [join $j \n]]
770 # asnArm: parses c-list in choice
771 proc asnArm {name defname lx jx} {
777 set pq [asnName $name]
780 if {![string length $q]} {
784 asnMod ltag limplicit ltagtype
787 lappend enums "$inf(dprefix)$p"
788 if {![string compare $t Simple]} {
790 if {![string length $ltag]} {
791 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
792 lappend l "\t\t (Odr_fun) [lindex $tname 0], \"$q\"\},"
793 } elseif {$limplicit} {
794 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
795 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
797 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
798 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
800 lappend j "\t\t[lindex $tname 1] *$q;"
802 set subName [mapName ${name}_$q]
803 if {![string compare $inf(dprefix)${name}_$q \
804 $inf(vprefix)$subName]} {
805 set po [string toupper [string index $q 0]][string \
807 set subName [mapName ${name}${po}]
809 asnSub $subName $t $tname {} 0 {}
810 if {![string length $ltag]} {
811 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
812 lappend l "\t\t (Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
813 } elseif {$limplicit} {
814 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
815 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
817 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
818 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
820 lappend j "\t\t$inf(vprefix)$subName *$q;"
822 if {[string compare $type ,]} break
824 if {[string compare $type \}]} {
825 asnError "Missing \} got $type '$val'"
830 lappend j "#define $e $level"
833 lappend l "\t\t\{-1, -1, -1, -1, (Odr_fun) 0, 0\}"
836 # asnChoice: parses "CHOICE {c-list}" and generates C code.
838 # $name is the type we are defining
843 proc asnChoice {name tag implicit tagtype} {
846 if {[info exists inf(unionmap,$inf(module),$name)]} {
847 set uName $inf(unionmap,$inf(module),$name)
849 set uName [list which u $name]
852 lappend j "struct $inf(vprefix)$name \{"
853 lappend j "\tint [lindex $uName 0];"
854 lappend j "\tunion \{"
855 lappend l "\tstatic Odr_arm arm\[\] = \{"
856 asnArm $name [lindex $uName 2] l j
857 lappend j "\t\} [lindex $uName 1];"
860 if {![string length $tag]} {
861 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
862 lappend l "\t\treturn opt && odr_ok(o);"
863 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
864 } elseif {$implicit} {
865 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
866 lappend l "\t\treturn opt && odr_ok(o);"
867 lappend l "\todr_implicit_settag(o, $tagtype, $tag);"
868 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
870 lappend l "\tif (!*p && o->direction != ODR_DECODE)"
871 lappend l "\t\treturn opt;"
872 lappend l "\tif (!odr_constructed_begin(o, p, $tagtype, $tag, 0))"
873 lappend l "\t\treturn opt && odr_ok(o);"
874 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
875 lappend l "\t\treturn opt && odr_ok(o);"
876 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name) &&"
877 lappend l "\t\todr_constructed_end(o))"
879 lappend l "\t\treturn 1;"
880 lappend l "\t*p = 0;"
881 lappend l "\treturn opt && odr_ok(o);"
882 return [list [join $l \n] [join $j \n]]
885 # asnImports: parses i-list in "IMPORTS {i-list}"
886 # On return inf(import,..)-array is updated.
887 # inf(import,"module") is a list of {C-handler, C-type} elements.
888 # The {C-handler, C-type} is compatible with the $tname as is used by the
889 # asnType procedure to solve external references.
891 global type val inf file
894 if {[string compare $type n]} {
895 asnError "Missing name in IMPORTS list"
899 if {![string compare $type n] && ![string compare $val FROM]} {
902 if {[info exists inf(filename,$val)]} {
903 set fname $inf(filename,$val)
907 puts $file(outh) "\#include \"${fname}.h\""
909 if {[info exists inf(prefix,$val)]} {
910 set prefix $inf(prefix,$val)
912 set prefix $inf(prefix)
915 set m [join [split $n -] _]
916 set inf(imports,$n) [list [lindex $prefix 0]$m \
917 [lindex $prefix 1]$m]
921 if {[string compare $type n]} break
922 } elseif {![string compare $type ,]} {
926 if {[string compare $type \;]} {
927 asnError "Missing ; after IMPORTS list - got $type '$val'"
932 # asnExports: parses e-list in "EXPORTS {e-list}"
933 # This function does nothing with elements in the list.
938 if {[string compare $type n]} {
939 asnError "Missing name in EXPORTS list"
941 set inf(exports,$val) 1
943 if {[string compare $type ,]} break
946 if {[string compare $type \;]} {
947 asnError "Missing ; after EXPORTS list - got $type ($val)"
952 # asnModuleBody: parses a module specification and generates C code.
953 # Exports lists, imports lists, and type definitions are handled;
954 # other things are silently ignored.
955 proc asnModuleBody {} {
956 global type val file inf
958 if {[info exists inf(prefix,$inf(module))]} {
959 set prefix $inf(prefix,$inf(module))
961 set prefix $inf(prefix)
963 set inf(fprefix) [lindex $prefix 0]
964 set inf(vprefix) [lindex $prefix 1]
965 set inf(dprefix) [lindex $prefix 2]
966 if {[llength $prefix] > 3} {
967 set inf(cprefix) [lindex $prefix 3]
969 set inf(cprefix) {YAZ_EXPORT }
973 puts "Module $inf(module), $inf(lineno)"
977 if {[info exists inf(init,$inf(module),c)]} {
978 puts $file(outc) $inf(init,$inf(module),c)
980 if {[info exists inf(init,$inf(module),h)]} {
981 puts $file(outh) "\#ifdef __cplusplus"
982 puts $file(outh) "extern \"C\" \{"
983 puts $file(outh) "\#endif"
985 puts $file(outh) $inf(init,$inf(module),h)
987 if {[info exists inf(init,$inf(module),p)]} {
988 puts $file(outp) $inf(init,$inf(module),p)
991 while {[string length $type]} {
992 if {[string compare $type n]} {
996 if {![string compare $val END]} {
998 } elseif {![string compare $val EXPORTS]} {
1001 } elseif {![string compare $val IMPORTS]} {
1003 puts $file(outh) "\#ifdef __cplusplus"
1004 puts $file(outh) "\}"
1005 puts $file(outh) "\#endif"
1012 puts $file(outh) "\#ifdef __cplusplus"
1013 puts $file(outh) "extern \"C\" \{"
1014 puts $file(outh) "\#endif"
1017 set inf(asndef) $inf(nodef)
1020 if {![string compare $type :]} {
1024 } elseif {![string compare $type n]} {
1026 if {[string length $type]} {
1033 puts $file(outh) "\#ifdef __cplusplus"
1034 puts $file(outh) "\}"
1035 puts $file(outh) "\#endif"
1038 foreach x [array names inf imports,*] {
1043 # asnTagDefault: parses TagDefault section
1044 proc asnTagDefault {} {
1045 global type val inf file
1047 set inf(implicit-tags) 0
1048 while {[string length $type]} {
1049 if {[lex-name-move EXPLICIT]} {
1051 set inf(implicit-tags) 0
1052 } elseif {[lex-name-move IMPLICIT]} {
1054 set inf(implicit-tags) 1
1061 # asnModules: parses a collection of module specifications.
1062 # Depending on the module pattern, $inf(moduleP), a module is either
1063 # skipped or processed.
1064 proc asnModules {} {
1065 global type val inf file yc_version
1070 while {![string compare $type n]} {
1071 set inf(module) $val
1072 if {[info exists inf(moduleP)] && ![string match $inf(moduleP) $val]} {
1073 if {$inf(verbose)} {
1076 while {![lex-name-move END]} {
1083 while {![lex-name-move DEFINITIONS]} {
1085 if {![string length $type]} return
1089 if {[info exists inf(filename,$inf(module))]} {
1090 set fname $inf(filename,$inf(module))
1092 set fname $inf(module)
1094 set ppname [join [split $fname -] _]
1096 if {![info exists inf(c-file)]} {
1097 set inf(c-file) ${fname}.c
1099 set file(outc) [open $inf(c-file) w]
1101 if {![info exists inf(h-file)]} {
1102 set inf(h-file) ${fname}.h
1104 set file(outh) [open $inf(h-file) w]
1106 if {![info exists inf(p-file)]} {
1107 set inf(p-file) ${fname}-p.h
1109 set file(outp) [open $inf(p-file) w]
1111 set md [clock format [clock seconds]]
1113 puts $file(outc) "/* YC ${yc_version} $md */"
1114 puts $file(outc) "/* Module-C: $inf(module) */"
1117 puts $file(outh) "/* YC ${yc_version}: $md */"
1118 puts $file(outh) "/* Module-H $inf(module) */"
1121 puts $file(outp) "/* YC ${yc_version}: $md */"
1122 puts $file(outp) "/* Module-P: $inf(module) */"
1125 puts $file(outc) "\#include \"$inf(p-file)\""
1127 puts $file(outh) "\#ifndef ${ppname}_H"
1128 puts $file(outh) "\#define ${ppname}_H"
1130 puts $file(outh) "\#include <odr.h>"
1132 puts $file(outp) "\#ifndef ${ppname}_P_H"
1133 puts $file(outp) "\#define ${ppname}_P_H"
1135 puts $file(outp) "\#include \"$inf(h-file)\""
1138 puts $file(outp) "\#ifdef __cplusplus"
1139 puts $file(outp) "extern \"C\" \{"
1140 puts $file(outp) "\#endif"
1143 if {[string compare $type :]} {
1144 asnError "::= expected got $type '$val'"
1147 if {![lex-name-move BEGIN]} {
1148 asnError "BEGIN expected"
1153 if {[info exists file(outp)]} {
1158 for {set i 1} {$i < $inf(nodef)} {incr i} {
1159 puts $f $inf(var,$i)
1160 if {[info exists inf(asn,$i)]} {
1163 foreach comment $inf(asn,$i) {
1173 puts $file(outp) "\#ifdef __cplusplus"
1174 puts $file(outp) "\}"
1175 puts $file(outp) "\#endif"
1177 if {[info exists inf(body,$inf(module),h)]} {
1178 puts $file(outh) $inf(body,$inf(module),h)
1180 if {[info exists inf(body,$inf(module),c)]} {
1181 puts $file(outc) $inf(body,$inf(module),c)
1183 if {[info exists inf(body,$inf(module),p)]} {
1184 puts $file(outp) $inf(body,$inf(module),p)
1186 puts $file(outh) "\#endif"
1187 puts $file(outp) "\#endif"
1188 foreach f [array names file] {
1198 # asnFile: parses an ASN.1 specification file as specified in $inf(iname).
1202 if {$inf(verbose) > 1} {
1203 puts "Reading ASN.1 file $inf(iname)"
1207 set inf(inf) [open $inf(iname) r]
1213 # The following procedures are invoked by the asnType function.
1214 # Each procedure takes the form: asnBasic<TYPE> and they must return
1215 # two elements: the C function handler and the C type.
1216 # On entry upvar $name is the type we are defining and global, $inf(module), is
1217 # the current module name.
1219 proc asnBasicEXTERNAL {} {
1220 return {odr_external {Odr_external}}
1223 proc asnBasicINTEGER {} {
1224 return {odr_integer {int}}
1227 proc asnBasicENUMERATED {} {
1228 return {odr_enum {int}}
1231 proc asnBasicNULL {} {
1232 return {odr_null {Odr_null}}
1235 proc asnBasicBOOLEAN {} {
1236 return {odr_bool {bool_t}}
1239 proc asnBasicOCTET {} {
1241 lex-name-move STRING
1242 return {odr_octetstring {Odr_oct}}
1245 proc asnBasicBIT {} {
1247 lex-name-move STRING
1248 return {odr_bitstring {Odr_bitmask}}
1251 proc asnBasicOBJECT {} {
1253 lex-name-move IDENTIFIER
1254 return {odr_oid {Odr_oid}}
1257 proc asnBasicANY {} {
1260 return [list $inf(fprefix)ANY_$name void]
1263 # userDef: reads user definitions file $name
1264 proc userDef {name} {
1267 if {$inf(verbose) > 1} {
1268 puts "Reading definitions file $name"
1272 if {[info exists default-prefix]} {
1273 set inf(prefix) ${default-prefix}
1275 foreach m [array names prefix] {
1276 set inf(prefix,$m) $prefix($m)
1278 foreach m [array names body] {
1279 set inf(body,$m) $body($m)
1281 foreach m [array names init] {
1282 set inf(init,$m) $init($m)
1284 foreach m [array names filename] {
1285 set inf(filename,$m) $filename($m)
1287 foreach m [array names map] {
1288 set inf(map,$m) $map($m)
1290 foreach m [array names membermap] {
1291 set inf(membermap,$m) $membermap($m)
1293 foreach m [array names unionmap] {
1294 set inf(unionmap,$m) $unionmap($m)
1299 set inf(prefix) {yc_ Yc_ YC_}
1301 # Parse command line
1302 set l [llength $argv]
1305 set arg [lindex $argv $i]
1306 switch -glob -- $arg {
1311 set p [string range $arg 2 end]
1312 if {![string length $p]} {
1313 set p [lindex $argv [incr i]]
1318 set p [string range $arg 2 end]
1319 if {![string length $p]} {
1320 set p [lindex $argv [incr i]]
1325 set p [string range $arg 2 end]
1326 if {![string length $p]} {
1327 set p [lindex $argv [incr i]]
1332 set p [string range $arg 2 end]
1333 if {![string length $p]} {
1334 set p [lindex $argv [incr i]]
1339 set p [string range $arg 2 end]
1340 if {![string length $p]} {
1341 set p [lindex $argv [incr i]]
1346 set p [string range $arg 2 end]
1347 if {![string length $p]} {
1348 set p [lindex $argv [incr i]]
1350 if {[llength $p] == 1} {
1351 set inf(prefix) [list [string tolower $p] \
1352 [string toupper $p] [string toupper $p]]
1353 } elseif {[llength $p] == 3} {
1367 if {![info exists inf(iname)]} {
1368 puts "YAZ ODR Compiler ${yc_version}"
1369 puts -nonewline "Usage: ${argv0}"
1370 puts { [-v] [-c cfile] [-h hfile] [-p hfile] [-d dfile] }
1371 puts { [-x prefix] [-m module] file}