/* * load - go load the file name we got passed. */ int load(const char *fname) { int s = TRUE, line; int nbytes = 0; char excbuf[128]; FILE *ffp; if ((fname = adjustname(fname, TRUE)) == NULL) /* just to be careful */ return (FALSE); if (ffropen(&ffp, fname, NULL) != FIOSUC) return (FALSE); line = 0; while ((s = ffgetline(ffp, excbuf, sizeof(excbuf) - 1, &nbytes)) == FIOSUC) { line++; excbuf[nbytes] = '\0'; if (excline(excbuf) != TRUE) { s = FIOERR; ewprintf("Error loading file %s at line %d", fname, line); break; } } (void)ffclose(ffp, NULL); excbuf[nbytes] = '\0'; if (s != FIOEOF || (nbytes && excline(excbuf) != TRUE)) return (FALSE); return (TRUE); }
int Dinsertfile(f,n) { int s,nline,nbytes; WINDOW *wp; char fname[NFILEN],*line; fname[0] = 0; if (mlreply("Insert file: ",fname,NFILEN) == FALSE) return FALSE; s = ffropen(fname); /* open file for reading */ switch (s) { case FIOFNF: mlwrite("File not found"); case FIOERR: return FALSE; } mlwrite("[Reading file]"); nline = 0; while ((s = ffgetline(&line,&nbytes)) == FIOSUC) { register char *p; for (p = line; nbytes; p++,nbytes--) { if (line_insert(1,*p) == FALSE) return FALSE; } if (random_newline(FALSE,1) == FALSE) return FALSE; ++nline; } ffclose(); if (s == FIOEOF) { /* Don't zap message! */ if (nline == 1) mlwrite("[Read 1 line]"); else mlwrite("[Read %d lines]", nline); } for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) { if (wp->w_bufp == curbp) { wp->w_flag |= WFMODE|WFHARD; } } return s != FIOERR; }
/* * Insert file "fname" into the current buffer, Called by insert file command. * Return the final status of the read. */ int ifile(char fname[]) { LINE *lp0, *lp1, *lp2; BUFFER *bp; char line[NLINE]; int i, s, nbytes; int nline = 0; int lflag; /* any lines longer than allowed? */ bp = curbp; /* Cheap */ bp->b_flag |= BFCHG; /* we have changed */ bp->b_flag &= ~BFTEMP; /* and are not temporary */ if ((s = ffropen(fname)) == FIOERR) /* Hard file open */ goto out; if (s == FIOFNF) { /* File not found */ mlwrite("[No such file]"); return (FALSE); } mlwrite("[Inserting file]"); /* 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; lflag = FALSE; while ((s = ffgetline(line, NLINE)) == FIOSUC || s == FIOLNG) { if (s == FIOLNG) lflag = TRUE; nbytes = strlen(line); if ((lp1 = lalloc(nbytes)) == NULL) { s = FIOERR; /* keep message on the */ break; /* display */ } 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; for (i = 0; i < nbytes; ++i) lputc(lp1, i, line[i]); ++nline; } ffclose(); /* Ignore errors */ curwp->w_markp = lforw(curwp->w_markp); if (s == FIOEOF) { /* Don't zap message! */ if (nline != 1) mlwrite("[Inserted %d lines]", nline); else mlwrite("[Inserted 1 line]"); } if (lflag) { if (nline != 1) mlwrite("[Inserted %d lines: Long lines wrapped]", nline); else mlwrite("[Inserted 1 line: Long lines wrapped]"); } out: /* advance to the next line and mark the window for changes */ curwp->w_dotp = lforw(curwp->w_dotp); curwp->w_flag |= WFHARD; /* 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; /* we need to update number of lines in the buffer */ curwp->w_bufp->b_lines += nline; if (s == FIOERR) /* False if error */ return (FALSE); return (TRUE); }
/* * 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. */ int readin(char fname[]) { LINE *lp1, *lp2; WINDOW *wp; BUFFER *bp; char line[NLINE]; int nbytes, s, i; int nline = 0; /* initialize here to silence a gcc warning */ int lflag; /* any lines longer than allowed? */ bp = curbp; /* Cheap */ if ((s = bclear(bp)) != TRUE) /* Might be old */ return (s); bp->b_flag &= ~(BFTEMP | BFCHG); strncpy(bp->b_fname, fname, NFILEN); if ((s = ffropen(fname)) == FIOERR) /* Hard file open */ goto out; if (s == FIOFNF) { /* File not found */ mlwrite("[New file]"); goto out; } mlwrite("[Reading file]"); lflag = FALSE; while ((s = ffgetline(line, NLINE)) == FIOSUC || s == FIOLNG) { if (s == FIOLNG) lflag = TRUE; nbytes = strlen(line); if ((lp1 = lalloc(nbytes)) == NULL) { s = FIOERR; /* Keep message on the display */ break; } 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, line[i]); ++nline; } ffclose(); /* Ignore errors */ if (s == FIOEOF) { /* Don't zap message! */ if (nline != 1) mlwrite("[Read %d lines]", nline); else mlwrite("[Read 1 line]"); } if (lflag) { if (nline != 1) mlwrite("[Read %d lines: Long lines wrapped]", nline); else mlwrite("[Read 1 line: Long lines wrapped]"); } curwp->w_bufp->b_lines = nline; out: 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); return (TRUE); }
int insertfile(char *fname, char *newname, int replacebuf) { struct buffer *bp; struct line *lp1, *lp2; struct line *olp; /* line we started at */ struct mgwin *wp; int nbytes, s, nline = 0, siz, x, x2; int opos; /* offset we started at */ int oline; /* original line number */ char *dp; 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; } /* cheap */ bp = curbp; if (newname != NULL) { (void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname)); dp = xdirname(newname); (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd)); (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd)); free(dp); } /* hard file open */ if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR) goto out; if (s == FIOFNF) { /* file not found */ if (newname != NULL) ewprintf("(New file)"); else ewprintf("(File not found)"); goto out; } else if (s == FIODIR) { /* file was a directory */ if (replacebuf == FALSE) { ewprintf("Cannot insert: file is a directory, %s", fname); goto cleanup; } killbuffer(bp); if ((bp = dired_(fname)) == NULL) return (FALSE); undo_enable(FFRAND, x); curbp = bp; return (showbuffer(bp, curwp, WFFULL | WFMODE)); } else { dp = xdirname(fname); (void)strlcpy(bp->b_cwd, dp, sizeof(bp->b_cwd)); (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd)); free(dp); } opos = curwp->w_doto; oline = curwp->w_dotline; /* * Open a new line at dot and start inserting after it. * We will delete this newline after insertion. * Disable undo, as we create the undo record manually. */ x2 = undo_enable(FFRAND, 0); (void)lnewline(); olp = lback(curwp->w_dotp); undo_enable(FFRAND, x2); nline = 0; siz = 0; while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) { retry: siz += nbytes + 1; switch (s) { case FIOSUC: /* FALLTHRU */ case FIOEOF: ++nline; if ((lp1 = lalloc(nbytes)) == NULL) { /* keep message on the display */ 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: { /* a line too long to fit in our buffer */ char *cp; int newsize; newsize = linesize * 2; if (newsize < 0 || (cp = malloc(newsize)) == NULL) { ewprintf("Could not allocate %d bytes", newsize); s = FIOERR; goto endoffile; } bcopy(line, cp, linesize); free(line); line = cp; s = ffgetline(line + linesize, linesize, &nbytes); nbytes += linesize; linesize = newsize; if (s == FIOERR) goto endoffile; goto retry; } default: ewprintf("Unknown code %d reading file", s); s = FIOERR; break; } } endoffile: /* ignore errors */ ffclose(NULL); /* don't zap an error */ if (s == FIOEOF) { if (nline == 1) ewprintf("(Read 1 line)"); else ewprintf("(Read %d lines)", nline); } /* set mark at the end of the text */ curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp); curwp->w_marko = llength(curwp->w_markp); curwp->w_markline = oline + nline + 1; /* * if we are at the end of the file, ldelnewline is a no-op, * but we still need to decrement the line and markline counts * as we've accounted for this fencepost in our arithmetic */ 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; /* Need a backup. */ else bp->b_flag |= BFCHG; /* * If the insert was at the end of buffer, set lp1 to the end of * buffer line, and lp2 to the beginning of the newly inserted text. * (Otherwise lp2 is set to NULL.) This is used below to set * pointers in other windows correctly if they are also at the end of * buffer. */ lp1 = bp->b_headp; if (curwp->w_markp == lp1) { lp2 = curwp->w_dotp; } else { /* delete extraneous newline */ (void)ldelnewline(); out: lp2 = NULL; } for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { if (wp->w_bufp == curbp) { wp->w_flag |= 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 FALSE if error */ return (s != FIOERR); }
/* * 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) { struct line *lp1; struct line *lp2; int i; struct window *wp; struct buffer *bp; int s; int nbytes; int nline; 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 return ABORT; #endif #endif #if CRYPT s = resetkey(); if (s != TRUE) return s; #endif bp = curbp; /* Cheap. */ if ((s = bclear(bp)) != TRUE) /* Might be old. */ return s; bp->b_flag &= ~(BFINVS | BFCHG); strcpy(bp->b_fname, fname); /* let a user macro get hold of things...if he wants */ execute(META | SPEC | 'R', FALSE, 1); if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */ goto out; if (s == FIOFNF) { /* File not found. */ mlwrite("(New file)"); goto out; } /* read the file in */ mlwrite("(Reading file)"); nline = 0; while ((s = ffgetline()) == FIOSUC) { nbytes = strlen(fline); if ((lp1 = lalloc(nbytes)) == NULL) { s = FIOMEM; /* Keep message on the */ break; /* display. */ } #if PKCODE if (nline > MAXNLINE) { s = FIOMEM; break; } #endif 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. */ strcpy(mesg, "("); if (s == FIOERR) { 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); if (nline != 1) strcat(mesg, "s"); strcat(mesg, ")"); mlwrite(mesg); out: 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; return TRUE; }
/* * Insert file "fname" into the current * buffer, Called by insert file command. Return the final * status of the read. */ int ifile(char *fname) { struct line *lp0; struct line *lp1; struct line *lp2; int i; struct buffer *bp; int s; int nbytes; int nline; char mesg[NSTRING]; 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. */ mlwrite("(No such file)"); return FALSE; } mlwrite("(Inserting file)"); #if CRYPT s = resetkey(); if (s != TRUE) return s; #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; nline = 0; while ((s = ffgetline()) == FIOSUC) { nbytes = strlen(fline); if ((lp1 = lalloc(nbytes)) == NULL) { s = FIOMEM; /* Keep message on the */ break; /* display. */ } 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; for (i = 0; i < nbytes; ++i) lputc(lp1, i, fline[i]); ++nline; } ffclose(); /* Ignore errors. */ curwp->w_markp = lforw(curwp->w_markp); strcpy(mesg, "("); if (s == FIOERR) { 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); if (nline > 1) strcat(mesg, "s"); strcat(mesg, ")"); mlwrite(mesg); out: /* 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; if (s == FIOERR) /* False if error. */ return FALSE; return TRUE; }