-
Notifications
You must be signed in to change notification settings - Fork 19
/
luare
executable file
·154 lines (126 loc) · 3.93 KB
/
luare
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!lua
-- Luare: A Lua-based regular expression replacement tool similar
-- in function to sed.
-- 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.
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
fixedStringMode = false
if (arg[1] == "-f") then
fixedStringMode = true
end
interactiveMode = false
if (arg[1] == "-i") then
interactiveMode = true
end
if (interactiveMode) then
argIndex = 2
else
local fixed = 0
if (fixedStringMode) then fixed = 1 end
pattern = arg[1 + fixed]
replacement = arg[2 + fixed]
argIndex = 3 + fixed
end
filePath = arg[argIndex]
if (filePath == nil) then
print("Usage:\n\t(Interactive mode) luare -i file [newname]")
print("\t(Shell mode) luare [-f] pattern replacement file [newname] [+line]")
return 0
end
if (interactiveMode) then
print("Enter the pattern to use: ")
pattern = io.stdin:read()
print("Enter the replacement pattern: ")
replacement = io.stdin:read()
end
filePath = arg[argIndex]
newPath = arg[argIndex + 1]
matchLine = arg[argIndex + 2]
if (newPath == nil) then
newPath = filePath.. ".new"
end
if (newPath:sub(1,1) == "+") then
matchLine = tonumber(newPath:sub(2))
newPath = filePath.. ".new"
print("Matching at line " .. matchLine)
else
if (not matchLine) then
matchLine = -1
else
matchLine = tonumber(matchLine)
end
end
if (fixedStringMode) then
pattern = CharacterEscape(pattern, "^$()%.[]*+-?", "%")
replacement = CharacterEscape(replacement, "%", "%")
end
-- Operate on all instances if a line was not specified
if (matchLine < 0) then
local file = io.open(filePath)
if (file == nil) then
print("Couldn't open " .. filePath)
return 1
end
local fileData = file:read("*all*")
file:close()
local outFile = io.open(newPath, "w+")
local newData = fileData:gsub(pattern, replacement);
outFile:write(newData)
outFile:close()
else
local fileTable = {}
file = io.open(filePath)
if (file == nil) then
print("Couldn't open " .. filePath)
return 1
end
for line in file:lines() do
table.insert(fileTable, line)
end
file:close()
if (fileTable[matchLine]) then
fileTable[matchLine] = string.gsub(fileTable[matchLine], pattern,
replacement)
end
local outFile = io.open(newPath, "w+")
local lineCount = table.maxn(fileTable)
for line = 1, lineCount do
outFile:write(fileTable[line] .. "\n")
end
outFile:close()
end