1 /**********
2 Copyright 1990 Regents of the University of California. All rights reserved.
3 Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
4 ***********/
5
6 #include "ngspice/ngspice.h"
7 #include "ngspice/const.h"
8 #include "ngspice/cpdefs.h"
9 #include "ngspice/ftedefs.h"
10 #include "ngspice/dvec.h"
11 #include "ngspice/fteparse.h"
12 #include "ngspice/stringskip.h"
13 #include "cpitf.h"
14 #include "com_let.h"
15 #include "com_set.h"
16 #include "define.h"
17 #include "misccoms.h"
18
19 #include "terminal.h"
20
21 #include "completion.h"
22 #include "variable.h"
23
24 /* Set some standard variables and aliases, etc, and init the ccom stuff.
25 Called by fcn main() */
26
27 /* Macros to expand a macro to its value and then quote that value */
28 #undef stringit
29 #undef stringit2
30 #define stringit2(x) #x
31 #define stringit(x) stringit2(x)
32 void
ft_cpinit(void)33 ft_cpinit(void)
34 {
35 bool found = FALSE, t = TRUE;
36 char buf[BSIZE_SP], **x, *s, *r, *copys;
37 struct comm *c;
38 int i;
39 FILE *fp;
40
41 static char *predefs[] = {
42 "yes", "1",
43 "TRUE", "1",
44 "no", "0",
45 "FALSE", "0",
46 "pi", stringit(CONSTpi),
47 "e", stringit(CONSTnap),
48 "c", stringit(CONSTc),
49 "i", "0,1",
50 "kelvin", stringit(CONSTKtoC_for_str),
51 "echarge", stringit(CHARGE),
52 "boltz", stringit(CONSTboltz),
53 "planck", stringit(CONSTplanck)
54 };
55
56 static char *udfs[] = {
57 "max(x,y)", "(x gt y) * x + (x le y) * y",
58 "min(x,y)", "(x lt y) * x + (x ge y) * y",
59 "vdb(x)", "db(v(x))",
60 "vdb(x,y)", "db(v(x) - v(y))",
61 "vi(x)", "im(v(x))",
62 "vi(x,y)", "im(v(x) - v(y))",
63 "vm(x)", "mag(v(x))",
64 "vm(x,y)", "mag(v(x) - v(y))",
65 "vg(x)", "group_delay(v(x))", //A.Rroldan 10/06/05 group delay new function
66 "gd(x)", "group_delay(v(x))", //A.Rroldan 10/06/05 group delay new function
67 "vp(x)", "ph(v(x))",
68 "vp(x,y)", "ph(v(x) - v(y))",
69 "vr(x)", "re(v(x))",
70 "vr(x,y)", "re(v(x) - v(y))"
71 };
72 #ifndef SHARED_MODULE
73 /* if TIOCSTI is defined (not available in MS Windows:
74 Make escape the break character.
75 So the user can type ahead...
76 fcn defined in complete.c. */
77 cp_ccon(TRUE);
78 #endif
79 /* Initialize io, cp_chars[], variable "history" in init.c. */
80 cp_init();
81
82 /* If command completion is available (global variable cp_nocc
83 set in main.c by command line option -q) */
84 if (!cp_nocc) {
85
86 /* Add commands... */
87 for (c = cp_coms; c->co_func; c++) {
88 if (c->co_spiceonly && ft_nutmeg)
89 continue;
90 cp_addcomm(c->co_comname,
91 c->co_cctypes[0], c->co_cctypes[1],
92 c->co_cctypes[2], c->co_cctypes[3]);
93 cp_addkword(CT_COMMANDS, c->co_comname);
94 }
95
96 /* And keywords... These are the ones that are constant... */
97 if (!ft_nutmeg) {
98 cp_addkword(CT_LISTINGARGS, "deck");
99 cp_addkword(CT_LISTINGARGS, "logical");
100 cp_addkword(CT_LISTINGARGS, "physical");
101 cp_addkword(CT_LISTINGARGS, "expand");
102
103 cp_addkword(CT_STOPARGS, "when");
104 cp_addkword(CT_STOPARGS, "after");
105 }
106
107 cp_addkword(CT_PLOT, "new");
108
109 cp_addkword(CT_PLOTKEYWORDS, "xlimit");
110 cp_addkword(CT_PLOTKEYWORDS, "ylimit");
111 cp_addkword(CT_PLOTKEYWORDS, "vs");
112 cp_addkword(CT_PLOTKEYWORDS, "xindices");
113 cp_addkword(CT_PLOTKEYWORDS, "xcompress");
114 cp_addkword(CT_PLOTKEYWORDS, "xdelta");
115 cp_addkword(CT_PLOTKEYWORDS, "ydelta");
116 cp_addkword(CT_PLOTKEYWORDS, "lingrid");
117 cp_addkword(CT_PLOTKEYWORDS, "loglog");
118 cp_addkword(CT_PLOTKEYWORDS, "linear");
119 cp_addkword(CT_PLOTKEYWORDS, "xlog");
120 cp_addkword(CT_PLOTKEYWORDS, "ylog");
121 cp_addkword(CT_PLOTKEYWORDS, "polar");
122 cp_addkword(CT_PLOTKEYWORDS, "smith");
123 cp_addkword(CT_PLOTKEYWORDS, "smithgrid");
124 cp_addkword(CT_PLOTKEYWORDS, "nointerp");
125 cp_addkword(CT_PLOTKEYWORDS, "title");
126 cp_addkword(CT_PLOTKEYWORDS, "xlabel");
127 cp_addkword(CT_PLOTKEYWORDS, "ylabel");
128 cp_addkword(CT_PLOTKEYWORDS, "linplot");
129 cp_addkword(CT_PLOTKEYWORDS, "combplot");
130 cp_addkword(CT_PLOTKEYWORDS, "pointplot");
131
132 cp_addkword(CT_RUSEARGS, "time");
133 cp_addkword(CT_RUSEARGS, "space");
134 cp_addkword(CT_RUSEARGS, "faults");
135 cp_addkword(CT_RUSEARGS, "elapsed");
136 cp_addkword(CT_RUSEARGS, "totiter");
137 cp_addkword(CT_RUSEARGS, "traniter");
138 cp_addkword(CT_RUSEARGS, "tranpoints");
139 cp_addkword(CT_RUSEARGS, "accept");
140 cp_addkword(CT_RUSEARGS, "rejected");
141 cp_addkword(CT_RUSEARGS, "time");
142 cp_addkword(CT_RUSEARGS, "trantime");
143 cp_addkword(CT_RUSEARGS, "lutime");
144 cp_addkword(CT_RUSEARGS, "solvetime");
145 cp_addkword(CT_RUSEARGS, "transolvetime");
146 cp_addkword(CT_RUSEARGS, "loadtime");
147 cp_addkword(CT_RUSEARGS, "all");
148
149 cp_addkword(CT_VECTOR, "all");
150
151 for (x = ft_setkwords; *x; x++)
152 cp_addkword(CT_VARIABLES, *x);
153 for (i = 0; ; i++) {
154 if ((s = ft_typenames(i)) == NULL)
155 break;
156 cp_addkword(CT_TYPENAMES, s);
157 }
158 }
159
160 cp_vset("program", CP_STRING, cp_program);
161
162 /* Make the prompt use only the last component of the path... */
163
164 if (DIR_TERM) {
165 for (s = cp_program; s && *s; s++)
166 ;
167 s--;
168 while ((s > cp_program) && (*s != DIR_TERM))
169 s--;
170 if (*s == DIR_TERM)
171 s++;
172 (void) strcpy(buf, s);
173 for (s = buf; *s && (*s != '.'); s++)
174 ;
175 *s = '\0';
176 (void) strcat(buf, " ! -> ");
177
178 } else {
179 (void) sprintf(buf, "%s ! -> ", cp_program);
180 }
181
182 cp_vset("prompt", CP_STRING, buf);
183 cp_vset("noglob", CP_BOOL, &t);
184 cp_vset("brief", CP_BOOL, &t);
185
186 /* Make vectors from values in predefs[] for the current plot.
187 Define functions from entries in udfs[] (like user defined functions).
188 */
189
190 {
191 wordlist wl1, wl2, wl3;
192 wl1.wl_next = &wl2;
193 wl1.wl_prev = NULL;
194 wl2.wl_next = NULL;
195 wl2.wl_prev = &wl1;
196 wl1.wl_word = "if";
197 wl2.wl_word = "1";
198 cp_setalias("begin", &wl1);
199 wl1.wl_next = NULL;
200 wl1.wl_word = "end";
201 cp_setalias("endif", &wl1);
202 cp_setalias("endwhile", &wl1);
203 cp_setalias("endforeach", &wl1);
204 cp_setalias("endrepeat", &wl1);
205 cp_setalias("enddowhile", &wl1);
206 wl1.wl_word = "help";
207 cp_setalias("?", &wl1);
208
209 wl1.wl_next = &wl2;
210 wl2.wl_next = &wl3;
211 wl2.wl_prev = &wl1;
212 wl3.wl_prev = &wl2;
213 wl3.wl_next = NULL;
214 wl2.wl_word = "=";
215 for (i = 0; (size_t) i < NUMELEMS(predefs); i += 2) {
216 wl1.wl_word = predefs[i];
217 wl3.wl_word = predefs[i + 1];
218 com_let(&wl1);
219 }
220
221 wl2.wl_next = NULL;
222 for (i = 0; (size_t) i < NUMELEMS(udfs); i += 2) {
223 wl1.wl_word = udfs[i];
224 wl2.wl_word = udfs[i + 1];
225 com_define(&wl1);
226 }
227 }
228
229 /* set variables to read program configuration into special spinit for VS */
230 #ifdef _MSC_VER
231 #ifdef CONFIG64
232 #ifdef NGDEBUG
233 cp_vset("pg_config", CP_STRING, "d64");
234 #else
235 cp_vset("pg_config", CP_STRING, "r64");
236 #endif
237 #else
238 #ifdef NGDEBUG
239 cp_vset("pg_config", CP_STRING, "d32");
240 #else
241 cp_vset("pg_config", CP_STRING, "r32");
242 #endif
243 #endif
244 #endif
245
246 /* set a variable to announce windows console (to be used in unselecting plotting) */
247 #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(HAS_WINGUI)
248 cp_vset("win_console", CP_BOOL, &t);
249 #endif
250
251 /* NGSPICEDATADIR has been set to path "$dprefix/share/ngspice" in configure.ac,
252 Spice_Lib_Dir has been set to NGSPICEDATADIR in conf.c,
253 may be overridden by environmental variable SPICE_LIB_DIR in ivars().
254 Lib_Path has been set to Spice_Lib_Dir adding /scripts in ivars() */
255 if (Lib_Path && *Lib_Path) {
256 /* set variable 'sourcepath' */
257 if (Inp_Path && *Inp_Path)
258 (void) sprintf(buf, "sourcepath = ( %s %s %s )", DIR_CWD, Lib_Path, Inp_Path);
259 else
260 (void) sprintf(buf, "sourcepath = ( %s %s )", DIR_CWD, Lib_Path);
261
262 {
263 wordlist *wl;
264 wl = cp_doglob(cp_lexer(buf));
265 com_set(wl);
266 wl_free(wl);
267 }
268
269 /* Now source the standard startup file spinit or tclspinit. */
270
271 /* jump over leading spaces */
272 for (copys = s = cp_tildexpand(Lib_Path); copys && *copys; ) {
273 s = skip_ws(s);
274 /* copy s into buf until end of s, r is the actual position in buf */
275 int ii;
276 for (r = buf, ii = 0; *s; r++, s++, ii++) {
277 *r = *s;
278 if (ii > 500) {
279 fprintf(stderr, "Warning: spinit path is too long.\n");
280 break;
281 }
282 }
283 tfree(copys);
284 /* add a path separator to buf at actual position */
285 (void) strcpy(r, DIR_PATHSEP);
286 #ifdef TCL_MODULE
287 /* add "tclspinit" to buf after actual position */
288 (void) strcat(r, "tclspinit");
289 #else
290 /* add "spinit" to buf after actual position */
291 (void) strcat(r, "spinit");
292 #endif
293
294 if ((fp = fopen(buf, "r")) != NULL) {
295
296 cp_interactive = FALSE;
297 inp_spsource(fp, TRUE, buf, FALSE);
298 cp_interactive = TRUE;
299 found = TRUE;
300 break;
301
302 #if defined(HAS_WINGUI) || defined(__MINGW32__) || defined(_MSC_VER)
303 /* search in local directory where ngspice.exe resides */
304 #if defined TCL_MODULE
305 } else if ((fp = fopen("./tclspinit", "r")) != NULL) {
306 #else
307 } else if ((fp = fopen("./spinit", "r")) != NULL) {
308 #endif
309 cp_interactive = FALSE;
310 inp_spsource(fp, TRUE, buf, FALSE);
311 cp_interactive = TRUE;
312 found = TRUE;
313 break;
314 #endif
315 } else if (ft_controldb) {
316 fprintf(cp_err, "Note: can't open \"%s\".\n", buf);
317 }
318 }
319
320 if (!found)
321 fprintf(cp_err, "Note: can't find init file.\n");
322 }
323
324 tcap_init();
325 }
326
327
328 /* Decide whether a condition is TRUE or not.
329 * In erroneous situations the condition shall evaluate to FALSE
330 * additionally error messages might have been printed.
331 */
332
333 bool
cp_istrue(wordlist * wl)334 cp_istrue(wordlist *wl)
335 {
336 struct dvec *v;
337 struct pnode *names;
338 bool rv;
339
340 /* First do all the csh-type stuff here... */
341 wl = wl_copy(wl);
342 wl = cp_variablesubst(wl);
343 /* return FALSE if this did not expand to anything */
344 if (!wl)
345 return FALSE;
346 /* backquote '`' substitution */
347 wl = cp_bquote(wl);
348
349 /* parse the condition */
350 names = ft_getpnames(wl, TRUE);
351 wl_free(wl);
352
353 /* evaluate the parsed condition.
354 * boolean expressions evaluate to real 1.0 or 0.0 */
355 v = ft_evaluate(names);
356
357 /* non boolean expressions will be considered TRUE
358 * if at least one real or complex vector element is non zero */
359 rv = !vec_iszero(v);
360
361 /* va: garbage collection for v, if pnode names is no simple value */
362 if (names && !names->pn_value && v)
363 vec_free(v);
364 free_pnode(names); /* free also v, if pnode names is simple value */
365
366 return rv;
367 }
368
369
370 /* This gets called before every command is executed...
371 from fcns do_command() or do_block() in control.c */
372
373 void
cp_periodic(void)374 cp_periodic(void)
375 {
376 ft_setflag = FALSE;
377 ft_intrpt = FALSE;
378 ft_ckspace(); /* check for max. data size (resource.c) */
379 ft_checkkids(); /* check for jobs (only if OK_ASPICE is defined, apsice.c) */
380 vec_gc(); /* remove vectors which do not have permanent flag set (vectors.c) */
381 }
382
383
384 void
cp_doquit(void)385 cp_doquit(void)
386 {
387 com_quit(NULL);
388 }
389
390
391 /* This is how we deal with emulation of commands by scripts... If the script
392 * is found, then set the variables argc and argv and call the script. Note
393 * that this also allows us to type a filename to load a spice deck...
394 */
395
396 bool
cp_oddcomm(char * s,wordlist * wl)397 cp_oddcomm(char *s, wordlist *wl)
398 {
399 FILE *fp;
400
401 if ((fp = inp_pathopen(s, "r")) != NULL) {
402 /* Buffer for building string, unless unusually long */
403 char buf[BSIZE_SP];
404 char *p_buf_active; /* buffer in use */
405 static const char header[] = "argc = %d argv = ( ";
406
407 /* Bound on initial length: Header - 2 for %d - 1 for null +
408 * + 2 for closing ')' and '\0' + bound on length of int
409 * as string */
410 size_t n_byte_data =
411 sizeof header / sizeof *header + 3 * sizeof(int) - 1;
412
413 (void) fclose(fp);
414
415 /* Step through word list finding length */
416 {
417 wordlist *wl1;
418 for (wl1 = wl; wl1 != (wordlist *) NULL; wl1 = wl1->wl_next) {
419 n_byte_data += strlen(wl1->wl_word) + 1;
420 }
421 }
422
423 /* Use fixed buffer unless it is too small */
424 if (n_byte_data <= sizeof buf / sizeof *buf) {
425 p_buf_active = buf;
426 }
427 else {
428 p_buf_active = TMALLOC(char, n_byte_data);
429 }
430
431 /* Step through word list again to build string */
432 {
433 char *p_dst = p_buf_active;
434 p_dst += sprintf(p_dst, header, wl_length(wl));
435 for ( ; wl != (wordlist *) NULL; wl = wl->wl_next) {
436 const char *p_src = wl->wl_word;
437 for ( ; ; p_src++) { /* copy source string */
438 const char ch_src = *p_src;
439 if (ch_src == '\0') {
440 *p_dst++ = ' ';
441 break;
442 }
443 *p_dst++ = ch_src;
444 } /* end of loop copying source string */
445 } /* end of loop over words in list */
446
447 /* Add ')' and terminate string */
448 *p_dst++ = ')';
449 *p_dst = '\0';
450 } /* end of block building string */
451
452 wordlist *setarg = cp_lexer(p_buf_active);
453
454 /* Free buffer allocation if made */
455 if (p_buf_active != buf) {
456 txfree(p_buf_active);
457 }
458
459 com_set(setarg);
460 wl_free(setarg);
461 inp_source(s);
462 cp_remvar("argc");
463 cp_remvar("argv");
464 return TRUE;
465 }
466
467 if (wl && eq(wl->wl_word, "=")) {
468 wordlist *ww = wl_cons(copy(s), wl);
469 com_let(ww);
470 wl_delete_slice(ww, ww->wl_next);
471 return (TRUE);
472 }
473
474 return (FALSE);
475 } /* end of function cp_oddcomm */
476
477
478
479