struct variable * cp_enqvar(char *word) { struct dvec *d; struct variable *vv; if (*word == '&') { word++; d = vec_get(word); if (!d) return (NULL); if (d->v_link2) fprintf(cp_err, "Warning: only one vector may be accessed with the $& notation.\n"); if (d->v_length == 1) { double value = isreal(d) ? d->v_realdata[0] : realpart(d->v_compdata[0]); return var_alloc_real(copy(word), value, NULL); } else { struct variable *list = NULL; int i; for (i = d->v_length; --i >= 0;) { double value = isreal(d) ? d->v_realdata[i] : realpart(d->v_compdata[i]); list = var_alloc_real(NULL, value, list); } return var_alloc_vlist(copy(word), list, NULL); } } if (plot_cur) { for (vv = plot_cur->pl_env; vv; vv = vv->va_next) if (eq(vv->va_name, word)) return (vv); if (eq(word, "curplotname")) return var_alloc_string(copy(word), copy(plot_cur->pl_name), NULL); if (eq(word, "curplottitle")) return var_alloc_string(copy(word), copy(plot_cur->pl_title), NULL); if (eq(word, "curplotdate")) return var_alloc_string(copy(word), copy(plot_cur->pl_date), NULL); if (eq(word, "curplot")) return var_alloc_string(copy(word), copy(plot_cur->pl_typename), NULL); if (eq(word, "plots")) { struct variable *list = NULL; struct plot *pl; for (pl = plot_list; pl; pl = pl->pl_next) list = var_alloc_string(NULL, copy(pl->pl_typename), list); return var_alloc_vlist(copy(word), list, NULL); } } if (ft_curckt) for (vv = ft_curckt->ci_vars; vv; vv = vv->va_next) if (eq(vv->va_name, word)) return (vv); return (NULL); }
/*CDHW This needs leak checking carefully CDHW*/ struct variable * cp_setparse(wordlist *wl) { char *name = NULL, *val, *copyval, *s, *ss; double *td; struct variable *listv = NULL, *vv, *lv = NULL; struct variable *vars = NULL; int balance; while (wl) { if (name) tfree(name); name = cp_unquote(wl->wl_word); wl = wl->wl_next; if ((!wl || (*wl->wl_word != '=')) && !strchr(name, '=')) { vars = var_alloc_bool(copy(name), TRUE, vars); tfree(name); /*DG: cp_unquote Memory leak*/ continue; } if (wl && eq(wl->wl_word, "=")) { wl = wl->wl_next; if (wl == NULL) { fprintf(cp_err, "Error: bad set form.\n"); tfree(name); /*DG: cp_unquote Memory leak*/ if (ft_stricterror) controlled_exit(EXIT_BAD); return (NULL); } val = wl->wl_word; wl = wl->wl_next; } else if (wl && (*wl->wl_word == '=')) { val = wl->wl_word + 1; wl = wl->wl_next; } else if ((s = strchr(name, '=')) != NULL) { val = s + 1; *s = '\0'; if (*val == '\0') { if (!wl) { fprintf(cp_err, "Error: %s equals what?.\n", name); tfree(name); /*DG: cp_unquote Memory leak: free name before exiting*/ if (ft_stricterror) controlled_exit(EXIT_BAD); return (NULL); } else { val = wl->wl_word; wl = wl->wl_next; } } } else { fprintf(cp_err, "Error: bad set form.\n"); tfree(name); /*DG: cp_unquote Memory leak: free name befor exiting */ if (ft_stricterror) controlled_exit(EXIT_BAD); return (NULL); } /* val = cp_unquote(val); DG: bad old val is lost*/ copyval = cp_unquote(val); /*DG*/ strcpy(val, copyval); tfree(copyval); if (eq(val, "(")) { /* The beginning of a list... We have to walk down the * list until we find a close paren... If there are nested * ()'s, treat them as tokens... */ balance = 1; while (wl && wl->wl_word) { if (eq(wl->wl_word, "(")) { balance++; } else if (eq(wl->wl_word, ")")) { if (!--balance) break; } copyval = ss = cp_unquote(wl->wl_word); td = ft_numparse(&ss, FALSE); if (td) vv = var_alloc_real(NULL, *td, NULL); else vv = var_alloc_string(NULL, copy(ss), NULL); tfree(copyval); /*DG: must free ss any way to avoid cp_unquote memory leak*/ if (listv) { lv->va_next = vv; lv = vv; } else { listv = lv = vv; } wl = wl->wl_next; } if (balance && !wl) { fprintf(cp_err, "Error: bad set form.\n"); tfree(name); /* va: cp_unquote memory leak: free name before exiting */ if (ft_stricterror) controlled_exit(EXIT_BAD); return (NULL); } vars = var_alloc_vlist(copy(name), listv, vars); wl = wl->wl_next; continue; } copyval = ss = cp_unquote(val); td = ft_numparse(&ss, FALSE); if (td) { /*** We should try to get CP_NUM's... */ vars = var_alloc_real(copy(name), *td, vars); } else { vars = var_alloc_string(copy(name), copy(val), vars); } tfree(copyval); /*DG: must free ss any way to avoid cp_unquote memory leak */ tfree(name); /* va: cp_unquote memory leak: free name for every loop */ } if (name) tfree(name); return (vars); }