1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey --
11 -- Copyright 2000-2008,2011 Free Software Foundation, Inc. --
13 -- Permission is hereby granted, free of charge, to any person obtaining a --
14 -- copy of this software and associated documentation files (the --
15 -- "Software"), to deal in the Software without restriction, including --
16 -- without limitation the rights to use, copy, modify, merge, publish, --
17 -- distribute, distribute with modifications, sublicense, and/or sell --
18 -- copies of the Software, and to permit persons to whom the Software is --
19 -- furnished to do so, subject to the following conditions: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
32 -- Except as contained in this notice, the name(s) of the above copyright --
33 -- holders shall not be used in advertising or otherwise to promote the --
34 -- sale, use or other dealings in this Software without prior written --
36 ------------------------------------------------------------------------------
37 -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
40 -- $Date: 2020/02/02 23:34:34 $
41 -- Binding Version 01.00
42 ------------------------------------------------------------------------------
43 -- A simplified version of the GNU getopt function
44 -- copyright Free Software Foundtion
46 with Ada.Strings.Fixed;
47 with Ada.Strings.Bounded;
48 with Ada.Text_IO; use Ada.Text_IO;
50 package body ncurses2.getopt is
52 nextchar : Natural := 0;
54 -- Ncurses doesn't use the non option elements so we are spared
55 -- the job of computing those.
57 -- also the user is not allowed to modify argv or argc
58 -- Doing so is Erroneous execution.
60 -- long options are not handled.
62 procedure Qgetopt (retval : out Integer;
65 -- argv will be the Argument function.
67 optind : in out Integer;
68 -- ignored for ncurses, must be initialized to 1 by
71 -- a garbage collector would be useful here.
74 package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
76 optargx : Bounded_String;
84 optargx := To_Bounded_String ("");
88 if argv (optind) = "--" then
89 -- the rest are non-options, we ignore them
94 if argv (optind)(1) /= '-' or argv (optind)'Length = 1 then
96 Optarg := new String'(argv (optind));
101 nextchar := 2; -- skip the one hyphen.
104 -- Look at and handle the next short option-character.
106 c : Character := argv (optind) (nextchar);
107 temp : constant Natural :=
108 Ada.Strings.Fixed.Index (optstring, String'(1 => c));
110 if temp = 0 or c = ':' then
111 Put_Line (Standard_Error,
112 argv (optind) & ": invalid option -- " & c);
117 if optstring (temp + 1) = ':' then
118 if optstring (temp + 2) = ':' then
119 -- This is an option that accepts an argument optionally.
120 if nextchar /= argv (optind)'Length then
121 optargx := To_Bounded_String
122 (argv (optind) (nextchar .. argv (optind)'Length));
127 -- This is an option that requires an argument.
128 if nextchar /= argv (optind)'Length then
129 optargx := To_Bounded_String
130 (argv (optind) (nextchar .. argv (optind)'Length));
131 optind := optind + 1;
132 elsif optind = argc then
133 Put_Line (Standard_Error,
135 ": option requires an argument -- " & c);
136 if optstring (optstring'First) = ':' then
142 -- increment it again when taking next ARGV-elt as argument.
143 optind := optind + 1;
144 optargx := To_Bounded_String (argv (optind));
145 optind := optind + 1;
149 else -- no argument for the option
150 if nextchar = argv (optind)'Length then
151 optind := optind + 1;
154 nextchar := nextchar + 1;
158 retval := Character'Pos (c);
159 Optarg := new String'(To_String (optargx));