home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_100
/
157_01
/
qed
< prev
next >
Wrap
Text File
|
1987-10-12
|
14KB
|
628 lines
/* VERSION 0021 (DATE: 28/04/87) (TIME: 16:59) */
/*
e (qed) screen editor
(C) G. Nigel Gilbert, MICROLOGY, 1981
licensed for private non-profitmaking use 1983
August-December 1981
Modified: Aug-Dec 1984: BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
March 1985: BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
FILE: qed
FUNCTIONS: main, initialise, edit, finish, seldisk
PURPOSE: initialise; process commands;
*/
#include "qed.h"
#define VERNO 0x30 /* get MSDOS version number */
main(argc,argv)
int argc;
char **argv;
{
int argn;
int i;
char *dig;
/**************************Default option settings***************************/
initjmp=1; /*jmp to line 1*/
autoin=YES; /*auto indent [YES/NO]*/
backup=YES; /*make ".BAK" file [YES/NO]*/
readall=YES; /*read as much text as possible up to memory*/
trail=NO; /*don't strip trailing blanks [YES/NO]*/
helpon=NO; /*start with help menu on [YES/NO]*/
expert=YES; /*brief prompts [YES/NO]*/
tabwidth=4; /*tab stops every n cols [number]*/
displaypos=YES; /*display line:column at top of screen*/
blockscroll=NO; /*don't scroll whole page, just current line*/
pagingdisk=0; /*create buffer file on this disk -
set to either 0 (for currently logged-in disk)
or to desired disk letter (eg 'B') */
wboot=YES; /*do a warm boot at end of edit [YES/NO]*/
defext[0]='\0'; /* default extension */
#if WWRAP
rtmarg=LLIM; /* right margin*/
#endif
#if VRSNNUM
version=YES; /* change version num on save*/
*date='\0';
*time='\0';
#endif
/***********************End of default option settings***********************/
inbufp=0;
if((_os(VERNO,0) & 0x00ff) < 2) {
puts("Must use MSDOS vers 2.x with 'qed'");
exit(0);
}
filename[0]=name[0]='\0';
argn=0;
while (++argn < argc)
if (*argv[argn] == '-') {
dig=argv[argn]+1;
switch(toupper(*dig)) {
case 'J' :
initjmp = atoi((argv[argn]+2));
break;
case 'A' :
autoin=!autoin;
break;
case 'B' :
backup=!backup;
break;
case 'L' :
displaypos=!displaypos;
break;
case 'H' :
blockscroll=!blockscroll;
break;
case 'R' :
readall=!readall;
break;
#if WWRAP
case 'M' :
rtmarg=0;
rtmarg=atoi((argv[argn]+2));
if (rtmarg < LLIM) {
trail=YES;
autoin=NO;
}
break;
#endif
case 'T' :
tabwidth=0;
while (*++dig) tabwidth=tabwidth*10+*dig-'0';
break;
case 'S' :
trail=!trail;
break;
#if VRSNNUM
case 'U' :
version = !version;
break;
#endif
case 'V' :
helpon=!helpon;
break;
case 'E' :
expert=!expert;
break;
case 'X' :
wboot=!wboot;
break;
case 'D' :
pagingdisk=toupper(*(dig+1));
if (pagingdisk >= 'A' && pagingdisk <= 'P') break;
default :
putstr("Illegal option: ");
putstr(argv[argn]);
exit();
}
}
else strcpy(filename[0] ? name : filename,argv[argn]);
ans[0]=patt[0]=changeto[0]=opts[0]='\0';
keytranslate();
do {
initialise();
edit();
}
while (YES);
}
initialise()
{
int i,warn;
cursorx=charn=offset=lastoff=from=to=0;
cursory=topline=lastread=lastl=findir=jmpto=1;
changed=isdim=replace=repeat=blocking=warn=blankedmess=NO;
pfirst=-100;
goteof=YES;
errmess=NULL;
curdsk=_os(CURDSK);
bakpath[0]='B';
bakpath[1]=':';
bakpath[2]='\\';
bakpath[3]='\0';
initvm();
text[0]='\0';
cline=1;
altered=YES;
puttext();
setstatusname();
if (filename[0]) {
putclr();
gotoxy(8,11);
putstr("qed screen editor version ");
putstr(VERSION);
gotoxy(12,12);
putstr("MICROLOGY 1983 and JWH 1985");
gotoxy(12,13);
terminal();
while (opentext(filename) == FAIL) {
if (opentext(strcat(filename,defext)) == FAIL) {
askforfile();
if (!filename[0]) goto newfile;
}
}
lastl=UNKNOWN;
lastread=0;
goteof=NO;
if (name[0]) {
strcpy(filename,name);
name[0]='\0';
}
}
newfile: errmess=NULL;
format(filename);
/*read as much text to fill memory or end of file*/
if (readall)
for (i=1;(!(usage[slotsinmem-1]) && (!(goteof)));i+=100)
readtext(i);
setstatusname();
gettext(1);
putclr();
if (helpon) dohelp();
if (initjmp>2) { /* (2-1) is a bad jump from line 1 */
jumpline(initjmp-cline); /* not possible to do init jump to 2*/
initjmp=0;
putstatusline(cline);
}
else
putpage();
}
edit() /*command processor*/
{
char c;
char oldcrx,inc;
int to;
setjmp(0);
while (YES) {
goodline=cline;
if ((expert) && (!blankedmess)) xmessoff();
else if (!blankedmess) unmess();
putlineno(cline);
resetcursor();
c=getkey();
if (errmess != NULL) {
errmess=NULL;
putstatusline(cline);
}
switch(c) {
case DOWNKEY :
moveline(1);
break;
case UPKEY :
moveline(-1);
break;
case LEFTKEY :
movechar(-1);
break;
case RIGHTKEY :
movechar(1);
break;
case LEFTWKEY :
moveword(-1);
break;
case RIGHTWKEY :
moveword(1);
break;
case BOLKEY :
sync(0);
break;
case EOLKEY :
sync(strlen(text));
break;
case UPPAGE :
movepage(-1);
break;
case DOWNPAGE :
movepage(0);
break;
case HOMEKEY :
if (jumpline(lastl-cline)) sync(strlen(text));
break;
case BOFKEY :
if (jumpline(1-cline)) sync(0);
break;
case DELLEFT :
changed=YES;
deletechar(-1);
break;
case DELRIGHT :
deletechar(0);
changed=YES;
break;
case DELLNKEY :
changed=YES;
strcpy(textb,text); /*save for undelete line*/
text[0]='\0';
crdelete((cline == lastl ? -1 : 0));
break;
case DELWDKEY :
changed=YES;
deleteword();
break;
case JUMPKEY :
if (expert) xprtmess("J ? ");
else putmess("Jump ([+/-]n,RET=crnt) ? ");
scans(ans,8);
if (!*ans) jmpto = cline;
else
calcjmp();
if (expert) xmessoff();
break;
case TOPSCRKEY :
moveline((topline-cursory));
break;
case BOTSCRKEY :
moveline((SHEIGHT-cursory));
break;
case REPKEY :
repeat=YES;
dofindrep(1,1);
repeat=NO;
break;
case BLOCKKEY :
switch (to=blockops()) {
case YES : return;
break;
case 'x' :
case 'q' : if (finish(to)==YES) return;
break;
case 'r' :
if (expert) xprtmess("Read file? ");
else putmess("File to read? ");
scans(name,FILELEN);
if (*(name)) {
readfile(name);
putpage();
changed=YES;
}
break;
}
break;
case SCRLDNKEY :
/* original: slow but sure
oldcrx = cursorx;
to = plast-cline;
moveline(to);
inc=( (moveline(1)==YES) ? 1 : 0);
cursorx = oldcrx;
moveline(((cline-to)<=pfirst) ? (topline-cursory) : (-to - inc));
*/
/* new: 29.viii.86*/
if(cursory==topline) puttext();
if (plast>=(pfirst+2)) {
linedelete(topline);
inc = plast-pfirst;
if (plast<lastl) {
getline(++plast);
putline(plast,
(inc<(SHEIGHT-topline)) ? (inc) : SHEIGHT);
}
pfirst++;
if (cursory==topline) gettext(pfirst);
cursorx=adjustc(cursorx);
gotoxy(cursorx, (cursory -= cursory>topline ? 1 : 0));
}
break;
case UNDELINE :
/* helpon via kontext key
if (helpon) {
helpon=NO;
unmess();
}
else {
helpon=YES;
dohelp();
}
*/
puttext();
cline=inject(cline,textb);
if (cursory>=SHEIGHT) {
linedelete(topline);
gotoxy(0,cursory);
gettext(cline);
putline(cline,cursory);
plast++;
pfirst++;
}
else {
gotoxy(0,++cursory);
insertline();
gettext(cline);
putline(cline,cursory);
if ((plast-pfirst) < SHEIGHT-topline) plast++;
}
break;
case CR :
changed=YES;
crinsert(0);
break;
case CRSTILL :
changed=YES;
crinsert(-1);
break;
case SCRLUPKEY :
/* original: save current cursorx
oldcrx = cursorx;
to = topline-cursory;
moveline(to);
inc=( (moveline(-1)==YES) ? 1 : 0);
cursorx = oldcrx;
moveline(((cline-to)>=plast) ? (SHEIGHT-cursory) : (-to + inc));
*/
/* new: 29.viii.86 */
if(cursory==SHEIGHT)puttext();
if (pfirst>1) {
gotoxy(0,topline);
insertline();
inc=plast-pfirst;
putline((--pfirst),topline);
if (inc >= (SHEIGHT-topline)) plast--;
if (cursory==SHEIGHT) gettext(plast);
cursorx=adjustc(cursorx);
gotoxy(cursorx, (cursory += ((cursory<SHEIGHT) ? 1 : 0)));
}
break;
case QWIKKEY :
info();
break;
case OOPSKEY :
oops();
break;
case TAB:
changed=YES;
insertchar('\t');
break;
case ESCKEY :
resetcursor();
while (!(c=inkey()));
if (c < ' ') ; /*c already CTRL*/
else
if (c < '@') break; /*c not CTRL char*/
else
c -= ((c < '`') ? 64 : 96); /*c either upper or lower*/
default :
changed=YES;
insertchar(c);
break;
}
}
}
finish(o) int o; /*return YES to edit another file;
NO to return to current file
or don't return, but exit if finished altogther */
{
int abandon;
char c, tempname[FILELEN], namebak[FILELEN], *strcpy();
if (o != 'x') {
if (expert) xprtmess("W/A/R ? ");
else
putmess("W|rite edited text to file, |A|bandon all edits, or |R|eturn? ");
while ( (c=getlow()) != 'w' && c != 'a' && c != 'r');
putch(c);
if (c == 'r') return NO;
abandon= c == 'a';
}
if ((c == 'w') || (o == 'x')) {
if (!filename[0]) {
if (expert) xprtmess("Write file ?");
else putmess("File to write to? ");
scans(filename,FILELEN);
format(filename);
if (filename[0] <= ' ' || (!backup && !exists(filename)))
{
filename[0]='\0';
return NO;
}
}
if (backup) { /*delete old bak file*/
retag(strcpy(namebak,filename),"BAK");
if (checkexists(namebak))
if (funlink(namebak) == FAIL) return NO;
}
#if VRSNNUM
if (version) putvrsn(); /*update version num and date/time*/
#endif
strcpy(tempname,filename); /*keep old name in 'filename'*/
retag(tempname,"$$$"); /*new file called'.$$$'*/
if (writefile(1,lastl,tempname,filename,YES) == FAIL) return NO;
/*check original file still exists - may have been deleted or
renamed by user */
if (checkexists(filename)) {
if (backup) {
/*orig. file becomes '.bak' */
if (frename(filename,namebak) == FAIL) goto failed;
}
else {
/*delete orig file*/
if (funlink(filename) == FAIL) {
failed: /*if can't delete/rename old file, change
new name to '.$$$'*/
strcpy(filename,tempname);
goto nowrite;
}
}
}
frename(tempname,filename); /*new file goes from '$$$' to orig name*/
changed=NO; /*file now not changed*/
}
if (o == 'x') {
/*
if (pagefd != NOFILE) {
close(pagefd);
funlink(pagingfile);
}
*/
xit();
};
nowrite:
if (expert) xprtmess("E/R/A ? ");
else
putmess("E|xit to DOS, |R|eturn to this file, or edit |A|nother file? ");
while ( (c=getlow()) != 'e' && c!='a' && c!='r');
putch(c);
switch(c) {
case 'e' :
if (changed)
if (!dispose()) return NO;
fclose(textbuf);
/*
if (pagefd != NOFILE) {
close(pagefd);
funlink(pagingfile);
}
*/
xit();
case 'a' :
if (changed)
if (!dispose()) return NO;
fclose(textbuf);
if (pagefd != NOFILE) {
close(pagefd);
funlink(pagingfile);
}
askforfile();
return YES;
case 'r' :
if (!abandon) {
if (pagefd != NOFILE) {
close(pagefd);
funlink(pagingfile);
}
gotoxy(WAITPOS,0);
opentext(filename);
lastl=UNKNOWN;
lastread=0;
goteof=NO;
initvm();
gettext(cline);
errmess=NULL;
putstatusline(cline);
}
if (expert) xmessoff();
return NO;
}
}
dispose() /*check to dispose of modified file*/
{
char ans;
if (expert) xprtmess("Abandon mods ? ");
else putmess("Abandon modified file? (y/n) ");
ans=getlow();
/*if (getlow() == 'y') return YES;*/
if (expert) xmessoff();
if (ans=='y') return YES;
return NO;
}
xit() /*leave the editor, either by returning to CCP or by
warm booting, according to X option*/
{
/* simple no error exit for now */
if (pagefd != NOFILE) {
close(pagefd);
funlink(pagingfile);
}
deleteline(0,23);
gotoxy(0,22);
exit(0);
}
askforfile() /*get another file to edit into 'filename' */
{
int drive;
while(YES) {
putclr();
dirdone:
if (errmess != NULL) {
gotoxy(EMPOS,0);
putstr(errmess);
}
gotoxy(0,0);
putstr("File to edit\n |([return] to create a new file");
putstr(" >| to view directory|\n");
putstr(" |A:-P: to change default disk, [escape] to exit)| ? ");
if (scans(filename,FILELEN) == ESCKEY) xit();
/*if (filename[0]=='\0') justfile[0]='\0';*/
deleteline(EMPOS,0);
errmess = NULL;
if (filename[0] == '>') {
filename[0] = '\0';
deleteline(1,3);
putstr("Enter [drv:]mask| (RET for all) ");
dirmsk();
goto dirdone;
}
if (strlen(filename) == 2 &&
filename[1] == ':') {
if ( (drive=toupper(filename[0])-'A') >= 0 && drive < 16) {
{if (seldisk(drive)) continue;}
/* bdos(RESETDRV,1 << drive); */
curdsk=drive;
}
}
else {
name[0]='\0';
/*if (filename[0]=='\0') justfile[0]=='\0';*/
return YES;
}
}
}
seldisk(drive)
int drive;
{
/*
if (dskcheck(setjmp(dskerr))) return FAIL;
*/
_os(SELDSK,drive);
return 0;
}