-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebscript.pas
More file actions
246 lines (217 loc) · 5.6 KB
/
webscript.pas
File metadata and controls
246 lines (217 loc) · 5.6 KB
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
unit WebScript;
{$mode objfpc}
interface
uses
Classes, SysUtils, browserconsole, marked, Web, ajaxlib, qvfs, strutils;
type
TVMOutput = reference to procedure(s: string);
TVMOnOpCode = reference to function(op: char): Boolean;
{ TWebScript }
TWebScript = class(TComponent)
private
FData, FTarget: string;
FCP: integer;
FRemoteFile: TWebRequest;
FOnOpCode: TVMOnOpCode;
function NextOp: char;
Procedure MarkdownFail(Sender: TObject; Const aString : String);
procedure AssignProp(aID, aProp, aValue: string);
procedure HandleFile;
procedure LoadFile(AFile: string);
protected
procedure Output(s: string); virtual;
public
property Target: string read FTarget write FTarget;
property OnOpCode: TVMOnOpCode read FOnOpCode write FOnOpCode;
constructor Create(AOwner: TComponent; AData: string);
function GetString: string;
procedure Run;
end;
{ TSysRunner }
TSysRunner = class(TWebScript)
private
FOnOutput: TVMOutput;
procedure GotDir(f: TVFSFile);
procedure GetDir(f: TVFSFile);
procedure VMCallback(f: TVFSFile);
protected
procedure Output(s: string); override;
public
property OnOutput: TVMOutput read FOnOutput write FOnOutput;
procedure RunFile(AFile, ALoc: string);
end;
var
FileSys: TVFSRequest;
VFS_ENDPOINT, VFS_ROOT: string;
implementation
{ TSysRunner }
procedure TSysRunner.GotDir(f: TVFSFile);
var
e: TJSHTMLElement;
icon: string;
begin
if f.typ = -1 then
Exit;
case f.typ of
0: icon:='dir';
1: icon:='binary';
2: icon:='layout';
3: icon:='text';
4: icon:='portal';
5: icon:='folder.sec';
6: icon:='quill';
7: icon:='comp.blue';
8: icon:='comp.gray';
13: icon:='script';
else
icon:='generic';
end;
e:=TJSHTMLElement(document.getElementById(FTarget));
icon:='<img src="/icons/'+icon+'.png"/>';
e.innerHTML:=e.innerHTML+'<a href="#'+VFS_ROOT+f.wh+':'+f.fna+'">'+icon+f.fna+'</a><br/>';
end;
procedure TSysRunner.GetDir(f: TVFSFile);
var
data, fna: string;
fs: TVFSRequest;
begin
if f.data[1] = 'V' then
begin
TJSHTMLElement(document.getElementById(FTarget)).innerHTML:='VFS Directory of '+f.fna+'<hr>';
data:=RightStr(f.data, Length(f.data)-1);
repeat
fna:=Copy2SymbDel(data, ';');
if fna <> '' then
begin
fs:=TVFSRequest.Create(Nil, 'get', VFS_ENDPOINT);
fs.OnCallback:=@GotDir;
fs.GetFile(fna, f.fna);
end;
until fna = '';
end
else
Output('Directory Unavailable.');
end;
procedure TSysRunner.VMCallback(f: TVFSFile);
var
t: TMarkdownFile;
begin
if f.typ = -1 then
Output('File not found.')
else if f.typ = 0 then
GetDir(f)
else if f.typ = 2 then
t:=TMarkdownFile.Create(Self, f.data, FTarget, @MarkdownFail)
else if f.typ = 7 then
begin
FData:=f.data;
FCP:=1;
Run;
end
else
Output('Unhandled file-type.');
end;
procedure TSysRunner.Output(s: string);
begin
if Assigned(FOnOutput) then
FOnOutput(s);
end;
procedure TSysRunner.RunFile(AFile, ALoc: string);
begin
FileSys.OnCallback:=@VMCallback;
FileSys.GetFile(AFile, ALoc);
end;
{ TWebScript }
procedure TWebScript.Output(s: string);
begin
Writeln(s);
end;
function TWebScript.NextOp: char;
begin
Result:=FData[FCP];
Inc(FCP);
end;
function TWebScript.GetString: string;
var
c: char;
begin
Result:='';
repeat
c:=NextOp;
if c <> '~' then
Result:=Result+c;
until (c = '~') or (FCP > Length(FData));
end;
procedure TWebScript.MarkdownFail(Sender: TObject; const aString: String);
begin
TJSHTMLElement(document.getElementById(FTarget)).innerHTML:='Markdown failed to load: '+aString;
end;
procedure TWebScript.AssignProp(aID, aProp, aValue: string);
begin
TJSHTMLElement(document.getElementById(aID)).Properties[aProp]:=aValue;
end;
procedure TWebScript.HandleFile;
begin
if not FRemoteFile.Complete then
Exit;
Output(FRemoteFile.responseText);
FreeAndNil(FRemoteFile);
end;
procedure TWebScript.LoadFile(AFile: string);
begin
if Assigned(FRemoteFile) then
begin
Output('File Load Request already in progress...');
Exit;
end;
FRemoteFile:=TWebRequest.Create(Nil, 'get', AFile);
FRemoteFile.OnChange:=@HandleFile;
FRemoteFile.DoRequest;
end;
constructor TWebScript.Create(AOwner: TComponent; AData: string);
begin
inherited Create(AOwner);
FData:=AData;
FCP:=1;
FTarget:='TabBody';
end;
procedure TWebScript.Run;
var
op: char;
running: boolean;
t: TMarkdownFile;
target: string;
begin
running:=True;
repeat
op:=NextOp;
case op of
'$': target:=FTarget;
'A': window.alert(GetString);
'a': AssignProp(GetString, GetString, GetString);
'h': TJSHTMLElement(document.getElementById(GetString)).hidden:=True;
's': TJSHTMLElement(document.getElementById(GetString)).hidden:=False;
'C': TJSHTMLElement(document.getElementById(GetString)).classList.add(GetString);
'c': TJSHTMLElement(document.getElementById(GetString)).classList.remove(GetString);
'P': Output(GetString);
'L': LoadFile(GetString);
'p': TJSHTMLElement(document.getElementById(target)).innerHTML:=GetString;
{'B': LoadFileInto(GetString, FBody);}
'T': target:=GetString;
'M': t:=TMarkdownFile.Create(Self, GetString, target, @MarkdownFail);
'X': running:=False;
else
running:=False;
if op = '#' then
Output('Program can only run on server.')
else
begin
if Assigned(FOnOpCode) then
running:=FOnOpCode(op);
if not running then
Output('Invalid Op Code at '+IntToStr(FCP-1));
end;
end;
until not running;
end;
end.