11146
|
1 # This programs takes a C header file as the input and produces:
|
|
2 #
|
|
3 # with option --mode=xml: xml dbus specification
|
|
4 # with option --mode=c: C wrappers
|
|
5 #
|
|
6
|
|
7 import re
|
|
8 import string
|
|
9 import sys
|
|
10
|
|
11
|
|
12 # list of object types
|
|
13
|
11171
|
14 # objecttypes = []
|
11146
|
15
|
11171
|
16 # for objecttype in file("dbus-auto-structs.txt"):
|
|
17 # objecttypes.append(objecttype.strip())
|
11146
|
18
|
|
19 # a dictionary of simple types
|
|
20 # each TYPE maps into a pair (dbus-type-name, compatible-c-type-name)
|
|
21 # if compatible-c-type-name is None then it is the same as TYPE
|
|
22
|
11171
|
23 # simpletypes = {
|
|
24 # "int" : ("i", None),
|
|
25 # "gint" : ("i", None),
|
|
26 # "guint" : ("u", None),
|
|
27 # "gboolean" : ("i", "int")
|
|
28 # }
|
11146
|
29
|
11187
|
30
|
11171
|
31 simpletypes = ["int", "gint", "guint", "gboolean"]
|
|
32
|
|
33 # for enum in file("dbus-auto-enums.txt"):
|
|
34 # simpletypes[enum.strip()] = ("i", "int")
|
11146
|
35
|
|
36 # functions that shouldn't be exported
|
|
37
|
11328
|
38 excluded = ["gaim_conv_placement_add_fnc",
|
11187
|
39 "gaim_presence_add_list"]
|
|
40
|
|
41 stringlists = []
|
11146
|
42
|
|
43 pointer = "#pointer#"
|
|
44 myexception = "My Exception"
|
|
45
|
|
46 def ctopascal(name):
|
|
47 newname = ""
|
|
48 for word in name.split("_"):
|
|
49 newname += word.capitalize()
|
|
50 return newname
|
|
51
|
11241
|
52 class Parameter:
|
|
53 def __init__(self, type, name):
|
|
54 self.name = name
|
|
55 self.type = type
|
11146
|
56
|
11241
|
57 def fromtokens(tokens, parameternumber = -1):
|
11187
|
58 if len(tokens) == 0:
|
11146
|
59 raise myexception
|
11187
|
60 if (len(tokens) == 1) or (tokens[-1] == pointer):
|
11241
|
61 if parameternumber >= 0:
|
|
62 return Parameter(tokens, "param%i" % parameternumber)
|
|
63 else:
|
|
64 raise myexception
|
11187
|
65 else:
|
11241
|
66 return Parameter(tokens[:-1], tokens[-1])
|
|
67
|
|
68 fromtokens = staticmethod(fromtokens)
|
|
69
|
|
70 class Binding:
|
|
71 def __init__(self, functiontext, paramtexts):
|
|
72 self.function = Parameter.fromtokens(functiontext.split())
|
|
73
|
|
74 if self.function.name in excluded:
|
|
75 raise myexception
|
|
76
|
|
77 self.params = []
|
|
78 for i in range(len(paramtexts)):
|
|
79 self.params.append(Parameter.fromtokens(paramtexts[i].split(), i))
|
|
80
|
|
81 self.call = "%s(%s)" % (self.function.name,
|
|
82 ", ".join([param.name for param in self.params]))
|
|
83
|
|
84
|
|
85 def process(self):
|
|
86 for param in self.params:
|
|
87 self.processinput(param.type, param.name)
|
|
88
|
|
89 self.processoutput(self.function.type, "RESULT")
|
|
90 self.flush()
|
|
91
|
|
92
|
|
93 def processinput(self, type, name):
|
|
94 const = False
|
|
95 if type[0] == "const":
|
|
96 type = type[1:]
|
|
97 const = True
|
|
98
|
|
99 if len(type) == 1:
|
|
100 # simple types (int, gboolean, etc.) and enums
|
|
101 if (type[0] in simpletypes) or (type[0].startswith("Gaim")):
|
|
102 return self.inputsimple(type, name)
|
|
103
|
|
104
|
|
105 # va_list, replace by NULL
|
|
106 if type[0] == "va_list":
|
|
107 return self.inputvalist(type, name)
|
|
108
|
|
109 # pointers ...
|
|
110 if (len(type) == 2) and (type[1] == pointer):
|
|
111 # strings
|
|
112 if type[0] == "char":
|
|
113 if const:
|
|
114 return self.inputstring(type, name)
|
|
115 else:
|
|
116 raise myexception
|
|
117
|
|
118 elif type[0] == "GHashTable":
|
|
119 return self.inputhash(type, name)
|
|
120
|
|
121 # known object types are transformed to integer handles
|
|
122 elif type[0].startswith("Gaim"):
|
|
123 return self.inputgaimstructure(type, name)
|
|
124
|
|
125 # unknown pointers are always replaced with NULL
|
|
126 else:
|
|
127 return self.inputpointer(type, name)
|
|
128 return
|
|
129
|
|
130 raise myexception
|
|
131
|
|
132
|
|
133 def processoutput(self, type, name):
|
|
134 # the "void" type is simple ...
|
|
135 if type == ["void"]:
|
|
136 return self.outputvoid(type, name)
|
|
137
|
|
138 const = False
|
|
139 if type[0] == "const":
|
|
140 type = type[1:]
|
|
141 const = True
|
|
142
|
|
143 # a string
|
|
144 if type == ["char", pointer]:
|
|
145 return self.outputstring(type, name, const)
|
|
146
|
|
147 # simple types (ints, booleans, enums, ...)
|
|
148 if (len(type) == 1) and \
|
|
149 ((type[0] in simpletypes) or (type[0].startswith("Gaim"))):
|
|
150 return self.outputsimple(type, name)
|
|
151
|
|
152 # pointers ...
|
|
153 if (len(type) == 2) and (type[1] == pointer):
|
|
154
|
|
155 # handles
|
|
156 if type[0].startswith("Gaim"):
|
|
157 return self.outputgaimstructure(type, name)
|
|
158
|
|
159 if type[0] in ["GList", "GSList"]:
|
|
160 return self.outputlist(type, name)
|
|
161
|
|
162 raise myexception
|
|
163
|
11146
|
164
|
11241
|
165 class ClientBinding (Binding):
|
|
166 def __init__(self, functiontext, paramtexts, knowntypes, headersonly):
|
|
167 Binding.__init__(self, functiontext, paramtexts)
|
|
168 self.knowntypes = knowntypes
|
|
169 self.headersonly = headersonly
|
|
170 self.paramshdr = []
|
|
171 self.decls = []
|
|
172 self.inputparams = []
|
|
173 self.outputparams = []
|
|
174 self.returncode = []
|
|
175
|
|
176 def flush(self):
|
|
177 print "%s %s(%s)" % (self.functiontype, self.function.name,
|
|
178 ", ".join(self.paramshdr)),
|
|
179
|
|
180 if self.headersonly:
|
|
181 print ";"
|
|
182 return
|
|
183
|
|
184 print "{"
|
|
185
|
|
186 for decl in self.decls:
|
|
187 print decl
|
|
188
|
|
189 print 'dbus_g_proxy_call(gaim_proxy, "%s", NULL,' % ctopascal(self.function.name)
|
|
190
|
|
191 for type_name in self.inputparams:
|
11277
|
192 print "%s, %s, " % type_name,
|
11241
|
193 print "G_TYPE_INVALID,"
|
|
194
|
|
195 for type_name in self.outputparams:
|
11277
|
196 print "%s, &%s, " % type_name,
|
11241
|
197 print "G_TYPE_INVALID);"
|
|
198
|
|
199 for code in self.returncode:
|
|
200 print code
|
|
201
|
|
202 print "}\n"
|
|
203
|
|
204
|
|
205 def definegaimstructure(self, type):
|
|
206 if (self.headersonly) and (type[0] not in self.knowntypes):
|
|
207 print "struct _%s;" % type[0]
|
|
208 print "typedef struct _%s %s;" % (type[0], type[0])
|
|
209 self.knowntypes.append(type[0])
|
11146
|
210
|
11277
|
211 # fixme: import the definitions of the enumerations from gaim
|
|
212 # header files
|
11241
|
213 def definegaimenum(self, type):
|
|
214 if (self.headersonly) and (type[0] not in self.knowntypes) \
|
|
215 and (type[0] not in simpletypes):
|
|
216 print "typedef int %s;" % type[0]
|
|
217 self.knowntypes.append(type[0])
|
|
218
|
|
219 def inputsimple(self, type, name):
|
|
220 self.paramshdr.append("%s %s" % (type[0], name))
|
11277
|
221 self.inputparams.append(("G_TYPE_INT", name))
|
11241
|
222 self.definegaimenum(type)
|
|
223
|
|
224 def inputvalist(self, type, name):
|
11277
|
225 self.paramshdr.append("va_list %s_NULL" % name)
|
11241
|
226
|
|
227 def inputstring(self, type, name):
|
|
228 self.paramshdr.append("const char *%s" % name)
|
11277
|
229 self.inputparams.append(("G_TYPE_STRING", name))
|
11241
|
230
|
|
231 def inputgaimstructure(self, type, name):
|
11277
|
232 self.paramshdr.append("const %s *%s" % (type[0], name))
|
|
233 self.inputparams.append(("G_TYPE_INT", "GPOINTER_TO_INT(%s)" % name))
|
11241
|
234 self.definegaimstructure(type)
|
|
235
|
|
236 def inputpointer(self, type, name):
|
11277
|
237 name += "_NULL"
|
|
238 self.paramshdr.append("const %s *%s" % (type[0], name))
|
|
239 self.inputparams.append(("G_TYPE_INT", "0"))
|
11241
|
240
|
|
241 def inputhash(self, type, name):
|
11277
|
242 self.paramshdr.append("const GHashTable *%s" % name)
|
|
243 self.inputparams.append(('dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)', name))
|
11241
|
244
|
|
245 def outputvoid(self, type, name):
|
|
246 self.functiontype = "void"
|
|
247
|
|
248 def outputstring(self, type, name, const):
|
|
249 self.functiontype = "char*"
|
|
250 self.decls.append("char *%s = NULL;" % name)
|
11277
|
251 self.outputparams.append(("G_TYPE_STRING", name))
|
11241
|
252 # self.returncode.append("NULLIFY(%s);" % name)
|
|
253 self.returncode.append("return %s;" % name);
|
|
254
|
|
255 def outputsimple(self, type, name):
|
|
256 self.functiontype = type[0]
|
|
257 self.decls.append("%s %s = 0;" % (type[0], name))
|
11277
|
258 self.outputparams.append(("G_TYPE_INT", name))
|
11241
|
259 self.returncode.append("return %s;" % name);
|
|
260 self.definegaimenum(type)
|
|
261
|
11277
|
262 # we could add "const" to the return type but this would probably
|
|
263 # be a nuisance
|
11241
|
264 def outputgaimstructure(self, type, name):
|
|
265 name = name + "_ID"
|
|
266 self.functiontype = "%s*" % type[0]
|
|
267 self.decls.append("int %s = 0;" % name)
|
11277
|
268 self.outputparams.append(("G_TYPE_INT", "%s" % name))
|
11241
|
269 self.returncode.append("return (%s*) GINT_TO_POINTER(%s);" % (type[0], name));
|
|
270 self.definegaimstructure(type)
|
|
271
|
|
272 def outputlist(self, type, name):
|
11277
|
273 self.functiontype = "%s*" % type[0]
|
|
274 self.decls.append("GArray *%s;" % name)
|
|
275 self.outputparams.append(('dbus_g_type_get_collection("GArray", G_TYPE_INT)', name))
|
|
276 self.returncode.append("return garray_int_to_%s(%s);" %
|
|
277 (type[0].lower(), name));
|
11171
|
278
|
11241
|
279
|
|
280 class ServerBinding (Binding):
|
|
281 def __init__(self, functiontext, paramtexts):
|
|
282 Binding.__init__(self, functiontext, paramtexts)
|
|
283 self.dparams = ""
|
|
284 self.cparams = []
|
|
285 self.cdecls = []
|
|
286 self.ccode = []
|
|
287 self.cparamsout = []
|
|
288 self.ccodeout = []
|
|
289 self.argfunc = "dbus_message_get_args"
|
11146
|
290
|
11241
|
291 def flush(self):
|
|
292 print "static DBusMessage*"
|
|
293 print "%s_DBUS(DBusMessage *message_DBUS, DBusError *error_DBUS) {" % \
|
|
294 self.function.name
|
|
295
|
|
296 print "DBusMessage *reply_DBUS;"
|
|
297
|
|
298 for decl in self.cdecls:
|
|
299 print decl
|
|
300
|
|
301 print "%s(message_DBUS, error_DBUS, " % self.argfunc,
|
|
302 for param in self.cparams:
|
|
303 print "DBUS_TYPE_%s, &%s," % param,
|
|
304 print "DBUS_TYPE_INVALID);"
|
|
305
|
|
306 print "CHECK_ERROR(error_DBUS);"
|
|
307
|
|
308 for code in self.ccode:
|
|
309 print code
|
|
310
|
|
311 print "reply_DBUS = dbus_message_new_method_return (message_DBUS);"
|
|
312
|
|
313 print "dbus_message_append_args(reply_DBUS, ",
|
|
314 for param in self.cparamsout:
|
|
315 if type(param) is str:
|
|
316 print "%s, " % param
|
|
317 else:
|
|
318 print "DBUS_TYPE_%s, &%s, " % param,
|
|
319 print "DBUS_TYPE_INVALID);"
|
|
320
|
|
321 for code in self.ccodeout:
|
|
322 print code
|
|
323
|
|
324 print "return reply_DBUS;\n}\n"
|
|
325
|
|
326
|
|
327 def addstring(self, *items):
|
|
328 for item in items:
|
|
329 self.dparams += item + r"\0"
|
|
330
|
|
331 def addintype(self, type, name):
|
|
332 self.addstring("in", type, name)
|
|
333
|
|
334 def addouttype(self, type, name):
|
|
335 self.addstring("out", type, name)
|
11171
|
336
|
11146
|
337
|
11241
|
338 # input parameters
|
11146
|
339
|
11241
|
340 def inputsimple(self, type, name):
|
|
341 self.cdecls.append("dbus_int32_t %s;" % name)
|
|
342 self.cparams.append(("INT32", name))
|
|
343 self.addintype("i", name)
|
|
344
|
|
345 def inputvalist(self, type, name):
|
|
346 self.cdecls.append("va_list %s;" % name);
|
|
347 self.ccode.append("%s = NULL;" % name);
|
|
348
|
|
349 def inputstring(self, type, name):
|
|
350 self.cdecls.append("const char *%s;" % name)
|
|
351 self.cparams.append(("STRING", name))
|
|
352 self.ccode .append("NULLIFY(%s);" % name)
|
|
353 self.addintype("s", name)
|
|
354
|
|
355 def inputhash(self, type, name):
|
|
356 self.argfunc = "gaim_dbus_message_get_args"
|
|
357 self.cdecls.append("DBusMessageIter %s_ITER;" % name)
|
|
358 self.cdecls.append("GHashTable *%s;" % name)
|
|
359 self.cparams.append(("ARRAY", "%s_ITER" % name))
|
|
360 self.ccode.append("%s = gaim_dbus_iter_hash_table(&%s_ITER, error_DBUS);" \
|
|
361 % (name, name))
|
|
362 self.ccode.append("CHECK_ERROR(error_DBUS);")
|
|
363 self.ccodeout.append("g_hash_table_destroy(%s);" % name)
|
|
364 self.addintype("a{ss}", name)
|
|
365
|
|
366 def inputgaimstructure(self, type, name):
|
|
367 self.cdecls.append("dbus_int32_t %s_ID;" % name)
|
|
368 self.cdecls.append("%s *%s;" % (type[0], name))
|
|
369 self.cparams.append(("INT32", name + "_ID"))
|
|
370 self.ccode.append("GAIM_DBUS_ID_TO_POINTER(%s, %s_ID, %s, error_DBUS);" % \
|
|
371 (name, name, type[0]))
|
|
372 self.addintype("i", name)
|
|
373
|
|
374 def inputpointer(self, type, name):
|
|
375 self.cdecls.append("dbus_int32_t %s_NULL;" % name)
|
|
376 self.cdecls .append("%s *%s;" % (type[0], name))
|
|
377 self.cparams.append(("INT32", name + "_NULL"))
|
|
378 self.ccode .append("%s = NULL;" % name)
|
|
379 self.addintype("i", name)
|
|
380
|
|
381 # output parameters
|
|
382
|
|
383 def outputvoid(self, type, name):
|
|
384 self.ccode.append("%s;" % self.call) # just call the function
|
|
385
|
|
386 def outputstring(self, type, name, const):
|
|
387 self.cdecls.append("const char *%s;" % name)
|
|
388 self.ccode.append("%s = null_to_empty(%s);" % (name, self.call))
|
|
389 self.cparamsout.append(("STRING", name))
|
|
390 self.addouttype("s", name)
|
|
391 if not const:
|
|
392 self.ccodeout.append("g_free(%s);" % name)
|
|
393
|
|
394 def outputsimple(self, type, name):
|
|
395 self.cdecls.append("dbus_int32_t %s;" % name)
|
|
396 self.ccode.append("%s = %s;" % (name, self.call))
|
|
397 self.cparamsout.append(("INT32", name))
|
|
398 self.addouttype("i", name)
|
|
399
|
|
400 def outputgaimstructure(self, type, name):
|
|
401 self.cdecls.append("dbus_int32_t %s;" % name)
|
|
402 self.ccode .append("GAIM_DBUS_POINTER_TO_ID(%s, %s, error_DBUS);" % (name, self.call))
|
|
403 self.cparamsout.append(("INT32", name))
|
|
404 self.addouttype("i", name)
|
|
405
|
|
406 # GList*, GSList*, assume that list is a list of objects
|
|
407
|
|
408 # fixme: at the moment, we do NOT free the memory occupied by
|
|
409 # the list, we should free it if the list has NOT been declared const
|
|
410
|
|
411 # fixme: we assume that this is a list of objects, not a list
|
|
412 # of strings
|
|
413
|
|
414 def outputlist(self, type, name):
|
|
415 self.cdecls.append("dbus_int32_t %s_LEN;" % name)
|
|
416 self.ccodeout.append("g_free(%s);" % name)
|
|
417
|
|
418 if self.function.name in stringlists:
|
|
419 self.cdecls.append("char **%s;" % name)
|
|
420 self.ccode.append("%s = gaim_%s_to_array(%s, FALSE, &%s_LEN);" % \
|
|
421 (name, type[0], self.call, name))
|
|
422 self.cparamsout.append("DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &%s, %s_LEN" \
|
|
423 % (name, name))
|
|
424 self.addouttype("as", name)
|
|
425 else:
|
|
426 self.cdecls.append("dbus_int32_t *%s;" % name)
|
|
427 self.ccode.append("%s = gaim_dbusify_%s(%s, FALSE, &%s_LEN);" % \
|
|
428 (name, type[0], self.call, name))
|
|
429 self.cparamsout.append("DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &%s, %s_LEN" \
|
|
430 % (name, name))
|
|
431 self.addouttype("ai", name)
|
|
432
|
|
433
|
|
434 class BindingSet:
|
|
435 regexp = r"^(\w[^()]*)\(([^()]*)\)\s*;\s*$";
|
11171
|
436
|
11241
|
437 def __init__(self, inputfile, fprefix):
|
|
438 self.inputiter = iter(inputfile)
|
|
439 self.functionregexp = \
|
|
440 re.compile("^%s(\w[^()]*)\(([^()]*)\)\s*;\s*$" % fprefix)
|
|
441
|
|
442
|
|
443
|
|
444 def process(self):
|
|
445 print "/* Generated by %s. Do not edit! */" % sys.argv[0]
|
|
446
|
|
447 for line in self.inputiter:
|
|
448 words = line.split()
|
|
449 if len(words) == 0: # empty line
|
|
450 continue
|
|
451 if line[0] == "#": # preprocessor directive
|
|
452 continue
|
|
453 if words[0] in ["typedef", "struct", "enum", "static"]:
|
|
454 continue
|
|
455
|
|
456 # accumulate lines until the parentheses are balance or an
|
|
457 # empty line has been encountered
|
|
458 myline = line.strip()
|
|
459 while myline.count("(") > myline.count(")"):
|
|
460 newline = self.inputiter.next().strip()
|
|
461 if len(newline) == 0:
|
|
462 break
|
|
463 myline += " " + newline
|
|
464
|
|
465 # is this a function declaration?
|
|
466 thematch = self.functionregexp.match(
|
|
467 myline.replace("*", " " + pointer + " "))
|
|
468
|
|
469 if thematch is None:
|
|
470 continue
|
|
471
|
|
472 functiontext = thematch.group(1)
|
|
473 paramstext = thematch.group(2).strip()
|
|
474
|
|
475 if (paramstext == "void") or (paramstext == ""):
|
|
476 paramtexts = []
|
|
477 else:
|
|
478 paramtexts = paramstext.split(",")
|
|
479
|
|
480 try:
|
|
481 self.processfunction(functiontext, paramtexts)
|
|
482 except myexception:
|
|
483 sys.stderr.write(myline + "\n")
|
|
484 except:
|
|
485 sys.stderr.write(myline + "\n")
|
|
486 raise
|
|
487
|
|
488 self.flush()
|
|
489
|
|
490 class ServerBindingSet (BindingSet):
|
|
491 def __init__(self, inputfile, fprefix):
|
|
492 BindingSet.__init__(self, inputfile, fprefix)
|
|
493 self.functions = []
|
|
494
|
|
495
|
|
496 def processfunction(self, functiontext, paramtexts):
|
|
497 binding = ServerBinding(functiontext, paramtexts)
|
|
498 binding.process()
|
|
499 self.functions.append((binding.function.name, binding.dparams))
|
|
500
|
|
501 def flush(self):
|
|
502 print "static GaimDBusBinding bindings_DBUS[] = { "
|
|
503 for function, params in self.functions:
|
|
504 print '{"%s", "%s", %s_DBUS},' % \
|
|
505 (ctopascal(function), params, function)
|
|
506
|
|
507 print "{NULL, NULL}"
|
|
508 print "};"
|
|
509
|
|
510 print "#define GAIM_DBUS_REGISTER_BINDINGS(handle) gaim_dbus_register_bindings(handle, bindings_DBUS)"
|
|
511
|
|
512 class ClientBindingSet (BindingSet):
|
|
513 def __init__(self, inputfile, fprefix, headersonly):
|
|
514 BindingSet.__init__(self, inputfile, fprefix)
|
|
515 self.functions = []
|
|
516 self.knowntypes = []
|
|
517 self.headersonly = headersonly
|
|
518
|
|
519 def processfunction(self, functiontext, paramtexts):
|
|
520 binding = ClientBinding(functiontext, paramtexts, self.knowntypes, self.headersonly)
|
|
521 binding.process()
|
|
522
|
|
523 def flush(self):
|
|
524 pass
|
|
525
|
|
526 # Main program
|
|
527
|
|
528 options = {}
|
|
529
|
|
530 for arg in sys.argv[1:]:
|
|
531 if arg[0:2] == "--":
|
|
532 mylist = arg[2:].split("=",1)
|
|
533 command = mylist[0]
|
|
534 if len(mylist) > 1:
|
|
535 options[command] = mylist[1]
|
|
536 else:
|
|
537 options[command] = None
|
11146
|
538
|
11171
|
539 if "export-only" in options:
|
|
540 fprefix = "DBUS_EXPORT\s+"
|
|
541 else:
|
|
542 fprefix = ""
|
11146
|
543
|
11277
|
544 sys.stderr.write("%s: Functions not exported:\n" % sys.argv[0])
|
|
545
|
11241
|
546 if "client" in options:
|
|
547 bindings = ClientBindingSet(sys.stdin, fprefix,
|
|
548 options.has_key("headers"))
|
|
549 else:
|
|
550 bindings = ServerBindingSet(sys.stdin, fprefix)
|
|
551 bindings.process()
|
11146
|
552
|
11241
|
553
|
11146
|
554
|
|
555
|