home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 100-199 / ff119.lzh / MicroEMACS / src / src.zoo / file.c < prev    next >
C/C++ Source or Header  |  1987-12-09  |  18KB  |  623 lines

  1. /*    FILE.C:   for MicroEMACS
  2.  
  3.     The routines in this file handle the reading, writing
  4.     and lookup of disk files.  All of details about the
  5.     reading and writing of the disk are in "fileio.c".
  6.  
  7. */
  8.  
  9. #include        <stdio.h>
  10. #include    "estruct.h"
  11. #include        "edef.h"
  12.  
  13. /*
  14.  * Read a file into the current
  15.  * buffer. This is really easy; all you do it
  16.  * find the name of the file, and call the standard
  17.  * "read a file into the current buffer" code.
  18.  * Bound to "C-X C-R".
  19.  */
  20. fileread(f, n)
  21. {
  22.         register int    s;
  23.         char fname[NFILEN];
  24.  
  25.     if (restflag)        /* don't allow this command if restricted */
  26.         return(resterr());
  27.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  28.                 return(s);
  29.         return(readin(fname, TRUE));
  30. }
  31.  
  32. /*
  33.  * Insert a file into the current
  34.  * buffer. This is really easy; all you do it
  35.  * find the name of the file, and call the standard
  36.  * "insert a file into the current buffer" code.
  37.  * Bound to "C-X C-I".
  38.  */
  39. insfile(f, n)
  40. {
  41.         register int    s;
  42.         char fname[NFILEN];
  43.  
  44.     if (restflag)        /* don't allow this command if restricted */
  45.         return(resterr());
  46.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  47.         return(rdonly());    /* we are in read only mode    */
  48.         if ((s=mlreply("Insert file: ", fname, NFILEN)) != TRUE)
  49.                 return(s);
  50.         return(ifile(fname));
  51. }
  52.  
  53. /*
  54.  * Select a file for editing.
  55.  * Look around to see if you can find the
  56.  * fine in another buffer; if you can find it
  57.  * just switch to the buffer. If you cannot find
  58.  * the file, create a new buffer, read in the
  59.  * text, and switch to the new buffer.
  60.  * Bound to C-X C-F.
  61.  */
  62. filefind(f, n)
  63. {
  64.         char fname[NFILEN];    /* file user wishes to find */
  65.         register int s;        /* status return */
  66.  
  67.     if (restflag)        /* don't allow this command if restricted */
  68.         return(resterr());
  69.         if ((s=mlreply("Find file: ", fname, NFILEN)) != TRUE)
  70.                 return(s);
  71.     return(getfile(fname, TRUE));
  72. }
  73.  
  74. viewfile(f, n)    /* visit a file in VIEW mode */
  75. {
  76.         char fname[NFILEN];    /* file user wishes to find */
  77.         register int s;        /* status return */
  78.     register WINDOW *wp;    /* scan for windows that need updating */
  79.  
  80.     if (restflag)        /* don't allow this command if restricted */
  81.         return(resterr());
  82.         if ((s=mlreply("View file: ", fname, NFILEN)) != TRUE)
  83.                 return (s);
  84.     s = getfile(fname, FALSE);
  85.     if (s) {    /* if we succeed, put it in view mode */
  86.         curwp->w_bufp->b_mode |= MDVIEW;
  87.  
  88.         /* scan through and update mode lines of all windows */
  89.         wp = wheadp;
  90.         while (wp != NULL) {
  91.             wp->w_flag |= WFMODE;
  92.             wp = wp->w_wndp;
  93.         }
  94.     }
  95.     return(s);
  96. }
  97.  
  98. #if    CRYPT
  99. resetkey()    /* reset the encryption key if needed */
  100.  
  101. {
  102.     register int s;    /* return status */
  103.  
  104.     /* turn off the encryption flag */
  105.     cryptflag = FALSE;
  106.  
  107.     /* if we are in crypt mode */
  108.     if (curbp->b_mode & MDCRYPT) {
  109.         if (curbp->b_key[0] == 0) {
  110.             s = setkey(FALSE, 0);
  111.             if (s != TRUE)
  112.                 return(s);
  113.         }
  114.  
  115.         /* let others know... */
  116.         cryptflag = TRUE;
  117.  
  118.         /* and set up the key to be used! */
  119.         /* de-encrypt it */
  120.         crypt((char *)NULL, 0);
  121.         crypt(curbp->b_key, strlen(curbp->b_key));
  122.  
  123.         /* re-encrypt it...seeding it to start */
  124.         crypt((char *)NULL, 0);
  125.         crypt(curbp->b_key, strlen(curbp->b_key));
  126.     }
  127.  
  128.     return(TRUE);
  129. }
  130. #endif
  131.  
  132. getfile(fname, lockfl)
  133.  
  134. char fname[];        /* file name to find */
  135. int lockfl;        /* check the file for locks? */
  136.  
  137. {
  138.         register BUFFER *bp;
  139.         register LINE   *lp;
  140.         register int    i;
  141.         register int    s;
  142.         char bname[NBUFN];    /* buffer name to put file */
  143.  
  144. #if    MSDOS
  145.     mklower(fname);        /* msdos isn't case sensitive */
  146. #endif
  147.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  148.                 if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  149.             swbuffer(bp);
  150.                         lp = curwp->w_dotp;
  151.                         i = curwp->w_ntrows/2;
  152.                         while (i-- && lback(lp)!=curbp->b_linep)
  153.                                 lp = lback(lp);
  154.                         curwp->w_linep = lp;
  155.                         curwp->w_flag |= WFMODE|WFHARD;
  156.                         mlwrite("[Old buffer]");
  157.                         return (TRUE);
  158.                 }
  159.         }
  160.         makename(bname, fname);                 /* New buffer name.     */
  161.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  162.         /* old buffer name conflict code */
  163.                 s = mlreply("Buffer name: ", bname, NBUFN);
  164.                 if (s == ABORT)                 /* ^G to just quit      */
  165.                         return (s);
  166.                 if (s == FALSE) {               /* CR to clobber it     */
  167.                         makename(bname, fname);
  168.                         break;
  169.                 }
  170.         }
  171.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  172.                 mlwrite("Cannot create buffer");
  173.                 return (FALSE);
  174.         }
  175.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  176.                 curbp->b_dotp = curwp->w_dotp;
  177.                 curbp->b_doto = curwp->w_doto;
  178.                 curbp->b_markp = curwp->w_markp;
  179.                 curbp->b_marko = curwp->w_marko;
  180.         }
  181.         curbp = bp;                             /* Switch to it.        */
  182.         curwp->w_bufp = bp;
  183.         curbp->b_nwnd++;
  184.         return(readin(fname, lockfl));          /* Read it in.          */
  185. }
  186.  
  187. /*
  188.     Read file "fname" into the current buffer, blowing away any text
  189.     found there.  Called by both the read and find commands.  Return
  190.     the final status of the read.  Also called by the mainline, to
  191.     read in a file specified on the command line as an argument. 
  192.     The command bound to M-FNR is called after the buffer is set up
  193.     and before it is read. 
  194. */
  195.  
  196. readin(fname, lockfl)
  197.  
  198. char    fname[];    /* name of file to read */
  199. int    lockfl;        /* check for file locks? */
  200.  
  201. {
  202.         register LINE   *lp1;
  203.         register LINE   *lp2;
  204.         register int    i;
  205.         register WINDOW *wp;
  206.         register BUFFER *bp;
  207.         register int    s;
  208.         register int    nbytes;
  209.         register int    nline;
  210.     int        lflag;        /* any lines longer than allowed? */
  211.     char mesg[NSTRING];
  212.  
  213. #if    FILOCK
  214.     if (lockfl && lockchk(fname) == ABORT)
  215.         return(ABORT);
  216. #endif
  217. #if    CRYPT
  218.     s = resetkey();
  219.     if (s != TRUE)
  220.         return(s);
  221. #endif
  222.         bp = curbp;                             /* Cheap.               */
  223.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  224.                 return (s);
  225.         bp->b_flag &= ~(BFINVS|BFCHG);
  226.         strcpy(bp->b_fname, fname);
  227.  
  228.     /* let a user macro get hold of things...if he wants */
  229.     execute(META|SPEC|'R', FALSE, 1);
  230.  
  231.     /* turn off ALL keyboard translation in case we get a dos error */
  232.     TTkclose();
  233.  
  234.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  235.                 goto out;
  236.  
  237.         if (s == FIOFNF) {                      /* File not found.      */
  238.                 mlwrite("[New file]");
  239.                 goto out;
  240.         }
  241.  
  242.     /* read the file in */
  243.         mlwrite("[Reading file]");
  244.         nline = 0;
  245.     lflag = FALSE;
  246.         while ((s=ffgetline()) == FIOSUC) {
  247.                 nbytes = strlen(fline);
  248.                 if ((lp1=lalloc(nbytes)) == NULL) {
  249.                         s = FIOMEM;             /* Keep message on the  */
  250.                         break;                  /* display.             */
  251.                 }
  252.                 lp2 = lback(curbp->b_linep);
  253.                 lp2->l_fp = lp1;
  254.                 lp1->l_fp = curbp->b_linep;
  255.                 lp1->l_bp = lp2;
  256.                 curbp->b_linep->l_bp = lp1;
  257.                 for (i=0; i<nbytes; ++i)
  258.                         lputc(lp1, i, fline[i]);
  259.                 ++nline;
  260.         }
  261.         ffclose();                              /* Ignore errors.       */
  262.     strcpy(mesg, "[");
  263.     if (s==FIOERR) {
  264.         strcat(mesg, "I/O ERROR, ");
  265.         curbp->b_flag |= BFTRUNC;
  266.     }
  267.     if (s == FIOMEM) {
  268.         strcat(mesg, "OUT OF MEMORY, ");
  269.         curbp->b_flag |= BFTRUNC;
  270.     }
  271.     sprintf(&mesg[strlen(mesg)], "Read %d line", nline);
  272.         if (nline > 1)
  273.         strcat(mesg, "s");
  274.     strcat(mesg, "]");
  275.     mlwrite(mesg);
  276.  
  277. out:
  278.     TTkopen();    /* open the keyboard again */
  279.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  280.                 if (wp->w_bufp == curbp) {
  281.                         wp->w_linep = lforw(curbp->b_linep);
  282.                         wp->w_dotp  = lforw(curbp->b_linep);
  283.                         wp->w_doto  = 0;
  284.                         wp->w_markp = NULL;
  285.                         wp->w_marko = 0;
  286.                         wp->w_flag |= WFMODE|WFHARD;
  287.                 }
  288.         }
  289.         if (s == FIOERR || s == FIOFNF)        /* False if error.      */
  290.                 return(FALSE);
  291.         return (TRUE);
  292. }
  293.  
  294. /*
  295.  * Take a file name, and from it
  296.  * fabricate a buffer name. This routine knows
  297.  * about the syntax of file names on the target system.
  298.  * I suppose that this information could be put in
  299.  * a better place than a line of code.
  300.  */
  301. makename(bname, fname)
  302. char    bname[];
  303. char    fname[];
  304. {
  305.         register char *cp1;
  306.         register char *cp2;
  307.  
  308.         cp1 = &fname[0];
  309.         while (*cp1 != 0)
  310.                 ++cp1;
  311.  
  312. #if     AMIGA
  313.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  314.                 --cp1;
  315. #endif
  316. #if     VMS
  317.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  318.                 --cp1;
  319. #endif
  320. #if     CPM
  321.         while (cp1!=&fname[0] && cp1[-1]!=':')
  322.                 --cp1;
  323. #endif
  324. #if     MSDOS
  325.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  326.                 --cp1;
  327. #endif
  328. #if     ST520
  329.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  330.                 --cp1;
  331. #endif
  332. #if     FINDER
  333.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  334.                 --cp1;
  335. #endif
  336. #if     V7 | USG | BSD
  337.         while (cp1!=&fname[0] && cp1[-1]!='/')
  338.                 --cp1;
  339. #endif
  340.         cp2 = &bname[0];
  341.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  342.                 *cp2++ = *cp1++;
  343.         *cp2 = 0;
  344. }
  345.  
  346. unqname(name)    /* make sure a buffer name is unique */
  347.  
  348. char *name;    /* name to check on */
  349.  
  350. {
  351.     register char *sp;
  352.  
  353.     /* check to see if it is in the buffer list */
  354.     while (bfind(name, 0, FALSE) != NULL) {
  355.  
  356.         /* go to the end of the name */
  357.         sp = name;
  358.         while (*sp)
  359.             ++sp;
  360.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  361.             *sp++ = '0';
  362.             *sp = 0;
  363.         } else
  364.             *(--sp) += 1;
  365.     }
  366. }
  367.  
  368. /*
  369.  * Ask for a file name, and write the
  370.  * contents of the current buffer to that file.
  371.  * Update the remembered file name and clear the
  372.  * buffer changed flag. This handling of file names
  373.  * is different from the earlier versions, and
  374.  * is more compatable with Gosling EMACS than
  375.  * with ITS EMACS. Bound to "C-X C-W".
  376.  */
  377. filewrite(f, n)
  378. {
  379.         register WINDOW *wp;
  380.         register int    s;
  381.         char            fname[NFILEN];
  382.  
  383.     if (restflag)        /* don't allow this command if restricted */
  384.         return(resterr());
  385.         if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  386.                 return (s);
  387.         if ((s=writeout(fname)) == TRUE) {
  388.                 strcpy(curbp->b_fname, fname);
  389.                 curbp->b_flag &= ~BFCHG;
  390.                 wp = wheadp;                    /* Update mode lines.   */
  391.                 while (wp != NULL) {
  392.                         if (wp->w_bufp == curbp)
  393.                                 wp->w_flag |= WFMODE;
  394.                         wp = wp->w_wndp;
  395.                 }
  396.         }
  397.         return (s);
  398. }
  399.  
  400. /*
  401.  * Save the contents of the current
  402.  * buffer in its associatd file. No nothing
  403.  * if nothing has changed (this may be a bug, not a
  404.  * feature). Error if there is no remembered file
  405.  * name for the buffer. Bound to "C-X C-S". May
  406.  * get called by "C-Z".
  407.  */
  408. filesave(f, n)
  409. {
  410.         register WINDOW *wp;
  411.         register int    s;
  412.  
  413.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  414.         return(rdonly());    /* we are in read only mode    */
  415.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  416.                 return (TRUE);
  417.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  418.                 mlwrite("No file name");
  419.                 return (FALSE);
  420.         }
  421.  
  422.     /* complain about truncated files */
  423.     if ((curbp->b_flag&BFTRUNC) != 0) {
  424.         if (mlyesno("Truncated file..write it out") == FALSE) {
  425.             mlwrite("[Aborted]");
  426.             return(FALSE);
  427.         }
  428.     }
  429.  
  430.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  431.                 curbp->b_flag &= ~BFCHG;
  432.                 wp = wheadp;                    /* Update mode lines.   */
  433.                 while (wp != NULL) {
  434.                         if (wp->w_bufp == curbp)
  435.                                 wp->w_flag |= WFMODE;
  436.                         wp = wp->w_wndp;
  437.                 }
  438.         }
  439.         return (s);
  440. }
  441.  
  442. /*
  443.  * This function performs the details of file
  444.  * writing. Uses the file management routines in the
  445.  * "fileio.c" package. The number of lines written is
  446.  * displayed. Sadly, it looks inside a LINE; provide
  447.  * a macro for this. Most of the grief is error
  448.  * checking of some sort.
  449.  */
  450. writeout(fn)
  451. char    *fn;
  452. {
  453.         register int    s;
  454.         register LINE   *lp;
  455.         register int    nline;
  456.  
  457. #if    CRYPT
  458.     s = resetkey();
  459.     if (s != TRUE)
  460.         return(s);
  461. #endif
  462.     /* turn off ALL keyboard translation in case we get a dos error */
  463.     TTkclose();
  464.  
  465.         if ((s=ffwopen(fn)) != FIOSUC) {        /* Open writes message. */
  466.         TTkopen();
  467.                 return (FALSE);
  468.         }
  469.     mlwrite("[Writing...]");        /* tell us were writing */
  470.         lp = lforw(curbp->b_linep);             /* First line.          */
  471.         nline = 0;                              /* Number of lines.     */
  472.         while (lp != curbp->b_linep) {
  473.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  474.                         break;
  475.                 ++nline;
  476.                 lp = lforw(lp);
  477.         }
  478.         if (s == FIOSUC) {                      /* No write error.      */
  479.                 s = ffclose();
  480.                 if (s == FIOSUC) {              /* No close error.      */
  481.                         if (nline == 1)
  482.                                 mlwrite("[Wrote 1 line]");
  483.                         else
  484.                                 mlwrite("[Wrote %d lines]", nline);
  485.                 }
  486.         } else                                  /* Ignore close error   */
  487.                 ffclose();                      /* if a write error.    */
  488.     TTkopen();
  489.         if (s != FIOSUC)                        /* Some sort of error.  */
  490.                 return (FALSE);
  491.         return (TRUE);
  492. }
  493.  
  494. /*
  495.  * The command allows the user
  496.  * to modify the file name associated with
  497.  * the current buffer. It is like the "f" command
  498.  * in UNIX "ed". The operation is simple; just zap
  499.  * the name in the BUFFER structure, and mark the windows
  500.  * as needing an update. You can type a blank line at the
  501.  * prompt if you wish.
  502.  */
  503. filename(f, n)
  504. {
  505.         register WINDOW *wp;
  506.         register int    s;
  507.         char            fname[NFILEN];
  508.  
  509.     if (restflag)        /* don't allow this command if restricted */
  510.         return(resterr());
  511.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  512.                 return (s);
  513.         if (s == FALSE)
  514.                 strcpy(curbp->b_fname, "");
  515.         else
  516.                 strcpy(curbp->b_fname, fname);
  517.         wp = wheadp;                            /* Update mode lines.   */
  518.         while (wp != NULL) {
  519.                 if (wp->w_bufp == curbp)
  520.                         wp->w_flag |= WFMODE;
  521.                 wp = wp->w_wndp;
  522.         }
  523.     curbp->b_mode &= ~MDVIEW;    /* no longer read only mode */
  524.         return (TRUE);
  525. }
  526.  
  527. /*
  528.  * Insert file "fname" into the current
  529.  * buffer, Called by insert file command. Return the final
  530.  * status of the read.
  531.  */
  532. ifile(fname)
  533. char    fname[];
  534. {
  535.         register LINE   *lp0;
  536.         register LINE   *lp1;
  537.         register LINE   *lp2;
  538.         register int    i;
  539.         register BUFFER *bp;
  540.         register int    s;
  541.         register int    nbytes;
  542.         register int    nline;
  543.     int        lflag;        /* any lines longer than allowed? */
  544.     char mesg[NSTRING];
  545.  
  546.         bp = curbp;                             /* Cheap.               */
  547.         bp->b_flag |= BFCHG;            /* we have changed    */
  548.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  549.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  550.                 goto out;
  551.         if (s == FIOFNF) {                      /* File not found.      */
  552.                 mlwrite("[No such file]");
  553.         return(FALSE);
  554.         }
  555.         mlwrite("[Inserting file]");
  556.  
  557. #if    CRYPT
  558.     s = resetkey();
  559.     if (s != TRUE)
  560.         return(s);
  561. #endif
  562.     /* back up a line and save the mark here */
  563.     curwp->w_dotp = lback(curwp->w_dotp);
  564.     curwp->w_doto = 0;
  565.     curwp->w_markp = curwp->w_dotp;
  566.     curwp->w_marko = 0;
  567.  
  568.         nline = 0;
  569.     lflag = FALSE;
  570.         while ((s=ffgetline()) == FIOSUC) {
  571.                 nbytes = strlen(fline);
  572.                 if ((lp1=lalloc(nbytes)) == NULL) {
  573.                         s = FIOMEM;             /* Keep message on the  */
  574.                         break;                  /* display.             */
  575.                 }
  576.         lp0 = curwp->w_dotp;    /* line previous to insert */
  577.         lp2 = lp0->l_fp;    /* line after insert */
  578.  
  579.         /* re-link new line between lp0 and lp2 */
  580.         lp2->l_bp = lp1;
  581.         lp0->l_fp = lp1;
  582.         lp1->l_bp = lp0;
  583.         lp1->l_fp = lp2;
  584.  
  585.         /* and advance and write out the current line */
  586.         curwp->w_dotp = lp1;
  587.                 for (i=0; i<nbytes; ++i)
  588.                         lputc(lp1, i, fline[i]);
  589.                 ++nline;
  590.         }
  591.         ffclose();                              /* Ignore errors.       */
  592.     curwp->w_markp = lforw(curwp->w_markp);
  593.     strcpy(mesg, "[");
  594.     if (s==FIOERR) {
  595.         strcat(mesg, "I/O ERROR, ");
  596.         curbp->b_flag |= BFTRUNC;
  597.     }
  598.     if (s == FIOMEM) {
  599.         strcat(mesg, "OUT OF MEMORY, ");
  600.         curbp->b_flag |= BFTRUNC;
  601.     }
  602.     sprintf(&mesg[strlen(mesg)], "Inserted %d line", nline);
  603.         if (nline > 1)
  604.         strcat(mesg, "s");
  605.     strcat(mesg, "]");
  606.     mlwrite(mesg);
  607.  
  608. out:
  609.     /* advance to the next line and mark the window for changes */
  610.     curwp->w_dotp = lforw(curwp->w_dotp);
  611.     curwp->w_flag |= WFHARD | WFMODE;
  612.  
  613.     /* copy window parameters back to the buffer structure */
  614.     curbp->b_dotp = curwp->w_dotp;
  615.     curbp->b_doto = curwp->w_doto;
  616.     curbp->b_markp = curwp->w_markp;
  617.     curbp->b_marko = curwp->w_marko;
  618.  
  619.         if (s == FIOERR)                        /* False if error.      */
  620.                 return (FALSE);
  621.         return (TRUE);
  622. }
  623.