Annotation of src/usr.bin/mg/file.c, Revision 1.14
1.14 ! vincent 1: /* $OpenBSD: file.c,v 1.13 2002/02/14 14:24:21 deraadt Exp $ */
1.5 niklas 2:
1.1 deraadt 3: /*
1.4 millert 4: * File commands.
1.1 deraadt 5: */
6:
1.6 art 7: #include <libgen.h>
1.4 millert 8: #include "def.h"
9:
1.1 deraadt 10: /*
1.4 millert 11: * Insert a file into the current buffer. Real easy - just call the
1.1 deraadt 12: * insertfile routine with the file name.
13: */
1.3 millert 14: /* ARGSUSED */
15: int
1.1 deraadt 16: fileinsert(f, n)
1.4 millert 17: int f, n;
1.1 deraadt 18: {
1.4 millert 19: int s;
20: char fname[NFILEN];
1.1 deraadt 21:
1.3 millert 22: s = eread("Insert file: ", fname, NFILEN, EFNEW | EFCR | EFFILE);
23: if (s != TRUE)
1.1 deraadt 24: return (s);
1.8 art 25: return insertfile(adjustname(fname), NULL, FALSE);
1.3 millert 26: /* don't set buffer name */
1.1 deraadt 27: }
28:
29: /*
1.9 mickey 30: * Select a file for editing. Look around to see if you can find the file
1.4 millert 31: * in another buffer; if you can find it, just switch to the buffer. If
1.9 mickey 32: * you cannot find the file, create a new buffer, read in the text, and
1.4 millert 33: * switch to the new buffer.
1.1 deraadt 34: */
1.3 millert 35: /* ARGSUSED */
36: int
1.1 deraadt 37: filevisit(f, n)
1.4 millert 38: int f, n;
1.1 deraadt 39: {
1.4 millert 40: BUFFER *bp;
41: int s;
42: char fname[NFILEN];
43: char *adjf;
1.1 deraadt 44:
1.3 millert 45: s = eread("Find file: ", fname, NFILEN, EFNEW | EFCR | EFFILE);
46: if (s != TRUE)
1.1 deraadt 47: return s;
48: adjf = adjustname(fname);
1.3 millert 49: if ((bp = findbuffer(adjf)) == NULL)
50: return FALSE;
1.1 deraadt 51: curbp = bp;
1.3 millert 52: if (showbuffer(bp, curwp, WFHARD) != TRUE)
53: return FALSE;
1.1 deraadt 54: if (bp->b_fname[0] == 0)
1.4 millert 55: return readin(adjf);
1.1 deraadt 56: return TRUE;
57: }
58:
59: /*
1.4 millert 60: * Pop to a file in the other window. Same as the last function, but uses
1.1 deraadt 61: * popbuf instead of showbuffer.
62: */
1.3 millert 63: /* ARGSUSED */
64: int
1.1 deraadt 65: poptofile(f, n)
1.4 millert 66: int f, n;
1.1 deraadt 67: {
1.4 millert 68: BUFFER *bp;
69: MGWIN *wp;
70: int s;
71: char fname[NFILEN];
72: char *adjf;
1.1 deraadt 73:
1.3 millert 74: if ((s = eread("Find file in other window: ", fname, NFILEN,
1.13 deraadt 75: EFNEW | EFCR | EFFILE)) != TRUE)
1.1 deraadt 76: return s;
77: adjf = adjustname(fname);
1.3 millert 78: if ((bp = findbuffer(adjf)) == NULL)
79: return FALSE;
80: if ((wp = popbuf(bp)) == NULL)
81: return FALSE;
1.1 deraadt 82: curbp = bp;
83: curwp = wp;
84: if (bp->b_fname[0] == 0)
1.4 millert 85: return readin(adjf);
1.1 deraadt 86: return TRUE;
87: }
88:
89: /*
90: * given a file name, either find the buffer it uses, or create a new
91: * empty buffer to put it in.
92: */
93: BUFFER *
1.14 ! vincent 94: findbuffer(char *fname)
1.1 deraadt 95: {
1.4 millert 96: BUFFER *bp;
1.10 vincent 97: char bname[NBUFN];
1.14 ! vincent 98: unsigned int count, remain, i;
1.1 deraadt 99:
1.3 millert 100: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.6 art 101: if (strcmp(bp->b_fname, fname) == 0)
1.1 deraadt 102: return bp;
103: }
1.14 ! vincent 104: i = strlcpy(bname, basename(fname), sizeof(bname));
! 105: remain = sizeof(bname) - i;
! 106: for (count = 2; bfind(bname, FALSE) != NULL; count++)
! 107: snprintf(&bname[i], remain, "<%d>", count);
1.10 vincent 108:
1.1 deraadt 109: return bfind(bname, TRUE);
110: }
111:
112: /*
1.9 mickey 113: * Read the file "fname" into the current buffer. Make all of the text
114: * in the buffer go away, after checking for unsaved changes. This is
115: * called by the "read" command, the "visit" command, and the mainline
1.4 millert 116: * (for "uemacs file").
1.1 deraadt 117: */
1.3 millert 118: int
119: readin(fname)
1.4 millert 120: char *fname;
1.3 millert 121: {
1.4 millert 122: MGWIN *wp;
123: int status;
1.1 deraadt 124:
1.4 millert 125: /* might be old */
126: if (bclear(curbp) != TRUE)
1.1 deraadt 127: return TRUE;
1.3 millert 128: status = insertfile(fname, fname, TRUE);
1.4 millert 129:
130: /* no change */
131: curbp->b_flag &= ~BFCHG;
1.3 millert 132: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 133: if (wp->w_bufp == curbp) {
1.3 millert 134: wp->w_dotp = wp->w_linep = lforw(curbp->b_linep);
135: wp->w_doto = 0;
1.1 deraadt 136: wp->w_markp = NULL;
137: wp->w_marko = 0;
138: }
139: }
140: return status;
141: }
1.4 millert 142:
1.1 deraadt 143: /*
144: * NB, getting file attributes is done here under control of a flag
145: * rather than in readin, which would be cleaner. I was concerned
146: * that some operating system might require the file to be open
147: * in order to get the information. Similarly for writing.
148: */
149:
150: /*
1.9 mickey 151: * Insert a file in the current buffer, after dot. Set mark at the end of
152: * the text inserted; point at the beginning. Return a standard status.
153: * Print a summary (lines read, error message) out as well. If the BACKUP
154: * conditional is set, then this routine also does the read end of backup
155: * processing. The BFBAK flag, if set in a buffer, says that a backup
156: * should be taken. It is set when a file is read in, but not on a new
1.4 millert 157: * file. (You don't need to make a backup copy of nothing.)
1.1 deraadt 158: */
1.9 mickey 159: static char *line = NULL;
160: static int linesize = 0;
1.1 deraadt 161:
1.3 millert 162: int
163: insertfile(fname, newname, needinfo)
1.4 millert 164: char *fname, *newname;
165: int needinfo;
1.3 millert 166: {
1.4 millert 167: BUFFER *bp;
168: LINE *lp1, *lp2;
169: LINE *olp; /* line we started at */
170: MGWIN *wp;
171: int nbytes, s, nline;
172: int opos; /* and offset into it */
173:
174: lp1 = NULL;
1.1 deraadt 175:
176: if (line == NULL) {
177: line = malloc(NLINE);
178: linesize = NLINE;
179: }
1.4 millert 180:
181: /* cheap */
182: bp = curbp;
1.8 art 183: if (newname != NULL)
1.10 vincent 184: (void)strlcpy(bp->b_fname, newname, sizeof bp->b_fname);
1.4 millert 185:
186: /* hard file open */
1.8 art 187: if ((s = ffropen(fname, needinfo ? bp : NULL)) == FIOERR)
1.1 deraadt 188: goto out;
1.4 millert 189: if (s == FIOFNF) {
190: /* file not found */
1.1 deraadt 191: if (newname != NULL)
192: ewprintf("(New file)");
1.3 millert 193: else
194: ewprintf("(File not found)");
1.1 deraadt 195: goto out;
196: }
197: opos = curwp->w_doto;
1.4 millert 198:
199: /* open a new line, at point, and start inserting after it */
1.7 art 200: (void)lnewline();
1.1 deraadt 201: olp = lback(curwp->w_dotp);
1.3 millert 202: if (olp == curbp->b_linep) {
1.1 deraadt 203: /* if at end of buffer, create a line to insert before */
1.7 art 204: (void)lnewline();
1.1 deraadt 205: curwp->w_dotp = lback(curwp->w_dotp);
206: }
1.4 millert 207:
208: /* don't count fake lines at the end */
209: nline = 0;
1.3 millert 210: while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
1.1 deraadt 211: doneread:
1.3 millert 212: switch (s) {
213: case FIOSUC:
214: ++nline;
215: /* and continue */
1.4 millert 216: case FIOEOF:
217: /* the last line of the file */
1.3 millert 218: if ((lp1 = lalloc(nbytes)) == NULL) {
1.4 millert 219: /* keep message on the display */
220: s = FIOERR;
221: goto endoffile;
1.3 millert 222: }
223: bcopy(line, <ext(lp1)[0], nbytes);
224: lp2 = lback(curwp->w_dotp);
225: lp2->l_fp = lp1;
226: lp1->l_fp = curwp->w_dotp;
227: lp1->l_bp = lp2;
228: curwp->w_dotp->l_bp = lp1;
229: if (s == FIOEOF)
230: goto endoffile;
231: break;
1.4 millert 232: case FIOLONG:{
233: /* a line too long to fit in our buffer */
1.9 mickey 234: char *cp;
235: int newsize;
1.3 millert 236:
237: newsize = linesize * 2;
238: if (newsize < 0 ||
1.4 millert 239: (cp = malloc((unsigned)newsize)) == NULL) {
1.3 millert 240: ewprintf("Could not allocate %d bytes",
1.4 millert 241: newsize);
242: s = FIOERR;
243: goto endoffile;
1.3 millert 244: }
245: bcopy(line, cp, linesize);
246: free(line);
247: line = cp;
1.9 mickey 248: s = ffgetline(line + linesize, linesize,
1.4 millert 249: &nbytes);
1.3 millert 250: nbytes += linesize;
251: linesize = newsize;
252: if (s == FIOERR)
253: goto endoffile;
254: goto doneread;
255: }
256: default:
257: ewprintf("Unknown code %d reading file", s);
258: s = FIOERR;
259: break;
1.1 deraadt 260: }
261: }
262: endoffile:
1.4 millert 263: /* ignore errors */
1.8 art 264: ffclose(NULL);
1.4 millert 265: /* don't zap an error */
266: if (s == FIOEOF) {
1.3 millert 267: if (nline == 1)
268: ewprintf("(Read 1 line)");
269: else
270: ewprintf("(Read %d lines)", nline);
1.1 deraadt 271: }
1.4 millert 272: /* set mark at the end of the text */
1.1 deraadt 273: curwp->w_dotp = curwp->w_markp = lback(curwp->w_dotp);
274: curwp->w_marko = llength(curwp->w_markp);
1.7 art 275: (void)ldelnewline();
1.1 deraadt 276: curwp->w_dotp = olp;
277: curwp->w_doto = opos;
1.3 millert 278: if (olp == curbp->b_linep)
279: curwp->w_dotp = lforw(olp);
1.1 deraadt 280: #ifndef NO_BACKUP
281: if (newname != NULL)
1.3 millert 282: bp->b_flag |= BFCHG | BFBAK; /* Need a backup. */
283: else
284: bp->b_flag |= BFCHG;
1.4 millert 285: #else /* !NO_BACKUP */
1.1 deraadt 286: bp->b_flag |= BFCHG;
1.4 millert 287: #endif /* !NO_BACKUP */
1.3 millert 288: /*
289: * if the insert was at the end of buffer, set lp1 to the end of
290: * buffer line, and lp2 to the beginning of the newly inserted text.
291: * (Otherwise lp2 is set to NULL.) This is used below to set
292: * pointers in other windows correctly if they are also at the end of
293: * buffer.
1.1 deraadt 294: */
295: lp1 = bp->b_linep;
296: if (curwp->w_markp == lp1) {
297: lp2 = curwp->w_dotp;
298: } else {
1.4 millert 299: /* delete extraneous newline */
1.7 art 300: (void)ldelnewline();
1.1 deraadt 301: out: lp2 = NULL;
302: }
1.3 millert 303: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 304: if (wp->w_bufp == curbp) {
1.3 millert 305: wp->w_flag |= WFMODE | WFEDIT;
1.1 deraadt 306: if (wp != curwp && lp2 != NULL) {
1.3 millert 307: if (wp->w_dotp == lp1)
308: wp->w_dotp = lp2;
309: if (wp->w_markp == lp1)
310: wp->w_markp = lp2;
311: if (wp->w_linep == lp1)
312: wp->w_linep = lp2;
1.1 deraadt 313: }
314: }
315: }
1.4 millert 316: /* return false if error */
317: return s != FIOERR;
1.1 deraadt 318: }
319:
320: /*
1.9 mickey 321: * Ask for a file name and write the contents of the current buffer to that
322: * file. Update the remembered file name and clear the buffer changed flag.
1.4 millert 323: * This handling of file names is different from the earlier versions and
324: * is more compatable with Gosling EMACS than with ITS EMACS.
1.1 deraadt 325: */
1.3 millert 326: /* ARGSUSED */
327: int
1.1 deraadt 328: filewrite(f, n)
1.4 millert 329: int f, n;
1.1 deraadt 330: {
1.4 millert 331: int s;
332: char fname[NFILEN];
333: char *adjfname;
1.1 deraadt 334:
1.3 millert 335: if ((s = eread("Write file: ", fname, NFILEN,
1.13 deraadt 336: EFNEW | EFCR | EFFILE)) != TRUE)
1.1 deraadt 337: return (s);
338: adjfname = adjustname(fname);
339: /* old attributes are no longer current */
340: bzero(&curbp->b_fi, sizeof(curbp->b_fi));
1.3 millert 341: if ((s = writeout(curbp, adjfname)) == TRUE) {
1.10 vincent 342: (void)strlcpy(curbp->b_fname, adjfname, sizeof curbp->b_fname);
1.1 deraadt 343: #ifndef NO_BACKUP
344: curbp->b_flag &= ~(BFBAK | BFCHG);
1.4 millert 345: #else /* !NO_BACKUP */
1.1 deraadt 346: curbp->b_flag &= ~BFCHG;
1.4 millert 347: #endif /* !NO_BACKUP */
1.1 deraadt 348: upmodes(curbp);
349: }
350: return s;
351: }
352:
353: /*
1.4 millert 354: * Save the contents of the current buffer back into its associated file.
1.1 deraadt 355: */
356: #ifndef NO_BACKUP
357: #ifndef MAKEBACKUP
358: #define MAKEBACKUP TRUE
1.4 millert 359: #endif /* !MAKEBACKUP */
1.9 mickey 360: static int makebackup = MAKEBACKUP;
1.4 millert 361: #endif /* !NO_BACKUP */
1.1 deraadt 362:
1.3 millert 363: /* ARGSUSED */
364: int
1.1 deraadt 365: filesave(f, n)
1.4 millert 366: int f, n;
1.1 deraadt 367: {
368: return buffsave(curbp);
369: }
370:
371: /*
1.9 mickey 372: * Save the contents of the buffer argument into its associated file. Do
373: * nothing if there have been no changes (is this a bug, or a feature?).
374: * Error if there is no remembered file name. If this is the first write
1.4 millert 375: * since the read or visit, then a backup copy of the file is made.
1.9 mickey 376: * Allow user to select whether or not to make backup files by looking at
1.4 millert 377: * the value of makebackup.
1.1 deraadt 378: */
1.3 millert 379: int
380: buffsave(bp)
381: BUFFER *bp;
382: {
1.4 millert 383: int s;
1.1 deraadt 384:
1.4 millert 385: /* return, no changes */
386: if ((bp->b_flag & BFCHG) == 0) {
1.1 deraadt 387: ewprintf("(No changes need to be saved)");
388: return TRUE;
389: }
1.4 millert 390:
391: /* must have a name */
392: if (bp->b_fname[0] == '\0') {
1.1 deraadt 393: ewprintf("No file name");
394: return (FALSE);
395: }
1.4 millert 396:
1.1 deraadt 397: #ifndef NO_BACKUP
1.3 millert 398: if (makebackup && (bp->b_flag & BFBAK)) {
1.1 deraadt 399: s = fbackupfile(bp->b_fname);
1.4 millert 400: /* hard error */
401: if (s == ABORT)
1.1 deraadt 402: return FALSE;
1.4 millert 403: /* softer error */
1.9 mickey 404: if (s == FALSE &&
1.4 millert 405: (s = eyesno("Backup error, save anyway")) != TRUE)
1.1 deraadt 406: return s;
407: }
1.4 millert 408: #endif /* !NO_BACKUP */
1.3 millert 409: if ((s = writeout(bp, bp->b_fname)) == TRUE) {
1.1 deraadt 410: #ifndef NO_BACKUP
411: bp->b_flag &= ~(BFCHG | BFBAK);
1.4 millert 412: #else /* !NO_BACKUP */
1.1 deraadt 413: bp->b_flag &= ~BFCHG;
1.4 millert 414: #endif /* !NO_BACKUP */
1.1 deraadt 415: upmodes(bp);
416: }
417: return s;
418: }
419:
420: #ifndef NO_BACKUP
1.3 millert 421: /*
422: * Since we don't have variables (we probably should) this is a command
423: * processor for changing the value of the make backup flag. If no argument
424: * is given, sets makebackup to true, so backups are made. If an argument is
425: * given, no backup files are made when saving a new version of a file. Only
426: * used when BACKUP is #defined.
1.1 deraadt 427: */
1.3 millert 428: /* ARGSUSED */
429: int
1.1 deraadt 430: makebkfile(f, n)
1.4 millert 431: int f, n;
1.1 deraadt 432: {
1.3 millert 433: if (f & FFARG)
434: makebackup = n > 0;
435: else
436: makebackup = !makebackup;
1.1 deraadt 437: ewprintf("Backup files %sabled", makebackup ? "en" : "dis");
438: return TRUE;
439: }
1.4 millert 440: #endif /* !NO_BACKUP */
1.1 deraadt 441:
442: /*
443: * NB: bp is passed to both ffwopen and ffclose because some
444: * attribute information may need to be updated at open time
445: * and others after the close. This is OS-dependent. Note
446: * that the ff routines are assumed to be able to tell whether
447: * the attribute information has been set up in this buffer
448: * or not.
449: */
450:
451: /*
1.9 mickey 452: * This function performs the details of file writing; writing the file
453: * in buffer bp to file fn. Uses the file management routines in the
1.4 millert 454: * "fileio.c" package. Most of the grief is checking of some sort.
1.1 deraadt 455: */
1.3 millert 456: int
457: writeout(bp, fn)
458: BUFFER *bp;
459: char *fn;
460: {
1.4 millert 461: int s;
1.1 deraadt 462:
1.4 millert 463: /* open writes message */
464: if ((s = ffwopen(fn, bp)) != FIOSUC)
1.1 deraadt 465: return (FALSE);
466: s = ffputbuf(bp);
1.4 millert 467: if (s == FIOSUC) {
468: /* no write error */
1.1 deraadt 469: s = ffclose(bp);
1.3 millert 470: if (s == FIOSUC)
1.1 deraadt 471: ewprintf("Wrote %s", fn);
1.4 millert 472: } else
473: /* ignore close error if it is a write error */
1.7 art 474: (void)ffclose(bp);
1.1 deraadt 475: return s == FIOSUC;
476: }
477:
478: /*
1.4 millert 479: * Tag all windows for bp (all windows if bp == NULL) as needing their
1.1 deraadt 480: * mode line updated.
481: */
1.7 art 482: void
1.3 millert 483: upmodes(bp)
484: BUFFER *bp;
485: {
1.4 millert 486: MGWIN *wp;
1.1 deraadt 487:
488: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
1.3 millert 489: if (bp == NULL || curwp->w_bufp == bp)
490: wp->w_flag |= WFMODE;
1.1 deraadt 491: }