/* Some header (Content-type) have a special syntax where attribute=value pairs are used after a leading semicolon. The parse_field code knows about these fields and changes the parsing to the one defined in RFC2045. Returns a pointer to the value which is valid as long as the parse context is valid; NULL is returned in case that attr is not defined in the header, a missing value is represented by an empty string. With LOWER_VALUE set to true, a matching field value will be lowercased. Note, that ATTR should be lowercase. If ATTR is NULL the fucntion returns the first token of the field; i.e. not the parameter but the actual value. A CTX of NULL is allowed and will return NULL. */ const char * rfc822parse_query_parameter (rfc822parse_field_t ctx, const char *attr, int lower_value) { TOKEN t, a; if (!attr) { t = ctx; if (t && (t->type == tATOM || t->type == tQUOTED || t->type == tDOMAINLIT)) { if ( lower_value && !t->flags.lowered ) { lowercase_string (t->data); t->flags.lowered = 1; } return t->data; } return NULL; } for (t = ctx; t; t = t->next) { /* skip to the next semicolon */ for (; t && !(t->type == tSPECIAL && t->data[0] == ';'); t = t->next) ; if (!t) return NULL; if (is_parameter (t)) { /* Look closer. */ a = t->next; /* We know that this is an atom */ if ( !a->flags.lowered ) { lowercase_string (a->data); a->flags.lowered = 1; } if (!strcmp (a->data, attr)) { /* found */ t = a->next->next; /* Either T is now an atom, a quoted string or NULL in * which case we return an empty string. */ if ( lower_value && t && !t->flags.lowered ) { lowercase_string (t->data); t->flags.lowered = 1; } return t ? t->data : ""; } } } return NULL; }
bool constraint_is_for_parameter_in(const Constraint *constraint, const char *names) { int i; bool found = false; CgreenVector *parameter_names = create_vector_of_names(names); if (!is_parameter(constraint)) return false; for (i = 0; i < cgreen_vector_size(parameter_names); i++) { const char *mock_parameter_name = (const char *)cgreen_vector_get(parameter_names, i); if (constraint_is_for_parameter(constraint, mock_parameter_name)) { found = true; break; } } destroy_cgreen_vector(parameter_names); return found; }
/**************** * Some header (Content-type) have a special syntax where attribute=value * pairs are used after a leading semicolon. the parse_field code * knows about these fields and changes the parsing to the one defined * in RFC2045. * Returns a pointer to the value which is valid as long as the * parse context is valid; NULL is returned in case that attr is not * defined in the header, a missing value is reppresented by an empty string. */ const char * rfc822_query_parameter( RFC822_PARSE_CTX ctx, const char *attr ) { TOKEN t, a; for( t = ctx; t ; t = t->next ) { /* skip to the next semicolon */ for( ; t && !(t->type == tSPECIAL && t->data[0]==';'); t = t->next ) ; if( !t ) return NULL; if( is_parameter( t ) ) { /* look closer */ a = t->next; /* we know that this is an atom */ if( !stricmp( a->data, attr ) ) { /* found */ t = a->next->next; /* either t is now an atom, a quoted string or NULL in * which case we retrun an empty string */ return t? t->data : ""; } } } return NULL; }
static Term dif_term(Term t) { if(t==A_SQRT2 || t==A_I || is_float(t) || is_integer(t)) return 0; if(is_atom(t) && is_parameter(t)) return MakeCompound1(OPR_USCORE,t); if(!is_compound(t)) { printf("Can't variate "); WriteTerm(t); puts(""); return 0; } if(CompoundName(t)==OPR_PLUS && CompoundArity(t)==2) { Term d1,d2; d1=dif_term(CompoundArg1(t)); d2=dif_term(CompoundArg2(t)); if(d1==0 && d2==0) return 0; if(d1==0) return d2; if(d2==0) return d1; return MakeCompound2(OPR_PLUS,d1,d2); } if(CompoundName(t)==OPR_MINUS && CompoundArity(t)==2) { Term d1,d2; d1=dif_term(CompoundArg1(t)); d2=dif_term(CompoundArg2(t)); if(d1==0 && d2==0) return 0; if(d1==0) return d2; if(d2==0) return d1; return MakeCompound2(OPR_MINUS,d1,d2); } if(CompoundName(t)==OPR_MINUS && CompoundArity(t)==1) { Term d1; d1=dif_term(CompoundArg1(t)); if(d1==0) return 0; return MakeCompound1(OPR_MINUS,d1); } if(CompoundName(t)==A_SQRT && CompoundArity(t)==1) { Term d1; d1=dif_term(CompoundArg1(t)); if(d1==0) return 0; if(is_compound(d1) && CompoundName(d1)==OPR_MLT && CompoundArg1(d1)==NewInteger(2)) { Term d2; d2=ConsumeCompoundArg(d1,2); FreeAtomic(d1); return MakeCompound1(OPR_MINUS, MakeCompound2(OPR_DIV,d2,t)); } return MakeCompound1(OPR_MINUS, MakeCompound2(OPR_DIV, d1, MakeCompound2(OPR_MLT, NewInteger(2), t))); } if(CompoundName(t)==OPR_POW && CompoundArity(t)==2) { Term d1; int ppow; ppow=IntegerValue(CompoundArg2(t)); d1=dif_term(CompoundArg1(t)); if(d1==0) return 0; if(ppow==2) return MakeCompound2(OPR_MLT,NewInteger(2), MakeCompound2(OPR_MLT, CompoundArg1(t), d1)); return MakeCompound2(OPR_MLT, d1, MakeCompound2(OPR_POW, CompoundArg1(t),NewInteger(ppow-1))); } if(CompoundName(t)==OPR_MLT && CompoundArity(t)==2) { Term d1,d2; d1=dif_term(CompoundArg1(t)); d2=dif_term(CompoundArg2(t)); if(d1==0 && d2==0) return 0; if(d1==0) return MakeCompound2(OPR_MLT,CompoundArg1(t),d2); if(d2==0) return MakeCompound2(OPR_MLT,CompoundArg2(t),d1); return MakeCompound2(OPR_PLUS, MakeCompound2(OPR_MLT,CompoundArg1(t),d2), MakeCompound2(OPR_MLT,CompoundArg2(t),d1)); } if(CompoundName(t)==OPR_DIV && CompoundArity(t)==2) { Term d1,d2; d1=dif_term(CompoundArg1(t)); d2=dif_term(CompoundArg2(t)); if(d1==0 && d2==0) return 0; if(d1==0) return MakeCompound1(OPR_MINUS, MakeCompound2(OPR_DIV, MakeCompound2(OPR_MLT,d2,CompoundArg1(t)), MakeCompound2(OPR_POW,CompoundArg2(t),NewInteger(2)) ) ); if(d2==0) return MakeCompound2(OPR_DIV,d1,CompoundArg2(t)); return MakeCompound2(OPR_DIV, MakeCompound2(OPR_MINUS, MakeCompound2(OPR_MLT,d1,CompoundArg2(t)), MakeCompound2(OPR_MLT,d2,CompoundArg1(t)) ), MakeCompound2(OPR_POW,CompoundArg2(t),NewInteger(2)) ); } printf("Can't variate "); WriteTerm(t); puts(""); return 0; }
Term ProcChVertex(Term t, List ind) { List l, pl, ml; Term rpl; Atom a1, a2; int pli; int ii; if(lagr_hash==NULL) { ErrorInfo(107); puts("ChVertex: no vertices"); return 0; } if(!is_compound(t)||CompoundArity(t)!=2) { ErrorInfo(107); puts("wrong call to ChVertex"); return 0; } pl=CompoundArg1(t); for(l=pl;l;l=ListTail(l)) if(is_function(ListFirst(l),0)) ChangeList(l,CallFunction(ListFirst(l),0)); rpl=CompoundArg2(t); if(!is_list(pl)|| !is_compound(rpl) || CompoundArity(rpl)!=2) { ErrorInfo(107); puts("wrong call to ChVertex"); return 0; } a1=CompoundArg1(rpl);a2=CompoundArg2(rpl); if(!is_parameter(a1)||!is_parameter(a2)) { ErrorInfo(107); puts("wrong call to ChVertex"); return 0; } pl=SortedList(pl,acmp); l=finda2(pl,0); if(is_empty_list(l)) { WarningInfo(108);printf("ChVertex: vertex "); WriteTerm(pl); puts(" not found"); return 0; } ml=CompoundArgN(ListFirst(l),5); ii=0; for(l=ml;l;l=ListTail(l)) { List l1; for(l1=CompoundArg2(ListFirst(l));l1;l1=ListTail(l1)) if(CompoundArg1(ListFirst(l1))==a1) { SetCompoundArg(ListFirst(l1),1,a2);ii++; } } if(ii==0) { WarningInfo(107);printf("ChVertex: vertex ");WriteTerm(pl); printf("has no '%s' within.\n",AtomValue(a1)); } return 0; }
Term ProcLet(Term t, Term ind) { Term t1,nm,sub,kl=0; List il,ol,l1; int transf_fl=0; Atom anti1=0, anti2=0; ol=il=NewList(); t1=ConsumeCompoundArg(t,1); FreeAtomic(t); t1=ProcessAlias(t1); if(is_compound(t1) && CompoundArity(t1)==2 && CompoundName(t1)==OPR_COMMA) { Term a1,a2; a1=ConsumeCompoundArg(t1,1); a2=ConsumeCompoundArg(t1,2); FreeAtomic(t1); ProcLet(MakeCompound1(OPR_LET,a1),0); ProcLet(MakeCompound1(OPR_LET,a2),0); return 0; } if(!is_compound(t1) || CompoundArity(t1)!=2 || (CompoundName(t1)!=OPR_EQSIGN && CompoundName(t1)!=OPR_RARROW)) { ErrorInfo(325); printf("bad argument in let statement\n"); return 0; } if(CompoundName(t1)==OPR_EQSIGN && is_atom(CompoundArg1(t1)) && is_atom(CompoundArg2(t1)) && (CompoundArg2(t1)==A_GAMMA5 || GetAtomProperty(CompoundArg2(t1), A_GAMMA5))) { SetAtomProperty(CompoundArg1(t1),A_GAMMA5,NewInteger(1)); } if(CompoundName(t1)==OPR_RARROW) transf_fl=1; if(CompoundName(t1)==OPR_EQSIGN && is_atom(CompoundArg1(t1)) && is_compound(CompoundArg2(t1)) && CompoundName(CompoundArg2(t1))==A_ANTI && is_atom(CompoundArg1(CompoundArg2(t1)))) { anti1=CompoundArg1(t1); anti2=CompoundArg1(CompoundArg2(t1)); } nm=ConsumeCompoundArg(t1,1); sub=ConsumeCompoundArg(t1,2); FreeAtomic(t1); if(transf_fl) allow_transf_lets=0; if(is_compound(nm) && (CompoundName(nm)==OPR_USCORE || CompoundName(nm)==OPR_CARET)) nm=SplitIndices(nm,&il); if(!is_atom(nm)) { ErrorInfo(325); printf("bad left argument in let call\n"); FreeAtomic(sub); FreeAtomic(t1); return 0; } if(transf_fl) { if(!is_parameter(nm) && !is_particle(nm,NULL)) { ErrorInfo(728); printf("Unknown object '%s'.\n",AtomValue(nm)); return 0; } } if(GetAtomProperty(nm,A_KEEP_LETS)) { Term prp; kl=ExprTo1kl(CopyTerm(sub)); if(kl==0) return 0; prp=GetAtomProperty(nm,A_KEEP_LETS); SetCompoundArg(prp,1,kl); } sub=ExprTo1(sub); alg1_set_cos0(sub); if(sub==0) return 0; if(transf_fl) allow_transf_lets=1; t1=CopyTerm(CompoundArg2(sub)); if(is_empty_list(il)) { l1=t1; while(!is_empty_list(l1)) { Term t; t=CompoundArg2(ListFirst(l1)); if(!is_label(t)) { ErrorInfo(326); printf("unbalanced index '"); WriteTerm(t); printf("' in let statement\n"); FreeAtomic(sub); FreeAtomic(t1); FreeAtomic(ol); return 0; } ol=AppendLast(ol,t); l1=ListTail(l1); } /* mk_let(nm,sub,ol,t1); */ } else { List t2; t2=0; if(ListLength(il)!=ListLength(t1)) { ErrorInfo(327); printf("distinct indices number "); printf("in let statement.\n"); FreeAtomic(sub); FreeAtomic(t1); return 0; } l1=t1; while(!is_empty_list(l1)) { Term t; t=CompoundArg2(ListFirst(l1)); if(!is_atom(t) || !ListMember(il,t)) { ErrorInfo(326); printf("unbalanced index '"); WriteTerm(t); printf("' in let statement\n"); FreeAtomic(sub); FreeAtomic(t1); FreeAtomic(ol); return 0; } ol=AppendLast(ol,t); l1=ListTail(l1); } l1=il; while(!is_empty_list(l1)) { List l2; if(!ListMember(ol,ListFirst(l1))) { ErrorInfo(326); printf("unbalanced index '"); WriteTerm(t); printf("' in let statement\n"); FreeAtomic(sub); FreeAtomic(t1); FreeAtomic(ol); return 0; } for(l2=t1;l2;l2=ListTail(l2)) if(ListFirst(l1)==CompoundArg2(ListFirst(l2))) { t2=AppendLast(t2,ListFirst(l2)); break; } l1=ListTail(l1); } RemoveList(t1); t1=t2; FreeAtomic(ol); ol=il; } /* mk_let(nm,sub,ol,t1); */ l1=t1; while(!is_empty_list(l1)) { SetCompoundArg(ListFirst(l1),2,0); l1=ListTail(l1); } /*WriteTerm(sub); puts(""); getchar();*/ t=MakeCompound(OPR_LET,5); SetCompoundArg(t,1,sub); SetCompoundArg(t,2,ol); SetCompoundArg(t,3,alg1_inv_alg(sub)); if(transf_fl==0) { Term tt; tt=alg1_guess_mpl(sub); if(tt) { SetCompoundArg(t,4,NewInteger(1)); SetCompoundArg(t,5,tt); } else { int tp; tt=alg1_guess_mtr(sub, &tp); if(tt) { SetCompoundArg(t,4,NewInteger(tp)); SetCompoundArg(t,5,tt); } } ReportRedefined(nm,"let-substitution"); SetAtomProperty(nm,PROP_INDEX,t1); SetAtomProperty(nm,PROP_TYPE,t); alg1_let_cw(nm); if(anti1 && anti2) { SetAtomProperty(anti1,A_ANTI,anti2); SetAtomProperty(anti2,A_ANTI,anti1); } else if(only_prm(CompoundArg1(t))) SetAtomProperty(nm,A_ANTI,nm); return 0; } l1=GetAtomProperty(nm,PROP_INDEX); if(!EqualTerms(l1,t1)) { ErrorInfo(729); puts("transformed object has other indices types"); return 0; } if(GetAtomProperty(nm,OPR_LET)) { WarningInfo(0); printf("Warning: transformation rule for '%s' is redefined.\n", AtomValue(nm)); } SetAtomProperty(nm,OPR_LET,t); return 0; }
Term AtomicTo1(Term t, Term ind) { List rl; Term ret; Term t1; Atom ttype; if(is_compound(t) && CompoundName(t)==A_ALG1) { List l; ttype=A_ALG1; rl=CopyTerm(CompoundArg2(t)); for(l=rl;l;l=ListTail(l)) SetCompoundArg(ListFirst(l),2,0); goto cnt; } if(is_compound(t) && CompoundName(t)==A_FBRACET) { t=alg1_mk_wild(t,&rl,&ind); ttype=OPR_WILD; goto cnt; } /* if(is_compound(t) && CompoundName(t)==A_CC) { t=cc_particle(t,&rl); ttype=OPR_FIELD; goto cnt; } */ if(is_let(t,&rl)) { ttype=OPR_LET; goto cnt; } if(is_parameter(t) || (is_compound(t) && (CompoundName(t)==A_COS || CompoundName(t)==A_SIN)) ) { rl=NewList(); ttype=OPR_PARAMETER; goto cnt; } if(is_particle(t,&rl)) { ttype=OPR_FIELD; goto cnt; } if(is_special(t,&rl)) { ttype=OPR_SPECIAL; goto cnt; } ErrorInfo(301); printf(" \'%s\' undefined object.\n",AtomValue(t)); FreeAtomic(ind); longjmp(alg1_jmp_buf,1); cnt: ret=MakeCompound(A_MTERM,4); SetCompoundArg(ret,1,NewInteger(1)); SetCompoundArg(ret,2,NewInteger(1)); t1=0; if(!is_empty_list(rl)) { Term ttt; int skipped; rl=CopyTerm(rl); if(is_empty_list(ind)) { SetCompoundArg(ret,3,AppendFirst(NewList(),MakeCompound2(ttype,rl,t))); return AppendFirst(NewList(),ret); } skipped=must_skip(ListLength(ind),rl); if(skipped==-1) { ErrorInfo(302); printf(" can not set indices "); WriteTerm(ind); printf(" to \'%s\'.\n",AtomValue(t)); FreeAtomic(ind); FreeAtomic(rl); longjmp(alg1_jmp_buf,1); } t1=rl; ttt=ind; while(!is_empty_list(rl)) { if(skipped!=0 && should_skip(skipped,CompoundArg1(ListFirst(rl)))) { rl=ListTail(rl); continue; } SetCompoundArg(ListFirst(rl),2,ListFirst(ttt)); rl=ListTail(rl); ttt=ListTail(ttt); } FreeAtomic(ind); } if(ttype==OPR_PARAMETER && is_compound(t)) { Term mmm=MakeCompound(ttype,CompoundArity(t)+2); SetCompoundArg(mmm,1,t1); SetCompoundArg(mmm,2,CompoundName(t)); SetCompoundArg(mmm,3,CompoundArg1(t)); if(CompoundArity(t)==2) { Term m=ExprTo1(ConsumeCompoundArg(t,2)); m=CompoundArg1(m); if(ListLength(m)!=1) { ErrorInfo(0); puts("bad expression in sin/cos."); longjmp(alg1_jmp_buf,1); } m=ListFirst(m); SetCompoundArg(mmm,4,m); } SetCompoundArg(ret,3,AppendFirst(NewList(),mmm)); } else SetCompoundArg(ret,3,AppendFirst(NewList(),MakeCompound2(ttype,t1,t))); return AppendFirst(NewList(),ret); }
/* Program extracts from Chapter 13 of
intptr_t mock_(TestReporter* test_reporter, const char *function, const char *mock_file, int mock_line, const char *parameters, ...) { va_list actuals; CgreenVector *actual_values; CgreenVector *parameter_names; int failures_before_read_only_constraints_executed; int failures_after_read_only_constraints_executed; int i; intptr_t stored_result; RecordedExpectation *expectation = find_expectation(function); va_start(actuals, parameters); actual_values = create_vector_of_actuals(actuals, number_of_parameters_in(parameters)); va_end(actuals); parameter_names = create_vector_of_names(parameters); if (expectation == NULL) { handle_missing_expectation_for(function, mock_file, mock_line, parameter_names, actual_values, test_reporter); destroy_cgreen_vector(actual_values); destroy_cgreen_vector(parameter_names); return 0; } if (is_never_call(expectation)) { report_violated_never_call(test_reporter, expectation); destroy_cgreen_vector(actual_values); destroy_cgreen_vector(parameter_names); return 0; } ensure_successfully_mocked_calls_list_exists(); cgreen_vector_add(successfully_mocked_calls, (void*)function); stored_result = stored_result_or_default_for(expectation->constraints); for (i = 0; i < cgreen_vector_size(expectation->constraints); i++) { Constraint *constraint = (Constraint *)cgreen_vector_get(expectation->constraints, i); if (!is_parameter(constraint)) continue; if (!constraint_is_for_parameter_in(constraint, parameters)) { // if expectation parameter name isn't in parameter_names, // fail test and skip applying constraints unlikely to match report_mock_parameter_name_not_found(test_reporter, expectation, constraint->parameter_name); destroy_expectation_if_time_to_die(expectation); destroy_cgreen_vector(actual_values); destroy_cgreen_vector(parameter_names); return stored_result; } } // if read-only constraints aren't matching, content-setting ones might corrupt memory // apply read-only ones first, and if they don't fail, then do the deeper constraints failures_before_read_only_constraints_executed = test_reporter->failures; for (i = 0; i < cgreen_vector_size(parameter_names); i++) { const char* parameter_name = (const char*)cgreen_vector_get(parameter_names, i); uintptr_t actual = (uintptr_t)cgreen_vector_get(actual_values, i); apply_any_read_only_parameter_constraints(expectation, parameter_name, actual, test_reporter); } failures_after_read_only_constraints_executed = test_reporter->failures; // FIXME: this comparison doesn't work because only parent processes' pass/fail counts are updated, // and even then only once they read from the pipe if (failures_before_read_only_constraints_executed == failures_after_read_only_constraints_executed) { for (i = 0; i < cgreen_vector_size(parameter_names); i++) { const char* parameter_name = (const char*)cgreen_vector_get(parameter_names, i); uintptr_t actual = (uintptr_t)cgreen_vector_get(actual_values, i); apply_any_content_setting_parameter_constraints(expectation, parameter_name, actual, test_reporter); } } destroy_cgreen_vector(parameter_names); destroy_cgreen_vector(actual_values); destroy_expectation_if_time_to_die(expectation); return stored_result; }