home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / tls / tls074c.sunsparc.Z / tls074c.sunsparc / usr / lib / tcl / buildidx.tcl < prev    next >
Encoding:
Text File  |  1995-07-20  |  4.7 KB  |  141 lines

  1. #
  2. # buildidx.tcl --
  3. #
  4. # Code to build Tcl package library. Defines the proc `buildpackageindex'.
  5. #------------------------------------------------------------------------------
  6. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: buildidx.tcl,v 1.3 1995/01/26 10:51:25 zibi Exp $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. #------------------------------------------------------------------------------
  20. # The following code passes around a array containing information about a
  21. # package.  The following fields are defined
  22. #
  23. #   o name - The name of the package.
  24. #   o offset - The byte offset of the package in the file.
  25. #   o procs - The list of entry point procedures defined for the package.
  26. #------------------------------------------------------------------------------
  27.  
  28. #------------------------------------------------------------------------------
  29. # Write a line to the index file describing the package.
  30. #
  31. proc TCLSH:PutIdxEntry {outfp pkgInfo endOffset} {
  32.     puts $outfp [concat [keylget pkgInfo name] \
  33.                         [keylget pkgInfo offset] \
  34.                         [expr {$endOffset - [keylget pkgInfo offset] - 1}] \
  35.                         [keylget pkgInfo procs]]
  36. }
  37.  
  38. #------------------------------------------------------------------------------
  39. # Parse a package header found by a scan match.  Handle backslashed
  40. # continuation lines.
  41. #
  42. proc TCLSH:ParsePkgHeader matchInfoVar {
  43.     upvar $matchInfoVar matchInfo
  44.  
  45.     set line [string trimright $matchInfo(line)]
  46.     while {[string match {*\\} $line]} {
  47.         set line [csubstr $line 0 [expr [clength $line]-1]]
  48.         append line " " [string trimright [gets $matchInfo(handle)]]
  49.     }
  50.  
  51.     keylset pkgInfo name   [lindex $line 1]
  52.     keylset pkgInfo offset [tell $matchInfo(handle)]
  53.     keylset pkgInfo procs  [lrange $line  2 end]
  54.     return $pkgInfo
  55. }
  56.  
  57. #------------------------------------------------------------------------------
  58. # Do the actual work of creating a package library index from a library file.
  59. #
  60. proc TCLSH:CreateLibIndex {libName} {
  61.  
  62.     if {[file extension $libName] != ".tlib"} {
  63.         error "Package library `$libName' does not have the extension `.tlib'"
  64.     }
  65.     set idxName "[file root $libName].tndx"
  66.  
  67.     unlink -nocomplain $idxName
  68.     set libFH [open $libName r]
  69.     set idxFH [open $idxName w]
  70.     set packageCnt 0
  71.  
  72.     set contectHdl [scancontext create]
  73.  
  74.     scanmatch $contectHdl "^#@package: " {
  75.         if {[llength $matchInfo(line)] < 2} {
  76.             error "invalid package header \"$matchInfo(line)\""
  77.         }
  78.         if ![lempty $pkgInfo] {
  79.             TCLSH:PutIdxEntry $idxFH $pkgInfo $matchInfo(offset)
  80.         }
  81.         set pkgInfo [TCLSH:ParsePkgHeader matchInfo]
  82.         incr packageCnt
  83.     }
  84.  
  85.     scanmatch $contectHdl "^#@packend" {
  86.         if [lempty $pkgInfo] {
  87.             error "#@packend without #@package in $libName"
  88.         }
  89.         TCLSH:PutIdxEntry $idxFH $pkgInfo $matchInfo(offset)
  90.         set pkgInfo {}
  91.     }
  92.  
  93.     set pkgInfo {}
  94.     if {[catch {
  95.         scanfile $contectHdl $libFH
  96.         if {$packageCnt == 0} {
  97.             error "No #@package definitions found in $libName"
  98.         }   
  99.     } msg] != 0} {
  100.        global errorInfo errorCode
  101.        close $libFH
  102.        close $idxFH
  103.        unlink -nocomplain $idxName
  104.        error $msg $errorInfo $errorCode
  105.     }
  106.     if ![lempty $pkgInfo] {
  107.         TCLSH:PutIdxEntry $idxFH $pkgInfo [tell $libFH] 
  108.     }
  109.     close $libFH
  110.     close $idxFH
  111.     
  112.     scancontext delete $contectHdl
  113.  
  114.     # Set mode and ownership of the index to be the same as the library.
  115.     # Ignore errors if you can't set the ownership.
  116.  
  117.     file stat $libName statInfo
  118.     chmod $statInfo(mode) $idxName
  119.     catch {
  120.        chown [list $statInfo(uid) $statInfo(gid)] $idxName
  121.     }
  122. }
  123.  
  124. #------------------------------------------------------------------------------
  125. # Create a package library index from a library file.
  126. #
  127. proc buildpackageindex {libfilelist} {
  128.     foreach libfile $libfilelist {
  129.         set status [catch {
  130.             TCLSH:CreateLibIndex $libfile
  131.         } errmsg]
  132.         if {$status != 0} {
  133.             global errorInfo errorCode
  134.             error "building package index for `$libfile' failed: $errmsg" \
  135.                 $errorInfo $errorCode
  136.         }
  137.     }
  138. }
  139.  
  140.