This file is indexed.

/usr/share/ada/adainclude/texttools/os.adb is in libtexttools5-dev 2.1.0-8.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
------------------------------------------------------------------------------
-- OS (package body)                                                        --
--                                                                          --
-- Part of TextTools                                                        --
-- Designed and Programmed by Ken O. Burtch                                 --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
--                 Copyright (C) 1999-2007 Ken O. Burtch                    --
--                                                                          --
-- This 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.  This is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- 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 this;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
-- This is maintained at http://www.pegasoft.ca/tt.html                     --
--                                                                          --
------------------------------------------------------------------------------

with Interfaces.C; use Interfaces.C;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Directories;
with Ada.Calendar.Formatting;
with Ada.Environment_Variables;
with Ada.Strings.Fixed;

--  Implement some facilities not yet in GNAT.
--  with Ada.Strings.Fixed.Hash_Case_Insensitive;
with Hash_Case_Insensitive;
--  with Ada.Strings.Fixed.Equal_Case_Insensitive;
with Equal_Case_Insensitive;

package body os is

LockPath : constant string := "/home/ken/";
SessionLogPath : Unbounded_String;

ttyname : Unbounded_String;
IsLocaltty : boolean;  -- true if not client/server

---> C Interface
--
-- These are POSIX system calls.

function System (Command : in char_array) return int;
pragma Import( C, System, "system");

-- These are all from C_code/system.c.

--  procedure CSync;
--  pragma Import( C, CSync, "CSync" );

function CRunIt (Cmd, Outfile, Parm1, Parm2, Parm3 : in char_array) return int;
pragma Import( C, CRunIt, "CRunIt" );

--->
--
-- Lintel Pathnames

package PathList is new Ada.Containers.Indefinite_Hashed_Maps
  (Key_Type => String,
   Element_Type => String,
   Hash => Hash_Case_Insensitive,
   Equivalent_Keys => Equal_Case_Insensitive);
Paths : PathList.Map;

---> Housekeeping

