Google
 

Trailing-Edge - PDP-10 Archives - decuslib10-05 - 43,50337/21/dired.sim
There is 1 other file named dired.sim in the archive. Click here to see a list.
BEGIN
  EXTERNAL TEXT PROCEDURE rest,upcase,conc,from,upto,scanto;
  EXTERNAL BOOLEAN PROCEDURE change;
  EXTERNAL LONG REAL PROCEDURE scanreal;
  EXTERNAL INTEGER PROCEDURE search,scanint,checkint;
  EXTERNAL CLASS safmin;		! Use SIMMIN for Simulations;
  safmin BEGIN
    REF (Directfile) d;
    INTEGER k,m,n,min,max,opcode,rlength;
    CHARACTER c,nullc;
    TEXT t,u,editstring,comcodes,nulltext;

    SWITCH command:=fin,edit,edit,list,list,replace,replace,delete;

    PROCEDURE stringrequest(t1,t3); NAME t3; VALUE t1;
    TEXT t1,t3;
    BEGIN TEXT t,u,lastc;
      request(t1,"",textinput(t,TRUE),"",
      help("Give new line. If last char is & continuation is expected."));
      IF t =/= NOTEXT THEN
      BEGIN
	lastc:-t.Sub(t.Length,1); WHILE lastc = "&" DO
	BEGIN
	  t:-t.Sub(1,t.Length-1); u:-conc(u,t);
	  request(">","",textinput(t,TRUE),"",
	  help("Type continuation line !"));
	  lastc:-IF t == NOTEXT THEN NOTEXT ELSE t.Sub(t.Length,1);
	END;
	u:-conc(u,t);
	t3:-u;
      END ELSE t3:-t;
    END stringrequest;

    REF (Directfile) PROCEDURE opendf;
    BEGIN TEXT t,u; REF (Directfile) d;
      request("Give name of direct file: ","x.tmp",textinput(
      t,TRUE),"?",
      help("Name.extension for direct file to be edited"));
      request("image size: ","68",textinput(u,Digit(u.Getchar)),"?",
      help("Record length (=buffer size) for file edited."));
      d:-NEW Directfile(conc(t,conc(Copy("/imagesize:"),u)));
      rlength:=u.Getint;
      d.Open(Blanks(rlength));
      opendf:-d;
    END of opendf;

    BOOLEAN PROCEDURE dispedit;
    BEGIN
      outline(
      "Give   /searchstring/newstring/  where  /   can be replaced by any");
      outline("character not contained in the strings");
      outline("with EDIT only the first occurence on each line is replaced");
      outline("with CHANGE all occurences are replaced");
    END;

    BOOLEAN PROCEDURE dispcom;
    BEGIN
      outline("Valid commands:  c  e  p  l  r  i  d    followed by ");
      outline("either a single number n  or a pair of numbers  n-m, ");
      outline("connected with a minus sign (no space before m !)");
      outline("only one character commands allowed, they stand for:");
      outline(" PRINT(=LIST),REPLACE(=INSERT),CHANGE,EDIT and DELETE");
      outline("For CHANGE and EDIT, the user is further prompted");
      outline("to give   /old string/new string/");
      outline("the strings must be of equal length");
      outline("type control z  end or stop for exit from editor");
    END;

    BOOLEAN PROCEDURE checkom;
    ! check that string t contains a valid command  Cn-m  or Cn
    where C is a valid operation code and n and m are numbers
    such that m >= m.  checkom=true is returned for invalid commands;
    BEGIN TEXT v; CHARACTER code; INTEGER n;
      IF t == NOTEXT THEN BEGIN checkom:=TRUE; GOTO fin; END;
      ! special command HT (like in teco);
      upcase(t);
      IF t = "HT" THEN
      BEGIN ! print whole file (at most 1000 lines);
	opcode:=4; min:=1; max:=1000; GOTO fin;
      END;
      IF t = "STOP" OR t = "END" OR t = "EXIT" THEN
      BEGIN opcode:=1; GOTO fin; END;
      ! locate command code in string of valid command characters;
      v:-t; comcodes.Setpos(1); code:=v.Getchar;
      WHILE comcodes.More DO
      IF comcodes.Getchar = code THEN GOTO ready;
      checkom:=TRUE; GOTO fin;
      ready:
      opcode:=comcodes.Pos;
      ! check that operand is single integer or pair of integers;
      v:-v.Sub(v.Pos,v.Length-v.Pos+1);
      n:=checkint(v);
      IF n > 0 THEN
      BEGIN
	min:=v.Getint;
	v:-v.Sub(v.Pos,v.Length-v.Pos+1);
	n:=checkint(v);
	IF n < 0 THEN checkom:=TRUE ELSE
	IF n = 0 THEN max:=min+1 ELSE max:=-v.Getint+1;
      END ELSE checkom:=TRUE;
      IF max <= min THEN checkom:=TRUE;
      fin:
    END of checkom;


    PROCEDURE Outfrac(t,marg); TEXT t; INTEGER marg;
    ! type a line, and if it is too long for a line use more
    lines with a blank margin of marg characters to the left;
    BEGIN TEXT v; INTEGER n;
      v:-t; n:=Sysout.Image.Length-marg-1;
      WHILE v.Length > n DO
      BEGIN
	Sysout.Image.Sub(marg+1,n):=v.Sub(1,n); Outimage;
	v:-v.Sub(n+1,v.Length-n);
      END;
      IF v.Length > 0 THEN
      BEGIN Sysout.Setpos(marg+1); outline(v); END;
    END of outfrac;

    BOOLEAN PROCEDURE matching(ed,t); NAME t; TEXT ed,t;
    BEGIN CHARACTER c; BOOLEAN b; TEXT oldstring,newstring;
      c:=ed.Getchar; oldstring:-scanto(ed,c);
      IF ed.Pos > ed.Length THEN
      BEGIN
	outline("Missing delimiter in string, try again !");
	GOTO edit;
      END;
      newstring:-scanto(ed,c); matching:=b:=change(t,oldstring,newstring);
      IF b AND opcode = 3 THEN
      WHILE t.More DO change(t,oldstring,newstring);
    END of matching;

    !  ____________   START OF MAIN PROGRAM   ________________;

    d:-opendf;
    margin:=0;
    comcodes:-Copy("ECLPRID");
    nulltext:-Blanks(rlength);
    WHILE nulltext.More DO nulltext.Putchar(nullc);
    displaydefault:=FALSE;


    start:	! ______________________  	START OF TOP LOOP;

    request("*",NOTEXT,textinput(t,\checkom),"Illegal command !",dispcom);
    d.Locate(min);
    GOTO command(opcode);
    edit:	! ____________________      STRING REPLACEMENT ;

    request("change: ",nodefault,textinput(editstring,TRUE),"?",dispedit);
    WHILE d.Location < max AND NOT d.Endfile DO
    BEGIN
      d.Inimage; u:-Copy(d.Image.Strip);
      IF matching(editstring,u) THEN
      BEGIN ! type changed line on terminal;
	Outint(d.Location-1,6); Outfrac(u,7);
	IF u.Length > d.Image.Length THEN
	outline("result of change too long ") ELSE
	BEGIN ! rewrite changed line on file;
	  d.Image:=u;
	  d.Locate(d.Location-1); d.Outimage;
	END;
      END;
    END;
    GOTO start;
    list:	! ______________________          LISTING OF LINES;

    WHILE d.Location < max AND NOT d.Endfile DO
    BEGIN
      IF d.Endfile THEN GOTO start;
      d.Inimage;
      IF d.Image.Strip =/= NOTEXT AND d.Image.Strip \= "/*" THEN
      BEGIN
	Outint(d.Location-1,6); Outfrac(d.Image.Strip,7);
      END;
    END;
    GOTO start;
    replace:	! ______________________         REPLACING LINES;

    WHILE d.Location < max DO
    BEGIN
      ll1: stringrequest("new image: ",u);
      IF u.Length > d.Image.Length THEN
      BEGIN outline("Too long line !"); GOTO ll1; END;
      d.Image:=u;
      d.Outimage;
    END;
    GOTO start;
    delete:	! ______________________         DELETING LINES ;

    WHILE d.Location < max DO
    BEGIN
      d.Image:=nulltext; d.Outimage;
    END;
    GOTO start;
    fin: eof:
    d.Setpos(1); d.Close;
  END;
END;