1#!/usr/bin/lua5.2
2
3usedlines = {c={}, n={}}
4useddefs = {c={}, n={}}
5
6function AddElem(tab, q)
7  if (tab.c[q]) then
8    tab.c[q] = tab.c[q] + 1
9  else
10    tab.c[q] = 1
11    tab.n[#tab.n+1]=q
12  end
13end
14
15function PrintTab(tab)
16  table.sort(tab.n)
17  for _,n in ipairs(tab.n) do
18    --print(tab.c[n], n)
19    print(n)
20  end
21end
22
23
24function noifdef(f)
25  local out = {}
26  local changed = false
27  for l in io.lines(f) do
28    local n = l:gsub("^#ifdef ([%w_]+)", "#if defined(%1)")
29    n = n:gsub("^#ifndef ([%w_]+)", "#if !defined(%1)")
30    out[#out+1] = (n)
31    if l ~= n then
32      --print(l , "-->", n)
33      changed = true
34    end
35
36    if n:match("^#if") then
37      local q = n:gsub("%/%*.+%*%/", "")
38      q = q:gsub("%s+$", "")
39      q = q:gsub("^%s+", "")
40      q = q:gsub("%s+", " ")
41      AddElem(usedlines, q)
42
43      for w in q:gmatch("%(%s*([%w_]+)%s*%)") do
44        AddElem(useddefs, w)
45      end
46    end
47  end
48
49  if changed then
50    local fi = io.open(f, "w")
51    for _,l in pairs(out) do
52      fi:write(l .. "\n")
53    end
54    fi:close()
55    print(f .. " rewritten")
56  end
57
58  -- print(#out .. " lines processed")
59end
60
61
62path = path or ""
63noifdef(path .. "src/civetweb.c")
64noifdef(path .. "src/civetweb_private_lua.h")
65noifdef(path .. "src/main.c")
66noifdef(path .. "src/md5.inl")
67noifdef(path .. "src/mod_duktape.inl")
68noifdef(path .. "src/mod_http2.inl")
69noifdef(path .. "src/mod_lua.inl")
70noifdef(path .. "src/mod_lua_shared.inl")
71noifdef(path .. "src/mod_zlib.inl")
72noifdef(path .. "src/sha1.inl")
73noifdef(path .. "src/timer.inl")
74noifdef(path .. "src/wolfssl_extras.inl")
75noifdef(path .. "src/response.inl")
76noifdef(path .. "src/handle_form.inl")
77
78--PrintTab(usedlines)
79
80--print("Defines used")
81PrintTab(useddefs)
82
83
84
85
86