Thursday, August 11, 2005

Cygwin, bash, grep, gawk, tclsh, VBScript

What a combination!! I've found Cygwin enormously helpful. Take the following snippet for example, which extracts the names of all the Subs and Functions from my library file into a file, and then hands that file on to a tcl script.
#!/bin/bash
grep -i '^Function ' StdFunctions.vbs >func.lst
grep -i '^Sub ' StdFunctions.vbs >> func.lst
cat func.lst | gawk -F " " '{ print $2 }' | gawk -F "(" '{ print $1 }' > Routines.lst
rm func.lst
tclsh routines.tcl
The output file, Routines.lst, looks like this (partially)
CreateMDB
OpenDatabase
OpenDatabase2
SelectSheet
GetColumnDownToEnd
GetColumnDownToLimit
RangeToArray
VectorToTwoDimArray
RelativeToAbsolute
MakeKey3
MakeKey4
TimeStamp
Zerofill
IsFile
IsINISection
GetINIString
Routines.tcl takes this, and an internally specified list of files to check (which I could have coded so as to specify on the commandline) and then tells me whether any of these routines are referred to in the list of files.
#!/bin/tclsh
set mm 0

proc searchFile {theFile theString} {
#puts "Searching $theFile for $theString"
set h [open $theFile r]
set d [split [read $h] \n]
close $h
set c 0
set m 0
foreach dline $d {
incr c
if {[string match -nocase "*${theString}*" $dline] == 1} then {
  set m 1
  #puts "$theFile $c: $dline"
}
}
return $m
}

set vlist [list Create.vbs Daily.vbs NewBatchPrepare.vbs convert.vbs]

set h [open Routines.lst r]
set flist [split [read $h] \n]
close $h

foreach fitem $flist {
if { $fitem == "" } { continue }
set mm 0

foreach vfile $vlist {
if { $vfile == "" } { continue }
#puts "To search for $fitem in $vfile"
set r [searchFile $vfile $fitem]
if {$r == 0} {
  #puts "No sign of $fitem in $vfile"
}
set mm [expr {$mm + $r}]
}
if { $mm < 1 } {
   puts "No sign at all of $fitem"
}
}
It's not foolproof. Some routines are used internally by StdFunctions.vbs. But it's helpful.

No comments: