(*----------------------------------------------------------------------*)
(*              InitOvly --- initialize PibTerm overlays                *)
(*----------------------------------------------------------------------*)

PROCEDURE InitOvly;

(*----------------------------------------------------------------------*)
(*                                                                      *)
(*     PROCEDURE:  InitOvly                                             *)
(*                                                                      *)
(*     Purpose:    Initializes PibTerm directory for overlay searches   *)
(*                                                                      *)
(*     Calling Sequence:                                                *)
(*                                                                      *)
(*        InitOvly;                                                     *)
(*                                                                      *)
(*     Remarks:                                                         *)
(*                                                                      *)
(*        The PibTerm directory should have been set by a previous      *)
(*        SET PIBTERM=   DOS specification.                             *)
(*                                                                      *)
(*----------------------------------------------------------------------*)

VAR
   Ovr_Dir : AnyStr;
   Ierr    : INTEGER;

(*----------------------------------------------------------------------*)
(*      GetPibTermSpec --- get PibTerm directory from environment       *)
(*----------------------------------------------------------------------*)

FUNCTION GetPibTermSpec: AnyStr;

(*----------------------------------------------------------------------*)
(*                                                                      *)
(*     Function:   GetPibTermSpec                                       *)
(*                                                                      *)
(*     Purpose:    Get PibTerm directory from DOS enviroment area       *)
(*                                                                      *)
(*     Calling Sequence:                                                *)
(*                                                                      *)
(*        PibTermDir := GetPibTermSpec : AnyStr;                        *)
(*                                                                      *)
(*           PibTermDir --- the PIBTERM= string from the DOS enviroment *)
(*                          area, or the null string if not found.      *)
(*                                                                      *)
(*     Remarks:                                                         *)
(*                                                                      *)
(*        The PibTerm directory must have been set by a previous        *)
(*        SET PIBTERM=   DOS specification.                             *)
(*                                                                      *)
(*----------------------------------------------------------------------*)

TYPE
   DosEnv = ARRAY[0..32767] OF CHAR;

VAR
   EnvPtr: ^DosEnv;
   EnvStr: AnyStr;
   Done  : BOOLEAN;
   I     : INTEGER;

BEGIN (* GetPibTermSpec *)
                                   (* Get address of DOS environment area *)

   EnvPtr := Ptr( MemW[ CSeg:$002C ] , 0 );

                                   (* Begin loop looking for 'PIBTERM='   *)
   I              := 0;
   Done           := FALSE;
   EnvStr         := '';
   GetPibTermSpec := '';

   REPEAT
      IF EnvPtr^[I] = CHR( 0 ) THEN
         BEGIN
                                   (* Environment area ends with two      *)
                                   (* successive 0 bytes.                 *)

            IF ( EnvPtr^[I+1] = CHR( 0 ) ) THEN
               Done := TRUE;

                                   (* See if we have 'PIBTERM='. If so,   *)
                                   (* extract the directory information.  *)

            IF ( COPY( EnvStr , 1 , 8 ) = 'PIBTERM=' ) THEN
               BEGIN
                  GetPibTermSpec := COPY( EnvStr, 9, LENGTH( EnvStr ) - 8 );
                  Done           := TRUE;
               END;

                                   (* Set current environment string to null *)
            EnvStr := '';

         END
      ELSE
                                   (* Not CHR(0) --- append to current    *)
                                   (* environment string being extracted. *)

         EnvStr := EnvStr + EnvPtr^[I];

      I := I + 1;

   UNTIL Done;

END    (* GetPibTermSpec *);

(*----------------------------------------------------------------------*)

BEGIN (* InitOvly *)
                                   (* Search DOS environment for     *)
                                   (* PIBTERM= definition.           *)
   Ovr_Dir := GetPibTermSpec;
                                   (* See if environment string      *)
                                   (* makes sense.                   *)

   IF ( LENGTH( Ovr_Dir ) > 0 ) THEN
      IF ( NOT ( Ovr_Dir[1] IN ['A'..'Z','a'..'z'] ) ) THEN
         Ovr_Dir := '';

   IF ( LENGTH( Ovr_Dir ) > 0 ) THEN
      BEGIN
                                   (* PIBTERM= found in environment --- *)
                                   (* set home drive and directory      *)

         IF ( Ovr_Dir[2] = ':' ) THEN
            BEGIN
               Home_Drive    := UpCase( Ovr_Dir[1] );
               IF LENGTH( Ovr_Dir ) > 2 THEN
                  Home_Dir_Path := COPY( Ovr_Dir, 3, LENGTH( Ovr_Dir ) - 2 )
               ELSE
                  Home_Dir_Path := '';
            END
         ELSE
            BEGIN
               Home_Drive    := Dir_Get_Default_Drive;
               Home_Dir_Path := Ovr_Dir;
            END;

         IF ( LENGTH( Home_Dir_Path ) > 0 ) THEN
            IF ( Home_Dir_Path[ LENGTH( Home_Dir_Path ) ] = '\' ) THEN
               IF LENGTH( Home_Dir_Path ) > 1 THEN
                  Home_Dir_Path := COPY( Home_Dir_Path, 1,
                                         LENGTH( Home_Dir_Path ) - 1 )
               ELSE
                  Home_Dir_Path := '';

         IF ( LENGTH( Home_Dir_Path ) > 0 ) THEN
            IF ( Home_Dir_Path[ 1 ] = '\' ) THEN
               IF LENGTH( Home_Dir_Path ) > 1 THEN
                  Home_Dir_Path := COPY( Home_Dir_Path, 2,
                                         LENGTH( Home_Dir_Path ) - 1 )
               ELSE
                  Home_Dir_Path := '';

      END
   ELSE
      BEGIN
                                   (* No PIBTERM= in environment ---  *)
                                   (* get current drive and directory *)

         Home_Drive := Dir_Get_Default_Drive;

         Ierr := Dir_Get_Current_Path( Home_Drive, Home_Dir_Path );

      END;
                                   (* Make PibTerm files findable    *)

   IF ( Home_Dir_Path <> '')  THEN
      Home_Dir := Home_Drive + ':\' + Home_Dir_Path + '\'
   ELSE
      Home_Dir := Home_Drive + ':';

   OvrPath( Home_Drive + ':\' + Home_Dir_Path );

END    (* InitOvly *);