Replaced some large local variables by other means in xorriso/match.c

This commit is contained in:
Thomas Schmitt 2011-05-05 07:52:14 +00:00
parent 2e48c434b4
commit b5f674647b
2 changed files with 56 additions and 42 deletions

View File

@ -37,8 +37,12 @@
*/ */
int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag) int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag)
{ {
int l,ret,i,count,bonked= 0,is_constant,is_still_relative= 0; int l,ret,i,count,bonked= 0,is_constant,is_still_relative= 0, adr_size;
char *cpt,*npt,adr_part[2*SfileadrL],absolute_adr[2*SfileadrL],*adr_start,*wd; char *cpt,*npt,*adr_part= NULL, *absolute_adr= NULL, *adr_start,*wd;
adr_size= 2 * SfileadrL;
Xorriso_alloc_meM(adr_part, char, adr_size);
Xorriso_alloc_meM(absolute_adr, char, adr_size);
if(flag&4) if(flag&4)
wd= xorriso->wdx; wd= xorriso->wdx;
@ -48,10 +52,10 @@ int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag)
if(xorriso->search_mode>=2 && xorriso->search_mode<=4) { if(xorriso->search_mode>=2 && xorriso->search_mode<=4) {
if(xorriso->search_mode==3 || xorriso->search_mode==4) { if(xorriso->search_mode==3 || xorriso->search_mode==4) {
l= strlen(adr)+strlen(wd)+1; l= strlen(adr)+strlen(wd)+1;
if(l*2+2>sizeof(xorriso->reg_expr) || l*2+2>sizeof(adr_part)) { if(l*2+2>sizeof(xorriso->reg_expr) || l * 2 + 2 > adr_size) {
sprintf(xorriso->info_text,"Search pattern too long"); sprintf(xorriso->info_text,"Search pattern too long");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0); Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
return(0); {ret= 0; goto ex;}
} }
} }
Xorriso_destroy_re(xorriso,0); Xorriso_destroy_re(xorriso,0);
@ -80,10 +84,10 @@ int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag)
count= i+1; count= i+1;
xorriso->re= TSOB_FELD(regex_t,count); xorriso->re= TSOB_FELD(regex_t,count);
if(xorriso->re==NULL) if(xorriso->re==NULL)
return(-1); {ret= -1; goto ex;}
xorriso->re_constants= TSOB_FELD(char *,count); xorriso->re_constants= TSOB_FELD(char *,count);
if(xorriso->re_constants==NULL) if(xorriso->re_constants==NULL)
return(-1); {ret= -1; goto ex;}
for(i= 0;i<count;i++) for(i= 0;i<count;i++)
xorriso->re_constants[i]= NULL; xorriso->re_constants[i]= NULL;
xorriso->re_count= count; xorriso->re_count= count;
@ -97,12 +101,12 @@ int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag)
for(i= 0;i<count;i++) { for(i= 0;i<count;i++) {
npt= strchr(cpt,'/'); npt= strchr(cpt,'/');
if(npt==NULL) { if(npt==NULL) {
if(strlen(cpt)>=sizeof(adr_part)) if(strlen(cpt) >= adr_size)
return(-1); {ret= -1; goto ex;}
strcpy(adr_part,cpt); strcpy(adr_part,cpt);
} else { } else {
if(npt-cpt>=sizeof(adr_part)) if(npt-cpt >= adr_size)
return(-1); {ret= -1; goto ex;}
strncpy(adr_part,cpt,npt-cpt); strncpy(adr_part,cpt,npt-cpt);
adr_part[npt-cpt]= 0; adr_part[npt-cpt]= 0;
} }
@ -134,7 +138,7 @@ int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag)
if(ret==2) { if(ret==2) {
if(Sregex_string(&(xorriso->re_constants[xorriso->re_fill]),adr_part,0) if(Sregex_string(&(xorriso->re_constants[xorriso->re_fill]),adr_part,0)
<=0) <=0)
return(-1); {ret= -1; goto ex;}
} else { } else {
if(regcomp(&(xorriso->re[xorriso->re_fill]),xorriso->reg_expr,0)!=0) if(regcomp(&(xorriso->re[xorriso->re_fill]),xorriso->reg_expr,0)!=0)
goto cannot_compile; goto cannot_compile;
@ -149,11 +153,11 @@ next_adr_part:;
} }
if(bonked) { if(bonked) {
if(flag&2) if(flag&2)
return(2); {ret= 2; goto ex;}
sprintf(xorriso->info_text, "Your '..' bonked at the %s directory.", sprintf(xorriso->info_text, "Your '..' bonked at the %s directory.",
is_still_relative ? "working" : "root"); is_still_relative ? "working" : "root");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE",0); Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE",0);
return(0); {ret= 0; goto ex;}
} }
Xorriso__bourne_to_reg(adr_start,xorriso->reg_expr,0); /* just for show */ Xorriso__bourne_to_reg(adr_start,xorriso->reg_expr,0); /* just for show */
@ -167,39 +171,43 @@ next_adr_part:;
is_constant= (ret==2); is_constant= (ret==2);
} else { } else {
if(strlen(adr)>=sizeof(xorriso->reg_expr)) if(strlen(adr)>=sizeof(xorriso->reg_expr))
return(-1); {ret= -1; goto ex;}
strcpy(xorriso->reg_expr,adr); strcpy(xorriso->reg_expr,adr);
} }
xorriso->re_count= 0; /* tells matcher that this is not structured */ xorriso->re_count= 0; /* tells matcher that this is not structured */
xorriso->re_constants= TSOB_FELD(char *,1); xorriso->re_constants= TSOB_FELD(char *,1);
if(xorriso->re_constants==NULL) if(xorriso->re_constants==NULL)
return(-1); {ret= -1; goto ex;}
xorriso->re_constants[0]= NULL; xorriso->re_constants[0]= NULL;
if(is_constant) { if(is_constant) {
if(strcmp(adr,"*")==0) { if(strcmp(adr,"*")==0) {
if(Sregex_string(&(xorriso->re_constants[0]),"",0)<=0) if(Sregex_string(&(xorriso->re_constants[0]),"",0)<=0)
return(-1); {ret= -1; goto ex;}
} else { } else {
if(Sregex_string(&(xorriso->re_constants[0]),adr,0)<=0) if(Sregex_string(&(xorriso->re_constants[0]),adr,0)<=0)
return(-1); {ret= -1; goto ex;}
} }
xorriso->re_fill= 1; xorriso->re_fill= 1;
} else { } else {
xorriso->re= TSOB_FELD(regex_t,1); xorriso->re= TSOB_FELD(regex_t,1);
if(xorriso->re==NULL) if(xorriso->re==NULL)
return(-1); {ret= -1; goto ex;}
if(regcomp(&(xorriso->re[0]),xorriso->reg_expr,0)!=0) { if(regcomp(&(xorriso->re[0]),xorriso->reg_expr,0)!=0) {
cannot_compile:; cannot_compile:;
sprintf(xorriso->info_text, "Cannot compile regular expression : %s", sprintf(xorriso->info_text, "Cannot compile regular expression : %s",
xorriso->reg_expr); xorriso->reg_expr);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE",0); Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE",0);
return(0); {ret= 0; goto ex;}
} }
} }
} }
} }
return(1); ret= 1;
ex:;
Xorriso_free_meM(adr_part);
Xorriso_free_meM(absolute_adr);
return(ret);
} }
@ -213,7 +221,9 @@ int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at,
int flag) int flag)
{ {
int ret,i,re_start= 0,reg_nomatch= -1; int ret,i,re_start= 0,reg_nomatch= -1;
char *cpt,*npt,adr_part[SfileadrL],*mpt; char *cpt,*npt, *adr_part= NULL, *mpt;
Xorriso_alloc_meM(adr_part, char, SfileadrL);
reg_nomatch= REG_NOMATCH; reg_nomatch= REG_NOMATCH;
@ -222,13 +232,13 @@ int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at,
if(xorriso->re_constants!=NULL) if(xorriso->re_constants!=NULL)
if(xorriso->re_constants[0]!=NULL) { if(xorriso->re_constants[0]!=NULL) {
if(xorriso->re_constants[0][0]==0) if(xorriso->re_constants[0][0]==0)
return(0); {ret= 0; goto ex;}
if(strcmp(xorriso->re_constants[0],to_match)!=0) if(strcmp(xorriso->re_constants[0],to_match)!=0)
return(reg_nomatch); {ret= reg_nomatch; goto ex;}
return(0); {ret= 0; goto ex;}
} }
ret= regexec(&(xorriso->re[0]),to_match,1,xorriso->match,0); ret= regexec(&(xorriso->re[0]),to_match,1,xorriso->match,0);
return(ret); goto ex;
} }
cpt= to_match; cpt= to_match;
@ -243,7 +253,7 @@ int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at,
npt= strchr(cpt,'/'); npt= strchr(cpt,'/');
if(npt==NULL) { if(npt==NULL) {
if(i<xorriso->re_fill-1 && !(flag&1)) if(i<xorriso->re_fill-1 && !(flag&1))
return(reg_nomatch); /* this must be the last expression part */ {ret= reg_nomatch; goto ex;} /* this must be the last expression part */
mpt= cpt; mpt= cpt;
} else { } else {
strncpy(adr_part,cpt,npt-cpt); strncpy(adr_part,cpt,npt-cpt);
@ -253,17 +263,17 @@ int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at,
if(xorriso->re_constants[i]!=NULL) { if(xorriso->re_constants[i]!=NULL) {
if(xorriso->re_constants[i][0]!=0) /* empty constant matches anything */ if(xorriso->re_constants[i][0]!=0) /* empty constant matches anything */
if(strcmp(xorriso->re_constants[i],mpt)!=0) if(strcmp(xorriso->re_constants[i],mpt)!=0)
return(reg_nomatch); {ret= reg_nomatch; goto ex;}
} else { } else {
ret= regexec(&(xorriso->re[i]),mpt,1,xorriso->match,0); ret= regexec(&(xorriso->re[i]),mpt,1,xorriso->match,0);
if(ret!=0) if(ret!=0)
return(ret); goto ex;
} }
if(npt==NULL) { if(npt==NULL) {
if(i>=xorriso->re_fill-1) if(i>=xorriso->re_fill-1)
return(0); /* MATCH */ {ret= 0; goto ex;} /* MATCH */
*failed_at= i+1; *failed_at= i+1;
return(reg_nomatch); {ret= reg_nomatch; goto ex;}
} }
cpt= npt+1; cpt= npt+1;
while(*cpt=='/') while(*cpt=='/')
@ -271,8 +281,11 @@ int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at,
} }
*failed_at= xorriso->re_fill; *failed_at= xorriso->re_fill;
if(flag & 2) if(flag & 2)
return(0); /* MATCH */ {ret= 0; goto ex;} /* MATCH */
return(reg_nomatch); ret= reg_nomatch;
ex:;
Xorriso_free_meM(adr_part);
return(ret);
} }
@ -557,7 +570,6 @@ int Xorriso_check_matchcount(struct XorrisO *xorriso,
int count, int nonconst_mismatches, int num_patterns, int count, int nonconst_mismatches, int num_patterns,
char **patterns, int flag) char **patterns, int flag)
{ {
char sfe[5*SfileadrL];
if((flag&1) && (count!=1 || nonconst_mismatches)){ if((flag&1) && (count!=1 || nonconst_mismatches)){
if(count-nonconst_mismatches>0) if(count-nonconst_mismatches>0)
@ -566,8 +578,8 @@ int Xorriso_check_matchcount(struct XorrisO *xorriso,
else else
sprintf(xorriso->info_text, "No pattern match with any file object"); sprintf(xorriso->info_text, "No pattern match with any file object");
if(num_patterns==1) if(num_patterns==1)
sprintf(xorriso->info_text+strlen(xorriso->info_text), ": %s", sprintf(xorriso->info_text+strlen(xorriso->info_text), ": ");
Text_shellsafe(patterns[0], sfe, 0)); Text_shellsafe(patterns[0], xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
(flag&2 ? "FAILURE" : "SORRY"), 0); (flag&2 ? "FAILURE" : "SORRY"), 0);
return(0); return(0);
@ -626,7 +638,9 @@ int Xorriso_expand_disk_pattern(struct XorrisO *xorriso,
{ {
int ret, count= 0, abs_adr= 0, i, was_count, was_filec; int ret, count= 0, abs_adr= 0, i, was_count, was_filec;
int nonconst_mismatches= 0, dive_count= 0; int nonconst_mismatches= 0, dive_count= 0;
char sfe[5*SfileadrL], dir_adr[SfileadrL]; char *dir_adr= NULL;
Xorriso_alloc_meM(dir_adr, char, SfileadrL);
*filec= 0; *filec= 0;
*filev= NULL; *filev= NULL;
@ -638,7 +652,7 @@ int Xorriso_expand_disk_pattern(struct XorrisO *xorriso,
ret= Xorriso_prepare_expansion_pattern(xorriso, patterns[i], 4); ret= Xorriso_prepare_expansion_pattern(xorriso, patterns[i], 4);
if(ret<=0) if(ret<=0)
return(ret); goto ex;
if(ret==2) if(ret==2)
abs_adr= 4; abs_adr= 4;
@ -652,9 +666,8 @@ int Xorriso_expand_disk_pattern(struct XorrisO *xorriso,
ret= Sfile_type(dir_adr, 1|4); ret= Sfile_type(dir_adr, 1|4);
if(ret!=2) { if(ret!=2) {
Xorriso_msgs_submit(xorriso, 0, dir_adr, 0, "ERRFILE", 0); Xorriso_msgs_submit(xorriso, 0, dir_adr, 0, "ERRFILE", 0);
sprintf(xorriso->info_text, sprintf(xorriso->info_text, "Address set by -cdx is not a directory: ");
"Address set by -cdx is not a directory: %s", Text_shellsafe(dir_adr, xorriso->info_text, 1);
Text_shellsafe(dir_adr, sfe, 0));
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0); Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex; ret= 0; goto ex;
} }
@ -695,7 +708,7 @@ int Xorriso_expand_disk_pattern(struct XorrisO *xorriso,
ret= Xorriso_prepare_expansion_pattern(xorriso, patterns[i], 4); ret= Xorriso_prepare_expansion_pattern(xorriso, patterns[i], 4);
if(ret<=0) if(ret<=0)
return(ret); goto ex;
if(patterns[i][0]=='/' || abs_adr) { if(patterns[i][0]=='/' || abs_adr) {
strcpy(dir_adr, "/"); strcpy(dir_adr, "/");
@ -730,6 +743,7 @@ ex:;
Sfile_destroy_argv(&count, filev, 0); Sfile_destroy_argv(&count, filev, 0);
*filec= 0; *filec= 0;
} }
Xorriso_free_meM(dir_adr);
return(ret); return(ret);
} }

View File

@ -1 +1 @@
#define Xorriso_timestamP "2011.05.04.171628" #define Xorriso_timestamP "2011.05.05.075233"