interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / allen_scene.c

519 lines · 16855 bytes Raw Blame History
1/* allen_scene.c -- Allen's canvas, the words side; see allen_scene.h and docs/ALLEN_CANVAS.md. */
2#include "allen_scene.h"
3
4#define LINE_MAX_BYTES CK_SCENE_BYTES
5#define SAY_ROOM 159u /* Allen's words: allen_ui.c TEXT_MAX - 1 */
6
7static int lower(int c) { return c >= 'A' && c <= 'Z' ? c + 32 : c; }
8static int alpha(int c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
9static int digit(int c) { return c >= '0' && c <= '9'; }
10
11/* a == the word w (any case), n letters of a */
12static int is(const char *a, unsigned n, const char *w)
13{
14 unsigned i = 0;
15 for (; i < n && w[i]; i++)
16 if (lower(a[i]) != w[i])
17 return 0;
18 return i == n && !w[i];
19}
20
21static int any_of(const char *a, unsigned n, const char *const *list)
22{
23 for (; *list; list++)
24 if (is(a, n, *list))
25 return 1;
26 return 0;
27}
28
29const char *ck_scene_kind_name(unsigned kind)
30{
31 static const char *const names[] = {"none", "note", "list", "number", "quote", "person", "timer", "picture", "video"};
32 return kind < sizeof names / sizeof names[0] ? names[kind] : "none";
33}
34
35/* the kind a first word names: exact (1) or a near word the model reached for (2, counted as tidied) */
36static unsigned kind_of(const char *w, unsigned n, int *near)
37{
38 *near = 0;
39 for (unsigned k = CK_THING_NOTE; k <= CK_THING_VIDEO; k++)
40 if (is(w, n, ck_scene_kind_name(k)))
41 return k;
42 static const struct {
43 const char *w;
44 unsigned k;
45 } nears[] = {{"card", CK_THING_NOTE}, {"text", CK_THING_NOTE}, {"items", CK_THING_LIST},
46 {"bignumber", CK_THING_NUMBER}, {"image", CK_THING_PICTURE}, {"photo", CK_THING_PICTURE},
47 {"clip", CK_THING_VIDEO}, {"movie", CK_THING_VIDEO}, {"countdown", CK_THING_TIMER},
48 {"reminder", CK_THING_TIMER}, {"message", CK_THING_PERSON}, {"friend", CK_THING_PERSON}};
49 for (unsigned i = 0; i < sizeof nears / sizeof nears[0]; i++)
50 if (is(w, n, nears[i].w)) {
51 *near = 1;
52 return nears[i].k;
53 }
54 return 0;
55}
56
57/* how much room each field of a kind has (bytes, without the NUL) */
58static unsigned room(unsigned kind, unsigned i)
59{
60 switch (kind) {
61 case CK_THING_NOTE:
62 return i == 0 ? 48 : 240;
63 case CK_THING_LIST:
64 return i == 0 ? 48 : 64;
65 case CK_THING_NUMBER:
66 return i == 0 ? 16 : 64;
67 case CK_THING_QUOTE:
68 return i == 0 ? 220 : 48;
69 case CK_THING_PERSON:
70 return i == 0 ? 32 : 200;
71 case CK_THING_TIMER:
72 return i == 0 ? 24 : 64;
73 default: /* picture, video: what it shows, where it comes from */
74 return i == 0 ? 120 : 64;
75 }
76}
77
78/* the fewest fields a kind needs to show anything */
79static unsigned needs(unsigned kind) { return kind == CK_THING_PERSON ? 2u : 1u; }
80
81struct parse {
82 ck_scene *s;
83 ck_scene_stats st;
84};
85
86/* Copies n bytes of a cleaned field into the scene's text, cut to max at a
87 * word (with "..." when cut) so a field never ends mid-word. Returns its
88 * offset, or -1 when the text is full. */
89static int keep(struct parse *p, const char *a, unsigned n, unsigned max)
90{
91 while (n && a[0] == ' ')
92 a++, n--;
93 while (n && a[n - 1] == ' ')
94 n--;
95 int cut = 0;
96 if (n > max) {
97 cut = 1;
98 unsigned m = max > 3 ? max - 3 : max, sp = m;
99 while (sp > m / 2 && a[sp] != ' ')
100 sp--;
101 n = sp > m / 2 ? sp : m;
102 while (n && a[n - 1] == ' ')
103 n--;
104 }
105 ck_scene *s = p->s;
106 unsigned need = n + (cut ? 3u : 0u) + 1u;
107 if ((unsigned)s->used + need > CK_SCENE_TEXT)
108 return -1;
109 int at = s->used;
110 for (unsigned i = 0; i < n; i++)
111 s->text[s->used++] = a[i];
112 if (cut) {
113 s->text[s->used++] = '.', s->text[s->used++] = '.', s->text[s->used++] = '.';
114 p->st.cut++;
115 }
116 s->text[s->used++] = 0;
117 return at;
118}
119
120/* A say: one or two sentences, cut at the last sentence end that fits, else at a word. */
121static void say(struct parse *p, const char *a, unsigned n)
122{
123 while (n && (a[0] == ' ' || a[0] == ':'))
124 a++, n--;
125 while (n && a[n - 1] == ' ')
126 n--;
127 if (n > SAY_ROOM) {
128 unsigned end = 0;
129 for (unsigned i = SAY_ROOM / 3; i < SAY_ROOM; i++)
130 if ((a[i] == '.' || a[i] == '!' || a[i] == '?') && (i + 1 == n || a[i + 1] == ' '))
131 end = i + 1;
132 if (end) {
133 p->st.cut++;
134 n = end;
135 }
136 }
137 int at = keep(p, a, n, SAY_ROOM);
138 if (at >= 0) {
139 p->s->say = (uint16_t)at;
140 p->s->has_say = 1;
141 }
142}
143
144static void mood(struct parse *p, const char *a, unsigned n)
145{
146 static const char *const night[] = {"night", "calm", "still-night", 0};
147 static const char *const dawn[] = {"dawn", "warm", "morning", "sunrise", "golden", 0};
148 static const char *const deep[] = {"deep", "quiet", "dark", "soft", 0};
149 static const char *const clear[] = {"clear", "bright", "day", "light", 0};
150 for (unsigned i = 0; i < n;) {
151 while (i < n && !alpha(a[i]) && a[i] != '-')
152 i++;
153 unsigned w = i;
154 while (i < n && (alpha(a[i]) || a[i] == '-'))
155 i++;
156 unsigned wn = i - w;
157 if (!wn)
158 continue;
159 const char *x = a + w;
160 if (any_of(x, wn, night))
161 p->s->tone = CK_TONE_NIGHT;
162 else if (any_of(x, wn, dawn))
163 p->s->tone = CK_TONE_DAWN;
164 else if (any_of(x, wn, deep))
165 p->s->tone = CK_TONE_DEEP;
166 else if (any_of(x, wn, clear))
167 p->s->tone = CK_TONE_CLEAR;
168 else if (is(x, wn, "still"))
169 p->s->water = CK_WATER_STILL;
170 else if (is(x, wn, "slow"))
171 p->s->water = CK_WATER_SLOW;
172 else if (is(x, wn, "lively"))
173 p->s->water = CK_WATER_LIVELY;
174 else if (!is(x, wn, "water") && !is(x, wn, "sky") && !is(x, wn, "tone"))
175 p->st.unknown++;
176 }
177}
178
179/* one word before the colon: where, how much, how it comes, which way */
180static int modifier(ck_scene_thing *t, const char *w, unsigned n)
181{
182 if (is(w, n, "left"))
183 t->place = CK_PLACE_LEFT;
184 else if (is(w, n, "right"))
185 t->place = CK_PLACE_RIGHT;
186 else if (is(w, n, "above") || is(w, n, "top"))
187 t->place = CK_PLACE_ABOVE;
188 else if (is(w, n, "warm"))
189 t->warm = 1, t->quiet = 0;
190 else if (is(w, n, "quiet"))
191 t->quiet = 1, t->warm = 0;
192 else if (is(w, n, "bloom"))
193 t->arrive = CK_ARRIVE_BLOOM;
194 else if (is(w, n, "rise"))
195 t->arrive = CK_ARRIVE_RISE;
196 else if (is(w, n, "fade"))
197 t->arrive = CK_ARRIVE_FADE;
198 else if (t->kind == CK_THING_PERSON && is(w, n, "to"))
199 t->way = CK_WAY_TO;
200 else if (t->kind == CK_THING_PERSON && is(w, n, "from"))
201 t->way = CK_WAY_FROM;
202 else
203 return 0;
204 return 1;
205}
206
207static void thing(struct parse *p, unsigned kind, const char *a, unsigned n)
208{
209 ck_scene *s = p->s;
210 if (s->nthings >= CK_SCENE_THINGS) {
211 p->st.over++;
212 return;
213 }
214 ck_scene_thing t = {0};
215 t.kind = (uint8_t)kind;
216 /* the head: the words before the first colon, when they are only words */
217 unsigned colon = n;
218 for (unsigned i = 0; i < n; i++)
219 if (a[i] == ':') {
220 colon = i;
221 break;
222 }
223 int head = colon < n && colon <= 40;
224 for (unsigned i = 0; head && i < colon; i++)
225 if (!alpha(a[i]) && a[i] != ' ' && a[i] != '-')
226 head = 0;
227 unsigned i = 0;
228 if (head) {
229 while (i < colon) {
230 while (i < colon && a[i] == ' ')
231 i++;
232 unsigned w = i;
233 while (i < colon && a[i] != ' ')
234 i++;
235 if (i > w && !modifier(&t, a + w, i - w))
236 p->st.unknown++;
237 }
238 i = colon + 1;
239 } else { /* no colon after the words: take the known words at the front */
240 for (;;) {
241 while (i < n && a[i] == ' ')
242 i++;
243 unsigned w = i;
244 while (i < n && alpha(a[i]))
245 i++;
246 if (i == w || (i < n && a[i] != ' ' && a[i] != ':') || !modifier(&t, a + w, i - w)) {
247 i = w;
248 break;
249 }
250 }
251 if (i < n && a[i] == ':')
252 i++;
253 }
254 /* the fields */
255 unsigned nf = 0, start = i;
256 for (unsigned j = i; j <= n; j++) {
257 if (j < n && a[j] != '|')
258 continue;
259 unsigned fs = start, fe = j;
260 while (fs < fe && a[fs] == ' ')
261 fs++;
262 start = j + 1;
263 if (fs == fe) /* an empty field (two bars together, a bar at the end) */
264 continue;
265 if (nf >= CK_SCENE_FIELDS) {
266 p->st.cut++;
267 break;
268 }
269 int at = keep(p, a + fs, fe - fs, room(kind, nf));
270 if (at < 0) {
271 p->st.cut++;
272 break;
273 }
274 t.f[nf++] = (uint16_t)at;
275 }
276 t.nf = (uint8_t)nf;
277 if (nf < needs(kind)) {
278 p->st.rejected++;
279 return;
280 }
281 if (kind == CK_THING_NUMBER) { /* a number shows a number */
282 const char *v = s->text + t.f[0];
283 int d = 0;
284 for (; *v; v++)
285 d |= digit(*v);
286 if (!d) {
287 p->st.rejected++;
288 return;
289 }
290 }
291 s->thing[s->nthings++] = t;
292 p->st.kept++;
293}
294
295int ck_scene_parse(const char *in, size_t len, ck_scene *s, ck_scene_stats *st)
296{
297 struct parse p = {s, {0}};
298 if (!s)
299 return 0;
300 for (unsigned i = 0; i < sizeof *s; i++)
301 ((char *)s)[i] = 0;
302 if (!in)
303 len = 0;
304 size_t n = 0;
305 while (n < len && n < CK_SCENE_BYTES && in[n])
306 n++;
307 if (n == CK_SCENE_BYTES && n < len && in[n])
308 p.st.truncated = 1;
309 char ln[LINE_MAX_BYTES + 1];
310 char plain[SAY_ROOM + 1]; /* the first line nobody understood: the say if nothing else is */
311 unsigned plain_n = 0;
312 int thinking = 0, had_mood = 0;
313 for (size_t i = 0; i < n;) {
314 size_t e = i;
315 while (e < n && in[e] != '\n')
316 e++;
317 int whole = e < n || !p.st.truncated; /* a line the byte limit cut through is dropped */
318 /* clean: printable ASCII, tabs as spaces, runs of spaces folded, ends trimmed */
319 unsigned m = 0, dropped = 0;
320 for (size_t j = i; j < e; j++) {
321 unsigned char c = (unsigned char)in[j];
322 if (c == '\t')
323 c = ' ';
324 if (c < 32 || c > 126) {
325 dropped += c != '\r';
326 continue;
327 }
328 if (c == ' ' && (m == 0 || ln[m - 1] == ' '))
329 continue;
330 ln[m++] = (char)c;
331 }
332 while (m && ln[m - 1] == ' ')
333 m--;
334 ln[m] = 0;
335 i = e + 1;
336 if (!whole) {
337 p.st.over++;
338 break;
339 }
340 /* the model's private thinking, if it shows any, is not a scene */
341 if (m >= 7 && is(ln, 7, "<think>"))
342 thinking = 1;
343 if (thinking) {
344 for (unsigned j = 0; j + 8 <= m; j++)
345 if (is(ln + j, 8, "</think>"))
346 thinking = 0;
347 continue;
348 }
349 if (!m)
350 continue;
351 if (m >= 3 && ln[0] == '`' && ln[1] == '`' && ln[2] == '`') { /* a code fence round the scene */
352 p.st.tidied++;
353 continue;
354 }
355 if (++p.st.lines > CK_SCENE_LINES) {
356 p.st.over++;
357 continue;
358 }
359 p.st.cut += dropped ? 1u : 0u;
360 /* a list mark, a number or bold marks before the word: taken off, counted */
361 unsigned a = 0;
362 int tidy = 0;
363 for (;;) {
364 if (a < m && (ln[a] == '-' || ln[a] == '*' || ln[a] == '#' || ln[a] == '>' || ln[a] == '"') &&
365 (a + 1 == m || ln[a + 1] == ' ' || ln[a + 1] == '*' || ln[a + 1] == '#' || alpha(ln[a + 1]))) {
366 a++, tidy = 1;
367 } else if (a < m && digit(ln[a])) {
368 unsigned b = a;
369 while (b < m && digit(ln[b]))
370 b++;
371 if (b < m && (ln[b] == '.' || ln[b] == ')') && b + 1 < m && ln[b + 1] == ' ')
372 a = b + 1, tidy = 1;
373 else
374 break;
375 } else
376 break;
377 while (a < m && ln[a] == ' ')
378 a++;
379 }
380 unsigned w = a;
381 while (w < m && alpha(ln[w]))
382 w++;
383 unsigned wn = w - a;
384 unsigned r = w; /* the rest of the line after the first word, past bold marks and a quote */
385 while (r < m && (ln[r] == '*' || ln[r] == '"'))
386 r++, tidy = 1;
387 int near = 0;
388 unsigned kind = wn ? kind_of(ln + a, wn, &near) : 0;
389 int understood = 1;
390 if (wn && is(ln + a, wn, "say") && (r == m || ln[r] == ' ' || ln[r] == ':')) {
391 if (p.s->has_say)
392 p.st.over++;
393 else {
394 say(&p, ln + r, m - r);
395 p.st.kept++;
396 }
397 } else if (wn && is(ln + a, wn, "mood") && (r == m || ln[r] == ' ' || ln[r] == ':')) {
398 if (had_mood)
399 p.st.over++;
400 else {
401 mood(&p, ln + r, m - r);
402 had_mood = 1;
403 p.st.kept++;
404 }
405 } else if (kind && (r == m || ln[r] == ' ' || ln[r] == ':')) {
406 unsigned before = p.st.rejected;
407 thing(&p, kind, ln + r, m - r);
408 if (p.st.rejected != before)
409 understood = 0;
410 tidy |= near;
411 } else {
412 p.st.rejected++;
413 understood = 0;
414 }
415 if (understood && tidy)
416 p.st.tidied++;
417 if (!understood && !plain_n) {
418 unsigned k = m - a < SAY_ROOM ? m - a : SAY_ROOM;
419 for (unsigned j = 0; j < k; j++)
420 plain[j] = ln[a + j];
421 plain_n = k;
422 }
423 }
424 if (!p.s->has_say && !p.s->nthings && !had_mood && plain_n)
425 say(&p, plain, plain_n); /* plain words: Allen still says them */
426 if (st)
427 *st = p.st;
428 return p.s->has_say || p.s->nthings || had_mood;
429}
430
431const char *ck_scene_field(const ck_scene *s, const ck_scene_thing *t, unsigned i)
432{
433 if (!s || !t || i >= t->nf || i >= CK_SCENE_FIELDS || t->f[i] >= CK_SCENE_TEXT)
434 return "";
435 return s->text + t->f[i];
436}
437
438const char *ck_scene_say(const ck_scene *s) { return s && s->has_say && s->say < CK_SCENE_TEXT ? s->text + s->say : ""; }
439
440int ck_scene_clean(const ck_scene_stats *st, const ck_scene *s)
441{
442 return st && s && !st->rejected && !st->tidied && !st->over && !st->truncated && s->has_say;
443}
444
445uint32_t ck_scene_duration_ms(const char *w)
446{
447 if (!w)
448 return 0;
449 uint64_t total = 0;
450 int parts = 0;
451 const char *p = w;
452 for (;;) {
453 while (*p == ' ' || *p == ',')
454 p++;
455 if (!*p)
456 break;
457 const char *word = p;
458 while (alpha(*p))
459 p++;
460 if (p > word) { /* "in 25 minutes", "for 10 min", "and" */
461 unsigned n = (unsigned)(p - word);
462 if (is(word, n, "in") || is(word, n, "for") || is(word, n, "and"))
463 continue;
464 return 0;
465 }
466 if (!digit(*p))
467 return 0;
468 uint64_t v = 0, tenths = 0;
469 while (digit(*p) && v < 100000)
470 v = v * 10 + (uint64_t)(*p++ - '0');
471 if (digit(*p))
472 return 0;
473 if (*p == '.' && digit(p[1])) {
474 tenths = (uint64_t)(p[1] - '0');
475 p += 2;
476 while (digit(*p))
477 p++;
478 }
479 while (*p == ' ')
480 p++;
481 const char *u = p;
482 while (alpha(*p))
483 p++;
484 unsigned un = (unsigned)(p - u);
485 uint64_t ms;
486 static const char *const hours[] = {"h", "hr", "hrs", "hour", "hours", 0};
487 static const char *const mins[] = {"m", "min", "mins", "minute", "minutes", 0};
488 static const char *const secs[] = {"s", "sec", "secs", "second", "seconds", 0};
489 if (any_of(u, un, hours))
490 ms = 3600000;
491 else if (any_of(u, un, mins))
492 ms = 60000;
493 else if (any_of(u, un, secs))
494 ms = 1000;
495 else
496 return 0;
497 total += v * ms + tenths * ms / 10;
498 parts++;
499 if (total > 86400000ull)
500 return 0;
501 }
502 return parts ? (uint32_t)total : 0;
503}
504
505size_t ck_scene_unwire(char *b, size_t n)
506{
507 size_t o = 0;
508 for (size_t i = 0; i < n && b[i]; i++) {
509 if (b[i] == '\\' && i + 1 < n && b[i + 1] == 'n')
510 b[o++] = '\n', i++;
511 else if (b[i] == '\\' && i + 1 < n && b[i + 1] == '\\')
512 b[o++] = '\\', i++;
513 else
514 b[o++] = b[i];
515 }
516 if (o < n)
517 b[o] = 0;
518 return o;
519}