aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / fmt.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* fmt.c -- see fmt.h. */ |
| 18 | #include "fmt.h" |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | static void emit_n(ck_emit_fn emit, void *ctx, char c, int n, int *count) |
| 23 | { |
| 24 | for (int i = 0; i < n; i++) { |
| 25 | emit(ctx, c); |
| 26 | (*count)++; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | int ck_vformat(ck_emit_fn emit, void *ctx, const char *fmt, va_list ap) |
| 31 | { |
| 32 | int count = 0; |
| 33 | for (const char *p = fmt; *p; p++) { |
| 34 | if (*p != '%') { |
| 35 | emit(ctx, *p); |
| 36 | count++; |
| 37 | continue; |
| 38 | } |
| 39 | const char *start = p++; |
| 40 | int left = 0, zero = 0, width = 0, len = 0; /* len: -2 hh, -1 h, 1 l, 2 ll, 3 z */ |
| 41 | for (;; p++) { |
| 42 | if (*p == '-') |
| 43 | left = 1; |
| 44 | else if (*p == '0') |
| 45 | zero = 1; |
| 46 | else |
| 47 | break; |
| 48 | } |
| 49 | if (*p == '*') { |
| 50 | width = va_arg(ap, int); |
| 51 | if (width < 0) { |
| 52 | left = 1; |
| 53 | width = -width; |
| 54 | } |
| 55 | p++; |
| 56 | } else { |
| 57 | while (*p >= '0' && *p <= '9') |
| 58 | width = width * 10 + (*p++ - '0'); |
| 59 | } |
| 60 | if (*p == 'h') { |
| 61 | len = -1; |
| 62 | if (*++p == 'h') { |
| 63 | len = -2; |
| 64 | p++; |
| 65 | } |
| 66 | } else if (*p == 'l') { |
| 67 | len = 1; |
| 68 | if (*++p == 'l') { |
| 69 | len = 2; |
| 70 | p++; |
| 71 | } |
| 72 | } else if (*p == 'z') { |
| 73 | len = 3; |
| 74 | p++; |
| 75 | } |
| 76 | char c = *p; |
| 77 | if (c == '\0') { |
| 78 | /* Dangling '%': print what we saw and stop. */ |
| 79 | for (const char *q = start; *q; q++) { |
| 80 | emit(ctx, *q); |
| 81 | count++; |
| 82 | } |
| 83 | break; |
| 84 | } |
| 85 | char tmp[24]; |
| 86 | int n = 0, neg = 0; |
| 87 | const char *str = tmp; |
| 88 | const char *prefix = ""; |
| 89 | if (c == 's' || c == 'c' || c == '%') { |
| 90 | if (c == 's') { |
| 91 | str = va_arg(ap, const char *); |
| 92 | if (!str) |
| 93 | str = "(null)"; |
| 94 | while (str[n]) |
| 95 | n++; |
| 96 | } else { |
| 97 | tmp[0] = c == 'c' ? (char)va_arg(ap, int) : '%'; |
| 98 | n = 1; |
| 99 | } |
| 100 | int pad = width > n ? width - n : 0; |
| 101 | if (!left) |
| 102 | emit_n(emit, ctx, ' ', pad, &count); |
| 103 | for (int i = 0; i < n; i++) { |
| 104 | emit(ctx, str[i]); |
| 105 | count++; |
| 106 | } |
| 107 | if (left) |
| 108 | emit_n(emit, ctx, ' ', pad, &count); |
| 109 | continue; |
| 110 | } |
| 111 | if (c != 'd' && c != 'i' && c != 'u' && c != 'x' && c != 'X' && c != 'p') { |
| 112 | /* Unknown conversion: print it literally. */ |
| 113 | for (const char *q = start; q <= p; q++) { |
| 114 | emit(ctx, *q); |
| 115 | count++; |
| 116 | } |
| 117 | continue; |
| 118 | } |
| 119 | uint64_t v; |
| 120 | if (c == 'p') { |
| 121 | v = (uint64_t)(uintptr_t)va_arg(ap, void *); |
| 122 | prefix = "0x"; |
| 123 | } else if (c == 'd' || c == 'i') { |
| 124 | int64_t s; |
| 125 | if (len == 2) |
| 126 | s = va_arg(ap, long long); |
| 127 | else if (len == 1) |
| 128 | s = va_arg(ap, long); |
| 129 | else if (len == 3) |
| 130 | s = (int64_t)va_arg(ap, size_t); |
| 131 | else |
| 132 | s = va_arg(ap, int); |
| 133 | if (len == -1) |
| 134 | s = (short)s; |
| 135 | else if (len == -2) |
| 136 | s = (signed char)s; |
| 137 | neg = s < 0; |
| 138 | v = neg ? (uint64_t)0 - (uint64_t)s : (uint64_t)s; |
| 139 | } else { |
| 140 | if (len == 2) |
| 141 | v = va_arg(ap, unsigned long long); |
| 142 | else if (len == 1) |
| 143 | v = va_arg(ap, unsigned long); |
| 144 | else if (len == 3) |
| 145 | v = va_arg(ap, size_t); |
| 146 | else |
| 147 | v = va_arg(ap, unsigned int); |
| 148 | if (len == -1) |
| 149 | v = (unsigned short)v; |
| 150 | else if (len == -2) |
| 151 | v = (unsigned char)v; |
| 152 | } |
| 153 | unsigned base = (c == 'x' || c == 'X' || c == 'p') ? 16 : 10; |
| 154 | const char *digits = c == 'X' ? "0123456789ABCDEF" : "0123456789abcdef"; |
| 155 | do { |
| 156 | tmp[n++] = digits[v % base]; |
| 157 | v /= base; |
| 158 | } while (v); |
| 159 | int plen = neg ? 1 : (prefix[0] ? 2 : 0); |
| 160 | int pad = width > n + plen ? width - n - plen : 0; |
| 161 | if (!left && !zero) |
| 162 | emit_n(emit, ctx, ' ', pad, &count); |
| 163 | if (neg) { |
| 164 | emit(ctx, '-'); |
| 165 | count++; |
| 166 | } |
| 167 | for (const char *q = prefix; *q; q++) { |
| 168 | emit(ctx, *q); |
| 169 | count++; |
| 170 | } |
| 171 | if (!left && zero) |
| 172 | emit_n(emit, ctx, '0', pad, &count); |
| 173 | while (n) |
| 174 | emit(ctx, tmp[--n]), count++; |
| 175 | if (left) |
| 176 | emit_n(emit, ctx, ' ', pad, &count); |
| 177 | } |
| 178 | return count; |
| 179 | } |
| 180 | |
| 181 | struct sbuf { |
| 182 | char *buf; |
| 183 | size_t cap, len; |
| 184 | }; |
| 185 | |
| 186 | static void sbuf_emit(void *ctx, char c) |
| 187 | { |
| 188 | struct sbuf *s = ctx; |
| 189 | if (s->len + 1 < s->cap) |
| 190 | s->buf[s->len] = c; |
| 191 | s->len++; |
| 192 | } |
| 193 | |
| 194 | int ck_vsnprintf(char *buf, size_t n, const char *fmt, va_list ap) |
| 195 | { |
| 196 | struct sbuf s = { buf, n, 0 }; |
| 197 | int r = ck_vformat(sbuf_emit, &s, fmt, ap); |
| 198 | if (n) |
| 199 | buf[s.len < n ? s.len : n - 1] = '\0'; |
| 200 | return r; |
| 201 | } |
| 202 | |
| 203 | int ck_snprintf(char *buf, size_t n, const char *fmt, ...) |
| 204 | { |
| 205 | va_list ap; |
| 206 | va_start(ap, fmt); |
| 207 | int r = ck_vsnprintf(buf, n, fmt, ap); |
| 208 | va_end(ap); |
| 209 | return r; |
| 210 | } |