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