summaryrefslogtreecommitdiff
path: root/lib9p/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/core.c')
-rw-r--r--lib9p/core.c90
1 files changed, 24 insertions, 66 deletions
diff --git a/lib9p/core.c b/lib9p/core.c
index a44bab6..464b31d 100644
--- a/lib9p/core.c
+++ b/lib9p/core.c
@@ -7,7 +7,6 @@
#include <stdarg.h> /* for va_* */
#include <string.h> /* for strlen(), strnlen(), strncpy(), memcmp(), memset() */
-#include <libfmt/fmt.h> /* for fmt_vsnprintf() */
#include <libmisc/assert.h> /* for assert() */
#include <libmisc/endian.h> /* for uint32le_decode() */
#include <libmisc/log.h> /* for const_byte_str() */
@@ -62,46 +61,6 @@ bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) {
return ctx->err_msg[0];
}
-int _lib9p_error(struct lib9p_ctx *ctx,
-#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
- lib9p_errno_t linux_errno,
-#endif
- char const *msg) {
- if (lib9p_ctx_has_error(ctx))
- return -1;
- strncpy(ctx->err_msg, msg, sizeof(ctx->err_msg));
- ctx->err_msg[sizeof(ctx->err_msg)-1] = '\0';
-
-#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
- ctx->err_num = linux_errno;
-#endif
-
- return -1;
-}
-
-int _lib9p_errorf(struct lib9p_ctx *ctx,
-#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
- lib9p_errno_t linux_errno,
-#endif
- char const *fmt, ...) {
- int n;
- va_list args;
-
- if (lib9p_ctx_has_error(ctx))
- return -1;
- va_start(args, fmt);
- n = fmt_vsnprintf(ctx->err_msg, sizeof(ctx->err_msg), fmt, args);
- va_end(args);
- if ((size_t)(n+1) < sizeof(ctx->err_msg))
- memset(&ctx->err_msg[n+1], 0, sizeof(ctx->err_msg)-(n+1));
-
-#if CONFIG_9P_ENABLE_9P2000_u || CONFIG_9P_ENABLE_9P2000_L
- ctx->err_num = linux_errno;
-#endif
-
- return -1;
-}
-
/* bounds checks **************************************************************/
static inline void assert_ver(enum lib9p_version ver) {
@@ -141,33 +100,32 @@ const char *lib9p_msgtype_str(enum lib9p_version ver, enum lib9p_msg_type typ) {
return _lib9p_table_msg[ver][typ].name ?: const_byte_str(typ);
}
-lo_interface fmt_formatter lo_box_lib9p_msg_as_fmt_formatter(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body) {
- assert(ctx);
- assert_ver(ctx->version);
- assert_typ(typ);
- assert(_lib9p_table_msg[ctx->version][typ].box_as_fmt_formatter);
- return _lib9p_table_msg[ctx->version][typ].box_as_fmt_formatter(body);
-}
-
/* main message functions *****************************************************/
-#define _lib9p_validate(LOW_TYP_BIT, ERRMSG, TABLE) do { \
- assert_ver(ctx->version); \
- /* Inspect the first 5 bytes ourselves. */ \
- uint32_t net_size = uint32le_decode(net_bytes); \
- if (net_size < 5) \
- return lib9p_error(ctx, LIB9P_ERRNO_L_EBADMSG, "message is impossibly short"); \
- uint8_t typ = net_bytes[4]; \
- if (typ % 2 != LOW_TYP_BIT) \
- return lib9p_errorf(ctx, LIB9P_ERRNO_L_EOPNOTSUPP, ERRMSG ": message_type=%s", \
- lib9p_msgtype_str(ctx->version, typ)); \
- struct _lib9p_recv_tentry tentry = TABLE[ctx->version][typ/2]; \
- if (!tentry.validate) \
- return lib9p_errorf(ctx, LIB9P_ERRNO_L_EOPNOTSUPP, "unknown message type: %s (protocol_version=%s)", \
- lib9p_msgtype_str(ctx->version, typ), lib9p_version_str(ctx->version)); \
- \
- /* Now use the message-type-specific tentry to process the whole thing. */ \
- return tentry.validate(ctx, net_size, net_bytes); \
+void fmt_print_lib9p_msg(lo_interface fmt_dest w, struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body) {
+ assert(ctx);
+ assert_ver(ctx->version);
+ assert_typ(typ);
+ assert(_lib9p_table_msg[ctx->version][typ].print);
+ _lib9p_table_msg[ctx->version][typ].print(w, ctx, body);
+}
+
+#define _lib9p_validate(LOW_TYP_BIT, ERRMSG, TABLE) do { \
+ assert_ver(ctx->version); \
+ /* Inspect the first 5 bytes ourselves. */ \
+ uint32_t net_size = uint32le_decode(net_bytes); \
+ if (net_size < 5) \
+ return lib9p_error(ctx, LIB9P_ERRNO_L_EBADMSG, "message is impossibly short"); \
+ uint8_t typ = net_bytes[4]; \
+ if (typ % 2 != LOW_TYP_BIT) \
+ return lib9p_error(ctx, LIB9P_ERRNO_L_EOPNOTSUPP, ERRMSG ": message_type=", lib9p_msgtype_str(ctx->version, typ)); \
+ struct _lib9p_recv_tentry tentry = TABLE[ctx->version][typ/2]; \
+ if (!tentry.validate) \
+ return lib9p_error(ctx, LIB9P_ERRNO_L_EOPNOTSUPP, "unknown message type: ", lib9p_msgtype_str(ctx->version, typ), \
+ " (protocol_version=", lib9p_version_str(ctx->version), ")"); \
+ \
+ /* Now use the message-type-specific tentry to process the whole thing. */ \
+ return tentry.validate(ctx, net_size, net_bytes); \
} while (0)
ssize_t lib9p_Tmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {