11#! /usr/bin/env lua
22
3- M = " malformed"
3+ P , M = " patched " , " malformed"
44local function patch (pf )
55 local ol ,bl ,cp ,of ,bf ,h ,e = 0 ,0
6- local function parseFileName (l )local first = l :sub (5 ,5 )
7- if first == ' "' or first == " '" then return l :match (first .. " ([^" .. first .. " ]+)" .. first ,5 )
8- else local words = {}for v in string.gmatch (l ," %S+" )do table.insert (words ,v )end return words [2 ]end end
6+ local function parseFileName (l )local f = l :sub (5 ,5 )
7+ if f == ' "' or f == " '" then return l :match (f .. " ([^" .. f .. " ]+)" .. f ,5 )
8+ else f = {}for v in string.gmatch (l ," %S+" )do table.insert (f ,v )end return f [2 ]end end
99 local function setPatchFile (f ) -- TODO: write patch to temporary file first
1010 local o ,b local function abort ()print (e )if o then o :close ()end if b then b :close ()end return false end
1111 o ,e = io.open (f )if not o then return abort ()end
@@ -14,27 +14,24 @@ local function patch(pf)
1414 o ,e = io.open (f ," w" )if not o then return abort ()end
1515 b ,e = io.open (f .. " .orig" )if not b then return abort ()end
1616 if of then of :close ()end if bf then bf :close ()end of ,ol ,bf ,bl ,h = o ,0 ,b ,0 ,nil return true end
17- local function writeRemainder ()if h then if (h .o .l > 0 and h .o .s > 0 )and (h .o .l ~= bl - (h .o .s - 1 )or h .n .l ~= ol - (h .n .s - 1 ))then print (M )os.exit (1 )else print (" patched " )end end
17+ local function writeRemainder ()if h then if (h .o .l > 0 and h .o .s > 0 )and (h .o .l ~= bl - (h .o .s - 1 )or h .n .l ~= ol - (h .n .s - 1 ))then print (M )os.exit (1 )else print (P )end end
1818 if of then local rest = bf :read (" *l" )while rest do of :write (rest .. ' \n ' )rest = bf :read (" *l" )end of :close ()of ,ol = nil ,0 end bf :close ()bf ,bl ,h = nil ,0 ,nil end
1919 local n = pf :read (" *l" )while n do
2020 local l ,ls = n n = pf :read (" *l" )ls = l :sub (1 ,1 )
2121 if ls == ' +' then
22- if l :sub (2 ,4 )== " ++ " then -- ignore
23- else if of then local nl = n and n :match (" ^\\ No newline at end of file" )of :write (l :sub (2 ).. (nl and " " or ' \n ' ))ol = ol + 1 end end
22+ if l :sub (2 ,4 )~= " ++ " then of :write (l :sub (2 ).. (n and n :match (" ^\\ No newline at end of file" )and " " or ' \n ' ))ol = ol + 1 end
2423 elseif ls == ' -' then
25- if l :sub (2 ,4 )== " -- " then local filename = parseFileName (l )if bf then writeRemainder ()end
26- if setPatchFile (filename )then cp = filename print (" \n '" .. cp .. " ':" )end
27- else local cmp = bf :read (" *l" )bl = bl + 1 if cmp ~= l :sub (2 )then print (M )os.exit (1 )end end
24+ if l :sub (2 ,4 )== " -- " then local fn = parseFileName (l )if bf then writeRemainder ()end if setPatchFile (fn )then cp = fn print (" \n '" .. cp .. " ':" )end
25+ else bl = bl + 1 if bf :read (" *l" )~= l :sub (2 )then print (M )os.exit (1 )end end
2826 elseif ls == ' ' then
29- if bf then local cmp , line = bf :read (" *l" ),l :sub (2 )bl = bl + 1 if cmp == line then if of then of :write (line .. ' \n ' )ol = ol + 1 end else print (M )os.exit (1 )end end
27+ if bf then local c , i = bf :read (" *l" ),l :sub (2 )bl = bl + 1 if c == i then if of then of :write (i .. ' \n ' )ol = ol + 1 end else print (M )os.exit (1 )end end
3028 elseif ls == ' @' then if l :sub (2 ,3 )== " @ " then
3129 local function parseHunkHeader ()
32- local function split (i ,d )local r = {}for m in (i .. d ):gmatch (" (.-)" .. d )do table.insert (r ,m )end return r end
33- local s ,o ,w = split (l ," " ),{},{}
34- if s [2 ]then o = split (s [2 ]," ," )end if s [3 ]then w = split (s [3 ]," ," )end
30+ local function split (i ,d )local r = {}for m in (i .. d ):gmatch (" (.-)" .. d )do table.insert (r ,m )end return r end
31+ local s ,o ,w = split (l ," " ),{},{}if s [2 ]then o = split (s [2 ]," ," )end if s [3 ]then w = split (s [3 ]," ," )end
3532 if o [1 ]then o [1 ]= o [1 ]:sub (2 )end if w [1 ]then w [1 ]= w [1 ]:sub (2 )end
3633 return {o = {s = tonumber (o [1 ]),l = tonumber (o [2 ])or 1 },n = {s = tonumber (w [1 ]),l = tonumber (w [2 ])or 1 }}end
37- if h then if h .o .l ~= bl - (h .o .s - 1 )or h .n .l ~= ol - (h .n .s - 1 )then print (M )os.exit (1 )else print (" patched " )end end
34+ if h then if h .o .l ~= bl - (h .o .s - 1 )or h .n .l ~= ol - (h .n .s - 1 )then print (M )os.exit (1 )else print (P )end end
3835 h = parseHunkHeader ()if h then
3936 io.write (" \t @@ -" .. h .o .s .. " ," .. h .o .l .. " +" .. h .n .s .. " ," .. h .n .l .. " @@: " )
4037 local s = h .o .s > 0 and (h .o .s - 1 )- bl or 0 if s < 0 then print (M )os.exit (1 )end
@@ -45,4 +42,4 @@ local function patch(pf)
4542if # arg < 1 then print (arg [- 1 ].. " " .. arg [0 ].. [[ diff...
4643
4744Patch unified difference file(s)]] )os.exit (1 )end
48- for _ ,a in ipairs (arg ) do local f ,e = io.open (a ) if f then patch (f )f :close ()else print (e )end end
45+ for _ ,a in ipairs (arg )do local f ,e = io.open (a )if f then patch (f )f :close ()else print (e )end end
0 commit comments