From: Ton v. O. <tvo...@us...> - 2006-10-01 22:43:43
|
Update of /cvsroot/easycalc/easycalc/mlib In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv8881/mlib Modified Files: konvert.c Log Message: New error c_stacklow. Add stack usage checks to konvert.c. Index: konvert.c =================================================================== RCS file: /cvsroot/easycalc/easycalc/mlib/konvert.c,v retrieving revision 1.65 retrieving revision 1.66 diff -C2 -d -r1.65 -r1.66 *** konvert.c 25 Sep 2006 20:49:05 -0000 1.65 --- konvert.c 1 Oct 2006 22:43:36 -0000 1.66 *************** *** 42,45 **** --- 42,46 ---- #include "txtask.h" #include "cmatrix.h" + #include "chkstack.h" #ifdef SPECFUN_ENABLED *************** *** 325,338 **** static Int16 konvert_to_meq(char *buf,Int16 priority,Int16 *param_count, ! char cbracket); Int16 fl_num(char *input,double *cislo) MLIB; static Int16 ! try_real_num(char *buf,Int16 priority) { double t1; Int16 res; Tmeq item; ! if (!(res=fl_num(buf,&t1))) return 0; --- 326,346 ---- static Int16 konvert_to_meq(char *buf,Int16 priority,Int16 *param_count, ! char cbracket, CError *err) PARSER; Int16 fl_num(char *input,double *cislo) MLIB; + static Int16 try_real_num(char *buf,Int16 priority,CError *err) PARSER; static Int16 ! try_real_num(char *buf,Int16 priority,CError *err) { double t1; Int16 res; Tmeq item; ! ! *err = c_noerror; ! if (chkStack()) { ! *err = c_stacklow; ! return 0; ! } ! if (!(res=fl_num(buf,&t1))) return 0; *************** *** 345,350 **** } static Int16 ! try_degree(char *buf,Int16 priority) { Int16 length=0; --- 353,359 ---- } + static Int16 try_degree(char *buf,Int16 priority,CError *err) PARSER; static Int16 ! try_degree(char *buf,Int16 priority,CError *err) { Int16 length=0; *************** *** 353,359 **** Int16 negate=0; Tmeq item; ! if (calcPrefs.trigo_mode!=degree || dispPrefs.base!=disp_decimal) ! return 0; item.rpn.type=real; --- 362,374 ---- Int16 negate=0; Tmeq item; ! ! *err = c_noerror; ! if (chkStack()) { ! *err = c_stacklow; ! return 0; ! } ! if (calcPrefs.trigo_mode!=degree || dispPrefs.base!=disp_decimal) ! return 0; item.rpn.type=real; *************** *** 408,417 **** } static Int16 ! try_int_num(char *buf,Int16 priority) { Int16 length=0; Tmeq item; ! item.rpn.type=integer; item.rpn.allocsize = 0; --- 423,439 ---- } + static Int16 try_int_num(char *buf,Int16 priority,CError *err) PARSER; static Int16 ! try_int_num(char *buf,Int16 priority,CError *err) { Int16 length=0; Tmeq item; ! ! *err = c_noerror; ! if (chkStack()) { ! *err = c_stacklow; ! return 0; ! } ! item.rpn.type=integer; item.rpn.allocsize = 0; *************** *** 447,453 **** item.rpn.u.intval*=16; if (*buf>='0' && *buf<='9') ! item.rpn.u.intval+=*buf-'0'; else ! item.rpn.u.intval+=*buf-'A'+10; buf++; } --- 469,475 ---- item.rpn.u.intval*=16; if (*buf>='0' && *buf<='9') ! item.rpn.u.intval+=*buf-'0'; else ! item.rpn.u.intval+=*buf-'A'+10; buf++; } *************** *** 475,485 **** } static Int16 ! try_op(char *buf,Int16 priority) { Int16 retval=0; Int16 i=0; Tmeq item; ! item.priority=priority; for (;def_ops[i].name;i++) { --- 497,514 ---- } + static Int16 try_op(char *buf,Int16 priority,CError *err) PARSER; static Int16 ! try_op(char *buf,Int16 priority,CError *err) { Int16 retval=0; Int16 i=0; Tmeq item; ! ! *err = c_noerror; ! if (chkStack()) { ! *err = c_stacklow; ! return 0; ! } ! item.priority=priority; for (;def_ops[i].name;i++) { *************** *** 509,512 **** --- 538,542 ---- Int16 prmcnt1 = 1; /* Temp variable for parameter to konvert_to_meq*/ Tmeq item; + CError err; if (buf[0] != '[') *************** *** 523,527 **** i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,']'))) return 0; meq_update_f_pcount(meqstack,i,prmcnt1); --- 553,557 ---- i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,']',&err))) return 0; meq_update_f_pcount(meqstack,i,prmcnt1); *************** *** 533,537 **** static Int16 ! try_func(char *buf,Int16 priority) { Int16 length=0; --- 563,567 ---- static Int16 ! try_func(char *buf,Int16 priority,CError *err) { Int16 length=0; *************** *** 540,549 **** char fname[MAX_FUNCNAME+1]; Tmeq item; ! if (*buf=='(') { /* Only parentheses */ length++; buf++; ! if (!(tmp=konvert_to_meq(buf,priority,&prmcnt1,')'))) ! return 0; return length+tmp; } --- 570,585 ---- char fname[MAX_FUNCNAME+1]; Tmeq item; ! ! *err = c_noerror; ! if (chkStack()) { ! *err = c_stacklow; ! return 0; ! } ! if (*buf=='(') { /* Only parentheses */ length++; buf++; ! if (!(tmp=konvert_to_meq(buf,priority,&prmcnt1,')',err))) ! return 0; return length+tmp; } *************** *** 560,564 **** fname[length]='\0'; if (*buf!='(') /* Not a function */ ! return 0; length++;buf++; --- 596,600 ---- fname[length]='\0'; if (*buf!='(') /* Not a function */ ! return 0; length++;buf++; *************** *** 572,576 **** i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,')'))) return 0; meq_update_f_pcount(meqstack,i,prmcnt1); --- 608,612 ---- i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,')',err))) return 0; meq_update_f_pcount(meqstack,i,prmcnt1); *************** *** 585,589 **** i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,')'))) return 0; meq_update_f_pcount(meqstack,i,prmcnt1); --- 621,625 ---- i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,')',err))) return 0; meq_update_f_pcount(meqstack,i,prmcnt1); *************** *** 609,613 **** i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,')'))) return 0; // item.rpn.u.textfunc.paramcount = 0; --- 645,649 ---- i = meq_push(meqstack,item); if (!(tmp=konvert_to_meq(buf,priority+1, ! &prmcnt1,')',err))) return 0; // item.rpn.u.textfunc.paramcount = 0; *************** *** 617,628 **** } static Int16 ! try_variable(char *buf,Int16 priority) { Int16 length=0; Tmeq item; if (!IS_FUNC_1LETTER(*buf)) ! return 0; item.priority=priority; --- 653,671 ---- } + static Int16 try_variable(char *buf,Int16 priority,CError *err) PARSER; static Int16 ! try_variable(char *buf,Int16 priority,CError *err) { Int16 length=0; Tmeq item; + *err = c_noerror; + if (chkStack()) { + *err = c_stacklow; + return 0; + } + if (!IS_FUNC_1LETTER(*buf)) ! return 0; item.priority=priority; *************** *** 632,636 **** } if (*buf=='(' || IS_FUNC_LETTER(*buf)) ! return 0; /* It is not a function, and it is smaller then MAX_FUNCNAME */ item.rpn.u.varname[length]='\0'; --- 675,679 ---- } if (*buf=='(' || IS_FUNC_LETTER(*buf)) ! return 0; /* It is not a function, and it is smaller then MAX_FUNCNAME */ item.rpn.u.varname[length]='\0'; *************** *** 654,659 **** * slash: "....\\...." */ static Int16 ! try_string(char *buf,Int16 priority) { char *text; --- 697,703 ---- * slash: "....\\...." */ + static Int16 try_string(char *buf,Int16 priority,CError *err) PARSER; static Int16 ! try_string(char *buf,Int16 priority,CError *err) { char *text; *************** *** 661,667 **** Int16 slashcount=0; Tmeq item; ! if (*buf!='"') ! return 0; buf++; text=MemPtrNew(MAX_INPUT_LENGTH+1); --- 705,717 ---- Int16 slashcount=0; Tmeq item; ! ! *err = c_noerror; ! if (chkStack()) { ! *err = c_stacklow; ! return 0; ! } ! if (*buf!='"') ! return 0; buf++; text=MemPtrNew(MAX_INPUT_LENGTH+1); *************** *** 724,740 **** #define EAT_SPACE for (;*buf == ' ';buf++,length++); static Int16 ! konvert_to_meq(char *buf,Int16 priority,Int16 *param_count, ! char cbracket) { Int16 tmp; Int16 length=0; Int16 param_encountred = 1; ! Int16 (*try_num)(char *,Int16); Tmeq item; if (dispPrefs.forceInteger) ! try_num=try_int_num; else ! try_num=try_real_num; /* We should allow an empty beginning */ --- 774,796 ---- #define EAT_SPACE for (;*buf == ' ';buf++,length++); static Int16 ! konvert_to_meq(char *buf,Int16 priority,Int16 *param_count, char cbracket, ! CError *err) { Int16 tmp; Int16 length=0; Int16 param_encountred = 1; ! Int16 (*try_num)(char *,Int16,CError *); Tmeq item; + *err = c_noerror; + if (chkStack()) { + *err = c_stacklow; + return 0; + } + if (dispPrefs.forceInteger) ! try_num=try_int_num; else ! try_num=try_real_num; /* We should allow an empty beginning */ *************** *** 761,779 **** /* try_degree must be BEFORE try_num, because otherwise * the degree form wouldn't be recognized */ ! if ((tmp=try_degree(buf,priority+3)) || ! (tmp=try_num(buf,priority+3)) || ! (tmp=try_func(buf,priority+3)) || ! (tmp=try_variable(buf,priority+3)) || ! (tmp=try_string(buf,priority+3)) || ! (tmp=try_shortlist(buf,priority+3))) { buf+=tmp; length+=tmp; } ! else ! return 0; /* Syntax error */ /* Time for postfix operator */ if (*buf == '\'') { ! try_op("\2",priority+1); buf+=1; length+=1; --- 817,836 ---- /* try_degree must be BEFORE try_num, because otherwise * the degree form wouldn't be recognized */ ! if ((tmp=try_degree(buf,priority+3,err)) || ! (!*err && (tmp=try_num(buf,priority+3,err))) || ! (!*err && (tmp=try_func(buf,priority+3,err))) || ! (!*err && (tmp=try_variable(buf,priority+3,err))) || ! (!*err && (tmp=try_string(buf,priority+3,err))) || ! (!*err && (tmp=try_shortlist(buf,priority+3)))) { buf+=tmp; length+=tmp; } ! else { ! return 0; /* Syntax error or stack low */ ! } /* Time for postfix operator */ if (*buf == '\'') { ! try_op("\2",priority+1,err); buf+=1; length+=1; *************** *** 782,786 **** Int16 pos; ! try_op("\3",priority+1); /* Remember the position of the command, so that * we can update parameter count when it is known --- 839,843 ---- Int16 pos; ! try_op("\3",priority+1,err); /* Remember the position of the command, so that * we can update parameter count when it is known *************** *** 792,796 **** length+=1; if (!(tmp = konvert_to_meq(buf,priority+3,&ptrcount, ! ']'))) return 0; /* Update the parameter count of the calling function */ --- 849,853 ---- length+=1; if (!(tmp = konvert_to_meq(buf,priority+3,&ptrcount, ! ']',err))) return 0; /* Update the parameter count of the calling function */ *************** *** 816,820 **** EAT_SPACE; ! if ((tmp=try_op(buf,priority+1))) { buf+=tmp; length+=tmp; --- 873,877 ---- EAT_SPACE; ! if ((tmp=try_op(buf,priority+1,err))) { buf+=tmp; length+=tmp; *************** *** 834,838 **** /* This is for syntax like '3x'=>'3*x' and 3(3+4)*/ else if (IS_FUNC_LETTER(*buf) || *buf=='(') { ! try_op("\1",priority+1); /* Special high-priority '*' */ } else if (*buf==';' && priority==0) { --- 891,895 ---- /* This is for syntax like '3x'=>'3*x' and 3(3+4)*/ else if (IS_FUNC_LETTER(*buf) || *buf=='(') { ! try_op("\1",priority+1,err); /* Special high-priority '*' */ } else if (*buf==';' && priority==0) { *************** *** 874,877 **** --- 931,935 ---- * meq */ + static void meq_prioritize(void) PARSER; static void meq_prioritize(void) *************** *** 886,903 **** /* Prioritize from operators everything, that has * pri>pri_of_the_operator */ ! for (i=0;i<meqcount;i++) ! if (meq[i].rpn.type==oper && meq[i].operpri>0) { ! /* Prioritize backwards */ ! for (j=i-1;j>=0 && meq[j].priority>meq[i].priority;j--) ! meq[j].priority+=meq[i].operpri; ! /* Prioritize forwards */ ! for (j=i+1;j<meqcount && meq[j].priority>meq[i].priority;j++) ! meq[j].priority+=meq[i].operpri; ! } /* Prioritize the particular operators*/ for (i=0;i<meqcount;i++) { if (meq[i].rpn.type==oper) ! meq[i].priority+=meq[i].operpri; } /* Prioritize forward +1 for evalRight operators (handle 2^3^4) --- 944,962 ---- /* Prioritize from operators everything, that has * pri>pri_of_the_operator */ ! for (i=0;i<meqcount;i++) { ! if (meq[i].rpn.type==oper && meq[i].operpri>0) { ! /* Prioritize backwards */ ! for (j=i-1;j>=0 && meq[j].priority>meq[i].priority;j--) ! meq[j].priority+=meq[i].operpri; ! /* Prioritize forwards */ ! for (j=i+1;j<meqcount && meq[j].priority>meq[i].priority;j++) ! meq[j].priority+=meq[i].operpri; ! } ! } /* Prioritize the particular operators*/ for (i=0;i<meqcount;i++) { if (meq[i].rpn.type==oper) ! meq[i].priority+=meq[i].operpri; } /* Prioritize forward +1 for evalRight operators (handle 2^3^4) *************** *** 908,918 **** for (i=0;i<meqcount;i++) { if (meq[i].rpn.type==oper && meq[i].evalRight) ! for (j=i+1;j<meqcount && meq[j].priority>=meq[i].priority;j++) ! meq[j].priority++; } /* Convert operators to ordinary function */ for (i=0;i<meqcount;i++) { if (meq[i].rpn.type==oper) ! meq[i].rpn.type=function; } } --- 967,977 ---- for (i=0;i<meqcount;i++) { if (meq[i].rpn.type==oper && meq[i].evalRight) ! for (j=i+1;j<meqcount && meq[j].priority>=meq[i].priority;j++) ! meq[j].priority++; } /* Convert operators to ordinary function */ for (i=0;i<meqcount;i++) { if (meq[i].rpn.type==oper) ! meq[i].rpn.type=function; } } *************** *** 935,938 **** --- 994,998 ---- * rpn, rpncount - global array, resulting rpn */ + static CodeStack *konvert_to_rpn(void) PARSER; static CodeStack * konvert_to_rpn(void) *************** *** 989,996 **** prmcount = 1; ! if (!konvert_to_meq(buf,0,&prmcount,')')) { /* Free the alocated variables */ meq_free(meqstack); ! *err = c_syntax; return NULL; } --- 1049,1057 ---- prmcount = 1; ! if (!konvert_to_meq(buf,0,&prmcount,')',err)) { /* Free the alocated variables */ meq_free(meqstack); ! if (*err == c_noerror) ! *err = c_syntax; return NULL; } |