void HashCopy(AssocHashTable *newhash, AssocHashTable *oldhash) { HashIterator i = HashIteratorInit(oldhash); CfAssoc *assoc; while ((assoc = HashIteratorNext(&i))) { HashInsertElement(newhash, assoc->lval, assoc->rval, assoc->dtype); } }
static void GetReturnValue(EvalContext *ctx, char *scope, Promise *pp) { char *result = ConstraintGetRvalValue(ctx, "useresult", pp, RVAL_TYPE_SCALAR); if (result) { AssocHashTableIterator i; CfAssoc *assoc; char newname[CF_BUFSIZE]; Scope *ptr; char index[CF_MAXVARSIZE], match[CF_MAXVARSIZE]; if ((ptr = ScopeGet(scope)) == NULL) { Log(LOG_LEVEL_INFO, "useresult was specified but the method returned no data"); return; } i = HashIteratorInit(ptr->hashtable); while ((assoc = HashIteratorNext(&i))) { snprintf(match, CF_MAXVARSIZE - 1, "last-result["); if (strncmp(match, assoc->lval, strlen(match)) == 0) { char *sp; index[0] = '\0'; sscanf(assoc->lval + strlen(match), "%127[^\n]", index); if ((sp = strchr(index, ']'))) { *sp = '\0'; } else { index[strlen(index) - 1] = '\0'; } if (strlen(index) > 0) { snprintf(newname, CF_BUFSIZE, "%s[%s]", result, index); } else { snprintf(newname, CF_BUFSIZE, "%s", result); } EvalContextVariablePut(ctx, (VarRef) { NULL, PromiseGetBundle(pp)->name, newname }, assoc->rval, DATA_TYPE_STRING); } } } }
static void PrintVariablesInScope(FILE *fp, Scope *scope) { HashIterator i = HashIteratorInit(scope->hashtable); CfAssoc *assoc; while ((assoc = HashIteratorNext(&i))) { fprintf(fp, "%8s %c %s = ", CF_DATATYPES[assoc->dtype], assoc->rval.rtype, assoc->lval); ShowRval(fp, assoc->rval); fprintf(fp, "\n"); } }
void ScopeToList(Scope *sp, Rlist **list) { if (sp == NULL) { return; } AssocHashTableIterator i = HashIteratorInit(sp->hashtable); CfAssoc *assoc; while ((assoc = HashIteratorNext(&i))) { RlistPrependScalar(list, assoc->lval); } }
void HashToList(Scope *sp, Rlist **list) { if (sp == NULL) { return; } HashIterator i = HashIteratorInit(sp->hashtable); CfAssoc *assoc; while ((assoc = HashIteratorNext(&i))) { PrependRScalar(list, assoc->lval, CF_SCALAR); } }
static void PrintVariablesInScopeHtml(FILE *fp, Scope *scope) { HashIterator i = HashIteratorInit(scope->hashtable); CfAssoc *assoc; fprintf(fp, "<table class=border width=600>\n"); fprintf(fp, "<tr><th>dtype</th><th>rtype</th><th>identifier</th><th>Rvalue</th></tr>\n"); while ((assoc = HashIteratorNext(&i))) { fprintf(fp, "<tr><th>%8s</th><td> %c</td><td> %s</td><td> ", CF_DATATYPES[assoc->dtype], assoc->rval.rtype, assoc->lval); ShowRval(fp, assoc->rval); fprintf(fp, "</td></tr>\n"); } fprintf(fp, "</table>\n"); }
void ScopeDeRefListsInHashtable(char *scope, Rlist *namelist, Rlist *dereflist) // Go through scope and for each variable in name-list, replace with a // value from the deref "lol" (list of lists) clock { int len; Scope *ptr; Rlist *rp; CfAssoc *cplist; AssocHashTableIterator i; CfAssoc *assoc; if ((len = RlistLen(namelist)) != RlistLen(dereflist)) { CfOut(OUTPUT_LEVEL_ERROR, "", " !! Name list %d, dereflist %d\n", len, RlistLen(dereflist)); ProgrammingError("Software Error DeRefLists... correlated lists not same length"); } if (len == 0) { return; } ptr = ScopeGet(scope); i = HashIteratorInit(ptr->hashtable); while ((assoc = HashIteratorNext(&i))) { for (rp = dereflist; rp != NULL; rp = rp->next) { cplist = (CfAssoc *) rp->item; if (strcmp(cplist->lval, assoc->lval) == 0) { /* Link up temp hash to variable lol */ if (rp->state_ptr == NULL || rp->state_ptr->type == RVAL_TYPE_FNCALL) { /* Unexpanded function, or blank variable must be skipped. */ return; } if (rp->state_ptr) { CfDebug("Rewriting expanded type for %s from %s to %s\n", assoc->lval, CF_DATATYPES[assoc->dtype], (char *) rp->state_ptr->item); // must first free existing rval in scope, then allocate new (should always be string) RvalDestroy(assoc->rval); // avoids double free - borrowing value from lol (freed in DeleteScope()) assoc->rval.item = xstrdup(rp->state_ptr->item); } switch (assoc->dtype) { case DATA_TYPE_STRING_LIST: assoc->dtype = DATA_TYPE_STRING; assoc->rval.type = RVAL_TYPE_SCALAR; break; case DATA_TYPE_INT_LIST: assoc->dtype = DATA_TYPE_INT; assoc->rval.type = RVAL_TYPE_SCALAR; break; case DATA_TYPE_REAL_LIST: assoc->dtype = DATA_TYPE_REAL; assoc->rval.type = RVAL_TYPE_SCALAR; break; default: /* Only lists need to be converted */ break; } CfDebug(" to %s\n", CF_DATATYPES[assoc->dtype]); } } } }
void ScopeAugment(EvalContext *ctx, const Bundle *bp, const Rlist *arguments) { if (RlistLen(bp->args) != RlistLen(arguments)) { CfOut(OUTPUT_LEVEL_ERROR, "", "While constructing scope \"%s\"\n", bp->name); fprintf(stderr, "Formal = "); RlistShow(stderr, bp->args); fprintf(stderr, ", Actual = "); RlistShow(stderr, arguments); fprintf(stderr, "\n"); FatalError(ctx, "Augment scope, formal and actual parameter mismatch is fatal"); } for (const Rlist *rpl = bp->args, *rpr = arguments; rpl != NULL; rpl = rpl->next, rpr = rpr->next) { const char *lval = rpl->item; CfOut(OUTPUT_LEVEL_VERBOSE, "", " ? Augment scope %s with %s (%c)\n", bp->name, lval, rpr->type); // CheckBundleParameters() already checked that there is no namespace collision // By this stage all functions should have been expanded, so we only have scalars left if (IsNakedVar(rpr->item, '@')) { DataType vtype; char qnaked[CF_MAXVARSIZE]; char naked[CF_BUFSIZE]; GetNaked(naked, rpr->item); if (IsQualifiedVariable(naked) && strchr(naked, CF_NS) == NULL) { snprintf(qnaked, CF_MAXVARSIZE, "%s%c%s", bp->ns, CF_NS, naked); } Rval retval; EvalContextVariableGet(ctx, (VarRef) { NULL, bp->name, qnaked }, &retval, &vtype); switch (vtype) { case DATA_TYPE_STRING_LIST: case DATA_TYPE_INT_LIST: case DATA_TYPE_REAL_LIST: ScopeNewList(ctx, (VarRef) { NULL, bp->name, lval }, RvalCopy((Rval) { retval.item, RVAL_TYPE_LIST}).item, DATA_TYPE_STRING_LIST); break; default: CfOut(OUTPUT_LEVEL_ERROR, "", " !! List parameter \"%s\" not found while constructing scope \"%s\" - use @(scope.variable) in calling reference", qnaked, bp->name); ScopeNewScalar(ctx, (VarRef) { NULL, bp->name, lval }, rpr->item, DATA_TYPE_STRING); break; } } else { switch(rpr->type) { case RVAL_TYPE_SCALAR: ScopeNewScalar(ctx, (VarRef) { NULL, bp->name, lval }, rpr->item, DATA_TYPE_STRING); break; case RVAL_TYPE_FNCALL: { FnCall *subfp = rpr->item; Promise *pp = NULL; // This argument should really get passed down. Rval rval = FnCallEvaluate(ctx, subfp, pp).rval; if (rval.type == RVAL_TYPE_SCALAR) { ScopeNewScalar(ctx, (VarRef) { NULL, bp->name, lval }, rval.item, DATA_TYPE_STRING); } else { CfOut(OUTPUT_LEVEL_ERROR, "", "Only functions returning scalars can be used as arguments"); } } break; default: ProgrammingError("An argument neither a scalar nor a list seemed to appear. Impossible"); } } } /* Check that there are no danglers left to evaluate in the hash table itself */ { Scope *ptr = ScopeGet(bp->name); AssocHashTableIterator i = HashIteratorInit(ptr->hashtable); CfAssoc *assoc = NULL; while ((assoc = HashIteratorNext(&i))) { Rval retval = ExpandPrivateRval(ctx, bp->name, assoc->rval); // Retain the assoc, just replace rval RvalDestroy(assoc->rval); assoc->rval = retval; } } return; }
void ScopeAugment(EvalContext *ctx, const Bundle *bp, const Promise *pp, const Rlist *arguments) { if (RlistLen(bp->args) != RlistLen(arguments)) { Log(LOG_LEVEL_ERR, "While constructing scope '%s'", bp->name); fprintf(stderr, "Formal = "); RlistShow(stderr, bp->args); fprintf(stderr, ", Actual = "); RlistShow(stderr, arguments); fprintf(stderr, "\n"); FatalError(ctx, "Augment scope, formal and actual parameter mismatch is fatal"); } const Bundle *pbp = NULL; if (pp != NULL) { pbp = PromiseGetBundle(pp); } for (const Rlist *rpl = bp->args, *rpr = arguments; rpl != NULL; rpl = rpl->next, rpr = rpr->next) { const char *lval = rpl->item; Log(LOG_LEVEL_VERBOSE, "Augment scope '%s' with variable '%s' (type: %c)", bp->name, lval, rpr->type); // CheckBundleParameters() already checked that there is no namespace collision // By this stage all functions should have been expanded, so we only have scalars left if (IsNakedVar(rpr->item, '@')) { DataType vtype; char naked[CF_BUFSIZE]; GetNaked(naked, rpr->item); Rval retval; if (pbp != NULL) { EvalContextVariableGet(ctx, (VarRef) { pbp->ns, pbp->name, naked }, &retval, &vtype); } else { EvalContextVariableGet(ctx, (VarRef) { NULL, bp->name, naked }, &retval, &vtype); } switch (vtype) { case DATA_TYPE_STRING_LIST: case DATA_TYPE_INT_LIST: case DATA_TYPE_REAL_LIST: EvalContextVariablePut(ctx, (VarRef) { NULL, bp->name, lval }, (Rval) { retval.item, RVAL_TYPE_LIST}, DATA_TYPE_STRING_LIST); break; default: Log(LOG_LEVEL_ERR, "List parameter '%s' not found while constructing scope '%s' - use @(scope.variable) in calling reference", naked, bp->name); EvalContextVariablePut(ctx, (VarRef) { NULL, bp->name, lval }, (Rval) { rpr->item, RVAL_TYPE_SCALAR }, DATA_TYPE_STRING); break; } } else { switch(rpr->type) { case RVAL_TYPE_SCALAR: EvalContextVariablePut(ctx, (VarRef) { NULL, bp->name, lval }, (Rval) { rpr->item, RVAL_TYPE_SCALAR }, DATA_TYPE_STRING); break; case RVAL_TYPE_FNCALL: { FnCall *subfp = rpr->item; Rval rval = FnCallEvaluate(ctx, subfp, pp).rval; if (rval.type == RVAL_TYPE_SCALAR) { EvalContextVariablePut(ctx, (VarRef) { NULL, bp->name, lval }, (Rval) { rval.item, RVAL_TYPE_SCALAR }, DATA_TYPE_STRING); } else { Log(LOG_LEVEL_ERR, "Only functions returning scalars can be used as arguments"); } } break; default: ProgrammingError("An argument neither a scalar nor a list seemed to appear. Impossible"); } } } /* Check that there are no danglers left to evaluate in the hash table itself */ { Scope *ptr = ScopeGet(bp->name); AssocHashTableIterator i = HashIteratorInit(ptr->hashtable); CfAssoc *assoc = NULL; while ((assoc = HashIteratorNext(&i))) { Rval retval = ExpandPrivateRval(ctx, bp->name, assoc->rval); // Retain the assoc, just replace rval RvalDestroy(assoc->rval); assoc->rval = retval; } } return; }
void DeRefListsInHashtable(char *scope,struct Rlist *namelist,struct Rlist *dereflist) // Go through scope and for each variable in name-list, replace with a // value from the deref "lol" (list of lists) clock { int len; struct Scope *ptr; struct Rlist *rp,*state; struct CfAssoc *cplist; HashIterator i; CfAssoc *assoc; if ((len = RlistLen(namelist)) != RlistLen(dereflist)) { CfOut(cf_error,""," !! Name list %d, dereflist %d\n",len, RlistLen(dereflist)); FatalError("Software Error DeRefLists... correlated lists not same length"); } if (len == 0) { return; } ptr = GetScope(scope); i = HashIteratorInit(ptr->hashtable); while ((assoc = HashIteratorNext(&i))) { for (rp = dereflist; rp != NULL; rp = rp->next) { cplist = (struct CfAssoc *)rp->item; if (strcmp(cplist->lval,assoc->lval) == 0) { /* Link up temp hash to variable lol */ state = (struct Rlist *)(cplist->rval); if (rp->state_ptr == NULL || rp->state_ptr->type == CF_FNCALL) { /* Unexpanded function, or blank variable must be skipped.*/ return; } if (rp->state_ptr) { Debug("Rewriting expanded type for %s from %s to %s\n",assoc->lval,CF_DATATYPES[assoc->dtype],rp->state_ptr->item); // must first free existing rval in scope, then allocate new (should always be string) DeleteRvalItem(assoc->rval,assoc->rtype); // avoids double free - borrowing value from lol (freed in DeleteScope()) assoc->rval = strdup(rp->state_ptr->item); } switch(assoc->dtype) { case cf_slist: assoc->dtype = cf_str; assoc->rtype = CF_SCALAR; break; case cf_ilist: assoc->dtype = cf_int; assoc->rtype = CF_SCALAR; break; case cf_rlist: assoc->dtype = cf_real; assoc->rtype = CF_SCALAR; break; } Debug(" to %s\n",CF_DATATYPES[assoc->dtype]); } } } }