OSDN Git Service

* function.h (incomming_args): Break out of struct function.
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-comlin.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                    G N A T . C O M M A N D _ L I N E                     --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                     Copyright (C) 1999-2008, AdaCore                     --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  High level package for command line parsing and manipulation
35
36 --  Parsing the command line
37 --  ========================
38
39 --  This package provides an interface for parsing command line arguments,
40 --  when they are either read from Ada.Command_Line or read from a string list.
41 --  As shown in the example below, one should first retrieve the switches
42 --  (special command line arguments starting with '-' by default) and their
43 --  parameters, and then the rest of the command line arguments.
44
45 --  This package is flexible enough to accommodate various needs: optional
46 --  switch parameters, various characters to separate a switch and its
47 --  parameter, whether to stop the parsing at the first non-switch argument
48 --  encountered, etc.
49
50 --  begin
51 --     loop
52 --        case Getopt ("a b: ad") is  -- Accepts '-a', '-ad', or '-b argument'
53 --           when ASCII.NUL => exit;
54
55 --           when 'a' =>
56 --                 if Full_Switch = "a" then
57 --                    Put_Line ("Got a");
58 --                 else
59 --                    Put_Line ("Got ad");
60 --                 end if;
61
62 --           when 'b' =>
63 --              Put_Line ("Got b + " & Parameter);
64
65 --           when others =>
66 --              raise Program_Error;         -- cannot occur!
67 --        end case;
68 --     end loop;
69
70 --     loop
71 --        declare
72 --           S : constant String := Get_Argument (Do_Expansion => True);
73 --        begin
74 --           exit when S'Length = 0;
75 --           Put_Line ("Got " & S);
76 --        end;
77 --     end loop;
78
79 --  exception
80 --     when Invalid_Switch    => Put_Line ("Invalid Switch " & Full_Switch);
81 --     when Invalid_Parameter => Put_Line ("No parameter for " & Full_Switch);
82 --  end;
83
84 --  A more complicated example would involve the use of sections for the
85 --  switches, as for instance in gnatmake. The same command line is used to
86 --  provide switches for several tools. Each tool recognizes its switches by
87 --  separating them with special switches, chosen by the programmer.
88 --  Each section acts as a command line of its own.
89
90 --  begin
91 --     Initialize_Option_Scan ('-', False, "largs bargs cargs");
92 --     loop
93 --        --  Same loop as above to get switches and arguments
94 --     end loop;
95
96 --     Goto_Section ("bargs");
97 --     loop
98 --        --  Same loop as above to get switches and arguments
99 --        --  The supported switches in Get_Opt might be different
100 --     end loop;
101
102 --     Goto_Section ("cargs");
103 --     loop
104 --        --  Same loop as above to get switches and arguments
105 --        --  The supported switches in Get_Opt might be different
106 --     end loop;
107 --  end;
108
109 --  The example above have shown how to parse the command line when the
110 --  arguments are read directly from Ada.Command_Line. However, these arguments
111 --  can also be read from a list of strings. This can be useful in several
112 --  contexts, either because your system does not support Ada.Command_Line, or
113 --  because you are manipulating other tools and creating their command line by
114 --  hand, or for any other reason.
115 --  To create the list of strings, it is recommended to use
116 --  GNAT.OS_Lib.Argument_String_To_List.
117
118 --  The example below shows how to get the parameters from such a list. Note
119 --  also the use of '*' to get all the switches, and not report errors when an
120 --  unexpected switch was used by the user
121
122 --  declare
123 --     Parser : Opt_Parser;
124 --     Args : constant Argument_List_Access :=
125 --        GNAT.OS_Lib.Argument_String_To_List ("-g -O1 -Ipath");
126 --  begin
127 --     Initialize_Option_Scan (Parser, Args);
128 --     while Get_Opt ("* g O! I=", Parser) /= ASCII.NUL loop
129 --        Put_Line ("Switch " & Full_Switch (Parser)
130 --                  & " param=" & Parameter (Parser));
131 --     end loop;
132 --     Free (Parser);
133 --  end;
134 --
135 --  Creating and manipulating the command line
136 --  ===========================================
137
138 --  This package provides handling of command line by providing methods to
139 --  add or remove arguments from it. The resulting command line is kept as
140 --  short as possible by coalescing arguments whenever possible.
141
142 --  This package can be used to construct complex command lines for instance
143 --  from an GUI interface (although the package itself does not depend on a
144 --  specific GUI toolkit). For instance, if you are configuring the command
145 --  line to use when spawning a tool with the following characteristics:
146
147 --    * Specifying -gnatwa is the same as specifying -gnatwu -gnatwv, but
148 --      shorter and more readable
149
150 --    * All switches starting with -gnatw can be grouped, for instance one
151 --      can write -gnatwcd instead of -gnatwc -gnatwd.
152 --      Of course, this can be combined with the above and -gnatwacd is the
153 --      same as -gnatwc -gnatwd -gnatwu -gnatwv
154
155 --    * The switch -T is the same as -gnatwAB
156
157 --    * A switch -foo takes one mandatory parameter
158
159 --  These attributes can be configured through this package with the following
160 --  calls:
161
162 --     Config : Command_Line_Configuration;
163 --     Define_Prefix (Config, "-gnatw");
164 --     Define_Alias  (Config, "-gnatwa", "-gnatwuv");
165 --     Define_Alias  (Config, "-T",      "-gnatwAB");
166
167 --  Using this configuration, one can then construct a command line for the
168 --  tool with:
169
170 --     Cmd : Command_Line;
171 --     Set_Configuration (Cmd, Config);
172 --     Add_Switch (Cmd, "-bar");
173 --     Add_Switch (Cmd, "-gnatwu");
174 --     Add_Switch (Cmd, "-gnatwv");  --  will be grouped with the above
175 --     Add_Switch (Cmd, "-T");
176
177 --  The resulting command line can be iterated over to get all its switches,
178 --  There are two modes for this iteration: either you want to get the
179 --  shortest possible command line, which would be:
180
181 --      -bar -gnatwaAB
182
183 --  or on the other hand you want each individual switch (so that your own
184 --  tool does not have to do further complex processing), which would be:
185
186 --      -bar -gnatwu -gnatwv -gnatwA -gnatwB
187
188 --  Of course, we can assume that the tool you want to spawn would understand
189 --  both of these, since they are both compatible with the description we gave
190 --  above. However, the first result is useful if you want to show the user
191 --  what you are spawning (since that keeps the output shorter), and the second
192 --  output is more useful for a tool that would check whether -gnatwu was
193 --  passed (which isn't obvious in the first output). Likewise, the second
194 --  output is more useful if you have a graphical interface since each switch
195 --  can be associated with a widget, and you immediately know whether -gnatwu
196 --  was selected.
197 --
198 --  Some command line arguments can have parameters, which on a command line
199 --  appear as a separate argument that must immediately follow the switch.
200 --  Since the subprograms in this package will reorganize the switches to group
201 --  them, you need to indicate what is a command line
202 --  parameter, and what is a switch argument.
203
204 --  This is done by passing an extra argument to Add_Switch, as in:
205
206 --     Add_Switch (Cmd, "-foo", "arg1");
207
208 --  This ensures that "arg1" will always be treated as the argument to -foo,
209 --  and will not be grouped with other parts of the command line.
210
211 --  Parsing the command line with grouped arguments
212 --  ===============================================
213
214 --  This package also works great in collaboration with GNAT.Command_Line, to
215 --  parse the input to your tools. If you are writing the tool we described
216 --  above, you would do a first loop with Getopt to pass the switches and
217 --  their arguments, and create a temporary representation of the command line
218 --  as a Command_Line object. Finally, you can ask each individual switch to
219 --  that object. For instance:
220
221 --    declare
222 --      Cmd  : Command_Line;
223 --      Iter : Command_Line_Iterator;
224
225 --    begin
226 --      while Getopt ("foo: gnatw! T bar") /= ASCII.NUL loop
227 --         Add_Switch (Cmd, Full_Switch, Parameter);
228 --      end loop;
229
230 --      Start (Cmd, Iter, Expanded => True);
231 --      while Has_More (Iter) loop
232 --        if Current_Switch (Iter) = "-gnatwu" then ..
233 --        elsif Current_Switch (Iter) = "-gnatwv" then ...
234 --        end if;
235 --        Next (Iter);
236 --      end loop;
237
238 --  The above means that your tool does not have to handle on its own whether
239 --  the user passed -gnatwa (in which case -gnatwu was indeed selected), or
240 --  just -gnatwu, or a combination of -gnatw switches as in -gnatwuv.
241
242 with Ada.Command_Line;
243 with GNAT.Directory_Operations;
244 with GNAT.OS_Lib;
245 with GNAT.Regexp;
246
247 package GNAT.Command_Line is
248
249    -------------
250    -- Parsing --
251    -------------
252
253    type Opt_Parser is private;
254    Command_Line_Parser : constant Opt_Parser;
255    --  This object is responsible for parsing a list of arguments, which by
256    --  default are the standard command line arguments from Ada.Command_Line.
257    --  This is really a pointer to actual data, which must therefore be
258    --  initialized through a call to Initialize_Option_Scan, and must be freed
259    --  with a call to Free.
260    --
261    --  As a special case, Command_Line_Parser does not need to be either
262    --  initialized or free-ed.
263
264    procedure Initialize_Option_Scan
265      (Switch_Char              : Character := '-';
266       Stop_At_First_Non_Switch : Boolean := False;
267       Section_Delimiters       : String := "");
268    procedure Initialize_Option_Scan
269      (Parser                   : out Opt_Parser;
270       Command_Line             : GNAT.OS_Lib.Argument_List_Access;
271       Switch_Char              : Character := '-';
272       Stop_At_First_Non_Switch : Boolean := False;
273       Section_Delimiters       : String := "");
274    --  The first procedure resets the internal state of the package to prepare
275    --  to rescan the parameters. It does not need to be called before the first
276    --  use of Getopt (but it could be), but it must be called if you want to
277    --  start rescanning the command line parameters from the start. The
278    --  optional parameter Switch_Char can be used to reset the switch
279    --  character, e.g. to '/' for use in DOS-like systems.
280    --
281    --  The second subprogram initializes a parser that takes its arguments from
282    --  an array of strings rather than directly from the command line. In this
283    --  case, the parser is responsible for freeing the strings stored in
284    --  Command_Line. If you pass null to Command_Line, this will in fact create
285    --  a second parser for Ada.Command_Line, which doesn't share any data with
286    --  the default parser. This parser must be free-ed.
287    --
288    --  The optional parameter Stop_At_First_Non_Switch indicates if Getopt is
289    --  to look for switches on the whole command line, or if it has to stop as
290    --  soon as a non-switch argument is found.
291    --
292    --  Example:
293    --
294    --      Arguments: my_application file1 -c
295    --
296    --      If Stop_At_First_Non_Switch is False, then -c will be considered
297    --      as a switch (returned by getopt), otherwise it will be considered
298    --      as a normal argument (returned by Get_Argument).
299    --
300    --  If SECTION_DELIMITERS is set, then every following subprogram
301    --  (Getopt and Get_Argument) will only operate within a section, which
302    --  is delimited by any of these delimiters or the end of the command line.
303    --
304    --  Example:
305    --      Initialize_Option_Scan (Section_Delimiters => "largs bargs cargs");
306    --
307    --      Arguments on command line : my_application -c -bargs -d -e -largs -f
308    --      This line is made of three section, the first one is the default one
309    --      and includes only the '-c' switch, the second one is between -bargs
310    --      and -largs and includes '-d -e' and the last one includes '-f'
311
312    procedure Free (Parser : in out Opt_Parser);
313    --  Free the memory used by the parser. Calling this is not mandatory for
314    --  the Command_Line_Parser
315
316    procedure Goto_Section
317      (Name   : String := "";
318       Parser : Opt_Parser := Command_Line_Parser);
319    --  Change the current section. The next Getopt of Get_Argument will start
320    --  looking at the beginning of the section. An empty name ("") refers to
321    --  the first section between the program name and the first section
322    --  delimiter. If the section does not exist, then Invalid_Section is
323    --  raised.
324
325    function Full_Switch
326      (Parser : Opt_Parser := Command_Line_Parser) return String;
327    --  Returns the full name of the last switch found (Getopt only returns
328    --  the first character)
329
330    function Getopt
331      (Switches    : String;
332       Concatenate : Boolean := True;
333       Parser      : Opt_Parser := Command_Line_Parser) return Character;
334    --  This function moves to the next switch on the command line (defined as
335    --  switch character followed by a character within Switches, casing being
336    --  significant). The result returned is the first character of the switch
337    --  that is located. If there are no more switches in the current section,
338    --  returns ASCII.NUL. If Concatenate is True (by default), the switches
339    --  does not need to be separated by spaces (they can be concatenated if
340    --  they do not require an argument, e.g. -ab is the ame as two separate
341    --  arguments -a -b).
342    --
343    --  Switches is a string of all the possible switches, separated by a
344    --  space. A switch can be followed by one of the following characters:
345    --
346    --   ':'  The switch requires a parameter. There can optionally be a space
347    --        on the command line between the switch and its parameter.
348    --
349    --   '='  The switch requires a parameter. There can either be a '=' or a
350    --        space on the command line between the switch and its parameter.
351    --
352    --   '!'  The switch requires a parameter, but there can be no space on the
353    --        command line between the switch and its parameter.
354    --
355    --   '?'  The switch may have an optional parameter. There can be no space
356    --        between the switch and its argument.
357    --
358    --        e.g. if Switches has the following value : "a? b",
359    --        The command line can be:
360    --
361    --             -afoo    :  -a switch with 'foo' parameter
362    --             -a foo   :  -a switch and another element on the
363    --                           command line 'foo', returned by Get_Argument
364    --
365    --     Example: if Switches is "-a: -aO:", you can have the following
366    --              command lines:
367    --
368    --                -aarg    :  'a' switch with 'arg' parameter
369    --                -a arg   :  'a' switch with 'arg' parameter
370    --                -aOarg   :  'aO' switch with 'arg' parameter
371    --                -aO arg  :  'aO' switch with 'arg' parameter
372    --
373    --    Example:
374    --
375    --       Getopt ("a b: ac ad?")
376    --
377    --         accept either 'a' or 'ac' with no argument,
378    --         accept 'b' with a required argument
379    --         accept 'ad' with an optional argument
380    --
381    --  If the first item in switches is '*', then Getopt will catch
382    --  every element on the command line that was not caught by any other
383    --  switch. The character returned by GetOpt is '*', but Full_Switch
384    --  contains the full command line argument, including leading '-' if there
385    --  is one. If this character was not returned, there would be no way of
386    --  knowing whether it is there or not.
387    --
388    --    Example
389    --       Getopt ("* a b")
390    --       If the command line is '-a -c toto.o -b', Getopt will return
391    --       successively 'a', '*', '*' and 'b'. When '*' is returned,
392    --       Full_Switch returns the corresponding item on the command line.
393    --
394    --  When Getopt encounters an invalid switch, it raises the exception
395    --  Invalid_Switch and sets Full_Switch to return the invalid switch.
396    --  When Getopt cannot find the parameter associated with a switch, it
397    --  raises Invalid_Parameter, and sets Full_Switch to return the invalid
398    --  switch character.
399    --
400    --  Note: in case of ambiguity, e.g. switches a ab abc, then the longest
401    --  matching switch is returned.
402    --
403    --  Arbitrary characters are allowed for switches, although it is
404    --  strongly recommended to use only letters and digits for portability
405    --  reasons.
406    --
407    --  When Concatenate is False, individual switches need to be separated by
408    --  spaces.
409    --
410    --    Example
411    --       Getopt ("a b", Concatenate => False)
412    --       If the command line is '-ab', exception Invalid_Switch will be
413    --       raised and Full_Switch will return "ab".
414
415    function Get_Argument
416      (Do_Expansion : Boolean := False;
417       Parser       : Opt_Parser := Command_Line_Parser) return String;
418    --  Returns the next element on the command line which is not a switch.
419    --  This function should not be called before Getopt has returned
420    --  ASCII.NUL.
421    --
422    --  If Expansion is True, then the parameter on the command line will be
423    --  considered as a filename with wild cards, and will be expanded. The
424    --  matching file names will be returned one at a time. When there are no
425    --  more arguments on the command line, this function returns an empty
426    --  string. This is useful in non-Unix systems for obtaining normal
427    --  expansion of wild card references.
428
429    function Parameter
430      (Parser : Opt_Parser := Command_Line_Parser) return String;
431    --  Returns the parameter associated with the last switch returned by
432    --  Getopt. If no parameter was associated with the last switch, or no
433    --  previous call has been made to Get_Argument, raises Invalid_Parameter.
434    --  If the last switch was associated with an optional argument and this
435    --  argument was not found on the command line, Parameter returns an empty
436    --  string.
437
438    function Separator
439      (Parser : Opt_Parser := Command_Line_Parser) return Character;
440    --  The separator that was between the switch and its parameter. This is
441    --  of little use in general, only if you want to know exactly what was on
442    --  the command line. This is in general a single character, set to
443    --  ASCII.NUL if the switch and the parameter were concatenated. A space is
444    --  returned if the switch and its argument were in two separate arguments.
445
446    type Expansion_Iterator is limited private;
447    --  Type used during expansion of file names
448
449    procedure Start_Expansion
450      (Iterator     : out Expansion_Iterator;
451       Pattern      : String;
452       Directory    : String := "";
453       Basic_Regexp : Boolean := True);
454    --  Initialize a wild card expansion. The next calls to Expansion will
455    --  return the next file name in Directory which match Pattern (Pattern
456    --  is a regular expression, using only the Unix shell and DOS syntax if
457    --  Basic_Regexp is True). When Directory is an empty string, the current
458    --  directory is searched.
459    --
460    --  Pattern may contain directory separators (as in "src/*/*.ada").
461    --  Subdirectories of Directory will also be searched, up to one
462    --  hundred levels deep.
463    --
464    --  When Start_Expansion has been called, function Expansion should be
465    --  called repeatedly until it returns an empty string, before
466    --  Start_Expansion can be called again with the same Expansion_Iterator
467    --  variable.
468
469    function Expansion (Iterator : Expansion_Iterator) return String;
470    --  Returns the next file in the directory matching the parameters given
471    --  to Start_Expansion and updates Iterator to point to the next entry.
472    --  Returns an empty string when there is no more file in the directory
473    --  and its subdirectories.
474    --
475    --  If Expansion is called again after an empty string has been returned,
476    --  then the exception GNAT.Directory_Operations.Directory_Error is raised.
477
478    Invalid_Section : exception;
479    --  Raised when an invalid section is selected by Goto_Section
480
481    Invalid_Switch : exception;
482    --  Raised when an invalid switch is detected in the command line
483
484    Invalid_Parameter : exception;
485    --  Raised when a parameter is missing, or an attempt is made to obtain a
486    --  parameter for a switch that does not allow a parameter
487
488    -----------------
489    -- Configuring --
490    -----------------
491
492    type Command_Line_Configuration is private;
493
494    procedure Define_Alias
495      (Config   : in out Command_Line_Configuration;
496       Switch   : String;
497       Expanded : String);
498    --  Indicates that whenever Switch appears on the command line, it should
499    --  be expanded as Expanded. For instance, for the GNAT compiler switches,
500    --  we would define "-gnatwa" as an alias for "-gnatwcfijkmopruvz", ie some
501    --  default warnings to be activated.
502    --
503    --  Likewise, in some context you could define "--verbose" as an alias for
504    --  ("-v", "--full"), ie two switches.
505
506    procedure Define_Prefix
507      (Config   : in out Command_Line_Configuration;
508       Prefix   : String);
509    --  Indicates that all switches starting with the given prefix should be
510    --  grouped. For instance, for the GNAT compiler we would define "-gnatw"
511    --  as a prefix, so that "-gnatwu -gnatwv" can be grouped into "-gnatwuv"
512    --  It is assume that the remaining of the switch ("uv") is a set of
513    --  characters whose order is irrelevant. In fact, this package will sort
514    --  them alphabetically.
515
516    procedure Free (Config : in out Command_Line_Configuration);
517    --  Free the memory used by Config
518
519    -------------
520    -- Editing --
521    -------------
522
523    type Command_Line is private;
524
525    procedure Set_Configuration
526      (Cmd    : in out Command_Line;
527       Config : Command_Line_Configuration);
528    --  Set the configuration for this command line
529
530    function Get_Configuration
531      (Cmd : Command_Line) return Command_Line_Configuration;
532    --  Return the configuration used for that command line
533
534    procedure Set_Command_Line
535      (Cmd                : in out Command_Line;
536       Switches           : String;
537       Getopt_Description : String    := "";
538       Switch_Char        : Character := '-');
539    --  Set the new content of the command line, by replacing the current
540    --  version with Switches.
541    --
542    --  The parsing of Switches is done through calls to Getopt, by passing
543    --  Getopt_Description as an argument. (a "*" is automatically prepended so
544    --  that all switches and command line arguments are accepted).
545    --
546    --  To properly handle switches that take parameters, you should document
547    --  them in Getopt_Description. Otherwise, the switch and its parameter will
548    --  be recorded as two separate command line arguments as returned by a
549    --  Command_Line_Iterator (which might be fine depending on your
550    --  application).
551    --
552    --  This function can be used to reset Cmd by passing an empty string.
553
554    procedure Add_Switch
555      (Cmd       : in out Command_Line;
556       Switch    : String;
557       Parameter : String    := "";
558       Separator : Character := ' ');
559    --  Add a new switch to the command line, and combine/group it with existing
560    --  switches if possible. Nothing is done if the switch already exists with
561    --  the same parameter.
562    --
563    --  If the Switch takes a parameter, the latter should be specified
564    --  separately, so that the association between the two is always correctly
565    --  recognized even if the order of switches on the command line changes.
566    --  For instance, you should pass "--check=full" as ("--check", "full") so
567    --  that Remove_Switch below can simply take "--check" in parameter. That
568    --  will automatically remove "full" as well. The value of the parameter is
569    --  never modified by this package.
570    --
571    --  On the other hand, you could decide to simply pass "--check=full" as
572    --  the Switch above, and then pass no parameter. This means that you need
573    --  to pass "--check=full" to Remove_Switch as well.
574    --
575    --  A Switch with a parameter will never be grouped with another switch to
576    --  avoid ambiguities as to who the parameter applies to.
577    --
578    --  Separator is the character that goes between the switches and its
579    --  parameter on the command line. If it is set to ASCII.NUL, then no
580    --  separator is applied, and they are concatenated
581
582    procedure Remove_Switch
583      (Cmd        : in out Command_Line;
584       Switch     : String;
585       Remove_All : Boolean := False);
586    --  Remove Switch from the command line, and ungroup existing switches if
587    --  necessary.
588    --
589    --  The actual parameter to the switches are ignored. If for instance
590    --  you are removing "-foo", then "-foo param1" and "-foo param2" can
591    --  be removed.
592    --
593    --  If Remove_All is True, then all matching switches are removed, otherwise
594    --  only the first matching one is removed.
595
596    procedure Remove_Switch
597      (Cmd       : in out Command_Line;
598       Switch    : String;
599       Parameter : String);
600    --  Remove a switch with a specific parameter. If Parameter is the empty
601    --  string, then only a switch with no parameter will be removed.
602
603    ---------------
604    -- Iterating --
605    ---------------
606
607    type Command_Line_Iterator is private;
608
609    procedure Start
610      (Cmd      : in out Command_Line;
611       Iter     : in out Command_Line_Iterator;
612       Expanded : Boolean);
613    --  Start iterating over the command line arguments. If Expanded is true,
614    --  then the arguments are not grouped and no alias is used. For instance,
615    --  "-gnatwv" and "-gnatwu" would be returned instead of "-gnatwuv".
616    --
617    --  The iterator becomes invalid if the command line is changed through a
618    --  call to Add_Switch, Remove_Switch or Set_Command_Line.
619
620    function Current_Switch    (Iter : Command_Line_Iterator) return String;
621    function Current_Separator (Iter : Command_Line_Iterator) return String;
622    function Current_Parameter (Iter : Command_Line_Iterator) return String;
623    --  Return the current switch and its parameter (or the empty string if
624    --  there is no parameter or the switch was added through Add_Switch
625    --  without specifying the parameter.
626    --
627    --  Separator is the string that goes between the switch and its separator.
628    --  It could be the empty string if they should be concatenated, or a space
629    --  for instance. When printing, you should not add any other character.
630
631    function Has_More (Iter : Command_Line_Iterator) return Boolean;
632    --  Return True if there are more switches to be returned
633
634    procedure Next (Iter : in out Command_Line_Iterator);
635    --  Move to the next switch
636
637    procedure Free (Cmd : in out Command_Line);
638    --  Free the memory used by Cmd
639
640 private
641
642    Max_Depth : constant := 100;
643    --  Maximum depth of subdirectories
644
645    Max_Path_Length : constant := 1024;
646    --  Maximum length of relative path
647
648    type Depth is range 1 .. Max_Depth;
649
650    type Level is record
651       Name_Last : Natural := 0;
652       Dir       : GNAT.Directory_Operations.Dir_Type;
653    end record;
654
655    type Level_Array is array (Depth) of Level;
656
657    type Section_Number is new Natural range 0 .. 65534;
658    for Section_Number'Size use 16;
659
660    type Parameter_Type is record
661       Arg_Num : Positive;
662       First   : Positive;
663       Last    : Positive;
664       Extra   : Character;
665    end record;
666
667    type Is_Switch_Type is array (Natural range <>) of Boolean;
668    pragma Pack (Is_Switch_Type);
669
670    type Section_Type is array (Natural range <>) of Section_Number;
671    pragma Pack (Section_Type);
672
673    type Expansion_Iterator is limited record
674       Start : Positive := 1;
675       --  Position of the first character of the relative path to check against
676       --  the pattern.
677
678       Dir_Name : String (1 .. Max_Path_Length);
679
680       Current_Depth : Depth := 1;
681
682       Levels : Level_Array;
683
684       Regexp : GNAT.Regexp.Regexp;
685       --  Regular expression built with the pattern
686
687       Maximum_Depth : Depth := 1;
688       --  The maximum depth of directories, reflecting the number of directory
689       --  separators in the pattern.
690    end record;
691
692    type Opt_Parser_Data (Arg_Count : Natural) is record
693       Arguments : GNAT.OS_Lib.Argument_List_Access;
694       --  null if reading from the command line
695
696       The_Parameter : Parameter_Type;
697       The_Separator : Character;
698       The_Switch    : Parameter_Type;
699       --  This type and this variable are provided to store the current switch
700       --  and parameter.
701
702       Is_Switch : Is_Switch_Type (1 .. Arg_Count) := (others => False);
703       --  Indicates wich arguments on the command line are considered not be
704       --  switches or parameters to switches (leaving e.g. filenames,...)
705
706       Section : Section_Type (1 .. Arg_Count) := (others => 1);
707       --  Contains the number of the section associated with the current
708       --  switch. If this number is 0, then it is a section delimiter, which is
709       --  never returned by GetOpt.
710
711       Current_Argument : Natural := 1;
712       --  Number of the current argument parsed on the command line
713
714       Current_Index : Natural := 1;
715       --  Index in the current argument of the character to be processed
716
717       Current_Section : Section_Number := 1;
718
719       Expansion_It : aliased Expansion_Iterator;
720       --  When Get_Argument is expanding a file name, this is the iterator used
721
722       In_Expansion : Boolean := False;
723       --  True if we are expanding a file
724
725       Switch_Character : Character := '-';
726       --  The character at the beginning of the command line arguments,
727       --  indicating the beginning of a switch.
728
729       Stop_At_First : Boolean := False;
730       --  If it is True then Getopt stops at the first non-switch argument
731    end record;
732
733    Command_Line_Parser_Data : aliased Opt_Parser_Data
734      (Ada.Command_Line.Argument_Count);
735    --  The internal data used when parsing the command line
736
737    type Opt_Parser is access all Opt_Parser_Data;
738    Command_Line_Parser : constant Opt_Parser :=
739                            Command_Line_Parser_Data'Access;
740
741    type Command_Line_Configuration_Record is record
742       Prefixes : GNAT.OS_Lib.Argument_List_Access;
743       --  The list of prefixes
744
745       Aliases    : GNAT.OS_Lib.Argument_List_Access;
746       Expansions : GNAT.OS_Lib.Argument_List_Access;
747       --  The aliases. Both arrays have the same indices
748    end record;
749    type Command_Line_Configuration is access Command_Line_Configuration_Record;
750
751    type Command_Line is record
752       Config   : Command_Line_Configuration;
753       Expanded : GNAT.OS_Lib.Argument_List_Access;
754
755       Params : GNAT.OS_Lib.Argument_List_Access;
756       --  Parameter for the corresponding switch in Expanded. The first
757       --  character is the separator (or ASCII.NUL if there is no separator)
758
759       Coalesce        : GNAT.OS_Lib.Argument_List_Access;
760       Coalesce_Params : GNAT.OS_Lib.Argument_List_Access;
761       --  Cached version of the command line. This is recomputed every time the
762       --  command line changes. Switches are grouped as much as possible, and
763       --  aliases are used to reduce the length of the command line.
764       --  The parameters are not allocated, they point into Params, so must not
765       --  be freed.
766    end record;
767
768    type Command_Line_Iterator is record
769       List     : GNAT.OS_Lib.Argument_List_Access;
770       Params   : GNAT.OS_Lib.Argument_List_Access;
771       Current  : Natural;
772    end record;
773
774 end GNAT.Command_Line;