interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / visor / visor_parse_command.c

188 lines · 6978 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/* visor_parse_command.c -- Omega Visor console line parser (lane 2). No semantics. */
18#include "visor_parse_command.h"
19#include "omega_native.h"
20
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24
25typedef struct {
26 const char *word;
27 VisorCmd cmd;
28 size_t min_args, max_args;
29} ParseEntry;
30
31static const ParseEntry k_words[] = {
32 { "help", VISOR_CMD_HELP, 0, 1 },
33 { "quit", VISOR_CMD_QUIT, 0, 0 },
34 { "inspect", VISOR_CMD_INSPECT, 1, 1 },
35 { "type", VISOR_CMD_TYPE, 1, 1 },
36 { "id", VISOR_CMD_ID, 1, 1 },
37 { "graph", VISOR_CMD_GRAPH, 1, 1 },
38 { "verify", VISOR_CMD_VERIFY, 1, 1 },
39 { "machine", VISOR_CMD_MACHINE, 0, 0 },
40 { "realize", VISOR_CMD_REALIZE, 1, 1 },
41 { "cost", VISOR_CMD_COST, 1, 1 },
42 { "run", VISOR_CMD_RUN, 1, 4 },
43 { "evidence", VISOR_CMD_EVIDENCE, 0, 1 },
44 { "bindings", VISOR_CMD_BINDINGS, 0, 0 },
45 { "clear", VISOR_CMD_CLEAR, 0, 0 },
46 { "world", VISOR_CMD_WORLD, 0, 1 },
47 { "effects", VISOR_CMD_EFFECTS, 0, 1 },
48 { "alternatives", VISOR_CMD_ALTERNATIVES, 1, 1 },
49 { "compare", VISOR_CMD_COMPARE, 2, 2 },
50 { "why", VISOR_CMD_WHY, 1, 1 },
51};
52#define K_WORDS_N (sizeof(k_words) / sizeof(k_words[0]))
53
54const char *visor_cmd_name(VisorCmd c) {
55 for (size_t i = 0; i < K_WORDS_N; i++) if (k_words[i].cmd == c) return k_words[i].word;
56 switch (c) {
57 case VISOR_CMD_SOURCE: return "source";
58 case VISOR_CMD_EMPTY: return "empty";
59 default: return "unknown";
60 }
61}
62
63int visor_cmd_arity(VisorCmd c, size_t *min_args, size_t *max_args) {
64 uint64_t dst[2] = { 0, 0 }, ok = 0;
65 (void)omega2_visor_om_cmd_arity(&ok, (uint64_t)c, dst, 2);
66 if (!ok) return -1;
67 if (min_args) *min_args = (size_t)dst[0];
68 if (max_args) *max_args = (size_t)dst[1];
69 return 0;
70}
71
72/* Validate UTF-8 (RFC 3629: no overlongs, no surrogates, <= U+10FFFF) and
73 * reject control bytes other than tab. Returns 0 ok, -1 bad (err set). */
74static int check_bytes(const unsigned char *p, size_t n, char *err, size_t errn) {
75 uint64_t dst[2] = { 0, 0 }, rc = 0;
76 if (n == 0) return 0;
77 if (omega2_visor_om_check_bytes(&rc, p, n, n, dst, 2) != OMEGA_OK) {
78 if (err && errn) snprintf(err, errn, "command validation failed");
79 return -1;
80 }
81 if (rc == 1) {
82 if (err && errn) snprintf(err, errn, "control character 0x%02X at byte %zu is not allowed",
83 (unsigned)dst[0], (size_t)dst[1]);
84 return -1;
85 }
86 if (rc != 0) {
87 if (err && errn) snprintf(err, errn, "invalid UTF-8 at byte %zu", (size_t)dst[1]);
88 return -1;
89 }
90 return 0;
91}
92
93static bool is_blank(char ch) { return ch == ' ' || ch == '\t'; }
94
95int visor_parse_command(const char *line, VisorCommand *out, char *err, size_t errn) {
96 if (err && errn) err[0] = '\0';
97 if (!out) return -1;
98 memset(out, 0, sizeof(*out));
99 out->cmd = VISOR_CMD_UNKNOWN;
100 if (!line) {
101 if (err && errn) snprintf(err, errn, "no input");
102 return -1;
103 }
104
105 size_t n = strlen(line);
106 if (n > 0 && line[n - 1] == '\n') n--;
107 if (n > 0 && line[n - 1] == '\r') n--;
108 if (n >= VISOR_LINE_MAX) {
109 if (err && errn) snprintf(err, errn, "line too long (%zu bytes, max %d)", n, VISOR_LINE_MAX - 1);
110 return -1;
111 }
112 if (check_bytes((const unsigned char *)line, n, err, errn) != 0) return -1;
113
114 size_t a = 0, b = n;
115 if (n > 0) {
116 uint64_t tr[2] = { 0, 0 }, u = 0;
117 if (omega2_visor_om_trim_blanks(&u, (const uint8_t *)line, n, n, tr, 2) != OMEGA_OK) {
118 if (err && errn) snprintf(err, errn, "command validation failed");
119 return -1;
120 }
121 a = (size_t)tr[0];
122 b = (size_t)tr[1];
123 }
124 memcpy(out->line, line + a, b - a);
125 out->line[b - a] = '\0';
126
127 if (out->line[0] == '\0' || out->line[0] == '#') { out->cmd = VISOR_CMD_EMPTY; return 0; }
128
129 /* First word. */
130 const char *s = out->line;
131 size_t wl = 0;
132 while (s[wl] && !is_blank(s[wl])) wl++;
133 const ParseEntry *e = NULL;
134 for (size_t i = 0; i < K_WORDS_N; i++) {
135 if (strlen(k_words[i].word) == wl && memcmp(k_words[i].word, s, wl) == 0) { e = &k_words[i]; break; }
136 }
137 if (!e) { out->cmd = VISOR_CMD_SOURCE; return 0; }
138 out->cmd = e->cmd;
139
140 /* Tokenize the arguments. Room for MAX_ARGS+2 so `for current.machine` fits. */
141 char toks[VISOR_MAX_ARGS + 2][VISOR_TOKEN_MAX];
142 size_t nt = 0;
143 const char *p = s + wl;
144 for (;;) {
145 while (*p && is_blank(*p)) p++;
146 if (!*p) break;
147 size_t tl = 0;
148 while (p[tl] && !is_blank(p[tl])) tl++;
149 if (tl >= VISOR_TOKEN_MAX) {
150 if (err && errn) snprintf(err, errn, "argument %zu too long (%zu bytes, max %d)",
151 nt + 1, tl, VISOR_TOKEN_MAX - 1);
152 return -1;
153 }
154 if (nt >= VISOR_MAX_ARGS + 2) {
155 if (err && errn) snprintf(err, errn, "too many arguments (max %zu)", e->max_args);
156 return -1;
157 }
158 memcpy(toks[nt], p, tl);
159 toks[nt][tl] = '\0';
160 nt++;
161 p += tl;
162 }
163
164 if (e->cmd == VISOR_CMD_REALIZE && nt >= 2 && strcmp(toks[nt - 2], "for") == 0) {
165 if (strcmp(toks[nt - 1], "current.machine") != 0) {
166 if (err && errn) snprintf(err, errn, "only `for current.machine` is supported (got `for %s`)",
167 toks[nt - 1]);
168 return -1;
169 }
170 out->for_current_machine = true;
171 nt -= 2;
172 }
173
174 if (nt < e->min_args || nt > e->max_args) {
175 if (err && errn) {
176 if (e->min_args == e->max_args)
177 snprintf(err, errn, "expected %zu argument%s, got %zu", e->min_args,
178 e->min_args == 1 ? "" : "s", nt);
179 else
180 snprintf(err, errn, "expected %zu to %zu arguments, got %zu",
181 e->min_args, e->max_args, nt);
182 }
183 return -1;
184 }
185 for (size_t i = 0; i < nt; i++) memcpy(out->args[i], toks[i], VISOR_TOKEN_MAX);
186 out->argc = nt;
187 return 0;
188}