OSDN Git Service

2005-03-08 Doug Rupp <rupp@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatchop.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             G N A T C H O P                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --            Copyright (C) 1998-2005 Ada Core Technologies, Inc.           --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Ada.Command_Line;  use Ada.Command_Line;
28 with Ada.Text_IO;       use Ada.Text_IO;
29
30 with GNAT.Command_Line; use GNAT.Command_Line;
31 with GNAT.OS_Lib;       use GNAT.OS_Lib;
32 with GNAT.Heap_Sort_G;
33 with GNAT.Table;
34
35 with Gnatvsn;
36 with Hostparm;
37
38 with System.CRTL;       use System.CRTL;
39
40 procedure Gnatchop is
41
42    Terminate_Program : exception;
43    --  Used to terminate execution immediately
44
45    Config_File_Name : constant String_Access := new String'("gnat.adc");
46    --  The name of the file holding the GNAT configuration pragmas
47
48    Gcc : String_Access := new String'("gcc");
49    --  May be modified by switch --GCC=
50
51    Gcc_Set : Boolean := False;
52    --  True if a switch --GCC= is used
53
54    Gnat_Cmd : String_Access;
55    --  Command to execute the GNAT compiler
56
57    Gnat_Args : Argument_List_Access :=
58                  new Argument_List'
59                    (new String'("-c"),
60                     new String'("-x"),
61                     new String'("ada"),
62                     new String'("-gnats"),
63                     new String'("-gnatu"));
64    --  Arguments used in Gnat_Cmd call
65
66    EOF : constant Character := Character'Val (26);
67    --  Special character to signal end of file. Not required in input
68    --  files, but properly treated if present. Not generated in output
69    --  files except as a result of copying input file.
70
71    --------------------
72    -- File arguments --
73    --------------------
74
75    subtype File_Num is Natural;
76    subtype File_Offset is Natural;
77
78    type File_Entry is record
79       Name : String_Access;
80       --  Name of chop file or directory
81
82       SR_Name : String_Access;
83       --  Null unless the chop file starts with a source reference pragma
84       --  in which case this field points to the file name from this pragma.
85    end record;
86
87    package File is new GNAT.Table
88      (Table_Component_Type => File_Entry,
89       Table_Index_Type     => File_Num,
90       Table_Low_Bound      => 1,
91       Table_Initial        => 100,
92       Table_Increment      => 100);
93
94    Directory : String_Access;
95    --  Record name of directory, or a null string if no directory given
96
97    Compilation_Mode  : Boolean := False;
98    Overwrite_Files   : Boolean := False;
99    Preserve_Mode     : Boolean := False;
100    Quiet_Mode        : Boolean := False;
101    Source_References : Boolean := False;
102    Verbose_Mode      : Boolean := False;
103    Exit_On_Error     : Boolean := False;
104    --  Global options
105
106    Write_gnat_adc : Boolean := False;
107    --  Gets set true if we append to gnat.adc or create a new gnat.adc.
108    --  Used to inhibit complaint about no units generated.
109
110    ---------------
111    -- Unit list --
112    ---------------
113
114    type Line_Num is new Natural;
115    --  Line number (for source reference pragmas)
116
117    type Unit_Count_Type  is new Integer;
118    subtype Unit_Num      is Unit_Count_Type range 1 .. Unit_Count_Type'Last;
119    --  Used to refer to unit number in unit table
120
121    type SUnit_Num is new Integer;
122    --  Used to refer to entry in sorted units table. Note that entry
123    --  zero is only for use by Heapsort, and is not otherwise referenced.
124
125    type Unit_Kind is (Unit_Spec, Unit_Body, Config_Pragmas);
126
127    --  Structure to contain all necessary information for one unit.
128    --  Entries are also temporarily used to record config pragma sequences.
129
130    type Unit_Info is record
131       File_Name : String_Access;
132       --  File name from GNAT output line
133
134       Chop_File : File_Num;
135       --  File number in chop file sequence
136
137       Start_Line : Line_Num;
138       --  Line number from GNAT output line
139
140       Offset : File_Offset;
141       --  Offset name from GNAT output line
142
143       SR_Present : Boolean;
144       --  Set True if SR parameter present
145
146       Length : File_Offset;
147       --  A length of 0 means that the Unit is the last one in the file
148
149       Kind : Unit_Kind;
150       --  Indicates kind of unit
151
152       Sorted_Index : SUnit_Num;
153       --  Index of unit in sorted unit list
154
155       Bufferg : String_Access;
156       --  Pointer to buffer containing configuration pragmas to be
157       --  prepended. Null if no pragmas to be prepended.
158
159    end record;
160
161    --  The following table stores the unit offset information
162
163    package Unit is new GNAT.Table
164      (Table_Component_Type => Unit_Info,
165       Table_Index_Type     => Unit_Count_Type,
166       Table_Low_Bound      => 1,
167       Table_Initial        => 500,
168       Table_Increment      => 100);
169
170    --  The following table is used as a sorted index to the Unit.Table.
171    --  The entries in Unit.Table are not moved, instead we just shuffle
172    --  the entries in Sorted_Units. Note that the zeroeth entry in this
173    --  table is used by GNAT.Heap_Sort_G.
174
175    package Sorted_Units is new GNAT.Table
176      (Table_Component_Type => Unit_Num,
177       Table_Index_Type     => SUnit_Num,
178       Table_Low_Bound      => 0,
179       Table_Initial        => 500,
180       Table_Increment      => 100);
181
182    function Is_Duplicated (U : SUnit_Num) return Boolean;
183    --  Returns true if U is duplicated by a later unit.
184    --  Note that this function returns false for the last entry.
185
186    procedure Sort_Units;
187    --  Sort units and set up sorted unit table
188
189    ----------------------
190    -- File_Descriptors --
191    ----------------------
192
193    function dup  (handle   : File_Descriptor) return File_Descriptor;
194    function dup2 (from, to : File_Descriptor) return File_Descriptor;
195
196    ---------------------
197    -- Local variables --
198    ---------------------
199
200    Warning_Count : Natural := 0;
201    --  Count of warnings issued so far
202
203    -----------------------
204    -- Local subprograms --
205    -----------------------
206
207    procedure Error_Msg (Message : String; Warning : Boolean := False);
208    --  Produce an error message on standard error output
209
210    procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time);
211    --  Given the name of a file or directory, Name, set the
212    --  time stamp. This function must be used for an unopened file.
213
214    function Files_Exist return Boolean;
215    --  Check Unit.Table for possible file names that already exist
216    --  in the file system. Returns true if files exist, False otherwise
217
218    function Get_Maximum_File_Name_Length return Integer;
219    pragma Import (C, Get_Maximum_File_Name_Length,
220                  "__gnat_get_maximum_file_name_length");
221    --  Function to get maximum file name length for system
222
223    Maximum_File_Name_Length : constant Integer := Get_Maximum_File_Name_Length;
224    Maximum_File_Name_Length_String : constant String :=
225                                        Integer'Image
226                                          (Maximum_File_Name_Length);
227
228    function Locate_Executable
229      (Program_Name    : String;
230       Look_For_Prefix : Boolean := True)
231      return             String_Access;
232    --  Locate executable for given program name. This takes into account
233    --  the target-prefix of the current command, if Look_For_Prefix is True.
234
235    subtype EOL_Length is Natural range 0 .. 2;
236    --  Possible lengths of end of line sequence
237
238    type EOL_String (Len : EOL_Length := 0) is record
239       Str : String (1 .. Len);
240    end record;
241
242    function Get_EOL
243      (Source : access String;
244       Start  : Positive)
245       return   EOL_String;
246    --  Return the line terminator used in the passed string
247
248    procedure Parse_EOL (Source : access String; Ptr : in out Positive);
249    --  On return Source (Ptr) is the first character of the next line
250    --  or EOF. Source.all must be terminated by EOF.
251
252    function Parse_File (Num : File_Num) return Boolean;
253    --  Calls the GNAT compiler to parse the given source file and parses the
254    --  output using Parse_Offset_Info. Returns True if parse operation
255    --  completes, False if some system error (e.g. failure to read the
256    --  offset information) occurs.
257
258    procedure Parse_Offset_Info (Chop_File : File_Num; Source : access String);
259    --  Parses the output of the compiler indicating the offsets
260    --  and names of the compilation units in Chop_File.
261
262    procedure Parse_Token
263      (Source    : access String;
264       Ptr       : in out Positive;
265       Token_Ptr : out Positive);
266    --  Skips any separators and stores the start of the token in Token_Ptr.
267    --  Then stores the position of the next separator in Ptr.
268    --  On return Source (Token_Ptr .. Ptr - 1) is the token.
269
270    procedure Read_File
271      (FD       : File_Descriptor;
272       Contents : out String_Access;
273       Success  : out Boolean);
274    --  Reads file associated with FS into the newly allocated
275    --  string Contents.
276    --  [VMS] Success is true iff the number of bytes read is less than or
277    --   equal to the file size.
278    --  [Other] Success is true iff the number of bytes read is equal to
279    --   the file size.
280
281    function Report_Duplicate_Units return Boolean;
282    --  Output messages about duplicate units in the input files in Unit.Table
283    --  Returns True if any duplicates found, Fals if no duplicates found.
284
285    function Scan_Arguments return Boolean;
286    --  Scan command line options and set global variables accordingly.
287    --  Also scan out file and directory arguments. Returns True if scan
288    --  was successful, and False if the scan fails for any reason.
289
290    procedure Usage;
291    --  Output message on standard output describing syntax of gnatchop command
292
293    procedure Warning_Msg (Message : String);
294    --  Output a warning message on standard error and update warning count
295
296    function Write_Chopped_Files (Input : File_Num) return Boolean;
297    --  Write all units that result from chopping the Input file
298
299    procedure Write_Config_File (Input : File_Num; U : Unit_Num);
300    --  Call to write configuration pragmas (append them to gnat.adc)
301    --  Input is the file number for the chop file and U identifies the
302    --  unit entry for the configuration pragmas.
303
304    function Get_Config_Pragmas
305      (Input : File_Num;
306       U     : Unit_Num)
307       return  String_Access;
308    --  Call to read configuration pragmas from given unit entry, and
309    --  return a buffer containing the pragmas to be appended to
310    --  following units. Input is the file number for the chop file and
311    --  U identifies the unit entry for the configuration pragmas.
312
313    procedure Write_Source_Reference_Pragma
314      (Info    : Unit_Info;
315       Line    : Line_Num;
316       FD      : File_Descriptor;
317       EOL     : EOL_String;
318       Success : in out Boolean);
319    --  If Success is True on entry, writes a source reference pragma using
320    --  the chop file from Info, and the given line number. On return Success
321    --  indicates whether the write succeeded. If Success is False on entry,
322    --  or if the global flag Source_References is False, then the call to
323    --  Write_Source_Reference_Pragma has no effect. EOL indicates the end
324    --  of line sequence to be written at the end of the pragma.
325
326    procedure Write_Unit
327      (Source  : access String;
328       Num     : Unit_Num;
329       TS_Time : OS_Time;
330       Success : out Boolean);
331    --  Write one compilation unit of the source to file
332
333    ---------
334    -- dup --
335    ---------
336
337    function dup  (handle   : File_Descriptor) return File_Descriptor is
338    begin
339       return File_Descriptor (System.CRTL.dup (int (handle)));
340    end dup;
341
342    ----------
343    -- dup2 --
344    ----------
345
346    function dup2 (from, to : File_Descriptor) return File_Descriptor is
347    begin
348       return File_Descriptor (System.CRTL.dup2 (int (from), int (to)));
349    end dup2;
350
351    ---------------
352    -- Error_Msg --
353    ---------------
354
355    procedure Error_Msg (Message : String; Warning : Boolean := False) is
356    begin
357       Put_Line (Standard_Error, Message);
358
359       if not Warning then
360          Set_Exit_Status (Failure);
361
362          if Exit_On_Error then
363             raise Terminate_Program;
364          end if;
365       end if;
366    end Error_Msg;
367
368    ---------------------
369    -- File_Time_Stamp --
370    ---------------------
371
372    procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time) is
373       procedure Set_File_Time (Name : C_File_Name; Time : OS_Time);
374       pragma Import (C, Set_File_Time, "__gnat_set_file_time_name");
375
376    begin
377       Set_File_Time (Name, Time);
378    end File_Time_Stamp;
379
380    -----------------
381    -- Files_Exist --
382    -----------------
383
384    function Files_Exist return Boolean is
385       Exists : Boolean := False;
386
387    begin
388       for SNum in 1 .. SUnit_Num (Unit.Last) loop
389
390          --  Only check and report for the last instance of duplicated files
391
392          if not Is_Duplicated (SNum) then
393             declare
394                Info : constant Unit_Info :=
395                         Unit.Table (Sorted_Units.Table (SNum));
396
397             begin
398                if Is_Writable_File (Info.File_Name.all) then
399                   if Hostparm.OpenVMS then
400                      Error_Msg
401                        (Info.File_Name.all
402                         & " already exists, use /OVERWRITE to overwrite");
403                   else
404                      Error_Msg (Info.File_Name.all
405                                  & " already exists, use -w to overwrite");
406                   end if;
407
408                   Exists := True;
409                end if;
410             end;
411          end if;
412       end loop;
413
414       return Exists;
415    end Files_Exist;
416
417    ------------------------
418    -- Get_Config_Pragmas --
419    ------------------------
420
421    function Get_Config_Pragmas
422      (Input : File_Num;
423       U     : Unit_Num)
424       return  String_Access
425    is
426       Info    : Unit_Info renames Unit.Table (U);
427       FD      : File_Descriptor;
428       Name    : aliased constant String :=
429                   File.Table (Input).Name.all & ASCII.Nul;
430       Length  : File_Offset;
431       Buffer  : String_Access;
432       Success : Boolean;
433       Result  : String_Access;
434
435    begin
436       FD := Open_Read (Name'Address, Binary);
437
438       if FD = Invalid_FD then
439          Error_Msg ("cannot open " & File.Table (Input).Name.all);
440          return null;
441       end if;
442
443       Read_File (FD, Buffer, Success);
444
445       --  A length of 0 indicates that the rest of the file belongs to
446       --  this unit. The actual length must be calculated now. Take into
447       --  account that the last character (EOF) must not be written.
448
449       if Info.Length = 0 then
450          Length := Buffer'Last - (Buffer'First + Info.Offset);
451       else
452          Length := Info.Length;
453       end if;
454
455       Result := new String'(Buffer (1 .. Length));
456       Close (FD);
457       return Result;
458    end Get_Config_Pragmas;
459
460    -------------
461    -- Get_EOL --
462    -------------
463
464    function Get_EOL
465      (Source : access String;
466       Start  : Positive)
467       return   EOL_String
468    is
469       Ptr   : Positive := Start;
470       First : Positive;
471       Last  : Natural;
472
473    begin
474       --  Skip to end of line
475
476       while Source (Ptr) /= ASCII.CR and then
477             Source (Ptr) /= ASCII.LF and then
478             Source (Ptr) /= EOF
479       loop
480          Ptr := Ptr + 1;
481       end loop;
482
483       Last  := Ptr;
484
485       if Source (Ptr) /= EOF then
486
487          --  Found CR or LF
488
489          First := Ptr;
490
491       else
492          First := Ptr + 1;
493       end if;
494
495       --  Recognize CR/LF or LF/CR combination
496
497       if (Source (Ptr + 1) = ASCII.CR or Source (Ptr + 1) = ASCII.LF)
498          and then Source (Ptr) /= Source (Ptr + 1)
499       then
500          Last := First + 1;
501       end if;
502
503       return (Len => Last + 1 - First, Str => Source (First .. Last));
504    end Get_EOL;
505
506    -------------------
507    -- Is_Duplicated --
508    -------------------
509
510    function Is_Duplicated (U : SUnit_Num) return Boolean is
511    begin
512       return U < SUnit_Num (Unit.Last)
513         and then
514           Unit.Table (Sorted_Units.Table (U)).File_Name.all =
515           Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
516    end Is_Duplicated;
517
518    -----------------------
519    -- Locate_Executable --
520    -----------------------
521
522    function Locate_Executable
523      (Program_Name    : String;
524       Look_For_Prefix : Boolean := True) return String_Access
525    is
526       Current_Command : constant String := Normalize_Pathname (Command_Name);
527       End_Of_Prefix   : Natural;
528       Start_Of_Prefix : Positive;
529       Result          : String_Access;
530
531    begin
532       Start_Of_Prefix := Current_Command'First;
533       End_Of_Prefix   := Start_Of_Prefix - 1;
534
535       if Look_For_Prefix then
536
537          --  Find Start_Of_Prefix
538
539          for J in reverse Current_Command'Range loop
540             if Current_Command (J) = '/' or
541               Current_Command (J) = Directory_Separator or
542               Current_Command (J) = ':'
543             then
544                Start_Of_Prefix := J + 1;
545                exit;
546             end if;
547          end loop;
548
549          --  Find End_Of_Prefix
550
551          for J in reverse Start_Of_Prefix .. Current_Command'Last loop
552             if Current_Command (J) = '-' then
553                End_Of_Prefix := J;
554                exit;
555             end if;
556          end loop;
557       end if;
558
559       declare
560          Command : constant String :=
561                      Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
562                                                                 Program_Name;
563       begin
564          Result := Locate_Exec_On_Path (Command);
565
566          if Result = null then
567             Error_Msg
568               (Command & ": installation problem, executable not found");
569          end if;
570       end;
571
572       return Result;
573    end Locate_Executable;
574
575    ---------------
576    -- Parse_EOL --
577    ---------------
578
579    procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
580    begin
581       --  Skip to end of line
582
583       while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
584         and then Source (Ptr) /= EOF
585       loop
586          Ptr := Ptr + 1;
587       end loop;
588
589       if Source (Ptr) /= EOF then
590          Ptr := Ptr + 1;      -- skip CR or LF
591       end if;
592
593       --  Skip past CR/LF or LF/CR combination
594
595       if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
596          and then Source (Ptr) /= Source (Ptr - 1)
597       then
598          Ptr := Ptr + 1;
599       end if;
600    end Parse_EOL;
601
602    ----------------
603    -- Parse_File --
604    ----------------
605
606    function Parse_File (Num : File_Num) return Boolean is
607       Chop_Name   : constant String_Access   := File.Table (Num).Name;
608       Save_Stdout : constant File_Descriptor := dup (Standout);
609       Offset_Name : Temp_File_Name;
610       Offset_FD   : File_Descriptor;
611       Buffer      : String_Access;
612       Success     : Boolean;
613       Failure     : exception;
614
615    begin
616       --  Display copy of GNAT command if verbose mode
617
618       if Verbose_Mode then
619          Put (Gnat_Cmd.all);
620
621          for J in 1 .. Gnat_Args'Length loop
622             Put (' ');
623             Put (Gnat_Args (J).all);
624          end loop;
625
626          Put (' ');
627          Put_Line (Chop_Name.all);
628       end if;
629
630       --  Create temporary file
631
632       Create_Temp_File (Offset_FD, Offset_Name);
633
634       if Offset_FD = Invalid_FD then
635          Error_Msg ("gnatchop: cannot create temporary file");
636          Close (Save_Stdout);
637          return False;
638       end if;
639
640       --  Redirect Stdout to this temporary file in the Unix way
641
642       if dup2 (Offset_FD, Standout) = Invalid_FD then
643          Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
644          Close (Save_Stdout);
645          Close (Offset_FD);
646          return False;
647       end if;
648
649       --  Call Gnat on the source filename argument with special options
650       --  to generate offset information. If this special compilation completes
651       --  successfully then we can do the actual gnatchop operation.
652
653       Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
654
655       if not Success then
656          Error_Msg (Chop_Name.all & ": parse errors detected");
657          Error_Msg (Chop_Name.all & ": chop may not be successful");
658       end if;
659
660       --  Restore stdout
661
662       if dup2 (Save_Stdout, Standout) = Invalid_FD then
663          Error_Msg ("gnatchop: cannot restore stdout");
664       end if;
665
666       --  Reopen the file to start reading from the beginning
667
668       Close (Offset_FD);
669       Close (Save_Stdout);
670       Offset_FD := Open_Read (Offset_Name'Address, Binary);
671
672       if Offset_FD = Invalid_FD then
673          Error_Msg ("gnatchop: cannot access offset info");
674          raise Failure;
675       end if;
676
677       Read_File (Offset_FD, Buffer, Success);
678
679       if not Success then
680          Error_Msg ("gnatchop: error reading offset info");
681          Close (Offset_FD);
682          raise Failure;
683       else
684          Parse_Offset_Info (Num, Buffer);
685       end if;
686
687       --  Close and delete temporary file
688
689       Close (Offset_FD);
690       Delete_File (Offset_Name'Address, Success);
691
692       return Success;
693
694    exception
695       when Failure | Terminate_Program =>
696          Close (Offset_FD);
697          Delete_File (Offset_Name'Address, Success);
698          return False;
699
700    end Parse_File;
701
702    -----------------------
703    -- Parse_Offset_Info --
704    -----------------------
705
706    procedure Parse_Offset_Info
707      (Chop_File : File_Num;
708       Source    : access String)
709    is
710       First_Unit : constant Unit_Num := Unit.Last + 1;
711       Bufferg    : String_Access     := null;
712       Parse_Ptr  : File_Offset       := Source'First;
713       Token_Ptr  : File_Offset;
714       Info       : Unit_Info;
715
716       function Match (Literal : String) return Boolean;
717       --  Checks if given string appears at the current Token_Ptr location
718       --  and if so, bumps Parse_Ptr past the token and returns True. If
719       --  the string is not present, sets Parse_Ptr to Token_Ptr and
720       --  returns False.
721
722       -----------
723       -- Match --
724       -----------
725
726       function Match (Literal : String) return Boolean is
727       begin
728          Parse_Token (Source, Parse_Ptr, Token_Ptr);
729
730          if Source'Last  + 1 - Token_Ptr < Literal'Length
731            or else
732              Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
733          then
734             Parse_Ptr := Token_Ptr;
735             return False;
736          end if;
737
738          Parse_Ptr := Token_Ptr + Literal'Length;
739          return True;
740       end Match;
741
742    --  Start of processing for Parse_Offset_Info
743
744    begin
745       loop
746          --  Set default values, should get changed for all
747          --  units/pragmas except for the last
748
749          Info.Chop_File := Chop_File;
750          Info.Length := 0;
751
752          --  Parse the current line of offset information into Info
753          --  and exit the loop if there are any errors or on EOF.
754
755          --  First case, parse a line in the following format:
756
757          --  Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
758
759          --  Note that the unit name can be an operator name in quotes.
760          --  This is of course illegal, but both GNAT and gnatchop handle
761          --  the case so that this error does not intefere with chopping.
762
763          --  The SR ir present indicates that a source reference pragma
764          --  was processed as part of this unit (and that therefore no
765          --  Source_Reference pragma should be generated.
766
767          if Match ("Unit") then
768             Parse_Token (Source, Parse_Ptr, Token_Ptr);
769
770             if Match ("(body)") then
771                Info.Kind := Unit_Body;
772             elsif Match ("(spec)") then
773                Info.Kind := Unit_Spec;
774             else
775                exit;
776             end if;
777
778             exit when not Match ("line");
779             Parse_Token (Source, Parse_Ptr, Token_Ptr);
780             Info.Start_Line := Line_Num'Value
781               (Source (Token_Ptr .. Parse_Ptr - 1));
782
783             exit when not Match ("file offset");
784             Parse_Token (Source, Parse_Ptr, Token_Ptr);
785             Info.Offset := File_Offset'Value
786               (Source (Token_Ptr .. Parse_Ptr - 1));
787
788             Info.SR_Present := Match ("SR, ");
789
790             exit when not Match ("file name");
791             Parse_Token (Source, Parse_Ptr, Token_Ptr);
792             Info.File_Name := new String'
793               (Directory.all & Source (Token_Ptr .. Parse_Ptr - 1));
794             Parse_EOL (Source, Parse_Ptr);
795
796          --  Second case, parse a line of the following form
797
798          --  Configuration pragmas at line 10, file offset 223
799
800          elsif Match ("Configuration pragmas at") then
801             Info.Kind := Config_Pragmas;
802             Info.File_Name := Config_File_Name;
803
804             exit when not Match ("line");
805             Parse_Token (Source, Parse_Ptr, Token_Ptr);
806             Info.Start_Line := Line_Num'Value
807               (Source (Token_Ptr .. Parse_Ptr - 1));
808
809             exit when not Match ("file offset");
810             Parse_Token (Source, Parse_Ptr, Token_Ptr);
811             Info.Offset := File_Offset'Value
812               (Source (Token_Ptr .. Parse_Ptr - 1));
813
814             Parse_EOL (Source, Parse_Ptr);
815
816          --  Third case, parse a line of the following form
817
818          --    Source_Reference pragma for file "filename"
819
820          --  This appears at the start of the file only, and indicates
821          --  the name to be used on any generated Source_Reference pragmas.
822
823          elsif Match ("Source_Reference pragma for file ") then
824             Parse_Token (Source, Parse_Ptr, Token_Ptr);
825             File.Table (Chop_File).SR_Name :=
826               new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
827             Parse_EOL (Source, Parse_Ptr);
828             goto Continue;
829
830          --  Unrecognized keyword or end of file
831
832          else
833             exit;
834          end if;
835
836          --  Store the data in the Info record in the Unit.Table
837
838          Unit.Increment_Last;
839          Unit.Table (Unit.Last) := Info;
840
841          --  If this is not the first unit from the file, calculate
842          --  the length of the previous unit as difference of the offsets
843
844          if Unit.Last > First_Unit then
845             Unit.Table (Unit.Last - 1).Length :=
846               Info.Offset - Unit.Table (Unit.Last - 1).Offset;
847          end if;
848
849          --  If not in compilation mode combine current unit with any
850          --  preceding configuration pragmas.
851
852          if not Compilation_Mode
853            and then Unit.Last > First_Unit
854            and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
855          then
856             Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
857             Info.Offset := Unit.Table (Unit.Last - 1).Offset;
858
859             --  Delete the configuration pragma entry
860
861             Unit.Table (Unit.Last - 1) := Info;
862             Unit.Decrement_Last;
863          end if;
864
865          --  If in compilation mode, and previous entry is the initial
866          --  entry for the file and is for configuration pragmas, then
867          --  they are to be appended to every unit in the file.
868
869          if Compilation_Mode
870            and then Unit.Last = First_Unit + 1
871            and then Unit.Table (First_Unit).Kind = Config_Pragmas
872          then
873             Bufferg :=
874               Get_Config_Pragmas
875                 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
876             Unit.Table (Unit.Last - 1) := Info;
877             Unit.Decrement_Last;
878          end if;
879
880          Unit.Table (Unit.Last).Bufferg := Bufferg;
881
882          --  If in compilation mode, and this is not the first item,
883          --  combine configuration pragmas with previous unit, which
884          --  will cause an error message to be generated when the unit
885          --  is compiled.
886
887          if Compilation_Mode
888            and then Unit.Last > First_Unit
889            and then Unit.Table (Unit.Last).Kind = Config_Pragmas
890          then
891             Unit.Decrement_Last;
892          end if;
893
894       <<Continue>>
895          null;
896
897       end loop;
898
899       --  Find out if the loop was exited prematurely because of
900       --  an error or if the EOF marker was found.
901
902       if Source (Parse_Ptr) /= EOF then
903          Error_Msg
904            (File.Table (Chop_File).Name.all & ": error parsing offset info");
905          return;
906       end if;
907
908       --  Handle case of a chop file consisting only of config pragmas
909
910       if Unit.Last = First_Unit
911         and then Unit.Table (Unit.Last).Kind = Config_Pragmas
912       then
913          --  In compilation mode, we append such a file to gnat.adc
914
915          if Compilation_Mode then
916             Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
917             Unit.Decrement_Last;
918
919          --  In default (non-compilation) mode, this is invalid
920
921          else
922             Error_Msg
923               (File.Table (Chop_File).Name.all &
924                ": no units found (only pragmas)");
925             Unit.Decrement_Last;
926          end if;
927       end if;
928
929       --  Handle case of a chop file ending with config pragmas. This can
930       --  happen only in default non-compilation mode, since in compilation
931       --  mode such configuration pragmas are part of the preceding unit.
932       --  We simply concatenate such pragmas to the previous file which
933       --  will cause a compilation error, which is appropriate.
934
935       if Unit.Last > First_Unit
936         and then Unit.Table (Unit.Last).Kind = Config_Pragmas
937       then
938          Unit.Decrement_Last;
939       end if;
940    end Parse_Offset_Info;
941
942    -----------------
943    -- Parse_Token --
944    -----------------
945
946    procedure Parse_Token
947      (Source    : access String;
948       Ptr       : in out Positive;
949       Token_Ptr : out Positive)
950    is
951       In_Quotes : Boolean := False;
952
953    begin
954       --  Skip separators
955
956       while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
957          Ptr := Ptr + 1;
958       end loop;
959
960       Token_Ptr := Ptr;
961
962       --  Find end-of-token
963
964       while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
965         and then Source (Ptr) >= ' '
966       loop
967          if Source (Ptr) = '"' then
968             In_Quotes := not In_Quotes;
969          end if;
970
971          Ptr := Ptr + 1;
972       end loop;
973    end Parse_Token;
974
975    ---------------
976    -- Read_File --
977    ---------------
978
979    procedure Read_File
980      (FD       : File_Descriptor;
981       Contents : out String_Access;
982       Success  : out Boolean)
983    is
984       Length      : constant File_Offset := File_Offset (File_Length (FD));
985       --  Include room for EOF char
986       Buffer      : constant String_Access := new String (1 .. Length + 1);
987
988       This_Read   : Integer;
989       Read_Ptr    : File_Offset := 1;
990
991    begin
992
993       loop
994          This_Read := Read (FD,
995            A => Buffer (Read_Ptr)'Address,
996            N => Length + 1 - Read_Ptr);
997          Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
998          exit when This_Read <= 0;
999       end loop;
1000
1001       Buffer (Read_Ptr) := EOF;
1002       Contents := new String (1 .. Read_Ptr);
1003       Contents.all := Buffer (1 .. Read_Ptr);
1004
1005       --  Things aren't simple on VMS due to the plethora of file types
1006       --  and organizations. It seems clear that there shouldn't be more
1007       --  bytes read than are contained in the file though.
1008
1009       if Hostparm.OpenVMS then
1010          Success := Read_Ptr <= Length + 1;
1011       else
1012          Success := Read_Ptr = Length + 1;
1013       end if;
1014    end Read_File;
1015
1016    ----------------------------
1017    -- Report_Duplicate_Units --
1018    ----------------------------
1019
1020    function Report_Duplicate_Units return Boolean is
1021       US : SUnit_Num;
1022       U  : Unit_Num;
1023
1024       Duplicates : Boolean  := False;
1025
1026    begin
1027       US := 1;
1028       while US < SUnit_Num (Unit.Last) loop
1029          U := Sorted_Units.Table (US);
1030
1031          if Is_Duplicated (US) then
1032             Duplicates := True;
1033
1034             --  Move to last two versions of duplicated file to make it clearer
1035             --  to understand which file is retained in case of overwriting.
1036
1037             while US + 1 < SUnit_Num (Unit.Last) loop
1038                exit when not Is_Duplicated (US + 1);
1039                US := US + 1;
1040             end loop;
1041
1042             U := Sorted_Units.Table (US);
1043
1044             if Overwrite_Files then
1045                Warning_Msg (Unit.Table (U).File_Name.all
1046                  & " is duplicated (all but last will be skipped)");
1047
1048             elsif Unit.Table (U).Chop_File =
1049                     Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1050             then
1051                Error_Msg (Unit.Table (U).File_Name.all
1052                  & " is duplicated in "
1053                  & File.Table (Unit.Table (U).Chop_File).Name.all);
1054
1055             else
1056                Error_Msg (Unit.Table (U).File_Name.all
1057                   & " in "
1058                   & File.Table (Unit.Table (U).Chop_File).Name.all
1059                   & " is duplicated in "
1060                   & File.Table
1061                       (Unit.Table
1062                         (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1063             end if;
1064          end if;
1065
1066          US := US + 1;
1067       end loop;
1068
1069       if Duplicates and not Overwrite_Files then
1070          if Hostparm.OpenVMS then
1071             Put_Line
1072               ("use /OVERWRITE to overwrite files and keep last version");
1073          else
1074             Put_Line ("use -w to overwrite files and keep last version");
1075          end if;
1076       end if;
1077
1078       return Duplicates;
1079    end Report_Duplicate_Units;
1080
1081    --------------------
1082    -- Scan_Arguments --
1083    --------------------
1084
1085    function Scan_Arguments return Boolean is
1086       Kset : Boolean := False;
1087       --  Set true if -k switch found
1088
1089    begin
1090       Initialize_Option_Scan;
1091
1092       --  Scan options first
1093
1094       loop
1095          case Getopt ("c gnat? h k? p q r v w x -GCC=!") is
1096             when ASCII.NUL =>
1097                exit;
1098
1099             when '-' =>
1100                Gcc     := new String'(Parameter);
1101                Gcc_Set := True;
1102
1103             when 'c' =>
1104                Compilation_Mode := True;
1105
1106             when 'g' =>
1107                Gnat_Args :=
1108                  new Argument_List'(Gnat_Args.all &
1109                                       new String'("-gnat" & Parameter));
1110
1111             when 'h' =>
1112                Usage;
1113                raise Terminate_Program;
1114
1115             when 'k' =>
1116                declare
1117                   Param : String_Access := new String'(Parameter);
1118
1119                begin
1120                   if Param.all /= "" then
1121                      for J in Param'Range loop
1122                         if Param (J) not in '0' .. '9' then
1123                            if Hostparm.OpenVMS then
1124                               Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn" &
1125                                          " requires numeric parameter");
1126                            else
1127                               Error_Msg ("-k# requires numeric parameter");
1128                            end if;
1129
1130                            return False;
1131                         end if;
1132                      end loop;
1133
1134                   else
1135                      if Hostparm.OpenVMS then
1136                         Param := new String'("39");
1137                      else
1138                         Param := new String'("8");
1139                      end if;
1140                   end if;
1141
1142                   Gnat_Args :=
1143                     new Argument_List'(Gnat_Args.all &
1144                                          new String'("-gnatk" & Param.all));
1145                   Kset := True;
1146                end;
1147
1148             when 'p' =>
1149                Preserve_Mode := True;
1150
1151             when 'q' =>
1152                Quiet_Mode := True;
1153
1154             when 'r' =>
1155                Source_References := True;
1156
1157             when 'v' =>
1158                Verbose_Mode := True;
1159
1160                --  Why is following written to standard error. Most other
1161                --  tools write to standard output ???
1162
1163                Put (Standard_Error, "GNATCHOP ");
1164                Put_Line (Standard_Error, Gnatvsn.Gnat_Version_String);
1165                Put_Line
1166                  (Standard_Error,
1167                   "Copyright 1998-2005, Ada Core Technologies Inc.");
1168
1169             when 'w' =>
1170                Overwrite_Files := True;
1171
1172             when 'x' =>
1173                Exit_On_Error := True;
1174
1175             when others =>
1176                null;
1177          end case;
1178       end loop;
1179
1180       if not Kset and then Maximum_File_Name_Length > 0 then
1181
1182          --  If this system has restricted filename lengths, tell gnat1
1183          --  about them, removing the leading blank from the image string.
1184
1185          Gnat_Args :=
1186            new Argument_List'(Gnat_Args.all
1187              & new String'("-gnatk"
1188                & Maximum_File_Name_Length_String
1189                  (Maximum_File_Name_Length_String'First + 1
1190                   .. Maximum_File_Name_Length_String'Last)));
1191       end if;
1192
1193       --  Scan file names
1194
1195       loop
1196          declare
1197             S : constant String := Get_Argument (Do_Expansion => True);
1198
1199          begin
1200             exit when S = "";
1201             File.Increment_Last;
1202             File.Table (File.Last).Name    := new String'(S);
1203             File.Table (File.Last).SR_Name := null;
1204          end;
1205       end loop;
1206
1207       --  Case of more than one file where last file is a directory
1208
1209       if File.Last > 1
1210         and then Is_Directory (File.Table (File.Last).Name.all)
1211       then
1212          Directory := File.Table (File.Last).Name;
1213          File.Decrement_Last;
1214
1215          --  Make sure Directory is terminated with a directory separator,
1216          --  so we can generate the output by just appending a filename.
1217
1218          if Directory (Directory'Last) /= Directory_Separator
1219             and then Directory (Directory'Last) /= '/'
1220          then
1221             Directory := new String'(Directory.all & Directory_Separator);
1222          end if;
1223
1224       --  At least one filename must be given
1225
1226       elsif File.Last = 0 then
1227          Usage;
1228          return False;
1229
1230       --  No directory given, set directory to null, so that we can just
1231       --  concatenate the directory name to the file name unconditionally.
1232
1233       else
1234          Directory := new String'("");
1235       end if;
1236
1237       --  Finally check all filename arguments
1238
1239       for File_Num in 1 .. File.Last loop
1240          declare
1241             F : constant String := File.Table (File_Num).Name.all;
1242
1243          begin
1244
1245             if Is_Directory (F) then
1246                Error_Msg (F & " is a directory, cannot be chopped");
1247                return False;
1248
1249             elsif not Is_Regular_File (F) then
1250                Error_Msg (F & " not found");
1251                return False;
1252             end if;
1253          end;
1254       end loop;
1255
1256       return True;
1257
1258    exception
1259       when Invalid_Switch =>
1260          Error_Msg ("invalid switch " & Full_Switch);
1261          return False;
1262
1263       when Invalid_Parameter =>
1264          if Hostparm.OpenVMS then
1265             Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn qualifier" &
1266                        " requires numeric parameter");
1267          else
1268             Error_Msg ("-k switch requires numeric parameter");
1269          end if;
1270
1271          return False;
1272
1273    end Scan_Arguments;
1274
1275    ----------------
1276    -- Sort_Units --
1277    ----------------
1278
1279    procedure Sort_Units is
1280
1281       procedure Move (From : Natural; To : Natural);
1282       --  Procedure used to sort the unit list
1283       --  Unit.Table (To) := Unit_List (From); used by sort
1284
1285       function Lt (Left, Right : Natural) return Boolean;
1286       --  Compares Left and Right units based on file name (first),
1287       --  Chop_File (second) and Offset (third). This ordering is
1288       --  important to keep the last version in case of duplicate files.
1289
1290       package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1291       --  Used for sorting on filename to detect duplicates
1292
1293       --------
1294       -- Lt --
1295       --------
1296
1297       function Lt (Left, Right : Natural) return Boolean is
1298          L : Unit_Info renames
1299                Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1300
1301          R : Unit_Info renames
1302                Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1303
1304       begin
1305          return L.File_Name.all < R.File_Name.all
1306            or else (L.File_Name.all = R.File_Name.all
1307                      and then (L.Chop_File < R.Chop_File
1308                                  or else (L.Chop_File = R.Chop_File
1309                                             and then L.Offset < R.Offset)));
1310       end Lt;
1311
1312       ----------
1313       -- Move --
1314       ----------
1315
1316       procedure Move (From : Natural; To : Natural) is
1317       begin
1318          Sorted_Units.Table (SUnit_Num (To)) :=
1319            Sorted_Units.Table (SUnit_Num (From));
1320       end Move;
1321
1322    --  Start of processing for Sort_Units
1323
1324    begin
1325       Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1326
1327       for J in 1 .. Unit.Last loop
1328          Sorted_Units.Table (SUnit_Num (J)) := J;
1329       end loop;
1330
1331       --  Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1332
1333       Unit_Sort.Sort (Natural (Unit.Last));
1334
1335       --  Set the Sorted_Index fields in the unit tables
1336
1337       for J in 1 .. SUnit_Num (Unit.Last) loop
1338          Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1339       end loop;
1340    end Sort_Units;
1341
1342    -----------
1343    -- Usage --
1344    -----------
1345
1346    procedure Usage is
1347    begin
1348       Put_Line
1349         ("Usage: gnatchop [-c] [-h] [-k#] " &
1350          "[-r] [-p] [-q] [-v] [-w] [-x] [--GCC=xx] file [file ...] [dir]");
1351
1352       New_Line;
1353       Put_Line
1354         ("  -c       compilation mode, configuration pragmas " &
1355          "follow RM rules");
1356
1357       Put_Line
1358         ("  -gnatxxx passes the -gnatxxx switch to gnat parser");
1359
1360       Put_Line
1361         ("  -h       help: output this usage information");
1362
1363       Put_Line
1364         ("  -k#      krunch file names of generated files to " &
1365          "no more than # characters");
1366
1367       Put_Line
1368         ("  -k       krunch file names of generated files to " &
1369          "no more than 8 characters");
1370
1371       Put_Line
1372         ("  -p       preserve time stamp, output files will " &
1373          "have same stamp as input");
1374
1375       Put_Line
1376         ("  -q       quiet mode, no output of generated file " &
1377          "names");
1378
1379       Put_Line
1380         ("  -r       generate Source_Reference pragmas refer" &
1381          "encing original source file");
1382
1383       Put_Line
1384         ("  -v       verbose mode, output version and generat" &
1385          "ed commands");
1386
1387       Put_Line
1388         ("  -w       overwrite existing filenames");
1389
1390       Put_Line
1391         ("  -x       exit on error");
1392
1393       Put_Line
1394         ("  --GCC=xx specify the path of the gnat parser to be used");
1395
1396       New_Line;
1397       Put_Line
1398         ("  file...  list of source files to be chopped");
1399
1400       Put_Line
1401         ("  dir      directory location for split files (defa" &
1402          "ult = current directory)");
1403    end Usage;
1404
1405    -----------------
1406    -- Warning_Msg --
1407    -----------------
1408
1409    procedure Warning_Msg (Message : String) is
1410    begin
1411       Warning_Count := Warning_Count + 1;
1412       Put_Line (Standard_Error, "warning: " & Message);
1413    end Warning_Msg;
1414
1415    -------------------------
1416    -- Write_Chopped_Files --
1417    -------------------------
1418
1419    function Write_Chopped_Files (Input : File_Num) return Boolean is
1420       Name    : aliased constant String :=
1421                   File.Table (Input).Name.all & ASCII.Nul;
1422       FD      : File_Descriptor;
1423       Buffer  : String_Access;
1424       Success : Boolean;
1425       TS_Time : OS_Time;
1426
1427    begin
1428       FD := Open_Read (Name'Address, Binary);
1429       TS_Time := File_Time_Stamp (FD);
1430
1431       if FD = Invalid_FD then
1432          Error_Msg ("cannot open " & File.Table (Input).Name.all);
1433          return False;
1434       end if;
1435
1436       Read_File (FD, Buffer, Success);
1437
1438       if not Success then
1439          Error_Msg ("cannot read " & File.Table (Input).Name.all);
1440          Close (FD);
1441          return False;
1442       end if;
1443
1444       if not Quiet_Mode then
1445          Put_Line ("splitting " & File.Table (Input).Name.all & " into:");
1446       end if;
1447
1448       --  Only chop those units that come from this file
1449
1450       for Num in 1 .. Unit.Last loop
1451          if Unit.Table (Num).Chop_File = Input then
1452             Write_Unit (Buffer, Num, TS_Time, Success);
1453             exit when not Success;
1454          end if;
1455       end loop;
1456
1457       Close (FD);
1458       return Success;
1459
1460    end Write_Chopped_Files;
1461
1462    -----------------------
1463    -- Write_Config_File --
1464    -----------------------
1465
1466    procedure Write_Config_File (Input : File_Num; U : Unit_Num) is
1467       FD      : File_Descriptor;
1468       Name    : aliased constant String := "gnat.adc" & ASCII.NUL;
1469       Buffer  : String_Access;
1470       Success : Boolean;
1471       Append  : Boolean;
1472       Buffera : String_Access;
1473       Bufferl : Natural;
1474
1475    begin
1476       Write_gnat_adc := True;
1477       FD := Open_Read_Write (Name'Address, Binary);
1478
1479       if FD = Invalid_FD then
1480          FD := Create_File (Name'Address, Binary);
1481          Append := False;
1482
1483          if not Quiet_Mode then
1484             Put_Line ("writing configuration pragmas from " &
1485                File.Table (Input).Name.all & " to gnat.adc");
1486          end if;
1487
1488       else
1489          Append := True;
1490
1491          if not Quiet_Mode then
1492             Put_Line
1493               ("appending configuration pragmas from " &
1494                File.Table (Input).Name.all & " to gnat.adc");
1495          end if;
1496       end if;
1497
1498       Success := FD /= Invalid_FD;
1499
1500       if not Success then
1501          Error_Msg ("cannot create gnat.adc");
1502          return;
1503       end if;
1504
1505       --  In append mode, acquire existing gnat.adc file
1506
1507       if Append then
1508          Read_File (FD, Buffera, Success);
1509
1510          if not Success then
1511             Error_Msg ("cannot read gnat.adc");
1512             return;
1513          end if;
1514
1515          --  Find location of EOF byte if any to exclude from append
1516
1517          Bufferl := 1;
1518          while Bufferl <= Buffera'Last
1519            and then Buffera (Bufferl) /= EOF
1520          loop
1521             Bufferl := Bufferl + 1;
1522          end loop;
1523
1524          Bufferl := Bufferl - 1;
1525          Close (FD);
1526
1527          --  Write existing gnat.adc to new gnat.adc file
1528
1529          FD := Create_File (Name'Address, Binary);
1530          Success := Write (FD, Buffera (1)'Address, Bufferl) = Bufferl;
1531
1532          if not Success then
1533             Error_Msg ("error writing gnat.adc");
1534             return;
1535          end if;
1536       end if;
1537
1538       Buffer := Get_Config_Pragmas  (Input, U);
1539
1540       if Buffer /= null then
1541          Success := Write (FD, Buffer.all'Address, Buffer'Length) =
1542                                  Buffer'Length;
1543
1544          if not Success then
1545             Error_Msg ("disk full writing gnat.adc");
1546             return;
1547          end if;
1548       end if;
1549
1550       Close (FD);
1551    end Write_Config_File;
1552
1553    -----------------------------------
1554    -- Write_Source_Reference_Pragma --
1555    -----------------------------------
1556
1557    procedure Write_Source_Reference_Pragma
1558      (Info    : Unit_Info;
1559       Line    : Line_Num;
1560       FD      : File_Descriptor;
1561       EOL     : EOL_String;
1562       Success : in out Boolean)
1563    is
1564       FTE : File_Entry renames File.Table (Info.Chop_File);
1565       Nam : String_Access;
1566
1567    begin
1568       if Success and Source_References and not Info.SR_Present then
1569          if FTE.SR_Name /= null then
1570             Nam := FTE.SR_Name;
1571          else
1572             Nam := FTE.Name;
1573          end if;
1574
1575          declare
1576             Reference : aliased String :=
1577                           "pragma Source_Reference (000000, """
1578                             & Nam.all & """);" & EOL.Str;
1579
1580             Pos : Positive := Reference'First;
1581             Lin : Line_Num := Line;
1582
1583          begin
1584             while Reference (Pos + 1) /= ',' loop
1585                Pos := Pos + 1;
1586             end loop;
1587
1588             while Reference (Pos) = '0' loop
1589                Reference (Pos) := Character'Val
1590                  (Character'Pos ('0') + Lin mod 10);
1591                Lin := Lin / 10;
1592                Pos := Pos - 1;
1593             end loop;
1594
1595             --  Assume there are enough zeroes for any program length
1596
1597             pragma Assert (Lin = 0);
1598
1599             Success :=
1600               Write (FD, Reference'Address, Reference'Length)
1601                                                      = Reference'Length;
1602          end;
1603       end if;
1604    end Write_Source_Reference_Pragma;
1605
1606    ----------------
1607    -- Write_Unit --
1608    ----------------
1609
1610    procedure Write_Unit
1611      (Source  : access String;
1612       Num     : Unit_Num;
1613       TS_Time : OS_Time;
1614       Success : out Boolean)
1615    is
1616       Info   : Unit_Info renames Unit.Table (Num);
1617       FD     : File_Descriptor;
1618       Name   : aliased constant String := Info.File_Name.all & ASCII.NUL;
1619       Length : File_Offset;
1620       EOL    : constant EOL_String :=
1621                  Get_EOL (Source, Source'First + Info.Offset);
1622
1623    begin
1624       --  Skip duplicated files
1625
1626       if Is_Duplicated (Info.Sorted_Index) then
1627          Put_Line ("   " & Info.File_Name.all & " skipped");
1628          Success := Overwrite_Files;
1629          return;
1630       end if;
1631
1632       if Overwrite_Files then
1633          FD := Create_File (Name'Address, Binary);
1634       else
1635          FD := Create_New_File (Name'Address, Binary);
1636       end if;
1637
1638       Success := FD /= Invalid_FD;
1639
1640       if not Success then
1641          Error_Msg ("cannot create " & Info.File_Name.all);
1642          return;
1643       end if;
1644
1645       --  A length of 0 indicates that the rest of the file belongs to
1646       --  this unit. The actual length must be calculated now. Take into
1647       --  account that the last character (EOF) must not be written.
1648
1649       if Info.Length = 0 then
1650          Length := Source'Last - (Source'First + Info.Offset);
1651       else
1652          Length := Info.Length;
1653       end if;
1654
1655       --  Prepend configuration pragmas if necessary
1656
1657       if Success and then Info.Bufferg /= null then
1658          Write_Source_Reference_Pragma (Info, 1, FD, EOL, Success);
1659          Success :=
1660            Write (FD, Info.Bufferg.all'Address, Info.Bufferg'Length) =
1661                                                        Info.Bufferg'Length;
1662       end if;
1663
1664       Write_Source_Reference_Pragma (Info, Info.Start_Line, FD, EOL, Success);
1665
1666       if Success then
1667          Success := Write (FD, Source (Source'First + Info.Offset)'Address,
1668                            Length) = Length;
1669       end if;
1670
1671       if not Success then
1672          Error_Msg ("disk full writing " & Info.File_Name.all);
1673          return;
1674       end if;
1675
1676       if not Quiet_Mode then
1677          Put_Line ("   " & Info.File_Name.all);
1678       end if;
1679
1680       Close (FD);
1681
1682       if Preserve_Mode then
1683          File_Time_Stamp (Name'Address, TS_Time);
1684       end if;
1685
1686    end Write_Unit;
1687
1688 --  Start of processing for gnatchop
1689
1690 begin
1691    --  Add the directory where gnatchop is invoked in front of the
1692    --  path, if gnatchop is invoked with directory information.
1693    --  Only do this if the platform is not VMS, where the notion of path
1694    --  does not really exist.
1695
1696    if not Hostparm.OpenVMS then
1697       declare
1698          Command : constant String := Command_Name;
1699
1700       begin
1701          for Index in reverse Command'Range loop
1702             if Command (Index) = Directory_Separator then
1703                declare
1704                   Absolute_Dir : constant String :=
1705                                    Normalize_Pathname
1706                                      (Command (Command'First .. Index));
1707
1708                   PATH         : constant String :=
1709                                    Absolute_Dir &
1710                   Path_Separator &
1711                   Getenv ("PATH").all;
1712
1713                begin
1714                   Setenv ("PATH", PATH);
1715                end;
1716
1717                exit;
1718             end if;
1719          end loop;
1720       end;
1721    end if;
1722
1723    --  Process command line options and initialize global variables
1724
1725    if not Scan_Arguments then
1726       Set_Exit_Status (Failure);
1727       return;
1728    end if;
1729
1730    --  Check presence of required executables
1731
1732    Gnat_Cmd := Locate_Executable (Gcc.all, not Gcc_Set);
1733
1734    if Gnat_Cmd = null then
1735       goto No_Files_Written;
1736    end if;
1737
1738    --  First parse all files and read offset information
1739
1740    for Num in 1 .. File.Last loop
1741       if not Parse_File (Num) then
1742          goto No_Files_Written;
1743       end if;
1744    end loop;
1745
1746    --  Check if any units have been found (assumes non-empty Unit.Table)
1747
1748    if Unit.Last = 0 then
1749       if not Write_gnat_adc then
1750          Error_Msg ("no compilation units found", Warning => True);
1751       end if;
1752
1753       goto No_Files_Written;
1754    end if;
1755
1756    Sort_Units;
1757
1758    --  Check if any duplicate files would be created. If so, emit
1759    --  a warning if Overwrite_Files is true, otherwise generate an error.
1760
1761    if Report_Duplicate_Units and then not Overwrite_Files then
1762       goto No_Files_Written;
1763    end if;
1764
1765    --  Check if any files exist, if so do not write anything
1766    --  Because all files have been parsed and checked already,
1767    --  there won't be any duplicates
1768
1769    if not Overwrite_Files and then Files_Exist then
1770       goto No_Files_Written;
1771    end if;
1772
1773    --  After this point, all source files are read in succession
1774    --  and chopped into their destination files.
1775
1776    --  As the Source_File_Name pragmas are handled as logical file 0,
1777    --  write it first.
1778
1779    for F in 1 .. File.Last loop
1780       if not Write_Chopped_Files (F) then
1781          Set_Exit_Status (Failure);
1782          return;
1783       end if;
1784    end loop;
1785
1786    if Warning_Count > 0 then
1787       declare
1788          Warnings_Msg : constant String := Warning_Count'Img & " warning(s)";
1789       begin
1790          Error_Msg (Warnings_Msg (2 .. Warnings_Msg'Last), Warning => True);
1791       end;
1792    end if;
1793
1794    return;
1795
1796 <<No_Files_Written>>
1797
1798    --  Special error exit for all situations where no files have
1799    --  been written.
1800
1801    if not Write_gnat_adc then
1802       Error_Msg ("no source files written", Warning => True);
1803    end if;
1804
1805    return;
1806
1807 exception
1808    when Terminate_Program =>
1809       null;
1810
1811 end Gnatchop;