2 # the next line restarts using tclsh \
5 # YC: ASN.1 Compiler for YAZ
6 # (c) Index Data 1996-2000
7 # See the file LICENSE for details.
10 # Revision 1.6 2000-02-10 13:44:02 adam
11 # Tcl command clock not used if unavailable (Tcl7.4 and earlier).
13 # Revision 1.5 2000/01/15 09:18:42 adam
14 # Bug fix: some elements where treated as OPTIONAL when they shouldn't.
16 # Revision 1.4 1999/12/16 23:36:19 adam
17 # Implemented ILL protocol. Minor updates ASN.1 compiler.
19 # Revision 1.3 1999/11/30 13:47:12 adam
20 # Improved installation. Moved header files to include/yaz.
22 # Revision 1.2 1999/06/09 09:43:11 adam
23 # Added option -I and variable h-path to specify path for header files.
25 # Revision 1.1 1999/06/08 10:10:16 adam
26 # New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
28 # Revision 1.8 1999/04/20 10:37:04 adam
29 # Updated for ODR - added name parameter.
31 # Revision 1.7 1998/04/03 14:44:20 adam
34 # Revision 1.6 1998/04/03 13:21:17 adam
37 # Revision 1.5 1998/04/03 12:48:17 adam
38 # Fixed bug: missed handling of constructed tags for CHOICE.
40 # Revision 1.4 1998/03/31 15:47:45 adam
41 # First compiled ASN.1 code for YAZ.
43 # Revision 1.3 1998/03/23 17:13:20 adam
44 # Implemented SET OF and ENUM. The Compiler now eats ILL (ISO10161) and
47 # Revision 1.2 1997/10/07 10:31:01 adam
48 # Added facility to specify tag type (CONTEXT, APPLICATION, ...).
50 # Revision 1.1.1.1 1996/10/31 14:04:40 adam
51 # First version of the compiler for YAZ.
57 # Syntax for the ASN.1 supported:
60 # module -> name skip DEFINITIONS ::= mbody END
61 # mbody -> EXPORTS { nlist }
62 # | IMPORTS { imlist }
66 # type -> SEQUENCE { sqlist }
77 # sqlist -> sqlist , name tmt opt
79 # chlist -> chlist , name tmt
81 # enlist -> enlist , name (n)
83 # imlist -> nlist FROM name
84 # imlist nlist FROM name
87 # mod -> IMPLICIT | EXPLICIT | e
88 # tag -> [tagtype n] | [n] | e
91 # name identifier/token
93 # skip one token skipped
95 # tagtype APPLICATION, CONTEXT, etc.
97 # lex: moves input file pointer and returns type of token.
98 # The globals $type and $val are set. $val holds name if token
99 # is normal identifier name.
100 # sets global var type to one of:
102 # \{ left curly brace
103 # \} right curly brace
112 while {![string length $inf(str)]} {
114 set inf(cnt) [gets $inf(inf) inf(str)]
119 lappend inf(asn,$inf(asndef)) $inf(str)
120 set l [string first -- $inf(str)]
123 set inf(str) [string range $inf(str) 0 $l]
125 set inf(str) [string trim $inf(str)]
127 set s [string index $inf(str) 0]
137 \[ { regexp {^\[[ ]*(.+)[ ]*\]} $inf(str) s val }
138 : { regexp {^::=} $inf(str) s }
140 regexp "^\[^,\t :\{\}();\]+" $inf(str) s
145 set off [string length $s]
146 set inf(str) [string trim [string range $inf(str) $off end]]
150 # lex-expect: move pointer and expect token $t
151 proc lex-expect {t} {
154 if {[string compare $t $type]} {
155 asnError "Got $type '$val', expected $t"
159 # lex-name-move: see if token is $name; moves pointer and returns
160 # 1 if it is; returns 0 otherwise.
161 proc lex-name-move {name} {
163 if {![string compare $type n] && ![string compare $val $name]} {
170 # asnError: Report error and die
171 proc asnError {msg} {
174 puts "Error in line $inf(lineno) in module $inf(module)"
180 # asnWarning: Report warning and return
181 proc asnWarning {msg} {
184 puts "Warning in line $inf(lineno) in module $inf(module)"
188 # asnEnum: parses enumerated list - { name1 (n), name2 (n), ... }
189 # Uses $name as prefix. If there really is a list, $lx holds the C
190 # preprocessor definitions on return; otherwise lx isn't set.
191 proc asnEnum {name lx} {
194 if {[string compare $type \{]} return
197 set pq [asnName $name]
198 set id [lindex $pq 0]
201 lappend l "#define $inf(dprefix)$id $val"
204 if {[string compare $type ,]} break
206 if {[string compare $type \}]} {
207 asnError "Missing \} in enum list got $type '$val'"
212 # asnMod: parses tag and modifier.
213 # $xtag and $ximplicit holds tag and implicit-indication on return.
214 # $xtag is empty if no tag was specified. $ximplicit is 1 on implicit
215 # tagging; 0 otherwise.
216 proc asnMod {xtag ximplicit xtagtype} {
220 upvar $ximplicit implicit
221 upvar $xtagtype tagtype
225 if {![string compare $type \[]} {
226 if {[regexp {^([a-zA-Z]+)[ ]+([0-9]+)$} $val x tagtype tag]} {
227 set tagtype ODR_$tagtype
228 } elseif {[regexp {^([0-9]+)$} $val x tag]} {
229 set tagtype ODR_CONTEXT
231 asnError "bad tag specification: $val"
235 set implicit $inf(implicit-tags)
236 if {![string compare $type n]} {
237 if {![string compare $val EXPLICIT]} {
240 } elseif {![string compare $val IMPLICIT]} {
247 # asnName: moves pointer and expects name. Returns C-validated name.
248 proc asnName {name} {
251 if {[info exists inf(membermap,$inf(module),$name,$val)]} {
252 set nval $inf(membermap,$inf(module),$name,$val)
254 puts " mapping member $name,$val to $nval"
259 if {![string match {[A-Z]*} $val]} {
263 return [join [split $nval -] _]
266 # asnOptional: parses optional modifier. Returns 1 if OPTIONAL was
267 # specified; 0 otherwise.
268 proc asnOptional {} {
270 if {[lex-name-move OPTIONAL]} {
272 } elseif {[lex-name-move DEFAULT]} {
279 # asnSizeConstraint: parses the optional SizeConstraint.
280 # Currently not used for anything.
281 proc asnSizeConstraint {} {
283 if {[lex-name-move SIZE]} {
288 # asnSubtypeSpec: parses the SubtypeSpec ...
289 # Currently not used for anything. We now it's balanced however, i.e.
291 proc asnSubtypeSpec {} {
294 if {[string compare $type "("]} {
300 if {![string compare $type "("]} {
302 } elseif {![string compare $type ")"]} {
309 # asnType: parses ASN.1 type.
310 # On entry $name should hold the name we are currently defining.
311 # Returns type indicator:
312 # SequenceOf SEQUENCE OF
317 # Simple Basic types.
318 # In this casecalling procedure's $tname variable is a list holding:
319 # {C-Function C-Type} if the type is IMPORTed or ODR defined.
321 # {C-Function C-Type 1} if the type should be defined in this module
322 proc asnType {name} {
327 if {[string compare $type n]} {
328 asnError "Expects type specifier, but got $type"
335 if {[lex-name-move OF]} {
345 if {[lex-name-move OF]} {
358 if {[string length [info commands asnBasic$v]]} {
359 set tname [asnBasic$v]
361 if {[info exists inf(map,$inf(module),$v)]} {
362 set v $inf(map,$inf(module),$v)
364 if {[info exists inf(imports,$v)]} {
365 set tname $inf(imports,$v)
367 set w [join [split $v -] _]
368 set tname [list $inf(fprefix)$w $inf(vprefix)$w 1]
371 if {[lex-name-move DEFINED]} {
372 if {[lex-name-move BY]} {
380 proc mapName {name} {
382 if {[info exists inf(map,$inf(module),$name)]} {
383 set name $inf(map,$inf(module),$name)
385 puts -nonewline " $name ($inf(lineno))"
386 puts " mapping to $name"
390 puts " $name ($inf(lineno))"
396 # asnDef: parses type definition (top-level) and generates C code
397 # On entry $name holds the type we are defining.
401 set name [mapName $name]
402 if {[info exist inf(defined,$inf(fprefix)$name)]} {
403 incr inf(definedl,$name)
404 if {$inf(verbose) > 1} {
405 puts "set map($inf(module),$name) $name$inf(definedl,$name)"
408 set inf(definedl,$name) 0
410 set mname [join [split $name -] _]
411 asnMod tag implicit tagtype
412 set t [asnType $mname]
413 asnSub $mname $t $tname $tag $implicit $tagtype
417 # asnSub: parses type and generates C-code
419 # $name holds the type we are defining.
420 # $t is the type returned by the asnType procedure.
421 # $tname is the $tname set by the asnType procedure.
422 # $tag is the tag as returned by asnMod
423 # $implicit is the implicit indicator as returned by asnMod
424 proc asnSub {name t tname tag implicit tagtype} {
428 set defname defined,$inf(fprefix)$name
429 if {[info exist inf($defname)]} {
430 asnWarning "$name already defined in line $inf($defname)"
433 set inf($defname) $inf(lineno)
435 Sequence { set l [asnSequence $name $tag $implicit $tagtype] }
436 SequenceOf { set l [asnOf $name $tag $implicit $tagtype 0] }
437 SetOf { set l [asnOf $name $tag $implicit $tagtype 1] }
438 Choice { set l [asnChoice $name $tag $implicit $tagtype] }
439 Simple { set l [asnSimple $name $tname $tag $implicit $tagtype] }
440 default { asnError "switch asnType case not handled" }
445 puts $file(outc) "int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name)"
447 puts $file(outc) [lindex $l 0]
450 set fdef "$inf(cprefix)int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name);"
453 set decl "typedef [lindex $l 1] $inf(vprefix)$name;"
454 if {![string compare [lindex $tname 2] 1]} {
455 if {![info exist inf(defined,[lindex $tname 0])]} {
459 set inf(var,$inf(nodef)) [join [lindex $l 2] \n]
463 set decl "typedef struct $inf(vprefix)$name $inf(vprefix)$name;"
464 set inf(var,$inf(nodef)) "[lindex $l 1];"
470 puts $file(outh) $decl
471 puts $file(outh) $fdef
472 asnForwardTypes $name
474 lappend inf(forward,code,[lindex $tname 0]) {} $decl $fdef
475 lappend inf(forward,ref,[lindex $tname 0]) $name
479 proc asnForwardTypes {name} {
482 if {![info exists inf(forward,code,$inf(fprefix)$name)]} {
485 foreach r $inf(forward,code,$inf(fprefix)$name) {
488 unset inf(forward,code,$inf(fprefix)$name)
490 while {[info exists inf(forward,ref,$inf(fprefix)$name)]} {
491 set n $inf(forward,ref,$inf(fprefix)$name)
492 set m [lrange $n 1 end]
494 set inf(forward,ref,$inf(fprefix)$name) $m
496 unset inf(forward,ref,$inf(fprefix)$name)
498 asnForwardTypes [lindex $n 0]
502 # asnSimple: parses simple type definition and generates C code
504 # $name is the name we are defining
505 # $tname is the tname as returned by asnType
506 # $tag is the tag as returned by asnMod
507 # $implicit is the implicit indicator as returned by asnMod
510 # Note: Doesn't take care of enum lists yet.
511 proc asnSimple {name tname tag implicit tagtype} {
514 set j "[lindex $tname 1] "
516 if {[info exists inf(unionmap,$inf(module),$name)]} {
517 set uName $inf(unionmap,$inf(module),$name)
523 if {![string length $tag]} {
524 set l "\treturn [lindex $tname 0] (o, p, opt, name);"
525 } elseif {$implicit} {
527 "\treturn odr_implicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);"
530 "\treturn odr_explicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);" \
532 if {[info exists jj]} {
533 return [list $l $j $jj]
539 # asnSequence: parses "SEQUENCE { s-list }" and generates C code.
541 # $name is the type we are defining
546 proc asnSequence {name tag implicit tagtype} {
549 lappend j "struct $inf(vprefix)$name \{"
552 if {![string length $tag]} {
553 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), name))"
554 lappend l "\t\treturn opt && odr_ok (o);"
555 } elseif {$implicit} {
556 lappend l "\tif (!odr_implicit_settag (o, $tagtype, $tag) ||"
557 lappend l "\t\t!odr_sequence_begin (o, p, sizeof(**p), name))"
558 lappend l "\t\treturn opt && odr_ok(o);"
560 lappend l "\tif (!odr_constructed_begin (o, p, $tagtype, $tag, name))"
561 lappend l "\t\treturn opt && odr_ok(o);"
562 lappend l "\tif (o->direction == ODR_DECODE)"
563 lappend l "\t\t*p = odr_malloc (o, sizeof(**p));"
565 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), 0))"
567 lappend l "\t\t*p = 0;"
568 lappend l "\t\treturn 0;"
573 set p [lindex [asnName $name] 0]
574 asnMod ltag limplicit ltagtype
578 if {[info exists inf(unionmap,$inf(module),$name,$p)]} {
579 set uName $inf(unionmap,$inf(module),$name,$p)
582 if {![string compare $t Simple]} {
583 if {[string compare $uName { }]} {
589 set opt [asnOptional]
590 if {![string length $ltag]} {
591 lappend l "\t\t[lindex $tname 0](o, &(*p)->$p, $opt, \"$p\") &&"
592 } elseif {$limplicit} {
593 lappend l "\t\todr_implicit_tag (o, [lindex $tname 0],"
594 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
596 lappend l "\t\todr_explicit_tag (o, [lindex $tname 0],"
597 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
599 set dec "\t[lindex $tname 1] *$p;"
600 } elseif {![string compare $t SequenceOf] && [string length $uName] &&\
601 (![string length $ltag] || $limplicit)} {
604 if {[llength $uName] < 2} {
605 set uName [list num_$p $p]
607 if {[string length $ltag]} {
611 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
616 set tmpa "odr_sequence_of(o, (Odr_fun) [lindex $tname 0], &(*p)->$p,"
617 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
618 lappend j "\tint [lindex $uName 0];"
619 set dec "\t[lindex $tname 1] **[lindex $uName 1];"
622 set subName [mapName ${name}_$level]
623 asnSub $subName $u {} {} 0 {}
625 set tmpa "odr_sequence_of(o, (Odr_fun) $inf(fprefix)$subName, &(*p)->$p,"
626 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
627 lappend j "\tint [lindex $uName 0];"
628 set dec "\t$inf(vprefix)$subName **[lindex $uName 1];"
632 set opt [asnOptional]
634 lappend l "\t\t($tmpa"
635 lappend l "\t\t $tmpb || odr_ok(o)) &&"
637 lappend l "\t\t$tmpa"
638 lappend l "\t\t $tmpb &&"
640 } elseif {!$nchoice && ![string compare $t Choice] && \
641 [string length $uName]} {
642 if {[llength $uName] < 3} {
643 set uName [list which u $name]
646 lappend j "\tint [lindex $uName 0];"
647 lappend j "\tunion \{"
648 lappend v "\tstatic Odr_arm arm\[\] = \{"
649 asnArm $name [lindex $uName 2] v j
651 set dec "\t\} [lindex $uName 1];"
652 set opt [asnOptional]
655 if {[string length $ltag]} {
657 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
659 asnWarning "optional handling missing in CHOICE in SEQUENCE"
660 asnWarning " set unionmap($inf(module),$name,$p) to {}"
668 lappend l "\t\t${la}odr_constructed_begin (o, &(*p)->[lindex $uName 1], $ltagtype, $ltag, \"$p\") &&"
673 set ob " || odr_ok(o))"
676 lappend l "\t\t${oa}odr_choice (o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], 0)${ob} &&"
677 if {[string length $ltag]} {
680 set lb ") || odr_ok(o))"
684 lappend l "\t\todr_constructed_end (o)${lb} &&"
688 set subName [mapName ${name}_$level]
689 asnSub $subName $t {} {} 0 {}
690 set opt [asnOptional]
691 if {![string length $ltag]} {
692 lappend l "\t\t$inf(fprefix)${subName} (o, &(*p)->$p, $opt, \"$p\") &&"
693 } elseif {$limplicit} {
694 lappend l "\t\todr_implicit_tag (o, $inf(fprefix)${subName},"
695 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
697 lappend l "\t\todr_explicit_tag (o, $inf(fprefix)${subName},"
698 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
700 set dec "\t$inf(vprefix)${subName} *$p;"
704 lappend j "$dec /* OPT */"
708 if {[string compare $type ,]} break
711 if {[string length $tag] && !$implicit} {
712 lappend l "\t\todr_sequence_end (o) &&"
713 lappend l "\t\todr_constructed_end (o);"
715 lappend l "\t\todr_sequence_end (o);"
717 if {[string compare $type \}]} {
718 asnError "Missing \} got $type '$val'"
721 if {[info exists v]} {
724 return [list [join $l \n] [join $j \n]]
727 # asnOf: parses "SEQUENCE/SET OF type" and generates C code.
729 # $name is the type we are defining
734 proc asnOf {name tag implicit tagtype isset} {
740 set func odr_sequence_of
743 if {[info exists inf(unionmap,$inf(module),$name)]} {
744 set numName $inf(unionmap,$inf(module),$name)
746 set numName {num elements}
749 lappend j "struct $inf(vprefix)$name \{"
750 lappend j "\tint [lindex $numName 0];"
752 lappend l "\tif (!odr_initmember (o, p, sizeof(**p)))"
753 lappend l "\t\treturn opt && odr_ok(o);"
754 if {[string length $tag]} {
756 lappend l "\todr_implicit_settag (o, $tagtype, $tag);"
758 asnWarning "Constructed SEQUENCE/SET OF not handled"
761 set t [asnType $name]
765 lappend l "\tif ($func (o, (Odr_fun) [lindex $tname 0], &(*p)->[lindex $numName 1],"
766 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
767 lappend j "\t[lindex $tname 1] **[lindex $numName 1];"
770 set subName [mapName ${name}_s]
771 lappend l "\tif ($func (o, (Odr_fun) $inf(fprefix)$subName, &(*p)->[lindex $numName 1],"
772 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
773 lappend j "\t$inf(vprefix)$subName **[lindex $numName 1];"
774 asnSub $subName $t {} {} 0 {}
778 lappend l "\t\treturn 1;"
779 lappend l "\t*p = 0;"
780 lappend l "\treturn opt && odr_ok(o);"
781 return [list [join $l \n] [join $j \n]]
784 # asnArm: parses c-list in choice
785 proc asnArm {name defname lx jx} {
791 set pq [asnName $name]
794 if {![string length $q]} {
798 asnMod ltag limplicit ltagtype
801 lappend enums "$inf(dprefix)$p"
802 if {![string compare $t Simple]} {
804 if {![string length $ltag]} {
805 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
806 lappend l "\t\t (Odr_fun) [lindex $tname 0], \"$q\"\},"
807 } elseif {$limplicit} {
808 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
809 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
811 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
812 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
814 lappend j "\t\t[lindex $tname 1] *$q;"
816 set subName [mapName ${name}_$q]
817 if {![string compare $inf(dprefix)${name}_$q \
818 $inf(vprefix)$subName]} {
819 set po [string toupper [string index $q 0]][string \
821 set subName [mapName ${name}${po}]
823 asnSub $subName $t $tname {} 0 {}
824 if {![string length $ltag]} {
825 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
826 lappend l "\t\t (Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
827 } elseif {$limplicit} {
828 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
829 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
831 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
832 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
834 lappend j "\t\t$inf(vprefix)$subName *$q;"
836 if {[string compare $type ,]} break
838 if {[string compare $type \}]} {
839 asnError "Missing \} got $type '$val'"
844 lappend j "#define $e $level"
847 lappend l "\t\t\{-1, -1, -1, -1, (Odr_fun) 0, 0\}"
850 # asnChoice: parses "CHOICE {c-list}" and generates C code.
852 # $name is the type we are defining
857 proc asnChoice {name tag implicit tagtype} {
860 if {[info exists inf(unionmap,$inf(module),$name)]} {
861 set uName $inf(unionmap,$inf(module),$name)
863 set uName [list which u $name]
866 lappend j "struct $inf(vprefix)$name \{"
867 lappend j "\tint [lindex $uName 0];"
868 lappend j "\tunion \{"
869 lappend l "\tstatic Odr_arm arm\[\] = \{"
870 asnArm $name [lindex $uName 2] l j
871 lappend j "\t\} [lindex $uName 1];"
874 if {![string length $tag]} {
875 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
876 lappend l "\t\treturn opt && odr_ok(o);"
877 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
878 } elseif {$implicit} {
879 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
880 lappend l "\t\treturn opt && odr_ok(o);"
881 lappend l "\todr_implicit_settag(o, $tagtype, $tag);"
882 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
884 lappend l "\tif (!*p && o->direction != ODR_DECODE)"
885 lappend l "\t\treturn opt;"
886 lappend l "\tif (!odr_constructed_begin(o, p, $tagtype, $tag, 0))"
887 lappend l "\t\treturn opt && odr_ok(o);"
888 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
889 lappend l "\t\treturn opt && odr_ok(o);"
890 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name) &&"
891 lappend l "\t\todr_constructed_end(o))"
893 lappend l "\t\treturn 1;"
894 lappend l "\t*p = 0;"
895 lappend l "\treturn opt && odr_ok(o);"
896 return [list [join $l \n] [join $j \n]]
899 # asnImports: parses i-list in "IMPORTS {i-list}"
900 # On return inf(import,..)-array is updated.
901 # inf(import,"module") is a list of {C-handler, C-type} elements.
902 # The {C-handler, C-type} is compatible with the $tname as is used by the
903 # asnType procedure to solve external references.
905 global type val inf file
908 if {[string compare $type n]} {
909 asnError "Missing name in IMPORTS list"
913 if {![string compare $type n] && ![string compare $val FROM]} {
916 if {[info exists inf(filename,$val)]} {
917 set fname $inf(filename,$val)
921 puts $file(outh) "\#include <$inf(h-dir)${fname}.h>"
923 if {[info exists inf(prefix,$val)]} {
924 set prefix $inf(prefix,$val)
926 set prefix $inf(prefix)
929 if {[info exists inf(map,$val,$n)]} {
930 set v $inf(map,$val,$n)
934 set w [join [split $v -] _]
935 set inf(imports,$n) [list [lindex $prefix 0]$w \
936 [lindex $prefix 1]$w]
940 if {[string compare $type n]} break
941 } elseif {![string compare $type ,]} {
945 if {[string compare $type \;]} {
946 asnError "Missing ; after IMPORTS list - got $type '$val'"
951 # asnExports: parses e-list in "EXPORTS {e-list}"
952 # This function does nothing with elements in the list.
957 if {[string compare $type n]} {
958 asnError "Missing name in EXPORTS list"
960 set inf(exports,$val) 1
962 if {[string compare $type ,]} break
965 if {[string compare $type \;]} {
966 asnError "Missing ; after EXPORTS list - got $type ($val)"
971 # asnModuleBody: parses a module specification and generates C code.
972 # Exports lists, imports lists, and type definitions are handled;
973 # other things are silently ignored.
974 proc asnModuleBody {} {
975 global type val file inf
977 if {[info exists inf(prefix,$inf(module))]} {
978 set prefix $inf(prefix,$inf(module))
980 set prefix $inf(prefix)
982 set inf(fprefix) [lindex $prefix 0]
983 set inf(vprefix) [lindex $prefix 1]
984 set inf(dprefix) [lindex $prefix 2]
985 if {[llength $prefix] > 3} {
986 set inf(cprefix) [lindex $prefix 3]
988 set inf(cprefix) {YAZ_EXPORT }
992 puts "Module $inf(module), $inf(lineno)"
996 if {[info exists inf(init,$inf(module),c)]} {
997 puts $file(outc) $inf(init,$inf(module),c)
999 if {[info exists inf(init,$inf(module),h)]} {
1000 puts $file(outh) "\#ifdef __cplusplus"
1001 puts $file(outh) "extern \"C\" \{"
1002 puts $file(outh) "\#endif"
1004 puts $file(outh) $inf(init,$inf(module),h)
1006 if {[info exists inf(init,$inf(module),p)]} {
1007 puts $file(outp) $inf(init,$inf(module),p)
1010 while {[string length $type]} {
1011 if {[string compare $type n]} {
1015 if {![string compare $val END]} {
1017 } elseif {![string compare $val EXPORTS]} {
1020 } elseif {![string compare $val IMPORTS]} {
1022 puts $file(outh) "\#ifdef __cplusplus"
1023 puts $file(outh) "\}"
1024 puts $file(outh) "\#endif"
1031 puts $file(outh) "\#ifdef __cplusplus"
1032 puts $file(outh) "extern \"C\" \{"
1033 puts $file(outh) "\#endif"
1036 set inf(asndef) $inf(nodef)
1039 if {![string compare $type :]} {
1043 } elseif {![string compare $type n]} {
1045 if {[string length $type]} {
1052 puts $file(outh) "\#ifdef __cplusplus"
1053 puts $file(outh) "\}"
1054 puts $file(outh) "\#endif"
1057 foreach x [array names inf imports,*] {
1062 # asnTagDefault: parses TagDefault section
1063 proc asnTagDefault {} {
1064 global type val inf file
1066 set inf(implicit-tags) 0
1067 while {[string length $type]} {
1068 if {[lex-name-move EXPLICIT]} {
1070 set inf(implicit-tags) 0
1071 } elseif {[lex-name-move IMPLICIT]} {
1073 set inf(implicit-tags) 1
1080 # asnModules: parses a collection of module specifications.
1081 # Depending on the module pattern, $inf(moduleP), a module is either
1082 # skipped or processed.
1083 proc asnModules {} {
1084 global type val inf file yc_version
1089 while {![string compare $type n]} {
1090 set inf(module) $val
1091 if {[info exists inf(moduleP)] && ![string match $inf(moduleP) $val]} {
1092 if {$inf(verbose)} {
1095 while {![lex-name-move END]} {
1102 while {![lex-name-move DEFINITIONS]} {
1104 if {![string length $type]} return
1106 if {[info exists inf(filename,$inf(module))]} {
1107 set fname $inf(filename,$inf(module))
1109 set fname $inf(module)
1111 set ppname [join [split $fname -] _]
1113 if {![info exists inf(c-file)]} {
1114 set inf(c-file) ${fname}.c
1116 set file(outc) [open $inf(c-file) w]
1118 if {![info exists inf(h-file)]} {
1119 set inf(h-file) ${fname}.h
1121 set file(outh) [open $inf(h-path)/$inf(h-dir)$inf(h-file) w]
1124 if {![info exists inf(p-file)]} {
1125 set inf(p-file) ${fname}-p.h
1127 set file(outp) [open $inf(h-path)/$inf(h-dir)$inf(p-file) w]
1131 catch {set md [clock format [clock seconds]]}
1133 puts $file(outc) "/* YC ${yc_version} $md */"
1134 puts $file(outc) "/* Module-C: $inf(module) */"
1137 puts $file(outh) "/* YC ${yc_version}: $md */"
1138 puts $file(outh) "/* Module-H $inf(module) */"
1141 if {[info exists file(outp)]} {
1142 puts $file(outp) "/* YC ${yc_version}: $md */"
1143 puts $file(outp) "/* Module-P: $inf(module) */"
1147 if {[info exists inf(p-file)]} {
1148 puts $file(outc) "\#include <$inf(h-dir)$inf(p-file)>"
1150 puts $file(outc) "\#include <$inf(h-dir)$inf(h-file)>"
1152 puts $file(outh) "\#ifndef ${ppname}_H"
1153 puts $file(outh) "\#define ${ppname}_H"
1155 puts $file(outh) "\#include <$inf(h-dir)odr.h>"
1157 if {[info exists file(outp)]} {
1158 puts $file(outp) "\#ifndef ${ppname}_P_H"
1159 puts $file(outp) "\#define ${ppname}_P_H"
1161 puts $file(outp) "\#include <$inf(h-dir)$inf(h-file)>"
1166 if {[string compare $type :]} {
1167 asnError "::= expected got $type '$val'"
1170 if {![lex-name-move BEGIN]} {
1171 asnError "BEGIN expected"
1176 if {[info exists file(outp)]} {
1181 puts $f "\#ifdef __cplusplus"
1182 puts $f "extern \"C\" \{"
1184 for {set i 1} {$i < $inf(nodef)} {incr i} {
1185 puts $f $inf(var,$i)
1186 if {[info exists inf(asn,$i)]} {
1189 foreach comment $inf(asn,$i) {
1199 puts $f "\#ifdef __cplusplus"
1203 if {[info exists inf(body,$inf(module),h)]} {
1204 puts $file(outh) $inf(body,$inf(module),h)
1206 if {[info exists inf(body,$inf(module),c)]} {
1207 puts $file(outc) $inf(body,$inf(module),c)
1209 if {[info exists inf(body,$inf(module),p)]} {
1210 if {[info exists file(outp)]} {
1211 puts $file(outp) $inf(body,$inf(module),p)
1214 puts $file(outh) "\#endif"
1215 if {[info exists file(outp)]} {
1216 puts $file(outp) "\#endif"
1218 foreach f [array names file] {
1223 catch {unset inf(p-file)}
1228 # asnFile: parses an ASN.1 specification file as specified in $inf(iname).
1232 if {$inf(verbose) > 1} {
1233 puts "Reading ASN.1 file $inf(iname)"
1237 set inf(inf) [open $inf(iname) r]
1243 # The following procedures are invoked by the asnType function.
1244 # Each procedure takes the form: asnBasic<TYPE> and they must return
1245 # two elements: the C function handler and the C type.
1246 # On entry upvar $name is the type we are defining and global, $inf(module), is
1247 # the current module name.
1249 proc asnBasicEXTERNAL {} {
1250 return {odr_external {Odr_external}}
1253 proc asnBasicINTEGER {} {
1254 return {odr_integer {int}}
1257 proc asnBasicENUMERATED {} {
1258 return {odr_enum {int}}
1261 proc asnBasicNULL {} {
1262 return {odr_null {Odr_null}}
1265 proc asnBasicBOOLEAN {} {
1266 return {odr_bool {bool_t}}
1269 proc asnBasicOCTET {} {
1271 lex-name-move STRING
1272 return {odr_octetstring {Odr_oct}}
1275 proc asnBasicBIT {} {
1277 lex-name-move STRING
1278 return {odr_bitstring {Odr_bitmask}}
1281 proc asnBasicOBJECT {} {
1283 lex-name-move IDENTIFIER
1284 return {odr_oid {Odr_oid}}
1287 proc asnBasicGeneralString {} {
1288 return {odr_generalstring char}
1291 proc asnBasicVisibleString {} {
1292 return {odr_visiblestring char}
1295 proc asnBasicGeneralizedTime {} {
1296 return {odr_generalizedtime char}
1299 proc asnBasicANY {} {
1302 return [list $inf(fprefix)ANY_$name void]
1305 # userDef: reads user definitions file $name
1306 proc userDef {name} {
1309 if {$inf(verbose) > 1} {
1310 puts "Reading definitions file $name"
1314 if {[info exists default-prefix]} {
1315 set inf(prefix) ${default-prefix}
1317 if {[info exists h-path]} {
1318 set inf(h-path) ${h-path}
1320 foreach m [array names prefix] {
1321 set inf(prefix,$m) $prefix($m)
1323 foreach m [array names body] {
1324 set inf(body,$m) $body($m)
1326 foreach m [array names init] {
1327 set inf(init,$m) $init($m)
1329 foreach m [array names filename] {
1330 set inf(filename,$m) $filename($m)
1332 foreach m [array names map] {
1333 set inf(map,$m) $map($m)
1335 foreach m [array names membermap] {
1336 set inf(membermap,$m) $membermap($m)
1338 foreach m [array names unionmap] {
1339 set inf(unionmap,$m) $unionmap($m)
1344 set inf(prefix) {yc_ Yc_ YC_}
1348 # Parse command line
1349 set l [llength $argv]
1352 set arg [lindex $argv $i]
1353 switch -glob -- $arg {
1358 set p [string range $arg 2 end]
1359 if {![string length $p]} {
1360 set p [lindex $argv [incr i]]
1365 set p [string range $arg 2 end]
1366 if {![string length $p]} {
1367 set p [lindex $argv [incr i]]
1372 set p [string range $arg 2 end]
1373 if {![string length $p]} {
1374 set p [lindex $argv [incr i]]
1376 set inf(h-dir) [string trim $p \\/]/
1379 set p [string range $arg 2 end]
1380 if {![string length $p]} {
1381 set p [lindex $argv [incr i]]
1386 set p [string range $arg 2 end]
1387 if {![string length $p]} {
1388 set p [lindex $argv [incr i]]
1393 set p [string range $arg 2 end]
1394 if {![string length $p]} {
1395 set p [lindex $argv [incr i]]
1400 set p [string range $arg 2 end]
1401 if {![string length $p]} {
1402 set p [lindex $argv [incr i]]
1407 set p [string range $arg 2 end]
1408 if {![string length $p]} {
1409 set p [lindex $argv [incr i]]
1411 if {[llength $p] == 1} {
1412 set inf(prefix) [list [string tolower $p] \
1413 [string toupper $p] [string toupper $p]]
1414 } elseif {[llength $p] == 3} {
1428 if {![info exists inf(iname)]} {
1429 puts "YAZ ASN.1 Compiler ${yc_version}"
1430 puts -nonewline "Usage: ${argv0}"
1431 puts { [-v] [-c cfile] [-h hfile] [-p hfile] [-d dfile] [-I path]}
1432 puts { [-x prefix] [-m module] file}