procedure StartupOS is
  WasRaising : boolean;
  
  procedure InitializeSessionLog is
     use Ada.Directories;
  begin
     if Exists (Containing_Directory (To_String (Sessionlogpath))) then
        declare
           use Ada.Text_IO;
           File : File_Type;
        begin
           Create (File, Out_File, To_String (Sessionlogpath));
           Close (File);
           pragma Unreferenced (File);
        end;
        SessionLog( "StartupOS: New " & To_String (ProgramName) & " session log started" );
        SessionLog( "StartupOS: " & Ada.Calendar.Formatting.Image (Ada.Calendar.Clock));
        if LastError /= TT_OK then
           Ada.Text_IO.Put_Line ( "StartupOS: Unable to write to session_log; error "
                                    & AnErrorCode'Image (LastError));
        end if;
     end if;
  end InitializeSessionLog;

  procedure CheckOSServices is
  begin
    null;
    --MakeTempFilename( TempFile );
    --UNIX( ToString( "zoo a " & TempFile & " /etc/passwd > /dev/null" ) );
    --if LastError /= CoreOK then -- wierdness when I used if UNIX(zoo)...
    --   SessionLog( "StartupOS: Unable to find zoo command", LastError );
    --end if;
    --Erase( TempFile & ".zoo" );
  end CheckOSServices;

  use Ada.Environment_Variables;
begin
  NoError;
  WasRaising := TrapErrors;

  if Exists ("TMPDIR") then
     PathAlias ("tmp", Value ("TMPDIR"));
  else
     PathAlias ("tmp", "/tmp");
  end if;

  if Exists ("HOME") then
     PathAlias ("home", Value ("HOME"));
     -- Make the $SYS alias by adding ShortProgramName to $HOME
     PathAlias ("sys", Value ("HOME") & "/" & To_String (ShortProgramName));
     SessionLogPath := To_Unbounded_String (ExpandPath ("$SYS/session_log"));
  else
     SessionLogPath := Null_Unbounded_String;
  end if;

  InitializeSessionLog;
  if LastError = TT_OK then
     ttyname := To_Unbounded_String (UNIX( "tty" ));
     IsLocaltty := (ttyname >= "/dev/tty1" and
         ttyname <= "/dev/tty9" ) and Length (ttyname ) = 9;
     if LastError = TT_OK then
        CheckOSServices;
     end if;
     if LastError /= TT_OK then
        LastError := TT_OSService;
        null; --Put_Line( Standard_Error, "StartupOS: See session log for error details" );
     end if;
  end if;

  RestoreRaising( WasRaising );
  -- load system parameters
  -- load user defaults
end StartupOS;

procedure IdleOS( IdlePeriod : in Duration ) is
   pragma Unreferenced (Idleperiod);
begin
  NoError;
  --UNIX( "sync" ); -- should call sync() by C
  --should really check and remove an old file from the .Trash
  --directory, once per call
end IdleOS;

procedure ShutdownOS is
begin
  NoError;
  SessionLog( "ShutdownOS: End of session log" );
  PathList.Clear( Paths );
end ShutdownOS;


---> OS Interfacing

function UNIX (s : in String) return boolean is
   Res : constant Int := System (To_C (S));
begin
   NoError;
   if res /= 0 then -- DEBUG
      SessionLog( "Call to system(""" & s & """) returned error code" & Int'Image (Res));
   end if;
   return res = 0;
end UNIX;

procedure UNIX( s : in String ) is
   Command : constant Char_Array := To_C (S);
begin
   NoError;
   if System (Command) /= 0 then
      Error( TT_SystemError );
   end if;
end UNIX;

function UNIX ( s : string ) return String is
   use Ada.Text_IO;
   File : File_Type;
   Output : Unbounded_String := Null_Unbounded_String;
   Res : Int;
begin
   NoError;
   Create (File, In_File, Name => "");	--  temp file
   Res := System (To_C (S & " > " & Name (File)));
   if Res /= 0 then
      SessionLog ("Call to system(""" & S & """) returned error code" & Int'Image (Res));
      Error( TT_SystemError );
   else
      while not End_Of_File (File) loop
	 Append (Output, Get_Line (File));
      end loop;
      Delete (File);
   end if;
   return To_String (Output);
end UNIX;

procedure RunIt( cmd : string;
                 parm1, parm2, parm3 : string := "";
                 Results : out StrList.Vector) is
   use Ada.Text_IO;
   File : File_Type;
   Status : Int;
begin
   NoError;
   Create (File, In_File, Name => "");	--  temp file
   Status := CRunIt (To_C (Cmd), To_C (Name (File)), To_C (Parm1), To_C (Parm2), To_C (Parm3));
   if Status = 0 then
      LoadList (File, Results );
      Error( TT_OK );
   else
      Error( TT_SystemError );
   end if;
   Delete (File);
   pragma Unreferenced (File);
end RunIt;

function NotEmpty (S : in APathName ) return boolean is
   P : constant String := Expandpath (S);
   use Ada.Directories;
begin
   NoError;
   return Exists (P) and then Size (P) /= 0;
end NotEmpty;

function IsDirectory (S : in APathName ) return boolean is
   P : constant String := Expandpath (S);
   use Ada.Directories;
begin
   NoError;
   return Exists (P) and then Kind (P) = Directory;
end Isdirectory;

function IsFile (S : in APathName ) return boolean is
begin
  NoError;
  return Ada.Directories.Exists (ExpandPath (S));
end IsFile;

function Lock (File : in APathName) return Boolean is
   --  This is equivalent to the previous version, but arguably
   --  efficient since another instance is allowed to take the
   --  lock...
   use Ada.Text_IO;
   File_Handler : File_Type;
begin
   Create (File_Handler, Out_File, LockPath & ExpandPath (File));
   Close (File_Handler);
   return True;
exception
   when others =>
      return False;
end Lock;

procedure Unlock (File : in APathName ) is
begin
   Ada.Directories.Delete_File (LockPath & Expandpath (File));
end Unlock;

procedure ValidateFilename (fs : in AFileSystem;
			    oldfn : in APathname;
			    newfn : out Unbounded_String;
			    errmsg : out Unbounded_String) is
   Changed : Boolean := False;
   
   procedure ValidateUNIX is -- hastily assembled
      ch : character;
   begin
      -- length OK
      -- leading character: no special requirements
      Newfn := Null_Unbounded_String;
      for i in Oldfn'Range loop
	 Ch := Oldfn (I);
	 if Ch < ' ' then -- control character?
	    ch := '_';
	    Changed := true;
	 elsif ch > '~' then -- control character?
	    ch := '_';
	    Changed := true;
	    --elsif ch <= 'A' then -- special character?
	    --    ch := '_';
	    --    Changed := true;
	    --elsif ch = '~' then
	    --    ch := '-';
	    --    Changed := true;
	 end if;
	 Append (Newfn, Ch);
      end loop;
      if Changed then
	 ErrMsg := To_Unbounded_String ( "bad characters for UNIX filesystem" );
    end if;
  end ValidateUNIX;

  procedure ValidateUNIX14 is
  begin
     if Oldfn'Length > 14 then
	newfn := To_Unbounded_String (Ada.Strings.Fixed.Head (oldfn, 14 ));
	Changed := true;
	ErrMsg := To_Unbounded_String ("too many characters for old UNIX filesystem" );
     else
	ValidateUNIX;
     end if;
  end ValidateUNIX14;

  procedure ValidateDOS is
    ch : character;
  begin
     Newfn := Null_Unbounded_String;
     for i in Oldfn'Range loop
	ch := Oldfn (i);
	if ch <= ' ' then
	   ch := '_';
	   Changed := true;
	end if;
	Append (Newfn, Ch);
     end loop;
     if Changed then
	ErrMsg := To_Unbounded_String ( "bad characters for DOS" );
       end if;
  end ValidateDOS;

  procedure ValidateOS2 is
  begin
    ValidateUNIX; -- at least, for now
  end ValidateOS2;

begin
  ErrMsg := Null_Unbounded_String;
  if Oldfn'Length = 0 then
     Newfn := To_Unbounded_String ("untitled");
     ErrMsg := To_Unbounded_String ("Empty file name.");
  else
     case fs is
	when UNIXFS => ValidateUNIX;
	when UNIX14FS => ValidateUNIX14;
	when DOSFS => ValidateDOS;
	when OS2FS => ValidateOS2;
	when NONE => null;
     end case;
  end if;
end ValidateFilename;

procedure ValidatePathname
  ( fs : in AFileSystem;
    oldfn : in APathname;
    Newfn : out unbounded_string;
    errmsg : out unbounded_string) is
   SepChar : character;
   SepPos1 : integer;
   SepPos2 : integer;
   CorrectedFile : Unbounded_String := null_unbounded_string;
   thefs : AFileSystem;
   LastErrMsg : Unbounded_String := Null_Unbounded_String;
begin
   newfn := Null_Unbounded_String;
   ErrMsg := Null_Unbounded_String;
   case fs is
      when UNIXFS =>   SepChar := '/';
	 thefs := UnixFS;
      when UNIX14FS => SepChar := '/';
	 thefs := Unix14FS;
      when DOSFS =>    SepChar := '\';
	 thefs := DosFS;
      when OS2FS =>    SepChar := ':'; -- Is this right for OS/2?
	 thefs := OS2FS;
      when NONE => -- guess at separator
	 if Ada.Strings.Fixed.Index( oldfn, "/" ) > 0 then
	    SepChar := '/';
	    thefs := UnixFS;
	 elsif Ada.Strings.Fixed.Index( oldfn, "\" ) > 0 then
	    SepChar := '\';
	    thefs := DosFS;
	 elsif Ada.Strings.Fixed.Index( oldfn, ":" ) > 0 then
	    SepChar := ':';
	    thefs := OS2FS;
	 else
	    SepChar := '/'; -- guess UNIX by default
	    thefs := UnixFS;
	 end if;
   end case;
   SepPos1 := Ada.Strings.Fixed.Index( oldfn, (1 => SepChar));
   if SepPos1 = 0 then
      ValidateFilename( fs, oldfn, newfn, Errmsg );
   else
      loop
	 SepPos2 := Oldfn'length;
	 for i in SepPos1+1..Oldfn'Last loop
	    if Oldfn (i) = SepChar then
	       SepPos2 := i;
	       exit;
	    end if;
	 end loop;
	 ValidateFilename ( thefs, Oldfn (SepPos1 + 1 .. SepPos2 - 1), CorrectedFile, Lasterrmsg );
	 if length( CorrectedFile ) /= 0 then
	    Append (Newfn, SepChar & CorrectedFile);
	    if length( LastErrMsg ) > 0 then
	       ErrMsg := LastErrMsg;
	    end if;
	 else
	    Append (Newfn, SepChar & Oldfn (SepPos1 + 1 .. SepPos2 - 1));
	 end if;
	 exit when SepPos2 = Oldfn'Last;
	 SepPos1 := SepPos2;
      end loop;
      if length( ErrMsg ) = 0 then -- no errors? no changes
	 newfn := Null_Unbounded_String;
      end if;
   end if;
end ValidatePathname;

procedure Erase( File : APathName ) is
   use Ada.Directories;
begin
   NoError;
   Delete_File (Expandpath (File));
exception
   when Name_Error | Use_Error =>
      Error (TT_FileAccess);
end Erase;

procedure Trash( file : APathName ) is
   -- remove a file to the trash can, erasing if necessary
   WasRaising : boolean;
begin
  --NoError called in UNIX
   WasRaising := RaisingErrors;
   TrapErrors;
   UNIX ("mv " & Expandpath (File) & " $HOME/.Trash 2> /dev/null");
   if WasRaising then
      RaiseErrors;
   end if;
   if LastError /= TT_OK then
      Erase( file );
   end if;
end Trash;

procedure EmptyTrash is
begin
  --NoError called in UNIX
  UNIX ("find $HOME/.Trash -type f -mtime +3 -exec rm {} \;" );
end EmptyTrash;

procedure Move( file1, file2 : APathName ) is
begin
   --NoError called in UNIX
   UNIX ("mv " & ExpandPath (File1) & " " & ExpandPath (File2) & " 2> /dev/null");
end Move;

function Shrink( file : APathName ) return APathName is
   Path : constant String := Expandpath (File);
begin
  --NoError called in UNIX
  --shrinkstr := To255( "zoo aPq " );
   UNIX ("gzip " & Path & " " & Path);
   --  if LastError = 0 then
   --     Erase (Path & ".bak");
   --  end if;
   if LastError = 0 then
      --return Append( path, ".zoo" );
      return Path & ".gz";
   else
      return "";
   end if;
end Shrink;

function Expand( file : APathName ) return APathName is
   Path : constant String := Expandpath (File);
begin
  --NoError called in UNIX
  --expandstr := To255( "zoo x//qO " );
  UNIX ("gunzip " & Path);
  if LastError = 0 then
     pragma Assert (Ada.Strings.Fixed.Tail (Path, 3) = ".gz");
     return Ada.Strings.Fixed.Head (Path, Path'Length - 3);
  else
     return "";
  end if;
end Expand;

procedure Archive( arch, file : APathName ) is
   -- note possibility of overflow here!
   ArchPath : constant String := ExpandPath (Arch);
   FilePath : constant String := ExpandPath (File);
begin
   --NoError called in UNIX
   if Ada.Strings.Fixed.Tail (Archpath, 4) /= ".tgz"
     or NotEmpty (Filepath) then
      -- should really be not exists
      Error( TT_FileExistance );
   else
      --Cmd := To255( "zoo aunqP " ) & ArchPath & To255(" " ) & FilePath;
      UNIX ("tar cfz " & ArchPath & " " & FilePath);
   end if;
end Archive;

procedure Extract( arch, file : APathName ) is
   ArchPath : constant String := ExpandPath (Arch);
   FilePath : constant String := ExpandPath (File);
begin
   -- NoError called in UNIX
   if NotEmpty (Archpath) then
      -- should really be not exists
      Error( TT_FileExistance );
      return;
   end if;
   --Cmd := ( To255( "zoo xqO ") & ArchPath & To255(" ") ) & ( FilePath
   --    & " > /dev/null" );
   UNIX ("tar xfz " & ArchPath & " " & FilePath & " > /dev/null");
end Extract;

procedure Usage (file : in APathName;
		 me : in AFileUsage := Normal;
		 us : in AFileUsage := ReadOnly;
		 everyone : in AFileUsage := ReadOnly ) is
   Octal : constant array (Afileusage) of Character
     := (ReadOnly => '4',
	 Normal => '6',
	 Run => '7',
	 None => '0');
begin
   --NoError called in UNIX
   UNIX ("chmod " & Octal (Me) & Octal (Us) & Octal (Everyone) & " " & ExpandPath (file));
end Usage;

procedure BeginSession is
begin
  null;
end BeginSession;

procedure EndSession is
begin
  null; -- sync
end EndSession;


---> Directory Utilities

function SpaceUsed( dir : APathName ) return integer is
begin
   --NoError called in UNIX
   return Integer'Value (UNIX ("du -fs " & ExpandPath (dir)));
end SpaceUsed;


---> Device Utilities

--  function SpaceFree( dev : APathName ) return long_integer is
--     pragma Unreferenced(Dev);
--  begin
--    return 1; -- NYI
--  end SpaceFree;

--  function TotalSpace( dev : APathName ) return long_integer is
--     pragma Unreferenced(Dev);
--  begin
--    return 1;
--  end TotalSpace;

--  function EntriesFree( dev : APathName ) return long_integer is
--     pragma Unreferenced(Dev);
--  begin
--    return 1;
--  end EntriesFree;

--  function TotalEntries( dev : APathname ) return long_integer is
--     pragma Unreferenced(Dev);
--  begin
--    return 1;
--  end TotalEntries;

--  function OnDevice( path : APathName ) return APathname is
--     pragma Unreferenced(Path);
--  begin
--    return NullStr255;
--  end OnDevice;


--->

function GetFreeClusterHost return string is
begin
  return UNIX( "uname -n" );
end GetFreeClusterHost;


---> Str255Lists

procedure Loadlist (File : in Ada.Text_IO.File_Type;
		    StringList : out Strlist.Vector) is
   use Ada.Text_IO;
begin
   Stringlist.Clear;
   while not End_Of_File (File) loop
      StringList.Append (Get_Line (File));
   end loop;
exception
   when Storage_Error =>
      StringList.Clear;
      Error( TT_LowMemory );
end Loadlist;

procedure Loadlist (Path : in String;
		    StringList : out StrList.Vector) is
   -- load a string list from a file
   use Ada.Text_IO;
   File : File_Type;
begin
   begin
      Open (File, In_File, Expandpath (Path));
   exception
      when Status_Error =>
	 Error( TT_FileLocking );
	 return;
      when Name_Error =>
	 Error( TT_FileExistance );
	 return;
   end;
   Loadlist (File, Stringlist);
   Close (File);
exception
   when others =>
      if Is_Open (File) then
	 Close (File);
      end if;
end LoadList;

procedure savelist (File : in Ada.Text_IO.File_Type;
		    StringList : in Strlist.Vector) is
   use Ada.Text_IO;
   procedure Process (Position : in Strlist.Cursor);
   procedure Process (Position : in Strlist.Cursor) is
   begin
      Put_Line (File, Strlist.Element (Position));
   end Process;
begin
   StringList.Iterate (Process'Access);
end Savelist;

procedure SaveList (Path : in APathName;
		    StringList : in StrList.Vector ) is
   -- save a string list to a file
   use Ada.Text_IO;
   File : file_type;
begin
   begin
      Create (File, Out_File, ExpandPath (Path));
   exception
      when Status_Error =>
	 Error( TT_FileLocking );
	 return;
      when Name_Error =>
	 Error( TT_FileExistance );
	 return;
   end;
   Savelist (File, Stringlist);
   Close (File);
exception -- translate Text_IO errors into core errors
   when others =>
      if Is_Open (File) then
	 Close (File);
      end if;
end SaveList;

function IsLocal return boolean is
begin
  return IsLocaltty;
end IsLocal;

procedure SetPath( s : APathName ) is
   use Ada.Directories;
   Path : constant String := Expandpath (S);
begin
   Set_Directory (Path);
exception
   when Name_Error | Use_Error =>
      SessionLog( "SetPath: can't change path to " & Path);
      Ada.Text_IO.Put_Line( Ada.Text_IO.Standard_Error, "SetPath: TT_SystemError -- can't change path" );
      Error( TT_SystemError );
end SetPath;

procedure PathAlias (Alias : in String;
                     Path  : in APathName) is
begin
  NoError;
  Paths.Include (Alias, Path);
end PathAlias;

function ExpandPath (Path : in APathName )
                    return APathName is
   -- check for leading number and convert to a path
   use Pathlist;
   posn : Natural;
   Position : Cursor;
begin
   if Path'Length = 0 or else Path (Path'First) /= '$' then
      return Path;
   end if;
   Posn := Ada.Strings.Fixed.Index
     (Source => Path (Path'First + 1 .. Path'Last),
      Pattern => (1 => '/'));
   if Posn = 0 then
      Posn := Path'Last + 1;
   end if;
   Position := Paths.Find (Path (Path'First + 1 .. Posn - 1));
   if Position = No_Element then
      return Path (Posn .. Path'Last);
   end if;
   return Element (Position) & Path (Posn .. Path'Last);
end ExpandPath;

procedure SplitPath (path : in String;
		     dir : out Unbounded_String;
		     file : out unbounded_string) is
   -- split path into directory and file
   use Ada.Directories;
begin
   Dir := To_Unbounded_String (Containing_Directory (Path));
   File := To_Unbounded_String (Simple_Name (Path));
end SplitPath;

procedure DecomposePath( path : in APathname;
			 PathType : out APathType;
			 Host : out unbounded_string;
			 Filepath : out unbounded_string) is
   SlashPos : Natural;
begin
   FilePath := To_Unbounded_String (ExpandPath (Path));
   
   --
   -- Check for a standard path--process and bail out if is one
   --
   Slashpos := Index (FilePath, "://");
   if slashPos = 0 then       -- no ://?
      PathType := File;
      Host := To_Unbounded_String ("localhost"); --  then it's this machine
      return;
   end if;
   --
   -- Must be a URL.
   -- Check for the leading transfer method in the URL.
   --
   Pathtype := Unknown;
   for Kind in Apathtype'Succ (Unknown) .. Apathtype'Last loop
      if Equal_Case_Insensitive (To_String (Head (FilePath, Slashpos + 2)),
				 Apathtype'Image (Kind) & "://") then
	 Tail (Filepath, Length (Filepath) - Slashpos - 2);
	 PathType := Kind;
	 exit;
      end if;
   end loop;
   if Pathtype = Unknown then
      Delete (Filepath, 1, SlashPos + 2);     -- scrap unknown URL prefix
   end if;                                     -- and try to process anyway
   --
   -- all URL's are host and optional path
   --
   if Element( Filepath, length( Filepath ) ) = '/' then -- ending slash?
      Head (Filepath, Length (Filepath) - 1); -- delete it
   end if;
   Ada.text_io.put_line( "checking " & To_String( Filepath ) );
   SlashPos := Index( Filepath, "/" );      -- where's the next slash?
   if SlashPos > 0 then                     -- if there is one
      Ada.text_io.put_line("has path" );
      Host := Head( Filepath, SlashPos-1 ); --   the address is before it
      Ada.text_io.put_line("path = " & To_String( Filepath ) );
      Delete( Filepath, 1, SlashPos );      --   removing it and slash
      Ada.text_io.put_line("after host removal, path = " & To_String( Filepath ) );
  else                                     -- but if there isn't a slash
     Ada.text_io.put_line("has no path" );
     Host := Filepath;                     --   then it's just the address
     FilePath := Null_Unbounded_String;    --   with no path
  end if;
end DecomposePath;

---> Append for Text File

procedure AddFile( file, text : in String ) is
   use Ada.Text_IO;
   F : File_Type;
begin
   if Ada.Directories.Exists (File) then
      Open (F, Append_File, File);
      Put_Line (F, Text);
      Close (F);
   end if;
exception
   when others =>
      if Is_Open (F) then
	 Close (F);
      end if;
      raise;
end AddFile;

---> Logging

procedure SessionLog (Message : in String) is
   WasRaising : boolean;
begin
   NoError;
   WasRaising := TrapErrors;
   Addfile (To_String (Sessionlogpath), Message);
   RestoreRaising( WasRaising );
end SessionLog;

procedure SessionLog (Message : in String;
		      ErrorCode : in AnErrorCode ) is
begin
   --NoError implied
   SessionLog (Message & " (Error Code" &
		 AnErrorCode'Image (ErrorCode) & ")" );
end SessionLog;

end os;