1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-06-29 04:55:31 +00:00

Rework file primitives.

This commit is contained in:
Renaud 2021-08-14 11:45:41 +08:00
parent b4d69118f5
commit 3bce7a4751
4 changed files with 610 additions and 654 deletions

829
file.c
View File

@ -29,488 +29,476 @@
#include "window.h" #include "window.h"
typedef enum { typedef enum {
EOL_NONE, EOL_NONE,
EOL_UNIX, EOL_UNIX,
EOL_DOS, EOL_DOS,
EOL_MAC, EOL_MAC,
EOL_MIXED EOL_MIXED
} eoltype ; } eoltype ;
static const char *eolname[] = { static const char *eolname[] = {
"NONE", "NONE",
"UNIX", "UNIX",
"DOS", "DOS",
"MAC", "MAC",
"MIXED" "MIXED"
} ; } ;
static const char *codename[] = { static const char *codename[] = {
"ASCII", "ASCII",
"UTF-8", "UTF-8",
"EXTENDED", "EXTENDED",
"MIXED" "MIXED"
} ; } ;
boolean restflag = FALSE ; /* restricted use? */ boolean restflag = FALSE ; /* restricted use? */
static int ifile( const char *fname) ; static int ifile( const char *fname) ;
boolean resterr( void) { boolean resterr( void) {
mloutfmt( "%B(That command is RESTRICTED)") ; mloutfmt( "%B(That command is RESTRICTED)") ;
return FALSE ; return FALSE ;
} }
/* Read a file into the current buffer. This is really easy; all you do is /* Read a file into the current buffer. This is really easy; all you do is
* find the name of the file, and call the standard "read a file into the * find the name of the file, and call the standard "read a file into the
* current buffer" code. Bound to C-X C-R read-file. * current buffer" code. Bound to C-X C-R read-file.
*/ */
BINDABLE( fileread) { BINDABLE( fileread) {
char *fname ; char *fname ;
if( restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr() ; return resterr() ;
int status = newmlarg( &fname, "read-file: ", sizeof( fname_t)) ; int status = newmlarg( &fname, "read-file: ", sizeof( fname_t)) ;
if( status == TRUE) { if( status == TRUE) {
status = readin( fname, TRUE) ; status = readin( fname, TRUE) ;
free( fname) ; free( fname) ;
} }
return status ; return status ;
} }
/* Insert a file into the current buffer. This is really easy; all you do /* Insert a file into the current buffer. This is really easy; all you do
* is find the name of the file, and call the standard "insert a file into * is find the name of the file, and call the standard "insert a file into
* the current buffer" code. Bound to C-X C-I insert-file. * the current buffer" code. Bound to C-X C-I insert-file.
*/ */
BINDABLE( insfile) { BINDABLE( insfile) {
char *fname ; char *fname ;
if( restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr() ; return resterr() ;
assert( !(curbp->b_mode & MDVIEW)) ; assert( !(curbp->b_mode & MDVIEW)) ;
int status = newmlarg( &fname, "insert-file: ", sizeof( fname_t)) ; int status = newmlarg( &fname, "insert-file: ", sizeof( fname_t)) ;
if( status == TRUE) { if( status == TRUE) {
status = ifile( fname) ; status = ifile( fname) ;
free( fname) ; free( fname) ;
} }
if( status != TRUE) if( status != TRUE)
return status ; return status ;
return reposition( TRUE, -1) ; /* Redraw with dot at bottom of window */ return reposition( TRUE, -1) ; /* Redraw with dot at bottom of window */
} }
/* Select a file for editing. Look around to see if you can find the file /* Select a file for editing. Look around to see if you can find the file
* in another buffer; if you can find it just switch to the buffer. If you * 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 * cannot find the file, create a new buffer, read in the text, and switch
* to the new buffer. Bound to C-X C-F find-file. * to the new buffer. Bound to C-X C-F find-file.
*/ */
BINDABLE( filefind) { BINDABLE( filefind) {
char *fname ; /* file user wishes to find */ char *fname ; /* file user wishes to find */
if( restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr() ; return resterr() ;
int status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ; int status = newmlarg( &fname, "find-file: ", sizeof( fname_t)) ;
if( status == TRUE) { if( status == TRUE) {
status = getfile( fname, TRUE) ; status = getfile( fname, TRUE) ;
free( fname) ; free( fname) ;
} }
return status ; return status ;
} }
/* Update mode lines of all windows displaying current buffer */
static void upd_mode( void) { static void upd_mode( void) {
struct window *wp ; for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp)
if( wp->w_bufp == curbp)
/* Update mode lines */ wp->w_flag |= WFMODE ;
for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp)
if( wp->w_bufp == curbp)
wp->w_flag |= WFMODE ;
} }
BINDABLE( viewfile) { /* visit a file in VIEW mode */
char *fname ; /* file user wishes to find */
if( restflag) /* don't allow this command if restricted */ BINDABLE( viewfile) { /* visit a file in VIEW mode */
return resterr() ; char *fname ; /* file user wishes to find */
int status = newmlarg( &fname, "view-file: ", sizeof( fname_t)) ; if( restflag) /* don't allow this command if restricted */
if( status == TRUE) { return resterr() ;
status = getfile( fname, FALSE) ;
free( fname) ;
if( status == TRUE) { /* if we succeed, put it in view mode */ int status = newmlarg( &fname, "view-file: ", sizeof( fname_t)) ;
curwp->w_bufp->b_mode |= MDVIEW ; if( status == TRUE) {
upd_mode() ; status = getfile( fname, FALSE) ;
} free( fname) ;
}
return status ; if( status == TRUE) { /* if we succeed, put it in view mode */
curwp->w_bufp->b_mode |= MDVIEW ;
upd_mode() ;
}
}
return status ;
} }
/*
* getfile() /* getfile()
* *
* char fname[]; file name to find * char fname[]; file name to find
* boolean lockfl; check the file for locks? * boolean lockfl; check the file for locks?
*/ */
int getfile( const char *fname, boolean lockfl) { int getfile( const char *fname, boolean lockfl) {
buffer_p bp; buffer_p bp;
int s; int s;
bname_t bname ; /* buffer name to put file */ bname_t bname ; /* buffer name to put file */
/* Is the file in current buffer? */ /* Is the file in current buffer? */
if( strcmp( curbp->b_fname, fname) == 0) { if( strcmp( curbp->b_fname, fname) == 0) {
mloutstr( "(Current buffer)") ; mloutstr( "(Current buffer)") ;
return TRUE ; return TRUE ;
} }
/* Is the file in any buffer? */ /* Is the file in any buffer? */
for( bp = bheadp; bp != NULL; bp = bp->b_bufp) { for( bp = bheadp; bp != NULL; bp = bp->b_bufp) {
if( (bp->b_flag & BFINVS) == 0 if( (bp->b_flag & BFINVS) == 0
&& strcmp( bp->b_fname, fname) == 0) { && strcmp( bp->b_fname, fname) == 0) {
line_p lp ; line_p lp ;
int i ; int i ;
swbuffer( bp) ; swbuffer( bp) ;
/* Center dotted line in window */ /* Center dotted line in window */
i = curwp->w_ntrows / 2 ; i = curwp->w_ntrows / 2 ;
for( lp = curwp->w_dotp ; lback( lp) != curbp->b_linep ; lp = lback( lp)) for( lp = curwp->w_dotp ; lback( lp) != curbp->b_linep ; lp = lback( lp))
if( i-- == 0) if( i-- == 0)
break ; break ;
curwp->w_linep = lp ; curwp->w_linep = lp ;
/* Refresh window */ /* Refresh window */
curwp->w_flag |= WFMODE | WFHARD ; curwp->w_flag |= WFMODE | WFHARD ;
cknewwindow() ; cknewwindow() ;
mloutstr( "(Old buffer)") ; mloutstr( "(Old buffer)") ;
return TRUE ; return TRUE ;
} }
} }
makename(bname, fname); /* New buffer name. */ makename(bname, fname); /* New buffer name. */
while ((bp = bfind(bname, FALSE, 0)) != NULL) { while ((bp = bfind(bname, FALSE, 0)) != NULL) {
char *new_bname ; char *new_bname ;
/* old buffer name conflict code */ /* old buffer name conflict code */
s = newmlarg( &new_bname, "Buffer name: ", sizeof( bname_t)) ; s = newmlarg( &new_bname, "Buffer name: ", sizeof( bname_t)) ;
if( s == ABORT) /* ^G to just quit */ if( s == ABORT) /* ^G to just quit */
return s ; return s ;
else if (s == FALSE) { /* CR to clobber it */ else if (s == FALSE) { /* CR to clobber it */
makename( bname, fname) ; makename( bname, fname) ;
break ; break ;
} else { /* TRUE */ } else { /* TRUE */
mystrscpy( bname, new_bname, sizeof bname) ; mystrscpy( bname, new_bname, sizeof bname) ;
free( new_bname) ; free( new_bname) ;
} }
} }
if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) { if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) {
mloutstr( "Cannot create buffer") ; mloutstr( "Cannot create buffer") ;
return FALSE; return FALSE;
} }
if (--curbp->b_nwnd == 0) { /* Undisplay. */ if (--curbp->b_nwnd == 0) { /* Undisplay. */
curbp->b_dotp = curwp->w_dotp; curbp->b_dotp = curwp->w_dotp;
curbp->b_doto = curwp->w_doto; curbp->b_doto = curwp->w_doto;
curbp->b_markp = curwp->w_markp; curbp->b_markp = curwp->w_markp;
curbp->b_marko = curwp->w_marko; curbp->b_marko = curwp->w_marko;
} }
curbp = bp; /* Switch to it. */ curbp = bp; /* Switch to it. */
curwp->w_bufp = bp; curwp->w_bufp = bp;
curbp->b_nwnd++; curbp->b_nwnd++;
s = readin(fname, lockfl); /* Read it in. */ s = readin(fname, lockfl); /* Read it in. */
cknewwindow(); cknewwindow();
return s; return s;
} }
/* Read file "fname" into the current buffer, blowing away any text /* Read file "fname" into the current buffer, blowing away any text found
* found there. Called by both the read and find commands. Return there. Called by both the read and find commands. Return the final
* the final status of the read. Also called by the mainline, to status of the read. Also called by the mainline, to read in a file
* read in a file specified on the command line as an argument. specified on the command line as an argument. The command bound to
* The command bound to M-FNR is called after the buffer is set up M-FNR is called after the buffer is set up and before it is read.
* and before it is read.
*
* char fname[]; name of file to read
* boolean lockfl; check for file locks?
*/
int readin(const char *fname, boolean lockfl)
{
struct window *wp;
buffer_p bp;
int status ;
fio_code s ;
bp = curbp; /* Cheap. */ char fname[]; name of file to read
boolean lockfl; check for file locks?
*/
int readin( const char *fname, boolean lockfl) {
fio_code s ;
buffer_p bp = curbp ; /* Cheap. */
#if (FILOCK && BSD) || SVR4 #if (FILOCK && BSD) || SVR4
if( lockfl && lockchk( fname) == ABORT) { if( lockfl && lockchk( fname) == ABORT) {
#if PKCODE # if PKCODE
s = FIOFNF; s = FIOFNF;
strcpy(bp->b_fname, ""); strcpy(bp->b_fname, "");
mloutstr( "(File in use)") ; mloutstr( "(File in use)") ;
#else # else
return ABORT; return ABORT;
#endif # endif
} else } else
#endif #endif
{ {
if( (status = bclear( bp)) != TRUE) /* Might be old. */ int status = bclear( bp) ;
return status ; if( status != TRUE) /* Might be old. */
return status ;
bp->b_flag &= ~(BFINVS | BFCHG); bp->b_flag &= ~(BFINVS | BFCHG);
if( fname != bp->b_fname) /* Copy if source differs from destination */ if( fname != bp->b_fname) /* Copy if source differs from destination */
mystrscpy( bp->b_fname, fname, sizeof( fname_t)) ; mystrscpy( bp->b_fname, fname, sizeof( fname_t)) ;
/* let a user macro get hold of things...if he wants */ /* let a user macro get hold of things...if he wants */
execute(META | SPEC | 'R', FALSE, 1); execute(META | SPEC | 'R', FALSE, 1);
s = ffropen( bp->b_fname) ; /* Always use the name associated to buffer */ s = ffropen( bp->b_fname) ; /* Always use the name associated to buffer */
if( s == FIOFNF) /* File not found. */ if( s == FIOFNF) /* File not found. */
mloutstr( "(New file)") ; mloutstr( "(New file)") ;
else if( s == FIOSUC) { else if( s == FIOSUC) {
char *errmsg ; char *errmsg ;
eoltype found_eol ; eoltype found_eol ;
int nline = 0 ; int nline = 0 ;
/* read the file in */ /* read the file in */
mloutstr( "(Reading file)") ; mloutstr( "(Reading file)") ;
while( (s = ffgetline()) == FIOSUC) { while( (s = ffgetline()) == FIOSUC) {
line_p lp ; line_p lp ;
if( nline >= 10000000 /* Maximum # of lines from one file */ if( nline >= 10000000 /* Maximum # of lines from one file */
|| (lp = lalloc( fpayload)) == NULL) { || (lp = lalloc( fpayload)) == NULL) {
s = FIOMEM ; /* Keep message on the */ s = FIOMEM ; /* Keep message on the */
break ; /* display. */ break ; /* display. */
} }
memcpy( lp->l_text, fline, fpayload) ; memcpy( lp->l_text, fline, fpayload) ;
lp->l_fp = curbp->b_linep ; /* insert before end of buffer */ lp->l_fp = curbp->b_linep ; /* insert before end of buffer */
lp->l_bp = lp->l_fp->l_bp ; lp->l_bp = lp->l_fp->l_bp ;
lp->l_fp->l_bp = lp ; lp->l_fp->l_bp = lp ;
lp->l_bp->l_fp = lp ; lp->l_bp->l_fp = lp ;
nline += 1 ; nline += 1 ;
} }
if( s == FIOERR) if( s == FIOERR)
mloutstr( "File read error") ; mloutstr( "File read error") ;
switch( ftype) { switch( ftype) {
case FTYPE_DOS: case FTYPE_DOS:
found_eol = EOL_DOS ; found_eol = EOL_DOS ;
curbp->b_mode |= MDDOS ; curbp->b_mode |= MDDOS ;
break ; break ;
case FTYPE_UNIX: case FTYPE_UNIX:
found_eol = EOL_UNIX ; found_eol = EOL_UNIX ;
break ; break ;
case FTYPE_MAC: case FTYPE_MAC:
found_eol = EOL_MAC ; found_eol = EOL_MAC ;
break ; break ;
case FTYPE_NONE: case FTYPE_NONE:
found_eol = EOL_NONE ; found_eol = EOL_NONE ;
break ; break ;
default: default:
found_eol = EOL_MIXED ; found_eol = EOL_MIXED ;
curbp->b_mode |= MDVIEW ; /* force view mode as we have lost curbp->b_mode |= MDVIEW ; /* force view mode as we have lost
** EOL information */ ** EOL information */
} }
if( fcode == FCODE_UTF_8) if( fcode == FCODE_UTF_8)
curbp->b_mode |= MDUTF8 ; curbp->b_mode |= MDUTF8 ;
if( s == FIOERR if( s == FIOERR
|| s == FIOMEM) { || s == FIOMEM) {
errmsg = (s == FIOERR) ? "I/O ERROR, " : "OUT OF MEMORY, " ; errmsg = (s == FIOERR) ? "I/O ERROR, " : "OUT OF MEMORY, " ;
curbp->b_flag |= BFTRUNC ; curbp->b_flag |= BFTRUNC ;
curbp->b_mode |= MDVIEW ; /* force view mode as lost data */ curbp->b_mode |= MDVIEW ; /* force view mode as lost data */
} else } else
errmsg = "" ; errmsg = "" ;
mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)", mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)",
errmsg, errmsg,
nline, nline,
&"s"[ nline == 1], &"s"[ nline == 1],
codename[ fcode & (FCODE_MASK - 1)], codename[ fcode],
eolname[ found_eol]) ; eolname[ found_eol]) ;
ffclose() ; /* Ignore errors. */ ffclose() ; /* Ignore errors. */
} }
} }
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) { if( wp->w_bufp == curbp) {
wp->w_linep = lforw(curbp->b_linep); wp->w_linep = lforw( curbp->b_linep) ;
wp->w_dotp = lforw(curbp->b_linep); wp->w_dotp = lforw(curbp->b_linep) ;
wp->w_doto = 0; wp->w_doto = 0 ;
wp->w_markp = NULL; wp->w_markp = NULL ;
wp->w_marko = 0; wp->w_marko = 0 ;
wp->w_flag |= WFMODE | WFHARD; wp->w_flag |= WFMODE | WFHARD ;
} }
} }
return (s == FIOERR || s == FIOFNF) ? FALSE : TRUE ; return (s == FIOERR || s == FIOFNF) ? FALSE : TRUE ;
} }
/*
* Take a file name, and from it /* Take a file name, and from it fabricate a buffer name. This routine
* fabricate a buffer name. This routine knows knows about the syntax of file names on the target system. I suppose
* about the syntax of file names on the target system. that this information could be put in a better place than a line of
* I suppose that this information could be put in code.
* a better place than a line of code.
*/ */
void makename( bname_t bname, const char *fname) void makename( bname_t bname, const char *fname) {
{ const char *cp1 = fname ;
const char *cp1; for( const char *cp = cp1 ; *cp ; cp++)
char *cp2; if( *cp == '/')
cp1 = cp + 1 ;
cp1 = &fname[0]; char *cp2 = bname ;
while (*cp1 != 0) while( *cp1 != 0 && *cp1 != ';') {
++cp1; unicode_t c ;
int n = utf8_to_unicode( cp1, 0, 4, &c) ;
if( cp2 + n <= &bname[ sizeof( bname_t) - 2]) /* 1 digit buffer name conflict [0..9] + EOS */
while( n--)
*cp2++ = *cp1++ ;
else
break ;
}
while (cp1 != &fname[0] && cp1[-1] != '/') *cp2 = 0 ;
--cp1;
cp2 = &bname[0];
while( *cp1 != 0 && *cp1 != ';') {
unicode_t c ;
int n ;
n = utf8_to_unicode( cp1, 0, 4, &c) ;
if( cp2 + n <= &bname[ sizeof( bname_t) - 2]) /* 1 digit buffer name conflict [0..9] + EOS */
while( n--)
*cp2++ = *cp1++ ;
else
break ;
}
*cp2 = 0;
} }
/*
* make sure a buffer name is unique /* make sure a buffer name is unique
* *
* char *name; name to check on * char *name; name to check on
*/ */
void unqname(char *name) void unqname( char *name) {
{ /* check to see if it is in the buffer list */
char *sp; while( bfind( name, 0, FALSE) != NULL) {
/* go to the end of the name */
char *sp = name ;
while( *sp)
++sp ;
/* check to see if it is in the buffer list */ if( sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) {
while (bfind(name, 0, FALSE) != NULL) { *sp++ = '0' ;
*sp = 0 ;
/* go to the end of the name */ } else
sp = name; *(--sp) += 1 ;
while (*sp) }
++sp;
if (sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) {
*sp++ = '0';
*sp = 0;
} else
*(--sp) += 1;
}
} }
/* Ask for a file name, and write the content of the current buffer to that /* Ask for a file name, and write the content of the current buffer to that
* file. Update the remembered file name and clear the buffer changed * file. Update the remembered file name and clear the buffer changed
* flag. Bound to C-X C-W write-file. * flag. Bound to C-X C-W write-file.
*/ */
BINDABLE( filewrite) { BINDABLE( filewrite) {
char *fname ; char *fname ;
if( restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr() ; return resterr() ;
int status = newmlarg( &fname, "write-file: ", sizeof( fname_t)) ; int status = newmlarg( &fname, "write-file: ", sizeof( fname_t)) ;
if( status == TRUE) { if( status == TRUE) {
if( strlen( fname) > sizeof( fname_t) - 1) if( strlen( fname) > sizeof( fname_t) - 1)
status = FALSE ; status = FALSE ;
else { else {
status = writeout( fname) ; status = writeout( fname) ;
if( status == TRUE) if( status == TRUE)
strcpy( curbp->b_fname, fname) ; strcpy( curbp->b_fname, fname) ;
} }
free( fname) ; free( fname) ;
} }
return status ; return status ;
} }
/* Save the content of the current buffer in its associated file. Do /* Save the content of the current buffer in its associated file. Do
* nothing if nothing has changed (this may be a bug, not a feature). nothing 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 Error if there is no remembered file name for the buffer. Bound to "C-X
* C-S save-file". May get called by "M-Z quick-exit". C-S save-file". May get called by "M-Z quick-exit".
*/ */
BINDABLE( filesave) { BINDABLE( filesave) {
assert( !(curbp->b_mode & MDVIEW)) ; assert( !(curbp->b_mode & MDVIEW)) ;
if( (curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ if( (curbp->b_flag & BFCHG) == 0) /* Return, no changes. */
return TRUE ; return TRUE ;
if( curbp->b_fname[0] == 0) { /* Must have a name. */ if( curbp->b_fname[0] == 0) { /* Must have a name. */
mloutfmt( "%BNo file name") ; mloutfmt( "%BNo file name") ;
return FALSE ; return FALSE ;
} }
/* complain about truncated files */ /* complain about truncated files */
if( (curbp->b_flag & BFTRUNC) != 0 if( (curbp->b_flag & BFTRUNC) != 0
&& mlyesno( "Truncated file ... write it out") == FALSE) && mlyesno( "Truncated file ... write it out") == FALSE)
return mloutfail( "(Aborted)") ; return mloutfail( "(Aborted)") ;
return writeout( curbp->b_fname) ; return writeout( curbp->b_fname) ;
} }
/*
* This function performs the details of file /* This function performs the details of file writing. Uses the file
* writing. Uses the file management routines in the management routines in the "fileio.c" package. The number of lines
* "fileio.c" package. The number of lines written is written is displayed. Sadly, it looks inside a struct line; provide a
* displayed. Sadly, it looks inside a struct line; provide macro for this. Most of the grief is error checking of some sort.
* a macro for this. Most of the grief is error
* checking of some sort.
*/ */
int writeout( const char *fn) { int writeout( const char *fn) {
fio_code s ; fio_code s ;
s = ffwopen( fn) ; /* Open writes message. */ s = ffwopen( fn) ; /* Open writes message. */
if( s != FIOSUC) if( s != FIOSUC)
mloutstr( "Cannot open file for writing") ; mloutstr( "Cannot open file for writing") ;
else { else {
line_p lp ; line_p lp ;
fio_code s2 ; fio_code s2 ;
int nline = 0 ; int nline = 0 ;
mloutstr( "(Writing...)") ; /* tell us we are writing */ mloutstr( "(Writing...)") ; /* tell us we are writing */
for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) { for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) {
s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ; s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ;
if( s != FIOSUC) if( s != FIOSUC)
break ; break ;
nline += 1 ; nline += 1 ;
} }
s2 = ffclose() ; s2 = ffclose() ;
if( s != FIOSUC) if( s != FIOSUC)
mloutstr( "Write I/O error") ; mloutstr( "Write I/O error") ;
else if( s2 != FIOSUC) else if( s2 != FIOSUC)
mloutstr( "Error closing file") ; mloutstr( "Error closing file") ;
else { /* Successfull write and close. */ else { /* Successfull write and close. */
mloutfmt( "(Wrote %d line%s)", nline, &"s"[ nline == 1]) ; mloutfmt( "(Wrote %d line%s)", nline, &"s"[ nline == 1]) ;
curbp->b_flag &= ~BFCHG ; curbp->b_flag &= ~BFCHG ;
upd_mode() ; upd_mode() ;
return TRUE ; return TRUE ;
} }
} }
return FALSE ; return FALSE ;
} }
/* The command allows the user to modify the file name associated with the /* 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 current buffer. It is like the "f" command in UNIX "ed". The operation
is simple; just zap the name in the buffer structure, and mark the is simple; just zap the name in the buffer structure, and mark the
@ -518,103 +506,102 @@ int writeout( const char *fn) {
if you wish. Bound to C-X N change-file-name. if you wish. Bound to C-X N change-file-name.
*/ */
BINDABLE( filename) { BINDABLE( filename) {
char *fname ; char *fname ;
if( restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr() ; return resterr() ;
int status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ; int status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ;
if( status == ABORT) if( status == ABORT)
return status ; return status ;
else if( status == FALSE) else if( status == FALSE)
curbp->b_fname[ 0] = '\0' ; curbp->b_fname[ 0] = '\0' ;
else { /* TRUE */ else { /* TRUE */
mystrscpy( curbp->b_fname, fname, sizeof( fname_t)) ; mystrscpy( curbp->b_fname, fname, sizeof( fname_t)) ;
free( fname) ; free( fname) ;
} }
curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */ curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */
upd_mode() ; upd_mode() ;
return TRUE ; return TRUE ;
} }
/*
* Insert file "fname" into the current /* Insert file "fname" into the current buffer, Called by insert file
* buffer, Called by insert file command. Return the final command. Return the final status of the read.
* status of the read.
*/ */
static int ifile( const char *fname) { static int ifile( const char *fname) {
fio_code s ; fio_code s ;
curbp->b_flag |= BFCHG ; /* we have changed */ curbp->b_flag |= BFCHG ; /* we have changed */
curbp->b_flag &= ~BFINVS ; /* and are not temporary */ curbp->b_flag &= ~BFINVS ; /* and are not temporary */
s = ffropen( fname) ; s = ffropen( fname) ;
if( s == FIOFNF) { /* File not found. */ if( s == FIOFNF) { /* File not found. */
mloutstr( "(No such file)") ; mloutstr( "(No such file)") ;
return FALSE ; return FALSE ;
} }
if( s == FIOSUC) { /* Hard file open. */ if( s == FIOSUC) { /* Hard file open. */
int nline = 0 ; /* number of line read */ int nline = 0 ; /* number of line read */
char *errmsg ; char *errmsg ;
mloutstr( "(Inserting file)") ; mloutstr( "(Inserting file)") ;
/* back up a line and save the mark here */ /* back up a line and save the mark here */
curwp->w_dotp = lback(curwp->w_dotp); curwp->w_dotp = lback(curwp->w_dotp);
curwp->w_doto = 0; curwp->w_doto = 0;
curwp->w_markp = curwp->w_dotp; curwp->w_markp = curwp->w_dotp;
curwp->w_marko = 0; curwp->w_marko = 0;
while( (s = ffgetline()) == FIOSUC) { while( (s = ffgetline()) == FIOSUC) {
line_p lpp, lp, lpn ; line_p lpp, lp, lpn ;
if( (lp = lalloc( fpayload)) == NULL) { if( (lp = lalloc( fpayload)) == NULL) {
s = FIOMEM ; /* Keep message on the */ s = FIOMEM ; /* Keep message on the */
break ; /* display. */ break ; /* display. */
} }
memcpy( lp->l_text, fline, fpayload) ; memcpy( lp->l_text, fline, fpayload) ;
lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */ lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */
lp->l_fp = lpn = lpp->l_fp ; /* line after insert */ lp->l_fp = lpn = lpp->l_fp ; /* line after insert */
/* re-link new line between lpp and lpn */ /* re-link new line between lpp and lpn */
lpn->l_bp = lp ; lpn->l_bp = lp ;
lpp->l_fp = lp ; lpp->l_fp = lp ;
/* and advance and write out the current line */ /* and advance and write out the current line */
curwp->w_dotp = lp ; curwp->w_dotp = lp ;
nline += 1 ; nline += 1 ;
} }
ffclose() ; /* Ignore errors. */ ffclose() ; /* Ignore errors. */
curwp->w_markp = lforw(curwp->w_markp); curwp->w_markp = lforw(curwp->w_markp);
if( s == FIOERR) { if( s == FIOERR) {
errmsg = "I/O ERROR, " ; errmsg = "I/O ERROR, " ;
curbp->b_flag |= BFTRUNC ; curbp->b_flag |= BFTRUNC ;
} else if( s == FIOMEM) { } else if( s == FIOMEM) {
errmsg = "OUT OF MEMORY, " ; errmsg = "OUT OF MEMORY, " ;
curbp->b_flag |= BFTRUNC; curbp->b_flag |= BFTRUNC;
} else } else
errmsg = "" ; errmsg = "" ;
mloutfmt( "(%sInserted %d line%s)", mloutfmt( "(%sInserted %d line%s)",
errmsg, errmsg,
nline, nline,
&"s"[ nline == 1]) ; &"s"[ nline == 1]) ;
} }
/* advance to the next line and mark the window for changes */ /* advance to the next line and mark the window for changes */
curwp->w_dotp = lforw(curwp->w_dotp); curwp->w_dotp = lforw(curwp->w_dotp);
curwp->w_flag |= WFHARD | WFMODE; curwp->w_flag |= WFHARD | WFMODE;
/* copy window parameters back to the buffer structure */ /* copy window parameters back to the buffer structure */
curbp->b_dotp = curwp->w_dotp; curbp->b_dotp = curwp->w_dotp;
curbp->b_doto = curwp->w_doto; curbp->b_doto = curwp->w_doto;
curbp->b_markp = curwp->w_markp; curbp->b_markp = curwp->w_markp;
curbp->b_marko = curwp->w_marko; curbp->b_marko = curwp->w_marko;
return (s == FIOERR) ? FALSE : TRUE ; return (s == FIOERR) ? FALSE : TRUE ;
} }
/* end of file.c */ /* end of file.c */

214
fileio.c
View File

@ -1,13 +1,10 @@
/* fileio.c -- implements fileio.h */ /* fileio.c -- implements fileio.h */
#include "fileio.h" #include "fileio.h"
/* FILEIO.C /* The routines in this file read and write ASCII files from the disk. All
* of the knowledge about files are here.
* The routines in this file read and write ASCII files from the disk. All of
* the knowledge about files are here. modified by Petri Kutvonen
*
* modified by Petri Kutvonen
*/ */
#include <stdio.h> #include <stdio.h>
@ -18,66 +15,62 @@
#include "retcode.h" #include "retcode.h"
#include "utf8.h" #include "utf8.h"
char *fline = NULL ; /* dynamic return line */ char *fline = NULL ; /* dynamic return line */
static int flen = 0 ; /* current allocated length of fline */ static int flen = 0 ; /* current allocated length of fline */
int ftype ; int ftype ;
int fcode ; /* encoding type FCODE_xxxxx */ int fcode ; /* encoding type FCODE_xxxxx */
int fpayload ; /* actual length of fline content */ int fpayload ; /* actual length of fline content */
static FILE *ffp ; /* File pointer, all functions. */ static FILE *ffp ; /* File pointer, all functions. */
static boolean eofflag ; /* end-of-file flag */ static boolean eofflag ; /* end-of-file flag */
/* /* Open a file for reading. */
* Open a file for reading. fio_code ffropen( const char *fn) {
*/ eofflag = FALSE ;
fio_code ffropen( const char *fn)
{
if ((ffp = fopen(fn, "r")) == NULL)
return FIOFNF;
eofflag = FALSE;
ftype = FTYPE_NONE ; ftype = FTYPE_NONE ;
fcode = FCODE_ASCII ; fcode = FCODE_ASCII ;
return FIOSUC; ffp = fopen( fn, "r") ;
return (ffp == NULL) ? FIOFNF : FIOSUC ;
} }
/*
* Open a file for writing. Return TRUE if all is well, and FALSE on error /* Open a file for writing. Return TRUE if all is well, and FALSE on error
* (cannot create). (cannot create).
*/ */
fio_code ffwopen( const char *fn) { fio_code ffwopen( const char *fn) {
ffp = fopen( fn, "w") ; ffp = fopen( fn, "w") ;
return (ffp == NULL) ? FIOERR : FIOSUC ; return (ffp == NULL) ? FIOERR : FIOSUC ;
} }
/*
* Close a file. Should look at the status in all systems. /* Close a file. Should look at the status in all systems.
*/ */
fio_code ffclose(void) fio_code ffclose( void) {
{
/* free this since we do not need it anymore */ /* free this since we do not need it anymore */
if (fline) { if( fline) {
free(fline); free( fline) ;
fline = NULL; fline = NULL ;
} }
eofflag = FALSE;
eofflag = FALSE ;
ftype = FTYPE_NONE ; ftype = FTYPE_NONE ;
fcode = FCODE_ASCII ; fcode = FCODE_ASCII ;
return (fclose( ffp) != FALSE) ? FIOERR : FIOSUC ; return (fclose( ffp) != FALSE) ? FIOERR : FIOSUC ;
} }
/*
* Write a line to the already opened file. The "buf" points to the buffer, /* Write a line to the already opened file. The "buf" points to the
* and the "nbuf" is its length, less the free newline. Return the status. buffer, and the "nbuf" is its length, less the free newline. Return the
* Check only at the newline. status. Check only at the newline.
*/ */
fio_code ffputline( char *buf, int nbuf, int dosflag) { fio_code ffputline( char *buf, int nbuf, int dosflag) {
fwrite( buf, 1, nbuf, ffp) ; fwrite( buf, 1, nbuf, ffp) ;
if( dosflag) if( dosflag)
fputc( '\r', ffp) ; fputc( '\r', ffp) ;
fputc( '\n', ffp) ; fputc( '\n', ffp) ;
@ -87,92 +80,91 @@ fio_code ffputline( char *buf, int nbuf, int dosflag) {
return FIOSUC ; return FIOSUC ;
} }
/* /* Read a line from a file, and store the bytes in the supplied buffer.
* Read a line from a file, and store the bytes in the supplied buffer. The The "nbuf" is the length of the buffer. Complain about long lines and
* "nbuf" is the length of the buffer. Complain about long lines and lines lines at the end of the file that don't have a newline present. Check
* at the end of the file that don't have a newline present. Check for I/O for I/O errors too. Return status.
* errors too. Return status.
*/ */
fio_code ffgetline( void) { fio_code ffgetline( void) {
int c ; /* current character read */ int c ; /* current character read */
int i ; /* current index into fline */ int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */
int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */
/* if we are at the end...return it */ /* if we are at the end...return it */
if( eofflag) if( eofflag)
return FIOEOF ; return FIOEOF ;
/* dump fline if it ended up too big */ /* dump fline if it ended up too big */
if( flen > NSTRING) { if( flen > NSTRING) {
free( fline) ; free( fline) ;
fline = NULL ; fline = NULL ;
} }
/* if we don't have an fline, allocate one */ /* if we don't have an fline, allocate one */
if( fline == NULL) if( fline == NULL)
if( (fline = malloc( flen = NSTRING)) == NULL) if( (fline = malloc( flen = NSTRING)) == NULL)
return FIOMEM ; return FIOMEM ;
/* read the line in */ /* read the line in */
i = 0 ; int i = 0 ; /* current index into fline */
while( (c = fgetc( ffp)) != EOF && c != '\r' && c != '\n') { while( (c = fgetc( ffp)) != EOF && c != '\r' && c != '\n') {
/* if line is full, get more room */ /* if line is full, get more room */
if( i >= flen) { if( i >= flen) {
char *tmpline ; /* temp storage for expanding line */ char *tmpline ; /* temp storage for expanding line */
tmpline = malloc( flen + NSTRING) ; tmpline = malloc( flen + NSTRING) ;
if( tmpline == NULL) if( tmpline == NULL)
return FIOMEM ; return FIOMEM ;
memcpy( tmpline, fline, flen) ; memcpy( tmpline, fline, flen) ;
flen += NSTRING ; flen += NSTRING ;
free( fline) ; free( fline) ;
fline = tmpline ; fline = tmpline ;
} }
fline[ i++] = c ; fline[ i++] = c ;
lcode |= c ; lcode |= c ;
} }
fpayload = i ; fpayload = i ;
lcode &= FCODE_MASK ; if( lcode & 0x80 /* line contains extended chars */
if( lcode && (fcode != FCODE_MIXED)) { /* line contains extended chars */ && (fcode != FCODE_MIXED)) {
/* Check if consistent UTF-8 encoding */ /* Check if consistent UTF-8 encoding */
int pos = 0 ; lcode = FCODE_ASCII ;
int pos = 0 ;
while( (pos < i) && (lcode != FCODE_MIXED)) {
unicode_t uc ;
while( (pos < i) && (lcode != FCODE_MIXED)) { int bytes = utf8_to_unicode( fline, pos, i, &uc) ;
unicode_t uc ; pos += bytes ;
int bytes ; if( bytes > 1) /* Multi byte UTF-8 sequence */
lcode |= FCODE_UTF_8 ;
else if( uc > 127) /* Extended ASCII */
lcode |= FCODE_EXTND ;
}
bytes = utf8_to_unicode( fline, pos, i, &uc) ; fcode |= lcode ;
pos += bytes ; }
if( bytes > 1) /* Multi byte UTF-8 sequence */
lcode |= FCODE_UTF_8 ;
else if( uc > 127) /* Extended ASCII */
lcode |= FCODE_EXTND ;
}
fcode |= lcode ;
}
/* test for any errors that may have occured */ /* test for any errors that may have occured */
if( c == EOF) { if( c == EOF) {
if( ferror( ffp)) if( ferror( ffp))
return FIOERR ; return FIOERR ;
if( i != 0) if( i != 0)
eofflag = TRUE ; eofflag = TRUE ;
else else
return FIOEOF ; return FIOEOF ;
} else if( c == '\r') { } else if( c == '\r') {
c = fgetc( ffp) ; c = fgetc( ffp) ;
if( c != '\n') { if( c != '\n') {
ftype |= FTYPE_MAC ; ftype |= FTYPE_MAC ;
ungetc( c, ffp) ; ungetc( c, ffp) ;
} else } else
ftype |= FTYPE_DOS ; ftype |= FTYPE_DOS ;
} else /* c == '\n' */ } else /* c == '\n' */
ftype |= FTYPE_UNIX ; ftype |= FTYPE_UNIX ;
return FIOSUC ; return FIOSUC ;
} }
/* end of fileio.c */

View File

@ -17,10 +17,9 @@ typedef enum {
/* FTYPE_MIXED [ 3, 5, 6, 7] */ /* FTYPE_MIXED [ 3, 5, 6, 7] */
#define FCODE_ASCII 0 #define FCODE_ASCII 0
#define FCODE_MASK 0x80 #define FCODE_UTF_8 1
#define FCODE_UTF_8 0x81 #define FCODE_EXTND 2
#define FCODE_EXTND 0x82 #define FCODE_MIXED 3
#define FCODE_MIXED 0x83
extern char *fline ; /* dynamic return line */ extern char *fline ; /* dynamic return line */
extern int ftype ; extern int ftype ;

214
flook.c
View File

@ -1,153 +1,131 @@
/* flook.c -- implements flook.h */ /* flook.c -- implements flook.h */
#include "flook.h" #include "flook.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "defines.h" #include "defines.h"
#include "fileio.h"
/* possible names and paths of help files under different OSs */ /* possible names and paths of help files under different OSes */
const char *pathname[] = { const char *pathname[] = {
#if BSD | USG #if BSD | USG
".emacsrc", ".emacsrc",
"emacs.hlp", "emacs.hlp",
#if PKCODE # if PKCODE
"/usr/global/lib/", "/usr/local/bin/", "/usr/local/lib/", "/usr/global/lib/", "/usr/local/bin/", "/usr/local/lib/",
#endif # endif
"/usr/local/", "/usr/lib/", "" "/usr/local/", "/usr/lib/", ""
#endif #endif
} ; } ;
#define PATHNAME_SIZE (sizeof pathname / sizeof pathname[ 0]) #define PATHTABLE_SIZE (sizeof pathname / sizeof pathname[ 0])
/* /* does <fname> exist on disk?
* does <fname> exist on disk?
* *
* char *fname; file to check for existance * char *fname; file to check for existance
*/ */
boolean fexist( const char *fname) boolean fexist( const char *fname) {
{ FILE *fp = fopen( fname, "r") ;
FILE *fp; if( fp == NULL)
return FALSE ;
else
fclose( fp) ;
/* try to open the file for reading */ return TRUE ;
fp = fopen(fname, "r");
/* if it fails, just return false! */
if (fp == NULL)
return FALSE;
/* otherwise, close it and report true */
fclose(fp);
return TRUE;
} }
/*
* Look up the existance of a file along the normal or PATH /* Look up the existence of a file along the normal or PATH environment
* environment variable. Look first in the HOME directory if variable. Look first in the HOME directory if asked and possible.
* asked and possible
* char *fname; base file name to search for
* char *fname; base file name to search for boolean hflag; look in the HOME environment variable first?
* boolean hflag; Look in the HOME environment variable first?
*/ */
char *flook( const char *fname, boolean hflag) char *flook( const char *fname, boolean hflag) {
{ static char fspec[ NSTRING] ; /* full path spec to search */
unsigned i ; /* index */
int len ;
static char fspec[NSTRING]; /* full path spec to search */
#if ENVFUNC int len = sizeof fspec - strlen( fname) - 1 ;
char *path; /* environmental PATH variable */ if( len < 0)
return NULL ;
#if ENVFUNC
if( hflag) {
char *home = getenv( "HOME") ; /* path to home directory */
if( home != NULL) {
if( len > (int) strlen( home) + 1) {
/* build home dir file spec */
strcpy( fspec, home) ;
strcat( fspec, "/") ;
strcat( fspec, fname) ;
/* and try it out */
if( fexist( fspec))
return fspec ;
}
}
}
#endif #endif
len = sizeof fspec - strlen( fname) - 1 ; /* always try the current directory first */
if( len < 0) if( len >= 0) {
return NULL ; strcpy( fspec, fname) ;
if( fexist( fspec))
return fspec ;
}
#if ENVFUNC #if ENVFUNC
if (hflag) { # if USG | BSD
char *home; /* path to home directory */ # define PATHCHR ':'
# else
# define PATHCHR ';'
# endif
home = getenv("HOME"); /* get the PATH variable */
if (home != NULL) { char *path = getenv( "PATH") ; /* environmental PATH variable */
if( len > (int) strlen( home) + 1) { if( path != NULL)
/* build home dir file spec */ while( *path) {
strcpy( fspec, home) ; int cnt = len ;
strcat(fspec, "/"); /* build next possible file spec */
strcat(fspec, fname); char *sp = fspec ; /* pointer into path spec */
while( *path && (*path != PATHCHR)) {
if( cnt-- > 0)
*sp++ = *path ;
/* and try it out */ path += 1 ;
if( fexist( fspec)) }
return fspec ;
} if( cnt >= 0) {
} /* add a terminating dir separator if we need it */
} if (sp != fspec)
*sp++ = '/';
*sp = 0;
strcat(fspec, fname);
/* and try it out */
if( fexist( fspec))
return fspec ;
}
if( *path == PATHCHR)
++path ;
}
#endif #endif
/* always try the current directory first */ /* look it up via the old table method */
if( len >= 0) { for( unsigned i = 2 ; i < PATHTABLE_SIZE ; i++)
strcpy( fspec, fname) ; if( len >= (int) strlen( pathname[ i])) {
if( fexist( fspec)) strcpy( fspec, pathname[ i]) ;
return fspec ; strcat( fspec, fname);
}
#if ENVFUNC /* and try it out */
#if USG | BSD if( fexist( fspec))
#define PATHCHR ':' return fspec ;
#else }
#define PATHCHR ';'
#endif
/* get the PATH variable */ return NULL ; /* no such luck */
path = getenv("PATH");
if (path != NULL)
while (*path) {
char *sp; /* pointer into path spec */
int cnt ;
cnt = len ;
/* build next possible file spec */
sp = fspec;
while( *path && (*path != PATHCHR)) {
if( cnt-- > 0)
*sp++ = *path ;
path += 1 ;
}
if( cnt >= 0) {
/* add a terminating dir separator if we need it */
if (sp != fspec)
*sp++ = '/';
*sp = 0;
strcat(fspec, fname);
/* and try it out */
if( fexist( fspec))
return fspec ;
}
if (*path == PATHCHR)
++path;
}
#endif
/* look it up via the old table method */
for( i = 2; i < PATHNAME_SIZE ; i++)
if( len >= (int) strlen( pathname[ i])) {
strcpy( fspec, pathname[ i]) ;
strcat( fspec, fname);
/* and try it out */
if( fexist( fspec))
return fspec ;
}
return NULL; /* no such luck */
} }
/* end of flook.c */