home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / bix / checkenv.pas < prev    next >
Pascal/Delphi Source File  |  1986-11-01  |  6KB  |  194 lines

  1. {TITLE: MODIFICATION OF THE ORIGINAL DOS ENVIRONMENT:}
  2. program checkenv;
  3. (*********************************************************************)
  4. {  Include module ENVIRON.INC ---  Modification of the Original DOS
  5.    Environment with Error Trapping (same as DOS SET command).
  6.  
  7.    Return Codes for Function AddToOrig:
  8.       0 : Normal Completion; String Was Added, Replaced or Deleted.
  9.       1 : Invalid Environment String.  Follows same rules as DOS SET Command.
  10.       2 : Insufficent Space in Environment.
  11.       3 : String Not Found in Environment.
  12.  
  13.    This code assumes that the DOS Critical Error Handler has not been
  14.    reset; in most cases, this is true.  If it has been modified, the
  15.    Environment used will be that of the Critical Error Handler.
  16.  
  17.    John Leonard  9/11/1986
  18. }
  19.  
  20. Type
  21.    EnvironmentType=Array [0..32767] Of Char;
  22.    EnvironPtr = ^EnvironmentType;
  23.    EnvironStr = String[255];
  24.  
  25. const
  26.    envseg : integer = 0;
  27.    envoff : integer = 0;
  28.    envsiz : integer = 0;
  29.  
  30. {$V-}
  31.  
  32. function EnvUpcaseStr(S : EnvironStr) : EnvironStr;
  33.    var P : Integer;
  34.    begin
  35.       for P := 1 to Length(S) do S[P] := UpCase(S[P]);
  36.       EnvUpcaseStr := S;
  37.    end;
  38.  
  39. procedure GetOrigEnvInfo;
  40.    begin
  41.       {The Segment of the Original Environment is the same
  42.        Segment as the critical error handler.  The critical error
  43.        handler vector is stored at offset of the program PSP.}
  44.  
  45.       EnvSeg := memw[cseg:$14];
  46.  
  47.       {The Offset of the Original Environment is stored at offset
  48.        $2C of the PSP of the critical error handler.}
  49.  
  50.       EnvOff := memw[EnvSeg:$2C];
  51.       if EnvOff = 0 then
  52.          EnvSeg  := pred(EnvSeg) + memw[pred(EnvSeg):$3] + 2;
  53.  
  54.       { The Size of the Environment is computed here. }
  55.  
  56.       EnvSiz :=  memw[pred(EnvSeg):$3] shl 4;
  57.    end;
  58.  
  59.  
  60. Function GetOldStr(SearchString: EnvironStr): EnvironStr;
  61.    Type
  62.       Env=EnvironmentType;
  63.    Var
  64.       EPtr: ^EnvironmentType;
  65.       EStr:  EnvironStr;
  66.       Done: Boolean;
  67.       I: Integer;
  68.    Begin
  69.       getorigenvinfo;
  70.       GetOldStr:='';
  71.       If SearchString<>'' Then Begin
  72.          EPtr:=Ptr(EnvSeg,0);
  73.          I:=0;
  74.          SearchString:=SearchString+'=';
  75.          Done:=False;
  76.          EStr:='';
  77.          Repeat
  78.             If EPtr^[I]=#0 Then Begin
  79.                If EPtr^[Succ(I)]=#0 Then Begin
  80.                   Done:=True;
  81.                   If SearchString='==' Then Begin
  82.                      EStr:='';
  83.                      I:=I+4;
  84.                      While EPtr^[I]<>#0 Do Begin
  85.                         EStr:=EStr+EPtr^[I];
  86.                         I:=Succ(I);
  87.                      End;
  88.                      GetOldStr:=EStr;
  89.                   End;
  90.                End;
  91.                If Copy(EStr,1,Length(SearchString))=SearchString Then Begin
  92.                   GetOldStr:=Copy(EStr,Succ(Length(SearchString)),255);
  93.                   Done:=True;
  94.                End;
  95.                EStr:='';
  96.             End
  97.             Else EStr:=EStr+EPtr^[I];
  98.             I:=Succ(I);
  99.          Until Done;
  100.       End;
  101.    End;
  102.  
  103.  
  104. Function AddToOrig(AddString: EnvironStr): integer;
  105.    Type
  106.       Env=EnvironmentType;
  107.    Var
  108.       EPtr: ^EnvironmentType;
  109.       EStr,name : EnvironStr;
  110.       kill,Done: Boolean;
  111.       I,istart,j: Integer;
  112.    Begin
  113.       getorigenvinfo;
  114.       if AddString = '' then begin
  115.          AddToOrig := 1;            { Return 1: Invalid String }
  116.          exit;
  117.       end;
  118.       i := pos('=',AddString);
  119.       if i = 0 then begin
  120.          AddToOrig := 1;
  121.          exit;
  122.       end;
  123.       name := EnvUpCaseStr( copy(AddString,1,pred(i)) );
  124.       delete(AddString,1,pred(i));
  125.       writeln;
  126.       if addstring = '=' then kill := true else kill := false;
  127.       AddString := name + addstring + #0;
  128.       If name<>'' Then Begin
  129.          EPtr:=Ptr(EnvSeg,0);
  130.          I:=0;
  131.          Done:=False;
  132.          EStr:='';
  133.          IStart := 0;
  134.          Repeat
  135.             If EPtr^[I]=#0 Then Begin
  136.                If Copy(EStr,1,ord(name[0]))=name Then begin
  137.                   move(EPtr^[succ(i)],EPtr^[succ(istart)],EnvSiz-i);
  138.                   if kill then begin
  139.                      AddtoOrig := 0;
  140.                      exit;
  141.                   end;
  142.                end;
  143.                If EPtr^[Succ(I)]=#0 Then Begin
  144.                   Done:=True;
  145.                   if kill then begin
  146.                      AddToOrig := 3;
  147.                      exit;
  148.                   end;
  149.                   if (ord(addstring[0])) > (EnvSiz-i+2) then begin
  150.                      AddtoOrig := 2;
  151.                      exit;
  152.                   end;
  153.                   addstring := addstring + #0;
  154.                   move(addstring[1],EPtr^[succ(i)],ord(addstring[0]));
  155.                   AddtoOrig := 0;
  156.                end;
  157.                EStr:='';
  158.                Istart := I;
  159.             end
  160.             Else EStr:=EStr+EPtr^[I];
  161.             I:=Succ(I);
  162.          Until Done;
  163.       End;
  164.    End;
  165. {$V+}
  166. (*********************************************************************)
  167. (*****               End of Include Module ENVIRON.INC           *****)
  168.  
  169.  
  170. (*** Example Program ***)
  171.  
  172. var i:integer;
  173.     Seg,Off,Size:integer;
  174.     ErrorMsg,SetString : string[100];
  175. begin
  176.    lowvideo;clrscr;
  177.    getorigenvinfo;
  178.    writeln('Size of Original DOS Environment: ',EnvSiz,' Bytes.');
  179.    writeln;
  180.    writeln('Enter Set String (Follow same rules as DOS SET. Leave off SET):');
  181.    write('---> ');normvideo;
  182.    readln(SetString);
  183.    writeln;lowvideo;
  184.    i := AddToOrig(SetString);
  185.    case i of
  186.       0 : errormsg := 'Environment Modified.  Use SET to examine.';
  187.       1 : errormsg := 'Invalid SET String.  Refer to DOS manual.';
  188.       2 : ErrorMsg := 'Not Enough Room in Environment.  Sorry.';
  189.       3 : ErrorMsg := 'String Not Found in Environment.';
  190.    end;
  191.    writeln(errormsg);
  192.    writeln;
  193. end.
  194.