#include <sys/queue.h>
#include <sys/stat.h>
#include <errno.h>
#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "def.h"
size_t xdirname(char *, const char *, size_t);
int
fileinsert(int f, int n)
{
char fname[NFILEN], *bufp, *adjf;
if (getbufcwd(fname, sizeof(fname)) != TRUE)
fname[0] = '\0';
bufp = eread("Insert file: ", fname, NFILEN,
EFNEW | EFCR | EFFILE | EFDEF);
if (bufp == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
adjf = adjustname(bufp, TRUE);
if (adjf == NULL)
return (FALSE);
return (insertfile(adjf, NULL, FALSE));
}
int
filevisit(int f, int n)
{
struct buffer *bp;
char fname[NFILEN], *bufp, *adjf;
int status;
if (getbufcwd(fname, sizeof(fname)) != TRUE)
fname[0] = '\0';
bufp = eread("Find file: ", fname, NFILEN,
EFNEW | EFCR | EFFILE | EFDEF);
if (bufp == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
adjf = adjustname(fname, TRUE);
if (adjf == NULL)
return (FALSE);
if (fisdir(adjf) == TRUE)
return (do_dired(adjf));
if ((bp = findbuffer(adjf)) == NULL)
return (FALSE);
curbp = bp;
if (showbuffer(bp, curwp, WFFULL) != TRUE)
return (FALSE);
if (bp->b_fname[0] == '\0') {
if ((status = readin(adjf)) != TRUE)
killbuffer(bp);
return (status);
}
return (TRUE);
}
int
filevisitalt(int f, int n)
{
char fname[NFILEN], *bufp;
if (getbufcwd(fname, sizeof(fname)) != TRUE)
fname[0] = '\0';
bufp = eread("Find alternate file: ", fname, NFILEN,
EFNEW | EFCR | EFFILE | EFDEF);
if (bufp == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
return (do_filevisitalt(fname));
}
int
do_filevisitalt(char *fn)
{
struct buffer *bp;
int status;
char *adjf;
status = killbuffer(curbp);
if (status == ABORT || status == FALSE)
return (ABORT);
adjf = adjustname(fn, TRUE);
if (adjf == NULL)
return (FALSE);
if (fisdir(adjf) == TRUE)
return (do_dired(adjf));
if ((bp = findbuffer(adjf)) == NULL)
return (FALSE);
curbp = bp;
if (showbuffer(bp, curwp, WFFULL) != TRUE)
return (FALSE);
if (bp->b_fname[0] == '\0') {
if ((status = readin(adjf)) != TRUE)
killbuffer(bp);
return (status);
}
return (TRUE);
}
int
filevisitro(int f, int n)
{
int error;
error = filevisit(f, n);
if (error != TRUE)
return (error);
curbp->b_flag |= BFREADONLY;
return (TRUE);
}
int
poptofile(int f, int n)
{
struct buffer *bp;
struct mgwin *wp;
char fname[NFILEN], *adjf, *bufp;
int status;
if (getbufcwd(fname, sizeof(fname)) != TRUE)
fname[0] = '\0';
if ((bufp = eread("Find file in other window: ", fname, NFILEN,
EFNEW | EFCR | EFFILE | EFDEF)) == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
adjf = adjustname(fname, TRUE);
if (adjf == NULL)
return (FALSE);
if (fisdir(adjf) == TRUE)
return (do_dired(adjf));
if ((bp = findbuffer(adjf)) == NULL)
return (FALSE);
if (bp == curbp)
return (splitwind(f, n));
if ((wp = popbuf(bp, WNONE)) == NULL)
return (FALSE);
curbp = bp;
curwp = wp;
if (bp->b_fname[0] == '\0') {
if ((status = readin(adjf)) != TRUE)
killbuffer(bp);
return (status);
}
return (TRUE);
}
int
readin(char *fname)
{
struct mgwin *wp;
struct stat statbuf;
int status, i, ro = FALSE;
PF *ael;
char dp[NFILEN];
if (bclear(curbp) != TRUE)
return (TRUE);
curbp->b_flag &= ~BFREADONLY;
if ((status = insertfile(fname, fname, TRUE)) != TRUE) {
dobeep();
ewprintf("File is not readable: %s", fname);
return (FALSE);
}
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
wp->w_dotp = wp->w_linep = bfirstlp(curbp);
wp->w_doto = 0;
wp->w_markp = NULL;
wp->w_marko = 0;
}
}
if ((ael = find_autoexec(fname)) != NULL) {
for (i = 0; ael[i] != NULL; i++)
(*ael[i])(0, 1);
free(ael);
}
curbp->b_flag &= ~BFCHG;
if (fisdir(fname) == TRUE) {
ro = TRUE;
} else if ((access(fname, W_OK) == -1)) {
if (errno != ENOENT) {
ro = TRUE;
} else if (errno == ENOENT) {
(void)xdirname(dp, fname, sizeof(dp));
(void)strlcat(dp, "/", sizeof(dp));
if (stat(dp, &statbuf) == -1 && errno == ENOENT) {
if (eyorn("Missing directory, create") == TRUE)
(void)do_makedir(dp);
} else if (access(dp, W_OK) == -1 && errno == EACCES) {
ewprintf("File not found and directory"
" write-protected");
ro = TRUE;
}
}
}
if (ro == TRUE)
curbp->b_flag |= BFREADONLY;
if (startrow) {
gotoline(FFARG, startrow);
startrow = 0;
}
undo_add_modified();
return (status);
}
static char *line = NULL;
static int linesize = 0;
int
insertfile(char *fname, char *newname, int replacebuf)
{
struct buffer *bp;
struct line *lp1, *lp2;
struct line *olp;
struct mgwin *wp;
int nbytes, s, nline = 0, siz, x, x2;
int opos;
int oline;
FILE *ffp;
if (replacebuf == TRUE)
x = undo_enable(FFRAND, 0);
else
x = undo_enabled();
lp1 = NULL;
if (line == NULL) {
line = malloc(NLINE);
if (line == NULL)
panic("out of memory");
linesize = NLINE;
}
bp = curbp;
if (newname != NULL) {
(void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname));
(void)xdirname(bp->b_cwd, newname, sizeof(bp->b_cwd));
(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
}
if ((s = ffropen(&ffp, fname, (replacebuf == TRUE) ? bp : NULL))
== FIOERR)
goto out;
if (s == FIOFNF) {
if (newname != NULL)
ewprintf("(New file)");
else
ewprintf("(File not found)");
goto out;
} else if (s == FIODIR) {
if (replacebuf == FALSE) {
dobeep();
ewprintf("Cannot insert: file is a directory, %s",
fname);
goto cleanup;
}
killbuffer(bp);
bp = dired_(fname);
undo_enable(FFRAND, x);
if (bp == NULL)
return (FALSE);
curbp = bp;
return (showbuffer(bp, curwp, WFFULL | WFMODE));
} else {
(void)xdirname(bp->b_cwd, fname, sizeof(bp->b_cwd));
(void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd));
}
opos = curwp->w_doto;
oline = curwp->w_dotline;
x2 = undo_enable(FFRAND, 0);
(void)lnewline();
olp = lback(curwp->w_dotp);
undo_enable(FFRAND, x2);
nline = 0;
siz = 0;
while ((s = ffgetline(ffp, line, linesize, &nbytes)) != FIOERR) {
retry:
siz += nbytes + 1;
switch (s) {
case FIOSUC:
case FIOEOF:
++nline;
if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOERR;
undo_add_insert(olp, opos,
siz - nbytes - 1 - 1);
goto endoffile;
}
bcopy(line, <ext(lp1)[0], nbytes);
lp2 = lback(curwp->w_dotp);
lp2->l_fp = lp1;
lp1->l_fp = curwp->w_dotp;
lp1->l_bp = lp2;
curwp->w_dotp->l_bp = lp1;
if (s == FIOEOF) {
undo_add_insert(olp, opos, siz - 1);
goto endoffile;
}
break;
case FIOLONG: {
char *cp;
int newsize;
newsize = linesize * 2;
if (newsize < 0 ||
(cp = malloc(newsize)) == NULL) {
dobeep();
ewprintf("Could not allocate %d bytes",
newsize);
s = FIOERR;
goto endoffile;
}
bcopy(line, cp, linesize);
free(line);
line = cp;
s = ffgetline(ffp, line + linesize, linesize,
&nbytes);
nbytes += linesize;
linesize = newsize;
if (s == FIOERR)
goto endoffile;
goto retry;
}
default:
dobeep();
ewprintf("Unknown code %d reading file", s);
s = FIOERR;
break;
}
}
endoffile:
(void)ffclose(ffp, NULL);
if (s == FIOEOF) {
if (nline == 1)
ewprintf("(Read 1 line)");
else
ewprintf("(Read %d lines)", nline);
}
curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
curwp->w_marko = llength(curwp->w_markp);
curwp->w_markline = oline + nline + 1;
if (lforw(curwp->w_dotp) == curwp->w_bufp->b_headp) {
curwp->w_bufp->b_lines--;
curwp->w_markline--;
} else
(void)ldelnewline();
curwp->w_dotp = olp;
curwp->w_doto = opos;
curwp->w_dotline = oline;
if (olp == curbp->b_headp)
curwp->w_dotp = lforw(olp);
if (newname != NULL)
bp->b_flag |= BFCHG | BFBAK;
else
bp->b_flag |= BFCHG;
lp1 = bp->b_headp;
if (curwp->w_markp == lp1) {
lp2 = curwp->w_dotp;
} else {
(void)ldelnewline();
out: lp2 = NULL;
}
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
wp->w_rflag |= WFMODE | WFEDIT;
if (wp != curwp && lp2 != NULL) {
if (wp->w_dotp == lp1)
wp->w_dotp = lp2;
if (wp->w_markp == lp1)
wp->w_markp = lp2;
if (wp->w_linep == lp1)
wp->w_linep = lp2;
}
}
}
bp->b_lines += nline;
cleanup:
undo_enable(FFRAND, x);
return (s != FIOERR);
}
int
filewrite(int f, int n)
{
struct stat statbuf;
int s;
char fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25];
char *adjfname, *bufp;
FILE *ffp;
if (getbufcwd(fname, sizeof(fname)) != TRUE)
fname[0] = '\0';
if ((bufp = eread("Write file: ", fname, NFILEN,
EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
return (ABORT);
else if (bufp[0] == '\0')
return (FALSE);
adjfname = adjustname(fname, TRUE);
if (adjfname == NULL)
return (FALSE);
if (stat(adjfname, &statbuf) == 0) {
if (S_ISDIR(statbuf.st_mode)) {
dobeep();
ewprintf("%s is a directory", adjfname);
return (FALSE);
}
snprintf(tmp, sizeof(tmp), "File `%s' exists; overwrite",
adjfname);
if ((s = eyorn(tmp)) != TRUE)
return (s);
}
bzero(&curbp->b_fi, sizeof(curbp->b_fi));
if ((s = writeout(&ffp, curbp, adjfname)) == TRUE) {
(void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
(void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
if (augbname(bn, curbp->b_fname, sizeof(bn))
== FALSE)
return (FALSE);
free(curbp->b_bname);
if ((curbp->b_bname = strdup(bn)) == NULL)
return (FALSE);
(void)fupdstat(curbp);
curbp->b_flag &= ~(BFBAK | BFCHG);
upmodes(curbp);
undo_add_boundary(FFRAND, 1);
undo_add_modified();
}
return (s);
}
static int makebackup = TRUE;
int
filesave(int f, int n)
{
if ((curbp->b_flag & BFCHG) == 0) {
ewprintf("(No changes need to be saved)");
return (TRUE);
}
if (curbp->b_fname[0] == '\0')
return (filewrite(f, n));
else
return (buffsave(curbp));
}
int
buffsave(struct buffer *bp)
{
int s;
FILE *ffp;
if (bp->b_fname[0] == '\0') {
dobeep();
ewprintf("No file name");
return (FALSE);
}
if (fchecktime(bp) != TRUE) {
if ((s = eyesno("File has changed on disk since last save. "
"Save anyway")) != TRUE)
return (s);
}
if (makebackup && (bp->b_flag & BFBAK)) {
s = fbackupfile(bp->b_fname);
if (s == ABORT)
return (FALSE);
if (s == FALSE &&
(s = eyesno("Backup error, save anyway")) != TRUE)
return (s);
}
if ((s = writeout(&ffp, bp, bp->b_fname)) == TRUE) {
(void)fupdstat(bp);
bp->b_flag &= ~(BFCHG | BFBAK);
upmodes(bp);
undo_add_boundary(FFRAND, 1);
undo_add_modified();
}
return (s);
}
int
makebkfile(int f, int n)
{
if (f & FFARG)
makebackup = n > 0;
else
makebackup = !makebackup;
ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
return (TRUE);
}
int
writeout(FILE ** ffp, struct buffer *bp, char *fn)
{
struct stat statbuf;
struct line *lpend;
int s, eobnl;
char dp[NFILEN];
if (stat(fn, &statbuf) == -1 && errno == ENOENT) {
errno = 0;
(void)xdirname(dp, fn, sizeof(dp));
(void)strlcat(dp, "/", sizeof(dp));
if (access(dp, W_OK) && errno == EACCES) {
dobeep();
ewprintf("Directory %s write-protected", dp);
return (FIOERR);
} else if (errno == ENOENT) {
dobeep();
ewprintf("%s: no such directory", dp);
return (FIOERR);
}
}
lpend = bp->b_headp;
eobnl = 0;
if (llength(lback(lpend)) != 0) {
eobnl = eyorn("No newline at end of file, add one");
if (eobnl != TRUE && eobnl != FALSE)
return (eobnl);
}
if ((s = ffwopen(ffp, fn, bp)) != FIOSUC)
return (FALSE);
s = ffputbuf(*ffp, bp, eobnl);
if (s == FIOSUC) {
s = ffclose(*ffp, bp);
if (s == FIOSUC)
ewprintf("Wrote %s", fn);
} else {
(void)ffclose(*ffp, bp);
dobeep();
ewprintf("Unable to write %s", fn);
}
return (s == FIOSUC);
}
void
upmodes(struct buffer *bp)
{
struct mgwin *wp;
for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
if (bp == NULL || curwp->w_bufp == bp)
wp->w_rflag |= WFMODE;
}
size_t
xdirname(char *dp, const char *path, size_t dplen)
{
char ts[NFILEN];
size_t len;
(void)strlcpy(ts, path, NFILEN);
len = strlcpy(dp, dirname(ts), dplen);
if (dplen > 0 && dp[0] == '/' && dp[1] == '\0') {
dp[0] = '\0';
len = 0;
}
return (len);
}
size_t
xbasename(char *bp, const char *path, size_t bplen)
{
char ts[NFILEN];
(void)strlcpy(ts, path, NFILEN);
return (strlcpy(bp, basename(ts), bplen));
}
int
do_dired(char *adjf)
{
struct buffer *bp;
if ((bp = dired_(adjf)) == FALSE)
return (FALSE);
curbp = bp;
return (showbuffer(bp, curwp, WFFULL | WFMODE));
}