forked from KolibriOS/kolibrios
138 lines
3.3 KiB
Plaintext
138 lines
3.3 KiB
Plaintext
|
(*
|
||
|
BSD 2-Clause License
|
||
|
|
||
|
Copyright (c) 2019-2020, Anton Krotov
|
||
|
All rights reserved.
|
||
|
*)
|
||
|
|
||
|
MODULE LINAPI;
|
||
|
|
||
|
IMPORT SYSTEM, API;
|
||
|
|
||
|
|
||
|
TYPE
|
||
|
|
||
|
TP* = API.TP;
|
||
|
SOFINI* = API.SOFINI;
|
||
|
|
||
|
|
||
|
VAR
|
||
|
|
||
|
argc*, envc*: INTEGER;
|
||
|
|
||
|
libc*, librt*: INTEGER;
|
||
|
|
||
|
stdout*,
|
||
|
stdin*,
|
||
|
stderr* : INTEGER;
|
||
|
|
||
|
malloc* : PROCEDURE [linux] (size: INTEGER): INTEGER;
|
||
|
free* : PROCEDURE [linux] (ptr: INTEGER);
|
||
|
exit* : PROCEDURE [linux] (code: INTEGER);
|
||
|
puts* : PROCEDURE [linux] (pStr: INTEGER);
|
||
|
fwrite*,
|
||
|
fread* : PROCEDURE [linux] (buffer, bytes, blocks, file: INTEGER): INTEGER;
|
||
|
fopen* : PROCEDURE [linux] (fname, fmode: INTEGER): INTEGER;
|
||
|
fclose* : PROCEDURE [linux] (file: INTEGER): INTEGER;
|
||
|
time* : PROCEDURE [linux] (ptr: INTEGER): INTEGER;
|
||
|
|
||
|
clock_gettime* : PROCEDURE [linux] (clock_id: INTEGER; VAR tp: TP): INTEGER;
|
||
|
|
||
|
|
||
|
PROCEDURE GetArg* (n: INTEGER; VAR s: ARRAY OF CHAR);
|
||
|
VAR
|
||
|
i, len, ptr: INTEGER;
|
||
|
c: CHAR;
|
||
|
|
||
|
BEGIN
|
||
|
i := 0;
|
||
|
len := LEN(s) - 1;
|
||
|
IF (0 <= n) & (n <= argc + envc) & (n # argc) & (len > 0) THEN
|
||
|
SYSTEM.GET(API.MainParam + (n + 1) * SYSTEM.SIZE(INTEGER), ptr);
|
||
|
REPEAT
|
||
|
SYSTEM.GET(ptr, c);
|
||
|
s[i] := c;
|
||
|
INC(i);
|
||
|
INC(ptr)
|
||
|
UNTIL (c = 0X) OR (i = len)
|
||
|
END;
|
||
|
s[i] := 0X
|
||
|
END GetArg;
|
||
|
|
||
|
|
||
|
PROCEDURE GetEnv* (n: INTEGER; VAR s: ARRAY OF CHAR);
|
||
|
BEGIN
|
||
|
IF (0 <= n) & (n < envc) THEN
|
||
|
GetArg(n + argc + 1, s)
|
||
|
ELSE
|
||
|
s[0] := 0X
|
||
|
END
|
||
|
END GetEnv;
|
||
|
|
||
|
|
||
|
PROCEDURE SetFini* (ProcFini: SOFINI);
|
||
|
BEGIN
|
||
|
API.SetFini(ProcFini)
|
||
|
END SetFini;
|
||
|
|
||
|
|
||
|
PROCEDURE init;
|
||
|
VAR
|
||
|
ptr: INTEGER;
|
||
|
|
||
|
BEGIN
|
||
|
IF API.MainParam # 0 THEN
|
||
|
envc := -1;
|
||
|
SYSTEM.GET(API.MainParam, argc);
|
||
|
REPEAT
|
||
|
SYSTEM.GET(API.MainParam + (envc + argc + 3) * SYSTEM.SIZE(INTEGER), ptr);
|
||
|
INC(envc)
|
||
|
UNTIL ptr = 0
|
||
|
ELSE
|
||
|
envc := 0;
|
||
|
argc := 0
|
||
|
END;
|
||
|
|
||
|
libc := API.libc;
|
||
|
|
||
|
stdout := API.stdout;
|
||
|
stdin := API.stdin;
|
||
|
stderr := API.stderr;
|
||
|
|
||
|
malloc := API.malloc;
|
||
|
free := API.free;
|
||
|
exit := API._exit;
|
||
|
puts := API.puts;
|
||
|
fwrite := API.fwrite;
|
||
|
fread := API.fread;
|
||
|
fopen := API.fopen;
|
||
|
fclose := API.fclose;
|
||
|
time := API.time;
|
||
|
|
||
|
librt := API.librt;
|
||
|
|
||
|
clock_gettime := API.clock_gettime
|
||
|
END init;
|
||
|
|
||
|
|
||
|
PROCEDURE [stdcall64-] syscall* (rax, rdi, rsi, rdx, r10, r8, r9: INTEGER): INTEGER;
|
||
|
BEGIN
|
||
|
SYSTEM.CODE(
|
||
|
048H, 08BH, 045H, 010H, (* mov rax, qword [rbp + 16] *)
|
||
|
048H, 08BH, 07DH, 018H, (* mov rdi, qword [rbp + 24] *)
|
||
|
048H, 08BH, 075H, 020H, (* mov rsi, qword [rbp + 32] *)
|
||
|
048H, 08BH, 055H, 028H, (* mov rdx, qword [rbp + 40] *)
|
||
|
04CH, 08BH, 055H, 030H, (* mov r10, qword [rbp + 48] *)
|
||
|
04CH, 08BH, 045H, 038H, (* mov r8, qword [rbp + 56] *)
|
||
|
04CH, 08BH, 04DH, 040H, (* mov r9, qword [rbp + 64] *)
|
||
|
00FH, 005H, (* syscall *)
|
||
|
0C9H, (* leave *)
|
||
|
0C2H, 038H, 000H (* ret 56 *)
|
||
|
)
|
||
|
RETURN 0
|
||
|
END syscall;
|
||
|
|
||
|
|
||
|
BEGIN
|
||
|
init
|
||
|
END LINAPI.
|