From 789edee376af67d430f36cac00ac8816464475d6 Mon Sep 17 00:00:00 2001 From: rlar Date: Sun, 19 Aug 2012 14:04:37 +0200 Subject: [PATCH] rewrite inp_list() avoid duplicate code --- src/frontend/inp.c | 120 ++++++++++++++------------------- src/frontend/terminal.c | 31 +++++---- src/include/ngspice/cpextern.h | 3 + 3 files changed, 71 insertions(+), 83 deletions(-) diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 1942f3bd8..d349a507f 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -52,6 +52,30 @@ void SetAnalyse(char *Analyse, int Percent); #endif +/* + * create an unique artificial *unusable* FILE ptr + * meant to be used with Xprintf() only to eventually + * redirect output to the `out_vprintf()' family + */ + +static FILE *cp_more; +static FILE *cp_more = (FILE*) &cp_more; + +static void +Xprintf(FILE *fdst, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + if (fdst == cp_more) + out_vprintf(fmt, ap); + else + vfprintf(fdst, fmt, ap); + + va_end(ap); +} + + /* Do a listing. Use is listing [expanded] [logical] [physical] [deck] */ void com_listing(wordlist *wl) @@ -142,8 +166,10 @@ inp_list(FILE *file, struct line *deck, struct line *extras, int type) #endif /* gtri - end - 03/07/91 */ - if (useout) + if (useout) { out_init(); + file = cp_more; + } renumber = cp_getvar("renumber", CP_BOOL, NULL); @@ -155,19 +181,9 @@ inp_list(FILE *file, struct line *deck, struct line *extras, int type) if (ciprefix(".end", here->li_line) && !isalpha(here->li_line[4])) continue; if (*here->li_line != '*') { - if (useout) { - out_printf("%6d : %s\n", - here->li_linenum, upper(here->li_line)); - } else { - fprintf(file, "%6d : %s\n", - here->li_linenum, upper(here->li_line)); - } - if (here->li_error) { - if (useout) - out_printf("%s\n", here->li_error); - else - fprintf(file, "%s\n", here->li_error); - } + Xprintf(file, "%6d : %s\n", here->li_linenum, upper(here->li_line)); + if (here->li_error) + Xprintf(file, "%s\n", here->li_error); } i++; } @@ -178,11 +194,7 @@ inp_list(FILE *file, struct line *deck, struct line *extras, int type) goto top1; } - if (useout) { - out_printf("%6d : .end\n", i); - } else { - fprintf(file, "%6d : .end\n", i); - } + Xprintf(file, "%6d : .end\n", i); } else if ((type == LS_PHYSICAL) || (type == LS_DECK)) { @@ -193,51 +205,25 @@ inp_list(FILE *file, struct line *deck, struct line *extras, int type) here->li_linenum = i; if (ciprefix(".end", here->li_line) && !isalpha(here->li_line[4])) continue; - if (type == LS_PHYSICAL) { - if (useout) { - out_printf("%6d : %s\n", - here->li_linenum, upper(here->li_line)); - } else { - fprintf(file, "%6d : %s\n", - here->li_linenum, upper(here->li_line)); - } - } else { - if (useout) - out_printf("%s\n", upper(here->li_line)); - else - fprintf(file, "%s\n", upper(here->li_line)); - } - if (here->li_error && (type == LS_PHYSICAL)) { - if (useout) - out_printf("%s\n", here->li_error); - else - fprintf(file, "%s\n", here->li_error); - } + if (type == LS_PHYSICAL) + Xprintf(file, "%6d : %s\n", + here->li_linenum, upper(here->li_line)); + else + Xprintf(file, "%s\n", upper(here->li_line)); + if (here->li_error && (type == LS_PHYSICAL)) + Xprintf(file, "%s\n", here->li_error); } else { for (there = here->li_actual; there; there = there->li_next) { there->li_linenum = i++; if (ciprefix(".end", here->li_line) && isalpha(here->li_line[4])) continue; - if (type == LS_PHYSICAL) { - if (useout) { - out_printf("%6d : %s\n", - there->li_linenum, upper(there->li_line)); - } else { - fprintf(file, "%6d : %s\n", - there->li_linenum, upper(there->li_line)); - } - } else { - if (useout) - out_printf("%s\n", upper(there->li_line)); - else - fprintf(file, "%s\n", upper(there->li_line)); - } - if (there->li_error && (type == LS_PHYSICAL)) { - if (useout) - out_printf("%s\n", there->li_error); - else - fprintf(file, "%s\n", there->li_error); - } + if (type == LS_PHYSICAL) + Xprintf(file, "%6d : %s\n", + there->li_linenum, upper(there->li_line)); + else + Xprintf(file, "%s\n", upper(there->li_line)); + if (there->li_error && (type == LS_PHYSICAL)) + Xprintf(file, "%s\n", there->li_error); } here->li_linenum = i; } @@ -248,18 +234,10 @@ inp_list(FILE *file, struct line *deck, struct line *extras, int type) extras = NULL; goto top2; } - if (type == LS_PHYSICAL) { - if (useout) { - out_printf("%6d : .end\n", i); - } else { - fprintf(file, "%6d : .end\n", i); - } - } else { - if (useout) - out_printf(".end\n"); - else - fprintf(file, ".end\n"); - } + if (type == LS_PHYSICAL) + Xprintf(file, "%6d : .end\n", i); + else + Xprintf(file, ".end\n"); } else { fprintf(cp_err, "inp_list: Internal Error: bad type %d\n", type); } diff --git a/src/frontend/terminal.c b/src/frontend/terminal.c index 7c03497c6..61f441b25 100644 --- a/src/frontend/terminal.c +++ b/src/frontend/terminal.c @@ -250,34 +250,35 @@ out_send(char *string) /* Printf some stuff using more mode. */ void -out_printf(char *fmt, ...) +out_vprintf(const char *fmt, va_list ap) { #if defined(HAVE_ASPRINTF) /* seems the best solution */ char * tbuf; - va_list ap; - va_start (ap, fmt); vasprintf(&tbuf, fmt, ap); - va_end (ap); out_send(tbuf); FREE(tbuf); #elif defined(HAVE_SNPRINTF) /* the second best */ static char out_pbuf[8*BSIZE_SP]; - va_list ap; - va_start (ap, fmt); vsnprintf(out_pbuf, sizeof(out_pbuf), fmt, ap); - va_end (ap); out_send(out_pbuf); #else /* guaranteed a bug for long messages */ static char out_pbuf[8*BSIZE_SP]; - va_list ap; - va_start (ap, fmt); vsprintf(out_pbuf, fmt, ap); - va_end (ap); out_send(out_pbuf); #endif - return; } + +void +out_printf(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + out_vprintf(fmt, ap); + va_end(ap); +} + + #ifdef HAVE_TERMCAP static int outfn(int c) @@ -379,12 +380,18 @@ out_send(char *string) fprintf(cp_out, "%s", string); } +void +out_vprintf(const char *fmt, va_list ap) +{ + vfprintf(cp_out, fmt, ap); +} + void out_printf(char *fmt, ...) { va_list ap; va_start (ap, fmt); - vfprintf(cp_out, fmt, ap); + out_vprintf(fmt, ap); va_end (ap); } diff --git a/src/include/ngspice/cpextern.h b/src/include/ngspice/cpextern.h index 97e761298..e5e525512 100644 --- a/src/include/ngspice/cpextern.h +++ b/src/include/ngspice/cpextern.h @@ -14,6 +14,8 @@ Modified: 2000 AlansFixes #include "ngspice/wordlist.h" #include "ngspice/bool.h" +#include + struct ccom; /* com_alias.c */ @@ -126,6 +128,7 @@ extern void out_printf(char *fmt, ...) __attribute__ ((format (__printf__, 1, 2) extern void out_printf(char *fmt, ...); #endif +extern void out_vprintf(const char *fmt, va_list ap); extern void out_send(char *string); /* quote.c */