* dwarf2out.c, fold-const.c, ipa-type-escape.c,
[official-gcc.git] / gcc / ada / g-dirope.adb
blob886f2468913752705dafad61eb744d6d6522b8bc
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2005 Ada Core Technologies, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Characters.Handling;
35 with Ada.Strings.Fixed;
36 with Ada.Strings.Maps;
38 with Unchecked_Deallocation;
39 with Unchecked_Conversion;
41 with System; use System;
42 with System.CRTL; use System.CRTL;
44 with GNAT.OS_Lib;
46 package body GNAT.Directory_Operations is
48 use Ada;
50 type Dir_Type_Value is new System.Address;
51 -- This is the low-level address directory structure as returned by the C
52 -- opendir routine.
54 Filename_Max : constant Integer := 1024;
55 -- 1024 is the value of FILENAME_MAX in stdio.h
57 procedure Free is new
58 Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
60 ---------------
61 -- Base_Name --
62 ---------------
64 function Base_Name
65 (Path : Path_Name;
66 Suffix : String := "") return String
68 function Get_File_Names_Case_Sensitive return Integer;
69 pragma Import
70 (C, Get_File_Names_Case_Sensitive,
71 "__gnat_get_file_names_case_sensitive");
73 Case_Sensitive_File_Name : constant Boolean :=
74 Get_File_Names_Case_Sensitive = 1;
76 function Basename
77 (Path : Path_Name;
78 Suffix : String := "") return String;
79 -- This function does the job. The only difference between Basename
80 -- and Base_Name (the parent function) is that the former is case
81 -- sensitive, while the latter is not. Path and Suffix are adjusted
82 -- appropriately before calling Basename under platforms where the
83 -- file system is not case sensitive.
85 --------------
86 -- Basename --
87 --------------
89 function Basename
90 (Path : Path_Name;
91 Suffix : String := "") return String
93 Cut_Start : Natural :=
94 Strings.Fixed.Index
95 (Path, Dir_Seps, Going => Strings.Backward);
96 Cut_End : Natural;
98 begin
99 -- Cut_Start point to the first basename character
101 if Cut_Start = 0 then
102 Cut_Start := Path'First;
104 else
105 Cut_Start := Cut_Start + 1;
106 end if;
108 -- Cut_End point to the last basename character.
110 Cut_End := Path'Last;
112 -- If basename ends with Suffix, adjust Cut_End.
114 if Suffix /= ""
115 and then Path (Path'Last - Suffix'Length + 1 .. Cut_End) = Suffix
116 then
117 Cut_End := Path'Last - Suffix'Length;
118 end if;
120 Check_For_Standard_Dirs : declare
121 Offset : constant Integer := Path'First - Base_Name.Path'First;
122 BN : constant String :=
123 Base_Name.Path (Cut_Start - Offset .. Cut_End - Offset);
124 -- Here we use Base_Name.Path to keep the original casing
126 Has_Drive_Letter : constant Boolean :=
127 OS_Lib.Path_Separator /= ':';
128 -- If Path separator is not ':' then we are on a DOS based OS
129 -- where this character is used as a drive letter separator.
131 begin
132 if BN = "." or else BN = ".." then
133 return "";
135 elsif Has_Drive_Letter
136 and then BN'Length > 2
137 and then Characters.Handling.Is_Letter (BN (BN'First))
138 and then BN (BN'First + 1) = ':'
139 then
140 -- We have a DOS drive letter prefix, remove it
142 return BN (BN'First + 2 .. BN'Last);
144 else
145 return BN;
146 end if;
147 end Check_For_Standard_Dirs;
148 end Basename;
150 -- Start processing for Base_Name
152 begin
153 if Path'Length <= Suffix'Length then
154 return Path;
155 end if;
157 if Case_Sensitive_File_Name then
158 return Basename (Path, Suffix);
159 else
160 return Basename
161 (Characters.Handling.To_Lower (Path),
162 Characters.Handling.To_Lower (Suffix));
163 end if;
164 end Base_Name;
166 ----------------
167 -- Change_Dir --
168 ----------------
170 procedure Change_Dir (Dir_Name : Dir_Name_Str) is
171 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
173 function chdir (Dir_Name : String) return Integer;
174 pragma Import (C, chdir, "chdir");
176 begin
177 if chdir (C_Dir_Name) /= 0 then
178 raise Directory_Error;
179 end if;
180 end Change_Dir;
182 -----------
183 -- Close --
184 -----------
186 procedure Close (Dir : in out Dir_Type) is
187 Discard : Integer;
188 pragma Warnings (Off, Discard);
190 begin
191 if not Is_Open (Dir) then
192 raise Directory_Error;
193 end if;
195 Discard := closedir (DIRs (Dir.all));
196 Free (Dir);
197 end Close;
199 --------------
200 -- Dir_Name --
201 --------------
203 function Dir_Name (Path : Path_Name) return Dir_Name_Str is
204 Last_DS : constant Natural :=
205 Strings.Fixed.Index
206 (Path, Dir_Seps, Going => Strings.Backward);
208 begin
209 if Last_DS = 0 then
211 -- There is no directory separator, returns current working directory
213 return "." & Dir_Separator;
215 else
216 return Path (Path'First .. Last_DS);
217 end if;
218 end Dir_Name;
220 -----------------
221 -- Expand_Path --
222 -----------------
224 function Expand_Path
225 (Path : Path_Name;
226 Mode : Environment_Style := System_Default) return Path_Name
228 Environment_Variable_Char : Character;
229 pragma Import (C, Environment_Variable_Char, "__gnat_environment_char");
231 Result : OS_Lib.String_Access := new String (1 .. 200);
232 Result_Last : Natural := 0;
234 procedure Append (C : Character);
235 procedure Append (S : String);
236 -- Append to Result
238 procedure Double_Result_Size;
239 -- Reallocate Result, doubling its size
241 function Is_Var_Prefix (C : Character) return Boolean;
242 pragma Inline (Is_Var_Prefix);
244 procedure Read (K : in out Positive);
245 -- Update Result while reading current Path starting at position K. If
246 -- a variable is found, call Var below.
248 procedure Var (K : in out Positive);
249 -- Translate variable name starting at position K with the associated
250 -- environment value.
252 ------------
253 -- Append --
254 ------------
256 procedure Append (C : Character) is
257 begin
258 if Result_Last = Result'Last then
259 Double_Result_Size;
260 end if;
262 Result_Last := Result_Last + 1;
263 Result (Result_Last) := C;
264 end Append;
266 procedure Append (S : String) is
267 begin
268 while Result_Last + S'Length - 1 > Result'Last loop
269 Double_Result_Size;
270 end loop;
272 Result (Result_Last + 1 .. Result_Last + S'Length) := S;
273 Result_Last := Result_Last + S'Length;
274 end Append;
276 ------------------------
277 -- Double_Result_Size --
278 ------------------------
280 procedure Double_Result_Size is
281 New_Result : constant OS_Lib.String_Access :=
282 new String (1 .. 2 * Result'Last);
284 begin
285 New_Result (1 .. Result_Last) := Result (1 .. Result_Last);
286 OS_Lib.Free (Result);
287 Result := New_Result;
288 end Double_Result_Size;
290 -------------------
291 -- Is_Var_Prefix --
292 -------------------
294 function Is_Var_Prefix (C : Character) return Boolean is
295 begin
296 return (C = Environment_Variable_Char and then Mode = System_Default)
297 or else
298 (C = '$' and then (Mode = UNIX or else Mode = Both))
299 or else
300 (C = '%' and then (Mode = DOS or else Mode = Both));
301 end Is_Var_Prefix;
303 ----------
304 -- Read --
305 ----------
307 procedure Read (K : in out Positive) is
308 P : Character;
309 begin
310 For_All_Characters : loop
311 if Is_Var_Prefix (Path (K)) then
312 P := Path (K);
314 -- Could be a variable
316 if K < Path'Last then
318 if Path (K + 1) = P then
320 -- Not a variable after all, this is a double $ or %,
321 -- just insert one in the result string.
323 Append (P);
324 K := K + 1;
326 else
327 -- Let's parse the variable
329 Var (K);
330 end if;
332 else
333 -- We have an ending $ or % sign
335 Append (P);
336 end if;
338 else
339 -- This is a standard character, just add it to the result
341 Append (Path (K));
342 end if;
344 -- Skip to next character
346 K := K + 1;
348 exit For_All_Characters when K > Path'Last;
349 end loop For_All_Characters;
350 end Read;
352 ---------
353 -- Var --
354 ---------
356 procedure Var (K : in out Positive) is
357 P : constant Character := Path (K);
358 T : Character;
359 E : Positive;
361 begin
362 K := K + 1;
364 if P = '%' or else Path (K) = '{' then
366 -- Set terminator character
368 if P = '%' then
369 T := '%';
370 else
371 T := '}';
372 K := K + 1;
373 end if;
375 -- Look for terminator character, k point to the first character
376 -- for the variable name.
378 E := K;
380 loop
381 E := E + 1;
382 exit when Path (E) = T or else E = Path'Last;
383 end loop;
385 if Path (E) = T then
387 -- OK found, translate with environment value
389 declare
390 Env : OS_Lib.String_Access :=
391 OS_Lib.Getenv (Path (K .. E - 1));
393 begin
394 Append (Env.all);
395 OS_Lib.Free (Env);
396 end;
398 else
399 -- No terminator character, not a variable after all or a
400 -- syntax error, ignore it, insert string as-is.
402 Append (P); -- Add prefix character
404 if T = '}' then -- If we were looking for curly bracket
405 Append ('{'); -- terminator, add the curly bracket
406 end if;
408 Append (Path (K .. E));
409 end if;
411 else
412 -- The variable name is everything from current position to first
413 -- non letter/digit character.
415 E := K;
417 -- Check that first chartacter is a letter
419 if Characters.Handling.Is_Letter (Path (E)) then
420 E := E + 1;
422 Var_Name : loop
423 exit Var_Name when E > Path'Last;
425 if Characters.Handling.Is_Letter (Path (E))
426 or else Characters.Handling.Is_Digit (Path (E))
427 then
428 E := E + 1;
429 else
430 exit Var_Name;
431 end if;
432 end loop Var_Name;
434 E := E - 1;
436 declare
437 Env : OS_Lib.String_Access := OS_Lib.Getenv (Path (K .. E));
439 begin
440 Append (Env.all);
441 OS_Lib.Free (Env);
442 end;
444 else
445 -- This is not a variable after all
447 Append ('$');
448 Append (Path (E));
449 end if;
451 end if;
453 K := E;
454 end Var;
456 -- Start of processing for Expand_Path
458 begin
459 declare
460 K : Positive := Path'First;
462 begin
463 Read (K);
465 declare
466 Returned_Value : constant String := Result (1 .. Result_Last);
468 begin
469 OS_Lib.Free (Result);
470 return Returned_Value;
471 end;
472 end;
473 end Expand_Path;
475 --------------------
476 -- File_Extension --
477 --------------------
479 function File_Extension (Path : Path_Name) return String is
480 First : Natural :=
481 Strings.Fixed.Index
482 (Path, Dir_Seps, Going => Strings.Backward);
484 Dot : Natural;
486 begin
487 if First = 0 then
488 First := Path'First;
489 end if;
491 Dot := Strings.Fixed.Index (Path (First .. Path'Last),
492 ".",
493 Going => Strings.Backward);
495 if Dot = 0 or else Dot = Path'Last then
496 return "";
497 else
498 return Path (Dot .. Path'Last);
499 end if;
500 end File_Extension;
502 ---------------
503 -- File_Name --
504 ---------------
506 function File_Name (Path : Path_Name) return String is
507 begin
508 return Base_Name (Path);
509 end File_Name;
511 ---------------------
512 -- Format_Pathname --
513 ---------------------
515 function Format_Pathname
516 (Path : Path_Name;
517 Style : Path_Style := System_Default) return String
519 N_Path : String := Path;
520 K : Positive := N_Path'First;
521 Prev_Dirsep : Boolean := False;
523 begin
524 if Dir_Separator = '\'
525 and then Path'Length > 1
526 and then Path (K .. K + 1) = "\\"
527 then
528 if Style = UNIX then
529 N_Path (K .. K + 1) := "//";
530 end if;
532 K := K + 2;
533 end if;
535 for J in K .. Path'Last loop
536 if Strings.Maps.Is_In (Path (J), Dir_Seps) then
537 if not Prev_Dirsep then
538 case Style is
539 when UNIX => N_Path (K) := '/';
540 when DOS => N_Path (K) := '\';
541 when System_Default => N_Path (K) := Dir_Separator;
542 end case;
544 K := K + 1;
545 end if;
547 Prev_Dirsep := True;
549 else
550 N_Path (K) := Path (J);
551 K := K + 1;
552 Prev_Dirsep := False;
553 end if;
554 end loop;
556 return N_Path (N_Path'First .. K - 1);
557 end Format_Pathname;
559 ---------------------
560 -- Get_Current_Dir --
561 ---------------------
563 Max_Path : Integer;
564 pragma Import (C, Max_Path, "__gnat_max_path_len");
566 function Get_Current_Dir return Dir_Name_Str is
567 Current_Dir : String (1 .. Max_Path + 1);
568 Last : Natural;
570 begin
571 Get_Current_Dir (Current_Dir, Last);
572 return Current_Dir (1 .. Last);
573 end Get_Current_Dir;
575 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
576 Path_Len : Natural := Max_Path;
577 Buffer : String (Dir'First .. Dir'First + Max_Path + 1);
579 procedure Local_Get_Current_Dir
580 (Dir : System.Address;
581 Length : System.Address);
582 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
584 begin
585 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
587 if Dir'Length > Path_Len then
588 Last := Dir'First + Path_Len - 1;
589 else
590 Last := Dir'Last;
591 end if;
593 Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
594 end Get_Current_Dir;
596 -------------
597 -- Is_Open --
598 -------------
600 function Is_Open (Dir : Dir_Type) return Boolean is
601 begin
602 return Dir /= Null_Dir
603 and then System.Address (Dir.all) /= System.Null_Address;
604 end Is_Open;
606 --------------
607 -- Make_Dir --
608 --------------
610 procedure Make_Dir (Dir_Name : Dir_Name_Str) is
611 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
613 function mkdir (Dir_Name : String) return Integer;
614 pragma Import (C, mkdir, "__gnat_mkdir");
616 begin
617 if mkdir (C_Dir_Name) /= 0 then
618 raise Directory_Error;
619 end if;
620 end Make_Dir;
622 ----------
623 -- Open --
624 ----------
626 procedure Open
627 (Dir : out Dir_Type;
628 Dir_Name : Dir_Name_Str)
630 C_File_Name : constant String := Dir_Name & ASCII.NUL;
632 begin
633 Dir := new Dir_Type_Value'(Dir_Type_Value (opendir (C_File_Name)));
635 if not Is_Open (Dir) then
636 Free (Dir);
637 Dir := Null_Dir;
638 raise Directory_Error;
639 end if;
640 end Open;
642 ----------
643 -- Read --
644 ----------
646 procedure Read
647 (Dir : in out Dir_Type;
648 Str : out String;
649 Last : out Natural)
651 Filename_Addr : Address;
652 Filename_Len : Integer;
654 Buffer : array (0 .. Filename_Max + 12) of Character;
655 -- 12 is the size of the dirent structure (see dirent.h), without the
656 -- field for the filename.
658 function readdir_gnat
659 (Directory : System.Address;
660 Buffer : System.Address) return System.Address;
661 pragma Import (C, readdir_gnat, "__gnat_readdir");
663 function strlen (S : Address) return Integer;
664 pragma Import (C, strlen, "strlen");
666 begin
667 if not Is_Open (Dir) then
668 raise Directory_Error;
669 end if;
671 Filename_Addr :=
672 readdir_gnat (System.Address (Dir.all), Buffer'Address);
674 if Filename_Addr = System.Null_Address then
675 Last := 0;
676 return;
677 end if;
679 Filename_Len := strlen (Filename_Addr);
681 if Str'Length > Filename_Len then
682 Last := Str'First + Filename_Len - 1;
683 else
684 Last := Str'Last;
685 end if;
687 declare
688 subtype Path_String is String (1 .. Filename_Len);
689 type Path_String_Access is access Path_String;
691 function Address_To_Access is new
692 Unchecked_Conversion
693 (Source => Address,
694 Target => Path_String_Access);
696 Path_Access : constant Path_String_Access :=
697 Address_To_Access (Filename_Addr);
699 begin
700 for J in Str'First .. Last loop
701 Str (J) := Path_Access (J - Str'First + 1);
702 end loop;
703 end;
704 end Read;
706 -------------------------
707 -- Read_Is_Thread_Sage --
708 -------------------------
710 function Read_Is_Thread_Safe return Boolean is
712 function readdir_is_thread_safe return Integer;
713 pragma Import
714 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
716 begin
717 return (readdir_is_thread_safe /= 0);
718 end Read_Is_Thread_Safe;
720 ----------------
721 -- Remove_Dir --
722 ----------------
724 procedure Remove_Dir
725 (Dir_Name : Dir_Name_Str;
726 Recursive : Boolean := False)
728 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
729 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
730 Last : Integer;
731 Str : String (1 .. Filename_Max);
732 Success : Boolean;
733 Working_Dir : Dir_Type;
735 begin
736 -- Remove the directory only if it is empty
738 if not Recursive then
739 rmdir (C_Dir_Name);
741 if GNAT.OS_Lib.Is_Directory (Dir_Name) then
742 raise Directory_Error;
743 end if;
745 -- Remove directory and all files and directories that it may contain
747 else
748 Change_Dir (Dir_Name);
749 Open (Working_Dir, ".");
751 loop
752 Read (Working_Dir, Str, Last);
753 exit when Last = 0;
755 if GNAT.OS_Lib.Is_Directory (Str (1 .. Last)) then
756 if Str (1 .. Last) /= "." and then Str (1 .. Last) /= ".." then
757 Remove_Dir (Str (1 .. Last), True);
758 Remove_Dir (Str (1 .. Last));
759 end if;
761 else
762 GNAT.OS_Lib.Delete_File (Str (1 .. Last), Success);
764 if not Success then
765 Change_Dir (Current_Dir);
766 raise Directory_Error;
767 end if;
768 end if;
769 end loop;
771 Change_Dir (Current_Dir);
772 Close (Working_Dir);
773 Remove_Dir (Dir_Name);
774 end if;
775 end Remove_Dir;
777 end GNAT.Directory_Operations;