2

以前に C/C++ でパイプを使用したことがありますが、レガシー Ada コードでパイプを設定する作業を行っています...しかし、私はまだ Ada の「学習」段階にあり、まだ知らないことがたくさんあります。 .

そうは言っても、私は Ada のパイプがどのようにセットアップされ、どのように使用するかを理解しようとしています。これまでのところ、次の記事しか見つかりませんでした。

  1. Unix の子プロセスとパイプのための厚い ADA 95 バインディング
  2. パッケージ: Util.Pipes
  3. パイプ - ada-util

誤解しないでほしいのですが、彼らには多くの優れた知識が含まれていますが、1 は Ada95 を対象としており (Ada05 までコンパイルする能力があります)、2 は機能をリストするだけで、3 は説明の方法をほとんど提供していません。

Adaパイプのチュートリアルを知っている人はいますか? または、Ada で単純なパイプを実装する方法の簡単な例を誰か教えていただけないでしょうか?

これが最も理想的な質問ではないことは承知していますが、「Google の組み合わせ」が不足しています...

4

5 に答える 5

2

コード

GNAT GPL 2015 を使用して Windows 10 でコンパイル

with Ada.Text_IO;
with System;
with Interfaces.C;

procedure Main is

   package Pipes is
      type Pipe is private;
      type Get_Result is private;
      function Open_Read (Command : String) return Pipe;
      procedure Close (Stream : Pipe);
      function Get (Stream : Pipe) return Get_Result;
      function End_Of_File (Item : Get_Result) return Boolean;
      function To_Ada (Item : Get_Result) return Character;
   private
      use System;
      use Interfaces.C;
      type Pipe is new Address;
      type Get_Result is new int;
   end;

   package body Pipes is
      function popen (command : char_array; mode : char_array) return Address with Import, Convention => C, External_Name => "popen";
      function pclose (stream : Address) return int with Import, Convention => C, External_Name => "pclose";
      function fgetc (stream : Address) return int with Import, Convention => C, External_Name => "fgetc";
      function Open_Read (Command : String) return Pipe is
         Mode : constant char_array := "r" & nul;
         Result : Address;
      begin
         Result := popen (To_C (Command), Mode);
         if Result = Null_Address then
            raise Program_Error with "popen error";
         end if;
         return Pipe (Result);
      end;
      procedure Close (Stream : Pipe) is
         Result : int;
      begin
         Result := pclose (Address (Stream));
         if Result = -1 then
            raise Program_Error with "pclose error";
         end if;
      end;
      function Get (Stream : Pipe) return Get_Result is
      begin
         return Get_Result (fgetc (Address (Stream)));
      end;
      function End_Of_File (Item : Get_Result) return Boolean is (Item = -1);
      function To_Ada (Item : Get_Result) return Character is (Character'Val (Get_Result'Pos (Item)));
   end;

   procedure Test is
      use Ada.Text_IO;
      use Pipes;
      P : Pipe;
      C : Get_Result;
   begin
      P := Open_Read ("netstat");
      loop
         C := Get (P);
         exit when End_Of_File (C);
         Put (To_Ada (C));
      end loop;
      Close (P);
   end;

begin
   Test;
end;

出力

出力はユーザーごとに異なります。

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    192.168.1.140:49698    stackoverflow:https    ESTABLISHED
  TCP    192.168.1.140:49874    stackoverflow:https    ESTABLISHED
  TCP    192.168.1.140:49915    stackoverflow:https    TIME_WAIT
  TCP    192.168.1.140:49916    stackoverflow:https    TIME_WAIT
于 2016-03-13T23:26:18.550 に答える