2 # the next line restarts using tclsh \
5 # YC: ASN.1 Compiler for YAZ
6 # (c) Index Data 1996-1999
7 # See the file LICENSE for details.
8 # Sebastian Hammer, Adam Dickmeiss
11 # Revision 1.3 1999-11-30 13:47:12 adam
12 # Improved installation. Moved header files to include/yaz.
14 # Revision 1.2 1999/06/09 09:43:11 adam
15 # Added option -I and variable h-path to specify path for header files.
17 # Revision 1.1 1999/06/08 10:10:16 adam
18 # New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
20 # Revision 1.8 1999/04/20 10:37:04 adam
21 # Updated for ODR - added name parameter.
23 # Revision 1.7 1998/04/03 14:44:20 adam
26 # Revision 1.6 1998/04/03 13:21:17 adam
29 # Revision 1.5 1998/04/03 12:48:17 adam
30 # Fixed bug: missed handling of constructed tags for CHOICE.
32 # Revision 1.4 1998/03/31 15:47:45 adam
33 # First compiled ASN.1 code for YAZ.
35 # Revision 1.3 1998/03/23 17:13:20 adam
36 # Implemented SET OF and ENUM. The Compiler now eats ILL (ISO10161) and
39 # Revision 1.2 1997/10/07 10:31:01 adam
40 # Added facility to specify tag type (CONTEXT, APPLICATION, ...).
42 # Revision 1.1.1.1 1996/10/31 14:04:40 adam
43 # First version of the compiler for YAZ.
49 # Syntax for the ASN.1 supported:
52 # module -> name skip DEFINITIONS ::= mbody END
53 # mbody -> EXPORTS { nlist }
54 # | IMPORTS { imlist }
58 # type -> SEQUENCE { sqlist }
69 # sqlist -> sqlist , name tmt opt
71 # chlist -> chlist , name tmt
73 # enlist -> enlist , name (n)
75 # imlist -> nlist FROM name
76 # imlist nlist FROM name
79 # mod -> IMPLICIT | EXPLICIT | e
80 # tag -> [tagtype n] | [n] | e
83 # name identifier/token
85 # skip one token skipped
87 # tagtype APPLICATION, CONTEXT, etc.
89 # lex: moves input file pointer and returns type of token.
90 # The globals $type and $val are set. $val holds name if token
91 # is normal identifier name.
92 # sets global var type to one of:
95 # \} right curly brace
104 while {![string length $inf(str)]} {
106 set inf(cnt) [gets $inf(inf) inf(str)]
111 lappend inf(asn,$inf(asndef)) $inf(str)
112 set l [string first -- $inf(str)]
115 set inf(str) [string range $inf(str) 0 $l]
117 set inf(str) [string trim $inf(str)]
119 set s [string index $inf(str) 0]
129 \[ { regexp {^\[[ ]*(.+)[ ]*\]} $inf(str) s val }
130 : { regexp {^::=} $inf(str) s }
132 regexp "^\[^,\t :\{\}();\]+" $inf(str) s
137 set off [string length $s]
138 set inf(str) [string trim [string range $inf(str) $off end]]
142 # lex-expect: move pointer and expect token $t
143 proc lex-expect {t} {
146 if {[string compare $t $type]} {
147 asnError "Got $type '$val', expected $t"
151 # lex-name-move: see if token is $name; moves pointer and returns
152 # 1 if it is; returns 0 otherwise.
153 proc lex-name-move {name} {
155 if {![string compare $type n] && ![string compare $val $name]} {
162 # asnError: Report error and die
163 proc asnError {msg} {
166 puts "Error in line $inf(lineno) in module $inf(module)"
172 # asnWarning: Report warning and return
173 proc asnWarning {msg} {
176 puts "Warning in line $inf(lineno) in module $inf(module)"
180 # asnEnum: parses enumerated list - { name1 (n), name2 (n), ... }
181 # Uses $name as prefix. If there really is a list, $lx holds the C
182 # preprocessor definitions on return; otherwise lx isn't set.
183 proc asnEnum {name lx} {
186 if {[string compare $type \{]} return
189 set pq [asnName $name]
190 set id [lindex $pq 0]
193 lappend l "#define $inf(dprefix)$id $val"
196 if {[string compare $type ,]} break
198 if {[string compare $type \}]} {
199 asnError "Missing \} in enum list got $type '$val'"
204 # asnMod: parses tag and modifier.
205 # $xtag and $ximplicit holds tag and implicit-indication on return.
206 # $xtag is empty if no tag was specified. $ximplicit is 1 on implicit
207 # tagging; 0 otherwise.
208 proc asnMod {xtag ximplicit xtagtype} {
212 upvar $ximplicit implicit
213 upvar $xtagtype tagtype
217 if {![string compare $type \[]} {
218 if {[regexp {^([a-zA-Z]+)[ ]+([0-9]+)$} $val x tagtype tag]} {
219 set tagtype ODR_$tagtype
220 } elseif {[regexp {^([0-9]+)$} $val x tag]} {
221 set tagtype ODR_CONTEXT
223 asnError "bad tag specification: $val"
227 set implicit $inf(implicit-tags)
228 if {![string compare $type n]} {
229 if {![string compare $val EXPLICIT]} {
232 } elseif {![string compare $val IMPLICIT]} {
239 # asnName: moves pointer and expects name. Returns C-validated name.
240 proc asnName {name} {
243 if {[info exists inf(membermap,$inf(module),$name,$val)]} {
244 set nval $inf(membermap,$inf(module),$name,$val)
246 puts " mapping member $name,$val to $nval"
251 if {![string match {[A-Z]*} $val]} {
255 return [join [split $nval -] _]
258 # asnOptional: parses optional modifier. Returns 1 if OPTIONAL was
259 # specified; 0 otherwise.
260 proc asnOptional {} {
262 if {[lex-name-move OPTIONAL]} {
264 } elseif {[lex-name-move DEFAULT]} {
271 # asnSizeConstraint: parses the optional SizeConstraint.
272 # Currently not used for anything.
273 proc asnSizeConstraint {} {
275 if {[lex-name-move SIZE]} {
280 # asnSubtypeSpec: parses the SubtypeSpec ...
281 # Currently not used for anything. We now it's balanced however, i.e.
283 proc asnSubtypeSpec {} {
286 if {[string compare $type "("]} {
292 if {![string compare $type "("]} {
294 } elseif {![string compare $type ")"]} {
301 # asnType: parses ASN.1 type.
302 # On entry $name should hold the name we are currently defining.
303 # Returns type indicator:
304 # SequenceOf SEQUENCE OF
309 # Simple Basic types.
310 # In this casecalling procedure's $tname variable is a list holding:
311 # {C-Function C-Type} if the type is IMPORTed or ODR defined.
313 # {C-Function C-Type 1} if the type should be defined in this module
314 proc asnType {name} {
319 if {[string compare $type n]} {
320 asnError "Expects type specifier, but got $type"
327 if {[lex-name-move OF]} {
337 if {[lex-name-move OF]} {
350 if {[string length [info commands asnBasic$v]]} {
351 set tname [asnBasic$v]
353 if {[info exists inf(map,$inf(module),$v)]} {
354 set v $inf(map,$inf(module),$v)
356 if {[info exists inf(imports,$v)]} {
357 set tname $inf(imports,$v)
359 set w [join [split $v -] _]
360 set tname [list $inf(fprefix)$w $inf(vprefix)$w 1]
363 if {[lex-name-move DEFINED]} {
364 if {[lex-name-move BY]} {
372 proc mapName {name} {
374 if {[info exists inf(map,$inf(module),$name)]} {
375 set name $inf(map,$inf(module),$name)
377 puts -nonewline " $name ($inf(lineno))"
378 puts " mapping to $name"
382 puts " $name ($inf(lineno))"
388 # asnDef: parses type definition (top-level) and generates C code
389 # On entry $name holds the type we are defining.
393 set name [mapName $name]
394 if {[info exist inf(defined,$inf(fprefix)$name)]} {
395 incr inf(definedl,$name)
396 if {$inf(verbose) > 1} {
397 puts "set map($inf(module),$name) $name$inf(definedl,$name)"
400 set inf(definedl,$name) 0
402 set mname [join [split $name -] _]
403 asnMod tag implicit tagtype
404 set t [asnType $mname]
405 asnSub $mname $t $tname $tag $implicit $tagtype
409 # asnSub: parses type and generates C-code
411 # $name holds the type we are defining.
412 # $t is the type returned by the asnType procedure.
413 # $tname is the $tname set by the asnType procedure.
414 # $tag is the tag as returned by asnMod
415 # $implicit is the implicit indicator as returned by asnMod
416 proc asnSub {name t tname tag implicit tagtype} {
420 set defname defined,$inf(fprefix)$name
421 if {[info exist inf($defname)]} {
422 asnWarning "$name already defined in line $inf($defname)"
425 set inf($defname) $inf(lineno)
427 Sequence { set l [asnSequence $name $tag $implicit $tagtype] }
428 SequenceOf { set l [asnOf $name $tag $implicit $tagtype 0] }
429 SetOf { set l [asnOf $name $tag $implicit $tagtype 1] }
430 Choice { set l [asnChoice $name $tag $implicit $tagtype] }
431 Simple { set l [asnSimple $name $tname $tag $implicit $tagtype] }
432 default { asnError "switch asnType case not handled" }
437 puts $file(outc) "int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name)"
439 puts $file(outc) [lindex $l 0]
442 set fdef "$inf(cprefix)int $inf(fprefix)$name (ODR o, $inf(vprefix)$name **p, int opt, const char *name);"
445 set decl "typedef [lindex $l 1] $inf(vprefix)$name;"
446 if {![string compare [lindex $tname 2] 1]} {
447 if {![info exist inf(defined,[lindex $tname 0])]} {
451 set inf(var,$inf(nodef)) [join [lindex $l 2] \n]
455 set decl "typedef struct $inf(vprefix)$name $inf(vprefix)$name;"
456 set inf(var,$inf(nodef)) "[lindex $l 1];"
462 puts $file(outh) $decl
463 puts $file(outh) $fdef
464 asnForwardTypes $name
466 lappend inf(forward,code,[lindex $tname 0]) {} $decl $fdef
467 lappend inf(forward,ref,[lindex $tname 0]) $name
471 proc asnForwardTypes {name} {
474 if {![info exists inf(forward,code,$inf(fprefix)$name)]} {
477 foreach r $inf(forward,code,$inf(fprefix)$name) {
480 unset inf(forward,code,$inf(fprefix)$name)
482 while {[info exists inf(forward,ref,$inf(fprefix)$name)]} {
483 set n $inf(forward,ref,$inf(fprefix)$name)
484 set m [lrange $n 1 end]
486 set inf(forward,ref,$inf(fprefix)$name) $m
488 unset inf(forward,ref,$inf(fprefix)$name)
490 asnForwardTypes [lindex $n 0]
494 # asnSimple: parses simple type definition and generates C code
496 # $name is the name we are defining
497 # $tname is the tname as returned by asnType
498 # $tag is the tag as returned by asnMod
499 # $implicit is the implicit indicator as returned by asnMod
502 # Note: Doesn't take care of enum lists yet.
503 proc asnSimple {name tname tag implicit tagtype} {
506 set j "[lindex $tname 1] "
508 if {[info exists inf(unionmap,$inf(module),$name)]} {
509 set uName $inf(unionmap,$inf(module),$name)
515 if {![string length $tag]} {
516 set l "\treturn [lindex $tname 0] (o, p, opt, name);"
517 } elseif {$implicit} {
519 "\treturn odr_implicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);"
522 "\treturn odr_explicit_tag (o, [lindex $tname 0], p, $tagtype, $tag, opt, name);" \
524 if {[info exists jj]} {
525 return [list $l $j $jj]
531 # asnSequence: parses "SEQUENCE { s-list }" and generates C code.
533 # $name is the type we are defining
538 proc asnSequence {name tag implicit tagtype} {
541 lappend j "struct $inf(vprefix)$name \{"
544 if {![string length $tag]} {
545 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), name))"
546 lappend l "\t\treturn opt && odr_ok (o);"
547 } elseif {$implicit} {
548 lappend l "\tif (!odr_implicit_settag (o, $tagtype, $tag) ||"
549 lappend l "\t\t!odr_sequence_begin (o, p, sizeof(**p), name))"
550 lappend l "\t\treturn opt && odr_ok(o);"
552 lappend l "\tif (!odr_constructed_begin (o, p, $tagtype, $tag, name)"
553 lappend l "\t\treturn opt && odr_ok(o);"
554 lappend l "\tif (o->direction == ODR_DECODE)"
555 lappend l "\t\t*p = odr_malloc (o, sizeof(**p));"
557 lappend l "\tif (!odr_sequence_begin (o, p, sizeof(**p), 0))"
559 lappend l "\t\t*p = 0;"
560 lappend l "\t\treturn 0;"
565 set p [lindex [asnName $name] 0]
566 asnMod ltag limplicit ltagtype
570 if {[info exists inf(unionmap,$inf(module),$name,$p)]} {
571 set uName $inf(unionmap,$inf(module),$name,$p)
574 if {![string compare $t Simple]} {
575 if {[string compare $uName { }]} {
581 set opt [asnOptional]
582 if {![string length $ltag]} {
583 lappend l "\t\t[lindex $tname 0](o, &(*p)->$p, $opt, \"$p\") &&"
584 } elseif {$limplicit} {
585 lappend l "\t\todr_implicit_tag (o, [lindex $tname 0],"
586 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
588 lappend l "\t\todr_explicit_tag (o, [lindex $tname 0],"
589 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
591 set dec "\t[lindex $tname 1] *$p;"
592 } elseif {![string compare $t SequenceOf] && [string length $uName] &&\
593 (![string length $ltag] || $limplicit)} {
596 if {[llength $uName] < 2} {
597 set uName [list num_$p $p]
599 if {[string length $ltag]} {
603 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
608 set tmpa "odr_sequence_of(o, (Odr_fun) [lindex $tname 0], &(*p)->$p,"
609 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
610 lappend j "\tint [lindex $uName 0];"
611 set dec "\t[lindex $tname 1] **[lindex $uName 1];"
614 set subName [mapName ${name}_$level]
615 asnSub $subName $u {} {} 0 {}
617 set tmpa "odr_sequence_of(o, (Odr_fun) $inf(fprefix)$subName, &(*p)->$p,"
618 set tmpb "&(*p)->[lindex $uName 0], \"$p\")"
619 lappend j "\tint [lindex $uName 0];"
620 set dec "\t$inf(vprefix)$subName **[lindex $uName 1];"
624 set opt [asnOptional]
626 lappend l "\t\t($tmpa"
627 lappend l "\t\t $tmpb || odr_ok(o)) &&"
629 lappend l "\t\t$tmpa"
630 lappend l "\t\t $tmpb &&"
632 } elseif {!$nchoice && ![string compare $t Choice] && \
633 [string length $uName]} {
634 if {[llength $uName] < 3} {
635 set uName [list which u $name]
638 lappend j "\tint [lindex $uName 0];"
639 lappend j "\tunion \{"
640 lappend v "\tstatic Odr_arm arm\[\] = \{"
641 asnArm $name [lindex $uName 2] v j
643 set dec "\t\} [lindex $uName 1];"
644 set opt [asnOptional]
647 if {[string length $ltag]} {
649 lappend l "\t\todr_implicit_settag (o, $ltagtype, $ltag) &&"
651 asnWarning "optional handling missing in CHOICE in SEQUENCE"
652 asnWarning " set unionmap($inf(module),$name,$p) to {}"
660 lappend l "\t\t${la}odr_constructed_begin (o, &(*p)->[lindex $uName 1], $ltagtype, $ltag, \"$p\") &&"
665 set ob " || odr_ok(o))"
668 lappend l "\t\t${oa}odr_choice (o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], 0)${ob} &&"
669 if {[string length $ltag]} {
672 set lb ") || odr_ok(o))"
676 lappend l "\t\todr_constructed_end (o)${lb} &&"
680 set subName [mapName ${name}_$level]
681 asnSub $subName $t {} {} 0 {}
682 set opt [asnOptional]
683 if {![string length $ltag]} {
684 lappend l "\t\t$inf(fprefix)${subName} (o, &(*p)->$p, $opt, \"$p\") &&"
685 } elseif {$limplicit} {
686 lappend l "\t\todr_implicit_tag (o, $inf(fprefix)${subName},"
687 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
689 lappend l "\t\todr_explicit_tag (o, $inf(fprefix)${subName},"
690 lappend l "\t\t\t&(*p)->$p, $ltagtype, $ltag, $opt, \"$p\") &&"
692 set dec "\t$inf(vprefix)${subName} *$p;"
696 lappend j "$dec /* OPT */"
700 if {[string compare $type ,]} break
703 if {[string length $tag] && !$implicit} {
704 lappend l "\t\todr_sequence_end (o) &&"
705 lappend l "\t\todr_constructed_end (o);"
707 lappend l "\t\todr_sequence_end (o);"
709 if {[string compare $type \}]} {
710 asnError "Missing \} got $type '$val'"
713 if {[info exists v]} {
716 return [list [join $l \n] [join $j \n]]
719 # asnOf: parses "SEQUENCE/SET OF type" and generates C code.
721 # $name is the type we are defining
726 proc asnOf {name tag implicit tagtype isset} {
732 set func odr_sequence_of
735 if {[info exists inf(unionmap,$inf(module),$name)]} {
736 set numName $inf(unionmap,$inf(module),$name)
738 set numName {num elements}
741 lappend j "struct $inf(vprefix)$name \{"
742 lappend j "\tint [lindex $numName 0];"
744 lappend l "\tif (!odr_initmember (o, p, sizeof(**p)))"
745 lappend l "\t\treturn opt && odr_ok(o);"
746 if {[string length $tag]} {
748 lappend l "\todr_implicit_settag (o, $tagtype, $tag);"
750 asnWarning "Constructed SEQUENCE/SET OF not handled"
753 set t [asnType $name]
757 lappend l "\tif ($func (o, (Odr_fun) [lindex $tname 0], &(*p)->[lindex $numName 1],"
758 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
759 lappend j "\t[lindex $tname 1] **[lindex $numName 1];"
762 set subName [mapName ${name}_s]
763 lappend l "\tif ($func (o, (Odr_fun) $inf(fprefix)$subName, &(*p)->[lindex $numName 1],"
764 lappend l "\t\t&(*p)->[lindex $numName 0], name))"
765 lappend j "\t$inf(vprefix)$subName **[lindex $numName 1];"
766 asnSub $subName $t {} {} 0 {}
770 lappend l "\t\treturn 1;"
771 lappend l "\t*p = 0;"
772 lappend l "\treturn opt && odr_ok(o);"
773 return [list [join $l \n] [join $j \n]]
776 # asnArm: parses c-list in choice
777 proc asnArm {name defname lx jx} {
783 set pq [asnName $name]
786 if {![string length $q]} {
790 asnMod ltag limplicit ltagtype
793 lappend enums "$inf(dprefix)$p"
794 if {![string compare $t Simple]} {
796 if {![string length $ltag]} {
797 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
798 lappend l "\t\t (Odr_fun) [lindex $tname 0], \"$q\"\},"
799 } elseif {$limplicit} {
800 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
801 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
803 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
804 lappend l "\t\t(Odr_fun) [lindex $tname 0], \"$q\"\},"
806 lappend j "\t\t[lindex $tname 1] *$q;"
808 set subName [mapName ${name}_$q]
809 if {![string compare $inf(dprefix)${name}_$q \
810 $inf(vprefix)$subName]} {
811 set po [string toupper [string index $q 0]][string \
813 set subName [mapName ${name}${po}]
815 asnSub $subName $t $tname {} 0 {}
816 if {![string length $ltag]} {
817 lappend l "\t\t\{-1, -1, -1, $inf(dprefix)$p,"
818 lappend l "\t\t (Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
819 } elseif {$limplicit} {
820 lappend l "\t\t\{ODR_IMPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
821 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
823 lappend l "\t\t\{ODR_EXPLICIT, $ltagtype, $ltag, $inf(dprefix)$p,"
824 lappend l "\t\t(Odr_fun) $inf(fprefix)$subName, \"$q\"\},"
826 lappend j "\t\t$inf(vprefix)$subName *$q;"
828 if {[string compare $type ,]} break
830 if {[string compare $type \}]} {
831 asnError "Missing \} got $type '$val'"
836 lappend j "#define $e $level"
839 lappend l "\t\t\{-1, -1, -1, -1, (Odr_fun) 0, 0\}"
842 # asnChoice: parses "CHOICE {c-list}" and generates C code.
844 # $name is the type we are defining
849 proc asnChoice {name tag implicit tagtype} {
852 if {[info exists inf(unionmap,$inf(module),$name)]} {
853 set uName $inf(unionmap,$inf(module),$name)
855 set uName [list which u $name]
858 lappend j "struct $inf(vprefix)$name \{"
859 lappend j "\tint [lindex $uName 0];"
860 lappend j "\tunion \{"
861 lappend l "\tstatic Odr_arm arm\[\] = \{"
862 asnArm $name [lindex $uName 2] l j
863 lappend j "\t\} [lindex $uName 1];"
866 if {![string length $tag]} {
867 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
868 lappend l "\t\treturn opt && odr_ok(o);"
869 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
870 } elseif {$implicit} {
871 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
872 lappend l "\t\treturn opt && odr_ok(o);"
873 lappend l "\todr_implicit_settag(o, $tagtype, $tag);"
874 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name))"
876 lappend l "\tif (!*p && o->direction != ODR_DECODE)"
877 lappend l "\t\treturn opt;"
878 lappend l "\tif (!odr_constructed_begin(o, p, $tagtype, $tag, 0))"
879 lappend l "\t\treturn opt && odr_ok(o);"
880 lappend l "\tif (!odr_initmember(o, p, sizeof(**p)))"
881 lappend l "\t\treturn opt && odr_ok(o);"
882 lappend l "\tif (odr_choice(o, arm, &(*p)->[lindex $uName 1], &(*p)->[lindex $uName 0], name) &&"
883 lappend l "\t\todr_constructed_end(o))"
885 lappend l "\t\treturn 1;"
886 lappend l "\t*p = 0;"
887 lappend l "\treturn opt && odr_ok(o);"
888 return [list [join $l \n] [join $j \n]]
891 # asnImports: parses i-list in "IMPORTS {i-list}"
892 # On return inf(import,..)-array is updated.
893 # inf(import,"module") is a list of {C-handler, C-type} elements.
894 # The {C-handler, C-type} is compatible with the $tname as is used by the
895 # asnType procedure to solve external references.
897 global type val inf file
900 if {[string compare $type n]} {
901 asnError "Missing name in IMPORTS list"
905 if {![string compare $type n] && ![string compare $val FROM]} {
908 if {[info exists inf(filename,$val)]} {
909 set fname $inf(filename,$val)
913 puts $file(outh) "\#include <$inf(h-dir)${fname}.h>"
915 if {[info exists inf(prefix,$val)]} {
916 set prefix $inf(prefix,$val)
918 set prefix $inf(prefix)
921 set m [join [split $n -] _]
922 set inf(imports,$n) [list [lindex $prefix 0]$m \
923 [lindex $prefix 1]$m]
927 if {[string compare $type n]} break
928 } elseif {![string compare $type ,]} {
932 if {[string compare $type \;]} {
933 asnError "Missing ; after IMPORTS list - got $type '$val'"
938 # asnExports: parses e-list in "EXPORTS {e-list}"
939 # This function does nothing with elements in the list.
944 if {[string compare $type n]} {
945 asnError "Missing name in EXPORTS list"
947 set inf(exports,$val) 1
949 if {[string compare $type ,]} break
952 if {[string compare $type \;]} {
953 asnError "Missing ; after EXPORTS list - got $type ($val)"
958 # asnModuleBody: parses a module specification and generates C code.
959 # Exports lists, imports lists, and type definitions are handled;
960 # other things are silently ignored.
961 proc asnModuleBody {} {
962 global type val file inf
964 if {[info exists inf(prefix,$inf(module))]} {
965 set prefix $inf(prefix,$inf(module))
967 set prefix $inf(prefix)
969 set inf(fprefix) [lindex $prefix 0]
970 set inf(vprefix) [lindex $prefix 1]
971 set inf(dprefix) [lindex $prefix 2]
972 if {[llength $prefix] > 3} {
973 set inf(cprefix) [lindex $prefix 3]
975 set inf(cprefix) {YAZ_EXPORT }
979 puts "Module $inf(module), $inf(lineno)"
983 if {[info exists inf(init,$inf(module),c)]} {
984 puts $file(outc) $inf(init,$inf(module),c)
986 if {[info exists inf(init,$inf(module),h)]} {
987 puts $file(outh) "\#ifdef __cplusplus"
988 puts $file(outh) "extern \"C\" \{"
989 puts $file(outh) "\#endif"
991 puts $file(outh) $inf(init,$inf(module),h)
993 if {[info exists inf(init,$inf(module),p)]} {
994 puts $file(outp) $inf(init,$inf(module),p)
997 while {[string length $type]} {
998 if {[string compare $type n]} {
1002 if {![string compare $val END]} {
1004 } elseif {![string compare $val EXPORTS]} {
1007 } elseif {![string compare $val IMPORTS]} {
1009 puts $file(outh) "\#ifdef __cplusplus"
1010 puts $file(outh) "\}"
1011 puts $file(outh) "\#endif"
1018 puts $file(outh) "\#ifdef __cplusplus"
1019 puts $file(outh) "extern \"C\" \{"
1020 puts $file(outh) "\#endif"
1023 set inf(asndef) $inf(nodef)
1026 if {![string compare $type :]} {
1030 } elseif {![string compare $type n]} {
1032 if {[string length $type]} {
1039 puts $file(outh) "\#ifdef __cplusplus"
1040 puts $file(outh) "\}"
1041 puts $file(outh) "\#endif"
1044 foreach x [array names inf imports,*] {
1049 # asnTagDefault: parses TagDefault section
1050 proc asnTagDefault {} {
1051 global type val inf file
1053 set inf(implicit-tags) 0
1054 while {[string length $type]} {
1055 if {[lex-name-move EXPLICIT]} {
1057 set inf(implicit-tags) 0
1058 } elseif {[lex-name-move IMPLICIT]} {
1060 set inf(implicit-tags) 1
1067 # asnModules: parses a collection of module specifications.
1068 # Depending on the module pattern, $inf(moduleP), a module is either
1069 # skipped or processed.
1070 proc asnModules {} {
1071 global type val inf file yc_version
1076 while {![string compare $type n]} {
1077 set inf(module) $val
1078 if {[info exists inf(moduleP)] && ![string match $inf(moduleP) $val]} {
1079 if {$inf(verbose)} {
1082 while {![lex-name-move END]} {
1089 while {![lex-name-move DEFINITIONS]} {
1091 if {![string length $type]} return
1095 if {[info exists inf(filename,$inf(module))]} {
1096 set fname $inf(filename,$inf(module))
1098 set fname $inf(module)
1100 set ppname [join [split $fname -] _]
1102 if {![info exists inf(c-file)]} {
1103 set inf(c-file) ${fname}.c
1105 set file(outc) [open $inf(c-file) w]
1107 if {![info exists inf(h-file)]} {
1108 set inf(h-file) ${fname}.h
1110 set file(outh) [open $inf(h-path)/$inf(h-dir)$inf(h-file) w]
1112 if {![info exists inf(p-file)]} {
1113 set inf(p-file) ${fname}-p.h
1115 set file(outp) [open $inf(h-path)/$inf(h-dir)$inf(p-file) w]
1117 set md [clock format [clock seconds]]
1119 puts $file(outc) "/* YC ${yc_version} $md */"
1120 puts $file(outc) "/* Module-C: $inf(module) */"
1123 puts $file(outh) "/* YC ${yc_version}: $md */"
1124 puts $file(outh) "/* Module-H $inf(module) */"
1127 puts $file(outp) "/* YC ${yc_version}: $md */"
1128 puts $file(outp) "/* Module-P: $inf(module) */"
1131 puts $file(outc) "\#include <$inf(h-dir)$inf(p-file)>"
1133 puts $file(outh) "\#ifndef ${ppname}_H"
1134 puts $file(outh) "\#define ${ppname}_H"
1136 puts $file(outh) "\#include <$inf(h-dir)odr.h>"
1138 puts $file(outp) "\#ifndef ${ppname}_P_H"
1139 puts $file(outp) "\#define ${ppname}_P_H"
1141 puts $file(outp) "\#include <$inf(h-dir)$inf(h-file)>"
1144 puts $file(outp) "\#ifdef __cplusplus"
1145 puts $file(outp) "extern \"C\" \{"
1146 puts $file(outp) "\#endif"
1149 if {[string compare $type :]} {
1150 asnError "::= expected got $type '$val'"
1153 if {![lex-name-move BEGIN]} {
1154 asnError "BEGIN expected"
1159 if {[info exists file(outp)]} {
1164 for {set i 1} {$i < $inf(nodef)} {incr i} {
1165 puts $f $inf(var,$i)
1166 if {[info exists inf(asn,$i)]} {
1169 foreach comment $inf(asn,$i) {
1179 puts $file(outp) "\#ifdef __cplusplus"
1180 puts $file(outp) "\}"
1181 puts $file(outp) "\#endif"
1183 if {[info exists inf(body,$inf(module),h)]} {
1184 puts $file(outh) $inf(body,$inf(module),h)
1186 if {[info exists inf(body,$inf(module),c)]} {
1187 puts $file(outc) $inf(body,$inf(module),c)
1189 if {[info exists inf(body,$inf(module),p)]} {
1190 puts $file(outp) $inf(body,$inf(module),p)
1192 puts $file(outh) "\#endif"
1193 puts $file(outp) "\#endif"
1194 foreach f [array names file] {
1204 # asnFile: parses an ASN.1 specification file as specified in $inf(iname).
1208 if {$inf(verbose) > 1} {
1209 puts "Reading ASN.1 file $inf(iname)"
1213 set inf(inf) [open $inf(iname) r]
1219 # The following procedures are invoked by the asnType function.
1220 # Each procedure takes the form: asnBasic<TYPE> and they must return
1221 # two elements: the C function handler and the C type.
1222 # On entry upvar $name is the type we are defining and global, $inf(module), is
1223 # the current module name.
1225 proc asnBasicEXTERNAL {} {
1226 return {odr_external {Odr_external}}
1229 proc asnBasicINTEGER {} {
1230 return {odr_integer {int}}
1233 proc asnBasicENUMERATED {} {
1234 return {odr_enum {int}}
1237 proc asnBasicNULL {} {
1238 return {odr_null {Odr_null}}
1241 proc asnBasicBOOLEAN {} {
1242 return {odr_bool {bool_t}}
1245 proc asnBasicOCTET {} {
1247 lex-name-move STRING
1248 return {odr_octetstring {Odr_oct}}
1251 proc asnBasicBIT {} {
1253 lex-name-move STRING
1254 return {odr_bitstring {Odr_bitmask}}
1257 proc asnBasicOBJECT {} {
1259 lex-name-move IDENTIFIER
1260 return {odr_oid {Odr_oid}}
1263 proc asnBasicANY {} {
1266 return [list $inf(fprefix)ANY_$name void]
1269 # userDef: reads user definitions file $name
1270 proc userDef {name} {
1273 if {$inf(verbose) > 1} {
1274 puts "Reading definitions file $name"
1278 if {[info exists default-prefix]} {
1279 set inf(prefix) ${default-prefix}
1281 if {[info exists h-path]} {
1282 set inf(h-path) ${h-path}
1284 foreach m [array names prefix] {
1285 set inf(prefix,$m) $prefix($m)
1287 foreach m [array names body] {
1288 set inf(body,$m) $body($m)
1290 foreach m [array names init] {
1291 set inf(init,$m) $init($m)
1293 foreach m [array names filename] {
1294 set inf(filename,$m) $filename($m)
1296 foreach m [array names map] {
1297 set inf(map,$m) $map($m)
1299 foreach m [array names membermap] {
1300 set inf(membermap,$m) $membermap($m)
1302 foreach m [array names unionmap] {
1303 set inf(unionmap,$m) $unionmap($m)
1308 set inf(prefix) {yc_ Yc_ YC_}
1312 # Parse command line
1313 set l [llength $argv]
1316 set arg [lindex $argv $i]
1317 switch -glob -- $arg {
1322 set p [string range $arg 2 end]
1323 if {![string length $p]} {
1324 set p [lindex $argv [incr i]]
1329 set p [string range $arg 2 end]
1330 if {![string length $p]} {
1331 set p [lindex $argv [incr i]]
1336 set p [string range $arg 2 end]
1337 if {![string length $p]} {
1338 set p [lindex $argv [incr i]]
1340 set inf(h-dir) [string trim $p \\/]/
1343 set p [string range $arg 2 end]
1344 if {![string length $p]} {
1345 set p [lindex $argv [incr i]]
1350 set p [string range $arg 2 end]
1351 if {![string length $p]} {
1352 set p [lindex $argv [incr i]]
1357 set p [string range $arg 2 end]
1358 if {![string length $p]} {
1359 set p [lindex $argv [incr i]]
1364 set p [string range $arg 2 end]
1365 if {![string length $p]} {
1366 set p [lindex $argv [incr i]]
1371 set p [string range $arg 2 end]
1372 if {![string length $p]} {
1373 set p [lindex $argv [incr i]]
1375 if {[llength $p] == 1} {
1376 set inf(prefix) [list [string tolower $p] \
1377 [string toupper $p] [string toupper $p]]
1378 } elseif {[llength $p] == 3} {
1392 if {![info exists inf(iname)]} {
1393 puts "YAZ ASN.1 Compiler ${yc_version}"
1394 puts -nonewline "Usage: ${argv0}"
1395 puts { [-v] [-c cfile] [-h hfile] [-p hfile] [-d dfile] [-I path]}
1396 puts { [-x prefix] [-m module] file}