variables, variables, cp_getvar, rewrite

This commit is contained in:
rlar 2016-08-05 21:28:53 +02:00
parent 26acc887e8
commit 18423ba8de
1 changed files with 56 additions and 69 deletions

View File

@ -516,16 +516,21 @@ cp_getvar(char *name, enum cp_types type, void *retval)
fprintf(stderr, "in cp_getvar, trying to get value of variable %s.\n", name); fprintf(stderr, "in cp_getvar, trying to get value of variable %s.\n", name);
#endif #endif
for (v = variables; v && !eq(name, v->va_name); v = v->va_next) for (v = variables; v; v = v->va_next)
; if (eq(name, v->va_name))
if (v == NULL) break;
for (v = uv1; v && !eq(name, v->va_name); v = v->va_next)
;
if (v == NULL && ft_curckt)
for (v = ft_curckt->ci_vars; v && !eq(name, v->va_name); v = v->va_next)
;
if (v == NULL) { if (!v)
for (v = uv1; v; v = v->va_next)
if (eq(name, v->va_name))
break;
if (!v && ft_curckt)
for (v = ft_curckt->ci_vars; v; v = v->va_next)
if (eq(name, v->va_name))
break;
if (!v) {
if (type == CP_BOOL && retval) if (type == CP_BOOL && retval)
*(bool *) retval = FALSE; *(bool *) retval = FALSE;
free_struct_variable(uv1); free_struct_variable(uv1);
@ -533,72 +538,54 @@ cp_getvar(char *name, enum cp_types type, void *retval)
} }
if (v->va_type == type) { if (v->va_type == type) {
if (retval) if (retval)
switch (type) { switch (type) {
case CP_BOOL: case CP_BOOL:
*(bool *) retval = TRUE; *(bool *) retval = TRUE;
break; break;
case CP_NUM: { case CP_NUM:
int *i; *(int *) retval = v->va_num;
i = (int *) retval;
*i = v->va_num;
break; break;
} case CP_REAL:
case CP_REAL: { *(double *) retval = v->va_real;
double *d;
d = (double *) retval;
*d = v->va_real;
break; break;
}
case CP_STRING: { /* Gotta be careful to have room. */ case CP_STRING: { /* Gotta be careful to have room. */
char *s; char *s = cp_unquote(v->va_string);
s = cp_unquote(v->va_string);
cp_wstrip(s); cp_wstrip(s);
(void) strcpy((char*) retval, s); strcpy((char*) retval, s);
tfree(s);/*DG*/ tfree(s);
break; break;
} }
case CP_LIST: { /* Funny case... */ case CP_LIST: /* Funny case... */
struct variable **tv; *(struct variable **) retval = v->va_vlist;
tv = (struct variable **) retval;
*tv = v->va_vlist;
break; break;
}
default: default:
fprintf(cp_err, fprintf(cp_err,
"cp_getvar: Internal Error: bad var type %d.\n", type); "cp_getvar: Internal Error: bad var type %d.\n", type);
break; break;
} }
free_struct_variable(uv1); free_struct_variable(uv1);
return (TRUE); return (TRUE);
}
} else {
/* Try to coerce it.. */ /* Try to coerce it.. */
if ((type == CP_NUM) && (v->va_type == CP_REAL)) { if ((type == CP_NUM) && (v->va_type == CP_REAL)) {
int *i; *(int *) retval = (int) v->va_real;
i = (int *) retval;
*i = (int) v->va_real;
free_struct_variable(uv1);
return (TRUE);
} else if ((type == CP_REAL) && (v->va_type == CP_NUM)) { } else if ((type == CP_REAL) && (v->va_type == CP_NUM)) {
double *d; *(double *) retval = (double) v->va_num;
d = (double *) retval;
*d = (double) v->va_num;
free_struct_variable(uv1);
return (TRUE);
} else if ((type == CP_STRING) && (v->va_type == CP_NUM)) { } else if ((type == CP_STRING) && (v->va_type == CP_NUM)) {
(void) sprintf((char*) retval, "%d", v->va_num); sprintf((char*) retval, "%d", v->va_num);
free_struct_variable(uv1);
return (TRUE);
} else if ((type == CP_STRING) && (v->va_type == CP_REAL)) { } else if ((type == CP_STRING) && (v->va_type == CP_REAL)) {
(void) sprintf((char*) retval, "%f", v->va_real); sprintf((char*) retval, "%f", v->va_real);
free_struct_variable(uv1); } else {
return (TRUE);
}
free_struct_variable(uv1); free_struct_variable(uv1);
return (FALSE); return (FALSE);
} }
free_struct_variable(uv1);
return (TRUE);
} }