home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / FILE.C < prev    next >
C/C++ Source or Header  |  1991-03-19  |  22KB  |  862 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    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. /*
  16.  * Read a file into the current
  17.  * buffer. This is really easy; all you do is
  18.  * find the name of the file, and call the standard
  19.  * "read a file into the current buffer" code.
  20.  * Bound to "C-X C-R".
  21.  */
  22. PASCAL NEAR fileread(f, n)
  23.  
  24. int f, n;    /* defualt and numeric arguments (unused) */
  25.  
  26. {
  27.     register int s;    /* status return */
  28.     char *fname;    /* file name to read */
  29.  
  30.     if (restflag)        /* don't allow this command if restricted */
  31.         return(resterr());
  32.  
  33.     if ((fname = gtfilename(TEXT131)) == NULL)
  34. /*                              "Read file" */
  35.         return(FALSE);
  36.     return(readin(fname, TRUE));
  37. }
  38.  
  39. /*
  40.  * Insert a file into the current
  41.  * buffer. This is really easy; all you do it
  42.  * find the name of the file, and call the standard
  43.  * "insert a file into the current buffer" code.
  44.  * Bound to "C-X C-I".
  45.  */
  46. PASCAL NEAR insfile(f, n)
  47.  
  48. int f,n;    /* prefix flag and argument */
  49.  
  50. {
  51.     register int    s;
  52.     char *fname;    /* file name */
  53.     short int curoff;
  54.     LINE *curline;
  55.  
  56.     if (restflag)        /* don't allow this command if restricted */
  57.         return(resterr());
  58.     if (curbp->b_mode&MDVIEW)      /* don't allow this command if  */
  59.         return(rdonly());    /* we are in read only mode    */
  60.  
  61.     if ((fname = gtfilename(TEXT132)) == NULL) 
  62. /*                              "Insert file" */
  63.         return(FALSE);
  64.     /*
  65.      * Save the local pointers to hold global ".", in case
  66.      * $yankpnt is set to 1.
  67.      */
  68.     if (yanktype == SRBEGIN) {
  69.         /* Find the *previous* line, since the line we are on
  70.          * may disappear due to re-allocation.  This works even
  71.          * if we are on the first line of the file.
  72.          */
  73.         curline = lback(curwp->w_dotp);
  74.         curoff = curwp->w_doto;
  75.     }
  76.     s = ifile(fname);
  77.  
  78.     if (yanktype == SRBEGIN) {
  79.         curwp->w_dotp = lforw(curline);
  80.         curwp->w_doto = curoff;
  81.     }
  82.     return (s);
  83. }
  84.  
  85. /*
  86.  * Select a file for editing.
  87.  * Look around to see if you can find the
  88.  * fine in another buffer; if you can find it
  89.  * just switch to the buffer. If you cannot find
  90.  * the file, create a new buffer, read in the
  91.  * text, and switch to the new buffer.
  92.  * Bound to C-X C-F.
  93.  */
  94. PASCAL NEAR filefind(f, n)
  95.  
  96. int f,n;    /* prefix flag and argument */
  97.  
  98. {
  99.     char *fname;    /* file user wishes to find */    /* file name */
  100.     register int s;        /* status return */
  101.  
  102.     if (restflag)        /* don't allow this command if restricted */
  103.         return(resterr());
  104.  
  105.     if ((fname = gtfilename(TEXT133)) == NULL) 
  106. /*                              "Find file" */
  107.         return(FALSE);
  108.     return(getfile(fname, TRUE));
  109. }
  110.  
  111. PASCAL NEAR viewfile(f, n)    /* visit a file in VIEW mode */
  112.  
  113. int f,n;    /* prefix flag and argument */
  114.  
  115. {
  116.     char *fname;    /* file user wishes to find */    /* file name */
  117.     register int s;    /* status return */
  118.  
  119.     if (restflag)        /* don't allow this command if restricted */
  120.         return(resterr());
  121.  
  122.     if ((fname = gtfilename(TEXT134)) == NULL) 
  123. /*                              "View file" */
  124.         return(FALSE);
  125.     s = getfile(fname, FALSE);
  126.     if (s) {    /* if we succeed, put it in view mode */
  127.         curwp->w_bufp->b_mode |= MDVIEW;
  128.         upmode();
  129.     }
  130.     return(s);
  131. }
  132.  
  133. #if    CRYPT
  134. PASCAL NEAR resetkey()    /* reset the encryption key if needed */
  135.  
  136. {
  137.     register int s; /* return status */
  138.  
  139.     /* turn off the encryption flag */
  140.     cryptflag = FALSE;
  141.  
  142.     /* if we are in crypt mode */
  143.     if (curbp->b_mode & MDCRYPT) {
  144.         if (curbp->b_key[0] == 0) {
  145.             s = setekey(FALSE, 0);
  146.             if (s != TRUE)
  147.                 return(s);
  148.         }
  149.  
  150.         /* let others know... */
  151.         cryptflag = TRUE;
  152.  
  153.         /* and set up the key to be used! */
  154.         /* de-encrypt it */
  155.         crypt((char *)NULL, 0);
  156.         crypt(curbp->b_key, strlen(curbp->b_key));
  157.  
  158.         /* re-encrypt it...seeding it to start */
  159.         crypt((char *)NULL, 0);
  160.         crypt(curbp->b_key, strlen(curbp->b_key));
  161.     }
  162.  
  163.     return(TRUE);
  164. }
  165. #endif
  166.  
  167. PASCAL NEAR getfile(fname, lockfl)
  168.  
  169. char fname[];        /* file name to find */
  170. int lockfl;        /* check the file for locks? */
  171.  
  172. {
  173.     register BUFFER *bp;
  174.     register LINE    *lp;
  175.     register int    i;
  176.     register int    s;
  177.     register int cmark;    /* current mark */
  178.     char bname[NBUFN];    /* buffer name to put file */
  179.  
  180. #if    MSDOS | OS2 | AOSVS | VMS | ATARI
  181.     mklower(fname);            /* msdos isn't case sensitive */
  182. #endif
  183.     for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  184.         if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  185.             swbuffer(bp);
  186.             lp = curwp->w_dotp;
  187.             i = curwp->w_ntrows/2;
  188.             while (i-- && lback(lp)!=curbp->b_linep)
  189.                 lp = lback(lp);
  190.             curwp->w_linep = lp;
  191.             curwp->w_flag |= WFMODE|WFHARD;
  192.             mlwrite(TEXT135);
  193. /*                              "[Old buffer]" */
  194.             return(TRUE);
  195.         }
  196.     }
  197.     makename(bname, fname);         /* New buffer name.    */
  198.  
  199.     while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  200.         /* old buffer name conflict code */
  201.         s = mlreply(TEXT136, bname, NBUFN);
  202. /*                          "Buffer name: " */
  203.         if (s == ABORT)         /* ^G to just quit    */
  204.             return(s);
  205.         if (s == FALSE) {        /* CR to clobber it    */
  206.             makename(bname, fname);
  207.             break;
  208.         }
  209.     }
  210.     if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  211.         mlwrite(TEXT137);
  212. /*                      "Cannot create buffer" */
  213.         return(FALSE);
  214.     }
  215.  
  216.     if (--curbp->b_nwnd == 0) {        /* Undisplay.        */
  217.         curbp->b_dotp = curwp->w_dotp;
  218.         curbp->b_doto = curwp->w_doto;
  219.         for (cmark = 0; cmark < NMARKS; cmark++) {
  220.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  221.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  222.         }
  223.         curbp->b_fcol = curwp->w_fcol;
  224.     }
  225.     curbp = bp;                /* Switch to it.    */
  226.     curwp->w_bufp = bp;
  227.     curbp->b_nwnd++;
  228.     return(readin(fname, lockfl));        /* Read it in.        */
  229. }
  230.  
  231. /*
  232.     Read file "fname" into the current buffer, blowing away any text
  233.     found there.  Called by both the read and find commands.  Return
  234.     the final status of the read.  Also called by the mainline, to
  235.     read in a file specified on the command line as an argument. 
  236.     The command in $readhook is called after the buffer is set up
  237.     and before it is read. 
  238. */
  239.  
  240. PASCAL NEAR readin(fname, lockfl)
  241.  
  242. char    fname[];    /* name of file to read */
  243. int    lockfl;        /* check for file locks? */
  244.  
  245. {
  246.     register LINE *lp1;
  247.     register LINE *lp2;
  248.     register int i;
  249.     register WINDOW *wp;
  250.     register BUFFER *bp;
  251.     register int s;
  252.     register int nbytes;
  253.     register int nline;
  254.     register int cmark;    /* current mark */
  255.     char mesg[NSTRING];
  256.  
  257. #if    FILOCK
  258.     if (lockfl && lockchk(fname) == ABORT)
  259.         return(ABORT);
  260. #endif
  261.  
  262.     bp = curbp;                /* Cheap.        */
  263.     if ((s=bclear(bp)) != TRUE)        /* Might be old.    */
  264.         return(s);
  265.     bp->b_flag &= ~(BFINVS|BFCHG);
  266.     strcpy(bp->b_fname, fname);
  267.  
  268.     /* let a user macro get hold of things...if he wants */
  269.     execkey(&readhook, FALSE, 1);
  270.  
  271. #if    CRYPT
  272.     /* set up for decryption */
  273.     s = resetkey();
  274.     if (s != TRUE)
  275.         return(s);
  276. #endif
  277.  
  278.     /* turn off ALL keyboard translation in case we get a dos error */
  279.     TTkclose();
  280.  
  281.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  282.         goto out;
  283.  
  284.     if (s == FIOFNF) {            /* File not found.    */
  285.         mlwrite(TEXT138);
  286. /*                      "[New file]" */
  287.         goto out;
  288.     }
  289.  
  290.     /* read the file in */
  291.     mlwrite(TEXT139);
  292. /*              "[Reading file]" */
  293.     nline = 0;
  294.     while ((s=ffgetline()) == FIOSUC) {
  295.         nbytes = strlen(fline);
  296.         if ((lp1=lalloc(nbytes)) == NULL) {
  297.             s = FIOMEM;        /* Keep message on the    */
  298.             break;            /* display.        */
  299.         }
  300.         lp2 = lback(curbp->b_linep);
  301.         lp2->l_fp = lp1;
  302.         lp1->l_fp = curbp->b_linep;
  303.         lp1->l_bp = lp2;
  304.         curbp->b_linep->l_bp = lp1;
  305.         for (i=0; i<nbytes; ++i)
  306.             lputc(lp1, i, fline[i]);
  307.         ++nline;
  308.     }
  309.     ffclose();                /* Ignore errors.    */
  310.     strcpy(mesg, "[");
  311.     if (s==FIOERR) {
  312.         strcat(mesg, TEXT141);
  313. /*                           "I/O ERROR, " */
  314.         curbp->b_flag |= BFTRUNC;
  315.     }
  316.     if (s == FIOMEM) {
  317.         strcat(mesg, TEXT142);
  318. /*                           "OUT OF MEMORY, " */
  319.         curbp->b_flag |= BFTRUNC;
  320.     }
  321.     strcat(mesg, TEXT140);
  322. /*                   "Read " */
  323.     strcat(mesg, int_asc(nline));
  324.     strcat(mesg, TEXT143);
  325. /*                   " line" */
  326.     if (nline > 1)
  327.         strcat(mesg, "s");
  328.     strcat(mesg, "]");
  329.     mlwrite(mesg);
  330.  
  331. out:
  332.     TTkopen();    /* open the keyboard again */
  333.     for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  334.         if (wp->w_bufp == curbp) {
  335.             wp->w_linep = lforw(curbp->b_linep);
  336.             wp->w_dotp  = lforw(curbp->b_linep);
  337.             wp->w_doto  = 0;
  338.             for (cmark = 0; cmark < NMARKS; cmark++) {
  339.                 wp->w_markp[cmark] = NULL;
  340.                 wp->w_marko[cmark] = 0;
  341.             }
  342.             wp->w_flag |= WFMODE|WFHARD;
  343.         }
  344.     }
  345.     if (s == FIOERR || s == FIOFNF)     /* False if error.    */
  346.         return(FALSE);
  347.     return(TRUE);
  348. }
  349.  
  350. /*
  351.  * Take a file name, and from it
  352.  * fabricate a buffer name. This routine knows
  353.  * about the syntax of file names on the target system.
  354.  * I suppose that this information could be put in
  355.  * a better place than a line of code.
  356.  * Returns a pointer into fname indicating the end of the file path; i.e.,
  357.  * 1 character BEYOND the path name.
  358.  */
  359. char *PASCAL NEAR makename(bname, fname)
  360.  
  361. char *bname;
  362. char *fname;
  363.  
  364. {
  365.     register char *cp1;
  366.     register char *cp2;
  367.     register char *pathp;
  368.  
  369. #if     AOSVS | MV_UX
  370.         resolve_full_pathname(fname, fname);
  371.         mklower(fname);   /* aos/vs not case sensitive */
  372. #endif
  373.     cp1 = &fname[0];
  374.     while (*cp1 != 0)
  375.         ++cp1;
  376.  
  377. #if    AMIGA
  378.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  379.         --cp1;
  380. #endif
  381. #if     AOSVS | MV_UX
  382.         while (cp1!=&fname[0] && cp1[-1]!=':')
  383.                 --cp1;
  384. #endif
  385. #if    VMS
  386.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  387.         --cp1;
  388. #endif
  389. #if    MSDOS | OS2
  390.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  391.         --cp1;
  392. #endif
  393. #if    ST520
  394.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  395.         --cp1;
  396. #endif
  397. #if    FINDER
  398.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  399.         --cp1;
  400. #endif
  401. #if    V7 | USG | SMOS | HPUX | BSD | SUN | XENIX | AVIION
  402.     while (cp1!=&fname[0] && cp1[-1]!='/')
  403.         --cp1;
  404. #endif
  405. #if WMCS
  406.     while (cp1!=&fname[0] && cp1[-1]!='_' && cp1[-1]!='/')
  407.         --cp1;
  408. #endif
  409.     /* cp1 is pointing to the first real filename char */
  410.     pathp = cp1;
  411.  
  412.     cp2 = &bname[0];
  413.     while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  414.         *cp2++ = *cp1++;
  415.     *cp2 = 0;
  416.  
  417.     return(pathp);
  418. }
  419.  
  420. PASCAL NEAR unqname(name)    /* make sure a buffer name is unique */
  421.  
  422. char *name;    /* name to check on */
  423.  
  424. {
  425.     register char *sp;
  426.  
  427.     /* check to see if it is in the buffer list */
  428.     while (bfind(name, 0, FALSE) != NULL) {
  429.  
  430.         /* go to the end of the name */
  431.         sp = name;
  432.         while (*sp)
  433.             ++sp;
  434.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  435.             *sp++ = '0';
  436.             *sp = 0;
  437.         } else
  438.               *(--sp) += 1;
  439.     }
  440. }
  441.  
  442. /*
  443.  * Ask for a file name, and write the
  444.  * contents of the current buffer to that file.
  445.  * Update the remembered file name and clear the
  446.  * buffer changed flag. This handling of file names
  447.  * is different from the earlier versions, and
  448.  * is more compatable with Gosling EMACS than
  449.  * with ITS EMACS. Bound to "C-X C-W" for writing
  450.  * and ^X^A for appending.
  451.  */
  452.  
  453. PASCAL NEAR filewrite(f, n)
  454.  
  455. int f, n;    /* emacs arguments */
  456.  
  457. {
  458.     register int s;
  459.     char fname[NFILEN];
  460.  
  461.     if (restflag)        /* don't allow this command if restricted */
  462.         return(resterr());
  463.     if ((s=mlreply(TEXT144, fname, NFILEN)) != TRUE)
  464. /*                     "Write file: " */
  465.         return(s);
  466.     if ((s=writeout(fname, "w")) == TRUE) {
  467.         strcpy(curbp->b_fname, fname);
  468.         curbp->b_flag &= ~BFCHG;
  469.         /* Update mode lines.    */
  470.         upmode();
  471.     }
  472.     return(s);
  473. }
  474.  
  475. PASCAL NEAR fileapp(f, n)    /* append file */
  476.  
  477. int f, n;    /* emacs arguments */
  478.  
  479. {
  480.     register int s;
  481.     char fname[NFILEN];
  482.  
  483.     if (restflag)        /* don't allow this command if restricted */
  484.         return(resterr());
  485.     if ((s=mlreply(TEXT218, fname, NFILEN)) != TRUE)
  486. /*                     "Append file: " */
  487.         return(s);
  488.     if ((s=writeout(fname, "a")) == TRUE) {
  489.         curbp->b_flag &= ~BFCHG;
  490.         /* Update mode lines.    */
  491.         upmode();
  492.     }
  493.     return(s);
  494. }
  495.  
  496. /*
  497.  * Save the contents of the current
  498.  * buffer in its associatd file. Do nothing
  499.  * if nothing has changed (this may be a bug, not a
  500.  * feature). Error if there is no remembered file
  501.  * name for the buffer. Bound to "C-X C-S". May
  502.  * get called by "C-Z".
  503.  */
  504. PASCAL NEAR filesave(f, n)
  505.  
  506. int f,n;    /* prefix flag and argument */
  507.  
  508. {
  509.     register int s;
  510.  
  511.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  512.         return(rdonly());    /* we are in read only mode    */
  513.     if ((curbp->b_flag&BFCHG) == 0)     /* Return, no changes.    */
  514.         return(TRUE);
  515.     if (curbp->b_fname[0] == 0) {        /* Must have a name.    */
  516.         mlwrite(TEXT145);
  517. /*                      "No file name" */
  518.         return(FALSE);
  519.     }
  520.  
  521.     /* complain about truncated files */
  522.     if ((curbp->b_flag&BFTRUNC) != 0) {
  523.         if (mlyesno(TEXT146) == FALSE) {
  524. /*                          "Truncated file..write it out" */
  525.             mlwrite(TEXT8);
  526. /*                              "[Aborted]" */
  527.             return(FALSE);
  528.         }
  529.     }
  530.  
  531.     /* complain about narrowed buffers */
  532.     if ((curbp->b_flag&BFNAROW) != 0) {
  533.         if (mlyesno(TEXT147) == FALSE) {
  534. /*                          "Narrowed Buffer..write it out" */
  535.             mlwrite(TEXT8);
  536. /*                              "[Aborted]" */
  537.             return(FALSE);
  538.         }
  539.     }
  540.  
  541.     if ((s=writeout(curbp->b_fname, "w")) == TRUE) {
  542.         curbp->b_flag &= ~BFCHG;
  543.         /* Update mode lines.    */
  544.         upmode();
  545.     }
  546.     return(s);
  547. }
  548.  
  549. /*
  550.  * This function performs the details of file writing. It uses
  551.  * the file management routines in the "fileio.c" package. The
  552.  * number of lines written is displayed. Several errors are
  553.  * posible, and cause writeout to return a FALSE result. When
  554.  * $ssave is TRUE,  the buffer is written out to a temporary
  555.  * file, and then the old file is unlinked and the temporary
  556.  * renamed to the original name.  Before the file is written,
  557.  * a user specifyable routine (in $writehook) can be run.
  558.  */
  559.  
  560. PASCAL NEAR writeout(fn, mode)
  561.  
  562. char *fn;    /* name of file to write current buffer to */
  563. char *mode;    /* mode to open file (w = write a = append) */
  564. {
  565.     register LINE *lp;    /* line to scan while writing */
  566.     register char *sp;    /* temporary string pointer */
  567.     register int nline;    /* number of lines written */
  568.     int status;        /* return status */
  569.     int sflag;        /* are we safe saving? */
  570.     char tname[NSTRING];    /* temporary file name */
  571.     char buf[NSTRING];    /* message buffer */
  572.  
  573.     /* let a user macro get hold of things...if he wants */
  574.     execkey(&writehook, FALSE, 1);
  575.  
  576.     /* determine if we will use the save method */
  577.     sflag = FALSE;
  578.     if (ssave && fexist(fn) && *mode == 'w')
  579.         sflag = TRUE;
  580.  
  581. #if    CRYPT
  582.     /* set up for file encryption */
  583.     status = resetkey();
  584.     if (status != TRUE)
  585.         return(status);
  586. #endif
  587.  
  588.     /* turn off ALL keyboard translation in case we get a dos error */
  589.     TTkclose();
  590.  
  591.     /* Perform Safe Save..... */
  592.     if (sflag) {
  593.         /* duplicate original file name, and find where to trunc it */
  594.         sp = tname + (makename(tname, fn) - fn) + 1;
  595.         strcpy(tname, fn);
  596.  
  597.         /* create a unique name, using random numbers */
  598.         do {
  599.             *sp = 0;
  600.             strcat(tname, int_asc(ernd() & 0xffff));
  601.         } while(fexist(tname));
  602.  
  603.         /* open the temporary file */
  604. #if     AOSVS
  605.                 status = ffwopen(fn, "w", tname);
  606. #else
  607.         status = ffwopen(tname, "w");
  608. #endif
  609.     } else
  610. #if     AOSVS
  611.                 status = ffwopen(fn, mode, NULL);
  612. #else
  613.         status = ffwopen(fn, mode);
  614. #endif
  615.  
  616.     /* if the open failed.. clean up and abort */
  617.     if (status != FIOSUC) {
  618.         TTkopen();
  619.         return(FALSE);
  620.     }
  621.  
  622.     /* write the current buffer's lines to the open disk file */
  623.     mlwrite(TEXT148);    /* tell us that we're writing */
  624. /*              "[Writing...]" */
  625.     lp = lforw(curbp->b_linep);    /* start at the first line.    */
  626.     nline = 0;            /* track the Number of lines    */
  627.     while (lp != curbp->b_linep) {
  628.         if ((status = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  629.             break;
  630.         ++nline;
  631.         lp = lforw(lp);
  632.     }
  633.  
  634.  
  635.     /* report on status of file write */
  636.     *buf = 0;
  637.     status |= ffclose();
  638.     if (status == FIOSUC) {
  639.         /* report on success (or lack therof) */
  640.         strcpy(buf, TEXT149);
  641. /*                          "[Wrote " */
  642.         strcat(buf, int_asc(nline));
  643.         strcat(buf, TEXT143);
  644. /*                          " line" */
  645.         if (nline > 1)
  646.             strcat(buf, "s");
  647.  
  648.         if (sflag) {
  649.             /* erase original file */
  650.             /* rename temporary file to original name */
  651.             if (unlink(fn) == 0 && rename(tname, fn) == 0)
  652.                 ;
  653.             else {
  654.                 strcat(buf, TEXT150);
  655. /*                                          ", saved as " */
  656.                 strcat(buf, tname);
  657.                 status = FIODEL;        /* failed */
  658.             }
  659.         }
  660.         strcat(buf, "]");
  661.         mlwrite(buf);
  662.     }
  663.  
  664.     /* reopen the keyboard, and return our status */
  665.     TTkopen();
  666.     return(status == FIOSUC);
  667. }
  668.  
  669. /*
  670.  * The command allows the user
  671.  * to modify the file name associated with
  672.  * the current buffer. It is like the "f" command
  673.  * in UNIX "ed". The operation is simple; just zap
  674.  * the name in the BUFFER structure, and mark the windows
  675.  * as needing an update. You can type a blank line at the
  676.  * prompt if you wish.
  677.  */
  678.  
  679. PASCAL NEAR filename(f, n)
  680.  
  681. int f,n;    /* prefix flag and argument */
  682.  
  683. {
  684.     register int    s;
  685.     char        fname[NFILEN];
  686.  
  687.     if (restflag)        /* don't allow this command if restricted */
  688.         return(resterr());
  689.     if ((s=mlreply(TEXT151, fname, NFILEN)) == ABORT)
  690. /*                     "Name: " */
  691.         return(s);
  692.     if (s == FALSE)
  693.         strcpy(curbp->b_fname, "");
  694.     else
  695.         strcpy(curbp->b_fname, fname);
  696.     /* Update mode lines.    */
  697.     upmode();
  698.     curbp->b_mode &= ~MDVIEW;      /* no longer read only mode */
  699.     return(TRUE);
  700. }
  701.  
  702. /*
  703.  * Insert file "fname" into the current
  704.  * buffer, Called by insert file command. Return the final
  705.  * status of the read.
  706.  */
  707. PASCAL NEAR ifile(fname)
  708. char    fname[];
  709. {
  710.     register LINE *lp0;
  711.     register LINE *lp1;
  712.     register LINE *lp2;
  713.     register int i;
  714.     register BUFFER *bp;
  715.     register int s;
  716.     register int nbytes;
  717.     register int nline;
  718.     int cmark;    /* current mark */
  719.     char mesg[NSTRING];
  720.  
  721.     bp = curbp;                /* Cheap.        */
  722.     bp->b_flag |= BFCHG;            /* we have changed    */
  723.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  724.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  725.         goto out;
  726.     if (s == FIOFNF) {            /* File not found.    */
  727.         mlwrite(TEXT152);
  728. /*                      "[No such file]" */
  729.         return(FALSE);
  730.     }
  731.     mlwrite(TEXT153);
  732. /*              "[Inserting file]" */
  733.  
  734. #if    CRYPT
  735.     s = resetkey();
  736.     if (s != TRUE)
  737.         return(s);
  738. #endif
  739.     /* back up a line and save the mark here */
  740.     curwp->w_dotp = lback(curwp->w_dotp);
  741.     curwp->w_doto = 0;
  742.     for (cmark = 0; cmark < NMARKS; cmark++) {
  743.         curwp->w_markp[cmark] = curwp->w_dotp;
  744.         curwp->w_marko[cmark] = 0;
  745.     }
  746.  
  747.     nline = 0;
  748.     while ((s=ffgetline()) == FIOSUC) {
  749.         nbytes = strlen(fline);
  750.         if ((lp1=lalloc(nbytes)) == NULL) {
  751.             s = FIOMEM;        /* Keep message on the    */
  752.             break;            /* display.        */
  753.         }
  754.         lp0 = curwp->w_dotp;  /* line previous to insert */
  755.         lp2 = lp0->l_fp;    /* line after insert */
  756.  
  757.         /* re-link new line between lp0 and lp2 */
  758.         lp2->l_bp = lp1;
  759.         lp0->l_fp = lp1;
  760.         lp1->l_bp = lp0;
  761.         lp1->l_fp = lp2;
  762.  
  763.         /* and advance and write out the current line */
  764.         curwp->w_dotp = lp1;
  765.         for (i=0; i<nbytes; ++i)
  766.             lputc(lp1, i, fline[i]);
  767.         ++nline;
  768.     }
  769.     ffclose();                /* Ignore errors.    */
  770.     curwp->w_markp[0] = lforw(curwp->w_markp[0]);
  771.     strcpy(mesg, "[");
  772.     if (s==FIOERR) {
  773.         strcat(mesg, TEXT141);
  774. /*                           "I/O ERROR, " */
  775.         curbp->b_flag |= BFTRUNC;
  776.     }
  777.     if (s == FIOMEM) {
  778.         strcat(mesg, TEXT142);
  779. /*                           "OUT OF MEMORY, " */
  780.         curbp->b_flag |= BFTRUNC;
  781.     }
  782.     strcat(mesg, TEXT154);
  783. /*                   "Inserted " */
  784.     strcat(mesg, int_asc(nline));
  785.     strcat(mesg, TEXT143);
  786. /*                   " line" */
  787.     if (nline > 1)
  788.         strcat(mesg, "s");
  789.     strcat(mesg, "]");
  790.     mlwrite(mesg);
  791.  
  792. out:
  793.     /* advance to the next line and mark the window for changes */
  794.     curwp->w_dotp = lforw(curwp->w_dotp);
  795.     curwp->w_flag |= WFHARD | WFMODE;
  796.  
  797.     /* copy window parameters back to the buffer structure */
  798.     curbp->b_dotp = curwp->w_dotp;
  799.     curbp->b_doto = curwp->w_doto;
  800.     for (cmark = 0; cmark < NMARKS; cmark++) {
  801.         curbp->b_markp[cmark] = curwp->w_markp[cmark];
  802.         curbp->b_marko[cmark] = curwp->w_marko[cmark];
  803.     }
  804.     curbp->b_fcol = curwp->w_fcol;
  805.  
  806.     if (s == FIOERR)            /* False if error.    */
  807.         return(FALSE);
  808.     return(TRUE);
  809. }
  810.  
  811. /*    show-files    Bring up a fake buffer and list the
  812.             names of all the files in a given directory
  813. */
  814.  
  815. PASCAL NEAR showfiles(f, n)
  816.  
  817. int f,n;    /* prefix flag and argument */
  818.  
  819. {
  820.     register BUFFER *dirbuf;/* buffer to put file list into */
  821.     char outseq[NSTRING];    /* output buffer for file names */
  822.     char *sp;        /* output ptr for file names */
  823.     char mstring[NSTRING];    /* string to match cmd names to */
  824.     int status;        /* status return */
  825.  
  826.     /* ask what directory mask to search */
  827.     status = mlreply("Directory to show: ", mstring, NSTRING - 1);
  828.     if (status == ABORT)
  829.         return(status);
  830.  
  831.     /* get a buffer for the file list */
  832.     dirbuf = bfind("File List", TRUE, 0);
  833.     if (dirbuf == NULL || bclear(dirbuf) == FALSE) {
  834.         mlwrite("Can not display file list");
  835. /*            "Can not display function list" */
  836.         return(FALSE);
  837.     }
  838.  
  839.     /* let us know this is in progress */
  840.     mlwrite("[Building File List]");
  841.  
  842.     /* get the first file name */
  843.     sp = getffile(mstring);
  844.  
  845.     while (sp) {
  846.  
  847.         /* add a name to the buffer */
  848.         strcpy(outseq, sp);
  849.         if (addline(dirbuf, outseq) != TRUE)
  850.             return(FALSE);
  851.  
  852.         /* and get the next name */
  853.         sp = getnfile();
  854.     }
  855.  
  856.     /* display the list */
  857.     wpopup(dirbuf);
  858.     mlerase();    /* clear the mode line */
  859.     return(TRUE);
  860. }
  861.  
  862.