source: ada-examples/shell/commands.adb @ c53f12b

ada-examples-4-10-branch
Last change on this file since c53f12b was c53f12b, checked in by Joel Sherrill <joel.sherrill@…>, on 02/02/11 at 19:08:06

2011-02-02 Joel Sherrill <joel.sherrill@…>

  • commands.adb, commands.ads, rtems_shell.ads, shell.adb: Add getopt_r Package and example command.
  • command_line_arguments.adb, command_line_arguments.ads, getopt_r.adb, getopt_r.ads: New files.
  • Property mode set to 100644
File size: 2.2 KB
Line 
1--
2--  $Id$
3--
4
5with Ada.Text_IO; use Ada.Text_IO;
6with Command_Line_Arguments; use Command_Line_Arguments;
7with Getopt_R;
8with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
9
10
11package body Commands is
12
13   function Prompt return String is begin
14      return "RTEMS> ";
15   end Prompt;
16
17   function C_Prompt return chars_ptr is
18   begin
19      return New_String (Prompt);
20   end C_Prompt;
21
22   function Command_Test_Arguments
23     (ArgC : Argument_Count_Type;
24      ArgV : Argument_Vector_Type)
25      return int
26   is
27      Arguments : Argument_Array (1 .. ArgC);
28      Count     : Argument_Count_Type := 1;
29   begin
30      Arguments := Argument_Vector_Package.Value (ArgV, ArgC);
31      loop
32         exit when Count > ArgC;
33         Put_Line (Value (Arguments (Count)));
34         Count := Count + 1;
35      end loop;
36      return 0;
37   end Command_Test_Arguments;
38
39   function Command_Getopt_R
40     (ArgC : Argument_Count_Type;
41      ArgV : Argument_Vector_Type)
42   return int is
43      Test_String : String := "c:di:n:p:u:V";
44      Optchar : character;
45      V       : Integer;
46      Reent   : aliased Getopt_R.Reentrant;
47   begin
48     Getopt_R.Initialize( Reent'Unrestricted_Access, Argc, Argv );
49     loop
50        V := Getopt_R.Getopt( Reent'Unrestricted_Access, Test_String );
51        exit when V = -1;
52
53        optchar :=  Character'Val( V );
54        case optchar is
55           when 'c' =>
56              Put_Line("command is "& To_String(Reent.Optarg));
57           when 'd' =>
58               Put_Line("debug on");
59           when 'i' =>
60              Put_line("got -i, its argument is:" & To_String(Reent.Optarg) );
61           when 'n' =>
62              Put_line("got -n, its argument is:" & To_String(Reent.Optarg));
63           when 'p' =>
64              Put_line("got -p, its argument is:" & To_String(Reent.Optarg));
65           when 'u' =>
66              Put_line("got -u, its argument is:" & To_String(Reent.Optarg));
67           when 'V' =>
68              Put_line("got -V");
69
70           when '?' =>
71              Put_Line("got ?, optopt is " & Reent.Optopt);
72
73           when ':' =>
74              Put_Line("get :, optopt is "& Reent.optopt);
75
76           when others => null;
77        end case;
78     end loop;
79
80     for Count in Reent.Optind .. Reent.ArgC
81     loop
82        Put_Line (ptrdiff_t'Image(Count) & ": " & Get_Argument(Argv, Count));
83     end loop;
84
85     return 0;
86   end Command_Getopt_R;
87end Commands;
Note: See TracBrowser for help on using the repository browser.