OSDN Git Service

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