Jspice3
cpitf.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1985 Wayne A. Christopher
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 #include "spice.h"
9 #include "ftedefs.h"
10 
11 #ifdef __STDC__
12 static bool testv(struct dvec*);
13 #else
14 static bool testv();
15 #endif
16 
17 
18 /* Set some standard variables and aliases, etc, and init the ccom stuff. */
19 
20 void
22 {
23  wordlist *wl;
24  wordlist wl1, wl2, wl3;
25  bool found = false, t = true;
26  char buf[BSIZE_SP], **x, *s, *r;
27  struct comm *c;
28  int i;
29  FILE *fp;
30  double dv;
31  static char *predefs[] = {
32  "yes", "1",
33  "true", "1",
34  "no", "0",
35  "false", "0",
36  "pi", "3.14159265358979323846",
37  "e", "2.71828182844590452353",
38  "c", "2.997925e8",
39  "i", "0,1",
40  "kelvin", "-273.15",
41  "echarge", "1.60219e-19",
42  "boltz", "1.38062e-23",
43  "planck", "6.62620e-34"
44  } ;
45  static char *udfs[] = {
46  "max(x,y)", "(x gt y) * x + (x le y) * y",
47  "min(x,y)", "(x lt y) * x + (x ge y) * y",
48  "vdb(x)", "db(v(x))",
49  "vdb(x,y)", "db(v(x) - v(y))",
50  "vi(x)", "im(v(x))",
51  "vi(x,y)", "im(v(x) - v(y))",
52  "vm(x)", "mag(v(x))",
53  "vm(x,y)", "mag(v(x) - v(y))",
54  "vp(x)", "ph(v(x))",
55  "vp(x,y)", "ph(v(x) - v(y))",
56  "vr(x)", "re(v(x))",
57  "vr(x,y)", "re(v(x) - v(y))"
58  } ;
59 
60  cp_ccon(true); /* So the user can type ahead... */
61  cp_init();
62 
63  if (!cp_nocc) {
64  /* Add commands... */
65  for (c = cp_coms; c->co_func; c++) {
66  if (c->co_spiceonly && ft_nutmeg)
67  continue;
69  c->co_cctypes[1], c->co_cctypes[2],
70  c->co_cctypes[3]);
72  }
73  /* And keywords... These are the ones that are constant... */
74  if (!ft_nutmeg) {
75  cp_addkword(CT_LISTINGARGS, "deck");
76  cp_addkword(CT_LISTINGARGS, "logical");
77  cp_addkword(CT_LISTINGARGS, "physical");
78  cp_addkword(CT_LISTINGARGS, "expand");
79 
80  cp_addkword(CT_STOPARGS, "when");
81  cp_addkword(CT_STOPARGS, "after");
82  }
83 
84  cp_addkword(CT_PLOT, "new");
85 
86  cp_addkword(CT_PLOTKEYWORDS, "combplot");
87  cp_addkword(CT_PLOTKEYWORDS, "lingrid");
88  cp_addkword(CT_PLOTKEYWORDS, "linplot");
89  cp_addkword(CT_PLOTKEYWORDS, "loglog");
90  cp_addkword(CT_PLOTKEYWORDS, "nogrid");
91  cp_addkword(CT_PLOTKEYWORDS, "nointerp");
92  cp_addkword(CT_PLOTKEYWORDS, "pointplot");
93  cp_addkword(CT_PLOTKEYWORDS, "polar");
94  cp_addkword(CT_PLOTKEYWORDS, "samep");
95  cp_addkword(CT_PLOTKEYWORDS, "smith");
96  cp_addkword(CT_PLOTKEYWORDS, "title");
98  cp_addkword(CT_PLOTKEYWORDS, "xcomp");
99  cp_addkword(CT_PLOTKEYWORDS, "xcompress");
100  cp_addkword(CT_PLOTKEYWORDS, "xdel");
101  cp_addkword(CT_PLOTKEYWORDS, "xdelta");
102  cp_addkword(CT_PLOTKEYWORDS, "xind");
103  cp_addkword(CT_PLOTKEYWORDS, "xindices");
104  cp_addkword(CT_PLOTKEYWORDS, "xlabel");
105  cp_addkword(CT_PLOTKEYWORDS, "xlimit");
106  cp_addkword(CT_PLOTKEYWORDS, "xlog");
107  cp_addkword(CT_PLOTKEYWORDS, "ydel");
108  cp_addkword(CT_PLOTKEYWORDS, "ydelta");
109  cp_addkword(CT_PLOTKEYWORDS, "ylabel");
110  cp_addkword(CT_PLOTKEYWORDS, "ylimit");
111  cp_addkword(CT_PLOTKEYWORDS, "ylog");
112 
113  cp_addkword(CT_RUSEARGS, "accept");
114  cp_addkword(CT_RUSEARGS, "all");
115  cp_addkword(CT_RUSEARGS, "elapsed");
116  cp_addkword(CT_RUSEARGS, "everything");
117  cp_addkword(CT_RUSEARGS, "faults");
118  cp_addkword(CT_RUSEARGS, "fillin");
119  cp_addkword(CT_RUSEARGS, "loadtime");
120  cp_addkword(CT_RUSEARGS, "lutime");
121  cp_addkword(CT_RUSEARGS, "matsize");
122  cp_addkword(CT_RUSEARGS, "nonzero");
123  cp_addkword(CT_RUSEARGS, "rejected");
124  cp_addkword(CT_RUSEARGS, "solvetime");
125  cp_addkword(CT_RUSEARGS, "space");
126  cp_addkword(CT_RUSEARGS, "time");
127  cp_addkword(CT_RUSEARGS, "totaltime");
128  cp_addkword(CT_RUSEARGS, "totiter");
129  cp_addkword(CT_RUSEARGS, "traniter");
130  cp_addkword(CT_RUSEARGS, "tranpoints");
131  cp_addkword(CT_RUSEARGS, "transolvetime");
132  cp_addkword(CT_RUSEARGS, "trantime");
133 
134  cp_addkword(CT_VECTOR, "all");
135 
136  for (i = 0; ; i++) {
137  if (!(s = ft_typenames(i)))
138  break;
140  }
141  }
142 
143  cp_vset("program", VT_STRING, cp_program);
144 
145  /* Make the prompt use only the last component of the path... */
146 
147  if (DIR_TERM) {
148  for (s = cp_program; s && *s; s++)
149  ;
150  s--;
151  while ((s > cp_program) && (*s != DIR_TERM))
152  s--;
153  if (*s == DIR_TERM)
154  s++;
155  (void) strcpy(buf, s);
156  for (s = buf; *s && (*s != '.'); s++)
157  ;
158  *s = '\0';
159  (void) strcat(buf, " ! -> ");
160  }
161  else
162  (void) sprintf(buf, "%s ! -> ", cp_program);
163 
164  cp_vset("prompt", VT_STRING, buf);
165  cp_vset("noglob", VT_BOOL, (char *) &t);
166 
167  /* set these to a default (used in decks for loop and check
168  * commands)
169  */
170  dv = 0.0;
171  cp_vset("value1", VT_REAL, (char*)&dv);
172  cp_vset("value2", VT_REAL, (char*)&dv);
173 
174  /* Now do a bunch of things that used to be in the spiceinit file
175  * but were too slow to read in...
176  */
177  wl1.wl_next = &wl2;
178  wl1.wl_prev = NULL;
179  wl2.wl_next = NULL;
180  wl2.wl_prev = &wl1;
181  wl1.wl_word = "if";
182  wl2.wl_word = "1";
183  cp_setalias("begin", &wl1);
184  wl1.wl_next = NULL;
185  wl1.wl_word = "end";
186  cp_setalias("endif", &wl1);
187  cp_setalias("endwhile", &wl1);
188  cp_setalias("endforeach", &wl1);
189  cp_setalias("endrepeat", &wl1);
190  cp_setalias("enddowhile", &wl1);
191  wl1.wl_word = "help";
192  cp_setalias("?", &wl1);
193 
194  wl1.wl_next = &wl2;
195  wl2.wl_next = &wl3;
196  wl2.wl_prev = &wl1;
197  wl3.wl_prev = &wl2;
198  wl3.wl_next = NULL;
199  wl2.wl_word = "=";
200  for (i = 0; i < sizeof (predefs) / sizeof (char *); i += 2) {
201  wl1.wl_word = predefs[i];
202  wl3.wl_word = predefs[i + 1];
203  com_let(&wl1);
204  }
205 
206  wl2.wl_next = NULL;
207  for (i = 0; i < sizeof (udfs) / sizeof (char *); i += 2) {
208  wl1.wl_word = udfs[i];
209  wl2.wl_word = udfs[i + 1];
210  com_define(&wl1);
211  }
212 
213  /* Reset this for the front end. */
214  cp_hash = '*';
215 
216  if (Lib_Path && *Lib_Path) {
217  (void) sprintf(buf, "sourcepath = ( %s %s )", DIR_CWD, Lib_Path);
218  wl = cp_lexer(buf);
219  cp_doglob(&wl);
220  cp_striplist(wl);
221  com_set(wl);
222  wl_free(wl);
223 
224  /* Now source the standard startup file. */
225  s = Lib_Path;
226  while (*s) {
227  while (isspace(*s))
228  s++;
229  for (r = buf; *s && !isspace(*s); r++, s++)
230  *r = *s;
231  *r++ = '/';
232  (void) strcpy(r, "spinit");
233  cp_pathfix(buf);
234  if (fp = fopen(buf, "r")) {
235  cp_interactive = false;
236  inp_spsource(fp, true, buf);
237  cp_interactive = true;
238  (void) fclose(fp);
239  found = true;
240  break;
241  }
242  else if (ft_controldb)
243  fprintf(cp_err, "Note: can't open \"%s\".\n", buf);
244  }
245  if (!found)
246  fprintf(cp_err, "Note: can't find init file.\n");
247  }
248 
249  return;
250 }
251 
252 
253 /* Decide whether a condition is true or not. */
254 
255 bool
257 
258 wordlist *wl;
259 {
260  struct dvec *v;
261  struct pnode *pn;
262  struct dvlist *dl;
263 
264  /* First do all the csh-type stuff here... */
265  wl = wl_copy(wl);
266  cp_variablesubst(&wl);
267  cp_bquote(&wl);
268  cp_striplist(wl);
269 
270  pn = ft_getpnames(wl, true);
271  wl_free(wl);
272  if (pn == NULL)
273  return (false);
274  v = ft_evaluate(pn);
275  inp_pnfree(pn);
276 
277  if (v->v_link2) {
278  /* It makes no sense to say while (all), but what the heck... */
279  for (dl = v->v_link2; dl; dl = dl->dl_next) {
280  if (testv(dl->dl_dvec))
281  return (true);
282  }
283  }
284  else {
285  if (testv(v))
286  return (true);
287  }
288  return (false);
289 }
290 
291 
292 static bool
294 
295 struct dvec *v;
296 {
297  int i;
298 
299  if (isreal(v)) {
300  for (i = 0; i < v->v_length; i++)
301  if (v->v_realdata[i] != 0.0) {
302  return (true);
303  }
304  }
305  else {
306  for (i = 0; i < v->v_length; i++)
307  if ((realpart(&v->v_compdata[i]) != 0.0) ||
308  (imagpart(&v->v_compdata[i]) != 0.0)) {
309  return (true);
310  }
311  }
312  return (false);
313 }
314 
315 /* This gets called before every command is executed... */
316 
317 void
319 {
320  ft_setflag = ft_intrpt = false;
321  ft_ckspace();
322  ft_checkkids();
323  vec_gc();
324  return;
325 }
326 
327 
328 void
330 {
331  com_quit((wordlist *) NULL);
332  return;
333 }
334 
335 
336 /* This is how we deal with emulation of commands by scripts... If the script
337  * is found, then set the variables argc and argv and call the script. Note
338  * that this also allows us to type a filename to load a spice deck...
339  */
340 
341 bool
343 
344 /* s is just wl->wl_word */
345 char *s;
346 wordlist *wl;
347 {
348  FILE *fp;
349 
350  if (fp = inp_pathopen(s, "r")) {
351  (void) fclose(fp);
352  cp_pusharg(wl);
353  inp_source(s);
354  cp_poparg();
355  return (true);
356  }
357  if (strchr(wl->wl_word,'=') ||
358  (wl->wl_next && *wl->wl_next->wl_word == '=')) {
359  com_let(wl);
360  return (true);
361  }
362  return (false);
363 }
364 
365 
366 bool
368 
369 char *s;
370 {
371  FILE *fp;
372  char buf[128];
373 
374  if (fp = inp_pathopen(s, "r")) {
375  (void) fclose(fp);
376  strcpy(buf,s);
377  strcat(buf," -n");
378  inp_source(buf);
379  return (true);
380  }
381  return (false);
382 }
383 
384 
385 void
386 cp_bind(name)
387 
388 char *name;
389 {
390  if (ft_curckt) {
391  if (name)
392  ft_curckt->ci_contblk = copy(name);
393  else
395  }
396  else {
397  if (!ft_nutmeg)
398  (void)fprintf(cp_err,"Error: no current circuit.\n");
399  }
400 }
401 
402 
403 void
405 
406 char *name;
407 {
408  struct circ *ct;
409 
410  for (ct = ft_circuits; ct; ct = ct->ci_next) {
411  if (ct->ci_contblk && eq(ct->ci_contblk,name))
412  tfree(ct->ci_contblk);
413  }
414 }
void cp_pathfix(char *buf)
Definition: help.c:198
void cp_init()
Definition: cshpar.c:83
struct dvec * ft_evaluate()
bool ft_intrpt
Definition: main.c:47
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
#define eq(a, b)
Definition: misc.h:29
struct circ * ci_next
Definition: ftedefs.h:37
void cp_periodic()
Definition: cpitf.c:318
void cp_vset()
struct dvlist * v_link2
Definition: ftedata.h:44
char * strcpy()
Definition: ftedefs.h:25
Definition: cddefs.h:119
#define CT_RUSEARGS
Definition: fteconst.h:92
void com_define()
Definition: ftedata.h:49
char cp_hash
Definition: lexical.c:54
struct pnode * ft_getpnames()
void vec_gc()
Definition: vectors.c:681
#define CT_PLOT
Definition: fteconst.h:90
void cp_setalias()
void cp_bquote()
#define CT_STOPARGS
Definition: fteconst.h:93
#define CT_LISTINGARGS
Definition: fteconst.h:88
Definition: library.c:18
char * ci_contblk
Definition: ftedefs.h:29
void cp_doglob()
void com_set()
struct comm * cp_coms
Definition: main.c:163
Definition: cpdefs.h:20
static bool testv()
#define CT_TYPENAMES
Definition: fteconst.h:97
bool ft_setflag
Definition: main.c:48
char * copy()
void com_quit()
void cp_poparg()
Definition: variable.c:1500
bool co_spiceonly
Definition: cpdefs.h:24
struct dvlist * dl_next
Definition: ftedata.h:51
void wl_free()
void inp_pnfree()
void inp_spsource(FILE *fp, bool i, char *s)
Definition: main.c:195
FILE * cp_err
Definition: help.c:101
bool ft_controldb
Definition: options.c:30
void ft_cpinit()
Definition: cpitf.c:21
struct circ * ft_circuits
Definition: main.c:185
struct wordlist * wl_prev
Definition: cpstd.h:24
char * Lib_Path
Definition: ivars.c:15
void cp_pusharg()
void inp_source(char *s)
Definition: main.c:203
bool cp_interactive
Definition: help.c:100
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
struct circ * ft_curckt
Definition: main.c:184
FILE * inp_pathopen()
char * cp_program
Definition: main.c:43
static double c
Definition: vectors.c:16
void ft_checkkids()
Definition: aspice.c:449
#define VT_STRING
Definition: cpstd.h:63
bool cp_oddcomm(char *s, wordlist *wl)
Definition: cpitf.c:342
#define isreal(v)
Definition: ftedata.h:54
Definition: ftedata.h:24
struct dvec * dl_dvec
Definition: ftedata.h:50
wordlist * cp_lexer()
#define imagpart(cval)
Definition: cpstd.h:36
#define VT_BOOL
Definition: cpstd.h:60
char * ft_typenames()
void cp_unbind(char *name)
Definition: cpitf.c:404
Definition: cpstd.h:21
bool cp_istrue(wordlist *wl)
Definition: cpitf.c:256
void cp_variablesubst()
bool cp_nocc
Definition: complete.c:75
bool cp_block(char *s)
Definition: cpitf.c:367
Definition: cddefs.h:162
void cp_bind(char *name)
Definition: cpitf.c:386
void cp_doquit()
Definition: cpitf.c:329
void ft_ckspace()
Definition: resource.c:91
struct wordlist * wl_next
Definition: cpstd.h:23
#define VT_REAL
Definition: cpstd.h:62
bool ft_nutmeg
Definition: main.c:161
void cp_striplist()
char * wl_word
Definition: cpstd.h:22
#define CT_COMMANDS
Definition: fteconst.h:85
void(* co_func)()
Definition: cpdefs.h:22
void cp_addcomm()
void cp_addkword()
char * co_comname
Definition: cpdefs.h:21
Definition: cddefs.h:192
void com_let()
#define CT_PLOTKEYWORDS
Definition: fteconst.h:91
long co_cctypes[4]
Definition: cpdefs.h:26
void cp_ccon()
#define realpart(cval)
Definition: cpstd.h:35
Definition: fteparse.h:16
#define CT_VECTOR
Definition: fteconst.h:96
wordlist * wl_copy()