1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-06-10 14:00:41 +00:00

Introduce newtoken()/getnewtoken() for full length extraction of tokens.

This commit is contained in:
Renaud 2015-09-27 19:19:55 +08:00
parent 2c9cbbfb1e
commit b5eb424ad0

78
exec.c
View File

@ -212,16 +212,21 @@ static int docmd( char *cline) {
} }
/* /*
* token: * new token:
* chop a token off a string * chop a token off a string
* return a pointer past the token * return a pointer past the token
* *
* char *src, *tok; source string, destination token string * char *src in, source string
* int size; maximum size of token * char **tokref out, destination of newly allocated token string
*/ */
static char *token( char *src, char *tok, int size) { static char *newtoken( char *src, char **tokref) {
int quotef; /* is the current string quoted? */ boolean quotef ; /* is the current string quoted? */
char c; /* temporary character */ char *tok ; /* allocated string */
int size ; /* allocated size */
int idx = 0 ; /* insertion point into token string */
tok = malloc( NSTRING) ;
size = (tok == NULL) ? 0 : NSTRING ;
/* first scan past any whitespace in the source string */ /* first scan past any whitespace in the source string */
while (*src == ' ' || *src == '\t') while (*src == ' ' || *src == '\t')
@ -230,6 +235,8 @@ static char *token( char *src, char *tok, int size) {
/* scan through the source string */ /* scan through the source string */
quotef = FALSE; quotef = FALSE;
while (*src) { while (*src) {
char c ; /* temporary character */
/* process special characters */ /* process special characters */
if (*src == '~') { if (*src == '~') {
++src; ++src;
@ -254,9 +261,6 @@ static char *token( char *src, char *tok, int size) {
default: default:
c = *(src - 1); c = *(src - 1);
} }
if (--size > 0) {
*tok++ = c;
}
} else { } else {
/* check for the end of the token */ /* check for the end of the token */
if (quotef) { if (quotef) {
@ -273,31 +277,67 @@ static char *token( char *src, char *tok, int size) {
/* record the character */ /* record the character */
c = *src++; c = *src++;
if (--size > 0)
*tok++ = c;
} }
if( idx < size - 1)
tok[ idx++] = c ;
else if( size > 1) {
char *tmptok ;
tmptok = malloc( size + 32) ;
if( tmptok == NULL)
size = 0 ;
else {
memcpy( tmptok, tok, idx) ;
free( tok) ;
tok = tmptok ;
size += 32 ;
tok[ idx++] = c ;
}
}
} }
/* terminate the token and exit */ /* terminate the token and exit */
if (*src) if (*src)
++src; ++src;
*tok = 0;
if( tok != NULL)
tok[ idx] = 0 ;
*tokref = tok ;
return src; return src;
} }
static char *token( char *srcstr, char *tok, int maxtoksize) {
char *newtok ;
srcstr = newtoken( srcstr, &newtok) ;
if( newtok == NULL)
tok[ 0] = 0 ;
else {
strncpy( tok, newtok, maxtoksize - 1) ;
tok[ maxtoksize - 1] = 0 ;
free( newtok) ;
}
return srcstr ;
}
void gettoken( char *tok, int maxtoksize) { void gettoken( char *tok, int maxtoksize) {
execstr = token( execstr, tok, maxtoksize) ; execstr = token( execstr, tok, maxtoksize) ;
} }
void getnewtoken( char **tokref) {
execstr = newtoken( execstr, tokref) ;
}
boolean gettokval( char *tok, int size) { boolean gettokval( char *tok, int size) {
char *tmpbuf ; char *tmpbuf ;
tmpbuf = malloc( size) ;
if( tmpbuf == NULL)
return FALSE ;
/* grab token and advance past */ /* grab token and advance past */
gettoken( tmpbuf, size) ; getnewtoken( &tmpbuf) ;
if( tmpbuf == NULL)
return FALSE ;
/* evaluate it */ /* evaluate it */
strncpy( tok, getval( tmpbuf), size - 1) ; strncpy( tok, getval( tmpbuf), size - 1) ;
@ -770,10 +810,8 @@ static int dobuf(struct buffer *bp)
case DGOTO: /* GOTO directive */ case DGOTO: /* GOTO directive */
/* .....only if we are currently executing */ /* .....only if we are currently executing */
if (execlevel == 0) { if (execlevel == 0) {
/* grab label to jump to */ /* grab label to jump to */
eline = eline = token( eline, golabel, sizeof golabel) ;
token( eline, golabel, sizeof golabel) ;
linlen = strlen(golabel); linlen = strlen(golabel);
glp = hlp->l_fp; glp = hlp->l_fp;
while (glp != hlp) { while (glp != hlp) {