summaryrefslogtreecommitdiff
path: root/luaext/linit.lua
diff options
context:
space:
mode:
Diffstat (limited to 'luaext/linit.lua')
-rw-r--r--luaext/linit.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/luaext/linit.lua b/luaext/linit.lua
new file mode 100644
index 000000000..7b134ba75
--- /dev/null
+++ b/luaext/linit.lua
@@ -0,0 +1,41 @@
+
+rex.new = rex.newPOSIX
+
+util = {}
+
+function util.grep(expr, filename)
+ if not posix.stat(filename, "mode") then
+ return nil
+ end
+ local lines = {}
+ local pat = rex.new(expr)
+ local pos = 1
+ for line in io.lines(filename) do
+ if pat:match(line) then
+ table.insert(lines, pos, line)
+ end
+ pos = pos + 1
+ end
+ if table.getn(lines) == 0 then
+ return nil
+ end
+ return lines
+end
+
+function util.igrep(expr, filename)
+ return ipairs(rex.grep(expr, filename))
+end
+
+function util.bgrep(expr, filename)
+ if not posix.stat(filename, "mode") then
+ return nil
+ end
+ local pat = rex.new(expr)
+ for line in io.lines(filename) do
+ if pat:match(line) then
+ return true
+ end
+ end
+ return false
+end
+