rewrite using wl_ functions
This commit is contained in:
parent
cd2cab2a94
commit
512644ca34
|
|
@ -1,6 +1,6 @@
|
||||||
/*************
|
/*************
|
||||||
* streams.c
|
* streams.c
|
||||||
************/
|
************/
|
||||||
|
|
||||||
#include "ngspice/config.h"
|
#include "ngspice/config.h"
|
||||||
#include "ngspice/ngspice.h"
|
#include "ngspice/ngspice.h"
|
||||||
|
|
@ -28,8 +28,6 @@ FILE *cp_curin = NULL;
|
||||||
FILE *cp_curout = NULL;
|
FILE *cp_curout = NULL;
|
||||||
FILE *cp_curerr = NULL;
|
FILE *cp_curerr = NULL;
|
||||||
|
|
||||||
/* static functions */
|
|
||||||
static bool fileexists(char *name);
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
fileexists(char *name)
|
fileexists(char *name)
|
||||||
|
|
@ -47,132 +45,126 @@ fileexists(char *name)
|
||||||
wordlist *
|
wordlist *
|
||||||
cp_redirect(wordlist *wl)
|
cp_redirect(wordlist *wl)
|
||||||
{
|
{
|
||||||
bool gotinput = FALSE, gotoutput = FALSE, goterror = FALSE;
|
int gotinput = 0, gotoutput = 0, goterror = 0, append = 0;
|
||||||
bool app = FALSE, erralso = FALSE;
|
wordlist *w;
|
||||||
wordlist *w, *bt, *nw;
|
char *fname;
|
||||||
char *s,*copyword;
|
FILE *fp;
|
||||||
FILE *tmpfp;
|
|
||||||
|
|
||||||
w = wl->wl_next; /* Don't consider empty commands. */
|
w = wl->wl_next; /* Don't consider empty commands. */
|
||||||
|
|
||||||
while (w) {
|
while (w) {
|
||||||
if (*w->wl_word == cp_lt) {
|
if (*w->wl_word == cp_lt) {
|
||||||
bt = w;
|
|
||||||
if (gotinput) {
|
wordlist *bt = w->wl_prev;
|
||||||
fprintf(cp_err,
|
|
||||||
"Error: ambiguous input redirect.\n");
|
if (gotinput++) {
|
||||||
|
fprintf(cp_err, "Error: ambiguous input redirect.\n");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
gotinput = TRUE;
|
|
||||||
w = w->wl_next;
|
w = w->wl_next;
|
||||||
if (w == NULL) {
|
|
||||||
fprintf(cp_err,
|
if (w && *w->wl_word == cp_lt) {
|
||||||
"Error: missing name for input.\n");
|
fprintf(cp_err, "Error: `<<' redirection is not implemented.\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!w) {
|
||||||
|
fprintf(cp_err, "Error: missing name for input.\n");
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
if (*w->wl_word == cp_lt) {
|
|
||||||
/* Do reasonable stuff here... */
|
|
||||||
} else {
|
|
||||||
/*tmpfp = fopen(cp_unquote(w->wl_word), "r"); DG very bad: memory leak the string allocated by cp_unquote is lost*/
|
|
||||||
copyword=cp_unquote(w->wl_word);/*DG*/
|
|
||||||
tmpfp = fopen(copyword, "r");
|
|
||||||
tfree(copyword);
|
|
||||||
|
|
||||||
if (!tmpfp) {
|
fname = cp_unquote(w->wl_word);
|
||||||
perror(w->wl_word);
|
|
||||||
goto error;
|
|
||||||
} else
|
|
||||||
cp_in = tmpfp;
|
|
||||||
}
|
|
||||||
#ifdef CPDEBUG
|
#ifdef CPDEBUG
|
||||||
if (cp_debug)
|
if (cp_debug)
|
||||||
fprintf(cp_err, "Input file is %s...\n",
|
fprintf(cp_err, "Input file is %s...\n", fname);
|
||||||
w->wl_word);
|
|
||||||
#endif
|
#endif
|
||||||
bt->wl_prev->wl_next = w->wl_next;
|
|
||||||
if (w->wl_next)
|
fp = fopen(fname, "r");
|
||||||
w->wl_next->wl_prev = bt->wl_prev;
|
tfree(fname);
|
||||||
nw = w->wl_next;
|
|
||||||
w->wl_next = NULL;
|
if (!fp) {
|
||||||
w = nw;
|
|
||||||
wl_free(bt);
|
|
||||||
} else if (*w->wl_word == cp_gt) {
|
|
||||||
bt = w;
|
|
||||||
if (gotoutput) {
|
|
||||||
fprintf(cp_err,
|
|
||||||
"Error: ambiguous output redirect.\n");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
gotoutput = TRUE;
|
|
||||||
w = w->wl_next;
|
|
||||||
if (w == NULL) {
|
|
||||||
fprintf(cp_err,
|
|
||||||
"Error: missing name for output.\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if (*w->wl_word == cp_gt) {
|
|
||||||
app = TRUE;
|
|
||||||
w = w->wl_next;
|
|
||||||
if (w == NULL) {
|
|
||||||
fprintf(cp_err,
|
|
||||||
"Error: missing name for output.\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (*w->wl_word == cp_amp) {
|
|
||||||
erralso = TRUE;
|
|
||||||
if (goterror) {
|
|
||||||
fprintf(cp_err,
|
|
||||||
"Error: ambiguous error redirect.\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
goterror = TRUE;
|
|
||||||
w = w->wl_next;
|
|
||||||
if (w == NULL) {
|
|
||||||
fprintf(cp_err,
|
|
||||||
"Error: missing name for output.\n");
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s = cp_unquote(w->wl_word);
|
|
||||||
if (cp_noclobber && fileexists(s)) {
|
|
||||||
fprintf(stderr, "Error: %s: file exists\n", s);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (app)
|
|
||||||
tmpfp = fopen(s, "a");
|
|
||||||
else
|
|
||||||
tmpfp = fopen(s, "w+");
|
|
||||||
tfree(s);/*DG cp_unquote memory leak*/
|
|
||||||
if (!tmpfp) {
|
|
||||||
perror(w->wl_word);
|
perror(w->wl_word);
|
||||||
goto error;
|
goto error;
|
||||||
} else {
|
|
||||||
cp_out = tmpfp;
|
|
||||||
out_isatty = FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cp_in = fp;
|
||||||
|
|
||||||
|
w = wl_chop_rest(w);
|
||||||
|
wl_free(wl_chop_rest(bt));
|
||||||
|
wl_append(bt, w);
|
||||||
|
|
||||||
|
} else if (*w->wl_word == cp_gt) {
|
||||||
|
|
||||||
|
wordlist *bt = w->wl_prev;
|
||||||
|
|
||||||
|
if (gotoutput++) {
|
||||||
|
fprintf(cp_err, "Error: ambiguous output redirect.\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
w = w->wl_next;
|
||||||
|
|
||||||
|
if (w && *w->wl_word == cp_gt) {
|
||||||
|
append++;
|
||||||
|
w = w->wl_next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (w && *w->wl_word == cp_amp) {
|
||||||
|
if (goterror++) {
|
||||||
|
fprintf(cp_err, "Error: ambiguous error redirect.\n");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
w = w->wl_next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!w) {
|
||||||
|
fprintf(cp_err, "Error: missing name for output.\n");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
fname = cp_unquote(w->wl_word);
|
||||||
|
|
||||||
#ifdef CPDEBUG
|
#ifdef CPDEBUG
|
||||||
if (cp_debug)
|
if (cp_debug)
|
||||||
fprintf(cp_err, "Output file is %s... %s\n",
|
fprintf(cp_err, "Output file is %s... %s\n", fname,
|
||||||
w->wl_word, app ? "(append)" : "");
|
append ? "(append)" : "");
|
||||||
#endif
|
#endif
|
||||||
bt->wl_prev->wl_next = w->wl_next;
|
|
||||||
if (w->wl_next)
|
if (cp_noclobber && fileexists(fname)) {
|
||||||
w->wl_next->wl_prev = bt->wl_prev;
|
fprintf(stderr, "Error: %s: file exists\n", fname);
|
||||||
w = w->wl_next;
|
goto error;
|
||||||
if (w)
|
}
|
||||||
w->wl_prev->wl_next = NULL;
|
|
||||||
wl_free(bt);
|
fp = fopen(fname, append ? "a" : "w+");
|
||||||
if (erralso)
|
tfree(fname);
|
||||||
cp_err = cp_out;
|
|
||||||
} else
|
if (!fp) {
|
||||||
|
perror(w->wl_word);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
cp_out = fp;
|
||||||
|
if (goterror)
|
||||||
|
cp_err = fp;
|
||||||
|
|
||||||
|
out_isatty = FALSE;
|
||||||
|
|
||||||
|
w = wl_chop_rest(w);
|
||||||
|
wl_free(wl_chop_rest(bt));
|
||||||
|
wl_append(bt, w);
|
||||||
|
|
||||||
|
} else {
|
||||||
w = w->wl_next;
|
w = w->wl_next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (wl);
|
return (wl);
|
||||||
|
|
||||||
error: wl_free(wl);
|
error:
|
||||||
|
wl_free(wl);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Reset the cp_* FILE pointers to the standard ones. This is tricky,
|
/* Reset the cp_* FILE pointers to the standard ones. This is tricky,
|
||||||
* since if we are sourcing a command file, and io has been redirected
|
* since if we are sourcing a command file, and io has been redirected
|
||||||
* from inside the file, we have to reset it back to what it was for
|
* from inside the file, we have to reset it back to what it was for
|
||||||
|
|
@ -200,7 +192,6 @@ cp_ioreset(void)
|
||||||
|
|
||||||
/*** Minor bug here... */
|
/*** Minor bug here... */
|
||||||
out_isatty = TRUE;
|
out_isatty = TRUE;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -215,5 +206,4 @@ fixdescriptors(void)
|
||||||
dup2(fileno(cp_out), fileno(stdout));
|
dup2(fileno(cp_out), fileno(stdout));
|
||||||
if (cp_err != stderr)
|
if (cp_err != stderr)
|
||||||
dup2(fileno(cp_err), fileno(stderr));
|
dup2(fileno(cp_err), fileno(stderr));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue