2 # the next line restarts using tclsh \
5 # yaz-comp: ASN.1 Compiler for YAZ
6 # (c) Index Data 1996-2000
7 # See the file LICENSE for details.
10 # Revision 1.1 2000-03-02 08:48:20 adam
11 # Renamed ASN.1 compiler to yaz-comp (used to be yc.tcl).
13 # Revision 1.6 2000/02/10 13:44:02 adam
14 # Tcl command clock not used if unavailable (Tcl7.4 and earlier).
16 # Revision 1.5 2000/01/15 09:18:42 adam
17 # Bug fix: some elements where treated as OPTIONAL when they shouldn't.
19 # Revision 1.4 1999/12/16 23:36:19 adam
20 # Implemented ILL protocol. Minor updates ASN.1 compiler.
22 # Revision 1.3 1999/11/30 13:47:12 adam
23 # Improved installation. Moved header files to include/yaz.
25 # Revision 1.2 1999/06/09 09:43:11 adam
26 # Added option -I and variable h-path to specify path for header files.
28 # Revision 1.1 1999/06/08 10:10:16 adam
29 # New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
31 # Revision 1.8 1999/04/20 10:37:04 adam
32 # Updated for ODR - added name parameter.
34 # Revision 1.7 1998/04/03 14:44:20 adam
37 # Revision 1.6 1998/04/03 13:21:17 adam
40 # Revision 1.5 1998/04/03 12:48:17 adam
41 # Fixed bug: missed handling of constructed tags for CHOICE.
43 # Revision 1.4 1998/03/31 15:47:45 adam
44 # First compiled ASN.1 code for YAZ.
46 # Revision 1.3 1998/03/23 17:13:20 adam
47 # Implemented SET OF and ENUM. The Compiler now eats ILL (ISO10161) and
50 # Revision 1.2 1997/10/07 10:31:01 adam
51 # Added facility to specify tag type (CONTEXT, APPLICATION, ...).
53 # Revision 1.1.1.1 1996/10/31 14:04:40 adam
54 # First version of the compiler for YAZ.
60 # Syntax for the ASN.1 supported:
63 # module -> name skip DEFINITIONS ::= mbody END
64 # mbody -> EXPORTS { nlist }
65 # | IMPORTS { imlist }
69 # type -> SEQUENCE { sqlist }
80 # sqlist -> sqlist , name tmt opt
82 # chlist -> chlist , name tmt
84 # enlist -> enlist , name (n)
86 # imlist -> nlist FROM name
87 # imlist nlist FROM name
90 # mod -> IMPLICIT | EXPLICIT | e
91 # tag -> [tagtype n] | [n] | e
94 # name identifier/token
96 # skip one token skipped
98 # tagtype APPLICATION, CONTEXT, etc.
100 # lex: moves input file pointer and returns type of token.
101 # The globals $type and $val are set. $val holds name if token
102 # is normal identifier name.
103 # sets global var type to one of:
105 # \{ left curly brace
106 # \} right curly brace
115 while {![string length $inf(str)]} {
117 set inf(cnt) [gets $inf(inf) inf(str)]
122 lappend inf(asn,$inf(asndef)) $inf(str)
123 set l [string first -- $inf(str)]
126 set inf(str) [string range $inf(str) 0 $l]
128 set inf(str) [string trim $inf(str)]
130 set s [string index $inf(str) 0]
140 \[ { regexp {^\[[ ]*(.+)[ ]*\]} $inf(str) s val }
141 : { regexp {^::=} $inf(str) s }
143 regexp "^\[^,\t :\{\}();\]+" $inf(str) s
148 set off [string length $s]
149 set inf(str) [string trim [string range $inf(str) $off end]]
153 # lex-expect: move pointer and expect token $t
154 proc lex-expect {t} {
157 if {[string compare $t $type]} {
158 asnError "Got $type '$val', expected $t"
162 # lex-name-move: see if token is $name; moves pointer and returns
163 # 1 if it is; returns 0 otherwise.
164 proc lex-name-move {name} {
166 if {![string compare $type n] && ![string compare $val $name]} {
173 # asnError: Report error and die
174 proc asnError {msg} {
177 puts "Error in line $inf(lineno) in module $inf(module)"
183 # asnWarning: Report warning and return
184 proc asnWarning {msg} {
187 puts "Warning in line $inf(lineno) in module $inf(module)"
191 # asnEnum: parses enumerated list - { name1 (n), name2 (n), ... }
192 # Uses $name as prefix. If there really is a list, $lx holds the C
193 # preprocessor definitions on return; otherwise lx isn't set.
194 proc asnEnum {name lx} {
197 if {[string compare $type \{]} return
200 set pq [asnName $name]
201 set id [lindex $pq 0]
204 lappend l "#define $inf(dprefix)$id $val"
207 if {[string compare $type ,]} break
209 if {[string compare $type \}]} {
210 asnError "Missing \} in enum list got $type '$val'"
215 # asnMod: parses tag and modifier.
216 # $xtag and $ximplicit holds tag and implicit-indication on return.
217 # $xtag is empty if no tag was specified. $ximplicit is 1 on implicit
218 # tagging; 0 otherwise.
219 proc asnMod {xtag ximplicit xtagtype} {
223 upvar $ximplicit implicit
224 upvar $xtagtype tagtype
228 if {![string compare $type \[]} {
229 if {[regexp {^([a-zA-Z]+)[ ]+([0-9]+)$} $val x tagtype tag]} {
230 set tagtype ODR_$tagtype
231 } elseif {[regexp {^([0-9]+)$} $val x tag]} {
232 set tagtype ODR_CONTEXT
234 asnError "bad tag specification: $val"
238 set implicit $inf(implicit-tags)
239 if {![string compare $type n]} {
240 if {![string compare $val EXPLICIT]} {
243 } elseif {![string compare $val IMPLICIT]} {
250 # asnName: moves pointer and expects name. Returns C-validated name.
251 proc asnName {name} {
254 if {[info exists inf(membermap,$inf(module),$name,$val)]} {
255 set nval $inf(membermap,$inf(module),$name,$val)
257 puts " mapping member $name,$val to $nval"
262 if {![string match {[A-Z]*} $val]} {
266 return [join [split $nval -] _]
269 # asnOptional: parses optional modifier. Returns 1 if OPTIONAL was
270 # specified; 0 otherwise.
271 proc asnOptional {} {
273 if {[lex-name-move OPTIONAL]} {
275 } elseif {[lex-name-move DEFAULT]} {
282 # asnSizeConstraint: parses the optional SizeConstraint.
283 # Currently not used for anything.
284 proc asnSizeConstraint {} {
286 if {[lex-name-move SIZE]} {
291 # asnSubtypeSpec: parses the SubtypeSpec ...
292 # Currently not used for anything. We now it's balanced however, i.e.
294 proc asnSubtypeSpec {} {
297 if {[string compare $type "("]} {
303 if {![string compare $type "("]} {
305 } elseif {![string compare $type ")"]} {
312 # asnType: parses ASN.1 type.
313 # On entry $name should hold the name we are currently defining.
314 # Returns type indicator:
315 # SequenceOf SEQUENCE OF
320 # Simple Basic types.
321 # In this casecalling procedure's $tname variable is a list holding:
322 # {C-Function C-Type} if the type is IMPORTed or ODR defined.
324 # {C-Function C-Type 1} if the type should be defined in this module
325 proc asnType {name} {
330 if {[string compare $type n]} {
331 asnError "Expects type specifier, but got $type"
338 if {[lex-name-move OF]} {
348 if {[lex-name-move OF]} {
361 if {[string length [info commands asnBasic$v]]} {
362 set tname [asnBasic$v]
364 if {[info exists inf(map,$inf(module),$v)]} {
365 set v $inf(map,$inf(module),$v)
367 if {[info exists inf(imports,$v)]} {
368 set tname $inf(imports,$v)
370 set w [join [split $v -] _]
371 set tname [list $inf(fprefix)$w $inf(vprefix)$w 1]
374 if {[lex-name-move DEFINED]} {
375 if {[lex-name-move BY]} {
383 proc mapName {name} {
385 if {[info exists inf(map,$inf(module),$name)]} {
386 set name $inf(map,$inf(module),$name)
388 puts -nonewline " $name ($inf(lineno))"
389 puts " mapping to $name"
393 puts " $name ($inf(lineno))"
399 # asnDef: parses type definition (top-level) and generates C code
400 # On entry $name holds the type we are defining.
404 set name [mapName $name]
405 if {[info exist inf(defined,$inf(fprefix)$name)]} {
406 incr inf(definedl,$name)
407 if {$inf(verbose) > 1} {
408 puts "set map($inf(module),$name) $name$inf(definedl,$name)"
411 set inf(definedl,$name) 0
413 set mname [join [split $name -] _]
414 asnMod tag implicit tagtype
415 set t [asnType $mname]
416 asnSub $mname $t $tname $tag $implicit $tagtype
420 # asnSub: parses type and generates C-code
422 # $name holds the type we are defining.
423 # $t is the type returned by the asnType procedure.
424 # $tname is the $tname set by the asnType procedure.
425 # $tag is the tag as returned by asnMod
426 # $implicit is the implicit indicator as returned by asnMod
427 proc asnSub {name t tname tag implicit tagtype} {
431 set defname defined,$inf(fprefix)$name
432 if {[info exist inf($defname)]} {
433 asnWarning "$name already defined in line $inf($defname)"
436 set inf($defname) $inf(lineno)
438 Sequence { set l [asnSequence $name $tag $implicit $tagtype] }
439 SequenceOf { set l [asnOf $name $tag $implicit $tagtype 0] }
440 SetOf { set l [asnOf $name $tag $implicit $tagtype 1] }
441 Choice { set l [asnChoice $name $tag $implicit $tagtype] }
442 Simple { set l [asnSimple $name $tname $tag $implicit $tagtype] }
443 default { asnError "switch asnType case not handled" }
448 puts $file(outc) "int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name)"
450 puts $file(outc) [lindex $l 0]
453 set fdef "$inf(cprefix)int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name);"
456 set decl "typedef [lindex $l 1] $inf(vprefix)$name;"
457 if {![string compare [lindex $tname 2] 1]} {
458 if {![info exist inf(defined,[lindex $tname 0])]} {
462 set inf(var,$inf(nodef)) [join [lindex $l 2] \n]
466 set decl "typedef struct $inf(vprefix)$name $inf(vprefix)$name;"
467 set inf(var,$inf(nodef)) "[lindex $l 1];"
473 puts $file(outh) $decl
474 puts $file(outh) $fdef
475 asnForwardTypes $name
477 lappend inf(forward,code,[lindex $tname 0]) {} $decl $fdef
478 lappend inf(forward,ref,[lindex $tname 0]) $name
482 proc asnForwardTypes {name} {
485 if {![info exists inf(forward,code,$inf(fprefix)$name)]} {
488 foreach r $inf(forward,code,$inf(fprefix)$name) {
491 unset inf(forward,code,$inf(fprefix)$name)
493 while {[info exists inf(forward,ref,$inf(fprefix)$name)]} {
494 set n $inf(forward,ref,$inf(fprefix)$name)
495 set m [lrange $n 1 end]
497 set inf(forward,ref,$inf(fprefix)$name) $m
499 unset inf(forward,ref,$inf(fprefix)$name)
501 asnForwardTypes [lindex $n 0]
505 # asnSimple: parses simple type definition and generates C code
507 # $name is the name we are defining
508 # $tname is the tname as returned by asnType
509 # $tag is the tag as returned by asnMod
510 # $implicit is the implicit indicator as returned by asnMod
513 # Note: Doesn't take care of enum lists yet.
514 proc asnSimple {name tname tag implicit tagtype} {
517 set j "[lindex $tname 1] "
519 if {[info exists inf(unionmap,$inf(module),$name)]} {
520 set uName $inf(unionmap,$inf(module),$name)
526 if {![string length $tag]} {
527 set l "\treturn [lindex $tname 0] (o, p, opt, name);"
528 } elseif {$implicit} {
530 "\treturn odr_implicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);"
533 "\treturn odr_explicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);" \
535 if {[info exists jj]} {
536 return [list $l $j $jj]
542 # asnSequence: parses "SEQUENCE { s-list }" and generates C code.
544 # $name is the type we are defining
549 proc asnSequence {name tag implicit tagtype} {
552 lappend j "struct $inf(vprefix)$name \{"
555 if {![string length $tag]} {
556 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), name))"
557 lappend l "\t\treturn opt && odr_ok (o);"
558 } elseif {$implicit} {
559 lappend l "\tif (!odr_implicit_settag (o, $tagtype, $tag) ||"
560 lappend l "\t\t!odr_sequence_begin (o, p, sizeof(**p), name))"
561 lappend l "\t\treturn opt && odr_ok(o);"
563 lappend l "\tif (!odr_constructed_begin (o, p, $tagtype, $tag, name))"
564 lappend l "\t\treturn opt && odr_ok(o);"
565 lappend l "\tif (o->direction == ODR_DECODE)"
566 lappend l "\t\t*p = odr_malloc (o, sizeof(**p));"
568 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), 0))"
570 lappend l "\t\t*p = 0;"
571 lappend l "\t\treturn 0;"
576 set p [lindex [asnName $name] 0]
577 asnMod ltag limplicit ltagtype
581 if {[info exists inf(unionmap,$inf(module),$name,$p)]} {
582 set uName $inf(unionmap,$inf(module),$name,$p)
585 if {![string compare $t Simple]} {
586 if {[string compare $uName { }]} {
592 set opt [asnOptional]
593 if {![string length $ltag]} {
594 lappend l "\t\t[lindex $tname 0](o, &(*p)->$p, $opt, \"$p\") &&"
595 } elseif {$limplicit} {
596 lappend l "\t\todr_implicit_tag (o, [lindex $tname 0],"
597 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
599 lappend l "\t\todr_explicit_tag (o, [lindex $tname 0],"
600 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
602 set dec "\t[lindex $tname 1] *$p;"
603 } elseif {![string compare $t SequenceOf] && [string length $uName] &&\
604 (![string length $ltag] || $limplicit)} {
607 if {[llength $uName] < 2} {
608 set uName [list num_$p $p]
610 if {[string length $ltag]} {
614 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
619 set tmpa "odr_sequence_of(o, (Odr_fun) [lindex $tname 0], &(*p)->$p,"
620 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
621 lappend j "\tint [lindex $uName 0];"
622 set dec "\t[lindex $tname 1] **[lindex $uName 1];"
625 set subName [mapName ${name}_$level]
626 asnSub $subName $u {} {} 0 {}
628 set tmpa "odr_sequence_of(o, (Odr_fun) $inf(fprefix)$subName, &(*p)->$p,"
629 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
630 lappend j "\tint [lindex $uName 0];"
631 set dec "\t$inf(vprefix)$subName **[lindex $uName 1];"
635 set opt [asnOptional]
637 lappend l "\t\t($tmpa"
638 lappend l "\t\t $tmpb || odr_ok(o)) &&"
640 lappend l "\t\t$tmpa"
641 lappend l "\t\t $tmpb &&"
643 } elseif {!$nchoice && ![string compare $t Choice] && \
644 [string length $uName]} {
645 if {[llength $uName] < 3} {
646 set uName [list which u $name]
649 lappend j "\tint [lindex $uName 0];"
650 lappend j "\tunion \{"
651 lappend v "\tstatic Odr_arm arm\[\] = \{"
652 asnArm $name [lindex $uName 2] v j
654 set dec "\t\} [lindex $uName 1];"
655 set opt [asnOptional]
658 if {[string length $ltag]} {
660 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
662 asnWarning "optional handling missing in CHOICE in SEQUENCE"
663 asnWarning " set unionmap($inf(module),$name,$p) to {}"
671 lappend l "\t\t${la}odr_constructed_begin (o, &(*p)->[lindex $uName 1], $ltagtype, $ltag, \"$p\") &&"
676 set ob " || odr_ok(o))"
679 lappend l "\t\t${oa}odr_choice (o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], 0)${ob} &&"
680 if {[string length $ltag]} {
683 set lb ") || odr_ok(o))"
687 lappend l "\t\todr_constructed_end (o)${lb} &&"
691 set subName [mapName ${name}_$level]
692 asnSub $subName $t {} {} 0 {}
693 set opt [asnOptional]
694 if {![string length $ltag]} {
695 lappend l "\t\t$inf(fprefix)${subName} (o, &(*p)->$p, $opt, \"$p\") &&"
696 } elseif {$limplicit} {
697 lappend l "\t\todr_implicit_tag (o, $inf(fprefix)${subName},"
698 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
700 lappend l "\t\todr_explicit_tag (o, $inf(fprefix)${subName},"
701 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
703 set dec "\t$inf(vprefix)${subName} *$p;"
707 lappend j "$dec /* OPT */"
711 if {[string compare $type ,]} break
714 if {[string length $tag] && !$implicit} {
715 lappend l "\t\todr_sequence_end (o) &&"
716 lappend l "\t\todr_constructed_end (o);"
718 lappend l "\t\todr_sequence_end (o);"
720 if {[string compare $type \}]} {
721 asnError "Missing \} got $type '$val'"
724 if {[info exists v]} {
727 return [list [join $l \n] [join $j \n]]
730 # asnOf: parses "SEQUENCE/SET OF type" and generates C code.
732 # $name is the type we are defining
737 proc asnOf {name tag implicit tagtype isset} {
743 set func odr_sequence_of
746 if {[info exists inf(unionmap,$inf(module),$name)]} {
747 set numName $inf(unionmap,$inf(module),$name)
749 set numName {num elements}
752 lappend j "struct $inf(vprefix)$name \{"
753 lappend j "\tint [lindex $numName 0];"
755 lappend l "\tif (!odr_initmember (o, p, sizeof(**p)))"
756 lappend l "\t\treturn opt && odr_ok(o);"
757 if {[string length $tag]} {
759 lappend l "\todr_implicit_settag (o, $tagtype, $tag);"
761 asnWarning "Constructed SEQUENCE/SET OF not handled"
764 set t [asnType $name]
768 lappend l "\tif ($func (o, (Odr_fun) [lindex $tname 0], &(*p)->[lindex $numName 1],"
769 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
770 lappend j "\t[lindex $tname 1] **[lindex $numName 1];"
773 set subName [mapName ${name}_s]
774 lappend l "\tif ($func (o, (Odr_fun) $inf(fprefix)$subName, &(*p)->[lindex $numName 1],"
775 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
776 lappend j "\t$inf(vprefix)$subName **[lindex $numName 1];"
777 asnSub $subName $t {} {} 0 {}
781 lappend l "\t\treturn 1;"
782 lappend l "\t*p = 0;"
783 lappend l "\treturn opt && odr_ok(o);"
784 return [list [join $l \n] [join $j \n]]
787 # asnArm: parses c-list in choice
788 proc asnArm {name defname lx jx} {
794 set pq [asnName $name]
797 if {![string length $q]} {
801 asnMod ltag limplicit ltagtype
804 lappend enums "$inf(dprefix)$p"
805 if {![string compare $t Simple]} {
807 if {![string length $ltag]} {
808 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
809 lappend l "\t\t (Odr_fun) [lindex $tname 0], \"$q\"\},"
810 } elseif {$limplicit} {
811 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
812 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
814 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
815 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
817 lappend j "\t\t[lindex $tname 1] *$q;"
819 set subName [mapName ${name}_$q]
820 if {![string compare $inf(dprefix)${name}_$q \
821 $inf(vprefix)$subName]} {
822 set po [string toupper [string index $q 0]][string \
824 set subName [mapName ${name}${po}]
826 asnSub $subName $t $tname {} 0 {}
827 if {![string length $ltag]} {
828 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
829 lappend l "\t\t (Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
830 } elseif {$limplicit} {
831 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
832 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
834 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
835 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
837 lappend j "\t\t$inf(vprefix)$subName *$q;"
839 if {[string compare $type ,]} break
841 if {[string compare $type \}]} {
842 asnError "Missing \} got $type '$val'"
847 lappend j "#define $e $level"
850 lappend l "\t\t\{-1, -1, -1, -1, (Odr_fun) 0, 0\}"
853 # asnChoice: parses "CHOICE {c-list}" and generates C code.
855 # $name is the type we are defining
860 proc asnChoice {name tag implicit tagtype} {
863 if {[info exists inf(unionmap,$inf(module),$name)]} {
864 set uName $inf(unionmap,$inf(module),$name)
866 set uName [list which u $name]
869 lappend j "struct $inf(vprefix)$name \{"
870 lappend j "\tint [lindex $uName 0];"
871 lappend j "\tunion \{"
872 lappend l "\tstatic Odr_arm arm\[\] = \{"
873 asnArm $name [lindex $uName 2] l j
874 lappend j "\t\} [lindex $uName 1];"
877 if {![string length $tag]} {
878 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
879 lappend l "\t\treturn opt && odr_ok(o);"
880 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
881 } elseif {$implicit} {
882 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
883 lappend l "\t\treturn opt && odr_ok(o);"
884 lappend l "\todr_implicit_settag(o, $tagtype, $tag);"
885 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
887 lappend l "\tif (!*p && o->direction != ODR_DECODE)"
888 lappend l "\t\treturn opt;"
889 lappend l "\tif (!odr_constructed_begin(o, p, $tagtype, $tag, 0))"
890 lappend l "\t\treturn opt && odr_ok(o);"
891 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
892 lappend l "\t\treturn opt && odr_ok(o);"
893 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name) &&"
894 lappend l "\t\todr_constructed_end(o))"
896 lappend l "\t\treturn 1;"
897 lappend l "\t*p = 0;"
898 lappend l "\treturn opt && odr_ok(o);"
899 return [list [join $l \n] [join $j \n]]
902 # asnImports: parses i-list in "IMPORTS {i-list}"
903 # On return inf(import,..)-array is updated.
904 # inf(import,"module") is a list of {C-handler, C-type} elements.
905 # The {C-handler, C-type} is compatible with the $tname as is used by the
906 # asnType procedure to solve external references.
908 global type val inf file
911 if {[string compare $type n]} {
912 asnError "Missing name in IMPORTS list"
916 if {![string compare $type n] && ![string compare $val FROM]} {
919 if {[info exists inf(filename,$val)]} {
920 set fname $inf(filename,$val)
924 puts $file(outh) "\#include <$inf(h-dir)${fname}.h>"
926 if {[info exists inf(prefix,$val)]} {
927 set prefix $inf(prefix,$val)
929 set prefix $inf(prefix)
932 if {[info exists inf(map,$val,$n)]} {
933 set v $inf(map,$val,$n)
937 set w [join [split $v -] _]
938 set inf(imports,$n) [list [lindex $prefix 0]$w \
939 [lindex $prefix 1]$w]
943 if {[string compare $type n]} break
944 } elseif {![string compare $type ,]} {
948 if {[string compare $type \;]} {
949 asnError "Missing ; after IMPORTS list - got $type '$val'"
954 # asnExports: parses e-list in "EXPORTS {e-list}"
955 # This function does nothing with elements in the list.
960 if {[string compare $type n]} {
961 asnError "Missing name in EXPORTS list"
963 set inf(exports,$val) 1
965 if {[string compare $type ,]} break
968 if {[string compare $type \;]} {
969 asnError "Missing ; after EXPORTS list - got $type ($val)"
974 # asnModuleBody: parses a module specification and generates C code.
975 # Exports lists, imports lists, and type definitions are handled;
976 # other things are silently ignored.
977 proc asnModuleBody {} {
978 global type val file inf
980 if {[info exists inf(prefix,$inf(module))]} {
981 set prefix $inf(prefix,$inf(module))
983 set prefix $inf(prefix)
985 set inf(fprefix) [lindex $prefix 0]
986 set inf(vprefix) [lindex $prefix 1]
987 set inf(dprefix) [lindex $prefix 2]
988 if {[llength $prefix] > 3} {
989 set inf(cprefix) [lindex $prefix 3]
991 set inf(cprefix) {YAZ_EXPORT }
995 puts "Module $inf(module), $inf(lineno)"
999 if {[info exists inf(init,$inf(module),c)]} {
1000 puts $file(outc) $inf(init,$inf(module),c)
1002 if {[info exists inf(init,$inf(module),h)]} {
1003 puts $file(outh) "\#ifdef __cplusplus"
1004 puts $file(outh) "extern \"C\" \{"
1005 puts $file(outh) "\#endif"
1007 puts $file(outh) $inf(init,$inf(module),h)
1009 if {[info exists inf(init,$inf(module),p)]} {
1010 puts $file(outp) $inf(init,$inf(module),p)
1013 while {[string length $type]} {
1014 if {[string compare $type n]} {
1018 if {![string compare $val END]} {
1020 } elseif {![string compare $val EXPORTS]} {
1023 } elseif {![string compare $val IMPORTS]} {
1025 puts $file(outh) "\#ifdef __cplusplus"
1026 puts $file(outh) "\}"
1027 puts $file(outh) "\#endif"
1034 puts $file(outh) "\#ifdef __cplusplus"
1035 puts $file(outh) "extern \"C\" \{"
1036 puts $file(outh) "\#endif"
1039 set inf(asndef) $inf(nodef)
1042 if {![string compare $type :]} {
1046 } elseif {![string compare $type n]} {
1048 if {[string length $type]} {
1055 puts $file(outh) "\#ifdef __cplusplus"
1056 puts $file(outh) "\}"
1057 puts $file(outh) "\#endif"
1060 foreach x [array names inf imports,*] {
1065 # asnTagDefault: parses TagDefault section
1066 proc asnTagDefault {} {
1067 global type val inf file
1069 set inf(implicit-tags) 0
1070 while {[string length $type]} {
1071 if {[lex-name-move EXPLICIT]} {
1073 set inf(implicit-tags) 0
1074 } elseif {[lex-name-move IMPLICIT]} {
1076 set inf(implicit-tags) 1
1083 # asnModules: parses a collection of module specifications.
1084 # Depending on the module pattern, $inf(moduleP), a module is either
1085 # skipped or processed.
1086 proc asnModules {} {
1087 global type val inf file yc_version
1092 while {![string compare $type n]} {
1093 set inf(module) $val
1094 if {[info exists inf(moduleP)] && ![string match $inf(moduleP) $val]} {
1095 if {$inf(verbose)} {
1098 while {![lex-name-move END]} {
1105 while {![lex-name-move DEFINITIONS]} {
1107 if {![string length $type]} return
1109 if {[info exists inf(filename,$inf(module))]} {
1110 set fname $inf(filename,$inf(module))
1112 set fname $inf(module)
1114 set ppname [join [split $fname -] _]
1116 if {![info exists inf(c-file)]} {
1117 set inf(c-file) ${fname}.c
1119 set file(outc) [open $inf(c-file) w]
1121 if {![info exists inf(h-file)]} {
1122 set inf(h-file) ${fname}.h
1124 set file(outh) [open $inf(h-path)/$inf(h-dir)$inf(h-file) w]
1127 if {![info exists inf(p-file)]} {
1128 set inf(p-file) ${fname}-p.h
1130 set file(outp) [open $inf(h-path)/$inf(h-dir)$inf(p-file) w]
1133 set greeting {Generated automatically by the YAZ ASN.1 Compiler}
1135 puts $file(outc) "/* ${greeting} ${yc_version} */"
1136 puts $file(outc) "/* Module-C: $inf(module) */"
1139 puts $file(outh) "/* ${greeting} ${yc_version} */"
1140 puts $file(outh) "/* Module-H $inf(module) */"
1143 if {[info exists file(outp)]} {
1144 puts $file(outp) "/* ${greeting} ${yc_version} */"
1145 puts $file(outp) "/* Module-P: $inf(module) */"
1149 if {[info exists inf(p-file)]} {
1150 puts $file(outc) "\#include <$inf(h-dir)$inf(p-file)>"
1152 puts $file(outc) "\#include <$inf(h-dir)$inf(h-file)>"
1154 puts $file(outh) "\#ifndef ${ppname}_H"
1155 puts $file(outh) "\#define ${ppname}_H"
1157 puts $file(outh) "\#include <$inf(h-dir)odr.h>"
1159 if {[info exists file(outp)]} {
1160 puts $file(outp) "\#ifndef ${ppname}_P_H"
1161 puts $file(outp) "\#define ${ppname}_P_H"
1163 puts $file(outp) "\#include <$inf(h-dir)$inf(h-file)>"
1168 if {[string compare $type :]} {
1169 asnError "::= expected got $type '$val'"
1172 if {![lex-name-move BEGIN]} {
1173 asnError "BEGIN expected"
1178 if {[info exists file(outp)]} {
1183 puts $f "\#ifdef __cplusplus"
1184 puts $f "extern \"C\" \{"
1186 for {set i 1} {$i < $inf(nodef)} {incr i} {
1187 puts $f $inf(var,$i)
1188 if {[info exists inf(asn,$i)]} {
1191 foreach comment $inf(asn,$i) {
1201 puts $f "\#ifdef __cplusplus"
1205 if {[info exists inf(body,$inf(module),h)]} {
1206 puts $file(outh) $inf(body,$inf(module),h)
1208 if {[info exists inf(body,$inf(module),c)]} {
1209 puts $file(outc) $inf(body,$inf(module),c)
1211 if {[info exists inf(body,$inf(module),p)]} {
1212 if {[info exists file(outp)]} {
1213 puts $file(outp) $inf(body,$inf(module),p)
1216 puts $file(outh) "\#endif"
1217 if {[info exists file(outp)]} {
1218 puts $file(outp) "\#endif"
1220 foreach f [array names file] {
1225 catch {unset inf(p-file)}
1230 # asnFile: parses an ASN.1 specification file as specified in $inf(iname).
1234 if {$inf(verbose) > 1} {
1235 puts "Reading ASN.1 file $inf(iname)"
1239 set inf(inf) [open $inf(iname) r]
1245 # The following procedures are invoked by the asnType function.
1246 # Each procedure takes the form: asnBasic<TYPE> and they must return
1247 # two elements: the C function handler and the C type.
1248 # On entry upvar $name is the type we are defining and global, $inf(module), is
1249 # the current module name.
1251 proc asnBasicEXTERNAL {} {
1252 return {odr_external {Odr_external}}
1255 proc asnBasicINTEGER {} {
1256 return {odr_integer {int}}
1259 proc asnBasicENUMERATED {} {
1260 return {odr_enum {int}}
1263 proc asnBasicNULL {} {
1264 return {odr_null {Odr_null}}
1267 proc asnBasicBOOLEAN {} {
1268 return {odr_bool {bool_t}}
1271 proc asnBasicOCTET {} {
1273 lex-name-move STRING
1274 return {odr_octetstring {Odr_oct}}
1277 proc asnBasicBIT {} {
1279 lex-name-move STRING
1280 return {odr_bitstring {Odr_bitmask}}
1283 proc asnBasicOBJECT {} {
1285 lex-name-move IDENTIFIER
1286 return {odr_oid {Odr_oid}}
1289 proc asnBasicGeneralString {} {
1290 return {odr_generalstring char}
1293 proc asnBasicVisibleString {} {
1294 return {odr_visiblestring char}
1297 proc asnBasicGeneralizedTime {} {
1298 return {odr_generalizedtime char}
1301 proc asnBasicANY {} {
1304 return [list $inf(fprefix)ANY_$name void]
1307 # userDef: reads user definitions file $name
1308 proc userDef {name} {
1311 if {$inf(verbose) > 1} {
1312 puts "Reading definitions file $name"
1316 if {[info exists default-prefix]} {
1317 set inf(prefix) ${default-prefix}
1319 if {[info exists h-path]} {
1320 set inf(h-path) ${h-path}
1322 foreach m [array names prefix] {
1323 set inf(prefix,$m) $prefix($m)
1325 foreach m [array names body] {
1326 set inf(body,$m) $body($m)
1328 foreach m [array names init] {
1329 set inf(init,$m) $init($m)
1331 foreach m [array names filename] {
1332 set inf(filename,$m) $filename($m)
1334 foreach m [array names map] {
1335 set inf(map,$m) $map($m)
1337 foreach m [array names membermap] {
1338 set inf(membermap,$m) $membermap($m)
1340 foreach m [array names unionmap] {
1341 set inf(unionmap,$m) $unionmap($m)
1346 set inf(prefix) {yc_ Yc_ YC_}
1350 # Parse command line
1351 set l [llength $argv]
1354 set arg [lindex $argv $i]
1355 switch -glob -- $arg {
1360 set p [string range $arg 2 end]
1361 if {![string length $p]} {
1362 set p [lindex $argv [incr i]]
1367 set p [string range $arg 2 end]
1368 if {![string length $p]} {
1369 set p [lindex $argv [incr i]]
1374 set p [string range $arg 2 end]
1375 if {![string length $p]} {
1376 set p [lindex $argv [incr i]]
1378 set inf(h-dir) [string trim $p \\/]/
1381 set p [string range $arg 2 end]
1382 if {![string length $p]} {
1383 set p [lindex $argv [incr i]]
1388 set p [string range $arg 2 end]
1389 if {![string length $p]} {
1390 set p [lindex $argv [incr i]]
1395 set p [string range $arg 2 end]
1396 if {![string length $p]} {
1397 set p [lindex $argv [incr i]]
1402 set p [string range $arg 2 end]
1403 if {![string length $p]} {
1404 set p [lindex $argv [incr i]]
1409 set p [string range $arg 2 end]
1410 if {![string length $p]} {
1411 set p [lindex $argv [incr i]]
1413 if {[llength $p] == 1} {
1414 set inf(prefix) [list [string tolower $p] \
1415 [string toupper $p] [string toupper $p]]
1416 } elseif {[llength $p] == 3} {
1430 if {![info exists inf(iname)]} {
1431 puts "YAZ ASN.1 Compiler ${yc_version}"
1432 puts -nonewline "Usage: ${argv0}"
1433 puts { [-v] [-c cfile] [-h hfile] [-p hfile] [-d dfile] [-I path]}
1434 puts { [-x prefix] [-m module] file}