Recent

Author Topic: How to get a pipe input?  (Read 5074 times)

Alex Cones

  • Jr. Member
  • **
  • Posts: 73
    • FLSoft
How to get a pipe input?
« on: March 12, 2012, 05:15:53 am »
Hello!

I wrote a console program, which draw a table. How I can get a pipe input for my program like this:
Code: [Select]
cat blah | tabler -s=x8x?
To beer or not to beer?
_____________________
I`m terribly sorry for my Anglish. :)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to get a pipe input?
« Reply #1 on: March 12, 2012, 06:17:56 am »
Alex, see the wiki page on executing external processes...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Alex Cones

  • Jr. Member
  • **
  • Posts: 73
    • FLSoft
Re: How to get a pipe input?
« Reply #2 on: March 12, 2012, 06:38:25 am »
Hmm.. It looks like what I need, but what I need to do with TProcess.CommandLine, if I need only to read pipe-out? Like this:
Code: [Select]
cat README | myprogramname
To beer or not to beer?
_____________________
I`m terribly sorry for my Anglish. :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to get a pipe input?
« Reply #3 on: March 12, 2012, 07:16:06 am »
Quote
Hmm.. It looks like what I need, but what I need to do with TProcess.CommandLine, if I need only to read pipe-out? Like this:
TProcess.CommandLine is the main binary to execute and the parameters ('cat README' in your case). You could read the output from TProcess.Output property, which then you feed into myprogramname (started with another TProcess instance) via TProcess.Input.

Alex Cones

  • Jr. Member
  • **
  • Posts: 73
    • FLSoft
Re: How to get a pipe input?
« Reply #4 on: March 12, 2012, 07:40:16 am »
Quote
TProcess.CommandLine is the main binary to execute and the parameters ('cat README' in your case). You could read the output from TProcess.Output property, which then you feed into myprogramname (started with another TProcess instance) via TProcess.Input.
Looks like my English is worse, than i thought. I don`t need tor run anything else. I just need to get the information, that will be send to me. It mfy be cat, or grep after hundred of filters.
To beer or not to beer?
_____________________
I`m terribly sorry for my Anglish. :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to get a pipe input?
« Reply #5 on: March 12, 2012, 04:50:05 pm »
Quote
Looks like my English is worse, than i thought. I don`t need tor run anything else. I just need to get the information, that will be send to me. It mfy be cat, or grep after hundred of filters.
Sorry, I misunderstood then. I assume that you want to read something in your program which is inputted via pipe? Am I right? If so, then it's just standard input, you could read via Read(Ln) or use THandleStream with StdInputHandle as constructor argument.

Alex Cones

  • Jr. Member
  • **
  • Posts: 73
    • FLSoft
Re: How to get a pipe input?
« Reply #6 on: March 12, 2012, 05:10:33 pm »
Quote
Sorry, I misunderstood then.
It`s my bad.
Quote
I assume that you want to read something in your program which is inputted via pipe? Am I right?
Yes it does!
Quote
If so, then it's just standard input, you could read via Read(Ln)
Thank you! It`s working!

But I have one more question. How to read this data, while this isn`t over?

e.g:
Code: [Select]
[20:09:19][alex][~]: cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family   : 6
model      : 23
model name   : Pentium(R) Dual-Core CPU       T4400  @ 2.20GHz
stepping   : 10
cpu MHz      : 1200.000
cache size   : 1024 KB
fdiv_bug   : no
hlt_bug      : no
f00f_bug   : no
coma_bug   : no
fpu      : yes
fpu_exception   : yes
cpuid level   : 13
wp      : yes
flags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm dts
bogomips   : 4388.80
clflush size   : 64
cache_alignment   : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:
Code: [Select]
program project1;

{$mode objfpc}{$H+}

{$R *.res}
Var
  S : String;

begin
 Read(S);
 WriteLn('!!! ' + S + ' !!!');

end. 
Code: [Select]
[20:09:19][alex][~]: cat /proc/cpuinfo | ./project1
!!! processors : 0 !!!
To beer or not to beer?
_____________________
I`m terribly sorry for my Anglish. :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to get a pipe input?
« Reply #7 on: March 13, 2012, 03:33:38 am »
I don't really know the semantic of Read(<string variable>), for me it never goes right, so I always use ReadLn for string. Use:
Code: [Select]
while not EOF do begin
  ReadLn(S);
  // do something with S
end;
to process until end of input

 

TinyPortal © 2005-2018