Artifact 76281bc79a234a1cdbd8c0795a1ada549718de6e19b146b948210224168d5dec:
- File
src/curate_text.c
— part of check-in
[1ed63201f4]
at
2026-11-18 03:02:04
on branch trunk
— suit curate apply turns the page's choices into config changes
suit curate apply FILE reads the choices file the curation page makes and prints a plan: which outfits and wears it edits, which wardrobes it checks in and in what order, and the links that will appear and disappear at each scope. Without --yes nothing changes. With --yes it edits each wardrobe's suit.conf and checks it in, with a comment such as "Curate: 3 kept, 1 limited, 12 retired". It does not place anything; the next run sees the config changed and applies.
- keep leaves a worn asset as it is. A loose asset, or one coming back from the attic, goes to the outfit kept.WARDROBE, worn at user scope. - limit moves the asset from its user outfits to personal.WARDROBE.SLUG, worn by [wear personal PROJECT], created or reused wherever it is. - retire removes the asset from every outfit of its wardrobe and adds it to attic.WARDROBE, which nothing wears. Apply refuses if a wear reaches an attic. - Removing one name from an outfit that lists `*` turns the `*` into an explicit list of the names it reached, minus that one, and the plan says so: names added to the store later are then not included by that outfit. No exclusion syntax was added.
Outfit names carry the wardrobe because outfit names are one set across wardrobes; a name too long for that is refused for its line. A wardrobe that defines a new outfit is checked in before any wardrobe whose wear names it, so a failure part-way leaves only an outfit nothing wears; a run that would need the opposite order is refused before anything changes.
Configs are edited as text: only the lines that must change are rewritten, and comments, blank lines, alignment, line endings and the order of everything else are kept. Every edited config must parse and the combined set must pass the merge check before anything is written. A wardrobe with local changes is refused, so the check-in holds the curation only. A failed check-in puts that wardrobe's file back.
A malformed line, a duplicate, a bad header or a path given as a project refuses the whole file, listing every problem. An unknown asset is warned about and skipped; a choice already true is silent.
A new suite, test/curate_apply.sh, with 376 cases, is part of the gate, and a new fuzz target covers the choices parser and the line editor.
3669 unit checks (was 2993), 625 shell cases, 1153 store cases, 985 placement cases, 378 project cases, 236 usage cases, 288 curate cases and 376 curate-apply cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (44 minutes). 100% coverage in all 32 source files. No existing case was changed. (user: dmestas, size: 17990) [annotate] [blame] [check-ins using]
#include "curate.h" #include "platform.h" #include <stdio.h> #include <string.h> // ---- the front matter ------------------------------------------------------------------ typedef struct { char *out; size_t n; bool space; } Text; // Add the LEN bytes of S, a run of white space becoming one space and none at either end. static void text_add(Text *t, const char *s, size_t len) { for (size_t i = 0; i < len; i++) { char c = s[i]; if (c == ' ' || c == '\t' || c == '\r') { t->space = t->n > 0; } else { if (t->space) t->out[t->n++] = ' '; t->space = false; t->out[t->n++] = c; } } } static size_t line_end(const char *d, size_t from, size_t to) { const char *nl = memchr(d + from, '\n', to - from); return nl != nullptr ? (size_t)(nl - d) : to; } static bool is_fence(const char *line, size_t len) { while (len > 0 && (line[len - 1] == '\r' || line[len - 1] == ' ' || line[len - 1] == '\t')) len--; return len == 3 && memcmp(line, "---", 3) == 0; } static bool is_indented(const char *line, size_t len) { return len > 0 && (line[0] == ' ' || line[0] == '\t'); } // The text of a quoted value that starts at S[0] (after its opening quote) and may go on // over later indented lines: returns the offset in BODY where the next line starts. typedef struct { const char *body; size_t end; // the end of the front matter size_t next; // the start of the next line to read } Lines; static void quoted(Text *t, Lines *ls, const char *s, size_t len, char quote) { for (;;) { for (size_t i = 0; i < len; i++) { if (quote == '"' && s[i] == '\\' && i + 1 < len) { char c = s[++i]; if (c == 'n' || c == 't') text_add(t, " ", 1); else if (c == '"' || c == '\\') text_add(t, &c, 1); else text_add(t, s + i - 1, 2); } else if (quote == '\'' && s[i] == '\'' && i + 1 < len && s[i + 1] == '\'') { text_add(t, "'", 1); i++; } else if (s[i] == quote) { return; } else { text_add(t, s + i, 1); } } // The value goes on over the next line only if that is indented. if (ls->next >= ls->end) return; size_t stop = line_end(ls->body, ls->next, ls->end); if (!is_indented(ls->body + ls->next, stop - ls->next)) return; text_add(t, " ", 1); s = ls->body + ls->next; len = stop - ls->next; ls->next = stop + 1; } } // The value of a block scalar or a plain one: the lines after the key that are indented (for a // block, blank lines too). static void continued(Text *t, Lines *ls, bool block) { while (ls->next < ls->end) { size_t stop = line_end(ls->body, ls->next, ls->end); const char *line = ls->body + ls->next; size_t len = stop - ls->next; bool blank = true; for (size_t i = 0; i < len; i++) blank &= line[i] == ' ' || line[i] == '\t' || line[i] == '\r'; if (!(is_indented(line, len) || (block && blank))) return; text_add(t, line, len); text_add(t, " ", 1); ls->next = stop + 1; } } static bool block_header(const char *v, size_t len) { if (len == 0 || (v[0] != '>' && v[0] != '|')) return false; for (size_t i = 1; i < len; i++) if (strchr("+-0123456789", v[i]) == nullptr) return false; return true; } size_t curate_front_description(const char *data, size_t len, char *out) { out[0] = '\0'; size_t at = len >= 3 && memcmp(data, "\xEF\xBB\xBF", 3) == 0 ? 3 : 0; size_t stop = line_end(data, at, len); if (!is_fence(data + at, stop - at)) return 0; size_t body = stop + 1; size_t end = body; bool closed = false; while (end < len && !closed) { stop = line_end(data, end, len); closed = is_fence(data + end, stop - end); if (!closed) end = stop + 1; } if (!closed) return 0; Text t = {.out = out}; Lines ls = {.body = data, .end = end, .next = body}; while (ls.next < end) { size_t from = ls.next; stop = line_end(data, from, end); ls.next = stop + 1; static const char key[] = "description:"; size_t klen = sizeof key - 1; const char *line = data + from; size_t llen = stop - from; bool is_key = llen >= klen && memcmp(line, key, klen) == 0 && (llen == klen || line[klen] == ' ' || line[klen] == '\t' || line[klen] == '\r'); if (!is_key) continue; const char *v = line + klen; size_t vlen = llen - klen; while (vlen > 0 && (*v == ' ' || *v == '\t')) { v++; vlen--; } while (vlen > 0 && (v[vlen - 1] == ' ' || v[vlen - 1] == '\t' || v[vlen - 1] == '\r')) vlen--; if (block_header(v, vlen)) { continued(&t, &ls, true); } else if (vlen > 0 && (v[0] == '"' || v[0] == '\'')) { quoted(&t, &ls, v + 1, vlen - 1, v[0]); } else { text_add(&t, v, vlen); continued(&t, &ls, false); } break; } out[t.n] = '\0'; return t.n; } size_t curate_cut(const char *s, size_t len, size_t max) { if (len <= max) return len; size_t c = max; while (c > 0 && ((unsigned char)s[c] & 0xC0) == 0x80) c--; return c; } // ---- JSON ------------------------------------------------------------------------------ void curate_put(CurateBuf *b, const char *s, size_t n) { if (b->over || b->len + n > b->cap) { b->over = true; return; } memcpy(b->p + b->len, s, n); b->len += n; } static void put(CurateBuf *b, const char *s) { curate_put(b, s, strlen(s)); } // The length of the UTF-8 character at S (N bytes left), or 0 if the bytes are not one. static size_t utf8_len(const unsigned char *s, size_t n) { unsigned char c = s[0]; size_t need = 1; uint32_t cp; uint32_t min = 0; if (c >= 0x80) { if (c >= 0xC2 && c <= 0xDF) { need = 2; cp = c & 0x1Fu; min = 0x80; } else if ((c & 0xF0) == 0xE0) { need = 3; cp = c & 0x0Fu; min = 0x800; } else if (c >= 0xF0 && c <= 0xF4) { need = 4; cp = c & 0x07u; min = 0x10000; } else { return 0; } if (n < need) return 0; for (size_t i = 1; i < need; i++) { if ((s[i] & 0xC0) != 0x80) return 0; cp = cp << 6 | (s[i] & 0x3Fu); } if (cp < min || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) return 0; } return need; } void curate_json_string(CurateBuf *b, const char *s, size_t len) { put(b, "\""); const unsigned char *u = (const unsigned char *)s; for (size_t i = 0; i < len;) { size_t l = utf8_len(u + i, len - i); unsigned char c = u[i]; if (l == 0) { put(b, "\xEF\xBF\xBD"); i++; } else if (l == 3 && c == 0xE2 && u[i + 1] == 0x80 && (u[i + 2] == 0xA8 || u[i + 2] == 0xA9)) { put(b, u[i + 2] == 0xA8 ? "\\u2028" : "\\u2029"); i += 3; } else if (l > 1) { curate_put(b, s + i, l); i += l; } else { if (c == '"') put(b, "\\\""); else if (c == '\\') put(b, "\\\\"); else if (c == '<') put(b, "\\u003c"); else if (c == '>') put(b, "\\u003e"); else if (c == '&') put(b, "\\u0026"); else if (c < 0x20) { char e[8]; [[maybe_unused]] int n = snprintf(e, sizeof e, "\\u%04x", c); ensure(n == 6); put(b, e); } else { curate_put(b, s + i, 1); } i++; } } put(b, "\""); } // ---- the choices file -------------------------------------------------------------------------- bool curate_choices_header(const char *line, size_t len) { if (len > 0 && line[len - 1] == '\r') len--; return len == 16 && memcmp(line, "# suit choices 1", 16) == 0; } typedef struct { const char *p; size_t n; } Field; // Split LINE at single spaces into at most MAX fields; the number of fields, or MAX + 1 if there are more. static size_t split_fields(const char *line, size_t len, Field *f, size_t max) { size_t n = 0; size_t from = 0; for (size_t i = 0; i <= len; i++) { if (i < len && line[i] != ' ') continue; if (n == max) return max + 1; f[n++] = (Field){line + from, i - from}; from = i + 1; } return n; } static bool field_is(const Field *f, const char *word) { return f->n == strlen(word) && memcmp(f->p, word, f->n) == 0; } static void bad(char *msg, size_t cap, const char *what, const Field *f) { snprintf(msg, cap, "%s '%.*s'", what, (int)(f->n < 40 ? f->n : 40), f->p); } static bool target_chars(const Field *f) { for (size_t i = 0; i < f->n; i++) { char c = f->p[i]; bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || strchr("._-/:@~", c) != nullptr; if (!ok) return false; } return true; } ChoiceLine curate_choice_parse(const char *line, size_t len, CurateChoice *c, char *msg, size_t cap) { if (len > 0 && line[len - 1] == '\r') len--; if (len == 0 || line[0] == '#') return CHOICE_NONE; if (memchr(line, '\0', len) != nullptr) { snprintf(msg, cap, "NUL byte in line"); return CHOICE_BAD; } Field f[6]; size_t n = split_fields(line, len, f, 5); if (n > 5) { snprintf(msg, cap, "too many fields: a choice has four, or five for personal"); return CHOICE_BAD; } ChoiceAct act; if (field_is(&f[0], "keep")) act = CHOICE_KEEP; else if (field_is(&f[0], "retire")) act = CHOICE_RETIRE; else if (field_is(&f[0], "personal")) act = CHOICE_PERSONAL; else { bad(msg, cap, "unknown choice (keep, retire or personal):", &f[0]); return CHOICE_BAD; } size_t want = act == CHOICE_PERSONAL ? 5 : 4; if (n != want) { snprintf(msg, cap, "%s takes %zu fields, KIND WARDROBE NAME%s, and this has %zu", act == CHOICE_PERSONAL ? "personal" : "keep and retire", want - 1, act == CHOICE_PERSONAL ? " PROJECT" : "", n - 1); return CHOICE_BAD; } int kind = field_is(&f[1], "skill") ? 0 : field_is(&f[1], "agent") ? 1 : field_is(&f[1], "command") ? 2 : -1; if (kind < 0) { bad(msg, cap, "KIND is skill, agent or command, not", &f[1]); return CHOICE_BAD; } if (!conf_name_ok(f[2].p, f[2].n, false)) { bad(msg, cap, "WARDROBE is not a name:", &f[2]); return CHOICE_BAD; } if (!conf_name_ok(f[3].p, f[3].n, false)) { bad(msg, cap, "NAME is not a name:", &f[3]); return CHOICE_BAD; } if (act == CHOICE_PERSONAL) { if (f[4].n > CHOICE_PROJECT_MAX || !target_chars(&f[4]) || f[4].n == 0) { bad(msg, cap, "PROJECT is not a project key:", &f[4]); return CHOICE_BAD; } if (f[4].p[0] == '~' || f[4].p[0] == '/' || field_is(&f[4], "-")) { bad(msg, cap, "PROJECT is a folder, not a project key (give it one with `suit project set`):", &f[4]); return CHOICE_FOLDER; } } *c = (CurateChoice){.act = act, .kind = kind}; memcpy(c->ward, f[2].p, f[2].n); memcpy(c->name, f[3].p, f[3].n); if (act == CHOICE_PERSONAL) memcpy(c->project, f[4].p, f[4].n); return CHOICE_OK; } bool curate_is_attic(const char *name) { return strcmp(name, "attic") == 0 || strncmp(name, "attic.", 6) == 0; } size_t curate_home_outfit(const char *base, const char *ward, char *out) { size_t bl = strlen(base); size_t wl = strlen(ward); if (bl + 1 + wl > CONF_NAME_MAX) return 0; memcpy(out, base, bl); out[bl] = '.'; memcpy(out + bl + 1, ward, wl + 1); return bl + 1 + wl; } size_t curate_personal_outfit(const char *ward, const char *project, unsigned n, char *out) { char suffix[16] = ""; size_t sl = 0; if (n > 1) sl = (size_t)snprintf(suffix, sizeof suffix, "-%u", n); constexpr size_t PREFIX = sizeof "personal." - 1; size_t room = CONF_NAME_MAX - sl; size_t wl = strlen(ward); if (PREFIX + wl + 1 > room) return 0; memcpy(out, "personal.", PREFIX); memcpy(out + PREFIX, ward, wl); size_t o = PREFIX + wl; out[o++] = '.'; for (size_t i = 0; project[i] != '\0' && o < room; i++) { char c = project[i]; bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-'; out[o++] = ok ? c : '-'; } memcpy(out + o, suffix, sl + 1); return o + sl; } // ---- editing a suit.conf as text ------------------------------------------------------------------ static bool blank_char(char c) { return c == ' ' || c == '\t' || c == '\r'; } // The bytes of LINE (counted from 1 as the parser counts): its START and its END, before its newline. static void line_at(const char *t, size_t len, int line, size_t *start, size_t *end) { size_t pos = 0; for (int i = 1; i < line; i++) { const char *nl = memchr(t + pos, '\n', len - pos); ensure(nl != nullptr); // the caller got LINE from the parser pos = (size_t)(nl - t) + 1; } ensure(pos < len); const char *nl = memchr(t + pos, '\n', len - pos); *start = pos; *end = nl != nullptr ? (size_t)(nl - t) : len; } // Where the value of the entry in the N bytes of L starts and ends, as the parser finds it: the comment // (a # at the start or after white space) is not part of the line, the key ends at the first " = ". static void value_span(const char *l, size_t n, size_t *vs, size_t *ve) { size_t c = 1; // an entry does not start with a # while (c < n && !(l[c] == '#' && blank_char(l[c - 1]))) c++; size_t e = c; while (blank_char(l[e - 1])) e--; // there is a key before it size_t i = 0; while (memcmp(l + i, " = ", 3) != 0) { i++; ensure(i + 3 <= e); // the line is an entry the parser took } size_t s = i + 3; while (blank_char(l[s])) s++; // there is a value after it *vs = s; *ve = e; } // The line ending the first line of the text has. static size_t eol_len(const char *t, size_t len) { const char *nl = memchr(t, '\n', len); return nl != nullptr && nl > t && nl[-1] == '\r' ? 2 : 1; } EditResult curate_edit_value(char *t, size_t *len, size_t cap, int line, const char *const *words, size_t n) { size_t s; size_t e; line_at(t, *len, line, &s, &e); if (n == 0) { size_t from = s; size_t to = e < *len ? e + 1 : e; if (e == *len && from > 0) { // the last line, with no newline: the one before it becomes the last from -= from > 1 && t[from - 2] == '\r' ? 2 : 1; } memmove(t + from, t + to, *len - to); *len -= to - from; return EDIT_OK; } size_t vs; size_t ve; value_span(t + s, e - s, &vs, &ve); size_t vlen = n - 1; for (size_t i = 0; i < n; i++) vlen += strlen(words[i]); size_t line_len = vs + vlen + (e - s - ve); if (line_len > CONF_LINE_MAX) return EDIT_LONG; size_t total = *len - (e - s) + line_len; if (total > cap) return EDIT_FULL; memmove(t + s + vs + vlen, t + s + ve, *len - (s + ve)); size_t at = s + vs; for (size_t i = 0; i < n; i++) { if (i > 0) t[at++] = ' '; size_t wl = strlen(words[i]); memcpy(t + at, words[i], wl); at += wl; } *len = total; return EDIT_OK; } EditResult curate_edit_insert(char *t, size_t *len, size_t cap, int line, const char *entry) { size_t s; size_t e; line_at(t, *len, line, &s, &e); size_t el = eol_len(t, *len); const char *eol = el == 2 ? "\r\n" : "\n"; size_t ll = strlen(entry); if (ll > CONF_LINE_MAX) return EDIT_LONG; if (*len + ll + el > cap) return EDIT_FULL; if (e < *len) { size_t at = e + 1; memmove(t + at + ll + el, t + at, *len - at); memcpy(t + at, entry, ll); memcpy(t + at + ll, eol, el); *len += ll + el; } else { // the last line has no newline: the new one follows it, and has none either memcpy(t + *len, eol, el); memcpy(t + *len + el, entry, ll); *len += ll + el; } return EDIT_OK; } EditResult curate_edit_append(char *t, size_t *len, size_t cap, const char *header, const char *entry) { size_t el = eol_len(t, *len); const char *eol = el == 2 ? "\r\n" : "\n"; size_t hl = strlen(header); size_t nl = entry != nullptr ? strlen(entry) : 0; if (hl > CONF_LINE_MAX || nl > CONF_LINE_MAX) return EDIT_LONG; bool empty = *len == 0; bool ends = !empty && t[*len - 1] == '\n'; // A blank line already ends the text: its last line is empty or only white space. bool has_blank = false; if (ends) { size_t last = *len - 1; size_t from = last; while (from > 0 && t[from - 1] != '\n') from--; has_blank = true; for (size_t i = from; i < last; i++) has_blank &= blank_char(t[i]); } size_t need = (empty || ends ? 0 : el) + (empty || has_blank ? 0 : el) + hl + el + (entry != nullptr ? nl + el : 0); if (*len + need > cap) return EDIT_FULL; size_t at = *len; if (!empty && !ends) { memcpy(t + at, eol, el); at += el; } if (!empty && !has_blank) { memcpy(t + at, eol, el); at += el; } memcpy(t + at, header, hl); at += hl; memcpy(t + at, eol, el); at += el; if (entry != nullptr) { memcpy(t + at, entry, nl); at += nl; memcpy(t + at, eol, el); at += el; } *len = at; return EDIT_OK; }