Italian community of Lazarus and Free Pascal

Programmazione => Generale => Topic aperto da: xinyiman - Agosto 13, 2014, 09:27:51 pm

Titolo: output TProcess in Stream
Inserito da: xinyiman - Agosto 13, 2014, 09:27:51 pm
Ragazzi io cerco di ottenere il risultato di un comando linux all'interno di una stream, ma seguendo l'esempio a questa pagina non funziona

http://wiki.freepascal.org/Executing_External_Programs/it

chi mi sa dire perchè?!

Codice: [Seleziona]
program project1;
{
    Copyright (c) 2004 by Marc Weustink

    This example is creeated in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

uses
  Classes, Process, SysUtils;

const
  READ_BYTES = 2048;

var
  S: TStringList;
  M: TMemoryStream;
  P: TProcess;
  n: LongInt;
  BytesRead: LongInt;

begin
  // We cannot use poWaitOnExit here since we don't
  // know the size of the output. On Linux the size of the
  // output pipe is 2 kB. If the output data is more, we
  // need to read the data. This isn't possible since we are
  // waiting. So we get a deadlock here.
  //
  // A temp Memorystream is used to buffer the output

  M := TMemoryStream.Create;
  BytesRead := 0;

  P := TProcess.Create(nil);
  P.CommandLine := 'cat /etc/passwd | grep /home | cut -d: -f1 ';
  P.Options := [poUsePipes];
  WriteLn('-- executing --');
  P.Execute;
  while P.Running do
  begin
    // make sure we have room
    M.SetSize(BytesRead + READ_BYTES);

    // try reading it
    n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
    if n > 0
    then begin
      Inc(BytesRead, n);
      Write('.')
    end
    else begin
      // no data, wait 100 ms
      Sleep(100);
    end;
  end;
  // read last part
  repeat
    // make sure we have room
    M.SetSize(BytesRead + READ_BYTES);
    // try reading it
    n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
    if n > 0
    then begin
      Inc(BytesRead, n);
      Write('.');
    end;
  until n <= 0;
  if BytesRead > 0 then WriteLn;
  M.SetSize(BytesRead);
  WriteLn('-- executed --');

  S := TStringList.Create;
  S.LoadFromStream(M);
  WriteLn('-- linecount = ', S.Count, ' --');
  for n := 0 to S.Count - 1 do
  begin
    WriteLn('| ', S[n]);
  end;
  WriteLn('-- end --');
  S.Free;
  P.Free;
  M.Free;
end.
Titolo: Re:output TProcess in Stream
Inserito da: Stilgar - Agosto 13, 2014, 10:18:53 pm
In debug cosa dice?

Stilgar
Titolo: Re:output TProcess in Stream
Inserito da: bonmario - Agosto 14, 2014, 07:28:34 am
Ragazzi io cerco di ottenere il risultato di un comando linux all'interno di una stream, ma seguendo l'esempio a questa pagina non funziona


Cosa intendi per "non funziona": non c'è nessun output o non hai l'output completo?
Quale comando stai lanciando?

Ciao, Mario
Titolo: Re:output TProcess in Stream
Inserito da: SB - Agosto 14, 2014, 11:23:44 am
Il cat su un file di testo funziona e ritorna correttamente il contenuto
Evidentemente c'è un problema nel comando lanciato

Ciao
Titolo: Re:output TProcess in Stream
Inserito da: xinyiman - Agosto 14, 2014, 12:32:24 pm
Se faccio un redirect su file dell'output mi escono i nomi utente presenti sul sistema linux. Se provo a ridirigere l'output direttamente su stream non funziona. Non capisco perchè, io vorrei bypassare l'uso del file. Tutto li
Titolo: Re:output TProcess in Stream
Inserito da: bonmario - Agosto 14, 2014, 02:09:05 pm
Potresti postare il comando che lanci? Oppure non è un comando si sistema?

Ciao, Mario
Titolo: Re:output TProcess in Stream
Inserito da: SB - Agosto 14, 2014, 02:33:26 pm
Fino al grep funziona. Per il resto puoi farlo da programma che probabilmente fai prima.
Titolo: Re:output TProcess in Stream
Inserito da: xinyiman - Agosto 14, 2014, 04:24:35 pm
Fino al grep funziona. Per il resto puoi farlo da programma che probabilmente fai prima.

Già, noto ora che hai ragione. Ma non capisco il perchè
Titolo: Re:output TProcess in Stream
Inserito da: bonmario - Agosto 14, 2014, 04:56:31 pm
Potresti postare il comando che lanci? Oppure non è un comando si sistema?

Ciao, Mario


Scusami, mi sono accorto solo ora che il comando era nel codice che hai postato ...
Titolo: Re:output TProcess in Stream
Inserito da: bonmario - Agosto 14, 2014, 04:59:49 pm
Magari può esserti utile: http://forum.lazarus.freepascal.org/index.php/topic,22367.msg131863.html#msg131863

Ciao, Mario
Titolo: Re:output TProcess in Stream
Inserito da: xinyiman - Agosto 14, 2014, 05:07:20 pm
Magari può esserti utile: http://forum.lazarus.freepascal.org/index.php/topic,22367.msg131863.html#msg131863

Ciao, Mario
Grazie or vedo in che direzione muovermi