-
Notifications
You must be signed in to change notification settings - Fork 16
/
Batch.Mod.txt
244 lines (230 loc) · 6.46 KB
/
Batch.Mod.txt
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
MODULE Batch;
IMPORT Kernel, FileDir, Files, Display, Texts, TextFrames, Viewers, Oberon;
CONST
BatchStopped = 0;
BatchRunning = 1;
BatchWaitGC = 2;
BatchFailed = 3;
OberonVerifyVersion = "OR Compiler 22.7.2018";
TYPE
FileList = POINTER TO FileListDesc;
FileListDesc = RECORD
name: FileDir.FileName;
next: FileList;
END;
VAR
pat: ARRAY 32 OF CHAR;
fl: FileList;
state: INTEGER;
emptyReader: Texts.Reader;
script: Texts.Reader;
scriptFrame: TextFrames.Frame;
gcWaitTask: Oberon.Task;
gcWaitAllocated: INTEGER;
PROCEDURE ClearLog*;
VAR
buf: Texts.Buffer;
BEGIN
NEW(buf); Texts.OpenBuf(buf);
Texts.Delete(Oberon.Log, 0, Oberon.Log.len, buf)
END ClearLog;
PROCEDURE WriteLog*;
VAR
S: Texts.Scanner;
W: Texts.Writer;
V: Viewers.Viewer;
nl, go: BOOLEAN;
i: INTEGER;
BEGIN
nl := TRUE; go := TRUE;
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
Texts.OpenWriter(W);
WHILE go DO
Texts.Scan(S);
IF (S.class = Texts.Char) & (S.c = "\") THEN
nl := FALSE
ELSIF (S.class = Texts.Char) & (S.c = "*") THEN
V := Viewers.This(Display.Width - 10, Display.Height - 10);
TextFrames.Show(V.dsc.next(TextFrames.Frame), Oberon.Log.len);
ELSIF S.class = Texts.Int THEN
FOR i := 1 TO S.i DO Texts.WriteLn(W); END
ELSIF (S.class = Texts.Name) OR (S.class = Texts.String) THEN
Texts.WriteString(W, S.s);
go := FALSE
ELSE
go := FALSE
END
END;
IF nl THEN
Texts.WriteLn(W)
END;
Texts.Append(Oberon.Log, W.buf)
END WriteLog;
PROCEDURE VerifyLog*;
VAR
S: Texts.Scanner;
R: Texts.Reader;
W: Texts.Writer;
ch: CHAR;
csum, vcsum, vpos: INTEGER;
OberonVerifyVersionA: ARRAY 22 OF CHAR;
BEGIN
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
Texts.Scan(S);
IF S.class = Texts.Int THEN vcsum := S.i ELSE vcsum := 0 END;
Texts.OpenReader(R, Oberon.Log, 0);
ch := 0X;
csum := 0;
vpos := 0;
OberonVerifyVersionA := OberonVerifyVersion;
WHILE ~ R.eot DO
IF ch = "`" THEN
WHILE ((ch >= "0") & (ch <= "9")) OR ((ch >= "A") & (ch <= "F")) OR (ch = " ") OR (ch = ".") OR (ch = "`") DO Texts.Read(R, ch) END
ELSIF ch = " " THEN
WHILE ch = " " DO Texts.Read(R, ch) END
ELSE
Texts.Read(R, ch);
END;
csum := ROR(csum + ORD(ch), 13);
IF ch = OberonVerifyVersionA[vpos] THEN INC(vpos) ELSE vpos := 0 END;
IF vpos = 12 THEN
WHILE OberonVerifyVersionA[vpos] # 0X DO
csum := ROR(csum + ORD(OberonVerifyVersionA[vpos]), 13);
INC(vpos)
END;
ch := "`";
vpos := 0
END
END;
IF csum # vcsum THEN
Texts.OpenWriter(W);
Texts.WriteString(W, "Log checksum");
Texts.WriteHex(W, csum);
Texts.WriteString(W, "H does not match");
Texts.WriteHex(W, vcsum);
Texts.WriteString(W, "H!");
Texts.WriteLn(W);
Texts.Append(Oberon.Log, W.buf);
IF state = BatchRunning THEN state := BatchFailed END
END
END VerifyLog;
PROCEDURE AddFileToList(name: FileDir.FileName; adr: LONGINT; VAR cont: BOOLEAN);
VAR
i0, i, j0, j: INTEGER; hp: FileDir.FileHeader; f: FileList;
BEGIN
i := 0;
WHILE (pat[i] > "*") & (pat[i] = name[i]) DO INC(i) END;
IF (pat[i] = 0X) & (name[i] = 0X) THEN i0 := i; j0 := i
ELSIF pat[i] = "*" THEN
i0 := i; j0 := i+1;
WHILE name[i0] # 0X DO
i := i0; j := j0;
WHILE (name[i] # 0X) & (name[i] = pat[j]) DO INC(i); INC(j) END;
IF pat[j] = 0X THEN
IF name[i] = 0X THEN (*match*) j0 := j ELSE INC(i0) END
ELSIF pat[j] = "*" THEN i0 := i; j0 := j+1
ELSE INC(i0)
END
END
END;
IF (name[i0] = 0X) & (pat[j0] = 0X) THEN (*found*)
NEW(f);
f.next := fl;
f.name := name;
fl := f
END
END AddFileToList;
PROCEDURE DeleteFiles*;
VAR
R: Texts.Reader;
i: INTEGER;
ch: CHAR;
pre: ARRAY 32 OF CHAR;
BEGIN
Texts.OpenReader(R, Oberon.Par.text, Oberon.Par.pos);
Texts.Read(R, ch);
WHILE ch = " " DO Texts.Read(R, ch) END;
i := 0;
WHILE ch > " " DO pat[i] := ch; INC(i); Texts.Read(R, ch) END;
pat[i] := 0X;
i := 0;
WHILE pat[i] > "*" DO pre[i] := pat[i]; INC(i) END;
pre[i] := 0X;
fl := NIL;
FileDir.Enumerate(pre, AddFileToList);
WHILE fl # NIL DO
Files.Delete(fl.name, i);
fl := fl.next;
END
END DeleteFiles;
PROCEDURE Collect*;
BEGIN
Oberon.Collect(1);
IF state = BatchRunning THEN state := BatchWaitGC END
END Collect;
PROCEDURE Continue;
VAR
W: Texts.Writer;
ch: CHAR;
BEGIN
state := BatchRunning;
WHILE state = BatchRunning DO
Texts.Read(script, ch);
IF script.eot THEN
state := BatchStopped
ELSIF ch = "|" THEN
Texts.Read(script, ch);
IF script.eot OR (ch = "|") THEN
state := BatchStopped;
ELSIF ch = ">" THEN
Texts.Read(script, ch);
WHILE ch <= " " DO Texts.Read(script, ch) END;
TextFrames.Call(scriptFrame, Texts.Pos(script) - 1, FALSE);
END
END
END;
IF state = BatchWaitGC THEN
Texts.OpenWriter(W); (* calls NEW *)
Texts.WriteString(W, "Waiting for gc ...");
Texts.Append(Oberon.Log, W.buf);
gcWaitAllocated := Kernel.allocated;
Oberon.Install(gcWaitTask);
ELSE
state := BatchStopped;
script := emptyReader;
scriptFrame := NIL;
END
END Continue;
PROCEDURE GCWait;
VAR
W: Texts.Writer;
BEGIN
IF (state = BatchWaitGC) & (Kernel.allocated < gcWaitAllocated) THEN
Texts.OpenWriter(W);
Texts.WriteString(W, " done");
Texts.WriteLn(W);
Texts.Append(Oberon.Log, W.buf);
Oberon.Remove(gcWaitTask);
Continue();
ELSE
Oberon.Collect(0);
END
END GCWait;
PROCEDURE Run*;
VAR
W: Texts.Writer;
BEGIN
IF state # BatchStopped THEN
Texts.OpenWriter(W);
Texts.WriteString(W, "Cannot run Batch while another Batch is running");
Texts.WriteLn(W);
Texts.Append(Oberon.Log, W.buf);
ELSE
Texts.OpenReader(script, Oberon.Par.text, Oberon.Par.pos);
scriptFrame := Oberon.Par.frame(TextFrames.Frame);
Continue();
END
END Run;
BEGIN
gcWaitTask := Oberon.NewTask(GCWait, 100);
END Batch.