summaryrefslogtreecommitdiff
path: root/lib9p/idl.gen
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/idl.gen')
-rwxr-xr-xlib9p/idl.gen51
1 files changed, 37 insertions, 14 deletions
diff --git a/lib9p/idl.gen b/lib9p/idl.gen
index 4f1e8b8..bab36af 100755
--- a/lib9p/idl.gen
+++ b/lib9p/idl.gen
@@ -22,6 +22,26 @@ import idl
idprefix = "lib9p_"
+def tab_ljust(s: str, width: int) -> str:
+ cur = len(s.expandtabs(tabsize=8))
+ if cur >= width:
+ return s
+ return s + " " * (width - cur)
+
+
+def join_lines(*args: str) -> str:
+ return "\n".join([a.rstrip() for a in args]).rstrip() + "\n"
+
+
+def c_macro(*args: str) -> str:
+ full = join_lines(*args).rstrip()
+ assert "\n" in full
+ lines = [l.rstrip() for l in full.split("\n")]
+ width = max(len(l.expandtabs(tabsize=8)) for l in lines[:-1])
+ lines = [tab_ljust(l, width) for l in lines]
+ return " \\\n".join(lines).rstrip() + "\n"
+
+
def c_ver_enum(ver: str) -> str:
return f"{idprefix.upper()}VER_{ver.replace('.', '_')}"
@@ -664,26 +684,29 @@ LM_ALWAYS_INLINE static bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val)
ret += ifdef_pop(0)
# tables / exports #########################################################
- ret += f"""
+ ret += """
/* tables / exports ***********************************************************/
-
-#define _MSG(typ) [{idprefix.upper()}TYP_##typ] = {{ \\
- .name = #typ, \\
- .basesize = sizeof(struct {idprefix}msg_##typ), \\
- .validate = validate_##typ, \\
- .unmarshal = (_unmarshal_fn_t)unmarshal_##typ, \\
- .marshal = (_marshal_fn_t)marshal_##typ, \\
- }}
-#define _NONMSG(num) [num] = {{ \\
- .name = #num, \\
- }}
-
-struct _table_version _{idprefix}versions[{c_ver_enum('NUM')}] = {{
"""
id2typ: dict[int, idl.Message] = {}
for msg in [msg for msg in typs if isinstance(msg, idl.Message)]:
id2typ[msg.msgid] = msg
+ ret += "\n"
+ ret += c_macro(
+ f"#define _MSG(typ) [{idprefix.upper()}TYP_##typ] = {{",
+ f"\t\t.name = #typ,",
+ f"\t\t.basesize = sizeof(struct {idprefix}msg_##typ),",
+ f"\t\t.validate = validate_##typ,",
+ f"\t\t.unmarshal = (_unmarshal_fn_t)unmarshal_##typ,",
+ f"\t\t.marshal = (_marshal_fn_t)marshal_##typ,",
+ f"\t}}",
+ )
+ ret += c_macro(
+ f"#define _NONMSG(num) [num] = {{", f"\t\t.name = #num,", f"\t}}"
+ )
+
+ ret += "\n"
+ ret += f"struct _table_version _{idprefix}versions[{c_ver_enum('NUM')}] = {{\n"
for ver in ["unknown", *sorted(versions)]:
if ver != "unknown":
ret += ifdef_push(1, c_ver_ifdef({ver}))