Announcement

Collapse
No announcement yet.

Why no pipes?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why no pipes?

    The manual makes it clear that STDIN/STDOUT do not support piping. Was that perhaps just a performance decision?

    So that I am not forced to use C++ or Python for pipes, are there any examples of implementing pipes via API calls?
    Erich Schulman (KT4VOL/KTN4CA)
    Go Big Orange

  • #2
    See the 'ShellConsoleApp' function in David's code..
    PowerBASIC and related source code. Please do not post questions or discussions, just source code.


    That credits code posted by Semen (probably here)..
    Rgds, Dave

    Comment


    • #3
      Piping sends to STDIN of the target app.

      Both createprocess() and shellexecuteex() can start a process with an override for the STDIN (and/or STDOUT and/or STDERR) of that target process.

      However, it is the responsibility of the target application to use any overrides provided; the GetStartupInfo() or GetStdHandle() functions may be used to obtain the redirected standard handles.

      I have not tried with 5x, but I had problems with PB/CC up thru 4x respecting those overrides. I never had a problem when PB/Windows was used to create the child application.


      MCM
      Last edited by Michael Mattias; 11 Apr 2009, 10:18 AM.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        If I use GetStdHandle to get the stdin handle, will PB statements be able to use the handle, or do I need ReadConsoleInput to get what is being piped to me?
        Erich Schulman (KT4VOL/KTN4CA)
        Go Big Orange

        Comment


        • #5
          If your application is the target of an application which pipes its output to your application - that is, your STDIN - I don't think you would need to use GetStdHandle() at all.... I would think STDIN would work.... after all, aren't "CGI" applications based on this principle?

          You might try using one of the "CGI" samples provided with the compiler as the basis for a test program to see if that does the job.

          MCM

          PS: we "are" speaking of "command-line" piping here, right? eg...

          Code:
          C:\DOS>  ProgA.exe | progB.exe
          .. where "ProgB" is your program.

          Because if we are talking about Windows pipe objects, there is no problem at all using those with either flavor of compiler.
          Last edited by Michael Mattias; 11 Apr 2009, 05:10 PM.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            then again, if ...
            Code:
            C:\DOS>  ProgA.exe | progB.exe
            .. works, so would
            Code:
            C:\DOS>  ProgA.exe > Filename
            C:\DOS>  ProgB.exe < Filename
            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Ignoring the title, this...

              Wait for key, click or clock for PB/WIN and PB/CC

              ... shows you how to efficiently obtain and process input to your program's stdin.

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Originally posted by Michael Mattias View Post

                PS: we "are" speaking of "command-line" piping here, right? eg...

                Code:
                C:\DOS>  ProgA.exe | progB.exe
                .. where "ProgB" is your program.

                Because if we are talking about Windows pipe objects, there is no problem at all using those with either flavor of compiler.
                I want to be prepared to be anywhere in the pipeline, so I could be both source and target.

                Code:
                find (something) | sed (something) | myprog (something) | grep (something) > finalresult.txt
                Long pipelines are probably rare in Windows, but when you have Cygwin and Unix/Linux experience, you become more likely to use them.
                Erich Schulman (KT4VOL/KTN4CA)
                Go Big Orange

                Comment


                • #9
                  Code:
                  find (something) | sed (something) [COLOR="Red"]| myprog (something) |[/COLOR] grep (something) > finalresult.txt
                  I'm not sure you can use that syntax to be SOURCE (in this case to grep) but I'd sure give it a try by using one of the CGI samples as a starter program.

                  I'm pretty sure using STDIN as your input (like the CGI samples) will work when you are the target (from sed); on the source side, simply sending your output to STDOUT just might work, but that one I'm not so confident in. I know you could make it work using CreateProcess(), but that would not be fun to handle in the command-line at all, since now the "|" coming out of "myprog" would have to be treated as some kind of input parameter.

                  But I'd try the STDIN/STDOUT approach first anyway.

                  If you have PB/CC 4 or greater, I think I'd look at using the "no console" directive when compiling. IIRC the problems I encountered were that when a PB/CC program starts, it makes its console the STDIN/STDOUT regardless of other instructions received.

                  If I didn't have to do this other thing (that food, clothing, shelter thing) today, this sounds like a fun project.

                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    The docs say
                    Code:
                    Restrictions
                     STDIN LINE does not work with pipes.
                    Restrictions
                     STDOUT is not compatible with Pipes.  The current active page can be determined with the PAGEACTIVE function.
                    I started this thread because I took them at their word. I can't imagine why the powers that be would break such a basic function without some reason (like a substantial performance gain). I would have assumed STDIN and STDOUT to be as straightforward as cin/cout in C++. I do nothing special to accommodate piping in C++ and Python, yet it always works.

                    Could I possibly need a .dll that just exports cin and cout for use with PB? BTW, I do have both 4 and 5.
                    Erich Schulman (KT4VOL/KTN4CA)
                    Go Big Orange

                    Comment


                    • #11
                      Code:
                      'CLPIPE.BAS
                      ' test commandline piping
                      ' PBCC 5.0.1
                      ' MCM 4/13/09
                      
                      #COMPILE EXE
                      #CONSOLE OFF   ' <<< MAKES NO DIFFERENCE
                      #DIM ALL
                      
                      #INCLUDE "WIN32API.INC"
                      
                      FUNCTION PBMAIN () AS LONG
                      
                      LOCAL bFirst AS LONG
                      LOCAL a$
                      
                      LOCAL hPB AS LONG, hSys AS LONG, L AS LONG
                      
                      
                        hSys = GetStdHandle (%STD_INPUT_HANDLE)
                        hPB  = FREEFILE
                        OPEN HANDLE hSys FOR INPUT AS hPB
                        L = LOF(hPB)
                        LINE INPUT #hPB, a$
                        CLOSE hPB
                        hSys = GetStdHandle (%STD_OUTPUT_HANDLE)
                        hPB = FREEFILE
                        OPEN HANDLE hSys FOR OUTPUT AS hPB
                        PRINT #hPB, UCASE$(a$)
                        CLOSE hPB
                      
                      
                      END FUNCTION
                      Code:
                      D:\Software_Development\pbcc50\Work>echo abcdef |clpipe.exe | more
                      ABCDEF
                      
                      
                      D:\Software_Development\pbcc50\Work>echo abcdef |clpipe.exe | more
                      ABCDEF
                      
                      
                      D:\Software_Development\pbcc50\Work>
                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Better Still...

                        Code:
                        'CLPIPE.BAS
                        ' test commandline piping
                        
                        #COMPILE EXE
                        #CONSOLE OFF
                        #DIM ALL
                        
                        #INCLUDE "WIN32API.INC"
                        
                        FUNCTION PBMAIN () AS LONG
                        
                        LOCAL bFirst AS LONG
                        LOCAL a$
                        
                        LOCAL hPBI AS LONG, hPBO AS LONG, hSys AS LONG, L AS LONG
                        
                          hSys = GetStdHandle (%STD_INPUT_HANDLE)
                          hPBI  = FREEFILE
                          OPEN HANDLE hSys FOR INPUT AS hPBI
                          hSys = GetStdHandle (%STD_OUTPUT_HANDLE)
                          hPBO = FREEFILE
                          OPEN   HANDLE hSys FOR OUTPUT AS hPBO
                          WHILE NOT EOF(hPBI)
                             LINE INPUT #hPBI, a$
                             PRINT #hPBO, UCASE$(a$)
                          WEND
                          CLOSE hPBI
                          CLOSE hPBO
                        
                        END FUNCTION

                        Code:
                        D:\Software_Development\pbcc50\Work>echo abcdef |clpipe.exe | more
                        ABCDEF
                        
                        
                        D:\Software_Development\pbcc50\Work>echo abcdef |clpipe.exe | more
                        ABCDEF
                        
                        
                        D:\Software_Development\pbcc50\Work>dir *.* | clpipe.exe | more
                         VOLUME IN DRIVE D HAS NO LABEL.
                        
                        
                        D:\Software_Development\pbcc50\Work>dir *.* | clpipe.exe | more
                        
                        
                        D:\Software_Development\pbcc50\Work>dir *.* | clpipe.exe | more
                        VOLUME IN DRIVE D HAS NO LABEL.
                         VOLUME SERIAL NUMBER IS 049A-2E6F
                        
                         DIRECTORY OF D:\SOFTWARE_DEVELOPMENT\PBCC50\WORK
                        
                        04/13/2009  04:58 PM    <DIR>          .
                        04/13/2009  04:58 PM    <DIR>          ..
                        03/19/2009  11:35 AM    <DIR>          ADODEMO
                        11/10/2008  10:28 AM    <DIR>          BUGS
                        04/13/2009  04:58 PM               831 CLPIPE.BAK
                        04/13/2009  04:58 PM               838 CLPIPE.BAS
                        04/13/2009  04:58 PM            17,920 CLPIPE.EXE
                        04/13/2009  04:58 PM               610 CLPIPE.LOG
                        10/27/2008  10:37 AM             1,348 FREESPACE.BAK
                        10/27/2008  12:02 PM             1,377 FREESPACE.BAS
                        10/27/2008  10:37 AM            19,456 FREESPACE.EXE
                        10/27/2008  10:37 AM               621 FREESPACE.LOG
                        11/20/2008  10:40 AM             1,945 GP001.BAK
                        11/20/2008  10:40 AM             1,951 GP001.BAS
                        11/20/2008  10:41 AM            26,112 GP001.EXE
                        11/20/2008  10:41 AM               654 GP001.LOG
                        11/20/2008  10:41 AM               165 MEMTEST.BAK
                        11/20/2008  10:41 AM               153 MEMTEST.BAS
                        11/20/2008  10:41 AM             6,144 MEMTEST.DLL
                        11/20/2008  10:41 AM               560 MEMTEST.LOG
                        01/16/2009  10:54 AM            10,145 NETFINDCOMPUTERS.BAK
                        01/16/2009  11:04 AM            10,977 NETFINDCOMPUTERS.BAS
                        01/16/2009  10:54 AM            26,112 NETFINDCOMPUTERS.EXE
                        01/16/2009  10:54 AM               715 NETFINDCOMPUTERS.LOG
                        12/02/2008  01:38 PM                56 ORPHAN.TXT
                        12/02/2008  01:37 PM               653 ORPHANTHREAD.BAK
                        12/02/2008  01:38 PM               664 ORPHANTHREAD.BAS
                        12/02/2008  01:38 PM            15,360 ORPHANTHREAD.EXE
                        12/02/2008  01:38 PM               584 ORPHANTHREAD.LOG
                        03/20/2009  08:15 AM            27,537 PTMN_LAST_ORDER_NO.BAK
                        03/20/2009  12:36 PM            27,543 PTMN_LAST_ORDER_NO.BAS
                        03/20/2009  12:36 PM            76,800 PTMN_LAST_ORDER_NO.EXE
                        03/20/2009  12:36 PM               764 PTMN_LAST_ORDER_NO.LOG
                        03/19/2009  05:25 PM             2,203 PTMN_LAST_ORDER_NO_LOG.TXT
                        03/19/2009  03:42 PM               742 PTMN_LAST_ORDER_NO_LOG.TXT.BAK
                        10/09/2008  09:13 AM               283 RUNSTDERR.CMD
                        10/22/2008  09:42 AM             1,982 SHELLEE.BAK
                        10/22/2008  09:52 AM             1,986 SHELLEE.BAS
                        10/22/2008  09:52 AM            19,968 SHELLEE.EXE
                        10/22/2008  09:52 AM               571 SHELLEE.LOG
                        10/22/2008  09:50 AM             2,711 SHELLER.BAK
                        10/22/2008  09:52 AM             2,779 SHELLER.BAS
                        10/22/2008  09:52 AM            17,920 SHELLER.EXE
                        10/22/2008  09:52 AM               571 SHELLER.LOG
                        10/09/2008  09:26 AM            10,604 STDERR_BUG.ZIP
                        01/09/2009  12:57 PM               690 TEST_ARRAYSORT.BAK
                        01/09/2009  12:59 PM               702 TEST_ARRAYSORT.BAS
                        01/09/2009  12:59 PM            15,360 TEST_ARRAYSORT.EXE
                        01/09/2009  12:59 PM               591 TEST_ARRAYSORT.LOG
                        03/11/2009  11:40 AM               415 TEST_COSINE.BAK
                        03/11/2009  11:41 AM               415 TEST_COSINE.BAS
                        03/11/2009  11:43 AM            12,800 TEST_COSINE.EXE
                        03/11/2009  11:43 AM               580 TEST_COSINE.LOG
                        10/09/2008  09:21 AM               508 TEST_STDERR.BAK
                        10/09/2008  09:21 AM               531 TEST_STDERR.BAS
                        10/09/2008  09:21 AM            17,920 TEST_STDERR.EXE
                        10/09/2008  09:21 AM               626 TEST_STDERR.LOG
                        10/09/2008  09:22 AM                14 TEST_STDERR.TXT
                        10/09/2008  09:22 AM                13 TEST_STDOUT.TXT
                        10/13/2008  12:36 PM             5,107 TRANSNET1.BAK
                        10/13/2008  12:47 PM             5,101 TRANSNET1.BAS
                        10/13/2008  12:36 PM            35,328 TRANSNET1.EXE
                        10/13/2008  12:36 PM               622 TRANSNET1.LOG
                        10/13/2008  12:56 PM             1,975 TRANSNET2.BAK
                        10/13/2008  12:57 PM             2,006 TRANSNET2.BAS
                        10/13/2008  12:57 PM            29,184 TRANSNET2.EXE
                        10/13/2008  12:57 PM               622 TRANSNET2.LOG
                        10/27/2008  10:37 AM                13 ~DS
                        10/22/2008  09:52 AM               250 ~SHELLTEST.TXT
                                      65 FILE(S)        472,288 BYTES
                                       4 DIR(S)  62,216,282,112 BYTES FREE
                        
                        
                        
                        D:\Software_Development\pbcc50\Work>
                        Close enough?

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Even better still (well, better DEMO...)
                          Code:
                          'CLPIPE.BAS
                          ' test commandline piping
                          ' 4/13/09, 4/15/09 
                          ' PB/CC  5.0.1
                          
                          #COMPILE EXE
                          '#CONSOLE OFF  ' <<< does not make any difference if on or off  (if piping in only; required if NOT piping out)
                          #DIM ALL
                          
                          #INCLUDE "WIN32API.INC"
                          FUNCTION PBMAIN () AS LONG
                          
                          LOCAL bFirst AS LONG
                          LOCAL a$
                          
                          LOCAL hPBI AS LONG, hPBO AS LONG, hSys AS LONG, L AS LONG
                          
                            hSys = GetStdHandle (%STD_INPUT_HANDLE)
                            hPBI  = FREEFILE
                            OPEN HANDLE hSys FOR INPUT AS hPBI
                            hSys = GetStdHandle (%STD_OUTPUT_HANDLE)
                            hPBO = FREEFILE
                            OPEN   HANDLE hSys FOR OUTPUT AS hPBO
                            WHILE NOT EOF(hPBI)
                               LINE INPUT #hPBI, a$
                               'PRINT #hPBO, UCASE$(a$)
                               PRINT #hPBO, STRREVERSE$(A$)
                            WEND
                            CLOSE hPBI
                            CLOSE hPBO
                          
                          END FUNCTION
                          Code:
                          Microsoft Windows XP [Version 5.1.2600]
                          (C) Copyright 1985-2001 Microsoft Corp.
                          
                          
                          [B]D:\Software_Development\pbcc50\Work>dir clpipe.* | clpipe.exe[/B]
                          .lebal on sah D evird ni emuloV
                          F6E2-A940 si rebmuN laireS emuloV
                          
                          kroW\05ccbp\tnempoleveD_erawtfoS\:D fo yrotceriD
                          
                          KAB.epiplc 740,1             MA 81:80  9002/51/40
                          sab.epiplc 390,1             MA 91:80  9002/51/40
                          exe.epiplc 029,71            MA 91:80  9002/51/40
                          gol.epiplc 016               MA 91:80  9002/51/40
                          setyb 076,02         )s(eliF 4
                          eerf setyb 886,077,412,26  )s(riD 0
                          
                          
                          [B]D:\Software_Development\pbcc50\Work>dir clpipe.* | clpipe.exe | clpipe.exe[/B]
                          Volume in drive D has no label
                           Volume Serial Number is 049A-2E6F
                          
                           Directory of D:\Software_Development\pbcc50\Work
                          
                          04/15/2009  08:18 AM             1,047 clpipe.BAK
                          04/15/2009  08:19 AM             1,093 clpipe.bas
                          04/15/2009  08:19 AM            17,920 clpipe.exe
                          04/15/2009  08:19 AM               610 clpipe.log
                                         4 File(s)         20,670 bytes
                                         0 Dir(s)  62,214,770,688 bytes free
                          
                          
                          
                          D:\Software_Development\pbcc50\Work>
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Non-text input, you say? See...



                            ... and that one should work with text or non-text input.

                            I suppose I should kind of marry up all these examples and put a decent demo into the source code forum, huh?
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment

                            Working...
                            X