-
Notifications
You must be signed in to change notification settings - Fork 19
/
luagrep
executable file
·131 lines (105 loc) · 3.44 KB
/
luagrep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!lua
-- Luagrep: A Lua-based regular expression search similar to grep
-- Copyright (c) 2011 DarkWyrm
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--[[
Usage: luagrep [options] PATTERN [filename] [filename2] ...
Options:
-f, --fixed PATTERN is to be treated as a literal string, not a pattern
-i, --ignore-case perform a case-insensitive search
]]
local results = {}
function CharacterEscape(string, set, escapeChar)
-- This performs the same function as the BString::CharacterEscape
-- function in the Haiku API. This is not a cheap function, so use it
-- sparingly
if (not string or (not set) or (not escapeChar) or
type(string) ~= "string" or type(set) ~= "string" or
type(escapeChar) ~= "string") then
return nil
end
local out = {}
local length = string:len()
for i = 1, length do
local currentChar = string:sub(i,i), 1, plain
if (set:find(currentChar, 1, true)) then
table.insert(out, escapeChar)
table.insert(out, currentChar)
else
table.insert(out, currentChar)
end
end
return table.concat(out)
end
function GrepFile(path, searchString, options)
-- actually searches the file
if (not path) then
error("Nil path in GrepFile")
end
local file = io.open(path, "r")
if (not file) then
print("Couldn't open file '" .. path .. "'")
os.exit(1)
end
local data = {}
for line in file:lines() do
table.insert(data, line)
end
local pattern = nil
for i = 1, #data do
local line = data[i]:find(searchString, 1, options["fixed"])
if (line) then
local match = {}
match.file = path
match.line = i
match.string = data[i]
table.insert(results, match)
end
end
end
function PrintResultsNormal(matches)
-- Takes a table and prints them
-- The format is filename:\t<result>
for i = 1, #matches do
local out = {}
out[1] = matches[i].file
out[2] = matches[i].line
out[3] = matches[i].string
print(table.concat(out,":"))
end
end
-----------------------------------------------------------------------------
-- Main script execution
local optionList = {
["-f"] = "fixed",
["--fixed"] = "fixed",
}
local options = {}
local searchPattern = nil
local argIndex = 1
if (arg[1] == "-f" or arg[1] == "--fixed") then
optionList["fixed"] = true
argIndex = 2
end
searchPattern = arg[argIndex]
if (optionList.fixed) then
searchPattern = CharacterEscape(searchPattern, "^$()%.[]*+-?", "%")
end
for i = argIndex + 1, #arg do
GrepFile(arg[i], searchPattern, options)
end
PrintResultsNormal(results)