-
Notifications
You must be signed in to change notification settings - Fork 3
/
base.processes.bmx
373 lines (290 loc) · 8.28 KB
/
base.processes.bmx
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
Rem
===========================================================
PROCESS HANDLING CLASSES
===========================================================
These types allow handling of executed processes and fetching
their output strings.
ENDREM
SuperStrict
Import pub.freeprocess 'required for process handling
Type TCodeTesterProcess
Global processes:TList = CreateList()
Field name:String
Field handle:Size_T
Field standardIO:TPipeStream
Field errorIO:TPipeStream
Field flags:Int = 0
Field runAfterwards:TCodeTesterProcess
Method Init:TCodeTesterProcess( name:String, flags:Int = -1, runNow:Int=True )
?MacOS
If FileType(name+".app")=FILETYPE_DIR
Local a:String = StripExt( StripDir(name) )
name:+".app/Contents/MacOS/"+a
EndIf
?
If flags = -1
self.flags = HIDECONSOLE
Else
Self.flags = flags
EndIf
Self.name = name
'remove old processes when creating already
FlushZombies()
If runNow Then RunProcess()
Return Self
End Method
Method Alive:Int()
If handle
If fdProcessStatus(handle) Then Return True
handle = 0
EndIf
Return False
End Method
Method IOAvailable:Int()
Return StandardIOAvailable() Or ErrorIOAvailable()
End Method
Function StreamHasLine:int(stream:TPipeStream)
if stream.bufferPos <= 0 then return False
For local n:int = 0 To stream.bufferpos
'newline character found?
If stream.readbuffer[n] = 10 then return True
Next
return False
End Function
Method Read:String()
Local result:String = ""
'prefer errors first?
'If result="" Then result = ReadErrorIO()
'If result="" Then result = ReadStandardIO()
'or read them in straight one after another?
result :+ ReadErrorIO()
result :+ ReadStandardIO()
Return result
End Method
Method ReadStandardIO:String()
If StandardIOAvailable()
'local hasLine:int = StreamHasLine(standardIO)
'if not hasLine then return ""
local res:string = standardIO.ReadLine().Replace("~r","") '.Replace("~n","")
'read all what's left if app ended without newline
if res.length = 0 and not handle then return ReadStreamBuffer(standardIO)
return res
EndIf
End Method
Method ReadErrorIO:String()
If ErrorIOAvailable()
local res:string = errorIO.ReadLine().Replace("~r","") '.Replace("~n","")
'read all what's left if app ended without newline
if res.length = 0 and not handle then return ReadStreamBuffer(errorIO)
return res
EndIf
End Method
Method StandardIOAvailable:Int()
'readAvail() returns the number of bytes "to fetch"
'bufferPos describes what was already fetched but not "read" yet
Return standardIO and (standardIO.readAvail() or standardIO.bufferPos > 0)
End Method
Method ErrorIOAvailable:Int()
Return errorIO and (errorIO.readAvail() or errorIO.bufferPos > 0)
End Method
Method ReadStreamBuffer:string(stream:TPipeStream)
local result:string
local n:int = stream.bufferPos
local p1:int = n
If (n>0)
If stream.readbuffer[n-1] = 13
p1 = n-1
EndIf
EndIf
local p0:int = 0
If stream.readbuffer[0] = 13
p0 = 1
EndIf
If p1>p0
result = String.FromBytes(Varptr stream.readbuffer[p0],p1-p0)
EndIf
stream.bufferPos = 0
return result
End Method
Method CloseStreams()
'print "CloseStreams() - name: " + name
If standardIO
standardIO.Close()
standardIO = Null
EndIf
If errorIO
errorIO.Close()
errorIO = Null
EndIf
End Method
Method Close()
Terminate()
If runAfterwards Then runAfterwards.RunProcess()
End Method
Method Terminate:Int()
CloseStreams()
Local res:Int
If handle
res = fdTerminateProcess(handle)
handle = 0
EndIf
Return res
End Method
Method RunProcess:TCodeTesterProcess()
'print "RunProcess: "+processes.Count()+" processes still running."
Local infd:Size_T, outfd:Size_T, errfd:Size_T
'remove old processes when running
FlushZombies()
handle = fdProcess(name, Varptr infd, Varptr outfd, Varptr errfd, flags)
If Not handle Then Return Null
' standardIO = TPipeStreamUTF8.Create(infd, outfd)
' errorIO = TPipeStreamUTF8.Create(errfd, outfd)
standardIO = TPipeStream.Create(infd, outfd)
errorIO = TPipeStream.Create(errfd, outfd)
if not standardIO then Throw "Failed to create standardIO stream"
if not errorIO then Throw "Failed to create errorIO stream"
processes.AddLast(Self)
Return Self
End Method
Function FlushZombies()
Local aliveList:TList = CreateList()
For Local p:TCodeTesterProcess = EachIn processes
If p.Alive()
aliveList.AddLast p
else
p.Terminate()
endif
Next
processes = aliveList
End Function
Method Eof:Int()
'process alive, so might still output something
If Alive() Then Return False
'still something to read?
If IOAvailable() Then Return False
Return True
End Method
Function TerminateAll() NoDebug
For Local p:TCodeTesterProcess = EachIn processes
p.Terminate()
Next
processes = Null
End Function
End Type
Type TPipeStreamUTF8 Extends TPipeStream
Method ReadPipe:Byte[]()
Local n:Int = ReadAvail()
If n
Local bytes:Byte[] = New Byte[n]
Read(bytes, n)
Return bytes
EndIf
End Method
Method FillBuffer:int()
Local available:int = ReadAvail()
'fill buffer
If available
'calc unused buffer size
If bufferpos + available > 4096 then available = 4096 - bufferpos
If available <=0 then RuntimeError "TPipeStreamUTF8 FillBuffer Overflow"
'adjust buffer position
bufferpos :+ Read(Varptr readbuffer[bufferpos], available)
EndIf
return available > 0
End Method
Method ReadUTF8FromBuffer:string(bufferOffset:int, size:int)
local result:string = ""
local c:int, d:int, e:int
For local offset:int = bufferOffset To bufferOffset + size
c = readbuffer[offset]
If c < 128
result :+ chr(c)
continue
EndIf
offset :+ 1
d = readbuffer[offset]
If c < 224
result :+ chr((c-192)*64 + (d-128) )
continue
EndIf
offset :+1
e = readbuffer[offset]
If c < 240
result :+ chr( (c-224)*4096 + (d-128)*64 + (e-128) ) + "Ä"
continue
EndIf
Next
return result
End Method
Method HasLine:int()
'fill read buffer with new data (if available)
FillBuffer()
For local n:int = 0 To bufferpos
'newline character found?
If readbuffer[n] = 10 then return True
Next
return False
End Method
Method ReadLine:String()
'fill read buffer with new data (if available)
FillBuffer()
Local p0:int, p1:int, line:string
'read in existing buffer
For local n:int = 0 To bufferpos
'newline character found?
'also generate a line if the buffer reached its end
If readbuffer[n] = 10 or n = bufferpos-1
p1 = n
p0 = 0
'skip double newlines (10,13)
if readbuffer[n] = 10
If (n>0) and readbuffer[n-1] = 13 then p1 = n - 1
If readbuffer[0] = 13 then p0 = 1
endif
If p1 > p0
line = ReadUTF8FromBuffer(p0, p1-p0)
'line = String.FromBytes(Varptr readbuffer[p0], p1 - p0)
EndIf
'advance to next char
n:+1
bufferpos:-n
If bufferpos then MemMove(readbuffer, Varptr readbuffer[n], Size_T(bufferpos))
Return line
EndIf
Next
End Method
rem
Method ReadChar:Int()
Local c:Int = ReadByte()
If c < 128 Then Return c
Local d:Int = ReadByte()
If c < 224 Then Return (c-192)*64 + (d-128)
Local e:Int = ReadByte()
If c < 240 Then Return (c-224)*4096 + (d-128)*64 + (e-128)
End Method
Method ReadLine:String()
If ReadAvail()
Local buf:Short[1024], i:Int
'somehow "Eof()" returns False albeit there is nothing
'to read - that is why we check ReadAvail() too
While Not Eof() And ReadAvail()
Local n:Int = ReadChar()
If n = 0 Then Exit
If n = 10 Then Exit
If n = 13 Then Continue
If buf.length = i Then buf = buf[..i+1024]
buf[i] = n
i:+1
Wend
Return String.FromShorts(buf, i)
EndIf
Return ""
End Method
end rem
Function Create:TPipeStreamUTF8( in:Int, out:Int )
Local stream:TPipeStreamUTF8 = New TPipeStreamUTF8
stream.readHandle = in
stream.writeHandle = out
Return stream
End Function
End Type