Author Topic: Pipe Console Output to QB64 program!  (Read 8373 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #15 on: October 27, 2020, 09:42:43 pm »
That was the trick! "./" worked. I also tried "/home/name/QB64/source/program/" and that didn't take either. The wiki was not exactly clear either, but I tried.

@Jesterdev Sorry! I'm so used to keeping my source and everything in the same folder as the executable so I just naturally assume others will as well! Hope it helps you do what you need! Please tag me if you end up posting some code that uses the library so I can see what you come up with!
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #16 on: November 01, 2020, 05:43:08 pm »
Updated pipecom library with new code specifically for Windows using the WinAPI that makes the output the same as what you would get on Linux and Mac with no console window.
Shuwatch!

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: Pipe Console Output to QB64 program!
« Reply #17 on: November 02, 2020, 03:10:19 pm »
Hello

Does not look like you can use /P in there I have been trying that but it just goes to the end of the data and ends. Is that what it is supposed to do

Badger

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #18 on: November 02, 2020, 04:20:10 pm »
@badger Can you be more specific? What is it you are trying to do? Can you post some code?
Shuwatch!

Offline mpgcan

  • Newbie
  • Posts: 26
    • View Profile
Re: Pipe Console Output to QB64 program!
« Reply #19 on: November 03, 2020, 03:44:01 am »
You use the /P switch to have the Command Prompt pause results after it displays each screen. You have to press a key to continue viewing the next page of results. The command prompt window is hidden because of this it cannot receive input hence you cannot use /P.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #20 on: November 03, 2020, 08:46:42 am »
You use the /P switch to have the Command Prompt pause results after it displays each screen. You have to press a key to continue viewing the next page of results. The command prompt window is hidden because of this it cannot receive input hence you cannot use /P.
@mpgcan @badger Yes, you cannot do a command that will require user input. The same is true for a regular SHELL. This is meant to run a command and grab immediate output. You cannot interact with the program you are calling.
Shuwatch!

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: Pipe Console Output to QB64 program!
« Reply #21 on: November 03, 2020, 03:54:05 pm »
Hello

Sorry just saw your post. there is what i mean. It does not give me the paging option it just goes to the end of the task and exits.

Badger

Code: QB64: [Select]
  1. DECLARE LIBRARY "pipecom"
  2.     FUNCTION pipecom$ (cmd AS STRING)
  3.  
  4.  
  5. 'F = pipecom("dir *.BAS")
  6. F = pipecom("dir /b /s /p*.BAS") 'if you need just the filenames and not the rest of the output
  7.  
  8.  
  9.  
  10.  

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #22 on: November 03, 2020, 03:55:59 pm »
@badger Yes, you won't be able to interact with the console. This behaves just like SHELL except that it returns the text from the screen. You cannot provide input after the command is ran. This is expected. If you want the entire directory then you would have to ask for the whole list whether you used pipecom or if you used SHELL.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #23 on: November 03, 2020, 04:15:26 pm »
Updated 04:14 PM on 11/03/2020

Fixed bug in pipecom where the buffer wasn't cleared between calls which could lead to duplication of data.
Shuwatch!

Offline mpgcan

  • Newbie
  • Posts: 26
    • View Profile
Re: Pipe Console Output to QB64 program!
« Reply #24 on: November 05, 2020, 05:47:00 am »
Hi Spriggsy
What an excellent solution using pipes. I have tried using these in the pass but always failed, never understood what bits to twiddle.

I downloaded the latest (05/11/2020 08:05) version of your pipecom library and it failed to compile on Windows 8.1.

No big deal, change file pipecom.bas to:
Code: QB64: [Select]
  1.  
  2. DECLARE LIBRARY "pipecom"
  3.     FUNCTION pipecom$ (cmd AS STRING)
  4.  
  5.  
  6. comm = pipecom$("dir /b *.BAS")
  7.  
  8. PRINT comm
  9.  
Compiled and run with no issues.

Looking at pipecom.h
Lines "int first;" and "int last;" are leftover code? delete these not required.

The small buffer size does not look right, proposal:

Replace this code:
Code: QB64: [Select]
  1.     char buf[2] = { }; //I know, the buffer is small but I had to in order to work with things like PING
  2.     DWORD dwRead = 0;
  3.     while (ReadFile(hStdOutPipeRead, &buf, 2, &dwRead, NULL) && dwRead)
  4.     {
  5.         buf[dwRead] = '\0';
  6.                 pipecom_buffer.append(buf);
  7.     }
  8.  
With the following code:
Code: QB64: [Select]
  1. //Note: ReadFile will return when buffer is full, or pipe is closed
  2. #define BUFFSZ 4096       //Seems to work here. Better to place at top of code
  3.     char buf[BUFFSZ + 1]; //+1 Space for null terminator
  4.     DWORD dwRead = 0;     //Number of bytes read. Automatically reset to 0 on each call to ReadFile
  5.     while (ReadFile(hStdOutPipeRead, buf, BUFFSZ, &dwRead, NULL))
  6.     {
  7.         buf[dwRead] = '\0';
  8.         pipecom_buffer.append(buf);
  9.     }
  10.  
Not sure what the issue you had with PING (buf[2]) however with the above changes I tried this code:
comm = pipecom$("ping google.com")
 it works.

All the best.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #25 on: November 05, 2020, 10:29:13 am »
Yes, you are correct about int first and int last. Those were pieces I didn't see when I uploaded it last. As for the buffer. I know when I had large buffer sizes that a ping would only return part of the console output. I had not tested it again after making the last changes to see if making a larger buffer would still work. Yes, it works. I had the buffer at 4096 at the beginning and it never worked correctly with ping unless I changed it to 2. So really all I'm going to do is undo some changes. I don't know why you would have had compilation errors. Did you look at the compilation log?
« Last Edit: November 05, 2020, 10:34:59 am by SpriggsySpriggs »
Shuwatch!

Offline mpgcan

  • Newbie
  • Posts: 26
    • View Profile
Re: Pipe Console Output to QB64 program!
« Reply #26 on: November 06, 2020, 03:49:44 am »
Hi Spriggsy
   The term I used was incorrect instead of "failed to compile" I should have said failed to run!
The code you currently have for pipecom.bas is:
Code: QB64: [Select]
  1.  
  2. DECLARE LIBRARY "pipecom"
  3.     FUNCTION pipecom (cmd AS STRING)
  4.  
  5.  
  6. comm = "dir /b *.BAS"
  7.  
  8. PRINT comm
  9.  

It should be changed to:

Code: QB64: [Select]
  1.  
  2. DECLARE LIBRARY "pipecom"
  3.     FUNCTION pipecom$ (cmd AS STRING)
  4.  
  5.  
  6. comm = pipecom$("dir /b *.BAS")
  7.  
  8. PRINT comm
  9.  

All the best,
Again thanks for the working code.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pipe Console Output to QB64 program!
« Reply #27 on: November 06, 2020, 11:36:15 am »
@mpgcan Yes, you are correct. The file in my upload was wrong. I think I actually accidentally uploaded that one specifically. Even so, the very first post in this thread is valid code. Did you give it a look? It shows the proper usage so that there is no guesswork.
Shuwatch!