1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-07-01 14:05:24 +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

121
file.c
View File

@ -62,6 +62,7 @@ boolean resterr( void) {
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.
@ -81,6 +82,7 @@ BINDABLE( fileread) {
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.
@ -105,6 +107,7 @@ BINDABLE( insfile) {
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
@ -125,15 +128,15 @@ BINDABLE( filefind) {
return status ; return status ;
} }
static void upd_mode( void) {
struct window *wp ;
/* Update mode lines */ /* Update mode lines of all windows displaying current buffer */
for( wp = wheadp ; wp != NULL ; wp = wp->w_wndp) static void upd_mode( void) {
for( window_p wp = wheadp ; wp != NULL ; wp = wp->w_wndp)
if( wp->w_bufp == curbp) if( wp->w_bufp == curbp)
wp->w_flag |= WFMODE ; wp->w_flag |= WFMODE ;
} }
BINDABLE( viewfile) { /* visit a file in VIEW mode */ BINDABLE( viewfile) { /* visit a file in VIEW mode */
char *fname ; /* file user wishes to find */ char *fname ; /* file user wishes to find */
@ -154,8 +157,8 @@ BINDABLE( viewfile) { /* visit a file in VIEW mode */
return status ; 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?
@ -232,24 +235,19 @@ int getfile( const char *fname, boolean lockfl) {
} }
/* 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
* char fname[]; name of file to read boolean lockfl; check for file locks?
* boolean lockfl; check for file locks?
*/ */
int readin(const char *fname, boolean lockfl) int readin( const char *fname, boolean lockfl) {
{
struct window *wp;
buffer_p bp;
int status ;
fio_code s ; fio_code s ;
bp = curbp; /* Cheap. */ 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
@ -262,7 +260,8 @@ int readin(const char *fname, boolean lockfl)
} else } else
#endif #endif
{ {
if( (status = bclear( bp)) != TRUE) /* Might be old. */ int status = bclear( bp) ;
if( status != TRUE) /* Might be old. */
return status ; return status ;
bp->b_flag &= ~(BFINVS | BFCHG); bp->b_flag &= ~(BFINVS | BFCHG);
@ -337,13 +336,13 @@ int readin(const char *fname, boolean lockfl)
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) ;
@ -357,32 +356,23 @@ int readin(const char *fname, boolean lockfl)
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)
++cp1;
while (cp1 != &fname[0] && cp1[-1] != '/')
--cp1;
cp2 = &bname[0];
while( *cp1 != 0 && *cp1 != ';') { while( *cp1 != 0 && *cp1 != ';') {
unicode_t c ; unicode_t c ;
int n ;
n = utf8_to_unicode( cp1, 0, 4, &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 */ if( cp2 + n <= &bname[ sizeof( bname_t) - 2]) /* 1 digit buffer name conflict [0..9] + EOS */
while( n--) while( n--)
*cp2++ = *cp1++ ; *cp2++ = *cp1++ ;
@ -393,22 +383,19 @@ void makename( bname_t bname, const char *fname)
*cp2 = 0 ; *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) {
{
char *sp;
/* check to see if it is in the buffer list */ /* check to see if it is in the buffer list */
while( bfind( name, 0, FALSE) != NULL) { while( bfind( name, 0, FALSE) != NULL) {
/* go to the end of the name */ /* go to the end of the name */
sp = name; char *sp = name ;
while( *sp) while( *sp)
++sp ; ++sp ;
if( sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) { if( sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) {
*sp++ = '0' ; *sp++ = '0' ;
*sp = 0 ; *sp = 0 ;
@ -417,6 +404,7 @@ void unqname(char *name)
} }
} }
/* 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.
@ -443,10 +431,11 @@ BINDABLE( filewrite) {
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)) ;
@ -467,13 +456,11 @@ BINDABLE( filesave) {
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 ;
@ -511,6 +498,7 @@ int writeout( const char *fn) {
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
@ -538,10 +526,9 @@ BINDABLE( filename) {
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 ;

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>
@ -29,38 +26,34 @@ 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) {
*/
fio_code ffropen( const char *fn)
{
if ((ffp = fopen(fn, "r")) == NULL)
return FIOFNF;
eofflag = FALSE ; 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 ;
@ -68,10 +61,10 @@ fio_code ffclose(void)
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) ;
@ -87,15 +80,13 @@ 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 */
@ -114,7 +105,7 @@ fio_code ffgetline( void) {
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) {
@ -135,16 +126,15 @@ fio_code ffgetline( void) {
} }
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 */
lcode = FCODE_ASCII ;
int pos = 0 ; int pos = 0 ;
while( (pos < i) && (lcode != FCODE_MIXED)) { while( (pos < i) && (lcode != FCODE_MIXED)) {
unicode_t uc ; unicode_t uc ;
int bytes ;
bytes = utf8_to_unicode( fline, pos, i, &uc) ; int bytes = utf8_to_unicode( fline, pos, i, &uc) ;
pos += bytes ; pos += bytes ;
if( bytes > 1) /* Multi byte UTF-8 sequence */ if( bytes > 1) /* Multi byte UTF-8 sequence */
lcode |= FCODE_UTF_8 ; lcode |= FCODE_UTF_8 ;
@ -176,3 +166,5 @@ fio_code ffgetline( void) {
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 ;

64
flook.c
View File

@ -1,17 +1,14 @@
/* 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",
@ -23,57 +20,40 @@ const char *pathname[] = {
#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;
/* try to open the file for reading */
fp = fopen(fname, "r");
/* if it fails, just return false! */
if( fp == NULL) if( fp == NULL)
return FALSE ; return FALSE ;
else
/* otherwise, close it and report true */
fclose( fp) ; fclose( fp) ;
return TRUE ; 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) {
{
unsigned i ; /* index */
int len ;
static char fspec[ NSTRING] ; /* full path spec to search */ static char fspec[ NSTRING] ; /* full path spec to search */
#if ENVFUNC int len = sizeof fspec - strlen( fname) - 1 ;
char *path; /* environmental PATH variable */
#endif
len = sizeof fspec - strlen( fname) - 1 ;
if( len < 0) if( len < 0)
return NULL ; return NULL ;
#if ENVFUNC #if ENVFUNC
if( hflag) { if( hflag) {
char *home; /* path to home directory */ char *home = getenv( "HOME") ; /* path to home directory */
home = getenv("HOME");
if( home != NULL) { if( home != NULL) {
if( len > (int) strlen( home) + 1) { if( len > (int) strlen( home) + 1) {
/* build home dir file spec */ /* build home dir file spec */
@ -104,15 +84,12 @@ char *flook( const char *fname, boolean hflag)
# endif # endif
/* get the PATH variable */ /* get the PATH variable */
path = getenv("PATH"); char *path = getenv( "PATH") ; /* environmental PATH variable */
if( path != NULL) if( path != NULL)
while( *path) { while( *path) {
char *sp; /* pointer into path spec */ int cnt = len ;
int cnt ;
cnt = len ;
/* build next possible file spec */ /* build next possible file spec */
sp = fspec; char *sp = fspec ; /* pointer into path spec */
while( *path && (*path != PATHCHR)) { while( *path && (*path != PATHCHR)) {
if( cnt-- > 0) if( cnt-- > 0)
*sp++ = *path ; *sp++ = *path ;
@ -138,7 +115,7 @@ char *flook( const char *fname, boolean hflag)
#endif #endif
/* look it up via the old table method */ /* look it up via the old table method */
for( i = 2; i < PATHNAME_SIZE ; i++) for( unsigned i = 2 ; i < PATHTABLE_SIZE ; i++)
if( len >= (int) strlen( pathname[ i])) { if( len >= (int) strlen( pathname[ i])) {
strcpy( fspec, pathname[ i]) ; strcpy( fspec, pathname[ i]) ;
strcat( fspec, fname); strcat( fspec, fname);
@ -151,3 +128,4 @@ char *flook( const char *fname, boolean hflag)
return NULL ; /* no such luck */ return NULL ; /* no such luck */
} }
/* end of flook.c */