ngspice/src/frontend/options.c

426 lines
12 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
**********/
/*
* The user-supplied routine to deal with variables. Most variables we
* don't use often, so just call cp_getvar when they are needed. Spice
* variables, though, and a few commonly used ones are dealt with here.
*/
#include "ngspice/ngspice.h"
#include "ngspice/cpdefs.h"
#include "ngspice/ftedefs.h"
#include "ngspice/dvec.h"
#include "ngspice/fteinp.h"
2000-04-27 22:03:57 +02:00
src/Makefile.am src/help.c src/main.c src/circuit/Makefile.am src/circuit/ifnewuid.c src/frontend/Makefile.am src/frontend/aspice.c src/frontend/circuits.h src/frontend/com_display.c src/frontend/com_hardcopy.c src/frontend/commands.c src/frontend/commands.h src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/device.c src/frontend/diff.c src/frontend/display.c src/frontend/dotcards.c src/frontend/fourier.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/mw_coms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/postcoms.c src/frontend/postsc.c src/frontend/rawfile.c src/frontend/resource.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/subckt.c src/frontend/vectors.c src/frontend/where.c src/frontend/plotting/Makefile.am src/frontend/plotting/agraf.c src/frontend/plotting/graf.c src/frontend/plotting/plotcurv.c src/frontend/plotting/plotit.c src/frontend/plotting/x11.c src/frontend/plotting/xgraph.c src/include/Makefile.am src/maths/cmaths/cmath4.c src/misc/terminal.c src/misc/terminal.h src/parser/cshpar.c src/parser/front.c src/parser/front.h src/parser/history.c src/parser/history.h src/parser/modify.c src/parser/var2.c src/parser/var2.h src/parser/variable.c: Refactoring of frontend code. * src/include/ftehelp.h src/include/variable.h: Moved into frontend directory. * src/include/cpdefs.h src/include/cpextern.h src/include/ftedefs.h src/include/plot.h: Updates.
2000-06-27 18:09:02 +02:00
#include "circuits.h"
#include "options.h"
#include "variable.h"
#include "control.h"
#include "spiceif.h"
2000-04-27 22:03:57 +02:00
static void setdb(char *str);
2009-02-22 19:28:25 +01:00
bool ft_acctprint = FALSE, ft_noacctprint = FALSE, ft_listprint = FALSE;
2009-08-22 18:54:03 +02:00
bool ft_nodesprint = FALSE, ft_optsprint = FALSE, ft_noinitprint = FALSE;
bool ft_ngdebug = FALSE, ft_stricterror = FALSE;
2000-04-27 22:03:57 +02:00
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
/* The user-supplied routine to query the values of variables. This
* recognises the $&varname notation, and also searches the values of
* plot and circuit environment variables.
*/
struct variable *
cp_enqvar(char *word)
{
struct dvec *d;
struct variable *vv, *tv;
struct plot *pl;
int i;
if (*word == '&') {
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
word++;
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
d = vec_get(word);
2012-08-12 12:48:27 +02:00
if (!d)
2000-04-27 22:03:57 +02:00
return (NULL);
2012-08-12 12:48:27 +02:00
if (d->v_length == 1) {
vv = alloc(struct variable);
vv->va_next = NULL;
vv->va_name = copy(word);
vv->va_type = CP_REAL;
if (isreal(d))
vv->va_real = d->v_realdata[0];
else
vv->va_real = realpart(d->v_compdata[0]);
} else {
vv = alloc(struct variable);
vv->va_next = NULL;
vv->va_name = copy(word);
vv->va_type = CP_LIST;
vv->va_vlist = NULL;
for (i = d->v_length - 1; i >= 0; i--) {
tv = alloc(struct variable);
tv->va_type = CP_REAL;
if (isreal(d))
tv->va_real = d->v_realdata[i];
else
tv->va_real = realpart(d->v_compdata[i]);
tv->va_next = vv->va_vlist;
vv->va_vlist = tv;
}
}
if (d->v_link2)
fprintf(cp_err,
"Warning: only one vector may be accessed with the $& notation.\n");
return (vv);
2012-08-12 12:43:09 +02:00
}
2000-04-27 22:03:57 +02:00
if (plot_cur) {
for (vv = plot_cur->pl_env; vv; vv = vv->va_next)
if (eq(vv->va_name, word))
2012-08-12 12:48:27 +02:00
return (vv);
2000-04-27 22:03:57 +02:00
if (eq(word, "curplotname")) {
vv = alloc(struct variable);
2012-08-12 12:43:09 +02:00
vv->va_next = NULL;
2000-04-27 22:03:57 +02:00
vv->va_name = word;
vv->va_type = CP_STRING;
2000-04-27 22:03:57 +02:00
vv->va_string = copy(plot_cur->pl_name);
} else if (eq(word, "curplottitle")) {
vv = alloc(struct variable);
2012-08-12 12:43:09 +02:00
vv->va_next = NULL;
2000-04-27 22:03:57 +02:00
vv->va_name = word;
vv->va_type = CP_STRING;
2000-04-27 22:03:57 +02:00
vv->va_string = copy(plot_cur->pl_title);
} else if (eq(word, "curplotdate")) {
vv = alloc(struct variable);
2012-08-12 12:43:09 +02:00
vv->va_next = NULL;
2000-04-27 22:03:57 +02:00
vv->va_name = word;
vv->va_type = CP_STRING;
2000-04-27 22:03:57 +02:00
vv->va_string = copy(plot_cur->pl_date);
} else if (eq(word, "curplot")) {
vv = alloc(struct variable);
2012-08-12 12:43:09 +02:00
vv->va_next = NULL;
2000-04-27 22:03:57 +02:00
vv->va_name = word;
vv->va_type = CP_STRING;
2000-04-27 22:03:57 +02:00
vv->va_string = copy(plot_cur->pl_typename);
} else if (eq(word, "plots")) {
vv = alloc(struct variable);
2012-08-12 12:43:09 +02:00
vv->va_next = NULL;
2000-04-27 22:03:57 +02:00
vv->va_name = word;
vv->va_type = CP_LIST;
2012-08-12 12:43:09 +02:00
vv->va_vlist = NULL;
2000-04-27 22:03:57 +02:00
for (pl = plot_list; pl; pl = pl->pl_next) {
tv = alloc(struct variable);
tv->va_type = CP_STRING;
2000-04-27 22:03:57 +02:00
tv->va_string = copy(pl->pl_typename);
tv->va_next = vv->va_vlist;
vv->va_vlist = tv;
}
}
2012-08-12 12:48:27 +02:00
if (vv)
2000-04-27 22:03:57 +02:00
return (vv);
}
2012-08-12 12:43:09 +02:00
2012-08-12 12:48:27 +02:00
if (ft_curckt)
2000-04-27 22:03:57 +02:00
for (vv = ft_curckt->ci_vars; vv; vv = vv->va_next)
if (eq(vv->va_name, word))
2012-08-12 12:48:27 +02:00
return (vv);
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
return (NULL);
}
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
/* Return the plot and ckt env vars, $plots, and $curplot{name,title,date,} */
void
cp_usrvars(struct variable **v1, struct variable **v2)
{
struct variable *v, *tv;
2012-08-12 12:48:27 +02:00
v = plot_cur ? plot_cur->pl_env : NULL;
2012-08-12 12:43:09 +02:00
if ((tv = cp_enqvar("plots")) != NULL) {
2000-04-27 22:03:57 +02:00
tv->va_next = v;
v = tv;
}
if ((tv = cp_enqvar("curplot")) != NULL) {
2000-04-27 22:03:57 +02:00
tv->va_next = v;
v = tv;
}
if ((tv = cp_enqvar("curplottitle")) != NULL) {
2000-04-27 22:03:57 +02:00
tv->va_next = v;
v = tv;
}
if ((tv = cp_enqvar("curplotname")) != NULL) {
2000-04-27 22:03:57 +02:00
tv->va_next = v;
v = tv;
}
if ((tv = cp_enqvar("curplotdate")) != NULL) {
2000-04-27 22:03:57 +02:00
tv->va_next = v;
v = tv;
}
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
*v1 = v;
2012-08-12 12:48:27 +02:00
*v2 = ft_curckt ? ft_curckt->ci_vars : NULL;
2000-04-27 22:03:57 +02:00
}
/* Extract the .option lines from the deck */
struct line *
inp_getopts(struct line *deck)
{
struct line *last = NULL, *opts = NULL, *dd, *next = NULL;
for (dd = deck->li_next; dd; dd = next) {
next = dd->li_next;
if (ciprefix(".opt", dd->li_line)) {
inp_casefix(dd->li_line);
if (last)
last->li_next = dd->li_next;
else
deck->li_next = dd->li_next;
dd->li_next = opts;
opts = dd;
} else {
2000-04-27 22:03:57 +02:00
last = dd;
}
2000-04-27 22:03:57 +02:00
}
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
return (opts);
}
2012-08-12 12:43:09 +02:00
2010-07-14 22:59:23 +02:00
/* Extract the option lines from a comfile (spinit, .spiceinit) */
struct line *
inp_getoptsc(char *in_line, struct line *com_options)
{
struct line *next = NULL;
char *line;
2010-07-14 22:59:23 +02:00
/* option -> .options */
/* skip option */
gettok(&in_line);
2014-04-03 18:06:13 +02:00
line = tprintf(".options %s", in_line);
2012-08-12 12:43:09 +02:00
next = TMALLOC(struct line, 1);
2010-07-14 22:59:23 +02:00
next->li_line = line;
next->li_linenum = 0;
next->li_error = NULL;
next->li_actual = NULL;
2012-08-12 12:43:09 +02:00
2010-07-14 22:59:23 +02:00
/* put new line in front */
2012-08-12 12:48:27 +02:00
if (com_options)
2010-07-14 22:59:23 +02:00
next->li_next = com_options;
return next;
}
2000-04-27 22:03:57 +02:00
/* The one variable that we consider read-only so far is plots. The ones
* that are 'dontrecord' are curplottitle, curplotname, and curplotdate.
* Also things already in the plot env are 'dontrecord'.
*/
int
cp_usrset(struct variable *var, bool isset)
{
void *vv;
2000-04-27 22:03:57 +02:00
struct variable *tv;
int iv;
double dv;
bool bv;
if (eq(var->va_name, "debug")) {
if (var->va_type == CP_BOOL) {
2000-04-27 22:03:57 +02:00
cp_debug = ft_simdb = ft_parsedb = ft_evdb = ft_vecdb =
ft_grdb = ft_gidb = ft_controldb = isset;
} else if (var->va_type == CP_LIST) {
2000-04-27 22:03:57 +02:00
for (tv = var->va_vlist; tv; tv = tv->va_next)
2012-08-12 12:48:27 +02:00
if (var->va_type == CP_STRING)
2000-04-27 22:03:57 +02:00
setdb(tv->va_string);
2012-08-12 12:48:27 +02:00
else
fprintf(cp_err, "Error: bad type for debug var\n");
} else if (var->va_type == CP_STRING) {
2000-04-27 22:03:57 +02:00
setdb(var->va_string);
} else {
2000-04-27 22:03:57 +02:00
fprintf(cp_err, "Error: bad type for debug var\n");
}
2000-04-27 22:03:57 +02:00
#ifndef FTEDEBUG
fprintf(cp_err, "Warning: %s compiled without debug messages\n",
cp_program);
#endif
} else if (eq(var->va_name, "program")) {
cp_program = var->va_string;
2000-04-27 22:03:57 +02:00
} else if (eq(var->va_name, "rawfile")) {
ft_rawfile = copy(var->va_string);
} else if (eq(var->va_name, "acct")) {
ft_acctprint = isset;
2009-02-22 19:28:25 +01:00
} else if (eq(var->va_name, "noacct")) {
2012-08-12 12:43:09 +02:00
ft_noacctprint = isset;
} else if (eq(var->va_name, "ngdebug")) {
2012-08-12 12:43:09 +02:00
ft_ngdebug = isset;
2009-08-22 18:54:03 +02:00
} else if (eq(var->va_name, "noinit")) {
2012-08-12 12:43:09 +02:00
ft_noinitprint = isset;
2000-04-27 22:03:57 +02:00
} else if (eq(var->va_name, "list")) {
ft_listprint = isset;
} else if (eq(var->va_name, "nopage")) {
ft_nopage = isset;
} else if (eq(var->va_name, "nomod")) {
ft_nomod = isset;
} else if (eq(var->va_name, "node")) {
ft_nodesprint = isset;
} else if (eq(var->va_name, "opts")) {
ft_optsprint = isset;
} else if (eq(var->va_name, "strictnumparse")) {
ft_strictnumparse = isset;
} else if (eq(var->va_name, "strict_errorhandling")) {
ft_stricterror = isset;
2000-04-27 22:03:57 +02:00
} else if (eq(var->va_name, "rawfileprec")) {
if ((var->va_type == CP_BOOL) && (isset == FALSE))
2000-04-27 22:03:57 +02:00
raw_prec = -1;
else if (var->va_type == CP_REAL)
raw_prec = (int)floor(var->va_real + 0.5);
else if (var->va_type == CP_NUM)
2000-04-27 22:03:57 +02:00
raw_prec = var->va_num;
else
fprintf(cp_err, "Bad 'rawfileprec' \"%s\"\n", var->va_name);
} else if (eq(var->va_name, "numdgt")) {
if ((var->va_type == CP_BOOL) && (isset == FALSE))
2000-04-27 22:03:57 +02:00
cp_numdgt = -1;
else if (var->va_type == CP_REAL)
cp_numdgt = (int)floor(var->va_real + 0.5);
else if (var->va_type == CP_NUM)
2000-04-27 22:03:57 +02:00
cp_numdgt = var->va_num;
else
fprintf(cp_err, "Excuse me??\n");
} else if (eq(var->va_name, "unixcom")) {
cp_dounixcom = isset;
if (isset) {
2012-08-12 12:48:27 +02:00
char *s = getenv("PATH");
2000-04-27 22:03:57 +02:00
if (s)
cp_rehash(s, TRUE);
else
fprintf(cp_err, "Warning: no PATH in environment.\n");
}
} else if (eq(var->va_name, "units") && (var->va_type == CP_STRING)) {
2012-08-12 12:48:27 +02:00
if (isset && ((*var->va_string == 'd') || (*var->va_string == 'D')))
2000-04-27 22:03:57 +02:00
cx_degrees = TRUE;
else
cx_degrees = FALSE;
} else if (eq(var->va_name, "curplot")) {
if (var->va_type == CP_STRING)
2000-04-27 22:03:57 +02:00
plot_setcur(var->va_string);
else
fprintf(cp_err, "Error: plot name not a string\n");
return (US_DONTRECORD);
} else if (eq(var->va_name, "curplotname")) {
if (plot_cur && (var->va_type == CP_STRING))
2000-04-27 22:03:57 +02:00
plot_cur->pl_name = copy(var->va_string);
else
fprintf(cp_err, "Error: can't set plot name\n");
return (US_DONTRECORD);
} else if (eq(var->va_name, "curplottitle")) {
if (plot_cur && (var->va_type == CP_STRING))
2000-04-27 22:03:57 +02:00
plot_cur->pl_title = copy(var->va_string);
else
fprintf(cp_err, "Error: can't set plot title\n");
return (US_DONTRECORD);
} else if (eq(var->va_name, "curplotdate")) {
if (plot_cur && (var->va_type == CP_STRING))
2000-04-27 22:03:57 +02:00
plot_cur->pl_date = copy(var->va_string);
else
fprintf(cp_err, "Error: can't set plot date\n");
return (US_DONTRECORD);
} else if (eq(var->va_name, "plots")) {
return (US_READONLY);
}
if (plot_cur)
for (tv = plot_cur->pl_env; tv; tv = tv->va_next)
if (eq(tv->va_name, var->va_name))
return (US_READONLY);
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
/*
2012-08-12 12:43:09 +02:00
if (ft_curckt)
for (tv = ft_curckt->ci_vars; tv; tv = tv->va_next)
if (eq(tv->va_name, var->va_name))
return (US_READONLY);
2000-04-27 22:03:57 +02:00
*/
if (ft_nutmeg)
return (US_OK);
/* Now call the interface option routine. */
switch (var->va_type) {
2012-08-12 12:43:09 +02:00
case CP_BOOL:
2012-08-12 12:48:27 +02:00
bv = (var->va_bool) ? TRUE : FALSE;
vv = &bv;
2012-08-12 12:43:09 +02:00
break;
case CP_STRING:
vv = var->va_string;
break;
case CP_NUM:
iv = var->va_num;
vv = &iv;
break;
case CP_REAL:
dv = var->va_real;
vv = &dv;
break;
case CP_LIST:
/* if_option can't handle lists anyway. */
vv = NULL;
break;
default:
fprintf(cp_err,
2012-08-12 12:48:27 +02:00
"cp_usrset: Internal Error: Bad var type %d\n", var->va_type);
2012-08-12 12:43:09 +02:00
return (0);
2000-04-27 22:03:57 +02:00
}
if (ft_curckt) {
if (if_option(ft_curckt->ci_ckt, var->va_name, var->va_type, vv))
2012-08-12 12:43:09 +02:00
return US_SIMVAR;
2012-08-12 12:48:27 +02:00
} else {
if (if_option(NULL, var->va_name, var->va_type, vv))
return US_NOSIMVAR;
}
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
return (US_OK);
}
2012-08-12 12:43:09 +02:00
2000-04-27 22:03:57 +02:00
static void
setdb(char *str)
{
if (eq(str, "siminterface"))
ft_simdb = TRUE;
else if (eq(str, "cshpar"))
cp_debug = TRUE;
else if (eq(str, "parser"))
ft_parsedb = TRUE;
else if (eq(str, "eval"))
ft_evdb = TRUE;
else if (eq(str, "vecdb"))
ft_vecdb = TRUE;
else if (eq(str, "graf"))
ft_grdb = TRUE;
else if (eq(str, "ginterface"))
ft_gidb = TRUE;
else if (eq(str, "control"))
ft_controldb = TRUE;
else if (eq(str, "async"))
ft_asyncdb = TRUE;
else
fprintf(cp_err, "Warning: no such debug class %s\n", str);
}