Fresh IDE . Check-in [17ffd16cb7]
Not logged in

This repository is a mirror!

The original is located on: https://fresh.flatassembler.net/fossil/repo/fresh
If you want to follow the project, please update your remote-url

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:The Exec2 function is now the main. The old Exec is a wrapper of Exec2. Implemented for Win32 and Linux.
Timelines: family | ancestors | descendants | both | FreshLibDev
Files: files | file ages | folders
SHA1:17ffd16cb71cefd709c474177fca3b17dfcdace4
User & Date: johnfound 2018-10-18 13:31:32
Context
2018-10-19
13:53
CreatePipe and WaitProcessExit for Linux implemented. Fixed a small issue in Exec2 for Linux. check-in: cd025fbff5 user: johnfound tags: FreshLibDev
2018-10-18
13:31
The Exec2 function is now the main. The old Exec is a wrapper of Exec2. Implemented for Win32 and Linux. check-in: 17ffd16cb7 user: johnfound tags: FreshLibDev
2018-10-17
12:03
Work on the new Exec2 function for Linux that allows changing of the std i/o handles. It is implemented in _process_posix.asm, but uses only system calls and the same implementation will be OK for the _process_native.asm; check-in: f828bb2bc5 user: johnfound tags: FreshLibDev
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to freshlib/system/Linux/_process_posix.asm.

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

body FreeThreadID
begin
        return
endp


body Exec
begin
        stdcall Exec2, .hCommand, [STDIN], [STDOUT], [STDERR]
        return
endp



body Exec2
.pArgs dd ?
begin
        pushad

        DebugMsg "Exec2 procedure!!!"

; first handle the .hCommand

        stdcall StrSplitArg, [.hCommand]
        mov     [.pArgs], eax

        mov     eax, sys_fork
        int     $80

        OutputValue "Fork returns: ", eax, -1, 10

        test    eax, eax
        jnz     .parent         ; this is the parent process

; here is the child.

        mov     eax, sys_dup2
        mov     ebx, [.hStdIn]
        mov     ecx, [STDIN]
        int     $80

        OutputValue "Dup2 returns: ", eax, -1, 10

        mov     ebx, [.hStdOut]
        mov     ecx, [STDOUT]
        int     $80

        OutputValue "Dup2 returns: ", eax, -1, 10

        mov     ebx, [.hStdErr]
        mov     ecx, [STDERR]
        int     $80

        OutputValue "Dup2 returns: ", eax, -1, 10

        mov     ecx, [.pArgs]
        mov     ebx, [ecx]                      ; the first argument is the executable name.

        mov     edx, [_ptrEnvironment]
        mov     eax, [edx]                      ; argument count
        lea     edx, [edx + 4*eax + 8]          ; +4 for argc and +4 for the termination 0; edx points to the env table here.

        mov     eax, sys_execve
        int     $80

        stc
        mov     [esp+4*regEAX], eax
        popad
        return

.parent:
        stdcall FreeMem, [.pArgs]
        bt      eax, 31
        mov     [esp+4*regEAX], eax
        popad
        return
endp



; MUTEX functions
;_________________________________________________________________________________________


;struct TMutex







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







38
39
40
41
42
43
44








































































45
46
47
48
49
50
51

body FreeThreadID
begin
        return
endp












































































; MUTEX functions
;_________________________________________________________________________________________


;struct TMutex

Changes to freshlib/system/Linux/process.asm.

267
268
269
270
271
272
273




274


275






















































































































































276
277
278
279
280
281
282
        retn


end if







































































































































































