Actually spawn new processes from the PATH which are entered into the entry

This commit is contained in:
R. Tyler Croy 2017-01-07 22:39:59 -08:00
parent ddd8a7f3cf
commit 18e2f61dcf
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
4 changed files with 121 additions and 3 deletions

View File

@ -3,11 +3,14 @@
---
with Ada.Text_IO;
with GNAT.OS_Lib;
with Gtk.Main;
with Gtk.Widget;
with Gtk.Search_Entry;
with Gtkada.Builder; use Gtkada.Builder;
with Arun.Launcher;
package body Arun.Handlers is
procedure Quit (Object : access Gtkada_Builder_Record'Class) is
@ -34,8 +37,16 @@ package body Arun.Handlers is
use Gtkada.Builder;
Widget : Gtk_Search_Entry := Gtk_Search_Entry (Get_Object (Object, "commandEntry"));
Command : constant String := Widget.Get_Text;
Full_Path : aliased constant String := Arun.Launcher.Find_Full_Path (Command);
begin
Put_Line ("Should Execute: " & Widget.Get_Text);
Put_Line ("Should Execute: " & Command);
if Full_Path /= "" then
Arun.Launcher.Execute (Full_Path);
end if;
Gtk.Main.Main_Quit;
end Execute_Command;

View File

@ -8,11 +8,11 @@ package Arun.Handlers is
procedure Quit (Object : access Gtkada_Builder_Record'Class);
-- Whenever the search entry changes call this handler for autocompletion
procedure Search_Changed (Object : access Gtkada_Builder_Record'Class);
-- Whenever the search entry changes call this handler for autocompletion
procedure Execute_Command (Object : access Gtkada_Builder_Record'Class);
-- On "activate" of the search entry call this handler (basically when the user
-- hits the enter key
procedure Execute_Command (Object : access Gtkada_Builder_Record'Class);
end Arun.Handlers;

97
src/arun-launcher.adb Normal file
View File

@ -0,0 +1,97 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line.Environment;
with Ada.Environment_Variables;
with GNAT.OS_Lib;
with GNAT.String_Split;
with Interfaces.C;
with Interfaces.C.Strings;
with System;
package body Arun.Launcher is
function Find_Full_Path (Snippet : in String) return String is
use GNAT.String_Split;
PATH : constant String := Ada.Environment_Variables.Value ("PATH");
Separator : constant String := ":";
Path_Components : Slice_Set;
begin
Create (S => Path_Components,
From => PATH,
Separators => Separator,
Mode => Single);
for Index in 1 .. Slice_Count (Path_Components) loop
Put_Line ("Looking for an executable in " & Slice (S => Path_Components,
Index => Index));
declare
Path_Component : constant String := Slice (S => Path_Components,
Index => Index);
Computed_Location : constant String := Path_Component & "/" & Snippet;
begin
if GNAT.OS_Lib.Is_Executable_File (Computed_Location) then
Put_Line ("Found executable at " & Computed_Location);
return Computed_Location;
end if;
end;
end loop;
return "";
end Find_Full_Path;
procedure Execute (Executable_Path : in String) is
use GNAT.OS_Lib;
use Interfaces.C;
use Interfaces.C.Strings;
use Ada.Command_Line.Environment;
function Exec_And_Replace (Filename : in String;
Arguments : in Chars_Ptr_Array;
Env : in Chars_Ptr_Array) return Integer
with Import,
Convention => C,
Link_Name => "execve";
procedure Print_Errno (Message : in String)
with Import,
Convention => C,
Link_Name => "perror";
Default_Args : Chars_Ptr_Array (1 .. 2) := (1 => New_String (Executable_Path), 2 => Null_Ptr);
Environment_Args : Chars_Ptr_Array (1 .. Size_T(Environment_Count + 1)) := (others => Null_Ptr);
Status : Integer;
begin
-- Fierst we need to fill in our Environment_Args with the appropriate variables
for Index in 1 .. Environment_Count loop
Environment_Args (Size_T(Index)) := New_String (Environment_Value (Index));
end loop;
Put_Line ("Spawning " & Executable_Path);
Status := Exec_And_Replace (Filename => Executable_Path,
Arguments => Default_Args,
Env => Environment_Args);
-- If the Exec_And_Replace function returns then something has gone wrong
Put_Line ("Spawned " & Executable_Path & " with " & Status'Img);
if Status /= 0 then
Print_Errno ("Something went wrong");
end if;
end Execute;
end Arun.Launcher;

10
src/arun-launcher.ads Normal file
View File

@ -0,0 +1,10 @@
package Arun.Launcher is
function Find_Full_Path (Snippet : in String) return String;
-- Determine the full path of a Snippet based on the PATH environment variable
procedure Execute (Executable_Path : in String);
-- Spawn the process for the Executable_Path
end Arun.Launcher;