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