2005-05-31 11:50:56 -04:00
|
|
|
/* FILE.C
|
|
|
|
*
|
|
|
|
* The routines in this file handle the reading, writing
|
|
|
|
* and lookup of disk files. All of details about the
|
|
|
|
* reading and writing of the disk are in "fileio.c".
|
|
|
|
*
|
|
|
|
* modified by Petri Kutvonen
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-10-01 01:52:45 -04:00
|
|
|
#include <unistd.h>
|
2005-05-31 11:50:56 -04:00
|
|
|
#include "estruct.h"
|
|
|
|
#include "edef.h"
|
2005-10-01 01:52:45 -04:00
|
|
|
#include "efunc.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a file into the current
|
|
|
|
* buffer. This is really easy; all you do it
|
|
|
|
* find the name of the file, and call the standard
|
|
|
|
* "read a file into the current buffer" code.
|
|
|
|
* Bound to "C-X C-R".
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int fileread(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
char fname[NFILEN];
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (restflag) /* don't allow this command if restricted */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (resterr());
|
|
|
|
if ((s = mlreply("Read file: ", fname, NFILEN)) != TRUE)
|
|
|
|
return (s);
|
|
|
|
return (readin(fname, TRUE));
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a file into the current
|
|
|
|
* buffer. This is really easy; all you do it
|
|
|
|
* find the name of the file, and call the standard
|
|
|
|
* "insert a file into the current buffer" code.
|
|
|
|
* Bound to "C-X C-I".
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int insfile(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int s;
|
2005-05-31 11:50:56 -04:00
|
|
|
char fname[NFILEN];
|
|
|
|
|
|
|
|
if (restflag) /* don't allow this command if restricted */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (resterr());
|
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
|
|
|
return (rdonly()); /* we are in read only mode */
|
|
|
|
if ((s = mlreply("Insert file: ", fname, NFILEN)) != TRUE)
|
|
|
|
return (s);
|
|
|
|
if ((s = ifile(fname)) != TRUE)
|
|
|
|
return (s);
|
|
|
|
return (reposition(TRUE, -1));
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select a file for editing.
|
|
|
|
* Look around to see if you can find the
|
|
|
|
* fine in another buffer; if you can find it
|
|
|
|
* just switch to the buffer. If you cannot find
|
|
|
|
* the file, create a new buffer, read in the
|
|
|
|
* text, and switch to the new buffer.
|
|
|
|
* Bound to C-X C-F.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int filefind(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
char fname[NFILEN]; /* file user wishes to find */
|
2010-02-27 05:38:22 -05:00
|
|
|
int s; /* status return */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (restflag) /* don't allow this command if restricted */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (resterr());
|
|
|
|
if ((s = mlreply("Find file: ", fname, NFILEN)) != TRUE)
|
|
|
|
return (s);
|
|
|
|
return (getfile(fname, TRUE));
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
int viewfile(int f, int n)
|
2005-09-30 18:26:09 -04:00
|
|
|
{ /* visit a file in VIEW mode */
|
|
|
|
char fname[NFILEN]; /* file user wishes to find */
|
2010-02-27 05:38:22 -05:00
|
|
|
int s; /* status return */
|
|
|
|
window_t *wp; /* scan for windows that need updating */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (restflag) /* don't allow this command if restricted */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (resterr());
|
|
|
|
if ((s = mlreply("View file: ", fname, NFILEN)) != TRUE)
|
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
s = getfile(fname, FALSE);
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s) { /* if we succeed, put it in view mode */
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_bufp->b_mode |= MDVIEW;
|
|
|
|
|
|
|
|
/* scan through and update mode lines of all windows */
|
|
|
|
wp = wheadp;
|
|
|
|
while (wp != NULL) {
|
|
|
|
wp->w_flag |= WFMODE;
|
|
|
|
wp = wp->w_wndp;
|
|
|
|
}
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CRYPT
|
2006-11-19 14:52:18 -05:00
|
|
|
static int resetkey(void)
|
2005-09-30 18:26:09 -04:00
|
|
|
{ /* reset the encryption key if needed */
|
2010-02-27 05:38:22 -05:00
|
|
|
int s; /* return status */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* turn off the encryption flag */
|
|
|
|
cryptflag = FALSE;
|
|
|
|
|
|
|
|
/* if we are in crypt mode */
|
|
|
|
if (curbp->b_mode & MDCRYPT) {
|
|
|
|
if (curbp->b_key[0] == 0) {
|
2006-11-19 14:52:18 -05:00
|
|
|
s = set_encryption_key(FALSE, 0);
|
2005-05-31 11:50:56 -04:00
|
|
|
if (s != TRUE)
|
2005-09-30 18:26:09 -04:00
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* let others know... */
|
|
|
|
cryptflag = TRUE;
|
|
|
|
|
|
|
|
/* and set up the key to be used! */
|
|
|
|
/* de-encrypt it */
|
2006-11-19 14:52:18 -05:00
|
|
|
myencrypt((char *) NULL, 0);
|
|
|
|
myencrypt(curbp->b_key, strlen(curbp->b_key));
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* re-encrypt it...seeding it to start */
|
2006-11-19 14:52:18 -05:00
|
|
|
myencrypt((char *) NULL, 0);
|
|
|
|
myencrypt(curbp->b_key, strlen(curbp->b_key));
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
return (TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* getfile()
|
|
|
|
*
|
|
|
|
* char fname[]; file name to find
|
|
|
|
* int lockfl; check the file for locks?
|
|
|
|
*/
|
|
|
|
int getfile(char *fname, int lockfl)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
struct buffer *bp;
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
char bname[NBUFN]; /* buffer name to put file */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if MSDOS
|
|
|
|
mklower(fname); /* msdos isn't case sensitive */
|
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
|
|
|
|
if ((bp->b_flag & BFINVS) == 0
|
|
|
|
&& strcmp(bp->b_fname, fname) == 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
swbuffer(bp);
|
2005-09-30 18:26:09 -04:00
|
|
|
lp = curwp->w_dotp;
|
|
|
|
i = curwp->w_ntrows / 2;
|
|
|
|
while (i-- && lback(lp) != curbp->b_linep)
|
|
|
|
lp = lback(lp);
|
|
|
|
curwp->w_linep = lp;
|
|
|
|
curwp->w_flag |= WFMODE | WFHARD;
|
2005-05-31 11:50:56 -04:00
|
|
|
cknewwindow();
|
|
|
|
mlwrite("(Old buffer)");
|
2005-09-30 18:26:09 -04:00
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
makename(bname, fname); /* New buffer name. */
|
|
|
|
while ((bp = bfind(bname, FALSE, 0)) != NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* old buffer name conflict code */
|
2005-09-30 18:26:09 -04:00
|
|
|
s = mlreply("Buffer name: ", bname, NBUFN);
|
|
|
|
if (s == ABORT) /* ^G to just quit */
|
|
|
|
return (s);
|
|
|
|
if (s == FALSE) { /* CR to clobber it */
|
|
|
|
makename(bname, fname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) {
|
|
|
|
mlwrite("Cannot create buffer");
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (--curbp->b_nwnd == 0) { /* Undisplay. */
|
|
|
|
curbp->b_dotp = curwp->w_dotp;
|
|
|
|
curbp->b_doto = curwp->w_doto;
|
|
|
|
curbp->b_markp = curwp->w_markp;
|
|
|
|
curbp->b_marko = curwp->w_marko;
|
|
|
|
}
|
|
|
|
curbp = bp; /* Switch to it. */
|
|
|
|
curwp->w_bufp = bp;
|
|
|
|
curbp->b_nwnd++;
|
|
|
|
s = readin(fname, lockfl); /* Read it in. */
|
2005-05-31 11:50:56 -04:00
|
|
|
cknewwindow();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-01 01:52:45 -04:00
|
|
|
* Read file "fname" into the current buffer, blowing away any text
|
|
|
|
* found there. Called by both the read and find commands. Return
|
|
|
|
* the final status of the read. Also called by the mainline, to
|
|
|
|
* read in a file specified on the command line as an argument.
|
|
|
|
* The command bound to M-FNR is called after the buffer is set up
|
|
|
|
* and before it is read.
|
|
|
|
*
|
|
|
|
* char fname[]; name of file to read
|
|
|
|
* int lockfl; check for file locks?
|
|
|
|
*/
|
|
|
|
int readin(char *fname, int lockfl)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp1;
|
|
|
|
struct line *lp2;
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
|
|
|
window_t *wp;
|
|
|
|
struct buffer *bp;
|
|
|
|
int s;
|
|
|
|
int nbytes;
|
|
|
|
int nline;
|
2005-09-30 18:26:09 -04:00
|
|
|
int lflag; /* any lines longer than allowed? */
|
2005-05-31 11:50:56 -04:00
|
|
|
char mesg[NSTRING];
|
|
|
|
|
|
|
|
#if (FILOCK && BSD) || SVR4
|
|
|
|
if (lockfl && lockchk(fname) == ABORT)
|
|
|
|
#if PKCODE
|
|
|
|
{
|
|
|
|
s = FIOFNF;
|
|
|
|
bp = curbp;
|
|
|
|
strcpy(bp->b_fname, "");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
return (ABORT);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if CRYPT
|
|
|
|
s = resetkey();
|
|
|
|
if (s != TRUE)
|
2005-09-30 18:26:09 -04:00
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
bp = curbp; /* Cheap. */
|
|
|
|
if ((s = bclear(bp)) != TRUE) /* Might be old. */
|
|
|
|
return (s);
|
|
|
|
bp->b_flag &= ~(BFINVS | BFCHG);
|
|
|
|
strcpy(bp->b_fname, fname);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* let a user macro get hold of things...if he wants */
|
2005-09-30 18:26:09 -04:00
|
|
|
execute(META | SPEC | 'R', FALSE, 1);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* turn off ALL keyboard translation in case we get a dos error */
|
|
|
|
TTkclose();
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */
|
|
|
|
goto out;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s == FIOFNF) { /* File not found. */
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(New file)");
|
2005-09-30 18:26:09 -04:00
|
|
|
goto out;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* read the file in */
|
|
|
|
mlwrite("(Reading file)");
|
2005-09-30 18:26:09 -04:00
|
|
|
nline = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
lflag = FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
while ((s = ffgetline()) == FIOSUC) {
|
|
|
|
nbytes = strlen(fline);
|
|
|
|
if ((lp1 = lalloc(nbytes)) == NULL) {
|
|
|
|
s = FIOMEM; /* Keep message on the */
|
|
|
|
break; /* display. */
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
|
|
|
if (nline > MAXNLINE) {
|
|
|
|
s = FIOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
lp2 = lback(curbp->b_linep);
|
|
|
|
lp2->l_fp = lp1;
|
|
|
|
lp1->l_fp = curbp->b_linep;
|
|
|
|
lp1->l_bp = lp2;
|
|
|
|
curbp->b_linep->l_bp = lp1;
|
|
|
|
for (i = 0; i < nbytes; ++i)
|
|
|
|
lputc(lp1, i, fline[i]);
|
|
|
|
++nline;
|
|
|
|
}
|
|
|
|
ffclose(); /* Ignore errors. */
|
2005-05-31 11:50:56 -04:00
|
|
|
strcpy(mesg, "(");
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s == FIOERR) {
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(mesg, "I/O ERROR, ");
|
|
|
|
curbp->b_flag |= BFTRUNC;
|
|
|
|
}
|
|
|
|
if (s == FIOMEM) {
|
|
|
|
strcat(mesg, "OUT OF MEMORY, ");
|
|
|
|
curbp->b_flag |= BFTRUNC;
|
|
|
|
}
|
|
|
|
sprintf(&mesg[strlen(mesg)], "Read %d line", nline);
|
2005-09-30 18:26:09 -04:00
|
|
|
if (nline != 1)
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(mesg, "s");
|
|
|
|
strcat(mesg, ")");
|
|
|
|
mlwrite(mesg);
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
out:
|
|
|
|
TTkopen(); /* open the keyboard again */
|
|
|
|
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
|
|
|
|
if (wp->w_bufp == curbp) {
|
|
|
|
wp->w_linep = lforw(curbp->b_linep);
|
|
|
|
wp->w_dotp = lforw(curbp->b_linep);
|
|
|
|
wp->w_doto = 0;
|
|
|
|
wp->w_markp = NULL;
|
|
|
|
wp->w_marko = 0;
|
|
|
|
wp->w_flag |= WFMODE | WFHARD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s == FIOERR || s == FIOFNF) /* False if error. */
|
|
|
|
return (FALSE);
|
2005-05-31 11:50:56 -04:00
|
|
|
#if 0
|
|
|
|
if (s == ABORT)
|
2005-09-30 18:26:09 -04:00
|
|
|
return (ABORT);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
return (TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a file name, and from it
|
|
|
|
* fabricate a buffer name. This routine knows
|
|
|
|
* about the syntax of file names on the target system.
|
|
|
|
* I suppose that this information could be put in
|
|
|
|
* a better place than a line of code.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
void makename(char *bname, char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char *cp1;
|
|
|
|
char *cp2;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
cp1 = &fname[0];
|
|
|
|
while (*cp1 != 0)
|
|
|
|
++cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if VMS
|
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']'
|
|
|
|
&& cp1[-1] != '>')
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']')
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
--cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
#if MSDOS
|
2005-09-30 18:26:09 -04:00
|
|
|
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != '\\'
|
|
|
|
&& cp1[-1] != '/')
|
|
|
|
--cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
#if V7 | USG | BSD
|
2005-09-30 18:26:09 -04:00
|
|
|
while (cp1 != &fname[0] && cp1[-1] != '/')
|
|
|
|
--cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
cp2 = &bname[0];
|
|
|
|
while (cp2 != &bname[NBUFN - 1] && *cp1 != 0 && *cp1 != ';')
|
|
|
|
*cp2++ = *cp1++;
|
|
|
|
*cp2 = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* make sure a buffer name is unique
|
|
|
|
*
|
|
|
|
* char *name; name to check on
|
|
|
|
*/
|
|
|
|
void unqname(char *name)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char *sp;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* check to see if it is in the buffer list */
|
|
|
|
while (bfind(name, 0, FALSE) != NULL) {
|
|
|
|
|
|
|
|
/* go to the end of the name */
|
|
|
|
sp = name;
|
|
|
|
while (*sp)
|
|
|
|
++sp;
|
2005-09-30 18:26:09 -04:00
|
|
|
if (sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) {
|
2005-05-31 11:50:56 -04:00
|
|
|
*sp++ = '0';
|
|
|
|
*sp = 0;
|
|
|
|
} else
|
|
|
|
*(--sp) += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask for a file name, and write the
|
|
|
|
* contents of the current buffer to that file.
|
|
|
|
* Update the remembered file name and clear the
|
|
|
|
* buffer changed flag. This handling of file names
|
|
|
|
* is different from the earlier versions, and
|
|
|
|
* is more compatable with Gosling EMACS than
|
|
|
|
* with ITS EMACS. Bound to "C-X C-W".
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int filewrite(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
window_t *wp;
|
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
char fname[NFILEN];
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (restflag) /* don't allow this command if restricted */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (resterr());
|
|
|
|
if ((s = mlreply("Write file: ", fname, NFILEN)) != TRUE)
|
|
|
|
return (s);
|
|
|
|
if ((s = writeout(fname)) == TRUE) {
|
|
|
|
strcpy(curbp->b_fname, fname);
|
|
|
|
curbp->b_flag &= ~BFCHG;
|
|
|
|
wp = wheadp; /* Update mode lines. */
|
|
|
|
while (wp != NULL) {
|
|
|
|
if (wp->w_bufp == curbp)
|
|
|
|
wp->w_flag |= WFMODE;
|
|
|
|
wp = wp->w_wndp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the contents of the current
|
|
|
|
* buffer in its associatd file. No nothing
|
|
|
|
* if nothing has changed (this may be a bug, not a
|
|
|
|
* feature). Error if there is no remembered file
|
|
|
|
* name for the buffer. Bound to "C-X C-S". May
|
|
|
|
* get called by "C-Z".
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int filesave(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
window_t *wp;
|
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
|
|
|
return (rdonly()); /* we are in read only mode */
|
|
|
|
if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */
|
|
|
|
return (TRUE);
|
|
|
|
if (curbp->b_fname[0] == 0) { /* Must have a name. */
|
|
|
|
mlwrite("No file name");
|
|
|
|
return (FALSE);
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* complain about truncated files */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((curbp->b_flag & BFTRUNC) != 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (mlyesno("Truncated file ... write it out") == FALSE) {
|
|
|
|
mlwrite("(Aborted)");
|
2005-09-30 18:26:09 -04:00
|
|
|
return (FALSE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = writeout(curbp->b_fname)) == TRUE) {
|
|
|
|
curbp->b_flag &= ~BFCHG;
|
|
|
|
wp = wheadp; /* Update mode lines. */
|
|
|
|
while (wp != NULL) {
|
|
|
|
if (wp->w_bufp == curbp)
|
|
|
|
wp->w_flag |= WFMODE;
|
|
|
|
wp = wp->w_wndp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function performs the details of file
|
|
|
|
* writing. Uses the file management routines in the
|
|
|
|
* "fileio.c" package. The number of lines written is
|
2010-04-17 19:38:09 -04:00
|
|
|
* displayed. Sadly, it looks inside a struct line; provide
|
2005-05-31 11:50:56 -04:00
|
|
|
* a macro for this. Most of the grief is error
|
|
|
|
* checking of some sort.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int writeout(char *fn)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int s;
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2010-02-27 05:38:22 -05:00
|
|
|
int nline;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if CRYPT
|
|
|
|
s = resetkey();
|
|
|
|
if (s != TRUE)
|
2005-09-30 18:26:09 -04:00
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
/* turn off ALL keyboard translation in case we get a dos error */
|
|
|
|
TTkclose();
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = ffwopen(fn)) != FIOSUC) { /* Open writes message. */
|
2005-05-31 11:50:56 -04:00
|
|
|
TTkopen();
|
2005-09-30 18:26:09 -04:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
mlwrite("(Writing...)"); /* tell us were writing */
|
|
|
|
lp = lforw(curbp->b_linep); /* First line. */
|
|
|
|
nline = 0; /* Number of lines. */
|
|
|
|
while (lp != curbp->b_linep) {
|
|
|
|
if ((s = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
|
|
|
|
break;
|
|
|
|
++nline;
|
|
|
|
lp = lforw(lp);
|
|
|
|
}
|
|
|
|
if (s == FIOSUC) { /* No write error. */
|
|
|
|
s = ffclose();
|
|
|
|
if (s == FIOSUC) { /* No close error. */
|
|
|
|
if (nline == 1)
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(Wrote 1 line)");
|
2005-09-30 18:26:09 -04:00
|
|
|
else
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(Wrote %d lines)", nline);
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
} else /* Ignore close error */
|
|
|
|
ffclose(); /* if a write error. */
|
2005-05-31 11:50:56 -04:00
|
|
|
TTkopen();
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s != FIOSUC) /* Some sort of error. */
|
|
|
|
return (FALSE);
|
|
|
|
return (TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The command allows the user
|
|
|
|
* to modify the file name associated with
|
|
|
|
* the current buffer. It is like the "f" command
|
|
|
|
* in UNIX "ed". The operation is simple; just zap
|
2010-02-15 21:09:42 -05:00
|
|
|
* the name in the buffer structure, and mark the windows
|
2005-05-31 11:50:56 -04:00
|
|
|
* as needing an update. You can type a blank line at the
|
|
|
|
* prompt if you wish.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int filename(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
window_t *wp;
|
|
|
|
int s;
|
2005-09-30 18:26:09 -04:00
|
|
|
char fname[NFILEN];
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (restflag) /* don't allow this command if restricted */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (resterr());
|
|
|
|
if ((s = mlreply("Name: ", fname, NFILEN)) == ABORT)
|
|
|
|
return (s);
|
|
|
|
if (s == FALSE)
|
|
|
|
strcpy(curbp->b_fname, "");
|
|
|
|
else
|
|
|
|
strcpy(curbp->b_fname, fname);
|
|
|
|
wp = wheadp; /* Update mode lines. */
|
|
|
|
while (wp != NULL) {
|
|
|
|
if (wp->w_bufp == curbp)
|
|
|
|
wp->w_flag |= WFMODE;
|
|
|
|
wp = wp->w_wndp;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
curbp->b_mode &= ~MDVIEW; /* no longer read only mode */
|
2005-09-30 18:26:09 -04:00
|
|
|
return (TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert file "fname" into the current
|
|
|
|
* buffer, Called by insert file command. Return the final
|
|
|
|
* status of the read.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int ifile(char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp0;
|
|
|
|
struct line *lp1;
|
|
|
|
struct line *lp2;
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
|
|
|
struct buffer *bp;
|
|
|
|
int s;
|
|
|
|
int nbytes;
|
|
|
|
int nline;
|
2005-09-30 18:26:09 -04:00
|
|
|
int lflag; /* any lines longer than allowed? */
|
2005-05-31 11:50:56 -04:00
|
|
|
char mesg[NSTRING];
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
bp = curbp; /* Cheap. */
|
|
|
|
bp->b_flag |= BFCHG; /* we have changed */
|
|
|
|
bp->b_flag &= ~BFINVS; /* and are not temporary */
|
|
|
|
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */
|
|
|
|
goto out;
|
|
|
|
if (s == FIOFNF) { /* File not found. */
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(No such file)");
|
2005-09-30 18:26:09 -04:00
|
|
|
return (FALSE);
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(Inserting file)");
|
|
|
|
|
|
|
|
#if CRYPT
|
|
|
|
s = resetkey();
|
|
|
|
if (s != TRUE)
|
2005-09-30 18:26:09 -04:00
|
|
|
return (s);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
/* back up a line and save the mark here */
|
|
|
|
curwp->w_dotp = lback(curwp->w_dotp);
|
|
|
|
curwp->w_doto = 0;
|
|
|
|
curwp->w_markp = curwp->w_dotp;
|
|
|
|
curwp->w_marko = 0;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
nline = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
lflag = FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
while ((s = ffgetline()) == FIOSUC) {
|
|
|
|
nbytes = strlen(fline);
|
|
|
|
if ((lp1 = lalloc(nbytes)) == NULL) {
|
|
|
|
s = FIOMEM; /* Keep message on the */
|
|
|
|
break; /* display. */
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
lp0 = curwp->w_dotp; /* line previous to insert */
|
|
|
|
lp2 = lp0->l_fp; /* line after insert */
|
|
|
|
|
|
|
|
/* re-link new line between lp0 and lp2 */
|
|
|
|
lp2->l_bp = lp1;
|
|
|
|
lp0->l_fp = lp1;
|
|
|
|
lp1->l_bp = lp0;
|
|
|
|
lp1->l_fp = lp2;
|
|
|
|
|
|
|
|
/* and advance and write out the current line */
|
|
|
|
curwp->w_dotp = lp1;
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < nbytes; ++i)
|
|
|
|
lputc(lp1, i, fline[i]);
|
|
|
|
++nline;
|
|
|
|
}
|
|
|
|
ffclose(); /* Ignore errors. */
|
2005-05-31 11:50:56 -04:00
|
|
|
curwp->w_markp = lforw(curwp->w_markp);
|
|
|
|
strcpy(mesg, "(");
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s == FIOERR) {
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(mesg, "I/O ERROR, ");
|
|
|
|
curbp->b_flag |= BFTRUNC;
|
|
|
|
}
|
|
|
|
if (s == FIOMEM) {
|
|
|
|
strcat(mesg, "OUT OF MEMORY, ");
|
|
|
|
curbp->b_flag |= BFTRUNC;
|
|
|
|
}
|
|
|
|
sprintf(&mesg[strlen(mesg)], "Inserted %d line", nline);
|
2005-09-30 18:26:09 -04:00
|
|
|
if (nline > 1)
|
2005-05-31 11:50:56 -04:00
|
|
|
strcat(mesg, "s");
|
|
|
|
strcat(mesg, ")");
|
|
|
|
mlwrite(mesg);
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
out:
|
2005-05-31 11:50:56 -04:00
|
|
|
/* advance to the next line and mark the window for changes */
|
|
|
|
curwp->w_dotp = lforw(curwp->w_dotp);
|
|
|
|
curwp->w_flag |= WFHARD | WFMODE;
|
|
|
|
|
|
|
|
/* copy window parameters back to the buffer structure */
|
|
|
|
curbp->b_dotp = curwp->w_dotp;
|
|
|
|
curbp->b_doto = curwp->w_doto;
|
|
|
|
curbp->b_markp = curwp->w_markp;
|
|
|
|
curbp->b_marko = curwp->w_marko;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s == FIOERR) /* False if error. */
|
|
|
|
return (FALSE);
|
|
|
|
return (TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|