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 / tools / omega.om

203 lines · 5135 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Pure seeded OM-2 Omega Visor V1 terminal console (replaces main() of tools/omega.c).
3note omega [--json] [--command "<line>"]... [--script <file>|-] [--evidence-root <dir>]
4note No arguments: interactive REPL on stdin. Exit code: 0 if every command was ok,
5note 1 if any command errored, 2 on usage error.
6note Link: visor lanes, visor_console_hooks.om, tools/omega_ops.om, tools/omega_wire.om,
7note visor_console.om, this file.
8
9text omcli_f_json "--json"
10text omcli_f_cmd "--command"
11text omcli_f_scr "--script"
12text omcli_f_ev "--evidence-root"
13text omcli_f_help "--help"
14text omcli_f_h "-h"
15text omcli_usage "usage: omega [--json] [--command \"<line>\"]... [--script <file>|-] [--evidence-root <dir>]\n"
16text omcli_m_pfx "omega: "
17text omcli_m_needs " needs a value\n"
18text omcli_m_one "omega: only one --script allowed\n"
19text omcli_m_unk1 "omega: unknown argument '"
20text omcli_m_unk2 "'\n"
21text omcli_banner "Omega Visor V1 -- type `help` for commands, `quit` to leave.\n"
22text omcli_fd0 "/proc/self/fd/0"
23
24global omcli_arg uchar 65536
25global omcli_cmdi ulong 4096
26global omcli_ccnt ulong 1
27global omcli_st uchar 128
28global omcli_sink long 1
29
30fn omcli_arg_eq n ulong s view uchar * to bool
31 sl = len s
32 if ne n sl
33 ret false
34 end
35 for i ulong sl bound 64
36 if ne get omcli_arg i get s i
37 ret false
38 end
39 end
40 ret true
41end
42
43fn omcli_usage_err fd int to int
44 v_write_all fd omcli_usage
45 ret 2
46end
47
48note "omega: <flag> needs a value" + usage, exit 2.
49fn omcli_needs n ulong to int
50 v_write_all 2 omcli_m_pfx
51 put omcli_sink 0 long write 2 omcli_arg 0 n
52 v_write_all 2 omcli_m_needs
53 ret omcli_usage_err 2
54end
55
56fn omcli_unknown n ulong to int
57 v_write_all 2 omcli_m_unk1
58 put omcli_sink 0 long write 2 omcli_arg 0 n
59 v_write_all 2 omcli_m_unk2
60 ret omcli_usage_err 2
61end
62
63note isatty(0): stdin is a tty character device (majors 4, 5, 136..143).
64fn omcli_stdin_tty to bool
65 if lt stat omcli_fd0 0 len omcli_fd0 omcli_st 0 128 0
66 ret false
67 end
68 mode = or ulong get omcli_st 16 shl ulong get omcli_st 17 8
69 if ne and mode 61440 8192
70 ret false
71 end
72 rdev = or ulong get omcli_st 32 shl ulong get omcli_st 33 8
73 major = and shr rdev 8 4095
74 ret or or eq major 4 eq major 5 and ge major 136 le major 143
75end
76
77note Run --command line number ci (argv index in omcli_cmdi).
78fn omcli_run_cmd ci ulong to long
79 ai = get omcli_cmdi ci
80 r = argread ai vpc_in 0 65536
81 if lt r 0
82 ve_reset
83 ve_puts vcon_m_line
84 ve_put_u64 65536
85 ve_puts vcon_m_line2
86 u = vcon_emit_err 21 VCON_CL_INSP false
87 vk_flush
88 ret u
89 end
90 ret vcon_exec_in ulong r
91end
92
93fn main to int
94 ac_r = argcnt
95 if lt ac_r 1
96 ret 2
97 end
98 ac = ulong ac_r
99 isj = ulong 0
100 scr = long -1
101 put omcli_ccnt 0 0
102 put vcon_has_evroot 0 0
103 i = ulong 1
104 while lt i ac bound 65536
105 ar = argread i omcli_arg 0 65536
106 an = ulong 0
107 if gt ar 0
108 an = ulong ar
109 end
110 if omcli_arg_eq an omcli_f_json
111 isj = 1
112 else
113 vflag = or or omcli_arg_eq an omcli_f_cmd omcli_arg_eq an omcli_f_scr omcli_arg_eq an omcli_f_ev
114 if vflag
115 if ge add i 1 ac
116 ret omcli_needs an
117 end
118 if omcli_arg_eq an omcli_f_cmd
119 cc = get omcli_ccnt 0
120 if lt cc 4096
121 put omcli_cmdi cc add i 1
122 put omcli_ccnt 0 add cc 1
123 end
124 else
125 if omcli_arg_eq an omcli_f_scr
126 if ge scr 0
127 v_write_all 2 omcli_m_one
128 ret 2
129 end
130 scr = long add i 1
131 else
132 vr = argread add i 1 vcon_evroot 0 4096
133 if lt vr 0
134 put vcon_evlen 0 0
135 else
136 put vcon_evlen 0 ulong vr
137 end
138 put vcon_has_evroot 0 1
139 end
140 end
141 i = add i 1
142 else
143 if or omcli_arg_eq an omcli_f_help omcli_arg_eq an omcli_f_h
144 v_write_all 1 omcli_usage
145 ret 0
146 end
147 ret omcli_unknown an
148 end
149 end
150 i = add i 1
151 end
152 sfd = long -1
153 if ge scr 0
154 pr = argread ulong scr vom_path 0 4096
155 if lt pr 0
156 put vom_plen 0 0
157 else
158 put vom_plen 0 ulong pr
159 end
160 ve_reset
161 sfd = omega_tool_open_script
162 if lt sfd 0
163 for k ulong get vcon_elen 0 bound 512
164 put v_sbuf k get vcon_err k
165 end
166 put v_sbuf get vcon_elen 0 uchar 10
167 put omcli_sink 0 long write 2 v_sbuf 0 add get vcon_elen 0 1
168 ret 2
169 end
170 end
171 u0 = omega_tool_session_init
172 vcon_init isj
173 any = long u0
174 for ci ulong get omcli_ccnt 0 bound 4096
175 if ne get vcon_quit 0 0
176 break
177 end
178 if ne omcli_run_cmd ci 0
179 any = 1
180 end
181 end
182 if and ge sfd 0 eq get vcon_quit 0 0
183 if ne vcon_run_fd int sfd eq isj 0 false 0
184 any = 1
185 end
186 end
187 if and lt scr 0 eq get omcli_ccnt 0 0
188 tty = omcli_stdin_tty
189 if and eq isj 0 tty
190 v_write_all 1 omcli_banner
191 end
192 if ne vcon_run_fd 0 false and eq isj 0 tty 0
193 any = 1
194 end
195 end
196 if gt sfd 0
197 put omcli_sink 0 long fclose int sfd
198 end
199 if ne any 0
200 ret 1
201 end
202 ret 0
203end