2000-04-27 22:03:57 +02:00
|
|
|
/**********
|
|
|
|
|
Copyright 1990 Regents of the University of California. All rights reserved.
|
|
|
|
|
Author: 1985 Wayne A. Christopher
|
|
|
|
|
**********/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* For dealing with nutmeg input decks and command scripts
|
|
|
|
|
*/
|
|
|
|
|
|
2011-12-11 19:05:00 +01:00
|
|
|
#include "ngspice/ngspice.h"
|
|
|
|
|
#include "ngspice/cpdefs.h"
|
|
|
|
|
#include "ngspice/ftedefs.h"
|
|
|
|
|
#include "ngspice/dvec.h"
|
|
|
|
|
#include "ngspice/fteinp.h"
|
2000-06-27 18:09:02 +02:00
|
|
|
#include "nutinp.h"
|
|
|
|
|
#include "variable.h"
|
2010-10-15 20:11:11 +02:00
|
|
|
#include "../misc/mktemp.h"
|
2010-10-16 17:56:07 +02:00
|
|
|
#include "subckt.h"
|
2000-04-27 22:03:57 +02:00
|
|
|
|
|
|
|
|
/* The routine to source a spice input deck. We read the deck in, take out
|
|
|
|
|
* the front-end commands, and create a CKT structure. Also we filter out
|
|
|
|
|
* the following lines: .save, .width, .four, .print, and .plot, to perform
|
|
|
|
|
* after the run is over.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
inp_nutsource(FILE *fp, bool comfile, char *filename)
|
|
|
|
|
{
|
|
|
|
|
struct line *deck, *dd, *ld;
|
2001-02-09 20:46:36 +01:00
|
|
|
struct line *realdeck, *options = NULL;
|
|
|
|
|
char *tt = NULL, name[BSIZE_SP], *s, *t;
|
2010-07-20 20:41:25 +02:00
|
|
|
bool commands = FALSE;
|
2000-04-27 22:03:57 +02:00
|
|
|
wordlist *wl = NULL, *end = NULL;
|
|
|
|
|
wordlist *controls = NULL;
|
|
|
|
|
FILE *lastin, *lastout, *lasterr;
|
|
|
|
|
|
2010-06-29 23:18:34 +02:00
|
|
|
inp_readall(fp, &deck, 0, NULL, comfile) /* still to check if . or filename instead of NULL */;
|
2000-04-27 22:03:57 +02:00
|
|
|
if (!deck)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
realdeck = inp_deckcopy(deck);
|
|
|
|
|
|
|
|
|
|
if (!comfile) {
|
|
|
|
|
/* Save the title before INPgetTitle gets it. */
|
|
|
|
|
tt = copy(deck->li_line);
|
|
|
|
|
if (!deck->li_next)
|
|
|
|
|
fprintf(cp_err, "Warning: no lines in deck...\n");
|
|
|
|
|
}
|
|
|
|
|
(void) fclose(fp);
|
|
|
|
|
|
|
|
|
|
/* Now save the IO context and start a new control set... After
|
|
|
|
|
* we are done with the source we'll put the old file descriptors
|
|
|
|
|
* back. I guess we could use a FILE stack, but since this routine
|
|
|
|
|
* is recursive anyway...
|
|
|
|
|
*/
|
|
|
|
|
lastin = cp_curin;
|
|
|
|
|
lastout = cp_curout;
|
|
|
|
|
lasterr = cp_curerr;
|
|
|
|
|
cp_curin = cp_in;
|
|
|
|
|
cp_curout = cp_out;
|
|
|
|
|
cp_curerr = cp_err;
|
|
|
|
|
|
|
|
|
|
cp_pushcontrol();
|
|
|
|
|
|
|
|
|
|
/* We should now go through the deck and execute front-end
|
|
|
|
|
* commands and remove them. Front-end commands are enclosed by
|
|
|
|
|
* the lines .control and .endc, unless comfile
|
|
|
|
|
* is TRUE, in which case every line must be a front-end command.
|
|
|
|
|
* There are too many problems with matching the first word on
|
|
|
|
|
* the line.
|
|
|
|
|
*/
|
|
|
|
|
ld = deck;
|
|
|
|
|
if (comfile) {
|
|
|
|
|
/* This is easy. */
|
|
|
|
|
for (dd = deck; dd; dd = ld) {
|
|
|
|
|
ld = dd->li_next;
|
|
|
|
|
if ((dd->li_line[0] == '*') && (dd->li_line[1] != '#'))
|
|
|
|
|
continue;
|
|
|
|
|
if (!ciprefix(".control", dd->li_line) &&
|
|
|
|
|
!ciprefix(".endc", dd->li_line)) {
|
|
|
|
|
if (dd->li_line[0] == '*')
|
|
|
|
|
(void) cp_evloop(dd->li_line + 2);
|
|
|
|
|
else
|
|
|
|
|
(void) cp_evloop(dd->li_line);
|
|
|
|
|
}
|
|
|
|
|
tfree(dd->li_line);
|
|
|
|
|
tfree(dd);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (dd = deck->li_next; dd; dd = ld->li_next) {
|
|
|
|
|
if ((dd->li_line[0] == '*') &&
|
|
|
|
|
(dd->li_line[1] != '#')) {
|
|
|
|
|
ld = dd;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
(void) strncpy(name, dd->li_line, BSIZE_SP);
|
|
|
|
|
for (s = name; *s && isspace(*s); s++)
|
|
|
|
|
;
|
|
|
|
|
for (t = s; *t && !isspace(*t); t++)
|
|
|
|
|
;
|
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
|
|
if (ciprefix(".control", dd->li_line)) {
|
|
|
|
|
ld->li_next = dd->li_next;
|
|
|
|
|
tfree(dd->li_line);
|
|
|
|
|
tfree(dd);
|
|
|
|
|
if (commands)
|
|
|
|
|
fprintf(cp_err,
|
|
|
|
|
"Warning: redundant .control line\n");
|
|
|
|
|
else
|
|
|
|
|
commands = TRUE;
|
|
|
|
|
} else if (ciprefix(".endc", dd->li_line)) {
|
|
|
|
|
ld->li_next = dd->li_next;
|
|
|
|
|
tfree(dd->li_line);
|
|
|
|
|
tfree(dd);
|
|
|
|
|
if (commands)
|
|
|
|
|
commands = FALSE;
|
|
|
|
|
else
|
|
|
|
|
fprintf(cp_err,
|
|
|
|
|
"Warning: misplaced .endc line\n");
|
|
|
|
|
} else if (commands || prefix("*#", dd->li_line)) {
|
2012-07-14 10:23:51 +02:00
|
|
|
controls = wl_cons(NULL, controls);
|
|
|
|
|
wl = controls;
|
2000-04-27 22:03:57 +02:00
|
|
|
if (prefix("*#", dd->li_line))
|
|
|
|
|
wl->wl_word = copy(dd->li_line + 2);
|
|
|
|
|
else
|
|
|
|
|
wl->wl_word = dd->li_line;
|
|
|
|
|
ld->li_next = dd->li_next;
|
|
|
|
|
tfree(dd);
|
|
|
|
|
} else if (!*dd->li_line) {
|
|
|
|
|
/* So blank lines in com files don't get
|
|
|
|
|
* considered as circuits.
|
|
|
|
|
*/
|
|
|
|
|
ld->li_next = dd->li_next;
|
|
|
|
|
tfree(dd->li_line);
|
|
|
|
|
tfree(dd);
|
|
|
|
|
} else {
|
|
|
|
|
inp_casefix(s);
|
|
|
|
|
inp_casefix(dd->li_line);
|
|
|
|
|
if (eq(s, ".width") || ciprefix(".four", s) ||
|
|
|
|
|
eq(s, ".plot") ||
|
|
|
|
|
eq(s, ".print") ||
|
|
|
|
|
eq(s, ".save")) {
|
2012-07-14 10:23:51 +02:00
|
|
|
wl_append_word(&wl, &end, copy(dd->li_line));
|
2000-04-27 22:03:57 +02:00
|
|
|
ld->li_next = dd->li_next;
|
|
|
|
|
tfree(dd->li_line);
|
|
|
|
|
tfree(dd);
|
|
|
|
|
} else
|
|
|
|
|
ld = dd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (deck->li_next) {
|
|
|
|
|
/* There is something left after the controls. */
|
|
|
|
|
fprintf(cp_out, "\nCircuit: %s\n\n", tt);
|
2007-10-08 17:36:56 +02:00
|
|
|
fprintf(stderr, "\nCircuit: %s\n\n", tt);
|
2000-04-27 22:03:57 +02:00
|
|
|
|
|
|
|
|
/* Now expand subcircuit macros. Note that we have to
|
|
|
|
|
* fix the case before we do this but after we
|
|
|
|
|
* deal with the commands.
|
|
|
|
|
*/
|
2010-07-20 20:41:25 +02:00
|
|
|
if (!cp_getvar("nosubckt", CP_BOOL, NULL))
|
2000-04-27 22:03:57 +02:00
|
|
|
deck->li_next = inp_subcktexpand(deck->
|
|
|
|
|
li_next);
|
|
|
|
|
deck->li_actual = realdeck;
|
|
|
|
|
nutinp_dodeck(deck, tt, wl, FALSE, options, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now that the deck is loaded, do the commands... */
|
2012-07-14 12:39:13 +02:00
|
|
|
controls = wl_reverse(controls);
|
|
|
|
|
for (wl = controls; wl; wl = wl->wl_next)
|
|
|
|
|
(void) cp_evloop(wl->wl_word);
|
|
|
|
|
wl_free(controls);
|
2000-04-27 22:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now reset everything. Pop the control stack, and fix up the IO
|
|
|
|
|
* as it was before the source.
|
|
|
|
|
*/
|
|
|
|
|
cp_popcontrol();
|
|
|
|
|
|
|
|
|
|
cp_curin = lastin;
|
|
|
|
|
cp_curout = lastout;
|
|
|
|
|
cp_curerr = lasterr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nutcom_source(wordlist *wl)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp, *tp;
|
|
|
|
|
char buf[BSIZE_SP];
|
|
|
|
|
bool inter;
|
|
|
|
|
char *tempfile = NULL;
|
|
|
|
|
wordlist *owl = wl;
|
2010-11-04 20:15:41 +01:00
|
|
|
size_t n;
|
2000-04-27 22:03:57 +02:00
|
|
|
|
|
|
|
|
inter = cp_interactive;
|
|
|
|
|
cp_interactive = FALSE;
|
|
|
|
|
if (wl->wl_next) {
|
|
|
|
|
/* There are several files -- put them into a temp file... */
|
|
|
|
|
tempfile = smktemp("sp");
|
2010-11-19 19:54:40 +01:00
|
|
|
if ((fp = inp_pathopen(tempfile, "w+")) == NULL) {
|
2000-04-27 22:03:57 +02:00
|
|
|
perror(tempfile);
|
|
|
|
|
cp_interactive = TRUE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (wl) {
|
2010-11-19 19:54:40 +01:00
|
|
|
if ((tp = inp_pathopen(wl->wl_word, "r")) == NULL) {
|
2000-04-27 22:03:57 +02:00
|
|
|
perror(wl->wl_word);
|
|
|
|
|
(void) fclose(fp);
|
|
|
|
|
cp_interactive = TRUE;
|
|
|
|
|
(void) unlink(tempfile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-11-04 20:15:41 +01:00
|
|
|
while ((n = fread(buf, 1, BSIZE_SP, tp)) > 0)
|
|
|
|
|
(void) fwrite(buf, 1, n, fp);
|
2000-04-27 22:03:57 +02:00
|
|
|
(void) fclose(tp);
|
|
|
|
|
wl = wl->wl_next;
|
|
|
|
|
}
|
2011-07-09 18:22:51 +02:00
|
|
|
(void) fseek(fp, 0L, SEEK_SET);
|
2000-04-27 22:03:57 +02:00
|
|
|
} else
|
|
|
|
|
fp = inp_pathopen(wl->wl_word, "r");
|
|
|
|
|
if (fp == NULL) {
|
|
|
|
|
perror(wl->wl_word);
|
|
|
|
|
cp_interactive = TRUE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Don't print the title if this is a .spiceinit file. */
|
2005-04-26 21:42:41 +02:00
|
|
|
if (ft_nutmeg || substring(INITSTR, owl->wl_word)
|
|
|
|
|
|| substring(ALT_INITSTR, owl->wl_word))
|
2011-04-28 17:59:36 +02:00
|
|
|
inp_nutsource(fp, TRUE, tempfile ? NULL : wl->wl_word);
|
2000-04-27 22:03:57 +02:00
|
|
|
else
|
2011-04-28 17:59:36 +02:00
|
|
|
inp_nutsource(fp, FALSE, tempfile ? NULL : wl->wl_word);
|
2000-04-27 22:03:57 +02:00
|
|
|
cp_interactive = inter;
|
|
|
|
|
if (tempfile)
|
|
|
|
|
(void) unlink(tempfile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nutinp_source(char *file)
|
|
|
|
|
{
|
|
|
|
|
static struct wordlist wl = { NULL, NULL, NULL } ;
|
|
|
|
|
|
|
|
|
|
wl.wl_word = file;
|
|
|
|
|
nutcom_source(&wl);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This routine is cut in half here because com_rset has to do what follows
|
|
|
|
|
* also. End is the list of commands we execute when the job is finished:
|
|
|
|
|
* we only bother with this if we might be running in batch mode, since
|
|
|
|
|
* it isn't much use otherwise.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nutinp_dodeck(struct line *deck, char *tt, wordlist *end, bool reuse, struct line *options, char *filename)
|
|
|
|
|
{
|
2010-11-16 21:38:24 +01:00
|
|
|
NG_IGNORE(filename);
|
|
|
|
|
NG_IGNORE(options);
|
|
|
|
|
NG_IGNORE(reuse);
|
|
|
|
|
NG_IGNORE(end);
|
|
|
|
|
NG_IGNORE(tt);
|
|
|
|
|
NG_IGNORE(deck);
|
2010-11-16 20:11:32 +01:00
|
|
|
|
2000-04-27 22:03:57 +02:00
|
|
|
/* This was "ifdef notdef"-ed out, so I tossed it */
|
|
|
|
|
}
|