|
Post by rikky on Oct 28, 2023 8:35:41 GMT 1
Hello The quest is HOW to know if a program in a pipe, that feeds its output to your baconprogram has ended? For example I have this function : FUNCTION GET_STDIN$() LOCAL string$ LOCAL STDIN WHILE TRUE STDIN = WAIT(STDIN_FILENO, 50) IF STDIN = 0 THEN BREAK string$ = string$ & CHR$(STDIN) WEND RETURN string$ END FUNCTION which I use in my program, lets call it PRINT_STDIN.bac WHILE TRUE string$ = CHOP$(GET_STDIN$()) IF string$ = "" THEN CONTINUE PRINT string$ WEND And I have another program, lets call it FEED_STDIN.bac PRINT "hallo baconworld." SLEEP 3000 PRINT "dag baconworld." So if I do on the commandline : FEED_STDIN | PRINT_STDIN Then it does the job. It prints "hallo baconworld.", sleeps 3 seconds, and then it prints "dag baconworld.". And then it waits forever for new input. But FEED_STDIN has ended. HOW ?? to determine that the feeding program has ended? I've searched the internet for years, looking for a solution. Now I've got it. STDIN = WAIT(STDIN_FILENO, 50) returns immediately, if there is no input anymore. It does not wait 50 miliseconds. So my new program becomes: GLOBAL EOI : ' END OF INPUT
FUNCTION GET_STDIN$() LOCAL string$ LOCAL STDIN LOCAL iter EOI = 0 WHILE TRUE iter = TIMER STDIN = WAIT(STDIN_FILENO, 50) IF STDIN = 0 THEN IF TIMER - iter < 2 THEN EOI = 1 BREAK END IF string$ = string$ & CHR$(STDIN) WEND RETURN string$ END FUNCTION WHILE TRUE string$ = CHOP$(GET_STDIN$()) IF string$ = "" THEN IF EOI THEN END 0 CONTINUE END IF PRINT string$ WEND
And now it does the job as it should. Rik.
|
|
|
Post by Pjot on Oct 29, 2023 5:19:15 GMT 1
Thanks rikky, The WAIT function, which is based on the libc select function, indeed is often overseen. In combination with STDIN_FILENO it can be a very powerful function to process input from stdin. The internal BaCon editor also uses the WAIT function heavily for this purpose. Best regards Peter
|
|