-
Notifications
You must be signed in to change notification settings - Fork 96
/
OS.oz
664 lines (534 loc) · 15.3 KB
/
OS.oz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
%% Copyright © 2011, Université catholique de Louvain
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions are met:
%%
%% * Redistributions of source code must retain the above copyright notice,
%% this list of conditions and the following disclaimer.
%% * Redistributions in binary form must reproduce the above copyright notice,
%% this list of conditions and the following disclaimer in the documentation
%% and/or other materials provided with the distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
%%
%% Authors:
%% Sébastien Doeraene <sjrdoeraene@gmail.com>
%%
functor
require
Boot_OS at 'x-oz://boot/OS'
Boot_Reflection at 'x-oz://boot/Reflection'
import
BURL at 'x-oz://boot/URL'
export
% Random number generation
Rand
Srand
RandLimits
% Environment
GetEnv
PutEnv
% Directories
GetDir
GetCWD
ChDir
Tmpnam
% File I/0
Fopen
Fread
Fwrite
Fseek
Fclose
% Standard streams
Stdin
Stdout
Stderr
% Sockets
tcpAcceptorCreate: TCPAcceptorCreate
tcpAccept: TCPAccept
tcpCancelAccept: TCPCancelAccept
tcpAcceptorClose: TCPAcceptorClose
tcpConnect: TCPConnect
tcpConnectionRead: TCPConnectionRead
tcpConnectionWrite: TCPConnectionWrite
tcpConnectionShutdown: TCPConnectionShutdown
tcpConnectionClose: TCPConnectionClose
GetHostByName
UName
System
% Process management
SpawnProcess
SpawnProcessAndPipe
PipeConnectionRead
PipeConnectionWrite
PipeConnectionShutdown
PipeConnectionClose
GetPID
% Compatibility
open: CompatOpen
fileDesc: CompatFileDesc
read: CompatRead
write: CompatWrite
lSeek: CompatLSeek
close: CompatClose
acceptSelect: CompatAcceptSelect
readSelect: CompatReadSelect
writeSelect: CompatWriteSelect
deSelect: CompatDeSelect
socket: CompatSocket
bind: CompatBind
listen: CompatListen
accept: CompatAccept
connect: CompatConnect
shutDown: CompatShutDown
getSockName: CompatGetSockName
send: CompatSend
sendTo: CompatSendTo
receiveFrom: CompatReceiveFrom
exec: CompatExec
pipe: CompatPipe
kill: CompatKill
wait: CompatWait
define
% Random number generation
Rand = Boot_OS.rand
Srand = Boot_OS.srand
RandLimits = Boot_OS.randLimits
% Environment
GetEnv = Boot_OS.getEnv
PutEnv = Boot_OS.putEnv
% File I/0
fun {GetDir Path}
{Map {Boot_OS.getDir Path} AtomToString}
end
GetCWD = Boot_OS.getCWD
ChDir = Boot_OS.chDir
Tmpnam = Boot_OS.tmpnam
fun {Fopen FileName Mode}
{Boot_OS.fopen FileName Mode}
end
proc {Fread File Max ?Head Tail ?Count}
{Boot_OS.fread File Max Tail Count Head}
end
proc {Fwrite File DataV ?Count}
{Boot_OS.fwrite File DataV ?Count}
end
Fclose = Boot_OS.fclose
Fseek = Boot_OS.fseek
% Standard streams
Stdin = {Boot_OS.stdin}
Stdout = {Boot_OS.stdout}
Stderr = {Boot_OS.stderr}
% Sockets
fun {WaitResult Result}
{Wait Result}
Result
end
TCPAcceptorCreate = Boot_OS.tcpAcceptorCreate
fun {TCPAccept Acceptor}
{WaitResult {Boot_OS.tcpAccept Acceptor}}
end
TCPCancelAccept = Boot_OS.tcpCancelAccept
TCPAcceptorClose = Boot_OS.tcpAcceptorClose
fun {TCPConnect Host Service}
{WaitResult {Boot_OS.tcpConnect Host Service}}
end
proc {TCPConnectionRead Connection Count ?Head Tail ?ReadCount}
case {Boot_OS.tcpConnectionRead Connection Count Tail}
of succeeded(C H) then
Head = H
ReadCount = C
end
end
fun {TCPConnectionWrite Connection DataV}
{WaitResult {Boot_OS.tcpConnectionWrite Connection DataV}}
end
TCPConnectionShutdown = Boot_OS.tcpConnectionShutdown
TCPConnectionClose = Boot_OS.tcpConnectionClose
GetHostByName = Boot_OS.getHostByName
UName = Boot_OS.uName
System = Boot_OS.system
%% Process management
SpawnProcess = Boot_OS.exec
SpawnProcessAndPipe = Boot_OS.pipe
proc {PipeConnectionRead Connection Count ?Head Tail ?ReadCount}
case {Boot_OS.pipeConnectionRead Connection Count Tail}
of succeeded(C H) then
Head = H
ReadCount = C
end
end
fun {PipeConnectionWrite Connection DataV}
{WaitResult {Boot_OS.pipeConnectionWrite Connection DataV}}
end
PipeConnectionShutdown = Boot_OS.pipeConnectionShutdown
PipeConnectionClose = Boot_OS.pipeConnectionClose
GetPID = Boot_OS.getPID
%% POSIX-like file descriptor management
local
DescDictLock = {NewLock}
DescDict = {NewDictionary}
in
local
fun {AllocDescEx Underlying I}
if {Dictionary.member DescDict I} then
{AllocDescEx Underlying I+1}
else
{Dictionary.put DescDict I Underlying}
I
end
end
in
fun {AllocDesc Underlying}
lock DescDictLock then
{AllocDescEx Underlying 0}
end
end
end
fun {DescGet I}
lock DescDictLock then
case {Dictionary.condGet DescDict I false}
of false then
raise
system(os(os "unknown" 9 "Bad filedescriptor") debug:unit)
end
[] Underlying then
Underlying
end
end
end
proc {FreeDesc I}
lock DescDictLock then
{Dictionary.remove DescDict I}
end
end
end
%% POSIX-like generic compatibility layer
class CompatIOClass
feat desc
meth init(desc:Desc <= _)
Desc = self.desc = {AllocDesc self}
end
meth close()
{FreeDesc self.desc}
end
end
fun {PatchVS Data}
if {Not {IsVirtualByteString Data}} andthen {IsVirtualString Data} then
{Coders.encode Data [latin1]}
else
Data
end
end
proc {CompatRead FD Max ?Head Tail ?Count}
{{DescGet FD} read(Max ?Head Tail ?Count)}
end
proc {CompatWrite FD Data ?Count}
{{DescGet FD} write({PatchVS Data} ?Count)}
end
proc {CompatLSeek FD Whence Offset ?Where}
{{DescGet FD} lSeek(Whence Offset ?Where)}
end
proc {CompatClose FD}
{{DescGet FD} close()}
end
proc {CompatAcceptSelect FD}
skip
end
proc {CompatReadSelect FD}
skip
end
proc {CompatWriteSelect FD}
skip
end
proc {CompatDeSelect FD}
skip
end
%% POSIX-like file I/O compatibility
class CompatFileClass from CompatIOClass
feat file
meth init(File desc:Desc <= _)
CompatIOClass, init(desc:Desc)
self.file = File
end
meth read(Max ?Head Tail ?Count)
{Fread self.file Max ?Head Tail ?Count}
end
meth write(Data ?Count)
{Fwrite self.file Data ?Count}
end
meth lSeek(Whence Offset ?Where)
{Fseek self.file Offset Whence ?Where}
end
meth close()
CompatIOClass, close()
{Fclose self.file}
end
end
fun {FlagsToMode Flags}
if {Member 'O_WRONLY' Flags} then
if {Member 'O_APPEND' Flags} then
"ab"
else
"wb"
end
elseif {Member 'O_RDWR' Flags} then
if {Not {Member 'O_CREAT' Flags}} then
"r+b"
elseif {Member 'O_APPEND' Flags} then
"a+b"
else
"w+b"
end
else
"rb"
end
end
fun {CompatOpen FileName Flags CreateMode}
File = {Fopen FileName {FlagsToMode Flags}}
in
{New CompatFileClass init(File desc:$) _}
end
DescStdin = {New CompatFileClass init(Stdin desc:$) _}
DescStdout = {New CompatFileClass init(Stdout desc:$) _}
DescStderr = {New CompatFileClass init(Stderr desc:$) _}
fun {CompatFileDesc DescName}
case DescName
of 'STDIN_FILENO' then DescStdin
[] 'STDOUT_FILENO' then DescStdout
[] 'STDERR_FILENO' then DescStderr
end
end
% POSIX-like socket I/O compatibility
class CompatSocketClass from CompatIOClass
end
class CompatTCPSocketClass from CompatSocketClass
attr
mode: unit
bindPort: unit
backLog: unit
connector: unit
acceptor: unit
meth init(desc:Desc <= _)
true = @mode == unit
CompatSocketClass, init(desc:Desc)
mode := init
end
meth bind(Port)
true = @mode == init
if Port \= 0 then
bindPort := Port
else
% Generate a random port - let's do something stupid for now
bindPort := {Rand} mod 100 + 1234
end
mode := bound
end
meth listen(BackLog)
true = @mode == bound
backLog := BackLog
acceptor := {TCPAcceptorCreate 4 @bindPort}
mode := acceptor
end
meth accept(?Host ?Port ?Desc)
true = @mode == acceptor
Connection = {TCPAccept @acceptor}
in
{New CompatTCPConnectionClass init(Connection desc:?Desc) _}
end
meth connect(Host Port ?Desc)
true = @mode == init
Connection = {TCPConnect Host Port}
in
connector := {New CompatTCPConnectionClass init(Connection desc:?Desc)}
mode := connected
end
meth getSockName(?Port)
Port = @bindPort
end
meth close()
CompatSocketClass, close()
if @mode == acceptor then
{TCPAcceptorClose @acceptor}
elseif @mode == connected then
{@connector close}
end
mode := closed
end
meth send(Msg Flags ?Len)
true = @mode == connected
% what about the flags?
{@connector write(Msg ?Len)}
end
meth read(Max ?Head Tail ?Count)
true = @mode == connected
{@connector read(Max ?Head Tail ?Count)}
end
meth write(Msg ?Len)
true = @mode == connected
{@connector write(Msg ?Len)}
end
end
class CompatTCPConnectionClass from CompatSocketClass
attr
connection
meth init(Connection desc:Desc <= _)
CompatSocketClass, init(desc:Desc)
connection := Connection
end
meth read(Max ?Head Tail ?Count)
{TCPConnectionRead @connection Max ?Head Tail ?Count}
end
meth write(Data ?Count)
{TCPConnectionWrite @connection Data ?Count}
end
meth shutDown(How)
What = case How
of 0 then receive
[] 1 then send
else both
end
in
{TCPConnectionShutdown @connection What}
end
meth close()
CompatSocketClass, close()
{TCPConnectionClose @connection}
end
end
fun {CompatSocket Domain Type Proto}
case Domain of 'PF_INET' then
case Type
of 'SOCK_STREAM' then
{New CompatTCPSocketClass init(desc:$) _}
end
end
end
proc {CompatBind Sock Port}
{{DescGet Sock} bind(Port)}
end
proc {CompatListen Sock BackLog}
{{DescGet Sock} listen(BackLog)}
end
proc {CompatAccept Sock ?Host ?Port ?Desc}
{{DescGet Sock} accept(?Host ?Port ?Desc)}
end
proc {CompatConnect Sock Host Port}
{{DescGet Sock} connect(Host Port _)}
end
proc {CompatShutDown Sock How}
{{DescGet Sock} shutDown(How)}
end
proc {CompatGetSockName Sock ?Port}
{{DescGet Sock} getSockName(?Port)}
end
proc {CompatSend Sock Msg Flags ?Len}
{{DescGet Sock} send({PatchVS Msg} Flags ?Len)}
end
proc {CompatSendTo Sock Msg Flags Host Port ?Len}
{{DescGet Sock} sendTo({PatchVS Msg} Flags Host Port ?Len)}
end
proc {CompatReceiveFrom Sock Max Flags ?Head Tail ?Host ?Port ?Len}
{{DescGet Sock} receiveFrom(Max Flags ?Head Tail ?Host ?Port ?Len)}
end
%% POSIX-like process management
class CompatPipeConnectionClass from CompatIOClass
attr
connection
meth init(Connection desc:Desc <= _)
CompatIOClass, init(desc:Desc)
connection := Connection
end
meth read(Max ?Head Tail ?Count)
{PipeConnectionRead @connection Max ?Head Tail ?Count}
end
meth write(Data ?Count)
{PipeConnectionWrite @connection Data ?Count}
end
meth shutDown(How)
What = case How
of 0 then receive
[] 1 then send
else both
end
in
{TCPConnectionShutdown @connection What}
end
meth close()
CompatIOClass, close()
{PipeConnectionClose @connection}
end
end
proc {CompatExec Executable Args DoKill ?Pid}
{SpawnProcess Executable Executable|Args DoKill ?Pid}
end
proc {CompatPipe Executable Args ?Pid ?Status}
Connection = {SpawnProcessAndPipe Executable Executable|Args ?Pid}
Desc = {New CompatPipeConnectionClass init(Connection desc:$) _}
in
Status = Desc#Desc
end
proc {CompatKill Pid Signal ?Status}
% TODO
Status = 0
end
proc {CompatWait ?Pid ?Status}
% TODO
Status = 0
end
%% Set up a non-stub BURL
local
HexDigitToValue = o(&0:0 &1:1 &2:2 &3:3 &4:4 &5:5 &6:6 &7:7 &8:8 &9:9
&A:10 &B:11 &C:12 &D:13 &E:14 &F:15
&a:10 &b:11 &c:12 &d:13 &e:14 &f:15)
fun {DecodeURLChars Xs}
case Xs
of &%|A|B|Xr then
AVal = {CondSelect HexDigitToValue A 0}
BVal = {CondSelect HexDigitToValue B 0}
in
(AVal*16 + BVal) | {DecodeURLChars Xr}
[] X|Xr then
X | {DecodeURLChars Xr}
[] nil then
nil
end
end
fun {DecodeURL URL}
{Coders.decode {DecodeURLChars {VirtualString.toString URL}} [utf8]}
end
fun {URLToFilename URL}
case {VirtualString.toString {DecodeURL URL}}
of &f|&i|&l|&e|&:|Filename then Filename
[] Filename then Filename
end
end
fun {BURL_localize URL}
Filename = {URLToFilename URL}
in
try
{Fclose {Fopen Filename "rb"}}
old(Filename)
catch system(os(os _ _ Msg) ...) then
raise system(url(localize Msg URL) debug:unit) end
end
end
fun {BURL_open URL}
{CompatOpen {URLToFilename URL} ['O_RDONLY'] nil}
end
in
{Boot_Reflection.become BURL.localize BURL_localize}
{Boot_Reflection.become BURL.open BURL_open}
end
end