preserver latexmk's exit status
[latexmk-quieter.git] / latexmk-quieter
blobd70badd1f6d88d7403e69924211f6b3c7629a9c8
1 #!/usr/bin/env lua
3 local spawn = require('spawn.posix')
4 local unix = require('unix')
5 local lpeg = require('lpeg')
7 local V, R, P, S, C, Ct, Cg, Cmt, Cb =
8         lpeg.V, lpeg.R, lpeg.P, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cg, lpeg.Cmt,
9         lpeg.Cb
11 file_line_error = P{
12         "full";
13         cnc = (P":" * (R"09")^1 * P": "),
14         fn = (P(1) - (P" " + V"cnc"))^1,
15         full = V"fn" * V"cnc",
18 runaway = P"Runaway argument?" * (-1)
20 biber_error = P{
21         "full";
22         idf = P("I didn't find a database entry") + 1 * V"idf",
23         full = P("WARN") * V"idf",
26 function should_print(s, l)
27         if s.show_next > 0 then
28                 s.show_next = s.show_next - 1
29                 return l
30         end
32         if runaway:match(l) then
33                 s.show_next = 4
34                 return l
35         end
37         if file_line_error:match(l) then
38                 s.show_next = 1
39                 return l
40         end
42         if biber_error:match(l) then
43                 return l
44         end
46         return nil
47 end
49 local file_actions = assert(spawn.new_file_actions())
50 local attr = assert(spawn.new_attr())
51 local child_stdin, child_stdout, read, write
52 child_stdin, write = assert(unix.fpipe("e"))
53 read, child_stdout = assert(unix.fpipe("e"))
54 assert(file_actions:adddup2(unix.fileno(child_stdin), 0))
55 assert(file_actions:adddup2(unix.fileno(child_stdout), 1))
56 assert(file_actions:adddup2(1, 2))
58 local pid = assert(spawn.spawnp(arg[1], file_actions, attr,
59         arg, nil))
60 child_stdin:close()
61 child_stdout:close()
62 state = { show_next = 0, }
63 while true do
64         line = read:read('*line')
65         if not line then break end
66         line = should_print(state, line)
67         if line then
68                 io.stderr:write(line .. "\n")
69         end
70 end
72 local why, status
73 _, why, status = unix.waitpid(pid)
75 os.exit(status)