grep.src
// grep for grey hack by Plu70
colorRed = "<color=red><b>"
colorGreen = "<color=green><b>"
colorWhite = "<color=white><b>"
colorCyan = "<color=#00FFFF><b>"
colorGold = "<color=#CCCC00FF><b>"
colorBlack = "<color=#000000FF><b>"
colorOrange = "<color=#FF8400FF><b>"
colorLightBlue = "<color=#2382FFFF><b>"
CT = "</color></b>"
shell = get_shell
localmachine = shell.host_computer
currentPath = current_path
get_file = function(input_string)
if input_string == "" or input_string == " " or not input_string then return 0
if input_string.split("/")[0] == "" then
file = localmachine.File(input_string)
else
file = localmachine.File(currentPath+"/"+input_string)
if not file then file = localmachine.File(currentPath+input_string)
end if
if file then return file else return 0
end function
re = {}
re.match = function(regexp, text)
if regexp[0] == "^" then
regexp.pull
return self.match_here(regexp,text)
end if
while text.len > 0
if self.match_here(regexp, text) then return 1
text.pull
end while
return 0
end function
re.match_here = function(regexp, text)
if not regexp then return 1
if regexp.len > 1 and regexp[1] == "*" then
//return self.match_star(regexp[0], regexp[2:], text)
char = regexp.pull
regexp.pull
return self.match_star(char, regexp, text)
end if
if regexp[0] == "$" and not regexp.hasIndex(1) then
if text.hasIndex(1) then return 0 else return 1
end if
if text.len > 0 and (regexp[0] == "." or regexp[0] == text[0]) then
//return self.match_here(regexp[1:], text[1:])
regexp.pull
text.pull
return self.match_here(regexp,text)
end if
return 0
end function
// re.match_star needs work. misses certain cases.
re.match_star = function(c, regexp, text)
while text.len and (text[0] == c or c == ".")
if self.match_here( regexp, text) then return 1
//text = text[1:]
text.pull
end while
return 0
end function
command = {}
command.grep = function(a1, a2, arg3=0, arg4=0)
//code taken from 5hell >> dtools.5pk >> command.grep
//dependencies at top of source. a2 may be a 'piped object.'
//
// grep will first attempt exact match, then indexOf match, then invoke the regex engine.
// this is in attempt to minimize runtime as the regex engine is resource hungry.
//
// uncomment the below line if baked into your own script.
//if not a1 or not a2 or a1 == "-h" or a1 == "help" then return "Usage: grep [string] [file/folder]"+char(10)+"Advanced: grep [string] [object] -- search piped object (shell|file|computer) for string. "+char(10)+"-- piped computer and shell objects will be searched from / directory."+char(10)+"-- piping a file object searches from path of file."+char(10)+"n.b. supports partial matches and limited regex."
gresult = []
gp = function(grep_tar, t_f, use_re)
if t_f.is_folder and t_f.is_binary then
sub_folders = t_f.get_folders
sub_files = t_f.get_files
g_buf = null
for sub in sub_folders
if use_re == true then
if re.match(grep_tar.values, sub.name.values) then gresult.push(colorLightBlue+"Found folder: "+CT+colorOrange+sub.name+CT+colorLightBlue+" in: "+ sub.parent.path + CT)
else
if sub.name == grep_tar or sub.name.indexOf(grep_tar) then gresult.push(colorLightBlue+"Found folder: "+CT+colorOrange+sub.name+CT+colorLightBlue+" in: "+ sub.parent.path + CT)
end if
gp(grep_tar, sub, use_re)
end for
for f in sub_files
if use_re == true then
if re.match(grep_tar.values, f.name.values) then gresult.push(colorLightBlue+"Found file: "+CT+colorOrange+f.name+CT+colorLightBlue+" in: "+ f.parent.path + CT)
else
if f.name == grep_tar or f.name.indexOf(grep_tar) >= 0 then gresult.push(colorLightBlue+"Found file: "+CT+colorOrange+f.name+CT+colorLightBlue+" in: "+ f.parent.path + CT)
end if
gp(grep_tar, f, use_re)
end for
end if
if t_f.is_binary then return
buf = t_f.get_content
if buf == "" then return
lines = buf.split(char(10))
for line in lines
words = line.split(" ")
for word in words
if use_re == true then
if re.match(grep_tar.values, word.values) then gresult.push(colorLightBlue+"Found: "+CT+colorWhite+word+CT+colorLightBlue+" on line "+lines.indexOf(line)+" word "+words.indexOf(word)+" in file: "+ t_f.path + CT)
else
if word == grep_tar or word.indexOf(grep_tar) >= 0 then gresult.push(colorLightBlue+"Found: "+CT+colorWhite+word+CT+colorLightBlue+" on line "+lines.indexOf(line)+" word "+words.indexOf(word)+" in file: "+ t_f.path + CT)
end if
end for
end for
end function
output = "No "+a1+" found."
t_f = null // target file or folder
grep_tar = a1.trim
invoke_regex = false
special_chars = ["*","^","$","."]
for special in special_chars
if grep_tar.indexOf(special) >= 0 then invoke_regex = true
end for
tf_path = a2
if typeof(tf_path) == "string" then
t_f = globals.get_file(tf_path)
if not t_f then return "grep: "+a2+" not found."
end if
// piped object handling. Unused if run as standalone but if baked into your own script, pipe objects to grep them.
if typeof(tf_path) == "file" then t_f = tf_path
if typeof(tf_path) == "computer" then t_f = tf_path.File("/")
if typeof(tf_path) == "shell" then t_f = tf_path.host_computer.File("/")
if typeof(t_f) != "file" then return "grep: "+a2+": unkown type."
// end piped object handling.
gp(grep_tar, t_f, invoke_regex)
if gresult != [] then output = gresult.join(char(10))
return output
end function
// remove/comment the below line if baked into your own script and uncomment the help line further up.
if params.len != 2 then exit("Usage: grep [regex] [path] -- regex is type string and path is file or folder (string_path or object)")
print(command.grep(params[0],params[1])) // standalone
// command.grep( arg1, arg2 ) // baked in
//
// c match any literal char c
// ^ match from beginning of line
// $ match from end of line ($ goes at end of pattern)
// . matches any unicode char
// * matches zero or more of preceding char