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 / core / agent_cmd.c

145 lines · 4524 bytes Raw Blame History
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/* agent_cmd.c -- see agent_cmd.h. No kernel calls, no libc. */
18#include "agent_cmd.h"
19
20struct ck_agent_cmd_boot ck_agent_cmd_boot;
21
22struct verb {
23 const char *name;
24 int has_text; /* 1: needs text, 0: takes none */
25};
26
27/* The only verbs a card script may carry. unlock, rotate, update and shutdown are deliberately absent. */
28static const struct verb verbs[] = {
29 {"status", 0}, {"help", 0}, {"evidence", 0}, {"recall", 0}, {"facts", 0}, {"approve", 0},
30 {"deny", 0}, {"cancel", 0}, {"remember", 1}, {"prefer", 1}, {"prompt", 1}, {"task", 1},
31};
32
33static void zero(struct ck_agent_cmd *o)
34{
35 unsigned char *p = (unsigned char *)o;
36 for (size_t i = 0; i < sizeof *o; i++) p[i] = 0;
37}
38
39/* verb at line[0..vl): index in verbs[], or -1 */
40static int find_verb(const char *l, size_t vl)
41{
42#ifdef CK_AGENT_CMD_MUTANT_ANY_VERB
43 /* TEST-ONLY mutant: any word is a verb (a script could carry unlock or rotate). */
44 (void)l; (void)vl; (void)verbs;
45 return 0;
46#else
47 for (size_t k = 0; k < sizeof verbs / sizeof verbs[0]; k++) {
48 size_t i = 0;
49 while (verbs[k].name[i] && i < vl && verbs[k].name[i] == l[i]) i++;
50 if (!verbs[k].name[i] && i == vl) return (int)k;
51 }
52 return -1;
53#endif
54}
55
56int ck_agent_cmd_parse(const uint8_t *buf, size_t n, struct ck_agent_cmd *out, const char **why)
57{
58 const char *w = 0;
59 size_t i, hl = sizeof CK_AGENT_CMD_HEADER - 1, at;
60 if (out) zero(out);
61 if (!buf || !out) {
62 w = "no request";
63 goto out;
64 }
65 if (n == 0 || n > CK_AGENT_CMD_FILE_MAX) {
66 w = "file size must be 1..2048 bytes";
67 goto out;
68 }
69 for (i = 0; i < n; i++)
70 if (!(buf[i] == '\n' || (buf[i] >= 0x20 && buf[i] <= 0x7e))) {
71 w = "only printable ASCII and LF line ends are allowed";
72 goto out;
73 }
74 if (buf[n - 1] != '\n') {
75 w = "file does not end with a line feed";
76 goto out;
77 }
78 if (n < hl + 1) {
79 w = "bad header line";
80 goto out;
81 }
82 for (i = 0; i < hl; i++)
83 if (buf[i] != (uint8_t)CK_AGENT_CMD_HEADER[i]) {
84 w = "bad header line";
85 goto out;
86 }
87 if (buf[hl] != '\n') {
88 w = "bad header line";
89 goto out;
90 }
91 at = hl + 1;
92 while (at < n) {
93 size_t e = at, vl, tl;
94 int v;
95 uint32_t k = out->nlines;
96 while (buf[e] != '\n') e++;
97 if (e == at) {
98 w = "empty line";
99 goto out;
100 }
101 if (buf[at] == ' ' || buf[e - 1] == ' ') {
102 w = "line has a leading or trailing blank";
103 goto out;
104 }
105 if (k >= CK_AGENT_CMD_LINES_MAX) {
106 w = "more than 16 command lines";
107 goto out;
108 }
109 vl = 0;
110 while (at + vl < e && buf[at + vl] != ' ') vl++;
111 v = find_verb((const char *)buf + at, vl);
112 if (v < 0) {
113 w = "verb not allowed in a card script";
114 goto out;
115 }
116 tl = at + vl < e ? e - (at + vl + 1) : 0;
117#ifndef CK_AGENT_CMD_MUTANT_ANY_VERB
118 if (verbs[v].has_text) {
119 if (tl == 0 || tl > CK_AGENT_CMD_TEXT_MAX || buf[at + vl + 1] == ' ') {
120 w = "text must be 1..48 bytes after one blank";
121 goto out;
122 }
123 } else if (at + vl != e) {
124 w = "this verb takes no text";
125 goto out;
126 }
127#else
128 (void)tl;
129 if (e - at >= CK_AGENT_CMD_LINE_MAX) {
130 w = "line too long";
131 goto out;
132 }
133#endif
134 for (i = 0; i < e - at; i++) out->line[k][i] = (char)buf[at + i];
135 out->line[k][e - at] = 0;
136 out->len[k] = (uint32_t)(e - at);
137 out->nlines++;
138 at = e + 1;
139 }
140 if (out->nlines == 0) w = "no command line";
141out:
142 if (w && out) zero(out);
143 if (why) *why = w;
144 return w ? -1 : 0;
145}