2013-05-19 05:36:24 -04:00
|
|
|
/* file.c -- implements file.h */
|
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
/* file.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
2016-04-01 01:25:05 -04:00
|
|
|
* The routines in this file handle the reading, writing
|
|
|
|
* and lookup of disk files. All of details about the
|
|
|
|
* reading and writing of the disk are in "fileio.c".
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
2016-04-01 01:25:05 -04:00
|
|
|
* modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <stdio.h>
|
2015-10-04 23:34:33 -04:00
|
|
|
#include <stdlib.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <string.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-05-28 01:25:14 -04:00
|
|
|
#include "buffer.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "defines.h"
|
2015-11-05 22:33:05 -05:00
|
|
|
#include "display.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "estruct.h"
|
2013-06-03 23:52:28 -04:00
|
|
|
#include "execute.h"
|
2013-05-17 23:42:16 -04:00
|
|
|
#include "fileio.h"
|
2013-05-19 05:36:24 -04:00
|
|
|
#include "input.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2013-06-01 01:42:09 -04:00
|
|
|
#include "lock.h"
|
2015-02-12 23:31:59 -05:00
|
|
|
#include "mlout.h"
|
2013-05-28 00:47:48 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-11 22:41:28 -04:00
|
|
|
typedef enum {
|
|
|
|
EOL_NONE,
|
|
|
|
EOL_UNIX,
|
|
|
|
EOL_DOS,
|
|
|
|
EOL_MAC,
|
|
|
|
EOL_MIXED
|
|
|
|
} eoltype ;
|
|
|
|
|
|
|
|
static const char *eolname[] = {
|
|
|
|
"NONE",
|
|
|
|
"UNIX",
|
|
|
|
"DOS",
|
|
|
|
"MAC",
|
|
|
|
"MIXED"
|
|
|
|
} ;
|
|
|
|
|
2015-02-15 00:30:54 -05:00
|
|
|
static const char *codename[] = {
|
|
|
|
"ASCII",
|
|
|
|
"UTF-8",
|
|
|
|
"EXTENDED",
|
|
|
|
"MIXED"
|
|
|
|
} ;
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
boolean restflag = FALSE ; /* restricted use? */
|
2013-10-09 02:03:56 -04:00
|
|
|
|
2015-02-15 22:39:16 -05:00
|
|
|
|
|
|
|
static int ifile( const char *fname) ;
|
|
|
|
|
|
|
|
|
2015-02-12 22:23:12 -05:00
|
|
|
boolean resterr( void) {
|
2015-02-12 23:31:59 -05:00
|
|
|
mloutfmt( "%B(That command is RESTRICTED)") ;
|
2015-02-12 22:23:12 -05:00
|
|
|
return FALSE ;
|
|
|
|
}
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/*
|
|
|
|
* Read a file into the current
|
2016-04-01 01:25:05 -04:00
|
|
|
* buffer. This is really easy; all you do is
|
2005-05-31 11:50:56 -04:00
|
|
|
* find the name of the file, and call the standard
|
|
|
|
* "read a file into the current buffer" code.
|
|
|
|
* Bound to "C-X C-R".
|
|
|
|
*/
|
2015-10-04 23:34:33 -04:00
|
|
|
int fileread( int f, int n) {
|
2016-04-01 01:25:05 -04:00
|
|
|
int status ;
|
|
|
|
char *fname ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( restflag) /* don't allow this command if restricted */
|
|
|
|
return resterr() ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
status = newmlarg( &fname, "Read file: ", sizeof( fname_t)) ;
|
|
|
|
if( status == TRUE) {
|
|
|
|
status = readin( fname, TRUE) ;
|
|
|
|
free( fname) ;
|
|
|
|
}
|
2015-10-04 23:34:33 -04:00
|
|
|
|
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a file into the current
|
2016-04-01 01:25:05 -04:00
|
|
|
* buffer. This is really easy; all you do is
|
2005-05-31 11:50:56 -04:00
|
|
|
* find the name of the file, and call the standard
|
|
|
|
* "insert a file into the current buffer" code.
|
|
|
|
* Bound to "C-X C-I".
|
|
|
|
*/
|
2015-10-04 23:34:33 -04:00
|
|
|
int insfile( int f, int n) {
|
2016-04-01 01:25:05 -04:00
|
|
|
int status ;
|
|
|
|
char *fname ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( restflag) /* don't allow this command if restricted */
|
|
|
|
return resterr() ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( curbp->b_mode & MDVIEW) /* don't allow this command if */
|
|
|
|
return rdonly() ; /* we are in read only mode */
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
status = newmlarg( &fname, "Insert file: ", sizeof( fname_t)) ;
|
|
|
|
if( status == TRUE) {
|
|
|
|
status = ifile( fname) ;
|
|
|
|
free( fname) ;
|
2015-10-04 23:34:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( status != TRUE)
|
2016-04-01 01:25:05 -04:00
|
|
|
return status ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
return reposition( TRUE, -1) ; /* Redraw with dot at bottom of window */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select a file for editing.
|
|
|
|
* Look around to see if you can find the
|
|
|
|
* fine in another buffer; if you can find it
|
|
|
|
* just switch to the buffer. If you cannot find
|
|
|
|
* the file, create a new buffer, read in the
|
|
|
|
* text, and switch to the new buffer.
|
|
|
|
* Bound to C-X C-F.
|
|
|
|
*/
|
2015-10-04 23:34:33 -04:00
|
|
|
int filefind( int f, int n) {
|
2016-04-01 01:25:05 -04:00
|
|
|
char *fname ; /* file user wishes to find */
|
|
|
|
int status ; /* status return */
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( restflag) /* don't allow this command if restricted */
|
|
|
|
return resterr() ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
status = newmlarg( &fname, "Find file: ", sizeof( fname_t)) ;
|
|
|
|
if( status == TRUE) {
|
2015-10-04 23:34:33 -04:00
|
|
|
status = getfile( fname, TRUE) ;
|
|
|
|
free( fname) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
static void upd_mode( void) {
|
|
|
|
struct window *wp ;
|
|
|
|
|
|
|
|
/* Update mode lines */
|
|
|
|
for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp)
|
|
|
|
if( wp->w_bufp == curbp)
|
|
|
|
wp->w_flag |= WFMODE ;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:34:33 -04:00
|
|
|
int viewfile( int f, int n) { /* visit a file in VIEW mode */
|
2016-04-01 01:25:05 -04:00
|
|
|
char *fname ; /* file user wishes to find */
|
|
|
|
int status ; /* status return */
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( restflag) /* don't allow this command if restricted */
|
|
|
|
return resterr() ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
status = newmlarg( &fname, "View file: ", sizeof( fname_t)) ;
|
|
|
|
if( status == TRUE) {
|
|
|
|
status = getfile(fname, FALSE) ;
|
|
|
|
free( fname) ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( status == TRUE) { /* if we succeed, put it in view mode */
|
|
|
|
curwp->w_bufp->b_mode |= MDVIEW ;
|
|
|
|
upd_mode() ;
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* getfile()
|
|
|
|
*
|
2016-04-01 01:25:05 -04:00
|
|
|
* char fname[]; file name to find
|
|
|
|
* boolean lockfl; check the file for locks?
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2016-04-01 01:25:05 -04:00
|
|
|
int getfile( const char *fname, boolean lockfl) {
|
|
|
|
struct buffer *bp;
|
|
|
|
int s;
|
|
|
|
bname_t bname ; /* buffer name to put file */
|
|
|
|
|
|
|
|
/* Is the file in current buffer? */
|
|
|
|
if( strcmp( curbp->b_fname, fname) == 0) {
|
|
|
|
mloutstr( "(Current buffer)") ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the file in any buffer? */
|
|
|
|
for( bp = bheadp; bp != NULL; bp = bp->b_bufp) {
|
|
|
|
if( (bp->b_flag & BFINVS) == 0
|
|
|
|
&& strcmp( bp->b_fname, fname) == 0) {
|
|
|
|
line_p lp ;
|
|
|
|
int i ;
|
|
|
|
|
|
|
|
swbuffer( bp) ;
|
|
|
|
|
|
|
|
/* Center dotted line in window */
|
|
|
|
i = curwp->w_ntrows / 2 ;
|
|
|
|
for( lp = curwp->w_dotp ; lback( lp) != curbp->b_linep ; lp = lback( lp))
|
|
|
|
if( i-- == 0)
|
|
|
|
break ;
|
|
|
|
|
|
|
|
curwp->w_linep = lp ;
|
|
|
|
|
|
|
|
/* Refresh window */
|
|
|
|
curwp->w_flag |= WFMODE | WFHARD ;
|
|
|
|
cknewwindow() ;
|
|
|
|
mloutstr( "(Old buffer)") ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
makename(bname, fname); /* New buffer name. */
|
|
|
|
while ((bp = bfind(bname, FALSE, 0)) != NULL) {
|
|
|
|
char *new_bname ;
|
|
|
|
|
|
|
|
/* old buffer name conflict code */
|
|
|
|
s = newmlarg( &new_bname, "Buffer name: ", sizeof( bname_t)) ;
|
|
|
|
if( s == ABORT) /* ^G to just quit */
|
|
|
|
return s ;
|
|
|
|
else if (s == FALSE) { /* CR to clobber it */
|
|
|
|
makename( bname, fname) ;
|
|
|
|
break ;
|
|
|
|
} else { /* TRUE */
|
|
|
|
strncpy( bname, new_bname, sizeof bname - 1) ;
|
|
|
|
bname[ sizeof bname - 1] = '\0' ;
|
|
|
|
free( new_bname) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) {
|
|
|
|
mloutstr( "Cannot create buffer") ;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (--curbp->b_nwnd == 0) { /* Undisplay. */
|
|
|
|
curbp->b_dotp = curwp->w_dotp;
|
|
|
|
curbp->b_doto = curwp->w_doto;
|
|
|
|
curbp->b_markp = curwp->w_markp;
|
|
|
|
curbp->b_marko = curwp->w_marko;
|
|
|
|
}
|
|
|
|
curbp = bp; /* Switch to it. */
|
|
|
|
curwp->w_bufp = bp;
|
|
|
|
curbp->b_nwnd++;
|
|
|
|
s = readin(fname, lockfl); /* Read it in. */
|
|
|
|
cknewwindow();
|
|
|
|
return s;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-01 01:52:45 -04:00
|
|
|
* Read file "fname" into the current buffer, blowing away any text
|
|
|
|
* found there. Called by both the read and find commands. Return
|
|
|
|
* the final status of the read. Also called by the mainline, to
|
|
|
|
* read in a file specified on the command line as an argument.
|
|
|
|
* The command bound to M-FNR is called after the buffer is set up
|
|
|
|
* and before it is read.
|
|
|
|
*
|
2016-04-01 01:25:05 -04:00
|
|
|
* char fname[]; name of file to read
|
|
|
|
* boolean lockfl; check for file locks?
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2013-09-19 03:33:56 -04:00
|
|
|
int readin(const char *fname, boolean lockfl)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2016-04-01 01:25:05 -04:00
|
|
|
struct window *wp;
|
|
|
|
struct buffer *bp;
|
2016-03-30 05:44:46 -04:00
|
|
|
int status ;
|
|
|
|
fio_code s ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
bp = curbp; /* Cheap. */
|
2013-05-19 05:36:24 -04:00
|
|
|
#if (FILOCK && BSD) || SVR4
|
2016-04-01 01:25:05 -04:00
|
|
|
if( lockfl && lockchk( fname) == ABORT)
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
2016-04-01 01:25:05 -04:00
|
|
|
{
|
|
|
|
s = FIOFNF;
|
|
|
|
strcpy(bp->b_fname, "");
|
2016-03-30 08:14:19 -04:00
|
|
|
mloutstr( "(File in use)") ;
|
2016-04-01 01:25:05 -04:00
|
|
|
goto out;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2016-04-01 01:25:05 -04:00
|
|
|
return ABORT;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
#endif
|
2016-04-01 01:25:05 -04:00
|
|
|
if( (status = bclear( bp)) != TRUE) /* Might be old. */
|
|
|
|
return status ;
|
2016-03-30 05:44:46 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
bp->b_flag &= ~(BFINVS | BFCHG);
|
|
|
|
strncpy( bp->b_fname, fname, sizeof( fname_t) - 1) ;
|
|
|
|
bp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
/* let a user macro get hold of things...if he wants */
|
|
|
|
execute(META | SPEC | 'R', FALSE, 1);
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
s = ffropen( bp->b_fname) ; /* Always use the name associated to buffer */
|
2016-04-01 01:25:05 -04:00
|
|
|
if( s == FIOFNF) /* File not found. */
|
2016-03-30 05:44:46 -04:00
|
|
|
mloutstr( "(New file)") ;
|
|
|
|
else if( s == FIOSUC) {
|
|
|
|
char *errmsg ;
|
2016-04-01 01:25:05 -04:00
|
|
|
eoltype found_eol ;
|
|
|
|
int nline = 0 ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
/* read the file in */
|
|
|
|
mloutstr( "(Reading file)") ;
|
|
|
|
while ((s = ffgetline()) == FIOSUC) {
|
|
|
|
line_p lp ;
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
if( nline >= 10000000 /* MAXNLINE Maximum # of lines from one file */
|
|
|
|
|| (lp = lalloc( fpayload)) == NULL) {
|
2016-04-01 01:25:05 -04:00
|
|
|
s = FIOMEM ; /* Keep message on the */
|
|
|
|
break ; /* display. */
|
|
|
|
}
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
memcpy( lp->l_text, fline, fpayload) ;
|
2016-04-01 01:25:05 -04:00
|
|
|
lp->l_fp = curbp->b_linep ; /* insert before end of buffer */
|
2016-03-30 05:44:46 -04:00
|
|
|
lp->l_bp = lp->l_fp->l_bp ;
|
|
|
|
lp->l_fp->l_bp = lp ;
|
|
|
|
lp->l_bp->l_fp = lp ;
|
|
|
|
nline += 1 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( s == FIOERR)
|
|
|
|
mloutstr( "File read error") ;
|
|
|
|
|
|
|
|
switch( ftype) {
|
2016-04-01 01:25:05 -04:00
|
|
|
case FTYPE_DOS:
|
|
|
|
found_eol = EOL_DOS ;
|
|
|
|
curbp->b_mode |= MDDOS ;
|
|
|
|
break ;
|
|
|
|
case FTYPE_UNIX:
|
|
|
|
found_eol = EOL_UNIX ;
|
|
|
|
break ;
|
|
|
|
case FTYPE_MAC:
|
|
|
|
found_eol = EOL_MAC ;
|
|
|
|
break ;
|
|
|
|
case FTYPE_NONE:
|
|
|
|
found_eol = EOL_NONE ;
|
|
|
|
break ;
|
|
|
|
default:
|
|
|
|
found_eol = EOL_MIXED ;
|
2016-03-30 05:44:46 -04:00
|
|
|
curbp->b_mode |= MDVIEW ; /* force view mode as we have lost
|
|
|
|
** EOL information */
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fcode == FCODE_UTF_8)
|
|
|
|
curbp->b_mode |= MDUTF8 ;
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( s == FIOERR) {
|
2016-03-30 05:44:46 -04:00
|
|
|
errmsg = "I/O ERROR, " ;
|
2016-04-01 01:25:05 -04:00
|
|
|
curbp->b_flag |= BFTRUNC ;
|
|
|
|
} else if( s == FIOMEM) {
|
|
|
|
errmsg = "OUT OF MEMORY, " ;
|
|
|
|
curbp->b_flag |= BFTRUNC ;
|
|
|
|
} else
|
|
|
|
errmsg = "" ;
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)",
|
|
|
|
errmsg,
|
|
|
|
nline,
|
|
|
|
&"s"[ nline == 1],
|
|
|
|
codename[ fcode & (FCODE_MASK - 1)],
|
|
|
|
eolname[ found_eol]) ;
|
2016-04-01 01:25:05 -04:00
|
|
|
ffclose() ; /* Ignore errors. */
|
2013-06-11 22:41:28 -04:00
|
|
|
}
|
2013-06-08 23:21:35 -04:00
|
|
|
|
2016-03-30 08:14:19 -04:00
|
|
|
out:
|
2016-04-01 01:25:05 -04:00
|
|
|
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
|
|
|
|
if (wp->w_bufp == curbp) {
|
|
|
|
wp->w_linep = lforw(curbp->b_linep);
|
|
|
|
wp->w_dotp = lforw(curbp->b_linep);
|
|
|
|
wp->w_doto = 0;
|
|
|
|
wp->w_markp = NULL;
|
|
|
|
wp->w_marko = 0;
|
|
|
|
wp->w_flag |= WFMODE | WFHARD;
|
|
|
|
}
|
|
|
|
}
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
return (s == FIOERR || s == FIOFNF) ? FALSE : TRUE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a file name, and from it
|
|
|
|
* fabricate a buffer name. This routine knows
|
|
|
|
* about the syntax of file names on the target system.
|
|
|
|
* I suppose that this information could be put in
|
|
|
|
* a better place than a line of code.
|
|
|
|
*/
|
2014-05-30 21:36:25 -04:00
|
|
|
void makename( bname_t bname, const char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2016-04-01 01:25:05 -04:00
|
|
|
const char *cp1;
|
|
|
|
char *cp2;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
cp1 = &fname[0];
|
|
|
|
while (*cp1 != 0)
|
|
|
|
++cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
#if VMS
|
2013-05-19 05:36:24 -04:00
|
|
|
#if PKCODE
|
2016-04-01 01:25:05 -04:00
|
|
|
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']'
|
|
|
|
&& cp1[-1] != '>')
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2016-04-01 01:25:05 -04:00
|
|
|
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']')
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2016-04-01 01:25:05 -04:00
|
|
|
--cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2016-04-01 01:25:05 -04:00
|
|
|
#if MSDOS
|
|
|
|
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != '\\'
|
|
|
|
&& cp1[-1] != '/')
|
|
|
|
--cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2016-04-01 01:25:05 -04:00
|
|
|
#if V7 | USG | BSD
|
|
|
|
while (cp1 != &fname[0] && cp1[-1] != '/')
|
|
|
|
--cp1;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2016-04-01 01:25:05 -04:00
|
|
|
cp2 = &bname[0];
|
|
|
|
while( cp2 != &bname[ sizeof( bname_t) - 1] && *cp1 != 0 && *cp1 != ';')
|
|
|
|
*cp2++ = *cp1++;
|
|
|
|
*cp2 = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* make sure a buffer name is unique
|
|
|
|
*
|
2016-04-01 01:25:05 -04:00
|
|
|
* char *name; name to check on
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
|
|
|
void unqname(char *name)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2016-04-01 01:25:05 -04:00
|
|
|
char *sp;
|
|
|
|
|
|
|
|
/* check to see if it is in the buffer list */
|
|
|
|
while (bfind(name, 0, FALSE) != NULL) {
|
|
|
|
|
|
|
|
/* go to the end of the name */
|
|
|
|
sp = name;
|
|
|
|
while (*sp)
|
|
|
|
++sp;
|
|
|
|
if (sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) {
|
|
|
|
*sp++ = '0';
|
|
|
|
*sp = 0;
|
|
|
|
} else
|
|
|
|
*(--sp) += 1;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask for a file name, and write the
|
|
|
|
* contents of the current buffer to that file.
|
|
|
|
* Update the remembered file name and clear the
|
|
|
|
* buffer changed flag. This handling of file names
|
|
|
|
* is different from the earlier versions, and
|
|
|
|
* is more compatable with Gosling EMACS than
|
|
|
|
* with ITS EMACS. Bound to "C-X C-W".
|
|
|
|
*/
|
2015-10-04 23:34:33 -04:00
|
|
|
int filewrite( int f, int n) {
|
2016-04-01 01:25:05 -04:00
|
|
|
int status ;
|
|
|
|
char *fname ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( restflag) /* don't allow this command if restricted */
|
|
|
|
return resterr() ;
|
2015-10-04 23:34:33 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
status = newmlarg( &fname, "Write file: ", sizeof( fname_t)) ;
|
|
|
|
if( status == TRUE) {
|
2016-03-18 23:37:54 -04:00
|
|
|
if( strlen( fname) > sizeof( fname_t) - 1)
|
|
|
|
status = FALSE ;
|
|
|
|
else {
|
2016-04-01 01:25:05 -04:00
|
|
|
status = writeout( fname) ;
|
|
|
|
if( status == TRUE)
|
|
|
|
strcpy( curbp->b_fname, fname) ;
|
2016-03-18 23:37:54 -04:00
|
|
|
}
|
2016-04-01 01:25:05 -04:00
|
|
|
|
2015-10-04 23:34:33 -04:00
|
|
|
free( fname) ;
|
|
|
|
}
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the contents of the current
|
2016-03-30 05:44:46 -04:00
|
|
|
* buffer in its associated file. Do nothing
|
2005-05-31 11:50:56 -04:00
|
|
|
* if nothing has changed (this may be a bug, not a
|
|
|
|
* feature). Error if there is no remembered file
|
|
|
|
* name for the buffer. Bound to "C-X C-S". May
|
|
|
|
* get called by "C-Z".
|
|
|
|
*/
|
2016-04-01 01:25:05 -04:00
|
|
|
int filesave( int f, int n) {
|
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
|
|
|
return rdonly(); /* we are in read only mode */
|
|
|
|
if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */
|
|
|
|
return TRUE;
|
|
|
|
if (curbp->b_fname[0] == 0) { /* Must have a name. */
|
|
|
|
mloutstr( "No file name") ;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* complain about truncated files */
|
|
|
|
if ((curbp->b_flag & BFTRUNC) != 0) {
|
|
|
|
if (mlyesno("Truncated file ... write it out") == FALSE) {
|
|
|
|
mloutstr( "(Aborted)") ;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeout( curbp->b_fname) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function performs the details of file
|
|
|
|
* writing. Uses the file management routines in the
|
|
|
|
* "fileio.c" package. The number of lines written is
|
2010-04-17 19:38:09 -04:00
|
|
|
* displayed. Sadly, it looks inside a struct line; provide
|
2005-05-31 11:50:56 -04:00
|
|
|
* a macro for this. Most of the grief is error
|
|
|
|
* checking of some sort.
|
|
|
|
*/
|
2016-03-30 05:44:46 -04:00
|
|
|
int writeout( const char *fn) {
|
|
|
|
fio_code s ;
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
s = ffwopen( fn) ; /* Open writes message. */
|
2016-03-30 05:44:46 -04:00
|
|
|
if( s != FIOSUC)
|
|
|
|
mloutstr( "Cannot open file for writing") ;
|
|
|
|
else {
|
|
|
|
line_p lp ;
|
|
|
|
fio_code s2 ;
|
|
|
|
int nline = 0 ;
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
mloutstr( "(Writing...)") ; /* tell us we are writing */
|
2016-03-30 05:44:46 -04:00
|
|
|
for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) {
|
|
|
|
s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ;
|
|
|
|
if( s != FIOSUC)
|
|
|
|
break ;
|
2016-04-01 01:25:05 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
nline += 1 ;
|
|
|
|
}
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
s2 = ffclose() ;
|
|
|
|
if( s != FIOSUC)
|
|
|
|
mloutstr( "Write I/O error") ;
|
|
|
|
else if( s2 != FIOSUC)
|
2015-02-12 23:31:59 -05:00
|
|
|
mloutstr( "Error closing file") ;
|
2016-03-30 05:44:46 -04:00
|
|
|
else { /* Successfull write and close. */
|
|
|
|
mloutfmt( "(Wrote %d line%s)", nline, &"s"[ nline == 1]) ;
|
2016-04-01 01:25:05 -04:00
|
|
|
curbp->b_flag &= ~BFCHG ;
|
|
|
|
upd_mode() ;
|
2016-03-30 05:44:46 -04:00
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
return FALSE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The command allows the user
|
|
|
|
* to modify the file name associated with
|
|
|
|
* the current buffer. It is like the "f" command
|
|
|
|
* in UNIX "ed". The operation is simple; just zap
|
2010-02-15 21:09:42 -05:00
|
|
|
* the name in the buffer structure, and mark the windows
|
2005-05-31 11:50:56 -04:00
|
|
|
* as needing an update. You can type a blank line at the
|
|
|
|
* prompt if you wish.
|
|
|
|
*/
|
2015-10-04 23:34:33 -04:00
|
|
|
int filename( int f, int n) {
|
2016-04-01 01:25:05 -04:00
|
|
|
int status ;
|
|
|
|
char *fname ;
|
|
|
|
|
|
|
|
if( restflag) /* don't allow this command if restricted */
|
|
|
|
return resterr() ;
|
|
|
|
|
|
|
|
status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ;
|
|
|
|
if( status == ABORT)
|
|
|
|
return status ;
|
|
|
|
else if( status == FALSE)
|
|
|
|
curbp->b_fname[ 0] = '\0' ;
|
|
|
|
else { /* TRUE */
|
|
|
|
strncpy( curbp->b_fname, fname, sizeof( fname_t) - 1) ;
|
|
|
|
curbp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
|
|
|
|
free( fname) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */
|
|
|
|
upd_mode() ;
|
|
|
|
return TRUE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert file "fname" into the current
|
|
|
|
* buffer, Called by insert file command. Return the final
|
|
|
|
* status of the read.
|
|
|
|
*/
|
2015-02-15 22:39:16 -05:00
|
|
|
static int ifile( const char *fname) {
|
2016-03-30 05:44:46 -04:00
|
|
|
fio_code s ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
curbp->b_flag |= BFCHG ; /* we have changed */
|
2016-03-30 05:44:46 -04:00
|
|
|
curbp->b_flag &= ~BFINVS ; /* and are not temporary */
|
|
|
|
s = ffropen( fname) ;
|
2016-04-01 01:25:05 -04:00
|
|
|
if( s == FIOFNF) { /* File not found. */
|
|
|
|
mloutstr( "(No such file)") ;
|
|
|
|
return FALSE ;
|
|
|
|
}
|
2016-03-30 05:44:46 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
if( s == FIOSUC) { /* Hard file open. */
|
|
|
|
int nline = 0 ; /* number of line read */
|
2016-03-30 05:44:46 -04:00
|
|
|
char *errmsg ;
|
|
|
|
|
|
|
|
mloutstr( "(Inserting file)") ;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
/* back up a line and save the mark here */
|
2016-03-30 05:44:46 -04:00
|
|
|
curwp->w_dotp = lback(curwp->w_dotp);
|
|
|
|
curwp->w_doto = 0;
|
|
|
|
curwp->w_markp = curwp->w_dotp;
|
|
|
|
curwp->w_marko = 0;
|
|
|
|
|
|
|
|
while( (s = ffgetline()) == FIOSUC) {
|
|
|
|
line_p lpp, lp, lpn ;
|
|
|
|
|
|
|
|
if( (lp = lalloc( fpayload)) == NULL) {
|
2016-04-01 01:25:05 -04:00
|
|
|
s = FIOMEM ; /* Keep message on the */
|
|
|
|
break ; /* display. */
|
2016-03-30 05:44:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( lp->l_text, fline, fpayload) ;
|
|
|
|
lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */
|
|
|
|
lp->l_fp = lpn = lpp->l_fp ; /* line after insert */
|
|
|
|
|
|
|
|
/* re-link new line between lpp and lpn */
|
|
|
|
lpn->l_bp = lp ;
|
|
|
|
lpp->l_fp = lp ;
|
|
|
|
|
|
|
|
/* and advance and write out the current line */
|
|
|
|
curwp->w_dotp = lp ;
|
|
|
|
nline += 1 ;
|
2016-04-01 01:25:05 -04:00
|
|
|
}
|
2016-03-30 05:44:46 -04:00
|
|
|
|
2016-04-01 01:25:05 -04:00
|
|
|
ffclose() ; /* Ignore errors. */
|
|
|
|
curwp->w_markp = lforw(curwp->w_markp);
|
2016-03-30 05:44:46 -04:00
|
|
|
if( s == FIOERR) {
|
2016-04-01 01:25:05 -04:00
|
|
|
errmsg = "I/O ERROR, " ;
|
|
|
|
curbp->b_flag |= BFTRUNC ;
|
2016-03-30 05:44:46 -04:00
|
|
|
} else if( s == FIOMEM) {
|
|
|
|
errmsg = "OUT OF MEMORY, " ;
|
2016-04-01 01:25:05 -04:00
|
|
|
curbp->b_flag |= BFTRUNC;
|
2016-03-30 05:44:46 -04:00
|
|
|
} else
|
|
|
|
errmsg = "" ;
|
|
|
|
|
|
|
|
mloutfmt( "(%sInserted %d line%s)",
|
|
|
|
errmsg,
|
|
|
|
nline,
|
|
|
|
&"s"[ nline == 1]) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to the next line and mark the window for changes */
|
2016-04-01 01:25:05 -04:00
|
|
|
curwp->w_dotp = lforw(curwp->w_dotp);
|
|
|
|
curwp->w_flag |= WFHARD | WFMODE;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
/* copy window parameters back to the buffer structure */
|
2016-04-01 01:25:05 -04:00
|
|
|
curbp->b_dotp = curwp->w_dotp;
|
|
|
|
curbp->b_doto = curwp->w_doto;
|
|
|
|
curbp->b_markp = curwp->w_markp;
|
|
|
|
curbp->b_marko = curwp->w_marko;
|
2013-05-19 05:36:24 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
return (s == FIOERR) ? FALSE : TRUE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|