home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource3
/
114_01
/
ed3.bds
< prev
next >
Wrap
Text File
|
1987-07-15
|
14KB
|
713 lines
/*
* Screen editor: command mode commands -- enhanced
*
* Source: ed3.bds
* Version: December 20, 1981.
* Transliteration of small-C version of September 5, 1981
*
*/
/* define globals */
#include ed.h
#include bdscio.h
#include ed1.ccc
#include edext.cc
/* data global to these routines */
/* comment out -----
char filename [SYSFNMAX];
----- end comment out */
/* append command.
* load a file into main buffer at current location.
* this command does NOT change the current file name.
*/
append(args) char *args;
{
char buffer [MAXLEN]; /* disk line buffer */
int file;
int n;
int topline;
char locfn [SYSFNMAX]; /* local file name */
/* get file name which follows command */
if (name1(args,locfn) == ERR) {
return;
}
if (locfn [0] == EOS) {
message("no file argument");
return;
}
/* open the new file */
if ((file=sysopen(locfn,"r")) == ERR) {
message("file not found");
return;
}
/* read the file into the buffer */
while ((n=readline(file,buffer,MAXLEN)) >= 0) {
if (n > MAXLEN) {
message("line truncated");
n=MAXLEN;
}
if (bufins(buffer,n) == ERR) {
break;
}
if (bufdn() == ERR) {
break;
}
}
/* close the file */
sysclose(file);
/* redraw the screen so topline will be at top
* of the screen after command() does a CR/LF.
*/
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
}
/* global change command */
change(args) char *args;
{
char oldline [MAXLEN1]; /* reserve space for EOS */
char newline [MAXLEN1];
char oldpat [MAXLEN1];
char newpat [MAXLEN1];
int from, to, col, n, k;
if (get2args(args,&from,&to) == ERR) {
return;
}
/* get search and change masks into oldpat, newpat */
fmtsout("search mask ? ",0);
getcmnd(oldpat,15);
fmtcrlf();
if (oldpat [0] == EOS) {
return;
}
pmtline();
fmtsout("change mask ? ",0);
getcmnd(newpat,15);
fmtcrlf();
/* make substitution for lines between from, to */
while (from <= to) {
if (chkkey() == YES) {
break;
}
if (bufgo(from++) == ERR) {
break;
}
if (bufatbot() == YES) {
break;
}
n=bufgetln(oldline,MAXLEN);
n=min(n,MAXLEN);
oldline [n]=EOS;
/* '^' anchors search */
if (oldpat [0] == '^') {
if (amatch(oldline,oldpat+1,0) == YES) {
k=replace(oldline,newline,
oldpat+1,newpat,0);
if (k == ERR) {
return;
}
fmtcrlf();
putdec(bufln(),5);
fmtsout(newline,5);
outdeol();
bufrepl(newline,k);
}
continue;
}
/* search oldline for oldpat */
col=0;
while (col < n) {
if (amatch(oldline,oldpat,col++) == YES){
k=replace(oldline,newline,
oldpat,newpat,col-1);
if (k == ERR) {
return;
}
fmtcrlf();
putdec(bufln(),5);
fmtsout(newline,5);
outdeol();
bufrepl(newline,k);
break;
}
}
}
fmtcrlf();
}
/* clear main buffer and file name */
clear()
{
/* make sure it is ok to clear buffer */
if (chkbuf() == YES) {
filename [0]=0;
pmtfile("");
outclr();
outxy(0,SCRNL1);
bufnew();
message("buffer cleared");
}
}
/* multiple line delete command */
delete(args) char *args;
{
int from, to;
if (get2args(args,&from,&to) == ERR) {
return;
}
if (from > to) {
return;
}
/* go to first line to be deleted */
if (bufgo(from) == ERR) {
return;
}
/* delete all line between from and to */
if (bufdeln(to-from+1) == ERR) {
return;
}
/* redraw the screen */
bufout(bufln(),1,SCRNL1);
}
/* search all lines below the current line for a pattern
* return -1 if pattern not found.
* otherwise, return column number of start of pattern.
*/
find()
{
return(search1(bufln()+1,HUGE,YES));
}
/* list lines to list device */
list(args) char *args;
{
char linebuf [MAXLEN1];
int n;
int from, to, line, oldline;
/* save the buffer's current line */
oldline=bufln();
/* get starting, ending lines to print */
if (get2args(args,&from,&to) == ERR) {
return;
}
/* print lines one at a time to list device */
line=from;
while (line <= to) {
/* make sure prompt goes to console */
fmtassn(NO);
/* check for interrupt */
if (chkkey() == YES) {
break;
}
/* print line to list device */
fmtassn(YES);
if (bufgo(line++) != OK) {
break;
}
if (bufatbot()) {
break;
}
n=bufgetln(linebuf,MAXLEN1);
n=min(n,MAXLEN);
linebuf [n]=CR;
fmtsout(linebuf,0);
fmtcrlf();
}
/* redirect output to console */
fmtassn(NO);
/* restore cursor */
bufgo(oldline);
}
/* load file into buffer */
load (args) char *args;
{
char buffer [MAXLEN]; /* disk line buffer */
char locfn [SYSFNMAX]; /* file name until we check it */
int n;
int file;
int topline;
/* get filename following command */
if (name1(args,locfn) == ERR) {
return;
}
if (locfn [0] == EOS) {
message("no file argument");
return;
}
/* give user a chance to save the buffer */
if (chkbuf() == NO) {
return;
}
/* open the new file */
if ((file=sysopen(locfn,"r")) == ERR) {
message("file not found");
return;
}
/* update file name */
syscopfn(locfn, filename);
pmtfile(filename);
/* clear the buffer */
bufnew();
/* read the file into the buffer */
while ((n=readline(file,buffer,MAXLEN)) >= 0) {
if (n > MAXLEN) {
message("line truncated");
n=MAXLEN;
}
if (bufins(buffer,n) == ERR) {
break;
}
if (bufdn() == ERR) {
break;
}
}
/* close the file */
sysclose(file);
/* indicate that the buffer is fresh */
bufsaved();
/* set current line to line 1 */
bufgo(1);
/* redraw the screen so that topline will be
* on line 1 after command() does a CR/LF.
*/
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
}
/* change current file name */
name(args) char *args;
{
name1(args,filename);
pmtfile(filename);
}
/* check syntax of args.
* copy to filename.
* return OK if the name is valid.
*/
name1(args,filename) char *args, *filename;
{
/* skip command */
args=skiparg(args);
args=skipbl(args);
/* check file name syntax */
if (syschkfn(args) == ERR) {
return(ERR);
}
/* copy filename */
syscopfn(args,filename);
return(OK);
}
/* save the buffer in an already existing file */
resave()
{
char linebuf [MAXLEN];
int file, n, oldline;
/* make sure file has a name */
if (filename [0] == EOS) {
message("file not named");
return;
}
/* the file must exist for resave */
if ((file=sysopen(filename,"r")) == ERR) {
message("file not found");
return;
}
if (sysclose(file) == ERR) {
return;
}
/* open the file for writing */
if ((file=sysopen(filename,"w")) == ERR) {
return;
}
/* save the current position of file */
oldline=bufln();
/* write out the whole file */
if (bufgo(1) == ERR) {
sysclose(file);
return;
}
while (bufatbot() == NO) {
n=bufgetln(linebuf,MAXLEN);
n=min(n,MAXLEN);
if (pushline(file,linebuf,n) == ERR) {
break;
}
if (bufdn() == ERR) {
break;
}
}
/* indicate if all buffer was saved */
if (bufatbot()){
bufsaved();
}
/* close file and restore line number */
sysclose(file);
bufgo(oldline);
}
/* save the buffer in a new file */
save()
{
char linebuf [MAXLEN];
int file, n, oldline;
/* make sure the file is named */
if (filename [0] == EOS) {
message("file not named");
return;
}
/* file must NOT exist for save */
if ((file=sysopen(filename,"r")) != ERR) {
sysclose(file);
message("file exists");
return;
}
/* open file for writing */
if ((file=sysopen(filename,"w")) == ERR) {
return;
}
/* remember current line */
oldline=bufln();
/* write entire buffer to file */
if (bufgo(1) == ERR) {
sysclose(file);
return;
}
while (bufatbot() == NO) {
n=bufgetln(linebuf,MAXLEN);
n=min(n,MAXLEN);
if (pushline(file,linebuf,n) == ERR) {
break;
}
if (bufdn() == ERR) {
break;
}
}
/* indicate buffer saved if good write */
if (bufatbot()) {
bufsaved()