1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-23 09:36:31 -05:00

More dynamic argument string size as parameters to predefined functions.

This commit is contained in:
Renaud 2015-01-08 14:16:15 +08:00
parent b596a3a09c
commit 8ddc8676ef

162
eval.c
View File

@ -310,15 +310,17 @@ char *gtfun(char *fname)
{ {
int fnum; /* index to function to eval */ int fnum; /* index to function to eval */
char *tsp; /* temporary string pointer */ char *tsp; /* temporary string pointer */
char arg1[NSTRING]; /* value of first argument */ char stmp[ 512] ;
char arg2[NSTRING]; /* value of second argument */ char *arg1 ; /* value of first argument */
char arg3[NSTRING]; /* value of third argument */ char *arg2 ; /* value of second argument */
char *arg3 ; /* value of third argument */
static char *result ; /* string result */ static char *result ; /* string result */
static int ressize = 0 ; /* mark result as uninitialized */ static int ressize = 0 ; /* mark result as uninitialized */
char *retstr ; /* return value */
if( ressize == 0) { if( ressize == 0) {
result = malloc( NSTRING + 1) ; result = malloc( NSTRING) ;
ressize = NSTRING + 1 ; ressize = NSTRING ;
} }
/* look the function up in the function table */ /* look the function up in the function table */
@ -332,20 +334,30 @@ char *gtfun(char *fname)
if (fnum == ARRAY_SIZE(funcs)) if (fnum == ARRAY_SIZE(funcs))
return errorm; return errorm;
arg1 = arg2 = arg3 = NULL ;
/* if needed, retrieve the first argument */ /* if needed, retrieve the first argument */
if (funcs[fnum].f_type >= MONAMIC) { if (funcs[fnum].f_type >= MONAMIC) {
if( macarg( arg1, sizeof arg1) != TRUE) if( macarg( stmp, sizeof stmp) != TRUE)
return errorm; return errorm;
arg1 = malloc( strlen( stmp) + 1) ;
strcpy( arg1, stmp) ;
/* if needed, retrieve the second argument */ /* if needed, retrieve the second argument */
if (funcs[fnum].f_type >= DYNAMIC) { if (funcs[fnum].f_type >= DYNAMIC) {
if( macarg( arg2, sizeof arg2) != TRUE) if( macarg( stmp, sizeof stmp) != TRUE)
return errorm; return errorm;
arg2 = malloc( strlen( stmp) + 1) ;
strcpy( arg2, stmp) ;
/* if needed, retrieve the third argument */ /* if needed, retrieve the third argument */
if (funcs[fnum].f_type >= TRINAMIC) if (funcs[fnum].f_type >= TRINAMIC) {
if( macarg( arg3, sizeof arg3) != TRUE) if( macarg( stmp, sizeof stmp) != TRUE)
return errorm; return errorm;
arg3 = malloc( strlen( stmp) + 1) ;
strcpy( arg3, stmp) ;
}
} }
} }
@ -353,17 +365,23 @@ char *gtfun(char *fname)
/* and now evaluate it! */ /* and now evaluate it! */
switch (fnum) { switch (fnum) {
case UFADD: case UFADD:
return i_to_a(atoi(arg1) + atoi(arg2)); retstr = i_to_a( atoi( arg1) + atoi( arg2)) ;
break ;
case UFSUB: case UFSUB:
return i_to_a(atoi(arg1) - atoi(arg2)); retstr = i_to_a( atoi( arg1) - atoi( arg2)) ;
break ;
case UFTIMES: case UFTIMES:
return i_to_a(atoi(arg1) * atoi(arg2)); retstr = i_to_a( atoi( arg1) * atoi( arg2)) ;
break ;
case UFDIV: case UFDIV:
return i_to_a(atoi(arg1) / atoi(arg2)); retstr = i_to_a( atoi( arg1) / atoi( arg2)) ;
break ;
case UFMOD: case UFMOD:
return i_to_a(atoi(arg1) % atoi(arg2)); retstr = i_to_a( atoi( arg1) % atoi( arg2)) ;
break ;
case UFNEG: case UFNEG:
return i_to_a(-atoi(arg1)); retstr = i_to_a( -atoi( arg1)) ;
break ;
case UFCAT: { case UFCAT: {
int sz1, sz ; int sz1, sz ;
@ -377,8 +395,9 @@ char *gtfun(char *fname)
strcpy( result, arg1) ; strcpy( result, arg1) ;
strcat( &result[ sz1], arg2) ; strcat( &result[ sz1], arg2) ;
return result ; retstr = result ;
} }
break ;
case UFLEFT: { case UFLEFT: {
int sz ; int sz ;
@ -391,8 +410,9 @@ char *gtfun(char *fname)
strncpy( result, arg1, sz) ; strncpy( result, arg1, sz) ;
result[ sz] = 0 ; result[ sz] = 0 ;
return result ; retstr = result ;
} }
break ;
case UFRIGHT: { case UFRIGHT: {
int sz ; int sz ;
@ -403,8 +423,9 @@ char *gtfun(char *fname)
ressize = sz + 1 ; ressize = sz + 1 ;
} }
return strcpy( result, &arg1[ strlen( arg1) - sz]) ; retstr = strcpy( result, &arg1[ strlen( arg1) - sz]) ;
} }
break ;
case UFMID: { case UFMID: {
int sz ; int sz ;
@ -417,79 +438,120 @@ char *gtfun(char *fname)
strncpy( result, &arg1[ atoi( arg2) - 1], sz) ; strncpy( result, &arg1[ atoi( arg2) - 1], sz) ;
result[ sz] = 0 ; result[ sz] = 0 ;
return result ; retstr = result ;
} }
break ;
case UFNOT: case UFNOT:
return ltos(stol(arg1) == FALSE); retstr = ltos( stol( arg1) == FALSE) ;
break ;
case UFEQUAL: case UFEQUAL:
return ltos(atoi(arg1) == atoi(arg2)); retstr = ltos( atoi( arg1) == atoi( arg2)) ;
break ;
case UFLESS: case UFLESS:
return ltos(atoi(arg1) < atoi(arg2)); retstr = ltos( atoi( arg1) < atoi( arg2)) ;
break ;
case UFGREATER: case UFGREATER:
return ltos(atoi(arg1) > atoi(arg2)); retstr = ltos( atoi( arg1) > atoi( arg2)) ;
break ;
case UFSEQUAL: case UFSEQUAL:
return ltos(strcmp(arg1, arg2) == 0); retstr = ltos( strcmp( arg1, arg2) == 0) ;
break ;
case UFSLESS: case UFSLESS:
return ltos(strcmp(arg1, arg2) < 0); retstr = ltos( strcmp( arg1, arg2) < 0) ;
break ;
case UFSGREAT: case UFSGREAT:
return ltos(strcmp(arg1, arg2) > 0); retstr = ltos( strcmp( arg1, arg2) > 0) ;
break ;
case UFIND: case UFIND:
return strcpy(result, getval(arg1)); retstr = strcpy( result, getval( arg1)) ;
break ;
case UFAND: case UFAND:
return ltos(stol(arg1) && stol(arg2)); retstr = ltos( stol( arg1) && stol( arg2)) ;
break ;
case UFOR: case UFOR:
return ltos(stol(arg1) || stol(arg2)); retstr = ltos( stol( arg1) || stol( arg2)) ;
break ;
case UFLENGTH: case UFLENGTH:
return i_to_a(strlen(arg1)); retstr = i_to_a( strlen( arg1)) ;
break ;
case UFUPPER: case UFUPPER:
return mkupper(arg1); retstr = mkupper( arg1) ;
break ;
case UFLOWER: case UFLOWER:
return mklower(arg1); retstr = mklower( arg1) ;
break ;
case UFTRUTH: case UFTRUTH:
return ltos(atoi(arg1) == 42); retstr = ltos( atoi( arg1) == 42) ;
break ;
case UFASCII: case UFASCII:
return i_to_a((int) arg1[0]); retstr = i_to_a( (int) arg1[ 0]) ;
break ;
case UFCHR: case UFCHR:
result[0] = atoi(arg1); result[0] = atoi(arg1);
result[1] = 0; result[1] = 0;
return result; retstr = result ;
break ;
case UFGTKEY: case UFGTKEY:
result[0] = tgetc(); result[0] = tgetc();
result[1] = 0; result[1] = 0;
return result; retstr = result ;
break ;
case UFRND: case UFRND:
return i_to_a((ernd() % abs(atoi(arg1))) + 1); retstr = i_to_a( (ernd() % abs( atoi( arg1))) + 1) ;
break ;
case UFABS: case UFABS:
return i_to_a(abs(atoi(arg1))); retstr = i_to_a( abs( atoi( arg1))) ;
break ;
case UFSINDEX: case UFSINDEX:
return i_to_a(sindex(arg1, arg2)); retstr = i_to_a( sindex( arg1, arg2)) ;
break ;
case UFENV: case UFENV:
#if ENVFUNC #if ENVFUNC
tsp = getenv(arg1); tsp = getenv(arg1);
return tsp == NULL ? "" : tsp; retstr = tsp == NULL ? "" : tsp ;
#else #else
return ""; retstr = "" ;
#endif #endif
break ;
case UFBIND: case UFBIND:
return transbind(arg1); retstr = transbind( arg1) ;
break ;
case UFEXIST: case UFEXIST:
return ltos(fexist(arg1)); retstr = ltos( fexist( arg1)) ;
break ;
case UFFIND: case UFFIND:
tsp = flook(arg1, TRUE); tsp = flook(arg1, TRUE);
return tsp == NULL ? "" : tsp; retstr = tsp == NULL ? "" : tsp ;
break ;
case UFBAND: case UFBAND:
return i_to_a(atoi(arg1) & atoi(arg2)); retstr = i_to_a( atoi( arg1) & atoi( arg2)) ;
break ;
case UFBOR: case UFBOR:
return i_to_a(atoi(arg1) | atoi(arg2)); retstr = i_to_a( atoi( arg1) | atoi( arg2)) ;
break ;
case UFBXOR: case UFBXOR:
return i_to_a(atoi(arg1) ^ atoi(arg2)); retstr = i_to_a( atoi( arg1) ^ atoi( arg2)) ;
break ;
case UFBNOT: case UFBNOT:
return i_to_a(~atoi(arg1)); retstr = i_to_a( ~atoi( arg1)) ;
break ;
case UFXLATE: case UFXLATE:
return xlat(arg1, arg2, arg3); retstr = xlat( arg1, arg2, arg3) ;
break ;
default:
exit(-11); /* never should get here */
} }
exit(-11); /* never should get here */ if( arg1)
free( arg1) ;
if( arg2)
free( arg2) ;
if( arg3)
free( arg3) ;
return retstr ;
} }
/* /*