diff --git a/src/editprop.c b/src/editprop.c index 0fd469ae..6cfaa380 100644 --- a/src/editprop.c +++ b/src/editprop.c @@ -1263,6 +1263,54 @@ void change_elem_order(void) } } +/* You must free the result if result is non-NULL. */ +char *str_replace(char *orig, char *rep, char *with) +{ + char *result; /* the return string */ + char *ins; /* the next insert point */ + char *tmp; /* varies */ + size_t len_rep; /* length of rep (the string to remove) */ + size_t len_with; /* length of with (the string to replace rep with) */ + size_t len_front; /* distance between rep and end of last rep */ + int count; /* number of replacements */ + + /* sanity checks and initialization */ + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; /* empty rep causes infinite loop during count */ + if (!with) + with = ""; + len_with = strlen(with); + + /* count the number of replacements needed */ + ins = orig; + for (count = 0; (tmp = strstr(ins, rep)); ++count) { + ins = tmp + len_rep; + } + + tmp = result = my_malloc(1244, strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + /* first time through the loop, all the variable are set correctly */ + /* from here on, */ + /* tmp points to the end of the result string */ + /* ins points to the next occurrence of rep in orig */ + /* orig points to the remainder of orig after "end of rep" */ + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; /* move to next "end of rep" */ + } + strcpy(tmp, orig); + return result; +} + /* x=0 use tcl text widget x=1 use vim editor x=2 only view data */ void edit_property(int x) { diff --git a/src/token.c b/src/token.c index 480d9fa5..db4cb8e8 100644 --- a/src/token.c +++ b/src/token.c @@ -123,14 +123,17 @@ const char *tcl_hook2(char **res) { static char *result = NULL; static const char *empty=""; + char *unescaped_res; if(res == NULL || *res == NULL) { my_free(1285, &result); return empty; } if(strstr(*res, "tcleval(") == *res) { - tclvareval("tclpropeval2 {", *res, "}" , NULL); + unescaped_res = str_replace(*res, "\\}", "}"); + tclvareval("tclpropeval2 {", unescaped_res, "}" , NULL); my_strdup2(1286, &result, tclresult()); + my_free(1245, &unescaped_res); return result; } else { return *res; @@ -508,12 +511,14 @@ const char *get_sym_template(char *s,char *extra) /* 2: eat backslashes */ /* 3: 1+2 :) */ + dbg(1, "get_sym_template(): s=%s, extra=%s\n", s, extra); if(s==NULL) { my_free(978, &result); return ""; } l = strlen(s); STR_ALLOC(&result, l+1, &sizeres); + result[0] = '\0'; sizetok = sizeval = CADCHUNKALLOC; my_realloc(438, &value,sizeval); my_realloc(439, &token,sizetok); @@ -1610,14 +1615,17 @@ void print_spice_subckt(FILE *fd, int symbol) int i=0, multip; const char *str_ptr=NULL; register int c, state=TOK_BEGIN, space; - char *format=NULL,*s, *token=NULL; + char *format=NULL, *format1 = NULL, *s, *token=NULL; int pin_number; size_t sizetok=0; size_t token_pos=0; int escape=0; int no_of_pins=0; - my_strdup(103, &format, get_tok_value(xctx->sym[symbol].prop_ptr,"format",2)); + my_strdup(103, &format1, get_tok_value(xctx->sym[symbol].prop_ptr,"format",2)); + dbg(1, "print_spice_subckt(): format1=%s\n", format1); + my_strdup(455, &format, tcl_hook2(&format1)); + dbg(1, "print_spice_subckt(): format=%s\n", format); if( format==NULL ) { my_free(1012, &format); return; /* no format */ @@ -1680,7 +1688,7 @@ void print_spice_subckt(FILE *fd, int symbol) if(!strcmp(get_tok_value(prop, "name",0), token + 2)) break; } if(i