##################################################################################
#
# Grep TCL Port
#
http://perpleXa.net |
http://dev.perpleXa.net
# #perpleXa on QuakeNet
# (C) perpleXa 2003-2004
#
# ------------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# ------------------------------------------------------------------------------
#
##################################################################################
#
# Usage: grep [-ivl num] resource pattern
# i - ignore upper/lower case distinctions during comparisons
# v - invert the sense of matching, to select non-matching lines
# l num - list max <num> amounts of lines (20 by default)
# resource - an absolute or relative path
# ie: /dev/null/*
# pattern - a perl compatible regular expression (PCRE)
# ie: #channel2|user9|Cleanup or [a-z]{7}\s[0-9]{2}
#
##################################################################################
bind pub n|- {$grep} grep
bind dcc n|- grep grep:dcc
proc grep {nickname hostname handle channel arguments} {
set arguments [grep:clean $arguments] ;# clean the arguments (better solution than [split])
set offset 0 ;# for optional parameters
set limit 20 ;# the default output limit
set option [join [lindex $arguments 0]] ;# first check
if {[regexp -- {^-(\S+)$} [lindex $arguments 0] tmp option]} {
if {[regexp -- {^([ivl]{1,3})$} $option]} {
if {[regexp -- {l} $option]} {
set offset 2
regexp -- {^([0-9]+)$} [join [lindex $arguments 1]] tmp limit
} else {
set offset 1
}
} else {
putquick "NOTICE $nickname :unknown option \"$option\""
return
}
}
if {[llength $arguments] < [expr $offset + 2]} {
putquick "NOTICE $nickname :Usage: grep \[-ivl num\] resource pattern"
putquick "NOTICE $nickname : i - ignore upper/lower case distinctions during comparisons"
putquick "NOTICE $nickname : v - invert the sense of matching, to select non-matching lines"
putquick "NOTICE $nickname : l num - list max <num> amounts of lines (20 by default)"
putquick "NOTICE $nickname : resource - an absolute or relative path"
putquick "NOTICE $nickname : ie: /dev/null/*"
putquick "NOTICE $nickname : pattern - a perl compatible regular expression (PCRE)"
putquick "NOTICE $nickname : ie: #channel2|user9|Cleanup or \[a-z\]\{7\}\\s\[0-9\]\{2\}"
return
}
set resource [join [lindex [grep:clean $arguments] $offset]]
set pattern [join [lrange $arguments [expr $offset + 1] end]]
set count 0
catch {[glob -- $resource]} error
if {[regexp -nocase -- {no files matched glob pattern "(.*?)"} $error]} {
puthelp "NOTICE $nickname :--- End of list - $count matches"
return
}
foreach file "[glob -- $resource]" {
if {[file isdirectory "$file"]} {continue}
set fp [open $file r]
while {![eof $fp]} {
gets $fp line
if {$count == $limit} {break}
switch -regexp -- $option {
{v} {
if {[regexp -- {i} $option]} {
catch {if {![regexp -nocase -- $pattern $line]} {puthelp "NOTICE $nickname :${file}:${line}" ; incr count}}
} else {
catch {if {![regexp -- $pattern $line]} {puthelp "NOTICE $nickname :${file}:${line}" ; incr count}}
}
}
default {
if {[regexp -- {i} $option]} {
catch {if {[regexp -nocase -- $pattern $line]} {puthelp "NOTICE $nickname :${file}:${line}" ; incr count}}
} else {
catch {if {[regexp -- $pattern $line]} {puthelp "NOTICE $nickname :${file}:${line}" ; incr count}}
}
}
}
}
close $fp
if {$count == $limit} {
puthelp "NOTICE $nickname :--- More than $limit hits, list truncated"
break
}
}
puthelp "NOTICE $nickname :--- End of list - $count matches"
}
proc grep:dcc {handle idx arguments} {
set arguments [grep:clean $arguments] ;# clean the arguments
set offset 0 ;# for optional parameters
set limit 500 ;# the default output limit
set option [join [lindex $arguments 0]] ;# first check
if {[regexp -- {^-(\S+)$} [lindex $arguments 0] tmp option]} {
if {[regexp -- {^([ivl]{1,3})$} $option]} {
if {[regexp -- {l} $option]} {
set offset 2
regexp -- {^([0-9]+)$} [join [lindex $arguments 1]] tmp limit
} else {
set offset 1
}
} else {
putidx $idx "unknown option \"$option\""
return
}
}
if {[llength $arguments] < [expr $offset + 2]} {
putidx $idx "Usage: grep \[-ivl num\] resource pattern"
putidx $idx " i - ignore upper/lower case distinctions during comparisons"
putidx $idx " v - invert the sense of matching, to select non-matching lines"
putidx $idx " l num - list max <num> amounts of lines (20 by default)"
putidx $idx " resource - an absolute or relative path"
putidx $idx " ie: /dev/null/*"
putidx $idx " pattern - a perl compatible regular expression (PCRE)"
putidx $idx " ie: #channel2|user9|Cleanup or \[a-z\]\{7\}\\s\[0-9\]\{2\}"
return
}
putcmdlog "\$${handle}\$ $::lastbind $arguments"
set resource [join [lindex [grep:clean $arguments] $offset]]
set pattern [join [lrange $arguments [expr $offset + 1] end]]
set count 0
catch {[glob -- $resource]} error
if {[regexp -nocase -- {no files matched glob pattern "(.*?)"} $error]} {
putidx $idx "--- End of list - $count matches"
return
}
foreach file "[glob -- $resource]" {
if {[file isdirectory "$file"]} {continue}
set fp [open $file r]
while {![eof $fp]} {
gets $fp line
if {$count == $limit} {break}
switch -regexp -- $option {
{v} {
if {[regexp -- {i} $option]} {
catch {if {![regexp -nocase -- $pattern $line]} {putidx $idx "${file}:${line}" ; incr count}}
} else {
catch {if {![regexp -- $pattern $line]} {putidx $idx "${file}:${line}" ; incr count}}
}
}
default {
if {[regexp -- {i} $option]} {
catch {if {[regexp -nocase -- $pattern $line]} {putidx $idx "${file}:${line}" ; incr count}}
} else {
catch {if {[regexp -- $pattern $line]} {putidx $idx "${file}:${line}" ; incr count}}
}
}
}
}
close $fp
if {$count == $limit} {
putidx $idx "--- More than $limit hits, list truncated"
break
}
}
putidx $idx "--- End of list - $count matches"
}
proc grep:clean {i} {
regsub -all -- \\\\ $i \\\\\\\\ i
regsub -all -- \\\[ $i \\\\\[ i
regsub -all -- \\\] $i \\\\\] i
regsub -all -- \\\} $i \\\\\} i
regsub -all -- \\\{ $i \\\\\{ i
regsub -all -- \\\" $i \\\\\" i
return $i
}
putlog "Script loaded: Grep TCL Port (C) 2004 perpleXa"