fix save_inst resetting all symbols EMBEDDED flags, not only the used symbols in current schematic. Created get_generator_command(), will be used also for schematic generators

This commit is contained in:
stefan schippers 2023-04-24 11:36:07 +02:00
parent 7c04b009ec
commit 94d1865127
4 changed files with 85 additions and 67 deletions

View File

@ -1622,13 +1622,15 @@ static void save_inst(FILE *fd, int select_only)
char *tmp = NULL;
int *embedded_saved = NULL;
dbg(1, "save_inst(): saving instances\n");
inst=xctx->inst;
oldversion = !strcmp(xctx->file_version, "1.0");
for(i=0;i<xctx->symbols; ++i) xctx->sym[i].flags &=~EMBEDDED;
embedded_saved = my_calloc(_ALLOC_ID_, xctx->symbols, sizeof(int));
for(i=0;i<xctx->instances; ++i)
{
if (select_only && inst[i].sel != SELECTED) continue;
dbg(1, "save_inst() instance %d, name=%s\n", i, inst[i].name);
if (select_only && inst[i].sel != SELECTED) continue;
xctx->sym[inst[i].ptr].flags &=~EMBEDDED;
fputs("C ", fd);
if(oldversion) {
my_strdup2(_ALLOC_ID_, &tmp, add_ext(inst[i].name, ".sym"));
@ -1643,6 +1645,7 @@ static void save_inst(FILE *fd, int select_only)
if(is_symgen(inst[i].name)) {
embedded_saved[inst[i].ptr] = 1;
xctx->sym[inst[i].ptr].flags |= EMBEDDED;
dbg(1, "save_inst(): setting symbol %d to embedded\n", inst[i].ptr);
} else if(inst[i].embed) {
embedded_saved[inst[i].ptr] = 1;
fprintf(fd, "[\n");
@ -2428,6 +2431,11 @@ void load_schematic(int load_symbols, const char *fname, int reset_undo, int ale
if(reset_undo) xctx->prev_set_modify = -1; /* will force set_modify(0) to set window title */
else xctx->prev_set_modify = 0; /* will prevent set_modify(0) from setting window title */
if(fname && fname[0]) {
/*
int generator = 0;
tclvareval("is_xschem_file {", fname, "}", NULL);
if(!strcmp(tclresult(), "GENERATOR")) generator = 1;
*/
my_strncpy(name, fname, S(name));
/* remote web object specified */
if(strstr(fname , "http://") == fname ||
@ -2471,7 +2479,7 @@ void load_schematic(int load_symbols, const char *fname, int reset_undo, int ale
}
dbg(1, "load_schematic(): opening file for loading:%s, fname=%s\n", name, fname);
dbg(1, "load_schematic(): sch[currsch]=%s\n", xctx->sch[xctx->currsch]);
if(!name[0]) return;
if(!name[0]) return; /* empty filename */
if(reset_undo) {
if(!stat(name, &buf)) { /* file exists */
xctx->time_last_modify = buf.st_mtime;
@ -2675,6 +2683,7 @@ void pop_undo(int redo, int set_modify_status)
#endif
if(xctx->no_undo) return;
dbg(1, "pop_undo: redo=%d, set_modify_status=%d\n", redo, set_modify_status);
if(redo == 1) {
if(xctx->cur_undo_ptr < xctx->head_undo_ptr) {
dbg(1, "pop_undo(): redo; cur_undo_ptr=%d tail_undo_ptr=%d head_undo_ptr=%d\n",

View File

@ -198,72 +198,80 @@ char *sanitize(const char *name)
return s;
}
/* caller must free returned string
* given xxxx(a,b,c) return /path/to/xxxx a b c
* if no xxxx generator file found return NULL */
char *get_generator_command(const char *str)
{
char *cmd = NULL;
char *gen_cmd = NULL;
const char *cmd_filename;
char *spc_idx;
struct stat buf;
dbg(1, "get_generator_command(): symgen=%s\n",str);
cmd = str_chars_replace(str, " (),", ' '); /* transform str="xxx(a,b,c)" into cmd="xxx a b c" */
spc_idx = strchr(cmd, ' ');
if(!spc_idx) {
goto end;
}
*spc_idx = '\0';
cmd_filename = abs_sym_path(cmd, "");
if(stat(cmd_filename, &buf)) { /* symbol generator not found */
goto end;
}
my_strdup(_ALLOC_ID_, &gen_cmd, cmd_filename);
*spc_idx = ' ';
my_strcat(_ALLOC_ID_, &gen_cmd, spc_idx);
dbg(1, "get_generator_command(): cmd_filename=%s\n", cmd_filename);
dbg(1, "get_generator_command(): gen_cmd=%s\n", gen_cmd);
dbg(1, "get_generator_command(): is_symgen=%d\n", is_symgen(str));
end:
my_free(_ALLOC_ID_, &cmd);
return gen_cmd;
}
int match_symbol(const char *name) /* never returns -1, if symbol not found load systemlib/missing.sym */
{
int i,found;
found=0;
for(i=0;i<xctx->symbols; ++i)
{
/* dbg(1, "match_symbol(): name=%s, sym[i].name=%s\n",name, xctx->sym[i].name);*/
if(xctx->x_strcmp(name, xctx->sym[i].name) == 0)
{
dbg(1, "match_symbol(): found matching symbol:%s\n",name);
found=1;break;
}
}
if(!found)
{
dbg(1, "match_symbol(): matching symbol not found: loading\n");
if(!is_symgen(name)) {
dbg(1, "match_symbol(): symbol=%s\n",name);
load_sym_def(name, NULL, 0); /* append another symbol to the xctx->sym[] array */
} else { /* get symbol from generator script */
FILE *fp;
char *cmd = NULL;
char *ss = NULL;
const char *s;
char *spc_idx;
struct stat buf;
/* char *symgen_name = NULL; */
int i,found, is_sym_generator;
dbg(1, "match_symbol(): symgen=%s\n",name);
cmd = str_chars_replace(name, " (),", ' '); /* transform name="xxx(a,b,c)" into ss="xxx a b c" */
spc_idx = strchr(cmd, ' ');
if(!spc_idx) goto end;
*spc_idx = '\0';
s = abs_sym_path(cmd, "");
if(stat(s, &buf)) { /* symbol generator not found, load 'name' (will probably lead to missing.sym) */
load_sym_def(name, NULL, 0);
goto end;
}
my_strdup(_ALLOC_ID_, &ss, s);
*spc_idx = ' ';
my_strcat(_ALLOC_ID_, &ss, spc_idx);
fp = popen(ss, "r"); /* execute ss="xxx a b c" and pipe in the output */
dbg(1, "match_symbol(): fp=%p\n", fp);
dbg(1, "match_symbol(): s=%s\n", s);
dbg(1, "match_symbol(): ss=%s\n", ss);
dbg(1, "match_symbol(): is_symgen=%d\n", is_symgen(name));
/*
tclvareval("regsub -all { *[(),] *} {", name, "} _", NULL);
tclvareval("regsub {_$} {", tclresult(), "} {}", NULL);
my_strdup2(_ALLOC_ID_, &symgen_name, tclresult());
load_sym_def(symgen_name, fp, 1);
dbg(1, "match_symbol(): name=%s, regsub=%s\n", name, symgen_name);
my_free(_ALLOC_ID_, &symgen_name);
*/
load_sym_def(name, fp, 1);
dbg(1, "match_symbol(): symbol name%s\n", xctx->sym[xctx->symbols - 1].name);
pclose(fp);
my_free(_ALLOC_ID_, &ss);
end:
my_free(_ALLOC_ID_, &cmd);
}
}
dbg(1, "match_symbol(): returning %d\n",i);
return i;
found=0;
is_sym_generator = is_symgen(name);
for(i=0;i<xctx->symbols; ++i) {
/* dbg(1, "match_symbol(): name=%s, sym[i].name=%s\n",name, xctx->sym[i].name);*/
if(xctx->x_strcmp(name, xctx->sym[i].name) == 0)
{
dbg(1, "match_symbol(): found matching symbol:%s\n",name);
found=1;break;
}
}
if(!found) {
dbg(1, "match_symbol(): matching symbol not found: loading\n");
if(is_sym_generator) {
FILE *fp;
char *cmd;
cmd = get_generator_command(name);
if(cmd) {
fp = popen(cmd, "r"); /* execute ss="/path/to/xxx par1 par2 ..." and pipe in the stdout */
my_free(_ALLOC_ID_, &cmd);
load_sym_def(name, fp, 1); /* append another symbol to the xctx->sym[] array */
dbg(1, "match_symbol(): generator symbol name=%s\n", xctx->sym[xctx->symbols - 1].name);
pclose(fp);
} else {
i = xctx->symbols; /* not found */
}
} else {
dbg(1, "match_symbol(): symbol=%s\n",name);
load_sym_def(name, NULL, 0); /* append another symbol to the xctx->sym[] array */
dbg(1, "match_symbol(): symbol name=%s\n", xctx->sym[xctx->symbols - 1].name);
}
}
dbg(1, "match_symbol(): returning %d\n",i);
return i;
}
/* update **s modifying only the token values that are */

View File

@ -1284,6 +1284,7 @@ extern void attach_labels_to_inst(int interactive);
extern short connect_by_kissing(void);
extern void delete_files(void);
extern int sym_vs_sch_pins(void);
extern char *get_generator_command(const char *str);
extern int match_symbol(const char name[]);
extern int save_schematic(const char *); /* 20171020 added return value */
extern void copy_symbol(xSymbol *dest_sym, xSymbol *src_sym);

View File

@ -44,7 +44,7 @@ L 4 -20 -20 -20 20 {}
L 4 -20 20 20 0 {}
B 5 37.5 -2.5 42.5 2.5 {name=Y dir=out }
B 5 -42.5 -2.5 -37.5 2.5 {name=A dir=in }
T {@symname} -47.5 34 0 0 0.3 0.3 {}
T {@symname} -47.5 24 0 0 0.3 0.3 {}
T {@name} 25 -22 0 0 0.2 0.2 {}
T {Y} 7.5 -6.5 0 1 0.2 0.2 {}
T {A} -17.5 -6.5 0 0 0.2 0.2 {}