home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / me39src2 / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-16  |  18.3 KB  |  607 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.     register char    *sptr;        /* pointer into filename string */
  211.     int        lflag;        /* any lines longer than allowed? */
  212.     char mesg[NSTRING];
  213.  
  214. #if    FILOCK
  215.     if (lockfl && lockchk(fname) == ABORT)
  216.         return(ABORT);
  217. #endif
  218. #if    CRYPT
  219.     s = resetkey();
  220.     if (s != TRUE)
  221.         return(s);
  222. #endif
  223.         bp = curbp;                             /* Cheap.               */
  224.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  225.                 return (s);
  226.         bp->b_flag &= ~(BFINVS|BFCHG);
  227.         strcpy(bp->b_fname, fname);
  228.  
  229.     /* let a user macro get hold of things...if he wants */
  230.     execute(META|SPEC|'R', FALSE, 1);
  231.  
  232.     /* turn off ALL keyboard translation in case we get a dos error */
  233.     TTkclose();
  234.  
  235.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  236.                 goto out;
  237.  
  238.         if (s == FIOFNF) {                      /* File not found.      */
  239.                 mlwrite("[New file]");
  240.                 goto out;
  241.         }
  242.  
  243.     /* read the file in */
  244.         mlwrite("[Reading file]");
  245.         nline = 0;
  246.     lflag = FALSE;
  247.         while ((s=ffgetline()) == FIOSUC) {
  248.                 nbytes = strlen(fline);
  249.                 if ((lp1=lalloc(nbytes)) == NULL) {
  250.                         s = FIOMEM;             /* Keep message on the  */
  251.                         break;                  /* display.             */
  252.                 }
  253.                 lp2 = lback(curbp->b_linep);
  254.                 lp2->l_fp = lp1;
  255.                 lp1->l_fp = curbp->b_linep;
  256.                 lp1->l_bp = lp2;
  257.                 curbp->b_linep->l_bp = lp1;
  258.                 for (i=0; i<nbytes; ++i)
  259.                         lputc(lp1, i, fline[i]);
  260.                 ++nline;
  261.         }
  262.         ffclose();                              /* Ignore errors.       */
  263.     strcpy(mesg, "[");
  264.     if (s==FIOERR)
  265.         strcat(mesg, "I/O ERROR, ");
  266.     if (s == FIOMEM)
  267.         strcat(mesg, "OUT OF MEMORY, ");
  268.     sprintf(&mesg[strlen(mesg)], "Read %d line", nline);
  269.         if (nline > 1)
  270.         strcat(mesg, "s");
  271.     strcat(mesg, "]");
  272.     mlwrite(mesg);
  273.  
  274. out:
  275.     TTkopen();    /* open the keyboard again */
  276.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  277.                 if (wp->w_bufp == curbp) {
  278.                         wp->w_linep = lforw(curbp->b_linep);
  279.                         wp->w_dotp  = lforw(curbp->b_linep);
  280.                         wp->w_doto  = 0;
  281.                         wp->w_markp = NULL;
  282.                         wp->w_marko = 0;
  283.                         wp->w_flag |= WFMODE|WFHARD;
  284.                 }
  285.         }
  286.         if (s == FIOERR || s == FIOFNF)        /* False if error.      */
  287.                 return(FALSE);
  288.         return (TRUE);
  289. }
  290.  
  291. /*
  292.  * Take a file name, and from it
  293.  * fabricate a buffer name. This routine knows
  294.  * about the syntax of file names on the target system.
  295.  * I suppose that this information could be put in
  296.  * a better place than a line of code.
  297.  */
  298. makename(bname, fname)
  299. char    bname[];
  300. char    fname[];
  301. {
  302.         register char *cp1;
  303.         register char *cp2;
  304.  
  305.         cp1 = &fname[0];
  306.         while (*cp1 != 0)
  307.                 ++cp1;
  308.  
  309. #if     AMIGA
  310.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  311.                 --cp1;
  312. #endif
  313. #if     VMS
  314.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  315.                 --cp1;
  316. #endif
  317. #if     CPM
  318.         while (cp1!=&fname[0] && cp1[-1]!=':')
  319.                 --cp1;
  320. #endif
  321. #if     MSDOS
  322.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  323.                 --cp1;
  324. #endif
  325. #if     ST520
  326.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  327.                 --cp1;
  328. #endif
  329. #if     FINDER
  330.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  331.                 --cp1;
  332. #endif
  333. #if     V7 | USG | BSD
  334.         while (cp1!=&fname[0] && cp1[-1]!='/')
  335.                 --cp1;
  336. #endif
  337.         cp2 = &bname[0];
  338.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  339.                 *cp2++ = *cp1++;
  340.         *cp2 = 0;
  341. }
  342.  
  343. unqname(name)    /* make sure a buffer name is unique */
  344.  
  345. char *name;    /* name to check on */
  346.  
  347. {
  348.     register char *sp;
  349.  
  350.     /* check to see if it is in the buffer list */
  351.     while (bfind(name, 0, FALSE) != NULL) {
  352.  
  353.         /* go to the end of the name */
  354.         sp = name;
  355.         while (*sp)
  356.             ++sp;
  357.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  358.             *sp++ = '0';
  359.             *sp = 0;
  360.         } else
  361.             *(--sp) += 1;
  362.     }
  363. }
  364.  
  365. /*
  366.  * Ask for a file name, and write the
  367.  * contents of the current buffer to that file.
  368.  * Update the remembered file name and clear the
  369.  * buffer changed flag. This handling of file names
  370.  * is different from the earlier versions, and
  371.  * is more compatable with Gosling EMACS than
  372.  * with ITS EMACS. Bound to "C-X C-W".
  373.  */
  374. filewrite(f, n)
  375. {
  376.         register WINDOW *wp;
  377.         register int    s;
  378.         char            fname[NFILEN];
  379.  
  380.     if (restflag)        /* don't allow this command if restricted */
  381.         return(resterr());
  382.         if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  383.                 return (s);
  384.         if ((s=writeout(fname)) == TRUE) {
  385.                 strcpy(curbp->b_fname, fname);
  386.                 curbp->b_flag &= ~BFCHG;
  387.                 wp = wheadp;                    /* Update mode lines.   */
  388.                 while (wp != NULL) {
  389.                         if (wp->w_bufp == curbp)
  390.                                 wp->w_flag |= WFMODE;
  391.                         wp = wp->w_wndp;
  392.                 }
  393.         }
  394.         return (s);
  395. }
  396.  
  397. /*
  398.  * Save the contents of the current
  399.  * buffer in its associatd file. No nothing
  400.  * if nothing has changed (this may be a bug, not a
  401.  * feature). Error if there is no remembered file
  402.  * name for the buffer. Bound to "C-X C-S". May
  403.  * get called by "C-Z".
  404.  */
  405. filesave(f, n)
  406. {
  407.         register WINDOW *wp;
  408.         register int    s;
  409.  
  410.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  411.         return(rdonly());    /* we are in read only mode    */
  412.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  413.                 return (TRUE);
  414.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  415.                 mlwrite("No file name");
  416.                 return (FALSE);
  417.         }
  418.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  419.                 curbp->b_flag &= ~BFCHG;
  420.                 wp = wheadp;                    /* Update mode lines.   */
  421.                 while (wp != NULL) {
  422.                         if (wp->w_bufp == curbp)
  423.                                 wp->w_flag |= WFMODE;
  424.                         wp = wp->w_wndp;
  425.                 }
  426.         }
  427.         return (s);
  428. }
  429.  
  430. /*
  431.  * This function performs the details of file
  432.  * writing. Uses the file management routines in the
  433.  * "fileio.c" package. The number of lines written is
  434.  * displayed. Sadly, it looks inside a LINE; provide
  435.  * a macro for this. Most of the grief is error
  436.  * checking of some sort.
  437.  */
  438. writeout(fn)
  439. char    *fn;
  440. {
  441.         register int    s;
  442.         register LINE   *lp;
  443.         register int    nline;
  444.  
  445. #if    CRYPT
  446.     s = resetkey();
  447.     if (s != TRUE)
  448.         return(s);
  449. #endif
  450.     /* turn off ALL keyboard translation in case we get a dos error */
  451.     TTkclose();
  452.  
  453.         if ((s=ffwopen(fn)) != FIOSUC) {        /* Open writes message. */
  454.         TTkopen();
  455.                 return (FALSE);
  456.         }
  457.     mlwrite("[Writing...]");        /* tell us were writing */
  458.         lp = lforw(curbp->b_linep);             /* First line.          */
  459.         nline = 0;                              /* Number of lines.     */
  460.         while (lp != curbp->b_linep) {
  461.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  462.                         break;
  463.                 ++nline;
  464.                 lp = lforw(lp);
  465.         }
  466.         if (s == FIOSUC) {                      /* No write error.      */
  467.                 s = ffclose();
  468.                 if (s == FIOSUC) {              /* No close error.      */
  469.                         if (nline == 1)
  470.                                 mlwrite("[Wrote 1 line]");
  471.                         else
  472.                                 mlwrite("[Wrote %d lines]", nline);
  473.                 }
  474.         } else                                  /* Ignore close error   */
  475.                 ffclose();                      /* if a write error.    */
  476.     TTkopen();
  477.         if (s != FIOSUC)                        /* Some sort of error.  */
  478.                 return (FALSE);
  479.         return (TRUE);
  480. }
  481.  
  482. /*
  483.  * The command allows the user
  484.  * to modify the file name associated with
  485.  * the current buffer. It is like the "f" command
  486.  * in UNIX "ed". The operation is simple; just zap
  487.  * the name in the BUFFER structure, and mark the windows
  488.  * as needing an update. You can type a blank line at the
  489.  * prompt if you wish.
  490.  */
  491. filename(f, n)
  492. {
  493.         register WINDOW *wp;
  494.         register int    s;
  495.         char            fname[NFILEN];
  496.  
  497.     if (restflag)        /* don't allow this command if restricted */
  498.         return(resterr());
  499.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  500.                 return (s);
  501.         if (s == FALSE)
  502.                 strcpy(curbp->b_fname, "");
  503.         else
  504.                 strcpy(curbp->b_fname, fname);
  505.         wp = wheadp;                            /* Update mode lines.   */
  506.         while (wp != NULL) {
  507.                 if (wp->w_bufp == curbp)
  508.                         wp->w_flag |= WFMODE;
  509.                 wp = wp->w_wndp;
  510.         }
  511.     curbp->b_mode &= ~MDVIEW;    /* no longer read only mode */
  512.         return (TRUE);
  513. }
  514.  
  515. /*
  516.  * Insert file "fname" into the current
  517.  * buffer, Called by insert file command. Return the final
  518.  * status of the read.
  519.  */
  520. ifile(fname)
  521. char    fname[];
  522. {
  523.         register LINE   *lp0;
  524.         register LINE   *lp1;
  525.         register LINE   *lp2;
  526.         register int    i;
  527.         register BUFFER *bp;
  528.         register int    s;
  529.         register int    nbytes;
  530.         register int    nline;
  531.     int        lflag;        /* any lines longer than allowed? */
  532.     char mesg[NSTRING];
  533.  
  534.         bp = curbp;                             /* Cheap.               */
  535.         bp->b_flag |= BFCHG;            /* we have changed    */
  536.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  537.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  538.                 goto out;
  539.         if (s == FIOFNF) {                      /* File not found.      */
  540.                 mlwrite("[No such file]");
  541.         return(FALSE);
  542.         }
  543.         mlwrite("[Inserting file]");
  544.  
  545. #if    CRYPT
  546.     s = resetkey();
  547.     if (s != TRUE)
  548.         return(s);
  549. #endif
  550.     /* back up a line and save the mark here */
  551.     curwp->w_dotp = lback(curwp->w_dotp);
  552.     curwp->w_doto = 0;
  553.     curwp->w_markp = curwp->w_dotp;
  554.     curwp->w_marko = 0;
  555.  
  556.         nline = 0;
  557.     lflag = FALSE;
  558.         while ((s=ffgetline()) == FIOSUC) {
  559.                 nbytes = strlen(fline);
  560.                 if ((lp1=lalloc(nbytes)) == NULL) {
  561.                         s = FIOMEM;             /* Keep message on the  */
  562.                         break;                  /* display.             */
  563.                 }
  564.         lp0 = curwp->w_dotp;    /* line previous to insert */
  565.         lp2 = lp0->l_fp;    /* line after insert */
  566.  
  567.         /* re-link new line between lp0 and lp2 */
  568.         lp2->l_bp = lp1;
  569.         lp0->l_fp = lp1;
  570.         lp1->l_bp = lp0;
  571.         lp1->l_fp = lp2;
  572.  
  573.         /* and advance and write out the current line */
  574.         curwp->w_dotp = lp1;
  575.                 for (i=0; i<nbytes; ++i)
  576.                         lputc(lp1, i, fline[i]);
  577.                 ++nline;
  578.         }
  579.         ffclose();                              /* Ignore errors.       */
  580.     curwp->w_markp = lforw(curwp->w_markp);
  581.     strcpy(mesg, "[");
  582.     if (s==FIOERR)
  583.         strcat(mesg, "I/O ERROR, ");
  584.     if (s == FIOMEM)
  585.         strcat(mesg, "OUT OF MEMORY, ");
  586.     sprintf(&mesg[strlen(mesg)], "Inserted %d line", nline);
  587.         if (nline > 1)
  588.         strcat(mesg, "s");
  589.     strcat(mesg, "]");
  590.     mlwrite(mesg);
  591.  
  592. out:
  593.     /* advance to the next line and mark the window for changes */
  594.     curwp->w_dotp = lforw(curwp->w_dotp);
  595.     curwp->w_flag |= WFHARD | WFMODE;
  596.  
  597.     /* copy window parameters back to the buffer structure */
  598.     curbp->b_dotp = curwp->w_dotp;
  599.     curbp->b_doto = curwp->w_doto;
  600.     curbp->b_markp = curwp->w_markp;
  601.     curbp->b_marko = curwp->w_marko;
  602.  
  603.         if (s == FIOERR)                        /* False if error.      */
  604.                 return (FALSE);
  605.         return (TRUE);
  606. }
  607.