Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Cross-Platform-Entwicklung (https://www.delphipraxis.net/91-cross-platform-entwicklung/)
-   -   MacOS ShellExecute (https://www.delphipraxis.net/210237-macos-shellexecute.html)

Peter666 22. Mär 2022 16:21

MacOS ShellExecute
 
Hi,

ich habe in einem älteren Projekt folgendes gemacht um einen Befehl im Terminal auszuführen und mir die Ausgabe in TStrings zu speichern:

Delphi-Quellcode:
type
  PIOFile = Pointer;

// Create a new stream connected to a pipe running the given command.
function popen(const Command: MarshaledAString; Modes: MarshaledAString): PIOFile; cdecl;
   external libc name _PU +  '_popen';
{$EXTERNALSYM popen}
// Close a stream opened by popen and return the status of its child.
function pclose(Stream: PIOFile): Integer; cdecl; external libc name _PU +  '_pclose';
{$EXTERNALSYM pclose}

// Return the EOF indicator for STREAM.
function feof(Stream: PIOFile): Integer; cdecl; external libc name _PU + '_feof';
{$EXTERNALSYM feof}

// Read chunks of generic data from STREAM.
function fread(Ptr: Pointer; Size: LongWord; N: LongWord; Stream: PIOFile)
  : LongWord; cdecl; external libc name _PU + '_fread';
{$EXTERNALSYM fread}

// Wait for a child to die. When one does, put its status in *STAT_LOC
// and return its process ID. For errors, return (pid_t) -1.
function wait(__stat_loc: PInteger): Integer; external libc name _PU + '_wait';
{$EXTERNALSYM wait}

procedure ExecCmdline(const CmdLine: string; CmdResult: TStrings);
var
  Output: PIOFile;
  Buffer: PAnsiChar;
  TempString: AnsiString;
  Line: AnsiString;
  BytesRead: Integer;
const
  BufferSize: Integer = 1000;
begin
  TempString := '';
  CmdResult.Clear;

  Output := popen(PAnsiChar(AnsiString(CmdLine)), 'r');
  GetMem(Buffer, BufferSize);

  if Assigned(Output) then
    try
      while feof(Output) = 0 do
      begin
        BytesRead := fread(Buffer, 1, BufferSize, Output);
        SetLength(TempString, Length(TempString) + BytesRead);
        Move(Buffer^, TempString[Length(TempString) - (BytesRead - 1)],
          BytesRead);

        while Pos(AnsiChar(#10), TempString) > 0 do
        begin
          Line := Copy(TempString, 1, Pos(AnsiChar(#10), TempString) - 1);
          if CmdResult <> nil then
            CmdResult.Add(UTF8ToString(Line));

          TempString := Copy(TempString, Pos(AnsiChar(#10), TempString) + 1,
            Length(TempString));
        end;
      end;
    finally
      pclose(Output);
      wait(nil);
      FreeMem(Buffer, BufferSize);
    end;
end;
Das scheint aber in BigSur nicht mehr zu gehen, da die Pipe Befehle nicht mehr in der libc Bibliothek vorhanden sind. Gibt es da eine Alternative?

Peter.


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:35 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz