aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog13
-rw-r--r--awk.h4
-rw-r--r--builtin.c23
-rw-r--r--io.c40
4 files changed, 40 insertions, 40 deletions
diff --git a/ChangeLog b/ChangeLog
index 0ee6bf26..c61e8eec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2015-02-08 Andrew J. Schorr <aschorr@telemetry-investments.com>
+
+ * awk.h (RED_NON_FATAL): Removed.
+ (redirect): Add new failure_fatal parameter.
+ (is_non_fatal_redirect): Add declaration.
+ * builtin.c (efwrite): Rework check for non-fatal.
+ (do_printf): Adjust calls to redirect.
+ (do_print_rec): Ditto. Move check for redirection error up.
+ * io.c (redflags2str): Remove RED_NON_FATAL.
+ (redirect): Add new failure_fatal parameter. Simplify the code.
+ (is_non_fatal_redirect): New function.
+ (do_getline_redir): Adjust calls to redirect.
+
2014-12-27 Arnold D. Robbins <arnold@skeeve.com>
* awk.h (is_non_fatal_std): Declare new function.
diff --git a/awk.h b/awk.h
index 81a0e91f..b1272ade 100644
--- a/awk.h
+++ b/awk.h
@@ -918,7 +918,6 @@ struct redirect {
# define RED_PTY 512
# define RED_SOCKET 1024
# define RED_TCP 2048
-# define RED_NON_FATAL 4096
char *value;
FILE *ifp; /* input fp, needed for PIPES_SIMULATED */
IOBUF *iop;
@@ -1484,7 +1483,7 @@ extern void register_two_way_processor(awk_two_way_processor_t *processor);
extern void set_FNR(void);
extern void set_NR(void);
-extern struct redirect *redirect(NODE *redir_exp, int redirtype, int *errflg);
+extern struct redirect *redirect(NODE *redir_exp, int redirtype, int *errflg, bool failure_fatal);
extern NODE *do_close(int nargs);
extern int flush_io(void);
extern int close_io(bool *stdio_problem);
@@ -1497,6 +1496,7 @@ extern struct redirect *getredirect(const char *str, int len);
extern bool inrec(IOBUF *iop, int *errcode);
extern int nextfile(IOBUF **curfile, bool skipping);
extern bool is_non_fatal_std(FILE *fp);
+extern bool is_non_fatal_redirect(const char *str);
/* main.c */
extern int arg_assign(char *arg, bool initing);
extern int is_std_var(const char *var);
diff --git a/builtin.c b/builtin.c
index e80d46d4..aa8caf09 100644
--- a/builtin.c
+++ b/builtin.c
@@ -131,10 +131,9 @@ wrerror:
/* otherwise die verbosely */
- if ( (rp != NULL && (rp->flag & RED_NON_FATAL) != 0)
- || is_non_fatal_std(fp)) {
+ if ((rp != NULL) ? is_non_fatal_redirect(rp->value) : is_non_fatal_std(fp))
update_ERRNO_int(errno);
- } else
+ else
fatal(_("%s to \"%s\" failed (%s)"), from,
rp ? rp->value : _("standard output"),
errno ? strerror(errno) : _("reason unknown"));
@@ -1649,7 +1648,7 @@ do_printf(int nargs, int redirtype)
redir_exp = TOP();
if (redir_exp->type != Node_val)
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(redir_exp));
- rp = redirect(redir_exp, redirtype, & errflg);
+ rp = redirect(redir_exp, redirtype, & errflg, true);
DEREF(redir_exp);
decr_sp();
}
@@ -1662,7 +1661,7 @@ do_printf(int nargs, int redirtype)
redir_exp = PEEK(nargs);
if (redir_exp->type != Node_val)
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(redir_exp));
- rp = redirect(redir_exp, redirtype, & errflg);
+ rp = redirect(redir_exp, redirtype, & errflg, true);
if (rp != NULL)
fp = rp->output.fp;
else if (errflg) {
@@ -2093,7 +2092,7 @@ do_print(int nargs, int redirtype)
redir_exp = PEEK(nargs);
if (redir_exp->type != Node_val)
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(redir_exp));
- rp = redirect(redir_exp, redirtype, & errflg);
+ rp = redirect(redir_exp, redirtype, & errflg, true);
if (rp != NULL)
fp = rp->output.fp;
else if (errflg) {
@@ -2161,7 +2160,7 @@ do_print_rec(int nargs, int redirtype)
assert(nargs == 0);
if (redirtype != 0) {
redir_exp = TOP();
- rp = redirect(redir_exp, redirtype, & errflg);
+ rp = redirect(redir_exp, redirtype, & errflg, true);
if (rp != NULL)
fp = rp->output.fp;
DEREF(redir_exp);
@@ -2169,17 +2168,17 @@ do_print_rec(int nargs, int redirtype)
} else
fp = output_fp;
+ if (errflg) {
+ update_ERRNO_int(errflg);
+ return;
+ }
+
if (fp == NULL)
return;
if (! field0_valid)
(void) get_field(0L, NULL); /* rebuild record */
- if (errflg) {
- update_ERRNO_int(errflg);
- return;
- }
-
f0 = fields_arr[0];
if (do_lint && (f0->flags & NULL_FIELD) != 0)
diff --git a/io.c b/io.c
index b3819c01..db8a0a2b 100644
--- a/io.c
+++ b/io.c
@@ -261,7 +261,6 @@ struct recmatch {
static int iop_close(IOBUF *iop);
-struct redirect *redirect(NODE *redir_exp, int redirtype, int *errflg);
static void close_one(void);
static int close_redir(struct redirect *rp, bool exitwarn, two_way_close_type how);
#ifndef PIPES_SIMULATED
@@ -718,7 +717,6 @@ redflags2str(int flags)
{ RED_PTY, "RED_PTY" },
{ RED_SOCKET, "RED_SOCKET" },
{ RED_TCP, "RED_TCP" },
- { RED_NON_FATAL, "RED_NON_FATAL" },
{ 0, NULL }
};
@@ -728,7 +726,7 @@ redflags2str(int flags)
/* redirect --- Redirection for printf and print commands */
struct redirect *
-redirect(NODE *redir_exp, int redirtype, int *errflg)
+redirect(NODE *redir_exp, int redirtype, int *errflg, bool failure_fatal)
{
struct redirect *rp;
char *str;
@@ -745,8 +743,6 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
static struct redirect *save_rp = NULL; /* hold onto rp that should
* be freed for reuse
*/
- bool is_output = false;
- bool is_non_fatal = false;
if (do_sandbox)
fatal(_("redirection not allowed in sandbox mode"));
@@ -756,7 +752,6 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
tflag = RED_APPEND;
/* FALL THROUGH */
case redirect_output:
- is_output = true;
outflag = (RED_FILE|RED_WRITE);
tflag |= outflag;
if (redirtype == redirect_output)
@@ -765,7 +760,6 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
what = ">>";
break;
case redirect_pipe:
- is_output = true;
tflag = (RED_PIPE|RED_WRITE);
what = "|";
break;
@@ -778,7 +772,6 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
what = "<";
break;
case redirect_twoway:
- is_output = true;
tflag = (RED_READ|RED_WRITE|RED_TWOWAY);
what = "|&";
break;
@@ -800,14 +793,6 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
lintwarn(_("filename `%s' for `%s' redirection may be result of logical expression"),
str, what);
- /* input files/pipes are automatically nonfatal */
- if (is_output) {
- is_non_fatal = (in_PROCINFO("nonfatal", NULL, NULL) != NULL
- || in_PROCINFO(str, "nonfatal", NULL) != NULL);
- if (is_non_fatal)
- tflag |= RED_NON_FATAL;
- }
-
#ifdef HAVE_SOCKETS
/*
* Use /inet4 to force IPv4, /inet6 to force IPv6, and plain
@@ -907,7 +892,7 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
os_restore_mode(fileno(stdin));
/*
- * Don't check is_non_fatal; see input pipe below.
+ * Don't check failure_fatal; see input pipe below.
* Note that the failure happens upon failure to fork,
* using a non-existant program will still succeed the
* popen().
@@ -947,17 +932,11 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
case redirect_twoway:
direction = "to/from";
if (! two_way_open(str, rp)) {
-#ifdef HAVE_SOCKETS
- if (inetfile(str, NULL)) {
- *errflg = errno;
- /* do not free rp, saving it for reuse (save_rp = rp) */
- return NULL;
- } else if (is_non_fatal) {
+ if (! failure_fatal || is_non_fatal_redirect(str)) {
*errflg = errno;
/* do not free rp, saving it for reuse (save_rp = rp) */
return NULL;
} else
-#endif
fatal(_("can't open two way pipe `%s' for input/output (%s)"),
str, strerror(errno));
}
@@ -1038,7 +1017,7 @@ redirect(NODE *redir_exp, int redirtype, int *errflg)
*/
if (errflg != NULL)
*errflg = errno;
- if (! is_non_fatal &&
+ if (failure_fatal && ! is_non_fatal_redirect(str) &&
(redirtype == redirect_output
|| redirtype == redirect_append)) {
/* multiple messages make life easier for translators */
@@ -1104,6 +1083,15 @@ is_non_fatal_std(FILE *fp)
return false;
}
+/* is_non_fatal_redirect --- return true if redirected I/O should be nonfatal */
+
+bool
+is_non_fatal_redirect(const char *str)
+{
+ return in_PROCINFO("nonfatal", NULL, NULL) != NULL
+ || in_PROCINFO(str, "nonfatal", NULL) != NULL;
+}
+
/* close_one --- temporarily close an open file to re-use the fd */
static void
@@ -2467,7 +2455,7 @@ do_getline_redir(int into_variable, enum redirval redirtype)
assert(redirtype != redirect_none);
redir_exp = TOP();
- rp = redirect(redir_exp, redirtype, & redir_error);
+ rp = redirect(redir_exp, redirtype, & redir_error, false);
DEREF(redir_exp);
decr_sp();
if (rp == NULL) {