Added documentation.
[yaz-moved-to-github.git] / util / yc.tcl
index 76785d3..b7aca35 100755 (executable)
@@ -2,13 +2,27 @@
 # the next line restarts using tclsh \
 exec tclsh "$0" "$@"
 #
-# YC: YAZ ODR Compiler
-# (c) Index Data 1996-1998
+# YC: ASN.1 Compiler for YAZ
+# (c) Index Data 1996-2000
 # See the file LICENSE for details.
-# Sebastian Hammer, Adam Dickmeiss
 #
 # $Log: yc.tcl,v $
-# Revision 1.1  1999-06-08 10:10:16  adam
+# Revision 1.6  2000-02-10 13:44:02  adam
+# Tcl command clock not used if unavailable (Tcl7.4 and earlier).
+#
+# Revision 1.5  2000/01/15 09:18:42  adam
+# Bug fix: some elements where treated as OPTIONAL when they shouldn't.
+#
+# Revision 1.4  1999/12/16 23:36:19  adam
+# Implemented ILL protocol. Minor updates ASN.1 compiler.
+#
+# Revision 1.3  1999/11/30 13:47:12  adam
+# Improved installation. Moved header files to include/yaz.
+#
+# Revision 1.2  1999/06/09 09:43:11  adam
+# Added option -I and variable h-path to specify path for header files.
+#
+# Revision 1.1  1999/06/08 10:10:16  adam
 # New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
 #
 # Revision 1.8  1999/04/20 10:37:04  adam
@@ -257,7 +271,7 @@ proc asnOptional {} {
         return 1
     } elseif {[lex-name-move DEFAULT]} {
        lex
-       return 1
+       return 0
     }
     return 0
 }
