try to catch tikz errors; add testing
[latexmk-quieter.git] / latexmk-quieter
blob6f5822f49bd1007721ed1f3030a63e0ac0556267
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 package_error = P"! Package" + (P"l." * R"09"^1 * P" ")
22 biber_error = P{
23         "full";
24         idf = P("I didn't find a database entry") + 1 * V"idf",
25         full = P("WARN") * V"idf",
28 function should_print(s, l)
29         if s.show_next > 0 then
30                 s.show_next = s.show_next - 1
31                 return l
32         end
34         if runaway:match(l) then
35                 s.show_next = 4
36                 return l
37         end
39         if file_line_error:match(l) then
40                 s.show_next = 1
41                 return l
42         end
44         if biber_error:match(l) or package_error:match(l) then
45                 return l
46         end
48         return nil
49 end
51 local file_actions = assert(spawn.new_file_actions())
52 local attr = assert(spawn.new_attr())
53 local child_stdin, child_stdout, read, write
54 child_stdin, write = assert(unix.fpipe("e"))
55 read, child_stdout = assert(unix.fpipe("e"))
56 assert(file_actions:adddup2(unix.fileno(child_stdin), 0))
57 assert(file_actions:adddup2(unix.fileno(child_stdout), 1))
58 assert(file_actions:adddup2(1, 2))
60 local pid = assert(spawn.spawnp(arg[1], file_actions, attr,
61         arg, nil))
62 child_stdin:close()
63 child_stdout:close()
64 state = { show_next = 0, }
65 while true do
66         line = read:read('*line')
67         if not line then break end
68         line = should_print(state, line)
69         if line then
70                 io.stderr:write(line .. "\n")
71         end
72 end
74 local why, status
75 _, why, status = unix.waitpid(pid)
77 os.exit(status)