File commands based on newmlarg (replacement of mlreply).

This commit is contained in:
Renaud 2015-10-05 11:34:33 +08:00
parent b86ceeaf5e
commit b59a47bb3a
1 changed files with 150 additions and 107 deletions

257
file.c
View File

@ -12,6 +12,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -73,16 +74,20 @@ boolean resterr( void) {
* "read a file into the current buffer" code. * "read a file into the current buffer" code.
* Bound to "C-X C-R". * Bound to "C-X C-R".
*/ */
int fileread(int f, int n) int fileread( int f, int n) {
{ int status ;
int s; char *fname ;
fname_t fname ;
if (restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr(); return resterr() ;
if ((s = mlreply("Read file: ", fname, sizeof fname)) != TRUE)
return s; status = newmlarg( &fname, "Read file: ", sizeof( fname_t)) ;
return readin(fname, TRUE); if( status == TRUE) {
status = readin( fname, TRUE) ;
free( fname) ;
}
return status ;
} }
/* /*
@ -92,20 +97,26 @@ int fileread(int f, int n)
* "insert a file into the current buffer" code. * "insert a file into the current buffer" code.
* Bound to "C-X C-I". * Bound to "C-X C-I".
*/ */
int insfile(int f, int n) int insfile( int f, int n) {
{ int status ;
int s; char *fname ;
fname_t fname ;
if (restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr(); return resterr() ;
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return rdonly(); /* we are in read only mode */ if( curbp->b_mode & MDVIEW) /* don't allow this command if */
if ((s = mlreply("Insert file: ", fname, sizeof fname)) != TRUE) return rdonly() ; /* we are in read only mode */
return s;
if ((s = ifile(fname)) != TRUE) status = newmlarg( &fname, "Insert file: ", sizeof( fname_t)) ;
return s; if( status == TRUE) {
return reposition(TRUE, -1); status = ifile( fname) ;
free( fname) ;
}
if( status != TRUE)
return status ;
return reposition( TRUE, -1) ;
} }
/* /*
@ -117,40 +128,49 @@ int insfile(int f, int n)
* text, and switch to the new buffer. * text, and switch to the new buffer.
* Bound to C-X C-F. * Bound to C-X C-F.
*/ */
int filefind(int f, int n) int filefind( int f, int n) {
{ char *fname ; /* file user wishes to find */
fname_t fname ; /* file user wishes to find */ int status ; /* status return */
int s; /* status return */
if (restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr(); return resterr() ;
if ((s = mlreply("Find file: ", fname, sizeof fname)) != TRUE)
return s; status = newmlarg( &fname, "Find file: ", sizeof( fname_t)) ;
return getfile(fname, TRUE); if( status == TRUE) {
status = getfile( fname, TRUE) ;
free( fname) ;
}
return status ;
} }
int viewfile(int f, int n) int viewfile( int f, int n) { /* visit a file in VIEW mode */
{ /* visit a file in VIEW mode */ char *fname ; /* file user wishes to find */
fname_t fname ; /* file user wishes to find */ int status ; /* status return */
int s; /* status return */
struct window *wp; /* scan for windows that need updating */
if (restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr(); return resterr() ;
if ((s = mlreply("View file: ", fname, sizeof fname)) != TRUE)
return s; status = newmlarg( &fname, "View file: ", sizeof( fname_t)) ;
s = getfile(fname, FALSE); if( status == TRUE) {
if (s) { /* if we succeed, put it in view mode */ status = getfile(fname, FALSE) ;
curwp->w_bufp->b_mode |= MDVIEW; free( fname) ;
}
if( status == TRUE) { /* if we succeed, put it in view mode */
struct window *wp ; /* scan for windows that need updating */
curwp->w_bufp->b_mode |= MDVIEW ;
/* scan through and update mode lines of all windows */ /* scan through and update mode lines of all windows */
wp = wheadp; wp = wheadp ;
while (wp != NULL) { while( wp != NULL) {
wp->w_flag |= WFMODE; wp->w_flag |= WFMODE ;
wp = wp->w_wndp; wp = wp->w_wndp ;
} }
} }
return s;
return status ;
} }
#if CRYPT #if CRYPT
@ -165,28 +185,29 @@ void cryptbufferkey( struct buffer *bp) {
* int f; default flag * int f; default flag
* int n; numeric argument * int n; numeric argument
*/ */
int set_encryption_key(int f, int n) int set_encryption_key( int f, int n) {
{ int status ; /* return status */
int status; /* return status */ int odisinp ; /* original value of disinp */
int odisinp; /* original vlaue of disinp */ char *key ; /* new encryption string */
ekey_t key ; /* new encryption string */
/* turn command input echo off */ /* turn command input echo off */
odisinp = disinp; odisinp = disinp ;
disinp = FALSE; disinp = FALSE ;
/* get the string to use as an encrytion string */ /* get the string to use as an encrytion string */
status = mlreply("Encryption String: ", key, sizeof key - 1); status = newmlarg( &key, "Encryption String: ", sizeof( ekey_t)) ;
disinp = odisinp; disinp = odisinp ;
if (status != TRUE) if( status != TRUE)
return status; return status ;
/* save it off and encrypt it*/ /* save it off and encrypt it*/
strcpy(curbp->b_key, key); strncpy( curbp->b_key, key, sizeof( ekey_t) - 1) ;
curbp->b_key[ sizeof( ekey_t) - 1] = '\0' ;
free( key) ;
cryptbufferkey( curbp) ; cryptbufferkey( curbp) ;
mloutstr( "") ; /* clear the message line */ mloutstr( "") ; /* clear the message line */
return TRUE; return TRUE ;
} }
static int resetkey(void) static int resetkey(void)
@ -253,15 +274,22 @@ int getfile( const char *fname, boolean lockfl)
} }
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 ;
/* old buffer name conflict code */ /* old buffer name conflict code */
s = mlreply("Buffer name: ", bname, sizeof bname); 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 ;
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 */
strncpy( bname, new_bname, sizeof bname - 1) ;
bname[ sizeof bname - 1] = '\0' ;
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;
@ -495,27 +523,35 @@ void unqname(char *name)
* is more compatable with Gosling EMACS than * is more compatable with Gosling EMACS than
* with ITS EMACS. Bound to "C-X C-W". * with ITS EMACS. Bound to "C-X C-W".
*/ */
int filewrite(int f, int n) int filewrite( int f, int n) {
{ int status ;
struct window *wp; char *fname ;
int s;
fname_t fname ;
if (restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr(); return resterr() ;
if ((s = mlreply("Write file: ", fname, sizeof fname)) != TRUE)
return s; status = newmlarg( &fname, "Write file: ", sizeof( fname_t)) ;
if ((s = writeout(fname)) == TRUE) { if( status == TRUE) {
strcpy(curbp->b_fname, fname); status = writeout( fname) ;
curbp->b_flag &= ~BFCHG; if( status == TRUE) {
wp = wheadp; /* Update mode lines. */ struct window *wp ;
while (wp != NULL) {
if (wp->w_bufp == curbp) strncpy( curbp->b_fname, fname, sizeof( fname_t) - 1) ;
wp->w_flag |= WFMODE; curbp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
wp = wp->w_wndp; curbp->b_flag &= ~BFCHG ;
} wp = wheadp ; /* Update mode lines. */
} while( wp != NULL) {
return s; if( wp->w_bufp == curbp)
wp->w_flag |= WFMODE ;
wp = wp->w_wndp ;
}
}
free( fname) ;
}
return status ;
} }
/* /*
@ -622,28 +658,35 @@ int writeout( const char *fn)
* as needing an update. You can type a blank line at the * as needing an update. You can type a blank line at the
* prompt if you wish. * prompt if you wish.
*/ */
int filename(int f, int n) int filename( int f, int n) {
{ struct window *wp ;
struct window *wp; int status ;
int s; char *fname ;
fname_t fname ;
if (restflag) /* don't allow this command if restricted */ if( restflag) /* don't allow this command if restricted */
return resterr(); return resterr() ;
if ((s = mlreply("Name: ", fname, sizeof fname)) == ABORT)
return s; status = newmlarg( &fname, "Name: ", sizeof( fname_t)) ;
if (s == FALSE) if( status == ABORT)
strcpy(curbp->b_fname, ""); return status ;
else else if( status == FALSE)
strcpy(curbp->b_fname, fname); curbp->b_fname[ 0] = '\0' ;
wp = wheadp; /* Update mode lines. */ else { /* TRUE */
while (wp != NULL) { strncpy( curbp->b_fname, fname, sizeof( fname_t) - 1) ;
if (wp->w_bufp == curbp) curbp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
wp->w_flag |= WFMODE; free( fname) ;
wp = wp->w_wndp;
} }
curbp->b_mode &= ~MDVIEW; /* no longer read only mode */
return TRUE; wp = wheadp ; /* Update mode lines. */
while( wp != NULL) {
if( wp->w_bufp == curbp)
wp->w_flag |= WFMODE ;
wp = wp->w_wndp ;
}
curbp->b_mode &= ~MDVIEW ; /* no longer read only mode */
return TRUE ;
} }
/* /*