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