summaryrefslogtreecommitdiff
path: root/lib9p/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/core.c')
-rw-r--r--lib9p/core.c95
1 files changed, 95 insertions, 0 deletions
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 <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <inttypes.h> /* for PRIu{n} */
+#include <stdarg.h> /* for va_* */
+#include <string.h> /* for strncpy() */
+
+#include <libfmt/fmt.h> /* for fmt_vsnprintf() */
+
+#include <lib9p/core.h>
+
+/* 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;
+}