home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / lib / dejagnu / utils.exp < prev    next >
Encoding:
Text File  |  1996-10-12  |  6.7 KB  |  272 lines

  1. # Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program; if not, write to the Free Software
  13. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
  14.  
  15. # Please email any bugs, comments, and/or additions to this file to:
  16. # bug-dejagnu@prep.ai.mit.edu
  17.  
  18. # This file was written by Rob Savoye. (rob@cygnus.com)
  19.  
  20. #
  21. # Most of the procedures found here mimic their unix counter-part. 
  22. # This file is sourced by runtest.exp, so they are usable by any test case.
  23. #
  24.  
  25. #
  26. # getdirs -- gets the directories in a directory
  27. #            args: the first is the dir to look in, the next
  28. #                  is the pattern to match. It
  29. #                  defaults to *. Patterns are csh style
  30. #                  globbing rules
  31. #            returns: a list of dirs or NULL
  32. #
  33. proc getdirs { args } {
  34.     set path [lindex $args 0]
  35.     if [llength $args]>1 then {
  36.     set pattern [lindex $args 1]
  37.     } else {
  38.     set pattern "*"
  39.     }
  40.     verbose "Looking in $path for directories that match \"$pattern\"" 3
  41.     catch "glob $path/$pattern" tmp
  42.     if ![string match "" $tmp] then {
  43.     foreach i $tmp {
  44.         if [file isdirectory $i] then {
  45.                 if [file readable $i] then {
  46.             lappend dirs $i
  47.                 }
  48.         }
  49.     }    
  50.     } else {
  51.     perror "$tmp"
  52.     return ""
  53.     }
  54.     
  55.     if ![info exists dirs] then {
  56.     return ""
  57.     } else {
  58.     return $dirs
  59.     }
  60. }
  61.  
  62. #
  63. # find -- finds all the files recursively
  64. #            rootdir - this is the directory to start the search
  65. #                 from. This is and all subdirectories are search for
  66. #                 filenames. Directory names are not included in the
  67. #                 list, but the filenames have path information. 
  68. #            pattern - this is the pattern to match. Patterns are csh style
  69. #                 globbing rules.
  70. #         returns: a list or a NULL.
  71. #
  72. proc find { rootdir pattern } {
  73.     # first find all the directories
  74.     set dirs "$rootdir "
  75.     while 1 {
  76.     set tmp $rootdir
  77.     set rootdir ""
  78.     if [string match "" $tmp] then { break }
  79.     foreach i $tmp {
  80.         set j [getdirs $i]
  81.         if ![string match "" $j] then {
  82.         append dirs "$j "
  83.         set rootdir $j
  84.         unset j
  85.         } else {
  86.         set rootdir ""
  87.         }
  88.     }
  89.     set tmp ""
  90.     }
  91.     
  92.     # find all the files that match the pattern
  93.     foreach i $dirs {
  94.     verbose "Looking in $i\n" 3
  95.     set tmp [glob -nocomplain $i/$pattern]
  96.     if [llength $tmp]!=0 then {
  97.         foreach j $tmp {
  98.         if ![file isdirectory $j] then {
  99.             lappend files $j
  100.             verbose "Adding $j to file list" 3
  101.         }
  102.         }
  103.     }
  104.     }
  105.     
  106.     if ![info exists files] then {
  107.     lappend files ""
  108.     }
  109.     return $files
  110. }
  111.  
  112. #
  113. # which -- search the path for a file. This is basically a version
  114. #         of the BSD-unix which utility. This procedure depends on
  115. #         the shell environment variable $PATH. It returns 0 if $PATH
  116. #         does not exist or the binary is not in the path. If the
  117. #         binary is in the path, it returns the full path to the binary.
  118. #
  119. proc which { file } {
  120.     global env
  121.     
  122.     # strip off any extraneous arguments (like flags to the compiler)
  123.     set file [lindex $file 0]
  124.     
  125.     # if it exists then the path must be OK
  126.     if [file exists $file] then {
  127.     return $file
  128.     }
  129.     if [info exists env(PATH)] then {
  130.     set path [split $env(PATH) ":"]
  131.     } else {
  132.     return 0
  133.     }
  134.     
  135.     foreach i $path {
  136.     verbose "Checking against $i" 3
  137.     if [file exists $i/$file] then {
  138.         if [file executable $i/$file] then {
  139.         return $i/$file
  140.         } else {
  141.         warning "$i/$file exists but is not an executable"
  142.         }
  143.     }
  144.     }
  145.     # not in path
  146.     return 0
  147. }
  148.  
  149. #
  150. # grep -- looks for a string in a file. 
  151. #               return:list of lines that matched or NULL
  152. #                      if none match.
  153. #               args:  first arg is the filename,
  154. #                      second is the pattern,
  155. #                      third are any options.
  156. #               Options: line  - puts line numbers of match in list
  157. #
  158. proc grep { args } {
  159.     
  160.     set file [lindex $args 0]
  161.     set pattern [lindex $args 1]
  162.     
  163.     verbose "Grepping $file for the pattern \"$pattern\"" 3
  164.     
  165.     set argc [llength $args]
  166.     if $argc>2 then {
  167.     for { set i 2 } { $i < $argc } { incr i } {
  168.         append options [lindex $args $i]
  169.         append options " "
  170.     }
  171.     } else {
  172.     set options ""
  173.     }
  174.     
  175.     set i 0
  176.     set fd [open $file r]
  177.     while { [gets $fd cur_line]>=0 } {
  178.     incr i
  179.     if [regexp "$pattern.*" $cur_line match] then {
  180.         if ![string match "" $options] then {
  181.         foreach opt $options {
  182.             case $opt in {
  183.             "line" {
  184.                 lappend grep_out [concat $i $match]
  185.             }
  186.             }
  187.         }
  188.         } else {
  189.         lappend grep_out $match
  190.         }
  191.     }
  192.     }
  193.     close $fd
  194.     unset fd
  195.     unset i
  196.     if ![info exists grep_out] then {
  197.     set grep_out ""
  198.     }
  199.     return $grep_out
  200. }
  201.  
  202. #
  203. # prune -- remove elements based on patterns. elements are delimited by spaces.
  204. #          pattern is the pattern to look for using glob style matching
  205. #          list is the list to check against
  206. #          returns the new list
  207. #
  208. proc prune { list pattern } {
  209.     foreach i $list {
  210.     verbose "Checking pattern \"$pattern\" against $i" 3
  211.     if ![string match $pattern $i] then {
  212.         lappend tmp $i
  213.     } else {
  214.         verbose "Removing element $i from list" 3
  215.     }
  216.     }
  217.     return $tmp
  218. }
  219.  
  220. #
  221. # slay -- attempt to kill a process that you started
  222. #
  223. proc slay { name } {
  224.     set in [open [concat "|ps"] r]
  225.     while {[gets $in line]>-1} {
  226.     if ![string match "*expect*slay*" $line] then {
  227.         if [string match "*$name*" $line] then {
  228.         set pid [lindex $line 0]
  229.         catch "exec kill -9 $pid]"
  230.         verbose "Killing $name, pid = $pid\n"
  231.         }
  232.     }
  233.     }
  234.     close $in
  235. }
  236.  
  237. #
  238. # absolute -- convert a relative path to an absolute one
  239. #
  240. proc absolute { path } {
  241.     if [string match "." $path] then {
  242.         return [pwd]
  243.     }
  244.     
  245.     set basedir [pwd]
  246.     cd $path
  247.     set path [pwd]
  248.     cd $basedir
  249.     return $path
  250. }
  251.  
  252. #
  253. # psource -- source a file and trap any real errors. This ignores extraneous
  254. #            output. returns a 1 if there was an error, otherwise it returns 0.
  255. #
  256. proc psource { file } {
  257.     global errorInfo
  258.     global errorCode
  259.  
  260.     if [file exists $file] then {
  261.     catch "source $file"
  262.     if [info exists errorInfo] then {
  263.         send_error "ERROR: errors in $file\n"
  264.         send_error "$errorInfo"
  265.         return 1
  266.     }
  267.     }
  268.     return 0
  269. }
  270.