@@ -543,7 +557,7 @@ proc asnSequence {name tag implicit tagtype} {
         lappend l "\t\t!odr_sequence_begin (o, p, sizeof(**p), name))"
         lappend l "\t\treturn opt && odr_ok(o);"
     } else {
-        lappend l "\tif (!odr_constructed_begin (o, p, $tagtype, $tag, name)"
+        lappend l "\tif (!odr_constructed_begin (o, p, $tagtype, $tag, name))"
         lappend l "\t\treturn opt && odr_ok(o);"
         lappend l "\tif (o->direction == ODR_DECODE)"
         lappend l "\t\t*p = odr_malloc (o, sizeof(**p));"
@@ -904,7 +918,7 @@ proc asnImports {} {
            } else {
                set fname $val
            }
-           puts $file(outh) "\#include \"${fname}.h\""
+           puts $file(outh) "\#include <$inf(h-dir)${fname}.h>"
 
             if {[info exists inf(prefix,$val)]} {
                 set prefix $inf(prefix,$val)
@@ -912,9 +926,14 @@ proc asnImports {} {
                 set prefix $inf(prefix)
             }
             foreach n $nam {
-                set m [join [split $n -] _]
-                set inf(imports,$n) [list [lindex $prefix 0]$m \
-                                          [lindex $prefix 1]$m]
+               if {[info exists inf(map,$val,$n)]} {
+                   set v $inf(map,$val,$n)
+               } else {
+                   set v $n
+               }
+                set w [join [split $v -] _]
+                set inf(imports,$n) [list [lindex $prefix 0]$w \
+                                          [lindex $prefix 1]$w]
             }
             unset nam
             lex
@@ -1084,8 +1103,6 @@ proc asnModules {} {
                lex
                if {![string length $type]} return
            }
-
-
            if {[info exists inf(filename,$inf(module))]} {
                set fname $inf(filename,$inf(module))
            } else {
@@ -1101,14 +1118,17 @@ proc asnModules {} {
            if {![info exists inf(h-file)]} {
                set inf(h-file) ${fname}.h
            }
-           set file(outh) [open $inf(h-file) w]
+           set file(outh) [open $inf(h-path)/$inf(h-dir)$inf(h-file) w]
 
-           if {![info exists inf(p-file)]} {
-               set inf(p-file) ${fname}-p.h
-           }
-           set file(outp) [open $inf(p-file) w]
+           if {0} {
+               if {![info exists inf(p-file)]} {
+                   set inf(p-file) ${fname}-p.h
+               }
+               set file(outp) [open $inf(h-path)/$inf(h-dir)$inf(p-file) w]
+            }
 
-           set md [clock format [clock seconds]]
+           set md ""
+           catch {set md [clock format [clock seconds]]}
            
            puts $file(outc) "/* YC ${yc_version} $md */"
            puts $file(outc) "/* Module-C: $inf(module) */"
@@ -1118,26 +1138,29 @@ proc asnModules {} {
            puts $file(outh) "/* Module-H $inf(module) */"
            puts $file(outh) {}
 
-           puts $file(outp) "/* YC ${yc_version}: $md */"
-           puts $file(outp) "/* Module-P: $inf(module) */"
-           puts $file(outp) {}
-
-           puts $file(outc) "\#include \"$inf(p-file)\""
+           if {[info exists file(outp)]} {
+               puts $file(outp) "/* YC ${yc_version}: $md */"
+               puts $file(outp) "/* Module-P: $inf(module) */"
+               puts $file(outp) {}
+            }
 
+            if {[info exists inf(p-file)]} {
+               puts $file(outc) "\#include <$inf(h-dir)$inf(p-file)>"
+            } else {
+               puts $file(outc) "\#include <$inf(h-dir)$inf(h-file)>"
+            }
            puts $file(outh) "\#ifndef ${ppname}_H"
            puts $file(outh) "\#define ${ppname}_H"
            puts $file(outh) {}
-           puts $file(outh) "\#include <odr.h>"
-           
-           puts $file(outp) "\#ifndef ${ppname}_P_H"
-           puts $file(outp) "\#define ${ppname}_P_H"
-           puts $file(outp) {}
-           puts $file(outp) "\#include \"$inf(h-file)\""
+           puts $file(outh) "\#include <$inf(h-dir)odr.h>"
+          
+            if {[info exists file(outp)]} { 
+               puts $file(outp) "\#ifndef ${ppname}_P_H"
+               puts $file(outp) "\#define ${ppname}_P_H"
+               puts $file(outp) {}
+               puts $file(outp) "\#include <$inf(h-dir)$inf(h-file)>"
 
-
-           puts $file(outp) "\#ifdef __cplusplus"
-           puts $file(outp) "extern \"C\" \{"
-           puts $file(outp) "\#endif"
+            }
            
            asnTagDefault
            if {[string compare $type :]} {
@@ -1155,6 +1178,9 @@ proc asnModules {} {
            } else {
                set f $file(outh)
            }
+           puts $f "\#ifdef __cplusplus"
+           puts $f "extern \"C\" \{"
+           puts $f "\#endif"
            for {set i 1} {$i < $inf(nodef)} {incr i} {
                puts $f $inf(var,$i)
                if {[info exists inf(asn,$i)]} {
@@ -1170,9 +1196,9 @@ proc asnModules {} {
                unset inf(var,$i)
                puts $f {}
            }
-           puts $file(outp) "\#ifdef __cplusplus"
-           puts $file(outp) "\}"
-           puts $file(outp) "\#endif"
+           puts $f "\#ifdef __cplusplus"
+           puts $f "\}"
+           puts $f "\#endif"
 
            if {[info exists inf(body,$inf(module),h)]} {
                puts $file(outh) $inf(body,$inf(module),h)
@@ -1181,16 +1207,20 @@ proc asnModules {} {
                puts $file(outc) $inf(body,$inf(module),c)
            }
            if {[info exists inf(body,$inf(module),p)]} {
-               puts $file(outp) $inf(body,$inf(module),p)
+                if {[info exists file(outp)]} {
+                   puts $file(outp) $inf(body,$inf(module),p)
+                }
            }
            puts $file(outh) "\#endif"
-           puts $file(outp) "\#endif"
+            if {[info exists file(outp)]} {
+               puts $file(outp) "\#endif"
+            }
            foreach f [array names file] {
                close $file($f)
            }
            unset inf(c-file)
            unset inf(h-file)
-           unset inf(p-file)
+           catch {unset inf(p-file)}
        }
     }
 }
@@ -1254,6 +1284,18 @@ proc asnBasicOBJECT {} {
     return {odr_oid {Odr_oid}}
 }
 
+proc asnBasicGeneralString {} {
+    return {odr_generalstring char}
+}
+
+proc asnBasicVisibleString {} {
+    return {odr_visiblestring char}
+}
+
+proc asnBasicGeneralizedTime {} {
+    return {odr_generalizedtime char}
+}
+
 proc asnBasicANY {} {
     upvar name name
     global inf
@@ -1272,6 +1314,9 @@ proc userDef {name} {
     if {[info exists default-prefix]} {
         set inf(prefix) ${default-prefix}
     }
+    if {[info exists h-path]} {
+        set inf(h-path) ${h-path}
+    }
     foreach m [array names prefix] {
         set inf(prefix,$m) $prefix($m)
     }
@@ -1297,6 +1342,8 @@ proc userDef {name} {
 
 set inf(verbose) 0
 set inf(prefix) {yc_ Yc_ YC_}
+set inf(h-path) .
+set inf(h-dir) ""
 
 # Parse command line
 set l [llength $argv]
@@ -1314,6 +1361,20 @@ while {$i < $l} {
              }
            set inf(c-file) $p
         }
+        -I* {
+           set p [string range $arg 2 end]
+           if {![string length $p]} {
+                 set p [lindex $argv [incr i]]
+             }
+           set inf(h-path) $p
+        }
+       -i* {
+           set p [string range $arg 2 end]
+           if {![string length $p]} {
+                 set p [lindex $argv [incr i]]
+            }
+           set inf(h-dir) [string trim $p \\/]/
+       }
         -h* {
            set p [string range $arg 2 end]
            if {![string length $p]} {
@@ -1365,9 +1426,9 @@ while {$i < $l} {
 }
 
 if {![info exists inf(iname)]} {
-    puts "YAZ ODR Compiler ${yc_version}"
+    puts "YAZ ASN.1 Compiler ${yc_version}"
     puts -nonewline "Usage: ${argv0}"
-    puts { [-v] [-c cfile] [-h hfile] [-p hfile] [-d dfile] }
+    puts { [-v] [-c cfile] [-h hfile] [-p hfile] [-d dfile] [-I path]}
     puts {    [-x prefix] [-m module] file}
     exit 1
 }