1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
------------------------------------------------------------------------------
-- --
-- GNATPP COMPONENTS --
-- --
-- G N A T P P . E N V I R O N M E N T --
-- --
-- B o d y --
-- --
-- Copyright (C) 2001-2005, AdaCore --
-- --
-- GNATPP is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNATPP is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABI- --
-- LITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with GNAT; see file COPYING. If not, --
-- write to the Free Software Foundation, 59 Temple Place - Suite 330, --
-- Boston, --
-- --
-- GNATPP is maintained by AdaCore (http://www.adacore.com) --
-- --
------------------------------------------------------------------------------
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Wide_Text_IO;
with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Asis.Ada_Environments; use Asis.Ada_Environments;
with Asis.Implementation; use Asis.Implementation;
with ASIS_UL.Compiler_Options; use ASIS_UL.Compiler_Options;
with GNATPP.Common; use GNATPP.Common;
with GNATPP.Dictionaries; use GNATPP.Dictionaries;
with GNATPP.Options; use GNATPP.Options;
with GNATPP.Output; use GNATPP.Output;
with GNATPP.State; use GNATPP.State;
package body GNATPP.Environment is
------------------------
-- Local declarations --
------------------------
procedure Scan_Arguments;
-- Scans the command-line arguments
procedure Check_Parameters;
-- Checks, that GNATPP settings made as the result of parameter processing
-- are compatible (as a side effect, some internal parameters are
-- computed) and that the argument source file exist. If the check
-- fails, generates the diagnostic message and raises Parameter_Error.
procedure Read_Args_From_File (Par_File_Name : String);
-- Reads argument files from the file. Performs the same checks as when
-- file names are read from the command line. This procedure assumes that
-- the file named by Par_File_Name contains argiment file names separated
-- by one or more spaces.
-- ??? This is the direct duplication of the gnatmetric code!!!
-- We definitely need a common package for ASIS tools.
PP_Cont_Line_Indentation_Set : Boolean := False;
-- If this flag is OFF after scanning the gnatpp arguments, we have to
-- set the default value for PP_Cont_Line_Indentation
Temp_Dir : String_Access;
-- Contains the name of the temporary directory created by gnatpp for the
-- tree files
procedure Create_Temp_Dir;
-- Creates the temporary directory and stores its name in Temp_Dir. In case
-- if the directory is successfully created, changes the working directory
-- to this temporary directory. In case of a replace out mode
-- (Replace .. Replace_No_Backup) creates the out file in the temporary dir
-- to place the reformatted code before the reformatting is complete
----------------------
-- Check_Parameters --
----------------------
procedure Check_Parameters is
begin
Total_Sources := Natural (Last_Source);
Sources_Left := Total_Sources;
-- Check that GNAT_Comment_Inden and Standard_Comment_Indent
-- are not set together
if GNAT_Comment_Inden and then Standard_Comment_Indent then
Put (Standard_Error,
"gnatpp: comment processing modes -c1 and -c2 ");
Put_Line (Standard_Error, "can not be set together");
Brief_Help;
raise Parameter_Error;
end if;
if Last_Source = No_SF_Id then
Put (Standard_Error, "gnatpp: No input source file set");
New_Line;
Brief_Help;
raise Parameter_Error;
elsif Last_Source = First_SF_Id then
-- If we have only one source to reformat, we have to check
-- the settings of the output file, if it is set
Progress_Indicator_Mode := False;
-- We do not need this in case of one file, and we may be in the
-- mode of outputting the reformatted source into Stdout
Arg_Source_Name := new String'(Source_Name (Last_Source));
if Output_Mode in Create_File .. Force_Create_File then
-- We have to set the output file here, before we get into the
-- temporary directory
Out_File_Exists := False;
if Res_File_Name /= null and then
Is_Regular_File (Res_File_Name.all)
then
if Output_Mode = Create_File then
Put (Standard_Error, "gnatpp: file ");
Put (Standard_Error, Res_File_Name.all);
Put (Standard_Error, " exists. Use '-of' option");
Put (Standard_Error, " to override");
New_Line (Standard_Error);
raise Parameter_Error;
else
Out_File_Exists := True;
end if;
end if;
if Out_File_Exists then
Ada.Wide_Text_IO.Open
(File => Result_Out_File,
Mode => Ada.Wide_Text_IO.Out_File,
Name => Res_File_Name.all);
else
Ada.Wide_Text_IO.Create
(File => Result_Out_File,
Mode => Ada.Wide_Text_IO.Out_File,
Name => Res_File_Name.all);
end if;
end if;
else
-- If we have more then one file to reformat, we can not have options
-- '-pipe', '-o' or '-of' set
Multiple_File_Mode := True;
if Output_Mode = Pipe then
Put (Standard_Error, "gnatpp: can not send the output ");
Put (Standard_Error, "into Stdout when multiple ");
Put (Standard_Error, "argument sources set");
raise Parameter_Error;
elsif Output_Mode = Create_File or else
Output_Mode = Force_Create_File
then
Put (Standard_Error, "gnatpp: explicit output file name is not ");
Put (Standard_Error, "allowed when multiple ");
Put (Standard_Error, "argument sources set");
raise Parameter_Error;
end if;
end if;
if Output_Mode = Default and then -- ???
Arg_Source_Name /= null and then
Is_Regular_File (Arg_Source_Name.all & PP_Suffix)
then
Out_File_Exists := True;
end if;
-- And now - some preparations:
-- 1. Compute the default continuation line indentation, if needed
if not PP_Cont_Line_Indentation_Set then
PP_Cont_Line_Indentation := PP_Indentation;
if PP_Cont_Line_Indentation > 1 then
PP_Cont_Line_Indentation := PP_Cont_Line_Indentation - 1;
end if;
end if;
-- 2. Compute the max nesting level for which we can indent the code
Set_Max_Nesting_Level;
-- 3. Collect all the -I options into the argument list for gcc call
Set_Arg_List;
-- 4. Set all the alignments ON if needed
if Alignment_On then
Align_Colons_In_Decl := True;
Align_Asign_In_Decl := True;
Align_Asign_In_Stmts := True;
Align_Arrows := True;
Allign_Ats := True;
end if;
end Check_Parameters;
--------------
-- Clean_Up --
--------------
procedure Clean_Up is
begin
Report_Total_Failures;
if Is_Open (The_Context) then
Close (The_Context);
end if;
Dissociate (The_Context);
Finalize;
if Output_Mode /= Pipe and then -- ???
Ada.Wide_Text_IO.Is_Open (Result_Out_File)
then
Ada.Wide_Text_IO.Close (Result_Out_File);
end if;
-- Delete_Files;
Free (Arg_Source_Name);
Free (Res_File_Name);
-- Cleaning up temporary dir
if Temp_Dir /= null then
if not Is_Directory (Temp_Dir.all) then
-- We may be inside this temporary directory
Change_Dir ("..");
end if;
begin
Remove_Dir (Temp_Dir.all, Recursive => True);
exception
when Directory_Error =>
Free (Temp_Dir); -- to avoid cycling
Error ("gnatpp: can not remove temporary directory");
raise Fatal_Error;
end;
Free (Temp_Dir);
end if;
-- In case if we have only one argument file, set exit status according
-- to the success of the processing of this single file:
if Last_Source = First_SF_Id
and then
Source_Status (First_SF_Id) /= Processed
then
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end if;
end Clean_Up;
---------------------
-- Create_Temp_Dir --
---------------------
procedure Create_Temp_Dir is
FD : File_Descriptor;
Temp_Name : Temp_File_Name;
Success : Boolean;
begin
-- Here we use exactly the same approach as in gnatelim
-- ??? We create the temp dir by first creating the temp file, then
-- closing and deleting it, then creating a dir with the same name.
-- This is not atomary as another program can sneak in between file
-- deletion and dir creation and snatch this name for itself. This is
-- quite unlikely and anyway we don't have any other system-independent
-- way at the moment
Create_Temp_File (FD, Temp_Name);
Close (FD);
Delete_File (Temp_Name, Success);
if not Success then
Error ("gnatpp: can not delete the temporary file that was "
& "just created");
raise Fatal_Error;
end if;
Make_Dir (Temp_Name);
Temp_Dir :=
new String'(Temp_Name (Temp_Name'First .. Temp_Name'Last - 1));
Change_Dir (Temp_Dir.all);
if Output_Mode in Replace .. Replace_No_Backup then
Res_File_Name := new String'(Temp_Dir.all & PP_Suffix);
Ada.Wide_Text_IO.Create (File => Result_Out_File,
Mode => Ada.Wide_Text_IO.Out_File,
Name => Res_File_Name.all);
Ada.Wide_Text_IO.Close (File => Result_Out_File);
Out_File_Exists := True;
end if;
exception
when Directory_Error =>
Error ("gnatpp: can not create the temporary directory");
raise Fatal_Error;
when Ex : others =>
Report_Unhandled_Exception (Ex);
raise Fatal_Error;
end Create_Temp_Dir;
----------
-- Init --
----------
procedure Init is
begin
GNATPP.Source_Table.Init;
Scan_Arguments;
Check_Parameters;
Create_Temp_Dir;
exception
when Parameter_Error =>
raise Fatal_Error;
end Init;
-------------------------
-- Read_Args_From_File --
-------------------------
procedure Read_Args_From_File (Par_File_Name : String) is
No_More_Args : Boolean := False;
Arg_File : File_Type;
File_Name_Buffer : String (1 .. 16 * 1024);
File_Name_Len : Natural := 0;
Next_Ch : Character;
End_Of_Line : Boolean;
function Get_File_Name return String;
-- Reads from Par_File_Name the name of the next file (the file to read
-- from should exist and be opened). Returns an empty string if there is
-- no file names in Par_File_Name any more
function Get_File_Name return String is
begin
File_Name_Len := 0;
if not End_Of_File (Arg_File) then
Get (Arg_File, Next_Ch);
while Next_Ch = ' ' loop
exit when End_Of_File (Arg_File);
Get (Arg_File, Next_Ch);
end loop;
while Next_Ch /= ' ' loop
File_Name_Len := File_Name_Len + 1;
File_Name_Buffer (File_Name_Len) := Next_Ch;
Look_Ahead (Arg_File, Next_Ch, End_Of_Line);
exit when End_Of_Line or else End_Of_File (Arg_File);
Get (Arg_File, Next_Ch);
end loop;
end if;
return File_Name_Buffer (1 .. File_Name_Len);
end Get_File_Name;
begin
if not Is_Regular_File (Par_File_Name) then
Error ("gnatpp: " & To_Wide_String (Par_File_Name) &
"does not exist");
return;
end if;
Open (Arg_File, In_File, Par_File_Name);
loop
Add_Source_To_Process (Get_File_Name, No_More_Args);
exit when No_More_Args;
end loop;
Close (Arg_File);
exception
when others =>
Error ("gnatpp: can not read arguments from " &
To_Wide_String (Par_File_Name));
-- Exception info will be generated in main driver
raise;
end Read_Args_From_File;
--------------------
-- Scan_Arguments --
--------------------
procedure Scan_Arguments is
No_More_Args : Boolean := False;
Nat : Natural;
function Get_Nat_Switch_Parameter (Val : String) return Natural;
-- computes a natural parameter for switch from its string
-- representation. Raises Parameter_Error if Val can not be considered
-- as a string image of a natural number. This function supposes that
-- Val is not an empty string.
function Get_Nat_Switch_Parameter (Val : String) return Natural is
Result : Natural := 0;
begin
for I in Val'Range loop
if not Is_Digit (Val (I)) then
Put_Line ("gnatpp: wrong switch integer parameter " & Val);
raise Parameter_Error;
else
Result := Result * 10 +
Character'Pos (Val (I)) - Character'Pos ('0');
end if;
end loop;
return Result;
end Get_Nat_Switch_Parameter;
begin
PP_Cont_Line_Indentation_Set := False;
Initialize_Option_Scan
(Stop_At_First_Non_Switch => True,
Section_Delimiters => "cargs");
loop
case
GNAT.Command_Line.Getopt
("aL aU aM cl! c0 c1 c2 c3 c4 " &
"e ff i! kL kU l1 l2 l3 M! " &
"N notab nL nU nM nD D- D: " &
"pL pU pM " &
"A0 A1 A2 A3 A4 A5 T! " &
"I: gnatec! -RTS= v w q dd " &
-- output file control
"files= pipe o= of= r rf rnb")
is
when ASCII.NUL =>
exit;
when 'A' =>
if Full_Switch = "A0" then
Alignment_On := False;
elsif Full_Switch = "A1" then
Align_Colons_In_Decl := True;
elsif Full_Switch = "A2" then
Align_Colons_In_Decl := True;
Align_Asign_In_Decl := True;
elsif Full_Switch = "A3" then
Align_Asign_In_Stmts := True;
elsif Full_Switch = "A4" then
Align_Arrows := True;
elsif Full_Switch = "A5" then
Allign_Ats := True;
end if;
when 'a' =>
if Full_Switch = "aL" then
PP_Attribute_Casing := Lower_Case;
elsif Full_Switch = "aU" then
PP_Attribute_Casing := Upper_Case;
elsif Full_Switch = "aM" then
PP_Attribute_Casing := Mixed;
end if;
when 'c' =>
if Full_Switch = "c0" then
Format_Comments := False;
elsif Full_Switch = "c1" then
GNAT_Comment_Inden := True;
Standard_Comment_Indent := False;
elsif Full_Switch = "c2" then
GNAT_Comment_Inden := False;
Standard_Comment_Indent := True;
elsif Full_Switch = "c3" then
GNAT_Comment_Start := True;
elsif Full_Switch = "c4" then
Reformat_Comment_Block := True;
elsif Full_Switch = "cl" then
Nat := Get_Nat_Switch_Parameter (Parameter);
if Nat not in 1 .. 9 then
Put_Line ("gnatpp: wrong continuation line indentation ("
& Parameter & ")");
raise Parameter_Error;
else
PP_Cont_Line_Indentation := Nat;
PP_Cont_Line_Indentation_Set := True;
end if;
end if;
when 'D' =>
if Full_Switch = "D-" then
Use_Predefined_Casing := False;
else
Use_Dictionary := True;
Scan_Dictionary (Parameter);
end if;
when 'd' =>
Progress_Indicator_Mode := True;
when 'f' =>
if Full_Switch = "files" then
Read_Args_From_File (Parameter);
elsif Full_Switch = "ff" then
Add_FF := True;
end if;
when 'e' =>
End_Labels := False;
when 'i' =>
Nat := Get_Nat_Switch_Parameter (Parameter);
if Nat not in 1 .. 9 then
Put_Line ("gnatpp: wrong indentation (" & Parameter & ")");
raise Parameter_Error;
else
PP_Indentation := Nat;
end if;
when 'k' =>
if Full_Switch = "kL" then
PP_Keyword_Casing := Lower_Case;
elsif Full_Switch = "kU" then
PP_Keyword_Casing := Upper_Case;
end if;
when 'l' =>
if Full_Switch = "l1" then
Compact_Layout := True;
Add_Empty_Lines := True;
elsif Full_Switch = "l2" then
Compact_Layout := True;
Add_Empty_Lines := False;
elsif Full_Switch = "l3" then
Compact_Layout := False;
Add_Empty_Lines := False;
end if;
when 'M' =>
Nat := Get_Nat_Switch_Parameter (Parameter);
if Nat not in 32 .. 256 then
Put ("gnatpp: wrong max line length (");
Put (Parameter);
Put_Line (")");
raise Parameter_Error;
else
Max_Line_Length := Nat;
end if;
when 'n' =>
if Full_Switch = "nL" then
PP_Name_Casing := Lower_Case;
elsif Full_Switch = "nU" then
PP_Name_Casing := Upper_Case;
elsif Full_Switch = "nM" then
PP_Name_Casing := Mixed;
elsif Full_Switch = "nD" then
PP_Name_Casing := As_Declared;
elsif Full_Switch = "notab" then
-- -notab is an obsolete feature, replaced with -N
No_Tab_In_Comments := True;
end if;
when 'N' =>
No_Tab_In_Comments := True;
when 'p' =>
if Full_Switch = "pL" then
PP_Pragma_Casing := Lower_Case;
elsif Full_Switch = "pU" then
PP_Pragma_Casing := Upper_Case;
elsif Full_Switch = "pM" then
PP_Pragma_Casing := Mixed;
elsif Full_Switch = "pipe" then
Output_Mode := Pipe;
end if;
when 'q' =>
Quiet_Mode := True;
when 'r' =>
if Full_Switch = "r" then
Output_Mode := Replace;
elsif Full_Switch = "rnb" then
Output_Mode := Replace_No_Backup;
elsif Full_Switch = "rf" then
Output_Mode := Force_Replace;
end if;
when 'T' =>
Case_Threshold := Get_Nat_Switch_Parameter (Parameter);
when 'I' | 'g' | '-' =>
if Full_Switch = "I" then
Store_I_Option (Parameter);
elsif Full_Switch = "gnatec" then
Store_gnatec_Option (Parameter);
elsif Full_Switch = "-RTS" then
Store_Option ("--RTS=" & Parameter);
else
Brief_Help;
raise Parameter_Error;
end if;
when 'o' =>
if Full_Switch = "o" then
Output_Mode := Create_File;
elsif Full_Switch = "of" then
Output_Mode := Force_Create_File;
else
Brief_Help;
raise Parameter_Error;
end if;
Res_File_Name := new String'(Parameter);
when 'v' =>
Verbose_Mode := True;
when 'w' =>
Warnings_ON := True;
when others =>
Brief_Help;
raise Parameter_Error;
end case;
end loop;
if Verbose_Mode then
Version_Info;
end if;
loop
Add_Source_To_Process (Get_Argument, No_More_Args);
exit when No_More_Args;
end loop;
Process_cargs_Section;
exception
when GNAT.Command_Line.Invalid_Switch =>
Ada.Text_IO.Put_Line (Standard_Error,
"gnatpp: invalid switch : "
& GNAT.Command_Line.Full_Switch);
Brief_Help;
raise Parameter_Error;
when GNAT.Command_Line.Invalid_Parameter =>
Ada.Text_IO.Put_Line (Standard_Error,
"gnatpp: parameter missed for : "
& GNAT.Command_Line.Full_Switch);
Brief_Help;
raise Parameter_Error;
end Scan_Arguments;
---------------------
-- Source_Clean_Up --
---------------------
procedure Source_Clean_Up (SF : SF_Id) is
Success : Boolean;
begin
if Is_Open (The_Context) then
Close (The_Context);
end if;
if Has_Associations (The_Context) then
Dissociate (The_Context);
end if;
if Output_Mode /= Pipe and then
Ada.Wide_Text_IO.Is_Open (Result_Out_File)
then
Ada.Wide_Text_IO.Close (Result_Out_File);
end if;
Delete_File (Suffixless_Name (SF) & ".adt", Success); -- ???
Delete_File (Suffixless_Name (SF) & ".ali", Success); -- ???
end Source_Clean_Up;
end GNATPP.Environment;
|