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_console.c

511 lines · 22360 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_console.c -- Omega Visor console / REPL (lane 2). Dispatch only; no authority. */
18#include "visor_console.h"
19
20#include <stdlib.h>
21#include <string.h>
22#include <ctype.h>
23#include <unistd.h>
24
25#define VISOR_PROMPT "\xCE\xA9> "
26#define VISOR_EFFECT_REQUEST_MSG \
27 "EFFECT REQUEST: requires authority; routed via EffectIntent -> AEGIS/PHYSICS (not executed)"
28#define VISOR_NOT_WIRED_MSG "not wired in this build"
29
30/* ---------- command table: usage, class, meaning (source of truth for help + JSON class) ---------- */
31
32typedef struct {
33 VisorCmd cmd;
34 const char *usage;
35 VisorClass cls;
36 const char *meaning;
37} HelpEntry;
38
39static const HelpEntry k_help[] = {
40 { VISOR_CMD_HELP, "help [command]", VISOR_CLASS_INSPECTION, "list commands, or show one" },
41 { VISOR_CMD_QUIT, "quit", VISOR_CLASS_INSPECTION, "leave the console" },
42 { VISOR_CMD_INSPECT, "inspect <x>", VISOR_CLASS_INSPECTION, "show the object a name or id refers to" },
43 { VISOR_CMD_TYPE, "type <x>", VISOR_CLASS_INSPECTION, "show the type of an object" },
44 { VISOR_CMD_ID, "id <x>", VISOR_CLASS_INSPECTION, "show the semantic id of a name, _ or hex id" },
45 { VISOR_CMD_GRAPH, "graph <x>", VISOR_CLASS_INSPECTION, "show the semantic graph reachable from x" },
46 { VISOR_CMD_VERIFY, "verify <x>", VISOR_CLASS_PURE_EXECUTION, "run the verification pipeline on x" },
47 { VISOR_CMD_MACHINE, "machine", VISOR_CLASS_INSPECTION, "show the current machine model" },
48 { VISOR_CMD_REALIZE, "realize <x> [for current.machine]", VISOR_CLASS_PURE_EXECUTION, "build machine code for x (does not run it)" },
49 { VISOR_CMD_COST, "cost <x>", VISOR_CLASS_SIMULATION, "estimate the cost of x" },
50 { VISOR_CMD_RUN, "run <x> [args...]", VISOR_CLASS_PURE_EXECUTION, "run x if pure; effects become an effect request" },
51 { VISOR_CMD_EVIDENCE, "evidence [name]", VISOR_CLASS_INSPECTION, "show recorded evidence receipts" },
52 { VISOR_CMD_BINDINGS, "bindings", VISOR_CLASS_INSPECTION, "list session names in the order they were made" },
53 { VISOR_CMD_CLEAR, "clear", VISOR_CLASS_INSPECTION, "forget all session names and objects" },
54 { VISOR_CMD_WORLD, "world [cap]", VISOR_CLASS_INSPECTION, "show the runtime world state (read only)" },
55 { VISOR_CMD_EFFECTS, "effects [x]", VISOR_CLASS_EFFECT_REQUEST, "show effect objects as requests (never executed)" },
56 { VISOR_CMD_ALTERNATIVES, "alternatives <x>", VISOR_CLASS_SIMULATION, "list other ways to realize x" },
57 { VISOR_CMD_COMPARE, "compare <a> <b>", VISOR_CLASS_SIMULATION, "compare two objects or realizations" },
58 { VISOR_CMD_WHY, "why <x>", VISOR_CLASS_INSPECTION, "explain where x came from" },
59 { VISOR_CMD_SOURCE, "let x: u64 = <expr> | fn .. | <expr>", VISOR_CLASS_PURE_EXECUTION, "Omega source line, evaluated by the language" },
60};
61#define K_HELP_N (sizeof(k_help) / sizeof(k_help[0]))
62
63static const HelpEntry *help_for(VisorCmd cmd) {
64 for (size_t i = 0; i < K_HELP_N; i++) if (k_help[i].cmd == cmd) return &k_help[i];
65 return NULL;
66}
67
68static VisorClass class_for(VisorCmd cmd) {
69 const HelpEntry *h = help_for(cmd);
70 return h ? h->cls : VISOR_CLASS_INSPECTION;
71}
72
73const char *visor_class_name(VisorClass c) {
74 switch (c) {
75 case VISOR_CLASS_INSPECTION: return "inspection";
76 case VISOR_CLASS_PURE_EXECUTION: return "pure-execution";
77 case VISOR_CLASS_SIMULATION: return "simulation";
78 case VISOR_CLASS_EFFECT_REQUEST: return "effect-request";
79 }
80 return "inspection";
81}
82
83static const char *bind_kind_name(VisorBindKind k) {
84 switch (k) {
85 case VISOR_BIND_OBJECT: return "object";
86 case VISOR_BIND_PROGRAM: return "program";
87 case VISOR_BIND_REALIZATION: return "realization";
88 default: return "none";
89 }
90}
91
92/* ---------- JSON ---------- */
93
94void visor_json_string(FILE *out, const char *s) {
95 fputc('"', out);
96 for (const unsigned char *p = (const unsigned char *)(s ? s : ""); *p; p++) {
97 switch (*p) {
98 case '"': fputs("\\\"", out); break;
99 case '\\': fputs("\\\\", out); break;
100 case '\n': fputs("\\n", out); break;
101 case '\r': fputs("\\r", out); break;
102 case '\t': fputs("\\t", out); break;
103 default:
104 if (*p < 0x20 || *p == 0x7F) fprintf(out, "\\u%04x", *p);
105 else fputc(*p, out);
106 }
107 }
108 fputc('"', out);
109}
110
111/* ---------- output ---------- */
112
113/* body: in JSON mode a JSON value (NULL/empty -> null); in human mode text. */
114static int emit_ok_tag(VisorConsole *c, const char *name, VisorClass cls, const char *body, bool tag) {
115 if (c->json) {
116 fprintf(c->out, "{\"command\":");
117 visor_json_string(c->out, name);
118 fprintf(c->out, ",\"status\":\"ok\",\"class\":\"%s\",\"result\":%s,\"error\":null}\n",
119 visor_class_name(cls), (body && *body) ? body : "null");
120 } else {
121 if (tag && cls != VISOR_CLASS_INSPECTION) fprintf(c->out, "[%s]\n", visor_class_name(cls));
122 if (body && *body) {
123 fputs(body, c->out);
124 if (body[strlen(body) - 1] != '\n') fputc('\n', c->out);
125 }
126 }
127 c->last_status = 0;
128 return 0;
129}
130
131static int emit_ok(VisorConsole *c, const char *name, VisorClass cls, const char *body) {
132 return emit_ok_tag(c, name, cls, body, true);
133}
134
135/* Error; `body` (may be NULL) is a report printed despite the error
136 * (human: on out before the error line; JSON: as `result`). */
137static int emit_err_body(VisorConsole *c, const char *name, VisorClass cls, const char *msg, const char *body) {
138 if (!msg || !*msg) msg = "failed";
139 bool has_body = body && *body;
140 if (!c->json && has_body) {
141 fputs(body, c->out);
142 if (body[strlen(body) - 1] != '\n') fputc('\n', c->out);
143 }
144 fflush(c->out); /* keep out/err ordered when both reach the same terminal or file */
145 if (c->json) {
146 fprintf(c->out, "{\"command\":");
147 visor_json_string(c->out, name);
148 fprintf(c->out, ",\"status\":\"error\",\"class\":\"%s\",\"result\":%s,\"error\":", visor_class_name(cls),
149 has_body ? body : "null");
150 visor_json_string(c->out, msg);
151 fputs("}\n", c->out);
152 } else if (strncmp(msg, "EFFECT REQUEST", 14) == 0) {
153 fprintf(c->err, "%s\n", msg);
154 } else {
155 fprintf(c->err, "error: %s: %s\n", name, msg);
156 }
157 c->last_status = 1;
158 c->error_count++;
159 return 1;
160}
161
162static int emit_err(VisorConsole *c, const char *name, VisorClass cls, const char *msg) {
163 return emit_err_body(c, name, cls, msg, NULL);
164}
165
166/* A growable text buffer backed by open_memstream. */
167typedef struct { char *buf; size_t len; FILE *f; } MemOut;
168
169static FILE *mem_open(MemOut *m) {
170 m->buf = NULL; m->len = 0;
171 m->f = open_memstream(&m->buf, &m->len);
172 return m->f;
173}
174static void mem_close(MemOut *m) { if (m->f) { fclose(m->f); m->f = NULL; } }
175static void mem_free(MemOut *m) { mem_close(m); free(m->buf); m->buf = NULL; }
176
177/* ---------- built-in commands ---------- */
178
179static void help_row_human(FILE *f, const HelpEntry *h) {
180 fprintf(f, " %-36s %-15s %s\n", h->usage, visor_class_name(h->cls), h->meaning);
181}
182static void help_row_json(FILE *f, const HelpEntry *h) {
183 fputs("{\"command\":", f); visor_json_string(f, visor_cmd_name(h->cmd));
184 fputs(",\"usage\":", f); visor_json_string(f, h->usage);
185 fprintf(f, ",\"class\":\"%s\",\"meaning\":", visor_class_name(h->cls));
186 visor_json_string(f, h->meaning);
187 fputc('}', f);
188}
189
190static int cmd_help(VisorConsole *c, const VisorCommand *cmd) {
191 const char *name = "help";
192 const HelpEntry *one = NULL;
193 if (cmd->argc == 1) {
194 for (size_t i = 0; i < K_HELP_N; i++) {
195 if (strcmp(visor_cmd_name(k_help[i].cmd), cmd->args[0]) == 0) { one = &k_help[i]; break; }
196 }
197 if (!one) {
198 char msg[320];
199 snprintf(msg, sizeof(msg), "no such command '%s'", cmd->args[0]);
200 return emit_err(c, name, VISOR_CLASS_INSPECTION, msg);
201 }
202 }
203 MemOut m;
204 if (!mem_open(&m)) return emit_err(c, name, VISOR_CLASS_INSPECTION, "out of memory");
205 if (c->json) {
206 fputc('[', m.f);
207 for (size_t i = 0, k = 0; i < K_HELP_N; i++) {
208 if (one && &k_help[i] != one) continue;
209 if (k++) fputc(',', m.f);
210 help_row_json(m.f, &k_help[i]);
211 }
212 fputc(']', m.f);
213 } else if (one) {
214 help_row_human(m.f, one);
215 } else {
216 fputs("Omega Visor commands (classes: inspection, pure-execution, simulation, effect-request):\n", m.f);
217 for (size_t i = 0; i < K_HELP_N; i++) help_row_human(m.f, &k_help[i]);
218 fputs("Names: a session name, _ (last result), or a semantic id (64 hex, optional sha256: prefix).\n", m.f);
219 }
220 mem_close(&m);
221 int rc = emit_ok(c, name, VISOR_CLASS_INSPECTION, m.buf);
222 mem_free(&m);
223 return rc;
224}
225
226static void binding_row(VisorConsole *c, FILE *f, const VisorBinding *b, bool first) {
227 char id[72];
228 visor_format_id(&b->id, id);
229 if (c->json) {
230 if (!first) fputc(',', f);
231 fputs("{\"name\":", f); visor_json_string(f, b->name);
232 fprintf(f, ",\"kind\":\"%s\",\"id\":\"%s\"}", bind_kind_name(b->kind), id);
233 } else {
234 fprintf(f, "%s %s %s\n", b->name, bind_kind_name(b->kind), id);
235 }
236}
237
238static int cmd_bindings(VisorConsole *c) {
239 const VisorSession *s = c->session;
240 MemOut m;
241 if (!mem_open(&m)) return emit_err(c, "bindings", VISOR_CLASS_INSPECTION, "out of memory");
242 if (c->json) fputc('[', m.f);
243 size_t shown = 0;
244 for (size_t i = 0; i < s->bindings.count; i++, shown++) binding_row(c, m.f, &s->bindings.items[i], shown == 0);
245 if (s->has_last) { binding_row(c, m.f, &s->last, shown == 0); shown++; }
246 if (c->json) fputc(']', m.f);
247 else if (shown == 0) fputs("(no bindings)\n", m.f);
248 mem_close(&m);
249 int rc = emit_ok(c, "bindings", VISOR_CLASS_INSPECTION, m.buf);
250 mem_free(&m);
251 return rc;
252}
253
254static int unresolved(VisorConsole *c, const char *name, VisorClass cls, const char *tok) {
255 char msg[320];
256 snprintf(msg, sizeof(msg), "unknown name or id '%s'", tok);
257 return emit_err(c, name, cls, msg);
258}
259
260static int cmd_id(VisorConsole *c, const VisorCommand *cmd) {
261 VisorBinding b;
262 if (visor_resolve(c->session, cmd->args[0], &b) != 0) return unresolved(c, "id", VISOR_CLASS_INSPECTION, cmd->args[0]);
263 char id[72];
264 visor_format_id(&b.id, id);
265 MemOut m;
266 if (!mem_open(&m)) return emit_err(c, "id", VISOR_CLASS_INSPECTION, "out of memory");
267 if (c->json) {
268 fputs("{\"name\":", m.f); visor_json_string(m.f, cmd->args[0]);
269 fprintf(m.f, ",\"kind\":\"%s\",\"id\":\"%s\"}", bind_kind_name(b.kind), id);
270 } else {
271 fprintf(m.f, "%s\n", id);
272 }
273 mem_close(&m);
274 int rc = emit_ok(c, "id", VISOR_CLASS_INSPECTION, m.buf);
275 mem_free(&m);
276 return rc;
277}
278
279/* ---------- hook dispatch ---------- */
280
281static int call_view(VisorConsole *c, const char *name, VisorClass cls, VisorViewFn fn,
282 const VisorCommand *cmd, const VisorBinding *subj, const VisorBinding *subj2) {
283 if (!fn) return emit_err(c, name, cls, VISOR_NOT_WIRED_MSG);
284 VisorViewCall call = { c->session, cmd, subj, subj2, c->json, c->evidence_root, c->ops.ctx };
285 MemOut m;
286 if (!mem_open(&m)) return emit_err(c, name, cls, "out of memory");
287 char err[512] = {0};
288 int rc = fn(&call, m.f, err, sizeof(err));
289 mem_close(&m);
290 if (rc == 0) rc = emit_ok(c, name, cls, m.buf);
291 else if (rc == VISOR_VIEW_ERROR_WITH_OUTPUT) rc = emit_err_body(c, name, cls, err, m.buf);
292 else rc = emit_err(c, name, cls, err);
293 mem_free(&m);
294 return rc;
295}
296
297/* Resolve args[i] if present. Returns 0 ok (out filled or *have=false), -1 unresolved. */
298static int resolve_arg(VisorConsole *c, const VisorCommand *cmd, size_t i, VisorBinding *out, bool *have) {
299 *have = false;
300 if (cmd->argc <= i) return 0;
301 if (visor_resolve(c->session, cmd->args[i], out) != 0) return -1;
302 *have = true;
303 return 0;
304}
305
306static int cmd_subject_view(VisorConsole *c, const VisorCommand *cmd, VisorViewFn fn) {
307 const char *name = visor_cmd_name(cmd->cmd);
308 VisorClass cls = class_for(cmd->cmd);
309 if (!fn) return emit_err(c, name, cls, VISOR_NOT_WIRED_MSG);
310 VisorBinding a, b; bool ha, hb;
311 if (resolve_arg(c, cmd, 0, &a, &ha) != 0) return unresolved(c, name, cls, cmd->args[0]);
312 if (cmd->cmd == VISOR_CMD_COMPARE && resolve_arg(c, cmd, 1, &b, &hb) != 0)
313 return unresolved(c, name, cls, cmd->args[1]);
314 if (cmd->cmd != VISOR_CMD_COMPARE) hb = false;
315 return call_view(c, name, cls, fn, cmd, ha ? &a : NULL, hb ? &b : NULL);
316}
317
318/* `run`: classify FIRST; anything that is not provably pure is an effect request
319 * and is never executed. No classifier -> not wired (fail closed). */
320static int cmd_run(VisorConsole *c, const VisorCommand *cmd) {
321 const char *name = "run";
322 if (!c->ops.effects_classify) return emit_err(c, name, VISOR_CLASS_PURE_EXECUTION, VISOR_NOT_WIRED_MSG);
323 VisorBinding a; bool ha;
324 if (resolve_arg(c, cmd, 0, &a, &ha) != 0) return unresolved(c, name, VISOR_CLASS_PURE_EXECUTION, cmd->args[0]);
325 VisorViewCall call = { c->session, cmd, &a, NULL, c->json, c->evidence_root, c->ops.ctx };
326 VisorClass k = VISOR_CLASS_EFFECT_REQUEST;
327 char reason[256] = {0}, err[512] = {0};
328 if (c->ops.effects_classify(&call, &k, reason, sizeof(reason), err, sizeof(err)) != 0)
329 return emit_err(c, name, VISOR_CLASS_PURE_EXECUTION, err);
330 if (k != VISOR_CLASS_PURE_EXECUTION) {
331 char msg[512];
332 if (reason[0]) snprintf(msg, sizeof(msg), "%s; reason: %s", VISOR_EFFECT_REQUEST_MSG, reason);
333 else snprintf(msg, sizeof(msg), "%s", VISOR_EFFECT_REQUEST_MSG);
334 return emit_err(c, name, VISOR_CLASS_EFFECT_REQUEST, msg);
335 }
336 return call_view(c, name, VISOR_CLASS_PURE_EXECUTION, c->ops.run_pure, cmd, &a, NULL);
337}
338
339/* A failed source line whose shape is "<word> <word-or-_>..." (e.g. `authorize x`,
340 * `execute _`) was most likely meant as a command: report it as unknown.
341 * `let`/`fn` lines and expressions keep the language's own error. */
342static bool looks_like_command(const char *line, char *word, size_t n) {
343 size_t i = 0;
344 if (!(isalpha((unsigned char)line[0]) || line[0] == '_')) return false;
345 while (isalnum((unsigned char)line[i]) || line[i] == '_') i++;
346 if (line[i] != ' ' && line[i] != '\t') return false;
347 if ((i == 3 && strncmp(line, "let", 3) == 0) || (i == 2 && strncmp(line, "fn", 2) == 0)) return false;
348 size_t j = i;
349 while (line[j] == ' ' || line[j] == '\t') j++;
350 if (!(isalnum((unsigned char)line[j]) || line[j] == '_')) return false;
351 snprintf(word, n, "%.*s", (int)i, line);
352 return true;
353}
354
355static int cmd_source(VisorConsole *c, const VisorCommand *cmd) {
356 const char *name = "source";
357 VisorClass cls = VISOR_CLASS_PURE_EXECUTION;
358 if (!c->ops.eval_line) return emit_err(c, name, cls, VISOR_NOT_WIRED_MSG);
359 VisorBinding b;
360 memset(&b, 0, sizeof(b));
361 b.kind = VISOR_BIND_NONE;
362 b.index = -1;
363 char value[1024] = {0}, err[512] = {0};
364 if (c->ops.eval_line(c->session, cmd->line, &b, value, sizeof(value), err, sizeof(err)) != 0) {
365 char word[VISOR_TOKEN_MAX];
366 if (looks_like_command(cmd->line, word, sizeof(word))) {
367 char msg[VISOR_TOKEN_MAX + 64];
368 snprintf(msg, sizeof(msg), "unknown command or invalid source line: '%s'", word);
369 return emit_err(c, "unknown", cls, msg);
370 }
371 return emit_err(c, name, cls, err);
372 }
373 value[sizeof(value) - 1] = '\0';
374 bool has = b.kind != VISOR_BIND_NONE;
375 if (has) visor_set_last(c->session, &b);
376 char id[72] = {0};
377 if (has) visor_format_id(&b.id, id);
378 MemOut m;
379 if (!mem_open(&m)) return emit_err(c, name, cls, "out of memory");
380 if (c->json) {
381 fputs("{\"value\":", m.f); visor_json_string(m.f, value);
382 fprintf(m.f, ",\"kind\":\"%s\",\"id\":", bind_kind_name(b.kind));
383 if (has) fprintf(m.f, "\"%s\"}", id); else fputs("null}", m.f);
384 } else if (value[0]) {
385 fprintf(m.f, "%s\n", value);
386 } else if (has) {
387 fprintf(m.f, "%s\n", id);
388 }
389 mem_close(&m);
390 int rc = emit_ok_tag(c, name, cls, m.buf, false) /* source: value line only */;
391 mem_free(&m);
392 return rc;
393}
394
395/* ---------- public API ---------- */
396
397int visor_console_init(VisorConsole *c, VisorSession *s, FILE *out, FILE *err, bool json) {
398 if (!c || !s || !out || !err) return -1;
399 memset(c, 0, sizeof(*c));
400 c->session = s;
401 c->out = out;
402 c->err = err;
403 c->json = json;
404 return 0;
405}
406
407void visor_console_set_ops(VisorConsole *c, const VisorConsoleOps *ops) {
408 if (!c) return;
409 if (ops) c->ops = *ops; else memset(&c->ops, 0, sizeof(c->ops));
410}
411
412static int dispatch(VisorConsole *c, const VisorCommand *cmd) {
413 const VisorConsoleOps *o = &c->ops;
414 switch (cmd->cmd) {
415 case VISOR_CMD_EMPTY: c->last_status = 0; return 0;
416 case VISOR_CMD_HELP: return cmd_help(c, cmd);
417 case VISOR_CMD_QUIT:
418 c->quit = true;
419 return emit_ok(c, "quit", VISOR_CLASS_INSPECTION, c->json ? "\"bye\"" : "bye");
420 case VISOR_CMD_BINDINGS: return cmd_bindings(c);
421 case VISOR_CMD_CLEAR:
422 visor_session_clear(c->session);
423 return emit_ok(c, "clear", VISOR_CLASS_INSPECTION, c->json ? "\"session cleared\"" : "session cleared");
424 case VISOR_CMD_ID: return o->id_of ? cmd_subject_view(c, cmd, o->id_of) : cmd_id(c, cmd);
425 case VISOR_CMD_INSPECT: return cmd_subject_view(c, cmd, o->inspect);
426 case VISOR_CMD_TYPE: return cmd_subject_view(c, cmd, o->type_of);
427 case VISOR_CMD_GRAPH: return cmd_subject_view(c, cmd, o->graph_text);
428 case VISOR_CMD_VERIFY: return cmd_subject_view(c, cmd, o->verify);
429 case VISOR_CMD_REALIZE: return cmd_subject_view(c, cmd, o->realize);
430 case VISOR_CMD_COST: return cmd_subject_view(c, cmd, o->cost);
431 case VISOR_CMD_ALTERNATIVES: return cmd_subject_view(c, cmd, o->alternatives);
432 case VISOR_CMD_COMPARE: return cmd_subject_view(c, cmd, o->compare);
433 case VISOR_CMD_WHY: return cmd_subject_view(c, cmd, o->why);
434 case VISOR_CMD_MACHINE:
435 return call_view(c, "machine", VISOR_CLASS_INSPECTION, o->machine, cmd, NULL, NULL);
436 case VISOR_CMD_EVIDENCE: /* raw name, not resolved */
437 return call_view(c, "evidence", VISOR_CLASS_INSPECTION, o->evidence, cmd, NULL, NULL);
438 case VISOR_CMD_WORLD: /* raw cap name, not resolved */
439 return call_view(c, "world", VISOR_CLASS_INSPECTION, o->world, cmd, NULL, NULL);
440 case VISOR_CMD_EFFECTS: return cmd_subject_view(c, cmd, o->effects);
441 case VISOR_CMD_RUN: return cmd_run(c, cmd);
442 case VISOR_CMD_SOURCE: return cmd_source(c, cmd);
443 default: return emit_err(c, "unknown", VISOR_CLASS_INSPECTION, "unknown command");
444 }
445}
446
447int visor_console_exec_line(VisorConsole *c, const char *line) {
448 if (!c) return 1;
449 VisorCommand *cmd = calloc(1, sizeof(*cmd));
450 if (!cmd) {
451 fprintf(c->err, "error: out of memory\n");
452 c->last_status = 1; c->error_count++;
453 return 1;
454 }
455 char err[512];
456 int rc;
457 if (visor_parse_command(line, cmd, err, sizeof(err)) != 0)
458 rc = emit_err(c, visor_cmd_name(cmd->cmd), class_for(cmd->cmd), err);
459 else
460 rc = dispatch(c, cmd);
461 free(cmd);
462 fflush(c->out);
463 fflush(c->err);
464 return rc;
465}
466
467static bool line_is_empty(const char *line) {
468 const char *p = line;
469 while (*p == ' ' || *p == '\t') p++;
470 return *p == '\0' || *p == '\n' || *p == '\r' || *p == '#';
471}
472
473static int run_loop(VisorConsole *c, FILE *in, bool echo, bool prompt) {
474 char *line = NULL;
475 size_t cap = 0;
476 ssize_t n;
477 int any_err = 0;
478 for (;;) {
479 if (prompt) { fputs(VISOR_PROMPT, c->out); fflush(c->out); }
480 n = getline(&line, &cap, in);
481 if (n < 0) { if (prompt) fputc('\n', c->out); break; }
482 /* getline keeps embedded NULs; the parser would see a shorter line. Reject. */
483 if ((size_t)n != strlen(line)) {
484 if (echo) fputs(VISOR_PROMPT "<line with NUL byte>\n", c->out);
485 emit_err(c, "unknown", VISOR_CLASS_INSPECTION, "NUL byte in line is not allowed");
486 any_err = 1;
487 continue;
488 }
489 if (line_is_empty(line)) continue;
490 if (echo) {
491 size_t l = strlen(line);
492 while (l > 0 && (line[l - 1] == '\n' || line[l - 1] == '\r')) l--;
493 fprintf(c->out, VISOR_PROMPT "%.*s\n", (int)l, line);
494 }
495 if (visor_console_exec_line(c, line) != 0) any_err = 1;
496 if (c->quit) break;
497 }
498 free(line);
499 fflush(c->out);
500 return any_err;
501}
502
503int visor_console_run_script(VisorConsole *c, FILE *script) {
504 if (!c || !script) return 1;
505 return run_loop(c, script, !c->json, false);
506}
507
508int visor_console_repl(VisorConsole *c, FILE *in) {
509 if (!c || !in) return 1;
510 return run_loop(c, in, false, !c->json && isatty(fileno(in)));
511}