mirror of
https://github.com/vapaamies/KolibriOS.git
synced 2025-09-21 22:53:51 +02:00
Units initialization/finalization support added
This commit is contained in:
@@ -4,7 +4,7 @@ uses
|
|||||||
CRT;
|
CRT;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConsoleInit('Console Colors');
|
InitConsole('Console Colors');
|
||||||
|
|
||||||
TextBackground(Black);
|
TextBackground(Black);
|
||||||
WriteLn('Black');
|
WriteLn('Black');
|
||||||
@@ -75,6 +75,4 @@ begin
|
|||||||
WriteLn('White');
|
WriteLn('White');
|
||||||
|
|
||||||
ReadKey;
|
ReadKey;
|
||||||
|
|
||||||
ConsoleExit(True);
|
|
||||||
end.
|
end.
|
||||||
|
@@ -9,7 +9,7 @@ var
|
|||||||
CursorPos: TConsolePoint;
|
CursorPos: TConsolePoint;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConsoleInit('Date/Time');
|
InitConsole('Date/Time');
|
||||||
|
|
||||||
SetCursorHeight(0);
|
SetCursorHeight(0);
|
||||||
SetCursorPos(27, 11);
|
SetCursorPos(27, 11);
|
||||||
@@ -29,6 +29,4 @@ begin
|
|||||||
SetCursorPos(CursorPos);
|
SetCursorPos(CursorPos);
|
||||||
Sleep(50);
|
Sleep(50);
|
||||||
until KeyPressed;
|
until KeyPressed;
|
||||||
|
|
||||||
ConsoleExit(True);
|
|
||||||
end.
|
end.
|
||||||
|
@@ -13,13 +13,11 @@ var
|
|||||||
Buffer: array[0..BUFFER_SIZE - 1] of Char;
|
Buffer: array[0..BUFFER_SIZE - 1] of Char;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConsoleInit('Get Current Directory');
|
InitConsole('Get Current Directory', False);
|
||||||
|
|
||||||
GetCurrentDirectory(Buffer, BUFFER_SIZE);
|
GetCurrentDirectory(Buffer, BUFFER_SIZE);
|
||||||
|
|
||||||
Write('AppPath is "%s"'#10, AppPath^);
|
Write('AppPath is "%s"'#10, AppPath^);
|
||||||
Write('CmdLine is "%s"'#10, CmdLine^);
|
Write('CmdLine is "%s"'#10, CmdLine^);
|
||||||
Write('Current Directory is "%s"'#10, Buffer);
|
Write('Current Directory is "%s"'#10, Buffer);
|
||||||
|
|
||||||
ConsoleExit(False);
|
|
||||||
end.
|
end.
|
||||||
|
@@ -4,10 +4,7 @@ uses
|
|||||||
CRT;
|
CRT;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConsoleInit('Hello');
|
InitConsole('Hello');
|
||||||
|
|
||||||
WriteLn('Hello, world!');
|
WriteLn('Hello, world!');
|
||||||
|
|
||||||
ReadKey;
|
ReadKey;
|
||||||
ConsoleExit(True);
|
|
||||||
end.
|
end.
|
||||||
|
@@ -8,10 +8,8 @@ var
|
|||||||
Buffer: Pointer;
|
Buffer: Pointer;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConsoleInit('Load File');
|
InitConsole('Load File', False);
|
||||||
|
|
||||||
Buffer := LoadFile('/sys/example.asm', FileSize);
|
Buffer := LoadFile('/sys/example.asm', FileSize);
|
||||||
WriteText(Buffer, FileSize);
|
WriteText(Buffer, FileSize);
|
||||||
|
|
||||||
ConsoleExit(False);
|
|
||||||
end.
|
end.
|
||||||
|
@@ -17,7 +17,7 @@ var
|
|||||||
Pos: LongWord;
|
Pos: LongWord;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ConsoleInit('Read Folder');
|
InitConsole('Read Folder', False);
|
||||||
|
|
||||||
if ReadFolder(FolderPath, FolderInformation, 0, 0, 0, BlocksRead) = 0 then
|
if ReadFolder(FolderPath, FolderInformation, 0, 0, 0, BlocksRead) = 0 then
|
||||||
with FolderInformation do
|
with FolderInformation do
|
||||||
@@ -53,6 +53,4 @@ begin
|
|||||||
Write(#10);
|
Write(#10);
|
||||||
Inc(Pos);
|
Inc(Pos);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
ConsoleExit(False);
|
|
||||||
end.
|
end.
|
||||||
|
24
Lib/CRT.pas
24
Lib/CRT.pas
@@ -28,7 +28,8 @@ const
|
|||||||
Yellow = 14;
|
Yellow = 14;
|
||||||
White = 15;
|
White = 15;
|
||||||
|
|
||||||
procedure ConsoleInit(Title: PKolibriChar);
|
procedure InitConsole(Caption: PKolibriChar; CloseWindowOnExit: Boolean = True;
|
||||||
|
WndWidth: LongWord = $FFFFFFFF; WndHeight: LongWord = $FFFFFFFF; ScrWidth: LongWord = $FFFFFFFF; ScrHeight: LongWord = $FFFFFFFF);
|
||||||
|
|
||||||
function GetCursorPos: TConsolePoint;
|
function GetCursorPos: TConsolePoint;
|
||||||
procedure SetCursorPos(X, Y: Integer); overload;
|
procedure SetCursorPos(X, Y: Integer); overload;
|
||||||
@@ -43,7 +44,6 @@ function WriteLn(LineBreaks: Integer = 1): LongInt; overload;
|
|||||||
function WriteLn(Text: PKolibriChar; LineBreaks: Integer = 1): LongInt; overload;
|
function WriteLn(Text: PKolibriChar; LineBreaks: Integer = 1): LongInt; overload;
|
||||||
|
|
||||||
var
|
var
|
||||||
ConsoleExit: procedure(CloseWindow: Boolean); stdcall;
|
|
||||||
KeyPressed: function: Boolean;
|
KeyPressed: function: Boolean;
|
||||||
ReadKey: function: KolibriChar; stdcall;
|
ReadKey: function: KolibriChar; stdcall;
|
||||||
SetCursorHeight: function(Height: Integer): Integer; stdcall;
|
SetCursorHeight: function(Height: Integer): Integer; stdcall;
|
||||||
@@ -52,6 +52,9 @@ var
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
var
|
||||||
|
CloseWindow: Boolean;
|
||||||
|
|
||||||
procedure ResetAttributes;
|
procedure ResetAttributes;
|
||||||
begin
|
begin
|
||||||
Write(#27'[0m');
|
Write(#27'[0m');
|
||||||
@@ -129,15 +132,17 @@ end;
|
|||||||
|
|
||||||
var
|
var
|
||||||
hConsole: Pointer;
|
hConsole: Pointer;
|
||||||
ConsoleInitProc: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongInt; Caption: PKolibriChar); stdcall;
|
ConsoleExit: procedure(CloseWindow: Boolean); stdcall;
|
||||||
|
ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Caption: PKolibriChar); stdcall;
|
||||||
GetCursorPosProc: procedure(var X, Y: Integer); stdcall;
|
GetCursorPosProc: procedure(var X, Y: Integer); stdcall;
|
||||||
SetCursorPosProc: procedure(X, Y: Integer); stdcall;
|
SetCursorPosProc: procedure(X, Y: Integer); stdcall;
|
||||||
|
|
||||||
procedure ConsoleInit(Title: PKolibriChar);
|
procedure InitConsole(Caption: PKolibriChar; CloseWindowOnExit: Boolean;
|
||||||
|
WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord);
|
||||||
begin
|
begin
|
||||||
hConsole := LoadLibrary('/sys/lib/console.obj');
|
hConsole := LoadLibrary('/sys/lib/console.obj');
|
||||||
ConsoleInitProc := GetProcAddress(hConsole, 'con_init');
|
|
||||||
ConsoleExit := GetProcAddress(hConsole, 'con_exit');
|
ConsoleExit := GetProcAddress(hConsole, 'con_exit');
|
||||||
|
ConsoleInit := GetProcAddress(hConsole, 'con_init');
|
||||||
GetCursorPosProc := GetProcAddress(hConsole, 'con_get_cursor_pos');
|
GetCursorPosProc := GetProcAddress(hConsole, 'con_get_cursor_pos');
|
||||||
KeyPressed := GetProcAddress(hConsole, 'con_kbhit');
|
KeyPressed := GetProcAddress(hConsole, 'con_kbhit');
|
||||||
ReadKey := GetProcAddress(hConsole, 'con_getch');
|
ReadKey := GetProcAddress(hConsole, 'con_getch');
|
||||||
@@ -146,7 +151,8 @@ begin
|
|||||||
Write := GetProcAddress(hConsole, 'con_printf');
|
Write := GetProcAddress(hConsole, 'con_printf');
|
||||||
WriteText := GetProcAddress(hConsole, 'con_write_string');
|
WriteText := GetProcAddress(hConsole, 'con_write_string');
|
||||||
|
|
||||||
ConsoleInitProc(-1, -1, -1, -1, Title);
|
ConsoleInit(WndWidth, WndHeight, ScrWidth, ScrHeight, Caption);
|
||||||
|
CloseWindow := CloseWindowOnExit;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function GetCursorPos: TConsolePoint;
|
function GetCursorPos: TConsolePoint;
|
||||||
@@ -165,4 +171,10 @@ begin
|
|||||||
SetCursorPosProc(X, Y);
|
SetCursorPosProc(X, Y);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
|
||||||
|
finalization
|
||||||
|
if hConsole <> nil then
|
||||||
|
ConsoleExit(CloseWindow);
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@@ -1,15 +1,14 @@
|
|||||||
(*
|
(*
|
||||||
Minimal Delphi SysInit unit
|
KolibriOS RTL System unit
|
||||||
*)
|
*)
|
||||||
|
|
||||||
unit SysInit;
|
unit SysInit;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
|
||||||
procedure _InitExe;
|
procedure _InitExe(InitTable: PPackageInfo);
|
||||||
|
|
||||||
var
|
var
|
||||||
ModuleIsLib: Boolean;
|
|
||||||
TLSIndex: Integer = -1;
|
TLSIndex: Integer = -1;
|
||||||
TLSLast: Byte;
|
TLSLast: Byte;
|
||||||
|
|
||||||
@@ -18,8 +17,9 @@ const
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure _InitExe;
|
procedure _InitExe(InitTable: PPackageInfo);
|
||||||
asm
|
begin
|
||||||
|
_StartExe(InitTable);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
(*
|
(*
|
||||||
Minimal Delphi System unit
|
KolibriOS RTL System unit
|
||||||
*)
|
*)
|
||||||
|
|
||||||
unit System;
|
unit System;
|
||||||
@@ -19,33 +19,96 @@ type
|
|||||||
D4: array [0..7] of Byte;
|
D4: array [0..7] of Byte;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
PProcedure = procedure;
|
||||||
|
|
||||||
|
TPackageUnitEntry = packed record
|
||||||
|
Init, Finalize: PProcedure;
|
||||||
|
end;
|
||||||
|
|
||||||
|
PUnitEntryTable = ^TUnitEntryTable;
|
||||||
|
TUnitEntryTable = array [0..99999999] of TPackageUnitEntry;
|
||||||
|
|
||||||
|
PPackageInfo = ^TPackageInfo;
|
||||||
|
TPackageInfo = packed record
|
||||||
|
UnitCount: Integer;
|
||||||
|
UnitInfo: PUnitEntryTable;
|
||||||
|
end;
|
||||||
|
|
||||||
PInitContext = ^TInitContext;
|
PInitContext = ^TInitContext;
|
||||||
TInitContext = record
|
TInitContext = record
|
||||||
|
InitTable: PPackageInfo;
|
||||||
|
InitCount: Integer;
|
||||||
OuterContext: PInitContext;
|
OuterContext: PInitContext;
|
||||||
ExceptionFrame, InitTable, InitCount: Integer;
|
|
||||||
Module: Pointer;
|
|
||||||
DLLSaveEBP, DLLSaveEBX, DLLSaveESI, DLLSaveEDI: Pointer;
|
|
||||||
ExitProcessTLS: procedure;
|
|
||||||
DLLInitState: byte;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure _Halt0;
|
procedure _Halt0;
|
||||||
procedure _HandleFinally;
|
procedure _HandleFinally;
|
||||||
|
procedure _StartExe(InitTable: PPackageInfo);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
uses
|
||||||
SysInit;
|
SysInit;
|
||||||
|
|
||||||
procedure _Halt0;
|
var
|
||||||
asm
|
InitContext: TInitContext;
|
||||||
XOR EAX, EAX
|
|
||||||
DEC EAX
|
|
||||||
INT $40
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure _HandleFinally;
|
procedure _HandleFinally;
|
||||||
asm
|
asm
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure InitUnits;
|
||||||
|
var
|
||||||
|
Idx: Integer;
|
||||||
|
begin
|
||||||
|
if InitContext.InitTable <> nil then
|
||||||
|
with InitContext.InitTable^ do
|
||||||
|
begin
|
||||||
|
Idx := 0;
|
||||||
|
while Idx < UnitCount do
|
||||||
|
begin
|
||||||
|
with UnitInfo[Idx] do
|
||||||
|
begin
|
||||||
|
if Assigned(Init) then
|
||||||
|
Init;
|
||||||
|
end;
|
||||||
|
Inc(Idx);
|
||||||
|
InitContext.InitCount := Idx;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure FinalizeUnits;
|
||||||
|
begin
|
||||||
|
if InitContext.InitTable <> nil then
|
||||||
|
begin
|
||||||
|
with InitContext do
|
||||||
|
begin
|
||||||
|
while InitCount > 0 do
|
||||||
|
begin
|
||||||
|
Dec(InitCount);
|
||||||
|
with InitTable.UnitInfo[InitCount] do
|
||||||
|
if Assigned(Finalize) then
|
||||||
|
Finalize;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure _StartExe(InitTable: PPackageInfo);
|
||||||
|
begin
|
||||||
|
InitContext.InitTable := InitTable;
|
||||||
|
InitContext.InitCount := 0;
|
||||||
|
InitUnits;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure _Halt0;
|
||||||
|
begin
|
||||||
|
FinalizeUnits;
|
||||||
|
asm
|
||||||
|
OR EAX, -1
|
||||||
|
INT $40
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
Reference in New Issue
Block a user