From 1e4ad99311611a7fbb5cdfe4b1b79202381dc717 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 13 Apr 2025 22:13:43 -0600 Subject: Split lib9p into lib9p_core and lib9p_srv --- lib9p/core.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 lib9p/core.c (limited to 'lib9p/core.c') diff --git a/lib9p/core.c b/lib9p/core.c new file mode 100644 index 0000000..a07461d --- /dev/null +++ b/lib9p/core.c @@ -0,0 +1,95 @@ +/* lib9p/core.c - Base 9P protocol utilities for both clients and servers + * + * Copyright (C) 2024-2025 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include /* for PRIu{n} */ +#include /* for va_* */ +#include /* for strncpy() */ + +#include /* for fmt_vsnprintf() */ + +#include + +/* strings ********************************************************************/ + +struct lib9p_s lib9p_str(char *s) { + if (!s) + return (struct lib9p_s){0}; + return (struct lib9p_s){ + .len = strlen(s), + .utf8 = s, + }; +} +struct lib9p_s lib9p_strn(char *s, size_t maxlen) { + if (maxlen == 0 || !s) + return (struct lib9p_s){0}; + return (struct lib9p_s){ + .len = strnlen(s, maxlen), + .utf8 = s, + }; +} +struct lib9p_s lib9p_str_slice(struct lib9p_s s, uint16_t beg, uint16_t end) { + assert(s.len == 0 || s.utf8); + assert(beg <= end && end <= s.len); + return (struct lib9p_s){ + .len = end - beg, + .utf8 = &s.utf8[beg], + }; +} +bool lib9p_str_eq(struct lib9p_s a, struct lib9p_s b) { + return a.len == b.len && + (a.len == 0 || memcmp(a.utf8, b.utf8, a.len) == 0); +} + +/* ctx ************************************************************************/ + +void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) { + assert(ctx); +#if CONFIG_9P_ENABLE_9P2000_u + ctx->err_num = 0; +#endif + ctx->err_msg[0] = '\0'; +} + +bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) { + assert(ctx); + return ctx->err_msg[0]; +} + +int lib9p_error(struct lib9p_ctx *ctx, lib9p_errno_t linux_errno, 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 + ctx->err_num = linux_errno; +#else + (void)(linux_errno); +#endif + + return -1; +} + +int lib9p_errorf(struct lib9p_ctx *ctx, lib9p_errno_t linux_errno, 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 + ctx->err_num = linux_errno; +#else + (void)(linux_errno); +#endif + + return -1; +} -- cgit v1.2.3-2-g168b