interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / bring / src / 10base.om

298 lines · 5853 bytes Raw Blame History
1note tools/bring/src/10base.om: the interplane program's shared core (Linux and macOS alike).
2note Output, decimal numbers, byte copies and compares, little-endian fields, the packed argument
3note list, temp folder paths and the exit path. Platform words live in plat-*.om (pl_* functions).
4note Every loop that walks input carries the largest bound (2^63 - 1): each pass consumes at least
5note one byte or one record of finite input, so the bound is never what stops it.
6text tver "interplane 0.1.1\n"
7text tpfx "interplane: "
8text tnl "\n"
9global gtmp uchar 4096
10global gtmpl ulong 1
11global gtmpok uchar 1
12global gavn ulong 1
13text tavlong "a program's argument list does not fit the 4096 bytes the process start word carries"
14text ttmpf "cannot open the temp file "
15text tnames "reqh\nfiles\ndirs\nkeep\nlist.tar\nstmt\nstmt.sig\nchal\nchal.sig\nbody\nhdr\ncode\ncerr\nform\nget.tar\nmbox\ngout\n"
16
17fn wall fd int b view uchar * to bool
18n = ulong 0
19while lt n len b bound 9223372036854775807
20r = write fd b n sub len b n
21if le r 0
22ret false
23end
24n = add n ulong r
25end
26ret true
27end
28
29fn say b view uchar * to void
30x = wall 1 b
31end
32
33fn warn b view uchar * to void
34x = wall 2 b
35end
36
37note decw: the decimal digits of v at o[n..]; returns the new end
38fn decw v ulong o edit uchar * n ulong to ulong
39d = new uchar 20 0
40k = ulong 0
41x = v
42while true bound 21
43put d k uchar add 48 rem x 10
44k = add k 1
45x = div x 10
46if eq x 0
47break
48end
49end
50for i ulong k
51put o add n i get d sub sub k i 1
52end
53ret add n k
54end
55
56fn sayd v ulong to void
57d = new uchar 24 0
58k = decw v d 0
59say part d 0 k
60end
61
62fn warnd v ulong to void
63d = new uchar 24 0
64k = decw v d 0
65warn part d 0 k
66end
67
68note outp: a path or other untrusted bytes for the terminal: control bytes print as '?'
69fn sayp b view uchar * to void
70o = new uchar 4096 0
71n = ulong 0
72for i ulong len b bound 9223372036854775807
73c = get b i
74if or lt c 32 eq c 127
75c = uchar 63
76end
77put o n c
78n = add n 1
79if eq n 4096
80say part o 0 n
81n = ulong 0
82end
83end
84say part o 0 n
85end
86
87note cpy: copy s into d at n; returns the new end (d too short traps 3: callers size d first)
88fn cpy d edit uchar * n ulong s view uchar * to ulong
89for i ulong len s bound 9223372036854775807
90put d add n i get s i
91end
92ret add n len s
93end
94
95fn beq a view uchar * b view uchar * to bool
96if ne len a len b
97ret false
98end
99for i ulong len a bound 9223372036854775807
100if ne get a i get b i
101ret false
102end
103end
104ret true
105end
106
107note bat: b[i..] starts with t (false when b ends first)
108fn bat b view uchar * i ulong t view uchar * to bool
109if gt add i len t len b
110ret false
111end
112for k ulong len t bound 9223372036854775807
113if ne get b add i k get t k
114ret false
115end
116end
117ret true
118end
119
120note bfind: the first index >= from where t starts in b, or -1
121fn bfind b view uchar * t view uchar * from ulong to long
122if gt len t len b
123ret -1
124end
125i = from
126while le add i len t len b bound 9223372036854775807
127if bat b i t
128ret long i
129end
130i = add i 1
131end
132ret -1
133end
134
135fn lcb c uchar to uchar
136if and ge c 65 le c 90
137ret add c 32
138end
139ret c
140end
141
142note bati: as bat, ASCII case-insensitive (t is lowercase)
143fn bati b view uchar * i ulong t view uchar * to bool
144if gt add i len t len b
145ret false
146end
147for k ulong len t bound 9223372036854775807
148if ne lcb get b add i k get t k
149ret false
150end
151end
152ret true
153end
154
155fn isdig c uchar to bool
156ret and ge c 48 le c 57
157end
158
159fn isaln c uchar to bool
160a = and ge c 65 le c 90
161b = and ge c 97 le c 122
162ret or or a b isdig c
163end
164
165fn rd16 b view uchar * o ulong to ulong
166ret or ulong get b o shl ulong get b add o 1 8
167end
168
169fn rd32 b view uchar * o ulong to ulong
170lo = rd16 b o
171ret or lo shl rd16 b add o 2 16
172end
173
174fn rd64 b view uchar * o ulong to ulong
175lo = rd32 b o
176ret or lo shl rd32 b add o 4 32
177end
178
179fn wr16 b edit uchar * o ulong v ulong to void
180put b o uchar and v 255
181put b add o 1 uchar and shr v 8 255
182end
183
184fn wr64 b edit uchar * o ulong v ulong to void
185for i ulong 8
186put b add o i uchar and shr v mul i 8 255
187end
188end
189
190note pdec: a decimal number from all of s; false (o untouched) when s is empty, has a non-digit
191note or does not fit 64 bits
192fn pdec s view uchar * o edit ulong 1 to bool
193if eq len s 0
194ret false
195end
196v = ulong 0
197for i ulong len s bound 9223372036854775807
198c = get s i
199if not isdig c
200ret false
201end
202if gt v 1844674407370955160
203ret false
204end
205w = add mul v 10 ulong sub c 48
206if lt w mul v 10
207ret false
208end
209v = w
210end
211put o 0 v
212ret true
213end
214
215note ---- packed argument list (OMEGESE s10: u16 count, then u16 length + bytes per record)
216fn avinit a edit uchar * to void
217put a 0 0
218put a 1 0
219put gavn 0 2
220end
221
222fn avadd a edit uchar * s view uchar * to void
223n = get gavn 0
224if gt add add n 2 len s len a
225die tavlong
226end
227wr16 a n len s
228n = cpy a add n 2 s
229put gavn 0 n
230wr16 a 0 add rd16 a 0 1
231end
232
233note ---- the private temp folder
234note tpath: <temp folder>/<nm> into o; returns its length
235fn tpath nm view uchar * o edit uchar 4096 to ulong
236n = get gtmpl 0
237for i ulong n bound 4096
238put o i get gtmp i
239end
240put o n 47
241ret cpy o add n 1 nm
242end
243
244note topen: open the temp file nm: how 0 read, 2 write (created or emptied, mode 0600)
245fn topen nm view uchar * how ulong to int
246p = new uchar 4096 0
247k = tpath nm p
248fd = pl_open part p 0 k how 384
249if lt fd 0
250warn tpfx
251warn ttmpf
252warn nm
253warn tnl
254cleanup
255exit 1
256end
257ret int fd
258end
259
260note cleanup: remove the temp folder's known files, then the folder (best effort, never fails)
261fn cleanup to void
262if eq get gtmpok 0 0
263ret
264end
265p = new uchar 4096 0
266s = ulong 0
267for i ulong len tnames
268if eq get tnames i 10
269k = tpath part tnames s sub i s p
270x = pl_unlink part p 0 k
271s = add i 1
272end
273end
274k = get gtmpl 0
275for i ulong k bound 4096
276put p i get gtmp i
277end
278x = pl_rmdir part p 0 k
279put gtmpok 0 0
280end
281
282note die: one line on stderr, cleanup, exit 1
283fn die m view uchar * to void
284warn tpfx
285warn m
286warn tnl
287cleanup
288exit 1
289end
290
291fn die2 m view uchar * a view uchar * to void
292warn tpfx
293warn m
294warn a
295warn tnl
296cleanup
297exit 1
298end