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