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