missing ncurses sources
[tomato.git] / release / src / router / libncurses / Ada95 / samples / ncurses2-getopt.adb
blob29f1fee8281ac7ee5f96e0d8c0f09c761eeba421
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT ncurses Binding Samples --
4 -- --
5 -- ncurses --
6 -- --
7 -- B O D Y --
8 -- --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2008,2011 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.8 $
39 -- $Date: 2011/03/19 12:09:51 $
40 -- Binding Version 01.00
41 ------------------------------------------------------------------------------
42 -- A simplified version of the GNU getopt function
43 -- copyright Free Software Foundtion
45 with Ada.Strings.Fixed;
46 with Ada.Strings.Bounded;
47 with Ada.Text_IO; use Ada.Text_IO;
49 package body ncurses2.getopt is
51 nextchar : Natural := 0;
53 -- Ncurses doesn't use the non option elements so we are spared
54 -- the job of computing those.
56 -- also the user is not allowed to modify argv or argc
57 -- Doing so is Erroneous execution.
59 -- long options are not handled.
61 procedure Qgetopt (retval : out Integer;
62 argc : Integer;
63 argv : stringfunc;
64 -- argv will be the Argument function.
65 optstring : String;
66 optind : in out Integer;
67 -- ignored for ncurses, must be initialized to 1 by
68 -- the caller
69 Optarg : out stringa
70 -- a garbage collector would be useful here.
71 ) is
73 package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
74 use BS;
75 optargx : Bounded_String;
76 begin
78 if argc < optind then
79 retval := -1;
80 return;
81 end if;
83 optargx := To_Bounded_String ("");
85 if nextchar = 0 then
87 if argv (optind) = "--" then
88 -- the rest are non-options, we ignore them
89 retval := -1;
90 return;
91 end if;
93 if argv (optind)(1) /= '-' or argv (optind)'Length = 1 then
94 optind := optind + 1;
95 Optarg := new String'(argv (optind));
96 retval := 1;
97 return;
98 end if;
100 nextchar := 2; -- skip the one hyphen.
101 end if;
103 -- Look at and handle the next short option-character.
104 declare
105 c : Character := argv (optind) (nextchar);
106 temp : constant Natural :=
107 Ada.Strings.Fixed.Index (optstring, String'(1 => c));
108 begin
109 if temp = 0 or c = ':' then
110 Put_Line (Standard_Error,
111 argv (optind) & ": invalid option -- " & c);
112 c := '?';
113 return;
114 end if;
116 if optstring (temp + 1) = ':' then
117 if optstring (temp + 2) = ':' then
118 -- This is an option that accepts an argument optionally.
119 if nextchar /= argv (optind)'Length then
120 optargx := To_Bounded_String
121 (argv (optind) (nextchar .. argv (optind)'Length));
122 else
123 Optarg := null;
124 end if;
125 else
126 -- This is an option that requires an argument.
127 if nextchar /= argv (optind)'Length then
128 optargx := To_Bounded_String
129 (argv (optind) (nextchar .. argv (optind)'Length));
130 optind := optind + 1;
131 elsif optind = argc then
132 Put_Line (Standard_Error,
133 argv (optind) &
134 ": option requires an argument -- " & c);
135 if optstring (optstring'First) = ':' then
136 c := ':';
137 else
138 c := '?';
139 end if;
140 else
141 -- increment it again when taking next ARGV-elt as argument.
142 optind := optind + 1;
143 optargx := To_Bounded_String (argv (optind));
144 optind := optind + 1;
145 end if;
146 end if;
147 nextchar := 0;
148 else -- no argument for the option
149 if nextchar = argv (optind)'Length then
150 optind := optind + 1;
151 nextchar := 0;
152 else
153 nextchar := nextchar + 1;
154 end if;
155 end if;
157 retval := Character'Pos (c);
158 Optarg := new String'(To_String (optargx));
159 return;
160 end;
161 end Qgetopt;
163 end ncurses2.getopt;