match =LinuxThreads, LinuxThreads {







>
>
>
>

>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
        retn


end if



body Exec2
.pArgs dd ?
begin
        pushad

        stdcall StrSplitArg, [.hCommand]
        mov     [.pArgs], eax

        mov     eax, sys_fork
        int     $80

        test    eax, eax
        jnz     .parent         ; this is the parent process

; here is the child.

        mov     eax, sys_dup2
        mov     ebx, [.hStdIn]
        mov     ecx, [STDIN]
        int     $80

        mov     eax, sys_dup2
        mov     ebx, [.hStdOut]
        mov     ecx, [STDOUT]
        int     $80

        mov     eax, sys_dup2
        mov     ebx, [.hStdErr]
        mov     ecx, [STDERR]
        int     $80

        mov     ecx, [.pArgs]
        mov     ebx, [ecx]                      ; the first argument is the executable name.

        mov     edx, [_ptrEnvironment]
        mov     eax, [edx]                      ; argument count
        lea     edx, [edx + 4*eax + 8]          ; +4 for argc and +4 for the termination 0; edx points to the env table here.

        mov     eax, sys_execve
        int     $80

        stc
        mov     [esp+4*regEAX], eax
        popad
        return

.parent:
        stdcall FreeMem, [.pArgs]
        bt      eax, 31
        mov     [esp+4*regEAX], eax
        popad
        return
endp



; Splits the string on spaces and forms one-memory-block area that contains, zero terminated
; array of pointers to every part of the string.
; The spaces inside double or single quotes are ignored.
;
; The format of the memory block is the same as used in the sys_execve system function.

proc StrSplitArg, .hCommand
begin
        pushad

if defined StrLib & StrLib
        stdcall StrPtr, [.hCommand]
        mov     esi, eax
        mov     ebx, eax
else
        mov     esi, [.hCommand]
        mov     ebx, esi
end if
        xor     ecx, ecx
        xor     edx, edx

; find the maximal possible count of the arguments (edx)
; and the length of the string.

.loop1:
        lodsb
        test    al, al
        jz      .endstr1
        inc     ecx
        cmp     al, ' '
        jne     .loop1
        inc     edx
        jmp     .loop1

.endstr1:
        mov     esi, ebx

        add     edx, 3
        lea     edx, [4*edx + ecx]
        add     edx, 3
        and     edx, $fffffffc

        stdcall GetMem, edx
        mov     ebx, eax

        sub     edx, ecx
        sub     edx, 4
        and     edx, $fffffffc

        lea     edi, [ebx+edx]
        rep movsb

        mov     edi, ebx
        lea     esi, [ebx+edx]  ; start of the string.

.search_loop:
        mov     [edi], esi
        add     edi, 4

.loop_to_space:
        lodsb
        test    al, al
        jz      .end_of_string

        cmp     al, '"'
        je      .skip_quoted

        cmp     al, "'"
        je      .skip_quoted

        cmp     al, ' '
        jne     .next

        cmp     ah, ' '
        je      .loop_to_space

        mov     byte [esi-1], 0
        jmp     .search_loop

.next:
        mov     ah, al
        jmp     .loop_to_space


.skip_quoted:
        mov     ah, al

.loop_quote:
        lodsb
        test    al, al
        jz      .end_of_string

        cmp     al, ah
        jne     .loop_quote
        jmp     .loop_to_space


.end_of_string:
        mov     [esp+4*regEAX], ebx
        popad
        return
endp






match =LinuxThreads, LinuxThreads {

Changes to freshlib/system/Win32/process.asm.

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
        pop     edx ecx eax
        return
endp




body Exec
.startup STARTUPINFO
.process PROCESS_INFORMATION
begin
        push    ecx esi edi

        stdcall utf8ToWideChar, [.hCommand]
        mov     esi, eax
................................................................................

        lea     edi, [.startup]
        mov     ecx, (sizeof.STARTUPINFO + sizeof.PROCESS_INFORMATION)/4
        xor     eax, eax
        rep     stosd

        mov     [.startup.cb], sizeof.STARTUPINFO








        lea     ecx, [.startup]
        lea     edx, [.process]
        invoke  CreateProcessW, 0, esi, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, ecx, edx
        stdcall FreeMem, esi
        test    eax, eax
        jz      .error

        mov     eax, [.process.hProcess]
        mov     edx, [.process.hThread]








|







 







>
>
>
>
>
>
>



|







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
        pop     edx ecx eax
        return
endp




body Exec2
.startup STARTUPINFO
.process PROCESS_INFORMATION
begin
        push    ecx esi edi

        stdcall utf8ToWideChar, [.hCommand]
        mov     esi, eax
................................................................................

        lea     edi, [.startup]
        mov     ecx, (sizeof.STARTUPINFO + sizeof.PROCESS_INFORMATION)/4
        xor     eax, eax
        rep     stosd

        mov     [.startup.cb], sizeof.STARTUPINFO
        mov     [.startup.dwFlags], STARTF_USESTDHANDLES
        mov     eax, [.hStdIn]
        mov     ecx, [.hStdOut]
        mov     edx, [.hStdErr]
        mov     [.startup.hStdInput], eax
        mov     [.startup.hStdOutput], ecx
        mov     [.startup.hStdError], edx

        lea     ecx, [.startup]
        lea     edx, [.process]
        invoke  CreateProcessW, 0, esi, 0, 0, TRUE, NORMAL_PRIORITY_CLASS, 0, 0, ecx, edx
        stdcall FreeMem, esi
        test    eax, eax
        jz      .error

        mov     eax, [.process.hProcess]
        mov     edx, [.process.hThread]

Changes to freshlib/system/process.asm.

27
28
29
30
31
32
33
34


35
36
37
38
39
40
41
..
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


interface ThreadCreate, .ptrFunction, .ptrArguments

interface FreeThreadID, .ThreadID


interface Exec, .hCommand


interface Exec2, .hCommand, .hStdIn, .hStdOut, .hStdErr

interface FreeProcessID, .ProcessID

interface WaitProcessExit, .ProcessID, .timeout


................................................................................
interface WaitForMutex, .ptrMutex, .timeout

interface MutexRelease, .ptrMutex

interface MutexDestroy, .ptrMutex






proc StrSplitArg, .hString
.pRes dd ?
.strlen dd ?
begin
        pushad

        stdcall StrLen, [.hString]
        mov     [.strlen], eax

        stdcall StrPtr, [.hString]
        mov     esi, eax
        mov     edi, eax
        xor     edx, edx

.loop1:
        lodsb
        test    al, al
        jz      .endstr1
        cmp     al, ' '
        jne     .loop1
        inc     edx
        jmp     .loop1

.endstr1:
        mov     esi, edi
        mov     ecx, [.strlen]

        add     edx, 3
        lea     edx, [4*edx + ecx]
        add     edx, 3
        and     edx, $fffffffc

        stdcall GetMem, edx
        mov     [.pRes], eax

        sub     edx, ecx
        sub     edx, 4
        and     edx, $fffffffc

        lea     edi, [eax+edx]
        rep movsb

        mov     edi, [.pRes]
        lea     esi, [edi+edx]  ; start of the string.

.search_loop:
        mov     [edi], esi
        add     edi, 4

.loop_to_space:
        lodsb
        test    al, al
        jz      .end_of_string

        cmp     al, '"'
        je      .skip_quoted

        cmp     al, "'"
        je      .skip_quoted

        cmp     al, ' '
        jne     .next

        cmp     ah, ' '
        je      .loop_to_space

        mov     byte [esi-1], 0
        jmp     .search_loop

.next:
        mov     ah, al
        jmp     .loop_to_space


.skip_quoted:
        mov     ah, al

.loop_quote:
        lodsb
        test    al, al
        jz      .end_of_string

        cmp     al, ah
        jne     .loop_quote
        jmp     .loop_to_space


.end_of_string:

        mov     eax, [.pRes]
        mov     [esp+4*regEAX], eax
        popad

        return
endp







include '%TargetOS%/process.asm'








|
>
>







 







>
>

<
<
|
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>


<
<







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
..
47
48
49
50
51
52
53
54
55
56


57

58
























































































59
60
61


62
63
64
65
66
67
68


interface ThreadCreate, .ptrFunction, .ptrArguments

interface FreeThreadID, .ThreadID


; Executes the command .hCommand, gets the STDIN from [.hStdIn] file handle,
; Outputs the STDOUT to [.hStdOut] file handle and STDERR to [.hStdErr] handle.

interface Exec2, .hCommand, .hStdIn, .hStdOut, .hStdErr

interface FreeProcessID, .ProcessID

interface WaitProcessExit, .ProcessID, .timeout


................................................................................
interface WaitForMutex, .ptrMutex, .timeout

interface MutexRelease, .ptrMutex

interface MutexDestroy, .ptrMutex


; Simple wrapper on Exec2;
; See Exec2



proc Exec, .hCommand

begin
























































































        stdcall Exec2, .hCommand, [STDIN], [STDOUT], [STDERR]
        return
endp







include '%TargetOS%/process.asm'

Changes to freshlib/test_code/TestPThreads.asm.

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
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console

options.DebugMode = 1

include "%lib%/freshlib.asm"


; include your includes here.

iglobal
  Stop dd 0
endg

start:
        InitializeAll

        stdcall Exec2, "/usr/bin/ls --color=auto --group-directories-first", 0, 1, 2








        jmp     .finish

        stdcall ThreadCreate, TestThread, 0

.wait:
        stdcall Sleep, 100
        cmp     [Stop], 0







|













<

>
>
>
>
>
>
>







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
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console

options.DebugMode = 0

include "%lib%/freshlib.asm"


; include your includes here.

iglobal
  Stop dd 0
endg

start:
        InitializeAll



        stdcall FileOpenAccess, "./TestPThreads.asm", faReadOnly
        mov     ebx, eax

        stdcall FileOpenAccess, "./out.txt", faWriteOnly or faCreateAlways
        mov     edi, eax

        stdcall Exec2, '/usr/bin/grep "stdcall Exec2"', ebx, edi, edi   ; Runs in WINE as well, but will not in the original Windows.
        jmp     .finish

        stdcall ThreadCreate, TestThread, 0

.wait:
        stdcall Sleep, 100
        cmp     [Stop], 0

Changes to freshlib/test_code/TestPThreads.fpr.

cannot compute difference between binary files