diff options
Diffstat (limited to 'lib9p')
-rw-r--r-- | lib9p/9p.generated.c | 16 | ||||
-rwxr-xr-x | lib9p/idl.gen | 51 |
2 files changed, 45 insertions, 22 deletions
diff --git a/lib9p/9p.generated.c b/lib9p/9p.generated.c index d3dc36a..373d95e 100644 --- a/lib9p/9p.generated.c +++ b/lib9p/9p.generated.c @@ -1784,15 +1784,15 @@ LM_FLATTEN static bool marshal_Rswrite(struct _marshal_ctx *ctx, struct lib9p_ms /* tables / exports ***********************************************************/ -#define _MSG(typ) [LIB9P_TYP_##typ] = { \ - .name = #typ, \ - .basesize = sizeof(struct lib9p_msg_##typ), \ - .validate = validate_##typ, \ - .unmarshal = (_unmarshal_fn_t)unmarshal_##typ, \ - .marshal = (_marshal_fn_t)marshal_##typ, \ +#define _MSG(typ) [LIB9P_TYP_##typ] = { \ + .name = #typ, \ + .basesize = sizeof(struct lib9p_msg_##typ), \ + .validate = validate_##typ, \ + .unmarshal = (_unmarshal_fn_t)unmarshal_##typ, \ + .marshal = (_marshal_fn_t)marshal_##typ, \ } -#define _NONMSG(num) [num] = { \ - .name = #num, \ +#define _NONMSG(num) [num] = { \ + .name = #num, \ } struct _table_version _lib9p_versions[LIB9P_VER_NUM] = { 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})) |