-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-protocol-ids
More file actions
executable file
·73 lines (61 loc) · 1.85 KB
/
update-protocol-ids
File metadata and controls
executable file
·73 lines (61 loc) · 1.85 KB
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
#! /usr/bin/env lua
--
-- update-protocol-ids
-- Copyright (C) 2016-2017, 2020 Adrian Perez <aperez@igalia.com>
--
-- Distributed under terms of the Apache License 2.0.
--
local help_msg = [[
Usage: update-protocol-ids [--help | -h | (--enum|--defs)]
Parse the input of the "ndpi_protocol_ids.h" header included as part of
the nDPI distribution and generate Lua code that mimics the definitions
of protocol type constants found in the header.
Input is read from standard input and written to standard output.
]]
if #arg == 0 or arg[1] == "--help" or arg[1] == "-h" then
io.stderr:write(help_msg)
os.exit(0)
end
local PATTERNS = {
["--enum"] = "NDPI_([^%s]+)%s*=%s*([%d_]+)";
["--defs"] = "^%#define%s+NDPI_([^%s]+)%s+([%d_]+)";
}
local item_pattern = PATTERNS[arg[1]]
if not item_pattern then
io.stderr:write(help_msg)
os.exit(1)
end
--
-- Parse the input header
--
local ref_pattern = "^%#define%s+NDPI_([^%s]+)%s+NDPI_([%a%d_]+)$"
local items = {}
local references = {}
for line in io.lines() do
local name, value = line:match(item_pattern)
if name and value and name ~= "LAST_IMPLEMENTED_PROTOCOL" then
table.insert(items, { name = name, value = tonumber(value) })
else
name, value = line:match(ref_pattern)
if name and value and value ~= "LAST_IMPLEMENTED_PROTOCOL" then
table.insert(references, { name = name, value = value })
end
end
end
table.sort(items, function (a, b) return a.value < b.value end)
--
-- Generate Lua module
--
print("-- Generated by ljdnpi's tools/update-protocol-ids script")
print("local T = {")
for _, item in ipairs(items) do
print(" [" .. item.value .. "] = \"" .. item.name .. "\",")
end
for _, item in ipairs(items) do
print(" " .. item.name .. " = " .. item.value .. ",")
end
print("}")
for _, item in ipairs(references) do
print("T." .. item.name .. " = T." .. item.value)
end
print("return T")