-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdbus-codegen
executable file
·159 lines (142 loc) · 4.9 KB
/
cdbus-codegen
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env lua
-- cdbus-codegen - generate cdbus bindings from dbus xml files
-- SPDX-FileCopyrightText: Copyright © 2023 Nedko Arnaudov
-- SPDX-License-Identifier: GPL-3
local lxp = require('lxp')
local table = require('table')
interfaces = {}
local interface
local callable
cdbus_codegen = {}
cdbus_codegen.StartElement = function (parser, name, attr)
if (name == "interface") then
interface = {}
interface.name = attr['name']
-- interface.attr = attr
-- for k, v in pairs(attr) do print(" ", k, v1) end
end
if (name == "method") then
callable = {}
callable.typestr = name
callable.name = attr['name']:gsub("%s+", "")
--print("method", interface.name, callable.name)
--for k, v in pairs(attr) do print(" ", k, v1) end
end
if (name == "signal") then
callable = {}
callable.typestr = name
callable.name = attr['name']
--print("signal", interface.name, signal.name)
end
local arg
if (name == "arg") then
arg = {}
if attr.name then arg.name = attr.name end
arg.type = attr.type
arg.direction = attr.direction
--print(" arg", arg.name, arg.type, arg.direction)
callable.args = callable.args or {}
callable.args[#callable.args + 1] = arg
end
end
cdbus_codegen.EndElement = function (parser, name)
if (name == "interface") then
interfaces[interface.name] = interface
interface = nil
end
if (name == "method") then
interface.methods = interface.methods or {}
interface.methods[#interface.methods + 1] = callable
interface.callables = interface.callables or {}
interface.callables[#interface.callables + 1] = callable
callable = nil
end
if (name == "signal") then
interface.signals = interface.signals or {}
interface.signals[#interface.signals + 1] = callable
interface.callables = interface.callables or {}
interface.callables[#interface.callables + 1] = callable
callable = nil
end
end
p = lxp.new(cdbus_codegen)
for l in io.lines() do -- iterate lines
p:parse(l) -- parses the line
-- p:parse("\n") -- parses the end of line
end
p:parse() -- finishes the document
p:close() -- closes the parser
for name, iface in pairs(interfaces) do
--print(("interface: \"%s\""):format(name))
for _, callable in pairs(iface.callables) do
local rettype
local argtokens = {}
if callable.args then
for _, arg in ipairs(callable.args) do
if not arg.name and arg.direction == "out" then
rettype = arg.type
else
argtokens[#argtokens + 1] = ("%s %s %s"):format(arg.name, arg.type, arg.direction)
end
end
end
if callable.typestr == "method" then rettype = "void" end
if rettype then rettype = " => " .. rettype else rettype = "" end
--print(("%s %s(%s)%s"):format(callable.typestr, callable.name, table.concat(argtokens, ", "), rettype))
end
end
print("/* This data is GENERATED from D-Bus interface description XML")
print(" * Edit by hand only if you know what you are doing */")
print()
namespace = tostring(arg[1]) .. "_cdbus_"
for name, iface in pairs(interfaces) do
for _, callable in pairs(iface.callables) do
local typestr2
local direction2
if callable.typestr == "method" then
typestr2 = "METHOD"
elseif callable.typestr == "signal" then
typestr2 = "SIGNAL"
end
print(("CDBUS_%s_ARGS_BEGIN(%s, \"\")"):format(typestr2, callable.name))
if callable.args then
for _, arg in ipairs(callable.args) do
if arg.name then
if callable.typestr == "method" then
typestr2 = "METHOD"
direction2 = "_" .. arg.direction:upper()
elseif callable.typestr == "signal" then
typestr2 = "SIGNAL"
direction2 = ""
end
print((" CDBUS_%s_ARG_DESCRIBE%s(\"%s\", \"%s\", \"\")"): format(typestr2, direction2, arg.name, arg.type))
end
end
end
print(("CDBUS_%s_ARGS_END"):format(typestr2))
print()
end
print("CDBUS_METHODS_BEGIN")
for _, method in ipairs(iface.methods) do
print("CDBUS_METHOD_DESCRIBE(" .. method.name .. ", " .. namespace .. method.name .. "_dbus)")
end
print("CDBUS_METHODS_END")
print()
if iface.signals then
print("CDBUS_SIGNALS_BEGIN")
for _, signal in ipairs(iface.signals) do
print("CDBUS_SIGNAL_DESCRIBE(" .. signal.name .. ")")
end
print("CDBUS_SIGNALS_END")
end
print()
local iface_varname = namespace .. "interface_" .. iface.name:gsub("%.", "_")
local iface_name_define = namespace:upper() .. "IFACE_" .. iface.name:gsub("%.", "_"):upper()
print("#define " .. iface_name_define .. " \"" .. iface.name .. "\"")
print()
if not iface.signals then
print(("CDBUS_INTERFACE_DEFAULT_HANDLER_METHODS_ONLY(%s, %s)"):format(iface_varname, iface_name_define))
else
print(("CDBUS_INTERFACE_DEFAULT_HANDLER_METHODS_AND_SIGNALS(%s, %s)"):format(iface_varname, iface_name_define))
end
end