Debian Codegraph @

deps/clic/src/clic-command_line.adb

raw
1 -- This package is imported from GNAT FSF 11.2 source to have a common
2 -- implementation of Command_Line parsing across any version of GNAT.
3
4 ------------------------------------------------------------------------------
5 -- --
6 -- GNAT COMPILER COMPONENTS --
7 -- --
8 -- G N A T . C O M M A N D _ L I N E --
9 -- --
10 -- B o d y --
11 -- --
12 -- Copyright (C) 1999-2020, Free Software Foundation, Inc. --
13 -- --
14 -- GNAT is free software; you can redistribute it and/or modify it under --
15 -- terms of the GNU General Public License as published by the Free Soft- --
16 -- ware Foundation; either version 3, or (at your option) any later ver- --
17 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE. --
20 -- --
21 -- As a special exception under Section 7 of GPL version 3, you are granted --
22 -- additional permissions described in the GCC Runtime Library Exception, --
23 -- version 3.1, as published by the Free Software Foundation. --
24 -- --
25 -- You should have received a copy of the GNU General Public License and --
26 -- a copy of the GCC Runtime Library Exception along with this program; --
27 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
28 -- <http://www.gnu.org/licenses/>. --
29 -- --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
34
35 with Ada.Characters.Handling; use Ada.Characters.Handling;
36 with Ada.Strings.Unbounded;
37 with Ada.Text_IO; use Ada.Text_IO;
38 with Ada.Unchecked_Deallocation;
39
40 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
41 with GNAT.OS_Lib; use GNAT.OS_Lib;
42
43 package body CLIC.Command_Line is
44
45 -- General note: this entire body could use much more commenting. There
46 -- are large sections of uncommented code throughout, and many formal
47 -- parameters of local subprograms are not documented at all ???
48
49 package CL renames Ada.Command_Line;
50
51 type Switch_Parameter_Type is
52 (Parameter_None,
53 Parameter_With_Optional_Space, -- ':' in getopt
54 Parameter_With_Space_Or_Equal, -- '=' in getopt
55 Parameter_No_Space, -- '!' in getopt
56 Parameter_Optional); -- '?' in getopt
57
58 procedure Set_Parameter
59 (Variable : out Parameter_Type;
60 Arg_Num : Positive;
61 First : Positive;
62 Last : Natural;
63 Extra : Character := ASCII.NUL);
64 pragma Inline (Set_Parameter);
65 -- Set the parameter that will be returned by Parameter below
66 --
67 -- Extra is a character that needs to be added when reporting Full_Switch.
68 -- (it will in general be the switch character, for instance '-').
69 -- Otherwise, Full_Switch will report 'f' instead of '-f'. In particular,
70 -- it needs to be set when reporting an invalid switch or handling '*'.
71 --
72 -- Parameters need to be defined ???
73
74 function Goto_Next_Argument_In_Section (Parser : Opt_Parser) return Boolean;
75 -- Go to the next argument on the command line. If we are at the end of
76 -- the current section, we want to make sure there is no other identical
77 -- section on the command line (there might be multiple instances of
78 -- -largs). Returns True iff there is another argument.
79
80 function Get_File_Names_Case_Sensitive return Integer;
81 pragma Import (C, Get_File_Names_Case_Sensitive,
82 "__gnat_get_file_names_case_sensitive");
83
84 File_Names_Case_Sensitive : constant Boolean :=
85 Get_File_Names_Case_Sensitive /= 0;
86
87 procedure Canonical_Case_File_Name (S : in out String);
88 -- Given a file name, converts it to canonical case form. For systems where
89 -- file names are case sensitive, this procedure has no effect. If file
90 -- names are not case sensitive (i.e. for example if you have the file
91 -- "xyz.adb", you can refer to it as XYZ.adb or XyZ.AdB), then this call
92 -- converts the given string to canonical all lower case form, so that two
93 -- file names compare equal if they refer to the same file.
94
95 procedure Internal_Initialize_Option_Scan
96 (Parser : Opt_Parser;
97 Switch_Char : Character;
98 Stop_At_First_Non_Switch : Boolean;
99 Section_Delimiters : String);
100 -- Initialize Parser, which must have been allocated already
101
102 function Argument (Parser : Opt_Parser; Index : Integer) return String;
103 -- Return the index-th command line argument
104
105 procedure Find_Longest_Matching_Switch
106 (Switches : String;
107 Arg : String;
108 Index_In_Switches : out Integer;
109 Switch_Length : out Integer;
110 Param : out Switch_Parameter_Type);
111 -- Return the Longest switch from Switches that at least partially matches
112 -- Arg. Index_In_Switches is set to 0 if none matches. What are other
113 -- parameters??? in particular Param is not always set???
114
115 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
116 (Argument_List, Argument_List_Access);
117
118 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
119 (Command_Line_Configuration_Record, Command_Line_Configuration);
120
121 procedure Remove (Line : in out Argument_List_Access; Index : Integer);
122 -- Remove a specific element from Line
123
124 procedure Add
125 (Line : in out Argument_List_Access;
126 Str : String_Access;
127 Before : Boolean := False);
128 -- Add a new element to Line. If Before is True, the item is inserted at
129 -- the beginning, else it is appended.
130
131 procedure Add
132 (Config : in out Command_Line_Configuration;
133 Switch : Switch_Definition);
134 procedure Add
135 (Def : in out Alias_Definitions_List;
136 Alias : Alias_Definition);
137 -- Add a new element to Def
138
139 procedure Initialize_Switch_Def
140 (Def : out Switch_Definition;
141 Switch : String := "";
142 Long_Switch : String := "";
143 Help : String := "";
144 Section : String := "";
145 Argument : String := "ARG");
146 -- Initialize [Def] with the contents of the other parameters.
147 -- This also checks consistency of the switch parameters, and will raise
148 -- Invalid_Switch if they do not match.
149
150 procedure Decompose_Switch
151 (Switch : String;
152 Parameter_Type : out Switch_Parameter_Type;
153 Switch_Last : out Integer);
154 -- Given a switch definition ("name:" for instance), extracts the type of
155 -- parameter that is expected, and the name of the switch
156
157 function Can_Have_Parameter (S : String) return Boolean;
158 -- True if S can have a parameter
159
160 function Require_Parameter (S : String) return Boolean;
161 -- True if S requires a parameter
162
163 function Actual_Switch (S : String) return String;
164 -- Remove any possible trailing '!', ':', '?' and '='
165
166 generic
167 with procedure Callback
168 (Simple_Switch : String;
169 Separator : String;
170 Parameter : String;
171 Index : Integer); -- Index in Config.Switches, or -1
172 procedure For_Each_Simple_Switch
173 (Config : Command_Line_Configuration;
174 Section : String;
175 Switch : String;
176 Parameter : String := "";
177 Unalias : Boolean := True);
178 -- Breaks Switch into as simple switches as possible (expanding aliases and
179 -- ungrouping common prefixes when possible), and call Callback for each of
180 -- these.
181
182 procedure Sort_Sections
183 (Line : not null GNAT.OS_Lib.Argument_List_Access;
184 Sections : GNAT.OS_Lib.Argument_List_Access;
185 Params : GNAT.OS_Lib.Argument_List_Access);
186 -- Reorder the command line switches so that the switches belonging to a
187 -- section are grouped together.
188
189 procedure Group_Switches
190 (Cmd : Command_Line;
191 Result : Argument_List_Access;
192 Sections : Argument_List_Access;
193 Params : Argument_List_Access);
194 -- Group switches with common prefixes whenever possible. Once they have
195 -- been grouped, we also check items for possible aliasing.
196
197 procedure Alias_Switches
198 (Cmd : Command_Line;
199 Result : Argument_List_Access;
200 Params : Argument_List_Access);
201 -- When possible, replace one or more switches by an alias, i.e. a shorter
202 -- version.
203
204 function Looking_At
205 (Type_Str : String;
206 Index : Natural;
207 Substring : String) return Boolean;
208 -- Return True if the characters starting at Index in Type_Str are
209 -- equivalent to Substring.
210
211 generic
212 with function Callback (S : String; Index : Integer) return Boolean;
213 procedure Foreach_Switch
214 (Config : Command_Line_Configuration;
215 Section : String);
216 -- Iterate over all switches defined in Config, for a specific section.
217 -- Index is set to the index in Config.Switches. Stop iterating when
218 -- Callback returns False.
219
220 --------------
221 -- Argument --
222 --------------
223
224 function Argument (Parser : Opt_Parser; Index : Integer) return String is
225 begin
226 if Parser.Arguments /= null then
227 return Parser.Arguments (Index + Parser.Arguments'First - 1).all;
228 else
229 return CL.Argument (Index);
230 end if;
231 end Argument;
232
233 ------------------------------
234 -- Canonical_Case_File_Name --
235 ------------------------------
236
237 procedure Canonical_Case_File_Name (S : in out String) is
238 begin
239 if not File_Names_Case_Sensitive then
240 for J in S'Range loop
241 if S (J) in 'A' .. 'Z' then
242 S (J) := Character'Val
243 (Character'Pos (S (J)) +
244 (Character'Pos ('a') - Character'Pos ('A')));
245 end if;
246 end loop;
247 end if;
248 end Canonical_Case_File_Name;
249
250 ---------------
251 -- Expansion --
252 ---------------
253
254 function Expansion (Iterator : Expansion_Iterator) return String is
255 type Pointer is access all Expansion_Iterator;
256
257 It : constant Pointer := Iterator'Unrestricted_Access;
258 S : String (1 .. 1024);
259 Last : Natural;
260
261 Current : Depth := It.Current_Depth;
262 NL : Positive;
263
264 begin
265 -- It is assumed that a directory is opened at the current level.
266 -- Otherwise GNAT.Directory_Operations.Directory_Error will be raised
267 -- at the first call to Read.
268
269 loop
270 Read (It.Levels (Current).Dir, S, Last);
271
272 -- If we have exhausted the directory, close it and go back one level
273
274 if Last = 0 then
275 Close (It.Levels (Current).Dir);
276
277 -- If we are at level 1, we are finished; return an empty string
278
279 if Current = 1 then
280 return String'(1 .. 0 => ' ');
281
282 -- Otherwise continue with the directory at the previous level
283
284 else
285 Current := Current - 1;
286 It.Current_Depth := Current;
287 end if;
288
289 -- If this is a directory, that is neither "." or "..", attempt to
290 -- go to the next level.
291
292 elsif Is_Directory
293 (It.Dir_Name (1 .. It.Levels (Current).Name_Last) &
294 S (1 .. Last))
295 and then S (1 .. Last) /= "."
296 and then S (1 .. Last) /= ".."
297 then
298 -- We can go to the next level only if we have not reached the
299 -- maximum depth,
300
301 if Current < It.Maximum_Depth then
302 NL := It.Levels (Current).Name_Last;
303
304 -- And if relative path of this new directory is not too long
305
306 if NL + Last + 1 < Max_Path_Length then
307 Current := Current + 1;
308 It.Current_Depth := Current;
309 It.Dir_Name (NL + 1 .. NL + Last) := S (1 .. Last);
310 NL := NL + Last + 1;
311 It.Dir_Name (NL) := Directory_Separator;
312 It.Levels (Current).Name_Last := NL;
313 Canonical_Case_File_Name (It.Dir_Name (1 .. NL));
314
315 -- Open the new directory, and read from it
316
317 GNAT.Directory_Operations.Open
318 (It.Levels (Current).Dir, It.Dir_Name (1 .. NL));
319 end if;
320 end if;
321 end if;
322
323 -- Check the relative path against the pattern
324
325 -- Note that we try to match also against directory names, since
326 -- clients of this function may expect to retrieve directories.
327
328 declare
329 Name : String :=
330 It.Dir_Name (It.Start .. It.Levels (Current).Name_Last)
331 & S (1 .. Last);
332
333 begin
334 Canonical_Case_File_Name (Name);
335
336 -- If it matches return the relative path
337
338 if GNAT.Regexp.Match (Name, Iterator.Regexp) then
339 return Name;
340 end if;
341 end;
342 end loop;
343 end Expansion;
344
345 ---------------------
346 -- Current_Section --
347 ---------------------
348
349 function Current_Section
350 (Parser : Opt_Parser := Command_Line_Parser) return String
351 is
352 begin
353 if Parser.Current_Section = 1 then
354 return "";
355 end if;
356
357 for Index in reverse 1 .. Integer'Min (Parser.Current_Argument - 1,
358 Parser.Section'Last)
359 loop
360 if Parser.Section (Index) = 0 then
361 return Argument (Parser, Index);
362 end if;
363 end loop;
364
365 return "";
366 end Current_Section;
367
368 -----------------
369 -- Full_Switch --
370 -----------------
371
372 function Full_Switch
373 (Parser : Opt_Parser := Command_Line_Parser) return String
374 is
375 begin
376 if Parser.The_Switch.Extra = ASCII.NUL then
377 return Argument (Parser, Parser.The_Switch.Arg_Num)
378 (Parser.The_Switch.First .. Parser.The_Switch.Last);
379 else
380 return Parser.The_Switch.Extra
381 & Argument (Parser, Parser.The_Switch.Arg_Num)
382 (Parser.The_Switch.First .. Parser.The_Switch.Last);
383 end if;
384 end Full_Switch;
385
386 ------------------
387 -- Get_Argument --
388 ------------------
389
390 function Get_Argument
391 (Do_Expansion : Boolean := False;
392 Parser : Opt_Parser := Command_Line_Parser) return String
393 is
394 End_Of_Args : Boolean;
395 begin
396 return Get_Argument (Do_Expansion, Parser, End_Of_Args);
397 end Get_Argument;
398
399 ------------------
400 -- Get_Argument --
401 ------------------
402
403 function Get_Argument
404 (Do_Expansion : Boolean := False;
405 Parser : Opt_Parser := Command_Line_Parser;
406 End_Of_Arguments : out Boolean) return String is
407 begin
408 End_Of_Arguments := False;
409
410 if Parser.In_Expansion then
411 declare
412 S : constant String := Expansion (Parser.Expansion_It);
413 begin
414 if S'Length /= 0 then
415 return S;
416 else
417 Parser.In_Expansion := False;
418 end if;
419 end;
420 end if;
421
422 if Parser.Current_Argument > Parser.Arg_Count then
423
424 -- If this is the first time this function is called
425
426 if Parser.Current_Index = 1 then
427 Parser.Current_Argument := 1;
428 while Parser.Current_Argument <= Parser.Arg_Count
429 and then Parser.Section (Parser.Current_Argument) /=
430 Parser.Current_Section
431 loop
432 Parser.Current_Argument := Parser.Current_Argument + 1;
433 end loop;
434
435 else
436 End_Of_Arguments := True;
437 return String'(1 .. 0 => ' ');
438 end if;
439
440 elsif Parser.Section (Parser.Current_Argument) = 0 then
441 while Parser.Current_Argument <= Parser.Arg_Count
442 and then Parser.Section (Parser.Current_Argument) /=
443 Parser.Current_Section
444 loop
445 Parser.Current_Argument := Parser.Current_Argument + 1;
446 end loop;
447 end if;
448
449 Parser.Current_Index := Integer'Last;
450
451 while Parser.Current_Argument <= Parser.Arg_Count
452 and then Parser.Is_Switch (Parser.Current_Argument)
453 loop
454 Parser.Current_Argument := Parser.Current_Argument + 1;
455 end loop;
456
457 if Parser.Current_Argument > Parser.Arg_Count then
458 End_Of_Arguments := True;
459 return String'(1 .. 0 => ' ');
460
461 elsif Parser.Section (Parser.Current_Argument) = 0 then
462 return Get_Argument (Do_Expansion, Parser, End_Of_Arguments);
463 end if;
464
465 Parser.Current_Argument := Parser.Current_Argument + 1;
466
467 -- Could it be a file name with wildcards to expand?
468
469 if Do_Expansion then
470 declare
471 Arg : constant String :=
472 Argument (Parser, Parser.Current_Argument - 1);
473 begin
474 for Index in Arg'Range loop
475 if Arg (Index) in '*' | '?' | '[' then
476 Parser.In_Expansion := True;
477 Start_Expansion (Parser.Expansion_It, Arg);
478 return Get_Argument (Do_Expansion, Parser, End_Of_Arguments);
479 end if;
480 end loop;
481 end;
482 end if;
483
484 return Argument (Parser, Parser.Current_Argument - 1);
485 end Get_Argument;
486
487 ----------------------
488 -- Decompose_Switch --
489 ----------------------
490
491 procedure Decompose_Switch
492 (Switch : String;
493 Parameter_Type : out Switch_Parameter_Type;
494 Switch_Last : out Integer)
495 is
496 begin
497 if Switch = "" then
498 Parameter_Type := Parameter_None;
499 Switch_Last := Switch'Last;
500 return;
501 end if;
502
503 case Switch (Switch'Last) is
504 when ':' =>
505 Parameter_Type := Parameter_With_Optional_Space;
506 Switch_Last := Switch'Last - 1;
507
508 when '=' =>
509 Parameter_Type := Parameter_With_Space_Or_Equal;
510 Switch_Last := Switch'Last - 1;
511
512 when '!' =>
513 Parameter_Type := Parameter_No_Space;
514 Switch_Last := Switch'Last - 1;
515
516 when '?' =>
517 Parameter_Type := Parameter_Optional;
518 Switch_Last := Switch'Last - 1;
519
520 when others =>
521 Parameter_Type := Parameter_None;
522 Switch_Last := Switch'Last;
523 end case;
524 end Decompose_Switch;
525
526 ----------------------------------
527 -- Find_Longest_Matching_Switch --
528 ----------------------------------
529
530 procedure Find_Longest_Matching_Switch
531 (Switches : String;
532 Arg : String;
533 Index_In_Switches : out Integer;
534 Switch_Length : out Integer;
535 Param : out Switch_Parameter_Type)
536 is
537 Index : Natural;
538 Length : Natural := 1;
539 Last : Natural;
540 P : Switch_Parameter_Type;
541
542 begin
543 Param := Parameter_None;
544 Index_In_Switches := 0;
545 Switch_Length := 0;
546
547 -- Remove all leading spaces first to make sure that Index points
548 -- at the start of the first switch.
549
550 Index := Switches'First;
551 while Index <= Switches'Last and then Switches (Index) = ' ' loop
552 Index := Index + 1;
553 end loop;
554
555 while Index <= Switches'Last loop
556
557 -- Search the length of the parameter at this position in Switches
558
559 Length := Index;
560 while Length <= Switches'Last
561 and then Switches (Length) /= ' '
562 loop
563 Length := Length + 1;
564 end loop;
565
566 -- Length now marks the separator after the current switch. Last will
567 -- mark the last character of the name of the switch.
568
569 if Length = Index + 1 then
570 P := Parameter_None;
571 Last := Index;
572 else
573 Decompose_Switch (Switches (Index .. Length - 1), P, Last);
574 end if;
575
576 -- If it is the one we searched, it may be a candidate
577
578 if Arg'First + Last - Index <= Arg'Last
579 and then Switches (Index .. Last) =
580 Arg (Arg'First .. Arg'First + Last - Index)
581 and then Last - Index + 1 > Switch_Length
582 and then
583 (P /= Parameter_With_Space_Or_Equal
584 or else Arg'Last = Arg'First + Last - Index
585 or else Arg (Arg'First + Last - Index + 1) = '=')
586 then
587 Param := P;
588 Index_In_Switches := Index;
589 Switch_Length := Last - Index + 1;
590 end if;
591
592 -- Look for the next switch in Switches
593
594 while Index <= Switches'Last
595 and then Switches (Index) /= ' '
596 loop
597 Index := Index + 1;
598 end loop;
599
600 Index := Index + 1;
601 end loop;
602 end Find_Longest_Matching_Switch;
603
604 ------------
605 -- Getopt --
606 ------------
607
608 function Getopt
609 (Switches : String;
610 Concatenate : Boolean := True;
611 Parser : Opt_Parser := Command_Line_Parser) return Character
612 is
613 Dummy : Boolean;
614
615 begin
616 <<Restart>>
617
618 -- If we have finished parsing the current command line item (there
619 -- might be multiple switches in a single item), then go to the next
620 -- element.
621
622 if Parser.Current_Argument > Parser.Arg_Count
623 or else (Parser.Current_Index >
624 Argument (Parser, Parser.Current_Argument)'Last
625 and then not Goto_Next_Argument_In_Section (Parser))
626 then
627 return ASCII.NUL;
628 end if;
629
630 -- By default, the switch will not have a parameter
631
632 Parser.The_Parameter :=
633 (Integer'Last, Integer'Last, Integer'Last - 1, ASCII.NUL);
634 Parser.The_Separator := ASCII.NUL;
635
636 declare
637 Arg : constant String :=
638 Argument (Parser, Parser.Current_Argument);
639 Index_Switches : Natural := 0;
640 Max_Length : Natural := 0;
641 End_Index : Natural;
642 Param : Switch_Parameter_Type;
643 begin
644 -- If we are on a new item, test if this might be a switch
645
646 if Parser.Current_Index = Arg'First then
647 if Arg = "" or else Arg (Arg'First) /= Parser.Switch_Character then
648
649 -- If it isn't a switch, return it immediately. We also know it
650 -- isn't the parameter to a previous switch, since that has
651 -- already been handled.
652
653 if Switches (Switches'First) = '*' then
654 Set_Parameter
655 (Parser.The_Switch,
656 Arg_Num => Parser.Current_Argument,
657 First => Arg'First,
658 Last => Arg'Last);
659 Parser.Is_Switch (Parser.Current_Argument) := True;
660 Dummy := Goto_Next_Argument_In_Section (Parser);
661 return '*';
662 end if;
663
664 if Parser.Stop_At_First then
665 Parser.Current_Argument := Positive'Last;
666 return ASCII.NUL;
667
668 elsif not Goto_Next_Argument_In_Section (Parser) then
669 return ASCII.NUL;
670
671 else
672 -- Recurse to get the next switch on the command line
673
674 goto Restart;
675 end if;
676 end if;
677
678 -- We are on the first character of a new command line argument,
679 -- which starts with Switch_Character. Further analysis is needed.
680
681 Parser.Current_Index := Parser.Current_Index + 1;
682 Parser.Is_Switch (Parser.Current_Argument) := True;
683 end if;
684
685 Find_Longest_Matching_Switch
686 (Switches => Switches,
687 Arg => Arg (Parser.Current_Index .. Arg'Last),
688 Index_In_Switches => Index_Switches,
689 Switch_Length => Max_Length,
690 Param => Param);
691
692 -- If switch is not accepted, it is either invalid or is returned
693 -- in the context of '*'.
694
695 if Index_Switches = 0 then
696
697 -- Find the current switch that we did not recognize. This is in
698 -- fact difficult because Getopt does not know explicitly about
699 -- short and long switches. Ideally, we would want the following
700 -- behavior:
701
702 -- * for short switches, with Concatenate:
703 -- if -a is not recognized, and the command line has -daf
704 -- we should report the invalid switch as "-a".
705
706 -- * for short switches, wihtout Concatenate:
707 -- we should report the invalid switch as "-daf".
708
709 -- * for long switches:
710 -- if the commadn line is "--long" we should report --long
711 -- as unrecongized.
712
713 -- Unfortunately, the fact that long switches start with a
714 -- duplicate switch character is just a convention (so we could
715 -- have a long switch "-long" for instance). We'll still rely on
716 -- this convention here to try and get as helpful an error message
717 -- as possible.
718
719 -- Long switch case (starting with double switch character)
720
721 if Arg (Arg'First + 1) = Parser.Switch_Character then
722 End_Index := Arg'Last;
723
724 -- Short switch case
725
726 else
727 End_Index :=
728 (if Concatenate then Parser.Current_Index else Arg'Last);
729 end if;
730
731 if Switches /= "" and then Switches (Switches'First) = '*' then
732
733 -- Always prepend the switch character, so that users know
734 -- that this comes from a switch on the command line. This
735 -- is especially important when Concatenate is False, since
736 -- otherwise the current argument first character is lost.
737
738 if Parser.Section (Parser.Current_Argument) = 0 then
739
740 -- A section transition should not be returned to the user
741
742 Dummy := Goto_Next_Argument_In_Section (Parser);
743 goto Restart;
744
745 else
746 Set_Parameter
747 (Parser.The_Switch,
748 Arg_Num => Parser.Current_Argument,
749 First => Parser.Current_Index,
750 Last => Arg'Last,
751 Extra => Parser.Switch_Character);
752 Parser.Is_Switch (Parser.Current_Argument) := True;
753 Dummy := Goto_Next_Argument_In_Section (Parser);
754 return '*';
755 end if;
756 end if;
757
758 if Parser.Current_Index = Arg'First then
759 Set_Parameter
760 (Parser.The_Switch,
761 Arg_Num => Parser.Current_Argument,
762 First => Parser.Current_Index,
763 Last => End_Index);
764 else
765 Set_Parameter
766 (Parser.The_Switch,
767 Arg_Num => Parser.Current_Argument,
768 First => Parser.Current_Index,
769 Last => End_Index,
770 Extra => Parser.Switch_Character);
771 end if;
772
773 Parser.Current_Index := End_Index + 1;
774
775 raise Invalid_Switch with
776 "Unrecognized option '" & Full_Switch (Parser) & ''';
777 end if;
778
779 End_Index := Parser.Current_Index + Max_Length - 1;
780 Set_Parameter
781 (Parser.The_Switch,
782 Arg_Num => Parser.Current_Argument,
783 First => Parser.Current_Index,
784 Last => End_Index);
785
786 case Param is
787 when Parameter_With_Optional_Space =>
788 if End_Index < Arg'Last then
789 Set_Parameter
790 (Parser.The_Parameter,
791 Arg_Num => Parser.Current_Argument,
792 First => End_Index + 1,
793 Last => Arg'Last);
794 Dummy := Goto_Next_Argument_In_Section (Parser);
795
796 elsif Parser.Current_Argument < Parser.Arg_Count
797 and then Parser.Section (Parser.Current_Argument + 1) /= 0
798 then
799 Parser.Current_Argument := Parser.Current_Argument + 1;
800 Parser.The_Separator := ' ';
801 Set_Parameter
802 (Parser.The_Parameter,
803 Arg_Num => Parser.Current_Argument,
804 First => Argument (Parser, Parser.Current_Argument)'First,
805 Last => Argument (Parser, Parser.Current_Argument)'Last);
806 Parser.Is_Switch (Parser.Current_Argument) := True;
807 Dummy := Goto_Next_Argument_In_Section (Parser);
808
809 else
810 Parser.Current_Index := End_Index + 1;
811 raise Invalid_Parameter;
812 end if;
813
814 when Parameter_With_Space_Or_Equal =>
815
816 -- If the switch is of the form <switch>=xxx
817
818 if End_Index < Arg'Last then
819 if Arg (End_Index + 1) = '='
820 and then End_Index + 1 < Arg'Last
821 then
822 Parser.The_Separator := '=';
823 Set_Parameter
824 (Parser.The_Parameter,
825 Arg_Num => Parser.Current_Argument,
826 First => End_Index + 2,
827 Last => Arg'Last);
828 Dummy := Goto_Next_Argument_In_Section (Parser);
829
830 else
831 Parser.Current_Index := End_Index + 1;
832 raise Invalid_Parameter;
833 end if;
834
835 -- Case of switch of the form <switch> xxx
836
837 elsif Parser.Current_Argument < Parser.Arg_Count
838 and then Parser.Section (Parser.Current_Argument + 1) /= 0
839 then
840 Parser.Current_Argument := Parser.Current_Argument + 1;
841 Parser.The_Separator := ' ';
842 Set_Parameter
843 (Parser.The_Parameter,
844 Arg_Num => Parser.Current_Argument,
845 First => Argument (Parser, Parser.Current_Argument)'First,
846 Last => Argument (Parser, Parser.Current_Argument)'Last);
847 Parser.Is_Switch (Parser.Current_Argument) := True;
848 Dummy := Goto_Next_Argument_In_Section (Parser);
849
850 else
851 Parser.Current_Index := End_Index + 1;
852 raise Invalid_Parameter;
853 end if;
854
855 when Parameter_No_Space =>
856 if End_Index < Arg'Last then
857 Set_Parameter
858 (Parser.The_Parameter,
859 Arg_Num => Parser.Current_Argument,
860 First => End_Index + 1,
861 Last => Arg'Last);
862 Dummy := Goto_Next_Argument_In_Section (Parser);
863
864 else
865 Parser.Current_Index := End_Index + 1;
866 raise Invalid_Parameter;
867 end if;
868
869 when Parameter_Optional =>
870 if End_Index < Arg'Last then
871 Set_Parameter
872 (Parser.The_Parameter,
873 Arg_Num => Parser.Current_Argument,
874 First => End_Index + 1,
875 Last => Arg'Last);
876 end if;
877
878 Dummy := Goto_Next_Argument_In_Section (Parser);
879
880 when Parameter_None =>
881 if Concatenate or else End_Index = Arg'Last then
882 Parser.Current_Index := End_Index + 1;
883
884 else
885 -- If Concatenate is False and the full argument is not
886 -- recognized as a switch, this is an invalid switch.
887
888 if Switches (Switches'First) = '*' then
889 Set_Parameter
890 (Parser.The_Switch,
891 Arg_Num => Parser.Current_Argument,
892 First => Arg'First,
893 Last => Arg'Last);
894 Parser.Is_Switch (Parser.Current_Argument) := True;
895 Dummy := Goto_Next_Argument_In_Section (Parser);
896 return '*';
897 end if;
898
899 Set_Parameter
900 (Parser.The_Switch,
901 Arg_Num => Parser.Current_Argument,
902 First => Parser.Current_Index,
903 Last => Arg'Last,
904 Extra => Parser.Switch_Character);
905 Parser.Current_Index := Arg'Last + 1;
906 raise Invalid_Switch with
907 "Unrecognized option '" & Full_Switch (Parser) & ''';
908 end if;
909 end case;
910
911 return Switches (Index_Switches);
912 end;
913 end Getopt;
914
915 -----------------------------------
916 -- Goto_Next_Argument_In_Section --
917 -----------------------------------
918
919 function Goto_Next_Argument_In_Section
920 (Parser : Opt_Parser) return Boolean
921 is
922 begin
923 Parser.Current_Argument := Parser.Current_Argument + 1;
924
925 if Parser.Current_Argument > Parser.Arg_Count
926 or else Parser.Section (Parser.Current_Argument) = 0
927 then
928 loop
929 Parser.Current_Argument := Parser.Current_Argument + 1;
930
931 if Parser.Current_Argument > Parser.Arg_Count then
932 Parser.Current_Index := 1;
933 return False;
934 end if;
935
936 exit when Parser.Section (Parser.Current_Argument) =
937 Parser.Current_Section;
938 end loop;
939 end if;
940
941 Parser.Current_Index :=
942 Argument (Parser, Parser.Current_Argument)'First;
943
944 return True;
945 end Goto_Next_Argument_In_Section;
946
947 ------------------
948 -- Goto_Section --
949 ------------------
950
951 procedure Goto_Section
952 (Name : String := "";
953 Parser : Opt_Parser := Command_Line_Parser)
954 is
955 Index : Integer;
956
957 begin
958 Parser.In_Expansion := False;
959
960 if Name = "" then
961 Parser.Current_Argument := 1;
962 Parser.Current_Index := 1;
963 Parser.Current_Section := 1;
964 return;
965 end if;
966
967 Index := 1;
968 while Index <= Parser.Arg_Count loop
969 if Parser.Section (Index) = 0
970 and then Argument (Parser, Index) = Parser.Switch_Character & Name
971 then
972 Parser.Current_Argument := Index + 1;
973 Parser.Current_Index := 1;
974
975 if Parser.Current_Argument <= Parser.Arg_Count then
976 Parser.Current_Section :=
977 Parser.Section (Parser.Current_Argument);
978 end if;
979
980 -- Exit from loop if we have the start of another section
981
982 if Index = Parser.Section'Last
983 or else Parser.Section (Index + 1) /= 0
984 then
985 return;
986 end if;
987 end if;
988
989 Index := Index + 1;
990 end loop;
991
992 Parser.Current_Argument := Positive'Last;
993 Parser.Current_Index := 2; -- so that Get_Argument returns nothing
994 end Goto_Section;
995
996 ----------------------------
997 -- Initialize_Option_Scan --
998 ----------------------------
999
1000 procedure Initialize_Option_Scan
1001 (Switch_Char : Character := '-';
1002 Stop_At_First_Non_Switch : Boolean := False;
1003 Section_Delimiters : String := "")
1004 is
1005 begin
1006 Internal_Initialize_Option_Scan
1007 (Parser => Command_Line_Parser,
1008 Switch_Char => Switch_Char,
1009 Stop_At_First_Non_Switch => Stop_At_First_Non_Switch,
1010 Section_Delimiters => Section_Delimiters);
1011 end Initialize_Option_Scan;
1012
1013 ----------------------------
1014 -- Initialize_Option_Scan --
1015 ----------------------------
1016
1017 procedure Initialize_Option_Scan
1018 (Parser : out Opt_Parser;
1019 Command_Line : GNAT.OS_Lib.Argument_List_Access;
1020 Switch_Char : Character := '-';
1021 Stop_At_First_Non_Switch : Boolean := False;
1022 Section_Delimiters : String := "")
1023 is
1024 begin
1025 Free (Parser);
1026
1027 if Command_Line = null then
1028 Parser := new Opt_Parser_Data (CL.Argument_Count);
1029 Internal_Initialize_Option_Scan
1030 (Parser => Parser,
1031 Switch_Char => Switch_Char,
1032 Stop_At_First_Non_Switch => Stop_At_First_Non_Switch,
1033 Section_Delimiters => Section_Delimiters);
1034 else
1035 Parser := new Opt_Parser_Data (Command_Line'Length);
1036 Parser.Arguments := Command_Line;
1037 Internal_Initialize_Option_Scan
1038 (Parser => Parser,
1039 Switch_Char => Switch_Char,
1040 Stop_At_First_Non_Switch => Stop_At_First_Non_Switch,
1041 Section_Delimiters => Section_Delimiters);
1042 end if;
1043 end Initialize_Option_Scan;
1044
1045 -------------------------------------
1046 -- Internal_Initialize_Option_Scan --
1047 -------------------------------------
1048
1049 procedure Internal_Initialize_Option_Scan
1050 (Parser : Opt_Parser;
1051 Switch_Char : Character;
1052 Stop_At_First_Non_Switch : Boolean;
1053 Section_Delimiters : String)
1054 is
1055 Section_Num : Section_Number;
1056 Section_Index : Integer;
1057 Last : Integer;
1058 Delimiter_Found : Boolean;
1059
1060 Discard : Boolean;
1061 pragma Warnings (Off, Discard);
1062
1063 begin
1064 Parser.Current_Argument := 0;
1065 Parser.Current_Index := 0;
1066 Parser.In_Expansion := False;
1067 Parser.Switch_Character := Switch_Char;
1068 Parser.Stop_At_First := Stop_At_First_Non_Switch;
1069 Parser.Section := (others => 1);
1070
1071 -- If we are using sections, we have to preprocess the command line to
1072 -- delimit them. A section can be repeated, so we just give each item
1073 -- on the command line a section number
1074
1075 Section_Num := 1;
1076 Section_Index := Section_Delimiters'First;
1077 while Section_Index <= Section_Delimiters'Last loop
1078 Last := Section_Index;
1079 while Last <= Section_Delimiters'Last
1080 and then Section_Delimiters (Last) /= ' '
1081 loop
1082 Last := Last + 1;
1083 end loop;
1084
1085 Delimiter_Found := False;
1086 Section_Num := Section_Num + 1;
1087
1088 for Index in 1 .. Parser.Arg_Count loop
1089 pragma Assert (Argument (Parser, Index)'First = 1);
1090 if Argument (Parser, Index) /= ""
1091 and then Argument (Parser, Index)(1) = Parser.Switch_Character
1092 and then
1093 Argument (Parser, Index) = Parser.Switch_Character &
1094 Section_Delimiters
1095 (Section_Index .. Last - 1)
1096 then
1097 Parser.Section (Index) := 0;
1098 Delimiter_Found := True;
1099
1100 elsif Parser.Section (Index) = 0 then
1101
1102 -- A previous section delimiter
1103
1104 Delimiter_Found := False;
1105
1106 elsif Delimiter_Found then
1107 Parser.Section (Index) := Section_Num;
1108 end if;
1109 end loop;
1110
1111 Section_Index := Last + 1;
1112 while Section_Index <= Section_Delimiters'Last
1113 and then Section_Delimiters (Section_Index) = ' '
1114 loop
1115 Section_Index := Section_Index + 1;
1116 end loop;
1117 end loop;
1118
1119 Discard := Goto_Next_Argument_In_Section (Parser);
1120 end Internal_Initialize_Option_Scan;
1121
1122 ---------------
1123 -- Parameter --
1124 ---------------
1125
1126 function Parameter
1127 (Parser : Opt_Parser := Command_Line_Parser) return String
1128 is
1129 begin
1130 if Parser.The_Parameter.First > Parser.The_Parameter.Last then
1131 return String'(1 .. 0 => ' ');
1132 else
1133 return Argument (Parser, Parser.The_Parameter.Arg_Num)
1134 (Parser.The_Parameter.First .. Parser.The_Parameter.Last);
1135 end if;
1136 end Parameter;
1137
1138 ---------------
1139 -- Separator --
1140 ---------------
1141
1142 function Separator
1143 (Parser : Opt_Parser := Command_Line_Parser) return Character
1144 is
1145 begin
1146 return Parser.The_Separator;
1147 end Separator;
1148
1149 -------------------
1150 -- Set_Parameter --
1151 -------------------
1152
1153 procedure Set_Parameter
1154 (Variable : out Parameter_Type;
1155 Arg_Num : Positive;
1156 First : Positive;
1157 Last : Natural;
1158 Extra : Character := ASCII.NUL)
1159 is
1160 begin
1161 Variable.Arg_Num := Arg_Num;
1162 Variable.First := First;
1163 Variable.Last := Last;
1164 Variable.Extra := Extra;
1165 end Set_Parameter;
1166
1167 ---------------------
1168 -- Start_Expansion --
1169 ---------------------
1170
1171 procedure Start_Expansion
1172 (Iterator : out Expansion_Iterator;
1173 Pattern : String;
1174 Directory : String := "";
1175 Basic_Regexp : Boolean := True)
1176 is
1177 Directory_Separator : Character;
1178 pragma Import (C, Directory_Separator, "__gnat_dir_separator");
1179
1180 First : Positive := Pattern'First;
1181 Pat : String := Pattern;
1182
1183 begin
1184 Canonical_Case_File_Name (Pat);
1185 Iterator.Current_Depth := 1;
1186
1187 -- If Directory is unspecified, use the current directory ("./" or ".\")
1188
1189 if Directory = "" then
1190 Iterator.Dir_Name (1 .. 2) := "." & Directory_Separator;
1191 Iterator.Start := 3;
1192
1193 else
1194 Iterator.Dir_Name (1 .. Directory'Length) := Directory;
1195 Iterator.Start := Directory'Length + 1;
1196 Canonical_Case_File_Name (Iterator.Dir_Name (1 .. Directory'Length));
1197
1198 -- Make sure that the last character is a directory separator
1199
1200 if Directory (Directory'Last) /= Directory_Separator then
1201 Iterator.Dir_Name (Iterator.Start) := Directory_Separator;
1202 Iterator.Start := Iterator.Start + 1;
1203 end if;
1204 end if;
1205
1206 Iterator.Levels (1).Name_Last := Iterator.Start - 1;
1207
1208 -- Open the initial Directory, at depth 1
1209
1210 GNAT.Directory_Operations.Open
1211 (Iterator.Levels (1).Dir, Iterator.Dir_Name (1 .. Iterator.Start - 1));
1212
1213 -- If in the current directory and the pattern starts with "./" or ".\",
1214 -- drop the "./" or ".\" from the pattern.
1215
1216 if Directory = "" and then Pat'Length > 2
1217 and then Pat (Pat'First) = '.'
1218 and then Pat (Pat'First + 1) = Directory_Separator
1219 then
1220 First := Pat'First + 2;
1221 end if;
1222
1223 Iterator.Regexp :=
1224 GNAT.Regexp.Compile (Pat (First .. Pat'Last), Basic_Regexp, True);
1225
1226 Iterator.Maximum_Depth := 1;
1227
1228 -- Maximum_Depth is equal to 1 plus the number of directory separators
1229 -- in the pattern.
1230
1231 for Index in First .. Pat'Last loop
1232 if Pat (Index) = Directory_Separator then
1233 Iterator.Maximum_Depth := Iterator.Maximum_Depth + 1;
1234 exit when Iterator.Maximum_Depth = Max_Depth;
1235 end if;
1236 end loop;
1237 end Start_Expansion;
1238
1239 ----------
1240 -- Free --
1241 ----------
1242
1243 procedure Free (Parser : in out Opt_Parser) is
1244 procedure Unchecked_Free is new
1245 Ada.Unchecked_Deallocation (Opt_Parser_Data, Opt_Parser);
1246 begin
1247 if Parser /= null and then Parser /= Command_Line_Parser then
1248 Free (Parser.Arguments);
1249 Unchecked_Free (Parser);
1250 end if;
1251 end Free;
1252
1253 ------------------
1254 -- Define_Alias --
1255 ------------------
1256
1257 procedure Define_Alias
1258 (Config : in out Command_Line_Configuration;
1259 Switch : String;
1260 Expanded : String;
1261 Section : String := "")
1262 is
1263 Def : Alias_Definition;
1264
1265 begin
1266 if Config = null then
1267 Config := new Command_Line_Configuration_Record;
1268 end if;
1269
1270 Def.Alias := new String'(Switch);
1271 Def.Expansion := new String'(Expanded);
1272 Def.Section := new String'(Section);
1273 Add (Config.Aliases, Def);
1274 end Define_Alias;
1275
1276 -------------------
1277 -- Define_Prefix --
1278 -------------------
1279
1280 procedure Define_Prefix
1281 (Config : in out Command_Line_Configuration;
1282 Prefix : String)
1283 is
1284 begin
1285 if Config = null then
1286 Config := new Command_Line_Configuration_Record;
1287 end if;
1288
1289 Add (Config.Prefixes, new String'(Prefix));
1290 end Define_Prefix;
1291
1292 ---------
1293 -- Add --
1294 ---------
1295
1296 procedure Add
1297 (Config : in out Command_Line_Configuration;
1298 Switch : Switch_Definition)
1299 is
1300 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
1301 (Switch_Definitions, Switch_Definitions_List);
1302
1303 Tmp : Switch_Definitions_List;
1304
1305 begin
1306 if Config = null then
1307 Config := new Command_Line_Configuration_Record;
1308 end if;
1309
1310 Tmp := Config.Switches;
1311
1312 if Tmp = null then
1313 Config.Switches := new Switch_Definitions (1 .. 1);
1314 else
1315 Config.Switches := new Switch_Definitions (1 .. Tmp'Length + 1);
1316 Config.Switches (1 .. Tmp'Length) := Tmp.all;
1317 Unchecked_Free (Tmp);
1318 end if;
1319
1320 if Switch.Switch /= null and then Switch.Switch.all = "*" then
1321 Config.Star_Switch := True;
1322 end if;
1323
1324 Config.Switches (Config.Switches'Last) := Switch;
1325 end Add;
1326
1327 ---------
1328 -- Add --
1329 ---------
1330
1331 procedure Add
1332 (Def : in out Alias_Definitions_List;
1333 Alias : Alias_Definition)
1334 is
1335 procedure Unchecked_Free is new
1336 Ada.Unchecked_Deallocation
1337 (Alias_Definitions, Alias_Definitions_List);
1338
1339 Tmp : Alias_Definitions_List := Def;
1340
1341 begin
1342 if Tmp = null then
1343 Def := new Alias_Definitions (1 .. 1);
1344 else
1345 Def := new Alias_Definitions (1 .. Tmp'Length + 1);
1346 Def (1 .. Tmp'Length) := Tmp.all;
1347 Unchecked_Free (Tmp);
1348 end if;
1349
1350 Def (Def'Last) := Alias;
1351 end Add;
1352
1353 ---------------------------
1354 -- Initialize_Switch_Def --
1355 ---------------------------
1356
1357 procedure Initialize_Switch_Def
1358 (Def : out Switch_Definition;
1359 Switch : String := "";
1360 Long_Switch : String := "";
1361 Help : String := "";
1362 Section : String := "";
1363 Argument : String := "ARG")
1364 is
1365 P1, P2 : Switch_Parameter_Type := Parameter_None;
1366 Last1, Last2 : Integer;
1367
1368 begin
1369 if Switch /= "" then
1370 Def.Switch := new String'(Switch);
1371 Decompose_Switch (Switch, P1, Last1);
1372 end if;
1373
1374 if Long_Switch /= "" then
1375 Def.Long_Switch := new String'(Long_Switch);
1376 Decompose_Switch (Long_Switch, P2, Last2);
1377 end if;
1378
1379 if Switch /= "" and then Long_Switch /= "" then
1380 if (P1 = Parameter_None and then P2 /= P1)
1381 or else (P2 = Parameter_None and then P1 /= P2)
1382 or else (P1 = Parameter_Optional and then P2 /= P1)
1383 or else (P2 = Parameter_Optional and then P2 /= P1)
1384 then
1385 raise Invalid_Switch
1386 with "Inconsistent parameter types for "
1387 & Switch & " and " & Long_Switch;
1388 end if;
1389 end if;
1390
1391 if Section /= "" then
1392 Def.Section := new String'(Section);
1393 end if;
1394
1395 if Argument /= "ARG" then
1396 Def.Argument := new String'(Argument);
1397 end if;
1398
1399 if Help /= "" then
1400 Def.Help := new String'(Help);
1401 end if;
1402 end Initialize_Switch_Def;
1403
1404 -------------------
1405 -- Define_Switch --
1406 -------------------
1407
1408 procedure Define_Switch
1409 (Config : in out Command_Line_Configuration;
1410 Switch : String := "";
1411 Long_Switch : String := "";
1412 Help : String := "";
1413 Section : String := "";
1414 Argument : String := "ARG")
1415 is
1416 Def : Switch_Definition;
1417 begin
1418 if Switch /= "" or else Long_Switch /= "" then
1419 Initialize_Switch_Def
1420 (Def, Switch, Long_Switch, Help, Section, Argument);
1421 Add (Config, Def);
1422 end if;
1423 end Define_Switch;
1424
1425 -------------------
1426 -- Define_Switch --
1427 -------------------
1428
1429 procedure Define_Switch
1430 (Config : in out Command_Line_Configuration;
1431 Output : access Boolean;
1432 Switch : String := "";
1433 Long_Switch : String := "";
1434 Help : String := "";
1435 Section : String := "";
1436 Value : Boolean := True)
1437 is
1438 Def : Switch_Definition (Switch_Boolean);
1439 begin
1440 if Switch /= "" or else Long_Switch /= "" then
1441 Initialize_Switch_Def (Def, Switch, Long_Switch, Help, Section);
1442 Def.Boolean_Output := Output.all'Unchecked_Access;
1443 Def.Boolean_Value := Value;
1444 Add (Config, Def);
1445 end if;
1446 end Define_Switch;
1447
1448 -------------------
1449 -- Define_Switch --
1450 -------------------
1451
1452 procedure Define_Switch
1453 (Config : in out Command_Line_Configuration;
1454 Output : access Integer;
1455 Switch : String := "";
1456 Long_Switch : String := "";
1457 Help : String := "";
1458 Section : String := "";
1459 Initial : Integer := 0;
1460 Default : Integer := 1;
1461 Argument : String := "ARG")
1462 is
1463 Def : Switch_Definition (Switch_Integer);
1464 begin
1465 if Switch /= "" or else Long_Switch /= "" then
1466 Initialize_Switch_Def
1467 (Def, Switch, Long_Switch, Help, Section, Argument);
1468 Def.Integer_Output := Output.all'Unchecked_Access;
1469 Def.Integer_Default := Default;
1470 Def.Integer_Initial := Initial;
1471 Add (Config, Def);
1472 end if;
1473 end Define_Switch;
1474
1475 -------------------
1476 -- Define_Switch --
1477 -------------------
1478
1479 procedure Define_Switch
1480 (Config : in out Command_Line_Configuration;
1481 Output : access GNAT.Strings.String_Access;
1482 Switch : String := "";
1483 Long_Switch : String := "";
1484 Help : String := "";
1485 Section : String := "";
1486 Argument : String := "ARG")
1487 is
1488 Def : Switch_Definition (Switch_String);
1489 begin
1490 if Switch /= "" or else Long_Switch /= "" then
1491 Initialize_Switch_Def
1492 (Def, Switch, Long_Switch, Help, Section, Argument);
1493 Def.String_Output := Output.all'Unchecked_Access;
1494 Add (Config, Def);
1495 end if;
1496 end Define_Switch;
1497
1498 -------------------
1499 -- Define_Switch --
1500 -------------------
1501
1502 procedure Define_Switch
1503 (Config : in out Command_Line_Configuration;
1504 Callback : not null Value_Callback;
1505 Switch : String := "";
1506 Long_Switch : String := "";
1507 Help : String := "";
1508 Section : String := "";
1509 Argument : String := "ARG")
1510 is
1511 Def : Switch_Definition (Switch_Callback);
1512 begin
1513 if Switch /= "" or else Long_Switch /= "" then
1514 Initialize_Switch_Def
1515 (Def, Switch, Long_Switch, Help, Section, Argument);
1516 Def.Callback := Callback;
1517 Add (Config, Def);
1518 end if;
1519 end Define_Switch;
1520
1521 --------------------
1522 -- Define_Section --
1523 --------------------
1524
1525 procedure Define_Section
1526 (Config : in out Command_Line_Configuration;
1527 Section : String)
1528 is
1529 begin
1530 if Config = null then
1531 Config := new Command_Line_Configuration_Record;
1532 end if;
1533
1534 Add (Config.Sections, new String'(Section));
1535 end Define_Section;
1536
1537 --------------------
1538 -- Foreach_Switch --
1539 --------------------
1540
1541 procedure Foreach_Switch
1542 (Config : Command_Line_Configuration;
1543 Section : String)
1544 is
1545 begin
1546 if Config /= null and then Config.Switches /= null then
1547 for J in Config.Switches'Range loop
1548 if (Section = "" and then Config.Switches (J).Section = null)
1549 or else
1550 (Config.Switches (J).Section /= null
1551 and then Config.Switches (J).Section.all = Section)
1552 then
1553 exit when Config.Switches (J).Switch /= null
1554 and then not Callback (Config.Switches (J).Switch.all, J);
1555
1556 exit when Config.Switches (J).Long_Switch /= null
1557 and then
1558 not Callback (Config.Switches (J).Long_Switch.all, J);
1559 end if;
1560 end loop;
1561 end if;
1562 end Foreach_Switch;
1563
1564 ------------------
1565 -- Get_Switches --
1566 ------------------
1567
1568 function Get_Switches
1569 (Config : Command_Line_Configuration;
1570 Switch_Char : Character := '-';
1571 Section : String := "") return String
1572 is
1573 Ret : Ada.Strings.Unbounded.Unbounded_String;
1574 use Ada.Strings.Unbounded;
1575
1576 function Add_Switch (S : String; Index : Integer) return Boolean;
1577 -- Add a switch to Ret
1578
1579 ----------------
1580 -- Add_Switch --
1581 ----------------
1582
1583 function Add_Switch (S : String; Index : Integer) return Boolean is
1584 pragma Unreferenced (Index);
1585 begin
1586 if S = "*" then
1587 Ret := "*" & Ret; -- Always first
1588 elsif S (S'First) = Switch_Char then
1589 Append (Ret, " " & S (S'First + 1 .. S'Last));
1590 else
1591 Append (Ret, " " & S);
1592 end if;
1593
1594 return True;
1595 end Add_Switch;
1596
1597 Tmp : Boolean;
1598 pragma Unreferenced (Tmp);
1599
1600 procedure Foreach is new Foreach_Switch (Add_Switch);
1601
1602 -- Start of processing for Get_Switches
1603
1604 begin
1605 if Config = null then
1606 return "";
1607 end if;
1608
1609 Foreach (Config, Section => Section);
1610
1611 -- Add relevant aliases
1612
1613 if Config.Aliases /= null then
1614 for A in Config.Aliases'Range loop
1615 if Config.Aliases (A).Section.all = Section then
1616 Tmp := Add_Switch (Config.Aliases (A).Alias.all, -1);
1617 end if;
1618 end loop;
1619 end if;
1620
1621 return To_String (Ret);
1622 end Get_Switches;
1623
1624 ------------------------
1625 -- Section_Delimiters --
1626 ------------------------
1627
1628 function Section_Delimiters
1629 (Config : Command_Line_Configuration) return String
1630 is
1631 use Ada.Strings.Unbounded;
1632 Result : Unbounded_String;
1633
1634 begin
1635 if Config /= null and then Config.Sections /= null then
1636 for S in Config.Sections'Range loop
1637 Append (Result, " " & Config.Sections (S).all);
1638 end loop;
1639 end if;
1640
1641 return To_String (Result);
1642 end Section_Delimiters;
1643
1644 -----------------------
1645 -- Set_Configuration --
1646 -----------------------
1647
1648 procedure Set_Configuration
1649 (Cmd : in out Command_Line;
1650 Config : Command_Line_Configuration)
1651 is
1652 begin
1653 Cmd.Config := Config;
1654 end Set_Configuration;
1655
1656 -----------------------
1657 -- Get_Configuration --
1658 -----------------------
1659
1660 function Get_Configuration
1661 (Cmd : Command_Line) return Command_Line_Configuration
1662 is
1663 begin
1664 return Cmd.Config;
1665 end Get_Configuration;
1666
1667 ----------------------
1668 -- Set_Command_Line --
1669 ----------------------
1670
1671 procedure Set_Command_Line
1672 (Cmd : in out Command_Line;
1673 Switches : String;
1674 Getopt_Description : String := "";
1675 Switch_Char : Character := '-')
1676 is
1677 Tmp : Argument_List_Access;
1678 Parser : Opt_Parser;
1679 S : Character;
1680 Section : String_Access := null;
1681
1682 function Real_Full_Switch
1683 (S : Character;
1684 Parser : Opt_Parser) return String;
1685 -- Ensure that the returned switch value contains the Switch_Char prefix
1686 -- if needed.
1687
1688 ----------------------
1689 -- Real_Full_Switch --
1690 ----------------------
1691
1692 function Real_Full_Switch
1693 (S : Character;
1694 Parser : Opt_Parser) return String
1695 is
1696 begin
1697 if S = '*' then
1698 return Full_Switch (Parser);
1699 else
1700 return Switch_Char & Full_Switch (Parser);
1701 end if;
1702 end Real_Full_Switch;
1703
1704 -- Start of processing for Set_Command_Line
1705
1706 begin
1707 Free (Cmd.Expanded);
1708 Free (Cmd.Params);
1709
1710 if Switches /= "" then
1711 Tmp := Argument_String_To_List (Switches);
1712 Initialize_Option_Scan (Parser, Tmp, Switch_Char);
1713
1714 loop
1715 begin
1716 if Cmd.Config /= null then
1717
1718 -- Do not use Getopt_Description in this case. Otherwise,
1719 -- if we have defined a prefix -gnaty, and two switches
1720 -- -gnatya and -gnatyL!, we would have a different behavior
1721 -- depending on the order of switches:
1722
1723 -- -gnatyL1a => -gnatyL with argument "1a"
1724 -- -gnatyaL1 => -gnatya and -gnatyL with argument "1"
1725
1726 -- This is because the call to Getopt below knows nothing
1727 -- about prefixes, and in the first case finds a valid
1728 -- switch with arguments, so returns it without analyzing
1729 -- the argument. In the second case, the switch matches "*",
1730 -- and is then decomposed below.
1731
1732 -- Note: When a Command_Line object is associated with a
1733 -- Command_Line_Config (which is mostly the case for tools
1734 -- that let users choose the command line before spawning
1735 -- other tools, for instance IDEs), the configuration of
1736 -- the switches must be taken from the Command_Line_Config.
1737
1738 S := Getopt (Switches => "* " & Get_Switches (Cmd.Config),
1739 Concatenate => False,
1740 Parser => Parser);
1741
1742 else
1743 S := Getopt (Switches => "* " & Getopt_Description,
1744 Concatenate => False,
1745 Parser => Parser);
1746 end if;
1747
1748 exit when S = ASCII.NUL;
1749
1750 declare
1751 Sw : constant String := Real_Full_Switch (S, Parser);
1752 Is_Section : Boolean := False;
1753
1754 begin
1755 if Cmd.Config /= null
1756 and then Cmd.Config.Sections /= null
1757 then
1758 Section_Search :
1759 for S in Cmd.Config.Sections'Range loop
1760 if Sw = Cmd.Config.Sections (S).all then
1761 Section := Cmd.Config.Sections (S);
1762 Is_Section := True;
1763
1764 exit Section_Search;
1765 end if;
1766 end loop Section_Search;
1767 end if;
1768
1769 if not Is_Section then
1770 if Section = null then
1771 Add_Switch (Cmd, Sw, Parameter (Parser));
1772 else
1773 Add_Switch
1774 (Cmd, Sw, Parameter (Parser),
1775 Section => Section.all);
1776 end if;
1777 end if;
1778 end;
1779
1780 exception
1781 when Invalid_Parameter =>
1782
1783 -- Add it with no parameter, if that's the way the user
1784 -- wants it.
1785
1786 -- Specify the separator in all cases, as the switch might
1787 -- need to be unaliased, and the alias might contain
1788 -- switches with parameters.
1789
1790 if Section = null then
1791 Add_Switch
1792 (Cmd, Switch_Char & Full_Switch (Parser));
1793 else
1794 Add_Switch
1795 (Cmd, Switch_Char & Full_Switch (Parser),
1796 Section => Section.all);
1797 end if;
1798 end;
1799 end loop;
1800
1801 Free (Parser);
1802 end if;
1803 end Set_Command_Line;
1804
1805 ----------------
1806 -- Looking_At --
1807 ----------------
1808
1809 function Looking_At
1810 (Type_Str : String;
1811 Index : Natural;
1812 Substring : String) return Boolean
1813 is
1814 begin
1815 return Index + Substring'Length - 1 <= Type_Str'Last
1816 and then Type_Str (Index .. Index + Substring'Length - 1) = Substring;
1817 end Looking_At;
1818
1819 ------------------------
1820 -- Can_Have_Parameter --
1821 ------------------------
1822
1823 function Can_Have_Parameter (S : String) return Boolean is
1824 begin
1825 if S'Length <= 1 then
1826 return False;
1827 end if;
1828
1829 case S (S'Last) is
1830 when '!' | ':' | '?' | '=' =>
1831 return True;
1832 when others =>
1833 return False;
1834 end case;
1835 end Can_Have_Parameter;
1836
1837 -----------------------
1838 -- Require_Parameter --
1839 -----------------------
1840
1841 function Require_Parameter (S : String) return Boolean is
1842 begin
1843 if S'Length <= 1 then
1844 return False;
1845 end if;
1846
1847 case S (S'Last) is
1848 when '!' | ':' | '=' =>
1849 return True;
1850 when others =>
1851 return False;
1852 end case;
1853 end Require_Parameter;
1854
1855 -------------------
1856 -- Actual_Switch --
1857 -------------------
1858
1859 function Actual_Switch (S : String) return String is
1860 begin
1861 if S'Length <= 1 then
1862 return S;
1863 end if;
1864
1865 case S (S'Last) is
1866 when '!' | ':' | '?' | '=' =>
1867 return S (S'First .. S'Last - 1);
1868 when others =>
1869 return S;
1870 end case;
1871 end Actual_Switch;
1872
1873 ----------------------------
1874 -- For_Each_Simple_Switch --
1875 ----------------------------
1876
1877 procedure For_Each_Simple_Switch
1878 (Config : Command_Line_Configuration;
1879 Section : String;
1880 Switch : String;
1881 Parameter : String := "";
1882 Unalias : Boolean := True)
1883 is
1884 function Group_Analysis
1885 (Prefix : String;
1886 Group : String) return Boolean;
1887 -- Perform the analysis of a group of switches
1888
1889 Found_In_Config : Boolean := False;
1890 function Is_In_Config
1891 (Config_Switch : String; Index : Integer) return Boolean;
1892 -- If Switch is the same as Config_Switch, run the callback and sets
1893 -- Found_In_Config to True.
1894
1895 function Starts_With
1896 (Config_Switch : String; Index : Integer) return Boolean;
1897 -- if Switch starts with Config_Switch, sets Found_In_Config to True.
1898 -- The return value is for the Foreach_Switch iterator.
1899
1900 --------------------
1901 -- Group_Analysis --
1902 --------------------
1903
1904 function Group_Analysis
1905 (Prefix : String;
1906 Group : String) return Boolean
1907 is
1908 Idx : Natural;
1909 Found : Boolean;
1910
1911 function Analyze_Simple_Switch
1912 (Switch : String; Index : Integer) return Boolean;
1913 -- "Switches" is one of the switch definitions passed to the
1914 -- configuration, not one of the switches found on the command line.
1915
1916 ---------------------------
1917 -- Analyze_Simple_Switch --
1918 ---------------------------
1919
1920 function Analyze_Simple_Switch
1921 (Switch : String; Index : Integer) return Boolean
1922 is
1923 pragma Unreferenced (Index);
1924
1925 Full : constant String := Prefix & Group (Idx .. Group'Last);
1926
1927 Sw : constant String := Actual_Switch (Switch);
1928 -- Switches definition minus argument definition
1929
1930 Last : Natural;
1931 Param : Natural;
1932
1933 begin
1934 -- Verify that sw starts with Prefix
1935
1936 if Looking_At (Sw, Sw'First, Prefix)
1937
1938 -- Verify that the group starts with sw
1939
1940 and then Looking_At (Full, Full'First, Sw)
1941 then
1942 Last := Idx + Sw'Length - Prefix'Length - 1;
1943 Param := Last + 1;
1944
1945 if Can_Have_Parameter (Switch) then
1946
1947 -- Include potential parameter to the recursive call. Only
1948 -- numbers are allowed.
1949
1950 while Last < Group'Last
1951 and then Group (Last + 1) in '0' .. '9'
1952 loop
1953 Last := Last + 1;
1954 end loop;
1955 end if;
1956
1957 if not Require_Parameter (Switch) or else Last >= Param then
1958 if Idx = Group'First
1959 and then Last = Group'Last
1960 and then Last < Param
1961 then
1962 -- The group only concerns a single switch. Do not
1963 -- perform recursive call.
1964
1965 -- Note that we still perform a recursive call if
1966 -- a parameter is detected in the switch, as this
1967 -- is a way to correctly identify such a parameter
1968 -- in aliases.
1969
1970 return False;
1971 end if;
1972
1973 Found := True;
1974
1975 -- Recursive call, using the detected parameter if any
1976
1977 if Last >= Param then
1978 For_Each_Simple_Switch
1979 (Config,
1980 Section,
1981 Prefix & Group (Idx .. Param - 1),
1982 Group (Param .. Last));
1983
1984 else
1985 For_Each_Simple_Switch
1986 (Config, Section, Prefix & Group (Idx .. Last), "");
1987 end if;
1988
1989 Idx := Last + 1;
1990 return False;
1991 end if;
1992 end if;
1993
1994 return True;
1995 end Analyze_Simple_Switch;
1996
1997 procedure Foreach is new Foreach_Switch (Analyze_Simple_Switch);
1998
1999 -- Start of processing for Group_Analysis
2000
2001 begin
2002 Idx := Group'First;
2003 while Idx <= Group'Last loop
2004 Found := False;
2005 Foreach (Config, Section);
2006
2007 if not Found then
2008 For_Each_Simple_Switch
2009 (Config, Section, Prefix & Group (Idx), "");
2010 Idx := Idx + 1;
2011 end if;
2012 end loop;
2013
2014 return True;
2015 end Group_Analysis;
2016
2017 ------------------
2018 -- Is_In_Config --
2019 ------------------
2020
2021 function Is_In_Config
2022 (Config_Switch : String; Index : Integer) return Boolean
2023 is
2024 Last : Natural;
2025 P : Switch_Parameter_Type;
2026
2027 begin
2028 Decompose_Switch (Config_Switch, P, Last);
2029
2030 if Config_Switch (Config_Switch'First .. Last) = Switch then
2031 case P is
2032 when Parameter_None =>
2033 if Parameter = "" then
2034 Callback (Switch, "", "", Index => Index);
2035 Found_In_Config := True;
2036 return False;
2037 end if;
2038
2039 when Parameter_With_Optional_Space =>
2040 Callback (Switch, " ", Parameter, Index => Index);
2041 Found_In_Config := True;
2042 return False;
2043
2044 when Parameter_With_Space_Or_Equal =>
2045 Callback (Switch, "=", Parameter, Index => Index);
2046 Found_In_Config := True;
2047 return False;
2048
2049 when Parameter_No_Space
2050 | Parameter_Optional
2051 =>
2052 Callback (Switch, "", Parameter, Index);
2053 Found_In_Config := True;
2054 return False;
2055 end case;
2056 end if;
2057
2058 return True;
2059 end Is_In_Config;
2060
2061 -----------------
2062 -- Starts_With --
2063 -----------------
2064
2065 function Starts_With
2066 (Config_Switch : String; Index : Integer) return Boolean
2067 is
2068 Last : Natural;
2069 Param : Natural;
2070 P : Switch_Parameter_Type;
2071
2072 begin
2073 -- This function is called when we believe the parameter was
2074 -- specified as part of the switch, instead of separately. Thus we
2075 -- look in the config to find all possible switches.
2076
2077 Decompose_Switch (Config_Switch, P, Last);
2078
2079 if Looking_At
2080 (Switch, Switch'First,
2081 Config_Switch (Config_Switch'First .. Last))
2082 then
2083 -- Set first char of Param, and last char of Switch
2084
2085 Param := Switch'First + Last;
2086 Last := Switch'First + Last - Config_Switch'First;
2087
2088 case P is
2089
2090 -- None is already handled in Is_In_Config
2091
2092 when Parameter_None =>
2093 null;
2094
2095 when Parameter_With_Space_Or_Equal =>
2096 if Param <= Switch'Last
2097 and then
2098 (Switch (Param) = ' ' or else Switch (Param) = '=')
2099 then
2100 Callback (Switch (Switch'First .. Last),
2101 "=", Switch (Param + 1 .. Switch'Last), Index);
2102 Found_In_Config := True;
2103 return False;
2104 end if;
2105
2106 when Parameter_With_Optional_Space =>
2107 if Param <= Switch'Last and then Switch (Param) = ' ' then
2108 Param := Param + 1;
2109 end if;
2110
2111 Callback (Switch (Switch'First .. Last),
2112 " ", Switch (Param .. Switch'Last), Index);
2113 Found_In_Config := True;
2114 return False;
2115
2116 when Parameter_No_Space
2117 | Parameter_Optional
2118 =>
2119 Callback (Switch (Switch'First .. Last),
2120 "", Switch (Param .. Switch'Last), Index);
2121 Found_In_Config := True;
2122 return False;
2123 end case;
2124 end if;
2125 return True;
2126 end Starts_With;
2127
2128 procedure Foreach_In_Config is new Foreach_Switch (Is_In_Config);
2129 procedure Foreach_Starts_With is new Foreach_Switch (Starts_With);
2130
2131 -- Start of processing for For_Each_Simple_Switch
2132
2133 begin
2134 -- First determine if the switch corresponds to one belonging to the
2135 -- configuration. If so, run callback and exit.
2136
2137 -- ??? Is this necessary. On simple tests, we seem to have the same
2138 -- results with or without this call.
2139
2140 Foreach_In_Config (Config, Section);
2141
2142 if Found_In_Config then
2143 return;
2144 end if;
2145
2146 -- If adding a switch that can in fact be expanded through aliases,
2147 -- add separately each of its expansions.
2148
2149 -- This takes care of expansions like "-T" -> "-gnatwrs", where the
2150 -- alias and its expansion do not have the same prefix. Given the order
2151 -- in which we do things here, the expansion of the alias will itself
2152 -- be checked for a common prefix and split into simple switches.
2153
2154 if Unalias
2155 and then Config /= null
2156 and then Config.Aliases /= null
2157 then
2158 for A in Config.Aliases'Range loop
2159 if Config.Aliases (A).Section.all = Section
2160 and then Config.Aliases (A).Alias.all = Switch
2161 and then Parameter = ""
2162 then
2163 For_Each_Simple_Switch
2164 (Config, Section, Config.Aliases (A).Expansion.all, "");
2165 return;
2166 end if;
2167 end loop;
2168 end if;
2169
2170 -- If adding a switch grouping several switches, add each of the simple
2171 -- switches instead.
2172
2173 if Config /= null and then Config.Prefixes /= null then
2174 for P in Config.Prefixes'Range loop
2175 if Switch'Length > Config.Prefixes (P)'Length + 1
2176 and then
2177 Looking_At (Switch, Switch'First, Config.Prefixes (P).all)
2178 then
2179 -- Alias expansion will be done recursively
2180
2181 if Config.Switches = null then
2182 for S in Switch'First + Config.Prefixes (P)'Length
2183 .. Switch'Last
2184 loop
2185 For_Each_Simple_Switch
2186 (Config, Section,
2187 Config.Prefixes (P).all & Switch (S), "");
2188 end loop;
2189
2190 return;
2191
2192 elsif Group_Analysis
2193 (Config.Prefixes (P).all,
2194 Switch
2195 (Switch'First + Config.Prefixes (P)'Length .. Switch'Last))
2196 then
2197 -- Recursive calls already done on each switch of the group:
2198 -- Return without executing Callback.
2199
2200 return;
2201 end if;
2202 end if;
2203 end loop;
2204 end if;
2205
2206 -- Test if added switch is a known switch with parameter attached
2207 -- instead of being specified separately
2208
2209 if Parameter = ""
2210 and then Config /= null
2211 and then Config.Switches /= null
2212 then
2213 Found_In_Config := False;
2214 Foreach_Starts_With (Config, Section);
2215
2216 if Found_In_Config then
2217 return;
2218 end if;
2219 end if;
2220
2221 -- The switch is invalid in the config, but we still want to report it.
2222 -- The config could, for instance, include "*" to specify it accepts
2223 -- all switches.
2224
2225 Callback (Switch, " ", Parameter, Index => -1);
2226 end For_Each_Simple_Switch;
2227
2228 ----------------
2229 -- Add_Switch --
2230 ----------------
2231
2232 procedure Add_Switch
2233 (Cmd : in out Command_Line;
2234 Switch : String;
2235 Parameter : String := "";
2236 Separator : Character := ASCII.NUL;
2237 Section : String := "";
2238 Add_Before : Boolean := False)
2239 is
2240 Success : Boolean;
2241 pragma Unreferenced (Success);
2242 begin
2243 Add_Switch (Cmd, Switch, Parameter, Separator,
2244 Section, Add_Before, Success);
2245 end Add_Switch;
2246
2247 ----------------
2248 -- Add_Switch --
2249 ----------------
2250
2251 procedure Add_Switch
2252 (Cmd : in out Command_Line;
2253 Switch : String;
2254 Parameter : String := "";
2255 Separator : Character := ASCII.NUL;
2256 Section : String := "";
2257 Add_Before : Boolean := False;
2258 Success : out Boolean)
2259 is
2260 procedure Add_Simple_Switch
2261 (Simple : String;
2262 Sepa : String;
2263 Param : String;
2264 Index : Integer);
2265 -- Add a new switch that has had all its aliases expanded, and switches
2266 -- ungrouped. We know there are no more aliases in Switches.
2267
2268 -----------------------
2269 -- Add_Simple_Switch --
2270 -----------------------
2271
2272 procedure Add_Simple_Switch
2273 (Simple : String;
2274 Sepa : String;
2275 Param : String;
2276 Index : Integer)
2277 is
2278 Sep : Character;
2279
2280 begin
2281 if Index = -1
2282 and then Cmd.Config /= null
2283 and then not Cmd.Config.Star_Switch
2284 then
2285 raise Invalid_Switch
2286 with "Invalid switch " & Simple;
2287 end if;
2288
2289 if Separator /= ASCII.NUL then
2290 Sep := Separator;
2291
2292 elsif Sepa = "" then
2293 Sep := ASCII.NUL;
2294 else
2295 Sep := Sepa (Sepa'First);
2296 end if;
2297
2298 if Cmd.Expanded = null then
2299 Cmd.Expanded := new Argument_List'(1 .. 1 => new String'(Simple));
2300
2301 if Param /= "" then
2302 Cmd.Params :=
2303 new Argument_List'(1 .. 1 => new String'(Sep & Param));
2304 else
2305 Cmd.Params := new Argument_List'(1 .. 1 => null);
2306 end if;
2307
2308 if Section = "" then
2309 Cmd.Sections := new Argument_List'(1 .. 1 => null);
2310 else
2311 Cmd.Sections :=
2312 new Argument_List'(1 .. 1 => new String'(Section));
2313 end if;
2314
2315 else
2316 -- Do we already have this switch?
2317
2318 for C in Cmd.Expanded'Range loop
2319 if Cmd.Expanded (C).all = Simple
2320 and then
2321 ((Cmd.Params (C) = null and then Param = "")
2322 or else
2323 (Cmd.Params (C) /= null
2324 and then Cmd.Params (C).all = Sep & Param))
2325 and then
2326 ((Cmd.Sections (C) = null and then Section = "")
2327 or else
2328 (Cmd.Sections (C) /= null
2329 and then Cmd.Sections (C).all = Section))
2330 then
2331 return;
2332 end if;
2333 end loop;
2334
2335 -- Inserting at least one switch
2336
2337 Success := True;
2338 Add (Cmd.Expanded, new String'(Simple), Add_Before);
2339
2340 if Param /= "" then
2341 Add
2342 (Cmd.Params,
2343 new String'(Sep & Param),
2344 Add_Before);
2345 else
2346 Add
2347 (Cmd.Params,
2348 null,
2349 Add_Before);
2350 end if;
2351
2352 if Section = "" then
2353 Add
2354 (Cmd.Sections,
2355 null,
2356 Add_Before);
2357 else
2358 Add
2359 (Cmd.Sections,
2360 new String'(Section),
2361 Add_Before);
2362 end if;
2363 end if;
2364 end Add_Simple_Switch;
2365
2366 procedure Add_Simple_Switches is
2367 new For_Each_Simple_Switch (Add_Simple_Switch);
2368
2369 -- Local Variables
2370
2371 Section_Valid : Boolean := False;
2372
2373 -- Start of processing for Add_Switch
2374
2375 begin
2376 if Section /= "" and then Cmd.Config /= null then
2377 for S in Cmd.Config.Sections'Range loop
2378 if Section = Cmd.Config.Sections (S).all then
2379 Section_Valid := True;
2380 exit;
2381 end if;
2382 end loop;
2383
2384 if not Section_Valid then
2385 raise Invalid_Section;
2386 end if;
2387 end if;
2388
2389 Success := False;
2390 Add_Simple_Switches (Cmd.Config, Section, Switch, Parameter);
2391 Free (Cmd.Coalesce);
2392 end Add_Switch;
2393
2394 ------------
2395 -- Remove --
2396 ------------
2397
2398 procedure Remove (Line : in out Argument_List_Access; Index : Integer) is
2399 Tmp : Argument_List_Access := Line;
2400
2401 begin
2402 Line := new Argument_List (Tmp'First .. Tmp'Last - 1);
2403
2404 if Index /= Tmp'First then
2405 Line (Tmp'First .. Index - 1) := Tmp (Tmp'First .. Index - 1);
2406 end if;
2407
2408 Free (Tmp (Index));
2409
2410 if Index /= Tmp'Last then
2411 Line (Index .. Tmp'Last - 1) := Tmp (Index + 1 .. Tmp'Last);
2412 end if;
2413
2414 Unchecked_Free (Tmp);
2415 end Remove;
2416
2417 ---------
2418 -- Add --
2419 ---------
2420
2421 procedure Add
2422 (Line : in out Argument_List_Access;
2423 Str : String_Access;
2424 Before : Boolean := False)
2425 is
2426 Tmp : Argument_List_Access := Line;
2427
2428 begin
2429 if Tmp /= null then
2430 Line := new Argument_List (Tmp'First .. Tmp'Last + 1);
2431
2432 if Before then
2433 Line (Tmp'First) := Str;
2434 Line (Tmp'First + 1 .. Tmp'Last + 1) := Tmp.all;
2435 else
2436 Line (Tmp'Range) := Tmp.all;
2437 Line (Tmp'Last + 1) := Str;
2438 end if;
2439
2440 Unchecked_Free (Tmp);
2441
2442 else
2443 Line := new Argument_List'(1 .. 1 => Str);
2444 end if;
2445 end Add;
2446
2447 -------------------
2448 -- Remove_Switch --
2449 -------------------
2450
2451 procedure Remove_Switch
2452 (Cmd : in out Command_Line;
2453 Switch : String;
2454 Remove_All : Boolean := False;
2455 Has_Parameter : Boolean := False;
2456 Section : String := "")
2457 is
2458 Success : Boolean;
2459 pragma Unreferenced (Success);
2460 begin
2461 Remove_Switch (Cmd, Switch, Remove_All, Has_Parameter, Section, Success);
2462 end Remove_Switch;
2463
2464 -------------------
2465 -- Remove_Switch --
2466 -------------------
2467
2468 procedure Remove_Switch
2469 (Cmd : in out Command_Line;
2470 Switch : String;
2471 Remove_All : Boolean := False;
2472 Has_Parameter : Boolean := False;
2473 Section : String := "";
2474 Success : out Boolean)
2475 is
2476 procedure Remove_Simple_Switch
2477 (Simple, Separator, Param : String; Index : Integer);
2478 -- Removes a simple switch, with no aliasing or grouping
2479
2480 --------------------------
2481 -- Remove_Simple_Switch --
2482 --------------------------
2483
2484 procedure Remove_Simple_Switch
2485 (Simple, Separator, Param : String; Index : Integer)
2486 is
2487 C : Integer;
2488 pragma Unreferenced (Param, Separator, Index);
2489
2490 begin
2491 if Cmd.Expanded /= null then
2492 C := Cmd.Expanded'First;
2493 while C <= Cmd.Expanded'Last loop
2494 if Cmd.Expanded (C).all = Simple
2495 and then
2496 (Remove_All
2497 or else (Cmd.Sections (C) = null
2498 and then Section = "")
2499 or else (Cmd.Sections (C) /= null
2500 and then Section = Cmd.Sections (C).all))
2501 and then (not Has_Parameter or else Cmd.Params (C) /= null)
2502 then
2503 Remove (Cmd.Expanded, C);
2504 Remove (Cmd.Params, C);
2505 Remove (Cmd.Sections, C);
2506 Success := True;
2507
2508 if not Remove_All then
2509 return;
2510 end if;
2511
2512 else
2513 C := C + 1;
2514 end if;
2515 end loop;
2516 end if;
2517 end Remove_Simple_Switch;
2518
2519 procedure Remove_Simple_Switches is
2520 new For_Each_Simple_Switch (Remove_Simple_Switch);
2521
2522 -- Start of processing for Remove_Switch
2523
2524 begin
2525 Success := False;
2526 Remove_Simple_Switches
2527 (Cmd.Config, Section, Switch, "", Unalias => not Has_Parameter);
2528 Free (Cmd.Coalesce);
2529 end Remove_Switch;
2530
2531 -------------------
2532 -- Remove_Switch --
2533 -------------------
2534
2535 procedure Remove_Switch
2536 (Cmd : in out Command_Line;
2537 Switch : String;
2538 Parameter : String;
2539 Section : String := "")
2540 is
2541 procedure Remove_Simple_Switch
2542 (Simple, Separator, Param : String; Index : Integer);
2543 -- Removes a simple switch, with no aliasing or grouping
2544
2545 --------------------------
2546 -- Remove_Simple_Switch --
2547 --------------------------
2548
2549 procedure Remove_Simple_Switch
2550 (Simple, Separator, Param : String; Index : Integer)
2551 is
2552 pragma Unreferenced (Separator, Index);
2553 C : Integer;
2554
2555 begin
2556 if Cmd.Expanded /= null then
2557 C := Cmd.Expanded'First;
2558 while C <= Cmd.Expanded'Last loop
2559 if Cmd.Expanded (C).all = Simple
2560 and then
2561 ((Cmd.Sections (C) = null
2562 and then Section = "")
2563 or else
2564 (Cmd.Sections (C) /= null
2565 and then Section = Cmd.Sections (C).all))
2566 and then
2567 ((Cmd.Params (C) = null and then Param = "")
2568 or else
2569 (Cmd.Params (C) /= null
2570
2571 -- Ignore the separator stored in Parameter
2572
2573 and then
2574 Cmd.Params (C) (Cmd.Params (C)'First + 1
2575 .. Cmd.Params (C)'Last) = Param))
2576 then
2577 Remove (Cmd.Expanded, C);
2578 Remove (Cmd.Params, C);
2579 Remove (Cmd.Sections, C);
2580
2581 -- The switch is necessarily unique by construction of
2582 -- Add_Switch.
2583
2584 return;
2585
2586 else
2587 C := C + 1;
2588 end if;
2589 end loop;
2590 end if;
2591 end Remove_Simple_Switch;
2592
2593 procedure Remove_Simple_Switches is
2594 new For_Each_Simple_Switch (Remove_Simple_Switch);
2595
2596 -- Start of processing for Remove_Switch
2597
2598 begin
2599 Remove_Simple_Switches (Cmd.Config, Section, Switch, Parameter);
2600 Free (Cmd.Coalesce);
2601 end Remove_Switch;
2602
2603 --------------------
2604 -- Group_Switches --
2605 --------------------
2606
2607 procedure Group_Switches
2608 (Cmd : Command_Line;
2609 Result : Argument_List_Access;
2610 Sections : Argument_List_Access;
2611 Params : Argument_List_Access)
2612 is
2613 function Compatible_Parameter (Param : String_Access) return Boolean;
2614 -- True when the parameter can be part of a group
2615
2616 --------------------------
2617 -- Compatible_Parameter --
2618 --------------------------
2619
2620 function Compatible_Parameter (Param : String_Access) return Boolean is
2621 begin
2622 -- No parameter OK
2623
2624 if Param = null then
2625 return True;
2626
2627 -- We need parameters without separators
2628
2629 elsif Param (Param'First) /= ASCII.NUL then
2630 return False;
2631
2632 -- Parameters must be all digits
2633
2634 else
2635 for J in Param'First + 1 .. Param'Last loop
2636 if Param (J) not in '0' .. '9' then
2637 return False;
2638 end if;
2639 end loop;
2640
2641 return True;
2642 end if;
2643 end Compatible_Parameter;
2644
2645 -- Local declarations
2646
2647 Group : Ada.Strings.Unbounded.Unbounded_String;
2648 First : Natural;
2649 use type Ada.Strings.Unbounded.Unbounded_String;
2650
2651 -- Start of processing for Group_Switches
2652
2653 begin
2654 if Cmd.Config = null or else Cmd.Config.Prefixes = null then
2655 return;
2656 end if;
2657
2658 for P in Cmd.Config.Prefixes'Range loop
2659 Group := Ada.Strings.Unbounded.Null_Unbounded_String;
2660 First := 0;
2661
2662 for C in Result'Range loop
2663 if Result (C) /= null
2664 and then Compatible_Parameter (Params (C))
2665 and then Looking_At
2666 (Result (C).all,
2667 Result (C)'First,
2668 Cmd.Config.Prefixes (P).all)
2669 then
2670 -- If we are still in the same section, group the switches
2671
2672 if First = 0
2673 or else
2674 (Sections (C) = null
2675 and then Sections (First) = null)
2676 or else
2677 (Sections (C) /= null
2678 and then Sections (First) /= null
2679 and then Sections (C).all = Sections (First).all)
2680 then
2681 Group :=
2682 Group &
2683 Result (C)
2684 (Result (C)'First + Cmd.Config.Prefixes (P)'Length ..
2685 Result (C)'Last);
2686
2687 if Params (C) /= null then
2688 Group :=
2689 Group &
2690 Params (C) (Params (C)'First + 1 .. Params (C)'Last);
2691 Free (Params (C));
2692 end if;
2693
2694 if First = 0 then
2695 First := C;
2696 end if;
2697
2698 Free (Result (C));
2699
2700 -- We changed section: we put the grouped switches to the first
2701 -- place, on continue with the new section.
2702
2703 else
2704 Result (First) :=
2705 new String'
2706 (Cmd.Config.Prefixes (P).all &
2707 Ada.Strings.Unbounded.To_String (Group));
2708 Group :=
2709 Ada.Strings.Unbounded.To_Unbounded_String
2710 (Result (C)
2711 (Result (C)'First + Cmd.Config.Prefixes (P)'Length ..
2712 Result (C)'Last));
2713 First := C;
2714 end if;
2715 end if;
2716 end loop;
2717
2718 if First > 0 then
2719 Result (First) :=
2720 new String'
2721 (Cmd.Config.Prefixes (P).all &
2722 Ada.Strings.Unbounded.To_String (Group));
2723 end if;
2724 end loop;
2725 end Group_Switches;
2726
2727 --------------------
2728 -- Alias_Switches --
2729 --------------------
2730
2731 procedure Alias_Switches
2732 (Cmd : Command_Line;
2733 Result : Argument_List_Access;
2734 Params : Argument_List_Access)
2735 is
2736 Found : Boolean;
2737 First : Natural;
2738
2739 procedure Check_Cb (Switch, Separator, Param : String; Index : Integer);
2740 -- Checks whether the command line contains [Switch]. Sets the global
2741 -- variable [Found] appropriately. This is called for each simple switch
2742 -- that make up an alias, to know whether the alias should be applied.
2743
2744 procedure Remove_Cb (Switch, Separator, Param : String; Index : Integer);
2745 -- Remove the simple switch [Switch] from the command line, since it is
2746 -- part of a simpler alias
2747
2748 --------------
2749 -- Check_Cb --
2750 --------------
2751
2752 procedure Check_Cb
2753 (Switch, Separator, Param : String; Index : Integer)
2754 is
2755 pragma Unreferenced (Separator, Index);
2756
2757 begin
2758 if Found then
2759 for E in Result'Range loop
2760 if Result (E) /= null
2761 and then
2762 (Params (E) = null
2763 or else Params (E) (Params (E)'First + 1 ..
2764 Params (E)'Last) = Param)
2765 and then Result (E).all = Switch
2766 then
2767 return;
2768 end if;
2769 end loop;
2770
2771 Found := False;
2772 end if;
2773 end Check_Cb;
2774
2775 ---------------
2776 -- Remove_Cb --
2777 ---------------
2778
2779 procedure Remove_Cb (Switch, Separator, Param : String; Index : Integer)
2780 is
2781 pragma Unreferenced (Separator, Index);
2782
2783 begin
2784 for E in Result'Range loop
2785 if Result (E) /= null
2786 and then
2787 (Params (E) = null
2788 or else Params (E) (Params (E)'First + 1
2789 .. Params (E)'Last) = Param)
2790 and then Result (E).all = Switch
2791 then
2792 if First > E then
2793 First := E;
2794 end if;
2795
2796 Free (Result (E));
2797 Free (Params (E));
2798 return;
2799 end if;
2800 end loop;
2801 end Remove_Cb;
2802
2803 procedure Check_All is new For_Each_Simple_Switch (Check_Cb);
2804 procedure Remove_All is new For_Each_Simple_Switch (Remove_Cb);
2805
2806 -- Start of processing for Alias_Switches
2807
2808 begin
2809 if Cmd.Config = null or else Cmd.Config.Aliases = null then
2810 return;
2811 end if;
2812
2813 for A in Cmd.Config.Aliases'Range loop
2814
2815 -- Compute the various simple switches that make up the alias. We
2816 -- split the expansion into as many simple switches as possible, and
2817 -- then check whether the expanded command line has all of them.
2818
2819 Found := True;
2820 Check_All (Cmd.Config,
2821 Switch => Cmd.Config.Aliases (A).Expansion.all,
2822 Section => Cmd.Config.Aliases (A).Section.all);
2823
2824 if Found then
2825 First := Integer'Last;
2826 Remove_All (Cmd.Config,
2827 Switch => Cmd.Config.Aliases (A).Expansion.all,
2828 Section => Cmd.Config.Aliases (A).Section.all);
2829 Result (First) := new String'(Cmd.Config.Aliases (A).Alias.all);
2830 end if;
2831 end loop;
2832 end Alias_Switches;
2833
2834 -------------------
2835 -- Sort_Sections --
2836 -------------------
2837
2838 procedure Sort_Sections
2839 (Line : not null GNAT.OS_Lib.Argument_List_Access;
2840 Sections : GNAT.OS_Lib.Argument_List_Access;
2841 Params : GNAT.OS_Lib.Argument_List_Access)
2842 is
2843 Sections_List : Argument_List_Access :=
2844 new Argument_List'(1 .. 1 => null);
2845 Found : Boolean;
2846 Old_Line : constant Argument_List := Line.all;
2847 Old_Sections : constant Argument_List := Sections.all;
2848 Old_Params : constant Argument_List := Params.all;
2849 Index : Natural;
2850
2851 begin
2852 -- First construct a list of all sections
2853
2854 for E in Line'Range loop
2855 if Sections (E) /= null then
2856 Found := False;
2857 for S in Sections_List'Range loop
2858 if (Sections_List (S) = null and then Sections (E) = null)
2859 or else
2860 (Sections_List (S) /= null
2861 and then Sections (E) /= null
2862 and then Sections_List (S).all = Sections (E).all)
2863 then
2864 Found := True;
2865 exit;
2866 end if;
2867 end loop;
2868
2869 if not Found then
2870 Add (Sections_List, Sections (E));
2871 end if;
2872 end if;
2873 end loop;
2874
2875 Index := Line'First;
2876
2877 for S in Sections_List'Range loop
2878 for E in Old_Line'Range loop
2879 if (Sections_List (S) = null and then Old_Sections (E) = null)
2880 or else
2881 (Sections_List (S) /= null
2882 and then Old_Sections (E) /= null
2883 and then Sections_List (S).all = Old_Sections (E).all)
2884 then
2885 Line (Index) := Old_Line (E);
2886 Sections (Index) := Old_Sections (E);
2887 Params (Index) := Old_Params (E);
2888 Index := Index + 1;
2889 end if;
2890 end loop;
2891 end loop;
2892
2893 Unchecked_Free (Sections_List);
2894 end Sort_Sections;
2895
2896 -----------
2897 -- Start --
2898 -----------
2899
2900 procedure Start
2901 (Cmd : in out Command_Line;
2902 Iter : in out Command_Line_Iterator;
2903 Expanded : Boolean := False)
2904 is
2905 begin
2906 if Cmd.Expanded = null then
2907 Iter.List := null;
2908 return;
2909 end if;
2910
2911 -- Reorder the expanded line so that sections are grouped
2912
2913 Sort_Sections (Cmd.Expanded, Cmd.Sections, Cmd.Params);
2914
2915 -- Coalesce the switches as much as possible
2916
2917 if not Expanded
2918 and then Cmd.Coalesce = null
2919 then
2920 Cmd.Coalesce := new Argument_List (Cmd.Expanded'Range);
2921 for E in Cmd.Expanded'Range loop
2922 Cmd.Coalesce (E) := new String'(Cmd.Expanded (E).all);
2923 end loop;
2924
2925 Free (Cmd.Coalesce_Sections);
2926 Cmd.Coalesce_Sections := new Argument_List (Cmd.Sections'Range);
2927 for E in Cmd.Sections'Range loop
2928 Cmd.Coalesce_Sections (E) :=
2929 (if Cmd.Sections (E) = null then null
2930 else new String'(Cmd.Sections (E).all));
2931 end loop;
2932
2933 Free (Cmd.Coalesce_Params);
2934 Cmd.Coalesce_Params := new Argument_List (Cmd.Params'Range);
2935 for E in Cmd.Params'Range loop
2936 Cmd.Coalesce_Params (E) :=
2937 (if Cmd.Params (E) = null then null
2938 else new String'(Cmd.Params (E).all));
2939 end loop;
2940
2941 -- Not a clone, since we will not modify the parameters anyway
2942
2943 Alias_Switches (Cmd, Cmd.Coalesce, Cmd.Coalesce_Params);
2944 Group_Switches
2945 (Cmd, Cmd.Coalesce, Cmd.Coalesce_Sections, Cmd.Coalesce_Params);
2946 end if;
2947
2948 if Expanded then
2949 Iter.List := Cmd.Expanded;
2950 Iter.Params := Cmd.Params;
2951 Iter.Sections := Cmd.Sections;
2952 else
2953 Iter.List := Cmd.Coalesce;
2954 Iter.Params := Cmd.Coalesce_Params;
2955 Iter.Sections := Cmd.Coalesce_Sections;
2956 end if;
2957
2958 if Iter.List = null then
2959 Iter.Current := Integer'Last;
2960 else
2961 Iter.Current := Iter.List'First - 1;
2962 Next (Iter);
2963 end if;
2964 end Start;
2965
2966 --------------------
2967 -- Current_Switch --
2968 --------------------
2969
2970 function Current_Switch (Iter : Command_Line_Iterator) return String is
2971 begin
2972 return Iter.List (Iter.Current).all;
2973 end Current_Switch;
2974
2975 --------------------
2976 -- Is_New_Section --
2977 --------------------
2978
2979 function Is_New_Section (Iter : Command_Line_Iterator) return Boolean is
2980 Section : constant String := Current_Section (Iter);
2981
2982 begin
2983 if Iter.Sections = null then
2984 return False;
2985
2986 elsif Iter.Current = Iter.Sections'First
2987 or else Iter.Sections (Iter.Current - 1) = null
2988 then
2989 return Section /= "";
2990
2991 else
2992 return Section /= Iter.Sections (Iter.Current - 1).all;
2993 end if;
2994 end Is_New_Section;
2995
2996 ---------------------
2997 -- Current_Section --
2998 ---------------------
2999
3000 function Current_Section (Iter : Command_Line_Iterator) return String is
3001 begin
3002 if Iter.Sections = null
3003 or else Iter.Current > Iter.Sections'Last
3004 or else Iter.Sections (Iter.Current) = null
3005 then
3006 return "";
3007 end if;
3008
3009 return Iter.Sections (Iter.Current).all;
3010 end Current_Section;
3011
3012 -----------------------
3013 -- Current_Separator --
3014 -----------------------
3015
3016 function Current_Separator (Iter : Command_Line_Iterator) return String is
3017 begin
3018 if Iter.Params = null
3019 or else Iter.Current > Iter.Params'Last
3020 or else Iter.Params (Iter.Current) = null
3021 then
3022 return "";
3023
3024 else
3025 declare
3026 Sep : constant Character :=
3027 Iter.Params (Iter.Current) (Iter.Params (Iter.Current)'First);
3028 begin
3029 if Sep = ASCII.NUL then
3030 return "";
3031 else
3032 return "" & Sep;
3033 end if;
3034 end;
3035 end if;
3036 end Current_Separator;
3037
3038 -----------------------
3039 -- Current_Parameter --
3040 -----------------------
3041
3042 function Current_Parameter (Iter : Command_Line_Iterator) return String is
3043 begin
3044 if Iter.Params = null
3045 or else Iter.Current > Iter.Params'Last
3046 or else Iter.Params (Iter.Current) = null
3047 then
3048 return "";
3049
3050 else
3051 -- Return result, skipping separator
3052
3053 declare
3054 P : constant String := Iter.Params (Iter.Current).all;
3055 begin
3056 return P (P'First + 1 .. P'Last);
3057 end;
3058 end if;
3059 end Current_Parameter;
3060
3061 --------------
3062 -- Has_More --
3063 --------------
3064
3065 function Has_More (Iter : Command_Line_Iterator) return Boolean is
3066 begin
3067 return Iter.List /= null and then Iter.Current <= Iter.List'Last;
3068 end Has_More;
3069
3070 ----------
3071 -- Next --
3072 ----------
3073
3074 procedure Next (Iter : in out Command_Line_Iterator) is
3075 begin
3076 Iter.Current := Iter.Current + 1;
3077 while Iter.Current <= Iter.List'Last
3078 and then Iter.List (Iter.Current) = null
3079 loop
3080 Iter.Current := Iter.Current + 1;
3081 end loop;
3082 end Next;
3083
3084 ----------
3085 -- Free --
3086 ----------
3087
3088 procedure Free (Config : in out Command_Line_Configuration) is
3089 procedure Unchecked_Free is new
3090 Ada.Unchecked_Deallocation
3091 (Switch_Definitions, Switch_Definitions_List);
3092
3093 procedure Unchecked_Free is new
3094 Ada.Unchecked_Deallocation
3095 (Alias_Definitions, Alias_Definitions_List);
3096
3097 begin
3098 if Config /= null then
3099 Free (Config.Prefixes);
3100 Free (Config.Sections);
3101 Free (Config.Usage);
3102 Free (Config.Help);
3103 Free (Config.Help_Msg);
3104
3105 if Config.Aliases /= null then
3106 for A in Config.Aliases'Range loop
3107 Free (Config.Aliases (A).Alias);
3108 Free (Config.Aliases (A).Expansion);
3109 Free (Config.Aliases (A).Section);
3110 end loop;
3111
3112 Unchecked_Free (Config.Aliases);
3113 end if;
3114
3115 if Config.Switches /= null then
3116 for S in Config.Switches'Range loop
3117 Free (Config.Switches (S).Switch);
3118 Free (Config.Switches (S).Long_Switch);
3119 Free (Config.Switches (S).Help);
3120 Free (Config.Switches (S).Section);
3121 Free (Config.Switches (S).Argument);
3122 end loop;
3123
3124 Unchecked_Free (Config.Switches);
3125 end if;
3126
3127 Unchecked_Free (Config);
3128 end if;
3129 end Free;
3130
3131 ----------
3132 -- Free --
3133 ----------
3134
3135 procedure Free (Cmd : in out Command_Line) is
3136 begin
3137 Free (Cmd.Expanded);
3138 Free (Cmd.Coalesce);
3139 Free (Cmd.Coalesce_Sections);
3140 Free (Cmd.Coalesce_Params);
3141 Free (Cmd.Params);
3142 Free (Cmd.Sections);
3143 end Free;
3144
3145 ---------------
3146 -- Set_Usage --
3147 ---------------
3148
3149 procedure Set_Usage
3150 (Config : in out Command_Line_Configuration;
3151 Usage : String := "[switches] [arguments]";
3152 Help : String := "";
3153 Help_Msg : String := "")
3154 is
3155 begin
3156 if Config = null then
3157 Config := new Command_Line_Configuration_Record;
3158 end if;
3159
3160 Free (Config.Usage);
3161 Free (Config.Help);
3162 Free (Config.Help_Msg);
3163
3164 Config.Usage := new String'(Usage);
3165 Config.Help := new String'(Help);
3166 Config.Help_Msg := new String'(Help_Msg);
3167 end Set_Usage;
3168
3169 ------------------
3170 -- Display_Help --
3171 ------------------
3172
3173 procedure Display_Help (Config : Command_Line_Configuration) is
3174 function Switch_Name
3175 (Def : Switch_Definition;
3176 Section : String) return String;
3177 -- Return the "-short, --long=ARG" string for Def.
3178 -- Returns "" if the switch is not in the section.
3179
3180 function Param_Name
3181 (P : Switch_Parameter_Type;
3182 Name : String := "ARG") return String;
3183 -- Return the display for a switch parameter
3184
3185 procedure Display_Section_Help (Section : String);
3186 -- Display the help for a specific section ("" is the default section)
3187
3188 --------------------------
3189 -- Display_Section_Help --
3190 --------------------------
3191
3192 procedure Display_Section_Help (Section : String) is
3193 Max_Len : Natural := 0;
3194
3195 begin
3196 -- ??? Special display for "*"
3197
3198 New_Line;
3199
3200 if Section /= "" then
3201 Put_Line ("Switches after " & Section);
3202 end if;
3203
3204 -- Compute size of the switches column
3205
3206 for S in Config.Switches'Range loop
3207 Max_Len := Natural'Max
3208 (Max_Len, Switch_Name (Config.Switches (S), Section)'Length);
3209 end loop;
3210
3211 if Config.Aliases /= null then
3212 for A in Config.Aliases'Range loop
3213 if Config.Aliases (A).Section.all = Section then
3214 Max_Len := Natural'Max
3215 (Max_Len, Config.Aliases (A).Alias'Length);
3216 end if;
3217 end loop;
3218 end if;
3219
3220 -- Display the switches
3221
3222 for S in Config.Switches'Range loop
3223 declare
3224 N : constant String :=
3225 Switch_Name (Config.Switches (S), Section);
3226
3227 begin
3228 if N /= "" then
3229 Put (" ");
3230 Put (N);
3231 Put ((1 .. Max_Len - N'Length + 1 => ' '));
3232
3233 if Config.Switches (S).Help /= null then
3234 Put (Config.Switches (S).Help.all);
3235 end if;
3236
3237 New_Line;
3238 end if;
3239 end;
3240 end loop;
3241
3242 -- Display the aliases
3243
3244 if Config.Aliases /= null then
3245 for A in Config.Aliases'Range loop
3246 if Config.Aliases (A).Section.all = Section then
3247 Put (" ");
3248 Put (Config.Aliases (A).Alias.all);
3249 Put ((1 .. Max_Len - Config.Aliases (A).Alias'Length + 1
3250 => ' '));
3251 Put ("Equivalent to " & Config.Aliases (A).Expansion.all);
3252 New_Line;
3253 end if;
3254 end loop;
3255 end if;
3256 end Display_Section_Help;
3257
3258 ----------------
3259 -- Param_Name --
3260 ----------------
3261
3262 function Param_Name
3263 (P : Switch_Parameter_Type;
3264 Name : String := "ARG") return String
3265 is
3266 begin
3267 case P is
3268 when Parameter_None =>
3269 return "";
3270
3271 when Parameter_With_Optional_Space =>
3272 return " " & To_Upper (Name);
3273
3274 when Parameter_With_Space_Or_Equal =>
3275 return "=" & To_Upper (Name);
3276
3277 when Parameter_No_Space =>
3278 return To_Upper (Name);
3279
3280 when Parameter_Optional =>
3281 return '[' & To_Upper (Name) & ']';
3282 end case;
3283 end Param_Name;
3284
3285 -----------------
3286 -- Switch_Name --
3287 -----------------
3288
3289 function Switch_Name
3290 (Def : Switch_Definition;
3291 Section : String) return String
3292 is
3293 use Ada.Strings.Unbounded;
3294 Result : Unbounded_String;
3295 P1, P2 : Switch_Parameter_Type;
3296 Last1, Last2 : Integer := 0;
3297
3298 begin
3299 if (Section = "" and then Def.Section = null)
3300 or else (Def.Section /= null and then Def.Section.all = Section)
3301 then
3302 if Def.Switch /= null and then Def.Switch.all = "*" then
3303 return "[any switch]";
3304 end if;
3305
3306 if Def.Switch /= null then
3307 Decompose_Switch (Def.Switch.all, P1, Last1);
3308 Append (Result, Def.Switch (Def.Switch'First .. Last1));
3309
3310 if Def.Long_Switch /= null then
3311 Decompose_Switch (Def.Long_Switch.all, P2, Last2);
3312 Append (Result, ", "
3313 & Def.Long_Switch (Def.Long_Switch'First .. Last2));
3314
3315 if Def.Argument = null then
3316 Append (Result, Param_Name (P2, "ARG"));
3317 else
3318 Append (Result, Param_Name (P2, Def.Argument.all));
3319 end if;
3320
3321 else
3322 if Def.Argument = null then
3323 Append (Result, Param_Name (P1, "ARG"));
3324 else
3325 Append (Result, Param_Name (P1, Def.Argument.all));
3326 end if;
3327 end if;
3328
3329 -- Def.Switch is null (Long_Switch must be non-null)
3330
3331 else
3332 Decompose_Switch (Def.Long_Switch.all, P2, Last2);
3333 Append (Result,
3334 Def.Long_Switch (Def.Long_Switch'First .. Last2));
3335
3336 if Def.Argument = null then
3337 Append (Result, Param_Name (P2, "ARG"));
3338 else
3339 Append (Result, Param_Name (P2, Def.Argument.all));
3340 end if;
3341 end if;
3342 end if;
3343
3344 return To_String (Result);
3345 end Switch_Name;
3346
3347 -- Start of processing for Display_Help
3348
3349 begin
3350 if Config = null then
3351 return;
3352 end if;
3353
3354 if Config.Help /= null and then Config.Help.all /= "" then
3355 Put_Line (Config.Help.all);
3356 end if;
3357
3358 if Config.Usage /= null then
3359 Put_Line ("Usage: "
3360 & Base_Name
3361 (Ada.Command_Line.Command_Name) & " " & Config.Usage.all);
3362 else
3363 Put_Line ("Usage: " & Base_Name (Ada.Command_Line.Command_Name)
3364 & " [switches] [arguments]");
3365 end if;
3366
3367 if Config.Help_Msg /= null and then Config.Help_Msg.all /= "" then
3368 Put_Line (Config.Help_Msg.all);
3369
3370 else
3371 Display_Section_Help ("");
3372
3373 if Config.Sections /= null and then Config.Switches /= null then
3374 for S in Config.Sections'Range loop
3375 Display_Section_Help (Config.Sections (S).all);
3376 end loop;
3377 end if;
3378 end if;
3379 end Display_Help;
3380
3381 ------------
3382 -- Getopt --
3383 ------------
3384
3385 procedure Getopt
3386 (Config : Command_Line_Configuration;
3387 Callback : Switch_Handler := null;
3388 Parser : Opt_Parser := Command_Line_Parser;
3389 Concatenate : Boolean := True;
3390 Quiet : Boolean := False)
3391 is
3392 Local_Config : Command_Line_Configuration := Config;
3393 Getopt_Switches : String_Access;
3394 C : Character := ASCII.NUL;
3395
3396 Empty_Name : aliased constant String := "";
3397 Current_Section : Integer := -1;
3398 Section_Name : not null access constant String := Empty_Name'Access;
3399
3400 procedure Simple_Callback
3401 (Simple_Switch : String;
3402 Separator : String;
3403 Parameter : String;
3404 Index : Integer);
3405 -- Needs comments ???
3406
3407 procedure Do_Callback (Switch, Parameter : String; Index : Integer);
3408
3409 -----------------
3410 -- Do_Callback --
3411 -----------------
3412
3413 procedure Do_Callback (Switch, Parameter : String; Index : Integer) is
3414 begin
3415 -- Do automatic handling when possible
3416
3417 if Index /= -1 then
3418 case Local_Config.Switches (Index).Typ is
3419 when Switch_Untyped =>
3420 null; -- no automatic handling
3421
3422 when Switch_Boolean =>
3423 Local_Config.Switches (Index).Boolean_Output.all :=
3424 Local_Config.Switches (Index).Boolean_Value;
3425 return;
3426
3427 when Switch_Integer =>
3428 begin
3429 if Parameter = "" then
3430 Local_Config.Switches (Index).Integer_Output.all :=
3431 Local_Config.Switches (Index).Integer_Default;
3432 else
3433 Local_Config.Switches (Index).Integer_Output.all :=
3434 Integer'Value (Parameter);
3435 end if;
3436
3437 exception
3438 when Constraint_Error =>
3439 raise Invalid_Parameter
3440 with "Expected integer parameter for '"
3441 & Switch & "'";
3442 end;
3443
3444 return;
3445
3446 when Switch_String =>
3447 Free (Local_Config.Switches (Index).String_Output.all);
3448 Local_Config.Switches (Index).String_Output.all :=
3449 new String'(Parameter);
3450 return;
3451
3452 when Switch_Callback =>
3453 Local_Config.Switches (Index).Callback (Switch, Parameter);
3454 return;
3455 end case;
3456 end if;
3457
3458 -- Otherwise calls the user callback if one was defined
3459
3460 if Callback /= null then
3461 Callback (Switch => Switch,
3462 Parameter => Parameter,
3463 Section => Section_Name.all);
3464 end if;
3465 end Do_Callback;
3466
3467 procedure For_Each_Simple
3468 is new For_Each_Simple_Switch (Simple_Callback);
3469
3470 ---------------------
3471 -- Simple_Callback --
3472 ---------------------
3473
3474 procedure Simple_Callback
3475 (Simple_Switch : String;
3476 Separator : String;
3477 Parameter : String;
3478 Index : Integer)
3479 is
3480 pragma Unreferenced (Separator);
3481 begin
3482 Do_Callback (Switch => Simple_Switch,
3483 Parameter => Parameter,
3484 Index => Index);
3485 end Simple_Callback;
3486
3487 -- Start of processing for Getopt
3488
3489 begin
3490 -- We work with a local copy of Config, because Config can be null, for
3491 -- example if Define_Switch was never called. We could modify Config
3492 -- itself, but then we would have to make it into an 'in out' parameter,
3493 -- which would be incompatible.
3494
3495 if Local_Config = null then
3496 Local_Config := new Command_Line_Configuration_Record;
3497 end if;
3498
3499 if Local_Config.Switches = null then
3500 Local_Config.Switches := new Switch_Definitions (1 .. 0);
3501 end if;
3502
3503 -- Initialize sections
3504
3505 if Local_Config.Sections = null then
3506 Local_Config.Sections := new Argument_List'(1 .. 0 => null);
3507 end if;
3508
3509 Internal_Initialize_Option_Scan
3510 (Parser => Parser,
3511 Switch_Char => Parser.Switch_Character,
3512 Stop_At_First_Non_Switch => Parser.Stop_At_First,
3513 Section_Delimiters => Section_Delimiters (Local_Config));
3514
3515 Getopt_Switches := new String'
3516 (Get_Switches (Local_Config, Parser.Switch_Character, Section_Name.all)
3517 & " h -help");
3518
3519 -- Initialize output values for automatically handled switches
3520
3521 for S in Local_Config.Switches'Range loop
3522 case Local_Config.Switches (S).Typ is
3523 when Switch_Untyped | Switch_Callback =>
3524 null; -- Nothing to do
3525
3526 when Switch_Boolean =>
3527 Local_Config.Switches (S).Boolean_Output.all :=
3528 not Local_Config.Switches (S).Boolean_Value;
3529
3530 when Switch_Integer =>
3531 Local_Config.Switches (S).Integer_Output.all :=
3532 Local_Config.Switches (S).Integer_Initial;
3533
3534 when Switch_String =>
3535 if Local_Config.Switches (S).String_Output.all = null then
3536 Local_Config.Switches (S).String_Output.all :=
3537 new String'("");
3538 end if;
3539 end case;
3540 end loop;
3541
3542 -- For all sections, and all switches within those sections
3543
3544 loop
3545 C := Getopt (Switches => Getopt_Switches.all,
3546 Concatenate => Concatenate,
3547 Parser => Parser);
3548
3549 if C = '*' then
3550 -- Full_Switch already includes the leading '-'
3551
3552 Do_Callback (Switch => Full_Switch (Parser),
3553 Parameter => Parameter (Parser),
3554 Index => -1);
3555
3556 elsif C /= ASCII.NUL then
3557 if Full_Switch (Parser) = "h"
3558 or else
3559 Full_Switch (Parser) = "-help"
3560 then
3561 Display_Help (Local_Config);
3562 raise Exit_From_Command_Line;
3563 end if;
3564
3565 -- Do switch expansion if needed
3566
3567 For_Each_Simple
3568 (Local_Config,
3569 Section => Section_Name.all,
3570 Switch => Parser.Switch_Character & Full_Switch (Parser),
3571 Parameter => Parameter (Parser));
3572
3573 else
3574 if Current_Section = -1 then
3575 Current_Section := Local_Config.Sections'First;
3576 else
3577 Current_Section := Current_Section + 1;
3578 end if;
3579
3580 exit when Current_Section > Local_Config.Sections'Last;
3581
3582 Section_Name := Local_Config.Sections (Current_Section);
3583 Goto_Section (Section_Name.all, Parser);
3584
3585 Free (Getopt_Switches);
3586 Getopt_Switches := new String'
3587 (Get_Switches
3588 (Local_Config, Parser.Switch_Character, Section_Name.all));
3589 end if;
3590 end loop;
3591
3592 Free (Getopt_Switches);
3593
3594 exception
3595 when Invalid_Switch =>
3596 Free (Getopt_Switches);
3597
3598 -- Message inspired by "ls" on Unix
3599
3600 if not Quiet then
3601 Put_Line (Standard_Error,
3602 Base_Name (Ada.Command_Line.Command_Name)
3603 & ": unrecognized option '"
3604 & Full_Switch (Parser)
3605 & "'");
3606 Try_Help;
3607 end if;
3608
3609 raise;
3610
3611 when others =>
3612 Free (Getopt_Switches);
3613 raise;
3614 end Getopt;
3615
3616 -----------
3617 -- Build --
3618 -----------
3619
3620 procedure Build
3621 (Line : in out Command_Line;
3622 Args : out GNAT.OS_Lib.Argument_List_Access;
3623 Expanded : Boolean := False;
3624 Switch_Char : Character := '-')
3625 is
3626 Iter : Command_Line_Iterator;
3627 Count : Natural := 0;
3628
3629 begin
3630 Start (Line, Iter, Expanded => Expanded);
3631 while Has_More (Iter) loop
3632 if Is_New_Section (Iter) then
3633 Count := Count + 1;
3634 end if;
3635
3636 Count := Count + 1;
3637 Next (Iter);
3638 end loop;
3639
3640 Args := new Argument_List (1 .. Count);
3641 Count := Args'First;
3642
3643 Start (Line, Iter, Expanded => Expanded);
3644 while Has_More (Iter) loop
3645 if Is_New_Section (Iter) then
3646 Args (Count) := new String'(Switch_Char & Current_Section (Iter));
3647 Count := Count + 1;
3648 end if;
3649
3650 Args (Count) := new String'(Current_Switch (Iter)
3651 & Current_Separator (Iter)
3652 & Current_Parameter (Iter));
3653 Count := Count + 1;
3654 Next (Iter);
3655 end loop;
3656 end Build;
3657
3658 --------------
3659 -- Try_Help --
3660 --------------
3661
3662 -- Note: Any change to the message displayed should also be done in
3663 -- gnatbind.adb that does not use this interface.
3664
3665 procedure Try_Help is
3666 begin
3667 Put_Line
3668 (Standard_Error,
3669 "try """ & Base_Name (Ada.Command_Line.Command_Name, Suffix => ".exe")
3670 & " --help"" for more information.");
3671 end Try_Help;
3672
3673 end CLIC.Command_Line;