home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / newemacs / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  7.7 KB  |  313 lines

  1. /*
  2.  * The routines in this file
  3.  * handle the reading and writing of
  4.  * disk files. All of details about the
  5.  * reading and writing of the disk are
  6.  * in "fileio.c".
  7.  */
  8. #include    <stdio.h>
  9. #include    "ed.h"
  10.  
  11. /*
  12.  * Read a file into the current
  13.  * buffer. This is really easy; all you do it
  14.  * find the name of the file, and call the standard
  15.  * "read a file into the current buffer" code.
  16.  * Bound to "C-X C-R".
  17.  */
  18. fileread(f, n)
  19. {
  20.     register int    s;
  21.     char        fname[NFILEN];
  22.  
  23.     if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  24.         return (s);
  25.     return (readin(fname));
  26. }
  27.  
  28. /*
  29.  * Select a file for editing.
  30.  * Look around to see if you can find the
  31.  * fine in another buffer; if you can find it
  32.  * just switch to the buffer. If you cannot find
  33.  * the file, create a new buffer, read in the
  34.  * text, and switch to the new buffer.
  35.  * Bound to C-X C-V.
  36.  */
  37.  
  38. filevisit(f, n)
  39. {
  40.     register int s;
  41.     char fname[NFILEN];
  42.  
  43.     if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
  44.         return (s);
  45.     s = flvisit(fname);
  46.     return(s);
  47. }
  48.  
  49. flvisit(fname)
  50. char *fname;
  51.     {
  52.     register BUFFER    *bp;
  53.     register WINDOW    *wp;
  54.     register LINE    *lp;
  55.     register int    i;
  56.     register int    s;
  57.     char        bname[NBUFN];
  58.  
  59.     for (bp=bheadp; bp; bp=bp->b_bufp) {
  60.         if (!(bp->b_flag&BFTEMP) && !strcmp(bp->b_fname, fname)) {
  61.             if (!--curbp->b_nwnd) {
  62.                 curbp->b_dotp  = curwp->w_dotp;
  63.                 curbp->b_doto  = curwp->w_doto;
  64.                 curbp->b_markp = curwp->w_markp;
  65.                 curbp->b_marko = curwp->w_marko;
  66.             }
  67.             curbp = bp;
  68.             curwp->w_bufp  = bp;
  69.             if (!bp->b_nwnd++) {
  70.                 curwp->w_dotp  = bp->b_dotp;
  71.                 curwp->w_doto  = bp->b_doto;
  72.                 curwp->w_markp = bp->b_markp;
  73.                 curwp->w_marko = bp->b_marko;
  74.             } else {
  75.                 for (wp = wheadp; wp; wp = wp->w_wndp) {
  76.                     if (wp!=curwp && wp->w_bufp==bp) {
  77.                         curwp->w_dotp  = wp->w_dotp;
  78.                         curwp->w_doto  = wp->w_doto;
  79.                         curwp->w_markp = wp->w_markp;
  80.                         curwp->w_marko = wp->w_marko;
  81.                         break;
  82.                     }
  83.                 }
  84.             }
  85.             lp = curwp->w_dotp;
  86.             i = curwp->w_ntrows/2;
  87.             while (i-- && lback(lp)!=curbp->b_linep)
  88.                 lp = lback(lp);
  89.             curwp->w_linep = lp;
  90.             curwp->w_flag |= WFMODE|WFHARD;
  91.             mlwrite("[Old buffer]");
  92.             return (TRUE);
  93.         }
  94.     }
  95.     makename(bname, fname);            /* New buffer name.    */
  96.     while (bp=bfind(bname, FALSE, 0)) {
  97.         s = mlreply("Buffer name: ", bname, NBUFN);
  98.         if (s == ABORT)            /* ^G to just quit    */
  99.             return (s);
  100.         if (s == FALSE) {        /* CR to clobber it    */
  101.             makename(bname, fname);
  102.             break;
  103.         }
  104.     }
  105.     if (!bp && !(bp=bfind(bname, TRUE, 0))) {
  106.         mlwrite("Cannot create buffer");
  107.         return (FALSE);
  108.     }
  109.     if (!--curbp->b_nwnd) {        /* Undisplay.        */
  110.         curbp->b_dotp = curwp->w_dotp;
  111.         curbp->b_doto = curwp->w_doto;
  112.         curbp->b_markp = curwp->w_markp;
  113.         curbp->b_marko = curwp->w_marko;
  114.     }
  115.     curbp = bp;                /* Switch to it.    */
  116.     curwp->w_bufp = bp;
  117.     curbp->b_nwnd++;
  118.     return (readin(fname));            /* Read it in.        */
  119. }
  120.  
  121. /*
  122.  * Read file "fname" into the current
  123.  * buffer, blowing away any text found there. Called
  124.  * by both the read and visit commands. Return the final
  125.  * status of the read. Also called by the mainline,
  126.  * to read in a file specified on the command line as
  127.  * an argument.
  128.  */
  129. readin(fname)
  130. char    fname[];
  131. {
  132.     register LINE    *lp1;
  133.     register LINE    *lp2;
  134.     register int    i;
  135.     register WINDOW    *wp;
  136.     register BUFFER    *bp;
  137.     register int    s;
  138.     register int    nbytes;
  139.     register int    nline;
  140.     char        line[NLINE];
  141.  
  142.     bp = curbp;                /* Cheap.        */
  143.     if ((s=bclear(bp)) != TRUE)        /* Might be old.    */
  144.         return (s);
  145.     bp->b_flag &= ~(BFTEMP|BFCHG);
  146.     strcpy(bp->b_fname, fname);
  147.     if ((s=ffropen(fname)) == FIOERR)     /* Hard file open.    */
  148.         goto out;
  149.     if (s == FIOFNF) {            /* File not found.    */
  150.         mlwrite("[New file]");
  151.         goto out;
  152.     }
  153.     mlwrite("[Reading file]");
  154.     for (nline = 0; (s=ffgetline(line, NLINE)) == FIOSUC; ++nline) {
  155.         nbytes = strlen(line);
  156.         if (!(lp1=lalloc(nbytes))) {
  157.             s = FIOERR;        /* Keep message on the    */
  158.             break;            /* display.        */
  159.         }
  160.         lp2 = lback(curbp->b_linep);
  161.         lp2->l_fp = lp1;
  162.         lp1->l_fp = curbp->b_linep;
  163.         lp1->l_bp = lp2;
  164.         curbp->b_linep->l_bp = lp1;
  165.         for (i=0; i<nbytes; ++i) lputc(lp1, i, line[i]);
  166.     }
  167.     ffclose();                /* Ignore errors.    */
  168.     if (s == FIOEOF) {            /* Don't zap message!    */
  169.         if (nline == 1)
  170.             mlwrite("[Read 1 line]");
  171.         else
  172.             mlwrite("[Read %d lines]", nline);
  173.     }
  174. out:
  175.     for (wp=wheadp; wp; wp=wp->w_wndp) {
  176.         if (wp->w_bufp == curbp) {
  177.             wp->w_linep = lforw(curbp->b_linep);
  178.             wp->w_dotp  = lforw(curbp->b_linep);
  179.             wp->w_doto  = wp->w_markp = wp->w_marko = 0;
  180.             wp->w_flag |= WFMODE|WFHARD;
  181.         }
  182.     }
  183.     if (s == FIOERR)            /* False if error.    */
  184.         return (FALSE);
  185.     return (TRUE);
  186. }
  187.  
  188. /*
  189.  * Take a file name, and from it
  190.  * fabricate a buffer name. This routine knows
  191.  * about the syntax of file names on the target system.
  192.  * I suppose that this information could be put in
  193.  * a better place than a line of code.
  194.  */
  195. makename(bname, fname)
  196. char    bname[];
  197. char    fname[];
  198. {
  199.     register char    *cp1;
  200.     register char    *cp2;
  201.  
  202.     for (cp1 = fname; *cp1; ++cp1) {};
  203.     while (cp1!=fname && cp1[-1]!=':' && cp1[-1]!='\\') --cp1;
  204.     cp2 = bname;
  205.     while (cp2!=bname+NBUFN-1 && *cp1 && *cp1!=';') *cp2++ = *cp1++;
  206.     *cp2 = 0;
  207. }
  208.  
  209. /*
  210.  * Ask for a file name, and write the
  211.  * contents of the current buffer to that file.
  212.  * Update the remembered file name and clear the
  213.  * buffer changed flag. This handling of file names
  214.  * is different from the earlier versions, and
  215.  * is more compatable with Gosling EMACS than
  216.  * with ITS EMACS. Bound to "C-X C-W".
  217.  */
  218. filewrite(f, n)
  219. {
  220.     register WINDOW    *wp;
  221.     register int    s;
  222.     char        fname[NFILEN];
  223.  
  224.     if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  225.         return (s);
  226.     if ((s=writeout(fname)) == TRUE) {
  227.         strcpy(curbp->b_fname, fname);
  228.         curbp->b_flag &= ~BFCHG;
  229.  
  230.         /* Update mode lines */
  231.  
  232.         for (wp = wheadp; wp; wp = wp->w_wndp) {
  233.             if (wp->w_bufp == curbp) wp->w_flag |= WFMODE;
  234.         }
  235.     }
  236.     return (s);
  237. }
  238.  
  239. /*
  240.  * Save the contents of the current
  241.  * buffer in its associated file. Does nothing
  242.  * if nothing has changed (this may be a bug, not a
  243.  * feature). Error if there is no remembered file
  244.  * name for the buffer. Bound to "C-X C-S". May
  245.  * get called by "C-Z".
  246.  */
  247. filesave(f, n)
  248. {
  249.     register WINDOW    *wp;
  250.     register int    s;
  251.  
  252.     if (!(curbp->b_flag&BFCHG)) {        /* Return, no changes.    */
  253.         mlwrite("No changes - no need to write");
  254.         return (TRUE);
  255.     }
  256.     if (!*curbp->b_fname) {        /* Must have a name.    */
  257.         mlwrite("No file name");
  258.         return (FALSE);
  259.     }
  260.     if ((s=writeout(curbp->b_fname)) == TRUE) {
  261.         curbp->b_flag &= ~BFCHG;
  262.  
  263.         /* Update mode lines */
  264.  
  265.         for (wp = wheadp; wp; wp = wp->w_wndp) {
  266.             if (wp->w_bufp == curbp)
  267.                 wp->w_flag |= WFMODE;
  268.         }
  269.     }
  270.     return (s);
  271. }
  272.  
  273. /*
  274.  * This function performs the details of file
  275.  * writing. Uses the file management routines in the
  276.  * "fileio.c" package. The number of lines written is
  277.  * displayed. Sadly, it looks inside a LINE; provide
  278.  * a macro for this. Most of the grief is error
  279.  * checking of some sort.
  280.  */
  281. writeout(fn)
  282. char    *fn;
  283. {
  284.     register int    s;
  285.     register LINE    *lp;
  286.     register int    nline;
  287.  
  288.     if ((s=ffwopen(fn)) != FIOSUC)        /* Open writes message.    */
  289.         return (FALSE);
  290.     lp = lforw(curbp->b_linep);        /* First line.        */
  291.  
  292.     /* Number of lines.    */
  293.  
  294.     for (nline = 0; lp != curbp->b_linep; ++nline) {
  295.         if ((s=ffputline(lp->l_text, llength(lp))) != FIOSUC)
  296.             break;
  297.         lp = lforw(lp);
  298.     }
  299.     if (s == FIOSUC) {            /* No write error.    */
  300.         s = weof();            /* write eof mark       */
  301.         s = ffclose();
  302.         if (s == FIOSUC) {        /* No close error.    */
  303.             if (nline == 1)
  304.                 mlwrite("[Wrote 1 line]");
  305.             else
  306.                 mlwrite("[Wrote %d lines]", nline);
  307.         }
  308.     } else                    /* Ignore close error    */
  309.         ffclose();            /* if a write error.    */
  310.     return (s == FIOSUC);            /* Some sort of error.    */
  311. }
  312.  
  313.