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