🖥 OS.IO.Cmd Command Line - JulTob/Ada GitHub Wiki

Okay, you have learned how to have your application “communicate” using files. This is a good start, but we can do better. How about sending actual commands to the operating system itself? What if you would like to display the contents of the directory where your application is running? Let’s see how this can be done:

talk_to_os.adb:
with Ada.Text_IO;
with GNAT.OS_Lib;
procedure Talk_To_OS is
  function OS_Command(
    Command   : in String;
    Arguments : in String)
      return Integer is
    Return_Value : Integer := 0;
    Arguments_List : GNAT.OS_Lib.Argument_List :=
       (1 => new String'(Command),
        2 => new String'(Arguments));
    use type GNAT.OS_Lib.File_Descriptor;
    File_Descriptor : GNAT.OS_Lib.File_Descriptor := GNAT.OS_Lib.Standout;
  begin

    GNAT.OS_Lib.Spawn(
      Program_Name           => Command,
      Args                   => Arguments_List,
      Output_File_Descriptor => File_Descriptor,
      Return_Code            => Return_Value);

lue;
  end OS_Command;
  Return_Int : Integer := 0;
begin
  Return_Int := OS_Command(
    Command   => "cmd.exe",
    Arguments => "/C dir C:\introductory_ada_book\source_code\ch08\∗.adb");
  --Return_Int := OS_Command(
  --  Command   => "ls",
  --  Arguments => "-l ∗.adb");
end Talk_To_OS;

his is a very short and powerful little application. It was adapted from a Rosetta Code example. Let’s start from the top:

  1. with GNAT.OS_Lib; – This is not a standard Ada compiler library, but it has a number of things that we really need for this small application. For one, we can spawn processes that can run specific tasks in the operating system itself. We will use this as needed in order to implement some very useful functionality.
  2. On line 8, a function is created to run commands. In it, a command with its parameters will be passed in. This is done purely for convenience.
  3. Arguments_List : GNAT.OS_Lib.Argument_List := (1 => new String'(Command), 2 => new String'(Arguments)); – The command and the arguments now need to be turned into a specific format for the function that we need. The function Spawn (on line 22) will take only this input. .OS_Lib.Spawn – After preparing all of the inputs in a particular order, the spawn function is called. This will actually run our command. One thing that is worth paying attention to is the fact that Output_ File_Descriptor is set to File_Descriptor which is set to standard output. If you want to save the output somewhere, then you can open a file and redirect the output there.
  4. Now, have a look at the code after the begin keyword in the Talk_ To_OS procedure. The preceding example will run in Windows and a Unix operating system. You just need to comment out the initial call to OS_Command and remove the comments for the second call to the same function. Also, you will need to alter the structure of the arguments list to be like so: Arguments_List : GNAT.OS_Lib.Argument_List := (--1 => new String'(Command), 1 => new String'(Arguments)); If you are feeling adventurous, make an improvement to the preceding example where you check to see if the command in question does exist on your system before you actually execute it.