]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-getopt.adb
ncurses 5.4
[ncurses.git] / Ada95 / samples / ncurses2-getopt.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 ncurses                                  --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000,2004 Free Software Foundation, Inc.                   --
11 --                                                                          --
12 -- Permission is hereby granted, free of charge, to any person obtaining a  --
13 -- copy of this software and associated documentation files (the            --
14 -- "Software"), to deal in the Software without restriction, including      --
15 -- without limitation the rights to use, copy, modify, merge, publish,      --
16 -- distribute, distribute with modifications, sublicense, and/or sell       --
17 -- copies of the Software, and to permit persons to whom the Software is    --
18 -- furnished to do so, subject to the following conditions:                 --
19 --                                                                          --
20 -- The above copyright notice and this permission notice shall be included  --
21 -- in all copies or substantial portions of the Software.                   --
22 --                                                                          --
23 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
24 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
25 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
26 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
27 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
28 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
29 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
30 --                                                                          --
31 -- Except as contained in this notice, the name(s) of the above copyright   --
32 -- holders shall not be used in advertising or otherwise to promote the     --
33 -- sale, use or other dealings in this Software without prior written       --
34 -- authorization.                                                           --
35 ------------------------------------------------------------------------------
36 --  Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
37 --  Version Control
38 --  $Revision: 1.3 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 --  A simplified version of the  GNU getopt function
42 --  copyright Free Software Foundtion
43
44 with Ada.Strings.Fixed;
45 with Ada.Strings.Bounded;
46 with Ada.Text_IO; use Ada.Text_IO;
47
48 package body ncurses2.getopt is
49
50    optopt : Character := '?';
51
52    nextchar : Natural := 0;
53
54    --  Ncurses doesn't use the non option elements so we are spared
55    --  the job of computing those.
56
57    --  also the user is not allowed to modify argv or argc
58    --  Doing so is Erroneous execution.
59
60    --  longoptions are not handled.
61
62    procedure Qgetopt (retval : out Integer;
63                       argc : Integer;
64                       argv : stringfunc;
65                         --  argv will be the Argument function.
66                       optstring : String;
67                       optind : in out Integer;
68                         --  ignored for ncurses, must be initialized to 1 by
69                         --  the caller
70                       Optarg : out stringa
71                         --  a garbage colector would be useful here.
72                      ) is
73
74       package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
75       use BS;
76       optargx : Bounded_String;
77    begin
78
79       if argc < optind then
80          retval := -1;
81          return;
82       end if;
83
84       optargx := To_Bounded_String ("");
85
86       if nextchar = 0 then
87
88          if argv (optind) = "--" then
89                            --  the rest are non-options, we ignore them
90             retval := -1;
91             return;
92          end if;
93
94          if argv (optind)(1) /= '-' or argv (optind)'Length = 1 then
95             optind := optind + 1;
96             Optarg := new String'(argv (optind));
97             retval := 1;
98             return;
99          end if;
100
101          nextchar := 2; -- skip the one hyphen.
102       end if;
103
104       --  Look at and handle the next short option-character.
105       declare
106          c : Character := argv (optind) (nextchar);
107          temp : Natural :=
108            Ada.Strings.Fixed.Index (optstring, String'(1 => c));
109       begin
110          if temp = 0 or c = ':' then
111             Put_Line (Standard_Error,
112                       argv (optind) & ": invalid option -- " & c);
113             optopt := c;
114             c := '?';
115             return;
116          end if;
117
118          if optstring (temp + 1) = ':' then
119             if optstring (temp + 2) = ':' then
120                --  This is an option that accepts an argument optionally.
121                if nextchar /= argv (optind)'Length then
122                   optargx := To_Bounded_String
123                     (argv (optind) (nextchar .. argv (optind)'Length));
124                else
125                   Optarg := null;
126                end if;
127             else
128                --  This is an option that requires an argument.
129                if nextchar /= argv (optind)'Length then
130                   optargx := To_Bounded_String
131                     (argv (optind) (nextchar .. argv (optind)'Length));
132                   optind := optind + 1;
133                elsif optind = argc then
134                   Put_Line (Standard_Error,
135                             argv (optind) &
136                             ": option requires an argument -- " & c);
137                   optopt := c;
138                   if optstring (1) = ':'  then
139                      c := ':';
140                   else
141                      c := '?';
142                   end if;
143                else
144                   --  increment it again when taking next ARGV-elt as argument.
145                   optind := optind + 1;
146                   optargx := To_Bounded_String (argv (optind));
147                   optind := optind + 1;
148                end if;
149             end if;
150             nextchar := 0;
151          else -- no argument for the option
152             if nextchar = argv (optind)'Length then
153                optind := optind + 1;
154                nextchar := 0;
155             else
156                nextchar := nextchar + 1;
157             end if;
158          end if;
159
160          retval := Character'Pos (c);
161          Optarg := new String'(To_String (optargx));
162          return;
163       end;
164    end Qgetopt;
165
166 end ncurses2.getopt;