1e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley#!/usr/bin/env python2 2e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# 3e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# Copyright (c) 2005-2007 Niels Provos <provos@citi.umich.edu> 4e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 5e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# All rights reserved. 6e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# 7e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# Generates marshaling code based on libevent. 8e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 9e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# TODO: 10e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# 1) use optparse to allow the strategy shell to parse options, and 11e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# to allow the instantiated factory (for the specific output language) 12e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# to parse remaining options 13e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# 2) move the globals into a class that manages execution (including the 14e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# progress outputs that space stderr at the moment) 15e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# 3) emit other languages 16e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 17e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyimport sys 18e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyimport re 19e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 20e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley_NAME = "event_rpcgen.py" 21e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley_VERSION = "0.1" 22e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 23e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# Globals 24e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyline_count = 0 25e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 26e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileywhite = re.compile(r'\s+') 27e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileycppcomment = re.compile(r'\/\/.*$') 28e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileynonident = re.compile(r'[^a-zA-Z0-9_]') 29e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileystructref = re.compile(r'^struct\[([a-zA-Z_][a-zA-Z0-9_]*)\]$') 30e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileystructdef = re.compile(r'^struct +[a-zA-Z_][a-zA-Z0-9_]* *{$') 31e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 32e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyheaderdirect = [] 33e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileycppdirect = [] 34e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 35e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileydef TranslateList(mylist, mydict): 36e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return map(lambda x: x % mydict, mylist) 37e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 38e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# Exception class for parse errors 39e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass RpcGenError(Exception): 40e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, why): 41e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.why = why 42e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __str__(self): 43e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return str(self.why) 44e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 45e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley# Holds everything that makes a struct 46e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass Struct: 47e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, name): 48e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._name = name 49e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entries = [] 50e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._tags = {} 51e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, ' Created struct: %s' % name 52e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 53e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AddEntry(self, entry): 54e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if self._tags.has_key(entry.Tag()): 55e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError( 56e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'Entry "%s" duplicates tag number %d from "%s" ' 57e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'around line %d' % (entry.Name(), entry.Tag(), 58e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._tags[entry.Tag()], line_count)) 59e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entries.append(entry) 60e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._tags[entry.Tag()] = entry.Name() 61e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, ' Added entry: %s' % entry.Name() 62e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 63e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Name(self): 64e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._name 65e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 66e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryTagName(self, entry): 67e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """Creates the name inside an enumeration for distinguishing data 68e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley types.""" 69e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = "%s_%s" % (self._name, entry.Name()) 70e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return name.upper() 71e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 72e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def PrintIndented(self, file, ident, code): 73e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """Takes an array, add indentation to each entry and prints it.""" 74e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in code: 75e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '%s%s' % (ident, entry) 76e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 77e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass StructCCode(Struct): 78e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """ Knows how to generate C code for a struct """ 79e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 80e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, name): 81e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Struct.__init__(self, name) 82e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 83e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def PrintTags(self, file): 84e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """Prints the tag definitions for a structure.""" 85e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '/* Tag definition for %s */' % self._name 86e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, 'enum %s_ {' % self._name.lower() 87e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 88e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' %s=%d,' % (self.EntryTagName(entry), 89e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.Tag()) 90e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' %s_MAX_TAGS' % (self._name.upper()) 91e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '};\n' 92e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 93e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def PrintForwardDeclaration(self, file): 94e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, 'struct %s;' % self._name 95e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 96e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def PrintDeclaration(self, file): 97e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '/* Structure declaration for %s */' % self._name 98e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, 'struct %s_access_ {' % self._name 99e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 100e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = entry.AssignDeclaration('(*%s_assign)' % entry.Name()) 101e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl.extend( 102e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.GetDeclaration('(*%s_get)' % entry.Name())) 103e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if entry.Array(): 104e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl.extend( 105e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.AddDeclaration('(*%s_add)' % entry.Name())) 106e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, ' ', dcl) 107e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '};\n' 108e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 109e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, 'struct %s {' % self._name 110e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' struct %s_access_ *base;\n' % self._name 111e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 112e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = entry.Declaration() 113e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, ' ', dcl) 114e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '' 115e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 116e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' ev_uint8_t %s_set;' % entry.Name() 117e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '};\n' 118e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 119e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, \ 120e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley"""struct %(name)s *%(name)s_new(void); 121e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileystruct %(name)s *%(name)s_new_with_arg(void *); 122e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyvoid %(name)s_free(struct %(name)s *); 123e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyvoid %(name)s_clear(struct %(name)s *); 124e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyvoid %(name)s_marshal(struct evbuffer *, const struct %(name)s *); 125e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyint %(name)s_unmarshal(struct %(name)s *, struct evbuffer *); 126e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyint %(name)s_complete(struct %(name)s *); 127e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyvoid evtag_marshal_%(name)s(struct evbuffer *, ev_uint32_t, 128e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley const struct %(name)s *); 129e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyint evtag_unmarshal_%(name)s(struct evbuffer *, ev_uint32_t, 130e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley struct %(name)s *);""" % { 'name' : self._name } 131e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 132e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 133e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Write a setting function of every variable 134e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 135e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, '', entry.AssignDeclaration( 136e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.AssignFuncName())) 137e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, '', entry.GetDeclaration( 138e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.GetFuncName())) 139e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if entry.Array(): 140e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, '', entry.AddDeclaration( 141e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.AddFuncName())) 142e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 143e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '/* --- %s done --- */\n' % self._name 144e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 145e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def PrintCode(self, file): 146e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ('/*\n' 147e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' * Implementation of %s\n' 148e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' */\n') % self._name 149e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 150e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, \ 151e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'static struct %(name)s_access_ __%(name)s_base = {' % \ 152e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley { 'name' : self._name } 153e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 154e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, ' ', entry.CodeBase()) 155e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '};\n' 156e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 157e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Creation 158e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 159e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'struct %(name)s *\n' 160e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_new(void)\n' 161e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{\n' 162e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return %(name)s_new_with_arg(NULL);\n' 163e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n' 164e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '\n' 165e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'struct %(name)s *\n' 166e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_new_with_arg(void *unused)\n' 167e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{\n' 168e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' struct %(name)s *tmp;\n' 169e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if ((tmp = malloc(sizeof(struct %(name)s))) == NULL) {\n' 170e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warn("%%s: malloc", __func__);\n' 171e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (NULL);\n' 172e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }\n' 173e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' tmp->base = &__%(name)s_base;\n') % { 'name' : self._name } 174e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 175e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 176e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, ' ', entry.CodeInitialize('tmp')) 177e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' tmp->%s_set = 0;\n' % entry.Name() 178e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 179e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 180e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (tmp);\n' 181e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n') 182e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 183e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Adding 184e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 185e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if entry.Array(): 186e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, '', entry.CodeAdd()) 187e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '' 188e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 189e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Assigning 190e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 191e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, '', entry.CodeAssign()) 192e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '' 193e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 194e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Getting 195e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 196e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, '', entry.CodeGet()) 197e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '' 198e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 199e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Clearing 200e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 'void\n' 201e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_clear(struct %(name)s *tmp)\n' 202e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{' 203e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) % { 'name' : self._name } 204e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 205e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, ' ', entry.CodeClear('tmp')) 206e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 207e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '}\n' 208e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 209e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Freeing 210e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 'void\n' 211e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_free(struct %(name)s *tmp)\n' 212e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{' 213e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) % { 'name' : self._name } 214e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 215e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 216e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented(file, ' ', entry.CodeFree('tmp')) 217e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 218e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, (' free(tmp);\n' 219e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n') 220e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 221e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Marshaling 222e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ('void\n' 223e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_marshal(struct evbuffer *evbuf, ' 224e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'const struct %(name)s *tmp)' 225e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{') % { 'name' : self._name } 226e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 227e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley indent = ' ' 228e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Optional entries do not have to be set 229e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if entry.Optional(): 230e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley indent += ' ' 231e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' if (tmp->%s_set) {' % entry.Name() 232e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented( 233e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley file, indent, 234e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.CodeMarshal('evbuf', self.EntryTagName(entry), 235e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.GetVarName('tmp'), 236e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.GetVarLen('tmp'))) 237e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if entry.Optional(): 238e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' }' 239e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 240e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, '}\n' 241e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 242e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Unmarshaling 243e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ('int\n' 244e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_unmarshal(struct %(name)s *tmp, ' 245e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' struct evbuffer *evbuf)\n' 246e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{\n' 247e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' ev_uint32_t tag;\n' 248e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' while (evbuffer_get_length(evbuf) > 0) {\n' 249e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (evtag_peek(evbuf, &tag) == -1)\n' 250e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);\n' 251e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' switch (tag) {\n' 252e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) % { 'name' : self._name } 253e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 254e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ' case %s:\n' % self.EntryTagName(entry) 255e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not entry.Array(): 256e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 257e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (tmp->%s_set)\n' 258e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);' 259e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) % (entry.Name()) 260e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 261e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented( 262e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley file, ' ', 263e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.CodeUnmarshal('evbuf', 264e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.EntryTagName(entry), 265e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.GetVarName('tmp'), 266e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.GetVarLen('tmp'))) 267e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 268e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( ' tmp->%s_set = 1;\n' % entry.Name() + 269e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' break;\n' ) 270e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( ' default:\n' 271e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return -1;\n' 272e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }\n' 273e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }\n' ) 274e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Check if it was decoded completely 275e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( ' if (%(name)s_complete(tmp) == -1)\n' 276e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);' 277e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) % { 'name' : self._name } 278e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 279e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Successfully decoded 280e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( ' return (0);\n' 281e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n') 282e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 283e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Checking if a structure has all the required data 284e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 285e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int\n' 286e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(name)s_complete(struct %(name)s *msg)\n' 287e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{' ) % { 'name' : self._name } 288e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in self._entries: 289e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not entry.Optional(): 290e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 291e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (!msg->%(name)s_set)', 292e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);' ] 293e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = TranslateList(code, entry.GetTranslation()) 294e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented( 295e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley file, ' ', code) 296e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 297e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.PrintIndented( 298e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley file, ' ', 299e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.CodeComplete('msg', entry.GetVarName('msg'))) 300e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 301e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);\n' 302e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n' ) 303e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 304e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Complete message unmarshaling 305e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 306e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int\n' 307e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'evtag_unmarshal_%(name)s(struct evbuffer *evbuf, ' 308e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'ev_uint32_t need_tag, struct %(name)s *msg)\n' 309e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{\n' 310e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' ev_uint32_t tag;\n' 311e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' int res = -1;\n' 312e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '\n' 313e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' struct evbuffer *tmp = evbuffer_new();\n' 314e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '\n' 315e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (evtag_unmarshal(evbuf, &tag, tmp) == -1' 316e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' || tag != need_tag)\n' 317e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto error;\n' 318e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '\n' 319e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (%(name)s_unmarshal(msg, tmp) == -1)\n' 320e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto error;\n' 321e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '\n' 322e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' res = 0;\n' 323e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '\n' 324e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' error:\n' 325e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' evbuffer_free(tmp);\n' 326e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (res);\n' 327e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n' ) % { 'name' : self._name } 328e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 329e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Complete message marshaling 330e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>file, ( 331e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'void\n' 332e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'evtag_marshal_%(name)s(struct evbuffer *evbuf, ev_uint32_t tag, ' 333e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'const struct %(name)s *msg)\n' 334e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{\n' 335e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' struct evbuffer *_buf = evbuffer_new();\n' 336e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' assert(_buf != NULL);\n' 337e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(name)s_marshal(_buf, msg);\n' 338e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' evtag_marshal_buffer(evbuf, tag, _buf);\n ' 339e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' evbuffer_free(_buf);\n' 340e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}\n' ) % { 'name' : self._name } 341e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 342e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass Entry: 343e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, type, name, tag): 344e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._type = type 345e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._name = name 346e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._tag = int(tag) 347e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = type 348e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optional = 0 349e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._can_be_array = 0 350e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._array = 0 351e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._line_count = -1 352e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct = None 353e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._refname = None 354e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 355e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optpointer = True 356e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optaddarg = True 357e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 358e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 359e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley assert 0, "Entry does not provide initializer" 360e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 361e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def SetStruct(self, struct): 362e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct = struct 363e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 364e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def LineCount(self): 365e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley assert self._line_count != -1 366e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._line_count 367e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 368e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def SetLineCount(self, number): 369e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._line_count = number 370e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 371e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Array(self): 372e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._array 373e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 374e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Optional(self): 375e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._optional 376e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 377e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Tag(self): 378e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._tag 379e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 380e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Name(self): 381e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._name 382e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 383e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Type(self): 384e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return self._type 385e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 386e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def MakeArray(self, yes=1): 387e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._array = yes 388e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 389e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def MakeOptional(self): 390e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optional = 1 391e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 392e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Verify(self): 393e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if self.Array() and not self._can_be_array: 394e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError( 395e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'Entry "%s" cannot be created as an array ' 396e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'around line %d' % (self._name, self.LineCount())) 397e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self._struct: 398e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError( 399e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'Entry "%s" does not know which struct it belongs to ' 400e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'around line %d' % (self._name, self.LineCount())) 401e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if self._optional and self._array: 402e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError( 403e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'Entry "%s" has illegal combination of optional and array ' 404e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'around line %d' % (self._name, self.LineCount())) 405e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 406e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetTranslation(self, extradict = {}): 407e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley mapping = { 408e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "parent_name" : self._struct.Name(), 409e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "name" : self._name, 410e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "ctype" : self._ctype, 411e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "refname" : self._refname, 412e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "optpointer" : self._optpointer and "*" or "", 413e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "optreference" : self._optpointer and "&" or "", 414e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley "optaddarg" : 415e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optaddarg and ", const %s value" % self._ctype or "" 416e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } 417e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for (k, v) in extradict.items(): 418e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley mapping[k] = v 419e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 420e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return mapping 421e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 422e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarName(self, var): 423e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%(var)s->%(name)s_data' % self.GetTranslation({ 'var' : var }) 424e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 425e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarLen(self, var): 426e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return 'sizeof(%s)' % self._ctype 427e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 428e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetFuncName(self): 429e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%s_%s_get' % (self._struct.Name(), self._name) 430e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 431e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetDeclaration(self, funcname): 432e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, %s *);' % ( 433e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 434e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 435e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 436e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeGet(self): 437e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ( 438e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int', 439e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_get(struct %(parent_name)s *msg, ' 440e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(ctype)s *value)', 441e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 442e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%(name)s_set != 1)', 443e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 444e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' *value = msg->%(name)s_data;', 445e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 446e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ) 447e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) 448e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = code % self.GetTranslation() 449e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 450e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 451e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AssignFuncName(self): 452e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%s_%s_assign' % (self._struct.Name(), self._name) 453e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 454e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AddFuncName(self): 455e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%s_%s_add' % (self._struct.Name(), self._name) 456e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 457e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AssignDeclaration(self, funcname): 458e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, const %s);' % ( 459e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 460e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 461e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 462e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAssign(self): 463e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int', 464e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg,' 465e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' const %(ctype)s value)', 466e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 467e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%(name)s_set = 1;', 468e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%(name)s_data = value;', 469e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 470e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 471e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) 472e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = code % self.GetTranslation() 473e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 474e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 475e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeClear(self, structname): 476e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ '%s->%s_set = 0;' % (structname, self.Name()) ] 477e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 478e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 479e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 480e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeComplete(self, structname, var_name): 481e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [] 482e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 483e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeFree(self, name): 484e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [] 485e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 486e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeBase(self): 487e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 488e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_assign,', 489e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_get,' 490e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 491e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if self.Array(): 492e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code.append('%(parent_name)s_%(name)s_add,') 493e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 494e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) 495e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = code % self.GetTranslation() 496e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 497e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 498e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass EntryBytes(Entry): 499e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, type, name, tag, length): 500e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Init base class 501e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.__init__(self, type, name, tag) 502e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 503e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._length = length 504e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = 'ev_uint8_t' 505e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 506e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 507e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return "NULL" 508e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 509e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarLen(self, var): 510e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '(%s)' % self._length 511e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 512e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAdd(self, varname, value): 513e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # XXX: copy here 514e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [ '%(varname)s = NULL;' % { 'varname' : varname } ] 515e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 516e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetDeclaration(self, funcname): 517e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, %s **);' % ( 518e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 519e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 520e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 521e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AssignDeclaration(self, funcname): 522e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, const %s *);' % ( 523e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 524e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 525e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 526e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Declaration(self): 527e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = ['ev_uint8_t %s_data[%s];' % (self._name, self._length)] 528e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 529e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return dcl 530e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 531e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeGet(self): 532e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 533e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int', 534e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s_%s_get(struct %s *msg, %s **value)' % ( 535e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), name, 536e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), self._ctype), 537e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 538e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%s_set != 1)' % name, 539e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 540e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' *value = msg->%s_data;' % name, 541e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 542e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 543e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 544e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 545e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAssign(self): 546e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 547e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int', 548e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s_%s_assign(struct %s *msg, const %s *value)' % ( 549e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), name, 550e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), self._ctype), 551e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 552e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%s_set = 1;' % name, 553e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' memcpy(msg->%s_data, value, %s);' % ( 554e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name, self._length), 555e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 556e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 557e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 558e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 559e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeUnmarshal(self, buf, tag_name, var_name, var_len): 560e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'if (evtag_unmarshal_fixed(%(buf)s, %(tag)s, ' 561e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(var)s, %(varlen)s) == -1) {', 562e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: failed to unmarshal %(name)s", __func__);', 563e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 564e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 565e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 566e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, 567e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.GetTranslation({ 568e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var_name, 569e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'varlen' : var_len, 570e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'buf' : buf, 571e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'tag' : tag_name })) 572e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 573e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMarshal(self, buf, tag_name, var_name, var_len): 574e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['evtag_marshal(%s, %s, %s, %s);' % ( 575e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley buf, tag_name, var_name, var_len)] 576e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 577e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 578e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeClear(self, structname): 579e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ '%s->%s_set = 0;' % (structname, self.Name()), 580e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'memset(%s->%s_data, 0, sizeof(%s->%s_data));' % ( 581e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley structname, self._name, structname, self._name)] 582e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 583e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 584e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 585e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeInitialize(self, name): 586e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['memset(%s->%s_data, 0, sizeof(%s->%s_data));' % ( 587e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name, self._name, name, self._name)] 588e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 589e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 590e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Verify(self): 591e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self._length: 592e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError( 593e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'Entry "%s" needs a length ' 594e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'around line %d' % (self._name, self.LineCount())) 595e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 596e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.Verify(self) 597e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 598e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass EntryInt(Entry): 599e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, type, name, tag, bits=32): 600e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Init base class 601e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.__init__(self, type, name, tag) 602e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 603e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._can_be_array = 1 604e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if bits == 32: 605e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = 'ev_uint32_t' 606e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._marshal_type = 'int' 607e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if bits == 64: 608e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = 'ev_uint64_t' 609e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._marshal_type = 'int64' 610e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 611e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 612e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return "0" 613e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 614e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayFree(self, var): 615e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [] 616e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 617e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAssign(self, varname, srcvar): 618e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [ '%(varname)s = %(srcvar)s;' % { 'varname' : varname, 619e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'srcvar' : srcvar } ] 620e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 621e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAdd(self, varname, value): 622e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """Returns a new entry of this type.""" 623e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [ '%(varname)s = %(value)s;' % { 'varname' : varname, 624e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'value' : value } ] 625e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 626e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeUnmarshal(self, buf, tag_name, var_name, var_len): 627e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 628e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (evtag_unmarshal_%(ma)s(%(buf)s, %(tag)s, &%(var)s) == -1) {', 629e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: failed to unmarshal %(name)s", __func__);', 630e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 631e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 632e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) % self.GetTranslation({ 633e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'ma' : self._marshal_type, 634e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'buf' : buf, 635e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'tag' : tag_name, 636e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var_name }) 637e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 638e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 639e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMarshal(self, buf, tag_name, var_name, var_len): 640e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 641e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'evtag_marshal_%s(%s, %s, %s);' % ( 642e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._marshal_type, buf, tag_name, var_name)] 643e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 644e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 645e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Declaration(self): 646e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = ['%s %s_data;' % (self._ctype, self._name)] 647e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 648e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return dcl 649e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 650e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeInitialize(self, name): 651e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['%s->%s_data = 0;' % (name, self._name)] 652e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 653e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 654e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass EntryString(Entry): 655e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, type, name, tag): 656e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Init base class 657e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.__init__(self, type, name, tag) 658e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 659e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._can_be_array = 1 660e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = 'char *' 661e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 662e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 663e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return "NULL" 664e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 665e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayFree(self, varname): 666e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 667e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(var)s != NULL) free(%(var)s);' ] 668e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 669e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, { 'var' : varname }) 670e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 671e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAssign(self, varname, srcvar): 672e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 673e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(var)s != NULL)', 674e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free(%(var)s);', 675e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(var)s = strdup(%(srcvar)s);', 676e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(var)s == NULL) {', 677e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: strdup", __func__);', 678e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 679e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 680e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 681e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, { 'var' : varname, 682e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'srcvar' : srcvar }) 683e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 684e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAdd(self, varname, value): 685e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 686e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(value)s != NULL) {', 687e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(var)s = strdup(%(value)s);', 688e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (%(var)s == NULL) {', 689e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto error;', 690e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }', 691e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '} else {', 692e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(var)s = NULL;', 693e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 694e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 695e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, { 'var' : varname, 696e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'value' : value }) 697e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 698e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarLen(self, var): 699e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return 'strlen(%s)' % self.GetVarName(var) 700e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 701e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMakeInitalize(self, varname): 702e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%(varname)s = NULL;' % { 'varname' : varname } 703e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 704e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAssign(self): 705e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 706e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = """int 707e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg, 708e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley const %(ctype)s value) 709e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley{ 710e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (msg->%(name)s_data != NULL) 711e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley free(msg->%(name)s_data); 712e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if ((msg->%(name)s_data = strdup(value)) == NULL) 713e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return (-1); 714e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley msg->%(name)s_set = 1; 715e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return (0); 716e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley}""" % self.GetTranslation() 717e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 718e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 719e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 720e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeUnmarshal(self, buf, tag_name, var_name, var_len): 721e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['if (evtag_unmarshal_string(%(buf)s, %(tag)s, &%(var)s) == -1) {', 722e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: failed to unmarshal %(name)s", __func__);', 723e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 724e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 725e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 726e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) % self.GetTranslation({ 727e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'buf' : buf, 728e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'tag' : tag_name, 729e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var_name }) 730e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 731e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 732e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMarshal(self, buf, tag_name, var_name, var_len): 733e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['evtag_marshal_string(%s, %s, %s);' % ( 734e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley buf, tag_name, var_name)] 735e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 736e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 737e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeClear(self, structname): 738e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'if (%s->%s_set == 1) {' % (structname, self.Name()), 739e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free(%s->%s_data);' % (structname, self.Name()), 740e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_data = NULL;' % (structname, self.Name()), 741e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_set = 0;' % (structname, self.Name()), 742e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 743e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 744e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 745e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 746e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 747e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeInitialize(self, name): 748e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['%s->%s_data = NULL;' % (name, self._name)] 749e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 750e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 751e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeFree(self, name): 752e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['if (%s->%s_data != NULL)' % (name, self._name), 753e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free (%s->%s_data);' % (name, self._name)] 754e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 755e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 756e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 757e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Declaration(self): 758e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = ['char *%s_data;' % self._name] 759e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 760e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return dcl 761e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 762e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass EntryStruct(Entry): 763e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, type, name, tag, refname): 764e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Init base class 765e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.__init__(self, type, name, tag) 766e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 767e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optpointer = False 768e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._can_be_array = 1 769e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._refname = refname 770e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = 'struct %s*' % refname 771e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optaddarg = False 772e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 773e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 774e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return "NULL" 775e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 776e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarLen(self, var): 777e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '-1' 778e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 779e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAdd(self, varname, value): 780e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 781e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(varname)s = %(refname)s_new();', 782e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(varname)s == NULL)', 783e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto error;' ] 784e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 785e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, self.GetTranslation({ 'varname' : varname })) 786e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 787e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayFree(self, var): 788e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ '%(refname)s_free(%(var)s);' % self.GetTranslation( 789e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley { 'var' : var }) ] 790e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 791e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 792e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAssign(self, var, srcvar): 793e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 794e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int had_error = 0;', 795e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'struct evbuffer *tmp = NULL;', 796e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(refname)s_clear(%(var)s);', 797e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if ((tmp = evbuffer_new()) == NULL) {', 798e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warn("%%s: evbuffer_new()", __func__);', 799e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' had_error = 1;', 800e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto done;', 801e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}', 802e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(refname)s_marshal(tmp, %(srcvar)s);', 803e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(refname)s_unmarshal(%(var)s, tmp) == -1) {', 804e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: %(refname)s_unmarshal", __func__);', 805e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' had_error = 1;', 806e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto done;', 807e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}', 808e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'done:' 809e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (tmp != NULL)', 810e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' evbuffer_free(tmp);', 811e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (had_error) {', 812e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(refname)s_clear(%(var)s);', 813e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 814e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 815e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 816e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, self.GetTranslation({ 817e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var, 818e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'srcvar' : srcvar})) 819e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 820e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeGet(self): 821e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 822e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int', 823e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s_%s_get(struct %s *msg, %s *value)' % ( 824e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), name, 825e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), self._ctype), 826e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 827e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%s_set != 1) {' % name, 828e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%s_data = %s_new();' % (name, self._refname), 829e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%s_data == NULL)' % name, 830e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 831e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%s_set = 1;' % name, 832e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }', 833e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' *value = msg->%s_data;' % name, 834e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 835e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 836e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 837e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 838e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAssign(self): 839e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 840e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = """int 841e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg, 842e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley const %(ctype)s value) 843e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley{ 844e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley struct evbuffer *tmp = NULL; 845e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (msg->%(name)s_set) { 846e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley %(refname)s_clear(msg->%(name)s_data); 847e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley msg->%(name)s_set = 0; 848e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } else { 849e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley msg->%(name)s_data = %(refname)s_new(); 850e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (msg->%(name)s_data == NULL) { 851e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley event_warn("%%s: %(refname)s_new()", __func__); 852e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley goto error; 853e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } 854e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } 855e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if ((tmp = evbuffer_new()) == NULL) { 856e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley event_warn("%%s: evbuffer_new()", __func__); 857e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley goto error; 858e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } 859e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley %(refname)s_marshal(tmp, value); 860e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (%(refname)s_unmarshal(msg->%(name)s_data, tmp) == -1) { 861e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley event_warnx("%%s: %(refname)s_unmarshal", __func__); 862e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley goto error; 863e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } 864e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley msg->%(name)s_set = 1; 865e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley evbuffer_free(tmp); 866e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return (0); 867e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley error: 868e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (tmp != NULL) 869e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley evbuffer_free(tmp); 870e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (msg->%(name)s_data != NULL) { 871e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley %(refname)s_free(msg->%(name)s_data); 872e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley msg->%(name)s_data = NULL; 873e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley } 874e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return (-1); 875e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley}""" % self.GetTranslation() 876e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 877e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 878e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeComplete(self, structname, var_name): 879e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'if (%(structname)s->%(name)s_set && ' 880e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(refname)s_complete(%(var)s) == -1)', 881e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);' ] 882e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 883e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return TranslateList(code, self.GetTranslation({ 884e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'structname' : structname, 885e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var_name })) 886e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 887e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeUnmarshal(self, buf, tag_name, var_name, var_len): 888e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['%(var)s = %(refname)s_new();', 889e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(var)s == NULL)', 890e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 891e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (evtag_unmarshal_%(refname)s(%(buf)s, %(tag)s, ' 892e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(var)s) == -1) {', 893e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: failed to unmarshal %(name)s", __func__);', 894e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 895e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 896e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 897e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) % self.GetTranslation({ 898e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'buf' : buf, 899e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'tag' : tag_name, 900e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var_name }) 901e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 902e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 903e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMarshal(self, buf, tag_name, var_name, var_len): 904e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['evtag_marshal_%s(%s, %s, %s);' % ( 905e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._refname, buf, tag_name, var_name)] 906e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 907e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 908e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeClear(self, structname): 909e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'if (%s->%s_set == 1) {' % (structname, self.Name()), 910e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s_free(%s->%s_data);' % ( 911e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._refname, structname, self.Name()), 912e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_data = NULL;' % (structname, self.Name()), 913e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_set = 0;' % (structname, self.Name()), 914e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 915e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 916e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 917e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 918e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 919e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeInitialize(self, name): 920e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['%s->%s_data = NULL;' % (name, self._name)] 921e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 922e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 923e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeFree(self, name): 924e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['if (%s->%s_data != NULL)' % (name, self._name), 925e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s_free(%s->%s_data);' % ( 926e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._refname, name, self._name)] 927e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 928e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 929e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 930e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Declaration(self): 931e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = ['%s %s_data;' % (self._ctype, self._name)] 932e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 933e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return dcl 934e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 935e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass EntryVarBytes(Entry): 936e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, type, name, tag): 937e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Init base class 938e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.__init__(self, type, name, tag) 939e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 940e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = 'ev_uint8_t *' 941e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 942e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 943e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return "NULL" 944e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 945e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarLen(self, var): 946e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%(var)s->%(name)s_length' % self.GetTranslation({ 'var' : var }) 947e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 948e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeArrayAdd(self, varname, value): 949e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # xxx: copy 950e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [ '%(varname)s = NULL;' % { 'varname' : varname } ] 951e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 952e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetDeclaration(self, funcname): 953e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, %s *, ev_uint32_t *);' % ( 954e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 955e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 956e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 957e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AssignDeclaration(self, funcname): 958e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, const %s, ev_uint32_t);' % ( 959e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 960e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 961e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 962e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAssign(self): 963e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 964e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int', 965e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s_%s_assign(struct %s *msg, ' 966e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'const %s value, ev_uint32_t len)' % ( 967e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), name, 968e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), self._ctype), 969e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 970e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%s_data != NULL)' % name, 971e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free (msg->%s_data);' % name, 972e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%s_data = malloc(len);' % name, 973e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%s_data == NULL)' % name, 974e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 975e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%s_set = 1;' % name, 976e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%s_length = len;' % name, 977e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' memcpy(msg->%s_data, value, len);' % name, 978e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 979e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 980e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 981e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 982e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeGet(self): 983e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = self._name 984e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int', 985e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s_%s_get(struct %s *msg, %s *value, ev_uint32_t *plen)' % ( 986e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), name, 987e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._struct.Name(), self._ctype), 988e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 989e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (msg->%s_set != 1)' % name, 990e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 991e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' *value = msg->%s_data;' % name, 992e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' *plen = msg->%s_length;' % name, 993e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 994e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 995e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 996e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 997e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeUnmarshal(self, buf, tag_name, var_name, var_len): 998e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['if (evtag_payload_length(%(buf)s, &%(varlen)s) == -1)', 999e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 1000e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # We do not want DoS opportunities 1001e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(varlen)s > evbuffer_get_length(%(buf)s))', 1002e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 1003e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if ((%(var)s = malloc(%(varlen)s)) == NULL)', 1004e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 1005e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (evtag_unmarshal_fixed(%(buf)s, %(tag)s, %(var)s, ' 1006e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(varlen)s) == -1) {', 1007e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' event_warnx("%%s: failed to unmarshal %(name)s", __func__);', 1008e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 1009e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 1010e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 1011e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = '\n'.join(code) % self.GetTranslation({ 1012e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'buf' : buf, 1013e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'tag' : tag_name, 1014e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'var' : var_name, 1015e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'varlen' : var_len }) 1016e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 1017e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1018e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMarshal(self, buf, tag_name, var_name, var_len): 1019e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['evtag_marshal(%s, %s, %s, %s);' % ( 1020e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley buf, tag_name, var_name, var_len)] 1021e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1022e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1023e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeClear(self, structname): 1024e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'if (%s->%s_set == 1) {' % (structname, self.Name()), 1025e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free (%s->%s_data);' % (structname, self.Name()), 1026e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_data = NULL;' % (structname, self.Name()), 1027e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_length = 0;' % (structname, self.Name()), 1028e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %s->%s_set = 0;' % (structname, self.Name()), 1029e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 1030e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 1031e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1032e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1033e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1034e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeInitialize(self, name): 1035e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['%s->%s_data = NULL;' % (name, self._name), 1036e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s->%s_length = 0;' % (name, self._name) ] 1037e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1038e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1039e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeFree(self, name): 1040e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['if (%s->%s_data != NULL)' % (name, self._name), 1041e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free(%s->%s_data);' % (name, self._name)] 1042e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1043e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1044e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1045e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Declaration(self): 1046e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = ['ev_uint8_t *%s_data;' % self._name, 1047e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'ev_uint32_t %s_length;' % self._name] 1048e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1049e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return dcl 1050e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1051e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass EntryArray(Entry): 1052e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, entry): 1053e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Init base class 1054e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Entry.__init__(self, entry._type, entry._name, entry._tag) 1055e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1056e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry = entry 1057e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._refname = entry._refname 1058e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._ctype = self._entry._ctype 1059e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optional = True 1060e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optpointer = self._entry._optpointer 1061e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._optaddarg = self._entry._optaddarg 1062e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1063e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # provide a new function for accessing the variable name 1064e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarName(var_name): 1065e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '%(var)s->%(name)s_data[%(index)s]' % \ 1066e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry.GetTranslation({'var' : var_name, 1067e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'index' : self._index}) 1068e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry.GetVarName = GetVarName 1069e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1070e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetInitializer(self): 1071e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return "NULL" 1072e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1073e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarName(self, var_name): 1074e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return var_name 1075e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1076e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetVarLen(self, var_name): 1077e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '-1' 1078e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1079e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GetDeclaration(self, funcname): 1080e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """Allows direct access to elements of the array.""" 1081e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 1082e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int %(funcname)s(struct %(parent_name)s *, int, %(ctype)s *);' % 1083e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.GetTranslation({ 'funcname' : funcname }) ] 1084e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1085e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1086e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AssignDeclaration(self, funcname): 1087e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'int %s(struct %s *, int, const %s);' % ( 1088e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley funcname, self._struct.Name(), self._ctype ) ] 1089e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1090e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1091e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def AddDeclaration(self, funcname): 1092e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 1093e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(ctype)s %(optpointer)s ' 1094e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(funcname)s(struct %(parent_name)s *msg%(optaddarg)s);' % \ 1095e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.GetTranslation({ 'funcname' : funcname }) ] 1096e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1097e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1098e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeGet(self): 1099e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = """int 1100e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley%(parent_name)s_%(name)s_get(struct %(parent_name)s *msg, int offset, 1101e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley %(ctype)s *value) 1102e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley{ 1103e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if (!msg->%(name)s_set || offset < 0 || offset >= msg->%(name)s_length) 1104e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return (-1); 1105e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley *value = msg->%(name)s_data[offset]; 1106e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return (0); 1107e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley}""" % self.GetTranslation() 1108e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1109e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 1110e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1111e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAssign(self): 1112e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 1113e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int', 1114e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg, int off,', 1115e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' const %(ctype)s value)', 1116e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 1117e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (!msg->%(name)s_set || off < 0 || off >= msg->%(name)s_length)', 1118e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);\n', 1119e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' {' ] 1120e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = TranslateList(code, self.GetTranslation()) 1121e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1122e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley codearrayassign = self._entry.CodeArrayAssign( 1123e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'msg->%(name)s_data[off]' % self.GetTranslation(), 'value') 1124e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += map(lambda x: ' ' + x, codearrayassign) 1125e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1126e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += TranslateList([ 1127e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }', 1128e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (0);', 1129e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ], self.GetTranslation()) 1130e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1131e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1132e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1133e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeAdd(self): 1134e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley codearrayadd = self._entry.CodeArrayAdd( 1135e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'msg->%(name)s_data[msg->%(name)s_length - 1]' % self.GetTranslation(), 1136e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'value') 1137e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 1138e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'static int', 1139e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_expand_to_hold_more(' 1140e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'struct %(parent_name)s *msg)', 1141e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 1142e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' int tobe_allocated = msg->%(name)s_num_allocated;', 1143e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(ctype)s* new_data = NULL;', 1144e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;', 1145e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' new_data = (%(ctype)s*) realloc(msg->%(name)s_data,', 1146e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' tobe_allocated * sizeof(%(ctype)s));', 1147e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (new_data == NULL)', 1148e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return -1;', 1149e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%(name)s_data = new_data;', 1150e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%(name)s_num_allocated = tobe_allocated;', 1151e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return 0;' 1152e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}', 1153e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '', 1154e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(ctype)s %(optpointer)s', 1155e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(parent_name)s_%(name)s_add(' 1156e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'struct %(parent_name)s *msg%(optaddarg)s)', 1157e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 1158e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (++msg->%(name)s_length >= msg->%(name)s_num_allocated) {', 1159e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' if (%(parent_name)s_%(name)s_expand_to_hold_more(msg)<0)', 1160e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' goto error;', 1161e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }' ] 1162e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1163e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = TranslateList(code, self.GetTranslation()) 1164e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1165e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += map(lambda x: ' ' + x, codearrayadd) 1166e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1167e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += TranslateList([ 1168e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' msg->%(name)s_set = 1;', 1169e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return %(optreference)s(msg->%(name)s_data[' 1170e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'msg->%(name)s_length - 1]);', 1171e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'error:', 1172e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' --msg->%(name)s_length;', 1173e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (NULL);', 1174e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ], self.GetTranslation()) 1175e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1176e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1177e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1178e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeComplete(self, structname, var_name): 1179e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._index = 'i' 1180e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tmp = self._entry.CodeComplete(structname, self._entry.GetVarName(var_name)) 1181e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # skip the whole loop if there is nothing to check 1182e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not tmp: 1183e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return [] 1184e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1185e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley translate = self.GetTranslation({ 'structname' : structname }) 1186e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 1187e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '{', 1188e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' int i;', 1189e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' for (i = 0; i < %(structname)s->%(name)s_length; ++i) {' ] 1190e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1191e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = TranslateList(code, translate) 1192e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1193e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += map(lambda x: ' ' + x, tmp) 1194e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1195e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += [ 1196e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }', 1197e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' ] 1198e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1199e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1200e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1201e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeUnmarshal(self, buf, tag_name, var_name, var_len): 1202e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley translate = self.GetTranslation({ 'var' : var_name, 1203e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'buf' : buf, 1204e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'tag' : tag_name, 1205e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'init' : self._entry.GetInitializer()}) 1206e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 1207e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'if (%(var)s->%(name)s_length >= %(var)s->%(name)s_num_allocated &&', 1208e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(parent_name)s_%(name)s_expand_to_hold_more(%(var)s) < 0) {', 1209e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' puts("HEY NOW");', 1210e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' return (-1);', 1211e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}'] 1212e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1213e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # the unmarshal code directly returns 1214e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = TranslateList(code, translate) 1215e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1216e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._index = '%(var)s->%(name)s_length' % translate 1217e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += self._entry.CodeUnmarshal(buf, tag_name, 1218e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry.GetVarName(var_name), 1219e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry.GetVarLen(var_name)) 1220e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1221e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += [ '++%(var)s->%(name)s_length;' % translate ] 1222e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1223e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1224e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1225e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeMarshal(self, buf, tag_name, var_name, var_len): 1226e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['{', 1227e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' int i;', 1228e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' for (i = 0; i < %(var)s->%(name)s_length; ++i) {' ] 1229e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1230e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._index = 'i' 1231e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += self._entry.CodeMarshal(buf, tag_name, 1232e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry.GetVarName(var_name), 1233e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self._entry.GetVarLen(var_name)) 1234e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += [' }', 1235e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 1236e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ] 1237e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1238e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = "\n".join(code) % self.GetTranslation({ 'var' : var_name }) 1239e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1240e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code.split('\n') 1241e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1242e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeClear(self, structname): 1243e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley translate = self.GetTranslation({ 'structname' : structname }) 1244e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley codearrayfree = self._entry.CodeArrayFree( 1245e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%(structname)s->%(name)s_data[i]' % self.GetTranslation( 1246e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley { 'structname' : structname } )) 1247e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1248e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = [ 'if (%(structname)s->%(name)s_set == 1) {' ] 1249e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1250e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if codearrayfree: 1251e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += [ 1252e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' int i;', 1253e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' for (i = 0; i < %(structname)s->%(name)s_length; ++i) {' ] 1254e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1255e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = TranslateList(code, translate) 1256e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1257e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if codearrayfree: 1258e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += map(lambda x: ' ' + x, codearrayfree) 1259e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += [ 1260e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' }' ] 1261e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1262e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += TranslateList([ 1263e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' free(%(structname)s->%(name)s_data);', 1264e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(structname)s->%(name)s_data = NULL;', 1265e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(structname)s->%(name)s_set = 0;', 1266e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(structname)s->%(name)s_length = 0;', 1267e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' %(structname)s->%(name)s_num_allocated = 0;', 1268e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '}' 1269e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ], translate) 1270e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1271e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1272e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1273e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeInitialize(self, name): 1274e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = ['%s->%s_data = NULL;' % (name, self._name), 1275e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s->%s_length = 0;' % (name, self._name), 1276e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '%s->%s_num_allocated = 0;' % (name, self._name)] 1277e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1278e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1279e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeFree(self, structname): 1280e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code = self.CodeClear(structname); 1281e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1282e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley code += TranslateList([ 1283e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'free(%(structname)s->%(name)s_data);' ], 1284e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.GetTranslation({'structname' : structname })) 1285e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1286e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return code 1287e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1288e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Declaration(self): 1289e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley dcl = ['%s *%s_data;' % (self._ctype, self._name), 1290e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int %s_length;' % self._name, 1291e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'int %s_num_allocated;' % self._name ] 1292e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1293e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return dcl 1294e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1295e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileydef NormalizeLine(line): 1296e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley global white 1297e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley global cppcomment 1298e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1299e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = cppcomment.sub('', line) 1300e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = line.strip() 1301e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = white.sub(' ', line) 1302e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1303e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return line 1304e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1305e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileydef ProcessOneEntry(factory, newstruct, entry): 1306e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley optional = 0 1307e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley array = 0 1308e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry_type = '' 1309e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = '' 1310e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tag = '' 1311e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tag_set = None 1312e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley separator = '' 1313e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley fixed_length = '' 1314e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1315e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tokens = entry.split(' ') 1316e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley while tokens: 1317e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley token = tokens[0] 1318e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tokens = tokens[1:] 1319e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1320e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not entry_type: 1321e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not optional and token == 'optional': 1322e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley optional = 1 1323e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1324e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1325e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not array and token == 'array': 1326e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley array = 1 1327e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1328e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1329e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not entry_type: 1330e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry_type = token 1331e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1332e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1333e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not name: 1334e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley res = re.match(r'^([^\[\]]+)(\[.*\])?$', token) 1335e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not res: 1336e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError( 1337e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'Cannot parse name: \"%s\" ' 1338e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 'around line %d' % (entry, line_count)) 1339e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name = res.group(1) 1340e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley fixed_length = res.group(2) 1341e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if fixed_length: 1342e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley fixed_length = fixed_length[1:-1] 1343e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1344e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1345e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not separator: 1346e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley separator = token 1347e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if separator != '=': 1348e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Expected "=" after name \"%s\" got %s' 1349e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley % (name, token)) 1350e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1351e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1352e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not tag_set: 1353e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tag_set = 1 1354e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not re.match(r'^(0x)?[0-9]+$', token): 1355e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Expected tag number: \"%s\"' % entry) 1356e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tag = int(token, 0) 1357e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1358e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1359e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Cannot parse \"%s\"' % entry) 1360e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1361e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not tag_set: 1362e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Need tag number: \"%s\"' % entry) 1363e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1364e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Create the right entry 1365e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if entry_type == 'bytes': 1366e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if fixed_length: 1367e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryBytes(entry_type, name, tag, fixed_length) 1368e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley else: 1369e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryVarBytes(entry_type, name, tag) 1370e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley elif entry_type == 'int' and not fixed_length: 1371e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryInt(entry_type, name, tag) 1372e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley elif entry_type == 'int64' and not fixed_length: 1373e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryInt(entry_type, name, tag, bits=64) 1374e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley elif entry_type == 'string' and not fixed_length: 1375e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryString(entry_type, name, tag) 1376e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley else: 1377e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley res = structref.match(entry_type) 1378e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if res: 1379e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # References another struct defined in our file 1380e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryStruct(entry_type, name, tag, res.group(1)) 1381e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley else: 1382e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Bad type: "%s" in "%s"' % (entry_type, entry)) 1383e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1384e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley structs = [] 1385e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1386e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if optional: 1387e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.MakeOptional() 1388e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if array: 1389e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.MakeArray() 1390e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1391e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.SetStruct(newstruct) 1392e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.SetLineCount(line_count) 1393e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.Verify() 1394e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1395e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if array: 1396e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # We need to encapsulate this entry into a struct 1397e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newname = newentry.Name()+ '_array' 1398e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1399e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Now borgify the new entry. 1400e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry = factory.EntryArray(newentry) 1401e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.SetStruct(newstruct) 1402e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.SetLineCount(line_count) 1403e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newentry.MakeArray() 1404e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1405e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newstruct.AddEntry(newentry) 1406e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1407e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return structs 1408e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1409e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileydef ProcessStruct(factory, data): 1410e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tokens = data.split(' ') 1411e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1412e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # First three tokens are: 'struct' 'name' '{' 1413e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley newstruct = factory.Struct(tokens[1]) 1414e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1415e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley inside = ' '.join(tokens[3:-1]) 1416e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1417e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tokens = inside.split(';') 1418e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1419e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley structs = [] 1420e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1421e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in tokens: 1422e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry = NormalizeLine(entry) 1423e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not entry: 1424e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1425e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1426e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # It's possible that new structs get defined in here 1427e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley structs.extend(ProcessOneEntry(factory, newstruct, entry)) 1428e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1429e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley structs.append(newstruct) 1430e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return structs 1431e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1432e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileydef GetNextStruct(file): 1433e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley global line_count 1434e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley global cppdirect 1435e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1436e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley got_struct = 0 1437e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1438e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley processed_lines = [] 1439e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1440e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley have_c_comment = 0 1441e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley data = '' 1442e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley while 1: 1443e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = file.readline() 1444e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not line: 1445e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley break 1446e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1447e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line_count += 1 1448e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = line[:-1] 1449e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1450e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not have_c_comment and re.search(r'/\*', line): 1451e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if re.search(r'/\*.*?\*/', line): 1452e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = re.sub(r'/\*.*?\*/', '', line) 1453e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley else: 1454e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = re.sub(r'/\*.*$', '', line) 1455e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley have_c_comment = 1 1456e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1457e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if have_c_comment: 1458e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not re.search(r'\*/', line): 1459e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1460e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley have_c_comment = 0 1461e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = re.sub(r'^.*\*/', '', line) 1462e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1463e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley line = NormalizeLine(line) 1464e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1465e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not line: 1466e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1467e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1468e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not got_struct: 1469e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if re.match(r'#include ["<].*[>"]', line): 1470e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley cppdirect.append(line) 1471e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1472e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1473e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if re.match(r'^#(if( |def)|endif)', line): 1474e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley cppdirect.append(line) 1475e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1476e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1477e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if re.match(r'^#define', line): 1478e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley headerdirect.append(line) 1479e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1480e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1481e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not structdef.match(line): 1482e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Missing struct on line %d: %s' 1483e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley % (line_count, line)) 1484e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley else: 1485e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley got_struct = 1 1486e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley data += line 1487e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1488e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1489e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # We are inside the struct 1490e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley tokens = line.split('}') 1491e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if len(tokens) == 1: 1492e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley data += ' ' + line 1493e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley continue 1494e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1495e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if len(tokens[1]): 1496e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError('Trailing garbage after struct on line %d' 1497e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley % line_count) 1498e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1499e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # We found the end of the struct 1500e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley data += ' %s}' % tokens[0] 1501e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley break 1502e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1503e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Remove any comments, that might be in there 1504e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley data = re.sub(r'/\*.*\*/', '', data) 1505e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1506e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return data 1507e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1508e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1509e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileydef Parse(factory, file): 1510e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """ 1511e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley Parses the input file and returns C code and corresponding header file. 1512e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """ 1513e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1514e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entities = [] 1515e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1516e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley while 1: 1517e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Just gets the whole struct nicely formatted 1518e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley data = GetNextStruct(file) 1519e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1520e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not data: 1521e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley break 1522e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1523e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entities.extend(ProcessStruct(factory, data)) 1524e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1525e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return entities 1526e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1527e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass CCodeGenerator: 1528e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self): 1529e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pass 1530e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1531e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def GuardName(self, name): 1532e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Use the complete provided path to the input file, with all 1533e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # non-identifier characters replaced with underscores, to 1534e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # reduce the chance of a collision between guard macros. 1535e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '_' + nonident.sub('_', name).upper() + '_' 1536e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1537e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def HeaderPreamble(self, name): 1538e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley guard = self.GuardName(name) 1539e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre = ( 1540e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '/*\n' 1541e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' * Automatically generated from %s\n' 1542e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' */\n\n' 1543e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#ifndef %s\n' 1544e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#define %s\n\n' ) % ( 1545e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley name, guard, guard) 1546e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1547e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for statement in headerdirect: 1548e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += '%s\n' % statement 1549e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if headerdirect: 1550e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += '\n' 1551e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1552e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += ( 1553e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <event2/util.h> /* for ev_uint*_t */\n' 1554e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <event2/rpc.h>\n' 1555e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) 1556e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1557e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return pre 1558e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1559e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def HeaderPostamble(self, name): 1560e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley guard = self.GuardName(name) 1561e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '#endif /* %s */' % guard 1562e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1563e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def BodyPreamble(self, name, header_file): 1564e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley global _NAME 1565e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley global _VERSION 1566e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1567e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley slash = header_file.rfind('/') 1568e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if slash != -1: 1569e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley header_file = header_file[slash+1:] 1570e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1571e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre = ( '/*\n' 1572e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' * Automatically generated from %s\n' 1573e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' * by %s/%s. DO NOT EDIT THIS FILE.\n' 1574e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ' */\n\n' ) % (name, _NAME, _VERSION) 1575e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += ( '#include <stdlib.h>\n' 1576e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <string.h>\n' 1577e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <assert.h>\n' 1578e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <event2/event-config.h>\n' 1579e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <event2/event.h>\n' 1580e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <event2/buffer.h>\n' 1581e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#include <event2/tag.h>\n\n' 1582e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#ifdef _EVENT___func__\n' 1583e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#define __func__ _EVENT___func__\n' 1584e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley '#endif\n\n' 1585e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley ) 1586e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1587e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for statement in cppdirect: 1588e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += '%s\n' % statement 1589e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1590e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += '\n#include "%s"\n\n' % header_file 1591e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1592e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += 'void event_warn(const char *fmt, ...);\n' 1593e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley pre += 'void event_warnx(const char *fmt, ...);\n\n' 1594e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1595e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return pre 1596e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1597e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def HeaderFilename(self, filename): 1598e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '.'.join(filename.split('.')[:-1]) + '.h' 1599e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1600e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def CodeFilename(self, filename): 1601e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return '.'.join(filename.split('.')[:-1]) + '.gen.c' 1602e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1603e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def Struct(self, name): 1604e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return StructCCode(name) 1605e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1606e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryBytes(self, entry_type, name, tag, fixed_length): 1607e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return EntryBytes(entry_type, name, tag, fixed_length) 1608e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1609e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryVarBytes(self, entry_type, name, tag): 1610e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return EntryVarBytes(entry_type, name, tag) 1611e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1612e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryInt(self, entry_type, name, tag, bits=32): 1613e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return EntryInt(entry_type, name, tag, bits) 1614e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1615e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryString(self, entry_type, name, tag): 1616e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return EntryString(entry_type, name, tag) 1617e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1618e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryStruct(self, entry_type, name, tag, struct_name): 1619e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return EntryStruct(entry_type, name, tag, struct_name) 1620e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1621e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def EntryArray(self, entry): 1622e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley return EntryArray(entry) 1623e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1624e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass Usage(RpcGenError): 1625e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, argv0): 1626e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley RpcGenError.__init__("usage: %s input.rpc [[output.h] output.c]" 1627e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley % argv0) 1628e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1629e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyclass CommandLine: 1630e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def __init__(self, argv): 1631e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """Initialize a command-line to launch event_rpcgen, as if 1632e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley from a command-line with CommandLine(sys.argv). If you're 1633e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley calling this directly, remember to provide a dummy value 1634e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for sys.argv[0] 1635e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley """ 1636e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.filename = None 1637e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.header_file = None 1638e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.impl_file = None 1639e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.factory = CCodeGenerator() 1640e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1641e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if len(argv) < 2 or len(argv) > 4: 1642e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise Usage(argv[0]) 1643e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1644e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.filename = argv[1].replace('\\', '/') 1645e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if len(argv) == 3: 1646e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.impl_file = argv[2].replace('\\', '/') 1647e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if len(argv) == 4: 1648e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.header_file = argv[2].replace('\\', '/') 1649e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.impl_file = argv[3].replace('\\', '/') 1650e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1651e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self.filename: 1652e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise Usage(argv[0]) 1653e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1654e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self.impl_file: 1655e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.impl_file = self.factory.CodeFilename(self.filename) 1656e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1657e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self.header_file: 1658e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley self.header_file = self.factory.HeaderFilename(self.impl_file) 1659e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1660e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self.impl_file.endswith('.c'): 1661e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError("can only generate C implementation files") 1662e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if not self.header_file.endswith('.h'): 1663e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise RpcGenError("can only generate C header files") 1664e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1665e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley def run(self): 1666e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley filename = self.filename 1667e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley header_file = self.header_file 1668e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley impl_file = self.impl_file 1669e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley factory = self.factory 1670e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1671e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, 'Reading \"%s\"' % filename 1672e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1673e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley fp = open(filename, 'r') 1674e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entities = Parse(factory, fp) 1675e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley fp.close() 1676e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1677e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, '... creating "%s"' % header_file 1678e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley header_fp = open(header_file, 'w') 1679e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>header_fp, factory.HeaderPreamble(filename) 1680e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1681e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # Create forward declarations: allows other structs to reference 1682e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley # each other 1683e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in entities: 1684e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.PrintForwardDeclaration(header_fp) 1685e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>header_fp, '' 1686e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1687e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in entities: 1688e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.PrintTags(header_fp) 1689e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.PrintDeclaration(header_fp) 1690e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>header_fp, factory.HeaderPostamble(filename) 1691e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley header_fp.close() 1692e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1693e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, '... creating "%s"' % impl_file 1694e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley impl_fp = open(impl_file, 'w') 1695e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>impl_fp, factory.BodyPreamble(filename, header_file) 1696e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley for entry in entities: 1697e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley entry.PrintCode(impl_fp) 1698e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley impl_fp.close() 1699e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1700e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wileyif __name__ == '__main__': 1701e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley try: 1702e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley CommandLine(sys.argv).run() 1703e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley sys.exit(0) 1704e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1705e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley except RpcGenError, e: 1706e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, e 1707e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley sys.exit(1) 1708e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley 1709e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley except EnvironmentError, e: 1710e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley if e.filename and e.strerror: 1711e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >>sys.stderr, "%s: %s" % (e.filename, e.strerror) 1712e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley sys.exit(1) 1713e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley elif e.strerror: 1714e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley print >> sys.stderr, e.strerror 1715e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley sys.exit(1) 1716e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley else: 1717e867981d427db5e0b860d67485838e1f9e8c37daChristopher Wiley raise 1718