initial checkin, based on GSS 0.46 CVS
[gss-tcad.git] / lib / gui_script / findfile.tcl
blobf8a4c103e93b1a02615983c6425b9170410fc720
1 #----- search for specified file
3 proc findfile:check {type fname} {
4 global tcl_platform
5 if {$type == "executable" && $tcl_platform(platform) == "windows"} {
6 foreach ext {.com .exe .bat} {
7 if [file exists $fname$ext] {
8 return $fname$ext
12 if [file $type $fname] {
13 if {$type == "executable" && [file isdirectory $fname]} {
14 return {}
16 return $fname
18 return {}
21 proc findfile:path {str delim} {
22 global env
23 set path {}
24 foreach p [split $str $delim] {
25 set dir {}
26 foreach d [split $p /] {
27 if {[string length $d] > 1 && [string index $d 0] == {$}} {
28 if [info exists env([string range $d 1 end])] {
29 lappend dir $env([string range $d 1 end])
30 } else {
31 set dir {}
32 break
34 } else {
35 lappend dir $d
38 if [llength $dir] {
39 lappend path [join $dir /]
42 if [llength $path] {
43 return [join $path $delim]
45 return ""
48 proc find_file {type fname args} {
49 global tcl_platform env
51 if {$fname == ""} {
52 return {}
54 set dr [file dirname $fname]
55 set fn [file tail $fname]
56 if {$type == ""} {
57 set type exists
60 # check if path given
62 if {$dr != "."} {
63 set curdir [pwd]
64 catch {cd $dr}
65 set filename [pwd]/$fn
66 cd $curdir
67 return [findfile:check $type $filename]
70 # check if running under windows
72 if {[info exists tcl_platform(platform)] &&
73 $tcl_platform(platform) == "windows"} {
74 set delim ";"
75 } else {
76 set delim ":"
79 if {$args == ""} {
81 # check current directory
83 set filename [findfile:check $type [pwd]/$fn]
84 if {$filename != ""} {
85 return $filename
87 if {$fname != $fn} {
88 return {}
91 # check PATH environment variable
93 if [info exists env(PATH)] {
94 foreach dir [split $env(PATH) $delim] {
95 set filename [findfile:check $type $dir/$fn]
96 if {$filename != ""} {
97 return $filename
102 } else {
104 # check supplied list of paths/directories
106 foreach val $args {
107 foreach ent [split $val] {
108 if {$ent != ""} {
109 set path [findfile:path $ent $delim]
110 if {$path != ""} {
111 foreach dir [split $path $delim] {
112 set filename [findfile:check $type $dir/$fn]
113 if {$filename != ""} {
114 return $filename
123 return {}