ncurses2-getopt.adb 6.8 KB

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