#! /usr/libexec/platform-python
# Copyright (c) 2005-2007 Niels Provos <provos@citi.umich.edu>
# Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
# Generates marshaling code based on libevent.
# 1) use optparse to allow the strategy shell to parse options, and
# to allow the instantiated factory (for the specific output language)
# to parse remaining options
# 2) move the globals into a class that manages execution (including the
# progress outputs that space stderr at the moment)
# 3) emit other languages
_NAME = "event_rpcgen.py"
white = re.compile(r'\s+')
cppcomment = re.compile(r'\/\/.*$')
nonident = re.compile(r'[^a-zA-Z0-9_]')
structref = re.compile(r'^struct\[([a-zA-Z_][a-zA-Z0-9_]*)\]$')
structdef = re.compile(r'^struct +[a-zA-Z_][a-zA-Z0-9_]* *{$')
def TranslateList(mylist, mydict):
return [x % mydict for x in mylist]
# Exception class for parse errors
class RpcGenError(Exception):
# Holds everything that makes a struct
def __init__(self, name):
declare(' Created struct: %s' % name)
def AddEntry(self, entry):
if entry.Tag() in self._tags:
'Entry "%s" duplicates tag number %d from "%s" '
'around line %d' % (entry.Name(), entry.Tag(),
self._tags[entry.Tag()], line_count))
self._entries.append(entry)
self._tags[entry.Tag()] = entry.Name()
declare(' Added entry: %s' % entry.Name())
def EntryTagName(self, entry):
"""Creates the name inside an enumeration for distinguishing data
name = "%s_%s" % (self._name, entry.Name())
def PrintIndented(self, file, ident, code):
"""Takes an array, add indentation to each entry and prints it."""
file.write('%s%s\n' % (ident, entry))
class StructCCode(Struct):
""" Knows how to generate C code for a struct """
def __init__(self, name):
Struct.__init__(self, name)
def PrintTags(self, file):
"""Prints the tag definitions for a structure."""
file.write('/* Tag definition for %s */\n' % self._name)
file.write('enum %s_ {\n' % self._name.lower())
for entry in self._entries:
file.write(' %s=%d,\n' % (self.EntryTagName(entry), entry.Tag()))
file.write(' %s_MAX_TAGS\n' % (self._name.upper()))
def PrintForwardDeclaration(self, file):
file.write('struct %s;\n' % self._name)
def PrintDeclaration(self, file):
file.write('/* Structure declaration for %s */\n' % self._name)
file.write('struct %s_access_ {\n' % self._name)
for entry in self._entries:
dcl = entry.AssignDeclaration('(*%s_assign)' % entry.Name())
entry.GetDeclaration('(*%s_get)' % entry.Name()))
entry.AddDeclaration('(*%s_add)' % entry.Name()))
self.PrintIndented(file, ' ', dcl)
file.write('struct %s {\n' % self._name)
file.write(' struct %s_access_ *base;\n\n' % self._name)
for entry in self._entries:
dcl = entry.Declaration()
self.PrintIndented(file, ' ', dcl)
for entry in self._entries:
file.write(' ev_uint8_t %s_set;\n' % entry.Name())
file.write("""struct %(name)s *%(name)s_new(void);
struct %(name)s *%(name)s_new_with_arg(void *);
void %(name)s_free(struct %(name)s *);
void %(name)s_clear(struct %(name)s *);
void %(name)s_marshal(struct evbuffer *, const struct %(name)s *);
int %(name)s_unmarshal(struct %(name)s *, struct evbuffer *);
int %(name)s_complete(struct %(name)s *);
void evtag_marshal_%(name)s(struct evbuffer *, ev_uint32_t,
const struct %(name)s *);
int evtag_unmarshal_%(name)s(struct evbuffer *, ev_uint32_t,
struct %(name)s *);\n""" % { 'name' : self._name })
# Write a setting function of every variable
for entry in self._entries:
self.PrintIndented(file, '', entry.AssignDeclaration(
self.PrintIndented(file, '', entry.GetDeclaration(
self.PrintIndented(file, '', entry.AddDeclaration(
file.write('/* --- %s done --- */\n\n' % self._name)
def PrintCode(self, file):
' * Implementation of %s\n'
file.write('static struct %(name)s_access_ %(name)s_base__ = {\n' % \
for entry in self._entries:
self.PrintIndented(file, ' ', entry.CodeBase())
' return %(name)s_new_with_arg(NULL);\n'
'%(name)s_new_with_arg(void *unused)\n'
' struct %(name)s *tmp;\n'
' if ((tmp = malloc(sizeof(struct %(name)s))) == NULL) {\n'
' event_warn("%%s: malloc", __func__);\n'
' tmp->base = &%(name)s_base__;\n\n') % { 'name' : self._name })
for entry in self._entries:
self.PrintIndented(file, ' ', entry.CodeInitialize('tmp'))
file.write(' tmp->%s_set = 0;\n\n' % entry.Name())
for entry in self._entries:
self.PrintIndented(file, '', entry.CodeAdd())
for entry in self._entries:
self.PrintIndented(file, '', entry.CodeAssign())
for entry in self._entries:
self.PrintIndented(file, '', entry.CodeGet())
'%(name)s_clear(struct %(name)s *tmp)\n'
'\n') % { 'name' : self._name })
for entry in self._entries:
self.PrintIndented(file, ' ', entry.CodeClear('tmp'))
'%(name)s_free(struct %(name)s *tmp)\n'
'\n') % { 'name' : self._name })
for entry in self._entries:
self.PrintIndented(file, ' ', entry.CodeFree('tmp'))
file.write((' free(tmp);\n'
'%(name)s_marshal(struct evbuffer *evbuf, '
'const struct %(name)s *tmp)'
'{\n') % { 'name' : self._name })
for entry in self._entries:
# Optional entries do not have to be set
file.write(' if (tmp->%s_set) {\n' % entry.Name())
entry.CodeMarshal('evbuf', self.EntryTagName(entry),
'%(name)s_unmarshal(struct %(name)s *tmp, '
' struct evbuffer *evbuf)\n'
' while (evbuffer_get_length(evbuf) > 0) {\n'
' if (evtag_peek(evbuf, &tag) == -1)\n'
'\n') % { 'name' : self._name })
for entry in self._entries:
file.write(' case %s:\n' % self.EntryTagName(entry))
entry.CodeUnmarshal('evbuf',
self.EntryTagName(entry),
file.write(( ' tmp->%s_set = 1;\n' % entry.Name() +
file.write(( ' default:\n'
# Check if it was decoded completely
file.write(( ' if (%(name)s_complete(tmp) == -1)\n'
'\n') % { 'name' : self._name })
file.write(( ' return (0);\n'
# Checking if a structure has all the required data
'%(name)s_complete(struct %(name)s *msg)\n'
'{\n' ) % { 'name' : self._name })
for entry in self._entries:
'if (!msg->%(name)s_set)',
code = TranslateList(code, entry.GetTranslation())
entry.CodeComplete('msg', entry.GetVarName('msg')))
# Complete message unmarshaling
'evtag_unmarshal_%(name)s(struct evbuffer *evbuf, '
'ev_uint32_t need_tag, struct %(name)s *msg)\n'
' struct evbuffer *tmp = evbuffer_new();\n'
' if (evtag_unmarshal(evbuf, &tag, tmp) == -1'
' if (%(name)s_unmarshal(msg, tmp) == -1)\n'
'}\n\n' ) % { 'name' : self._name })
# Complete message marshaling
'evtag_marshal_%(name)s(struct evbuffer *evbuf, ev_uint32_t tag, '
'const struct %(name)s *msg)\n'
' struct evbuffer *buf_ = evbuffer_new();\n'
' assert(buf_ != NULL);\n'
' %(name)s_marshal(buf_, msg);\n'
' evtag_marshal_buffer(evbuf, tag, buf_);\n '
' evbuffer_free(buf_);\n'
'}\n\n' ) % { 'name' : self._name })
def __init__(self, type, name, tag):
def GetInitializer(self):
assert 0, "Entry does not provide initializer"
def SetStruct(self, struct):
assert self._line_count != -1
def SetLineCount(self, number):
self._line_count = number
def MakeArray(self, yes=1):
if self.Array() and not self._can_be_array:
'Entry "%s" cannot be created as an array '
'around line %d' % (self._name, self.LineCount()))
'Entry "%s" does not know which struct it belongs to '
'around line %d' % (self._name, self.LineCount()))
if self._optional and self._array:
'Entry "%s" has illegal combination of optional and array '
'around line %d' % (self._name, self.LineCount()))
def GetTranslation(self, extradict = {}):
"parent_name" : self._struct.Name(),
"refname" : self._refname,
"optpointer" : self._optpointer and "*" or "",
"optreference" : self._optpointer and "&" or "",
self._optaddarg and ", const %s value" % self._ctype or ""
for (k, v) in list(extradict.items()):
def GetVarName(self, var):
return '%(var)s->%(name)s_data' % self.GetTranslation({ 'var' : var })
def GetVarLen(self, var):
return 'sizeof(%s)' % self._ctype
return '%s_%s_get' % (self._struct.Name(), self._name)
def GetDeclaration(self, funcname):
code = [ 'int %s(struct %s *, %s *);' % (
funcname, self._struct.Name(), self._ctype ) ]
'%(parent_name)s_%(name)s_get(struct %(parent_name)s *msg, '
' if (msg->%(name)s_set != 1)',
' *value = msg->%(name)s_data;',
code = code % self.GetTranslation()
def AssignFuncName(self):
return '%s_%s_assign' % (self._struct.Name(), self._name)
return '%s_%s_add' % (self._struct.Name(), self._name)
def AssignDeclaration(self, funcname):
code = [ 'int %s(struct %s *, const %s);' % (
funcname, self._struct.Name(), self._ctype ) ]
'%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg,'
' const %(ctype)s value)',
' msg->%(name)s_set = 1;',
' msg->%(name)s_data = value;',
code = code % self.GetTranslation()
def CodeClear(self, structname):
code = [ '%s->%s_set = 0;' % (structname, self.Name()) ]
def CodeComplete(self, structname, var_name):
def CodeFree(self, name):
'%(parent_name)s_%(name)s_assign,',
'%(parent_name)s_%(name)s_get,'
code.append('%(parent_name)s_%(name)s_add,')
code = code % self.GetTranslation()