[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gawk-diffs] [SCM] gawk branch, print-exp, updated. gawk-4.1.0-1003-g5d3
From: |
Arnold Robbins |
Subject: |
[gawk-diffs] [SCM] gawk branch, print-exp, updated. gawk-4.1.0-1003-g5d35746 |
Date: |
Mon, 08 Dec 2014 21:09:51 +0000 |
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gawk".
The branch, print-exp has been updated
via 5d35746cc83255fb01e265b429f0941e4910ac3c (commit)
from 24eaa2af13b4dc19ee4a71fdd13dcbe7ed5033b2 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://git.sv.gnu.org/cgit/gawk.git/commit/?id=5d35746cc83255fb01e265b429f0941e4910ac3c
commit 5d35746cc83255fb01e265b429f0941e4910ac3c
Author: Arnold D. Robbins <address@hidden>
Date: Mon Dec 8 23:09:30 2014 +0200
print/printf as expression working!
diff --git a/ChangeLog b/ChangeLog
index dc87e53..0e9bf9d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2014-12-08 Arnold D. Robbins <address@hidden>
+
+ * awk.h: Additional codes for the expression versions of print,
+ printf, and print_rec.
+ (do_print, do_printf, do_print_rec): Return NODE *, take additional
+ boolean parameter.
+ * awkgram.y (yylex): Improve scanner hack some.
+ (mk_print): Use the right opcodes.
+ * builtin.c (efwrite): Return ssize_t which is number of bytes written.
+ Add a parameter for if can fatal or not.
+ (do_print): Return NODE with number bytes written; fatal based
+ parameter setting.
+ (do_printf): Ditto.
+ (do_print_rec): Ditto.
+ * eval.c (optypetab): Update.
+ * interpret.h (r_interpret): Handle the different versions of
+ print/printf: expression vs. statement. This could be cleaned
+ up a bit more.
+
2014-12-06 Arnold D. Robbins <address@hidden>
Start on changes to allow print/printf to return a value.
diff --git a/awk.h b/awk.h
index a932b54..1c01b81 100644
--- a/awk.h
+++ b/awk.h
@@ -616,6 +616,9 @@ typedef enum opcodeval {
Op_K_print,
Op_K_print_rec,
Op_K_printf,
+ Op_K_print_exp,
+ Op_K_print_rec_exp,
+ Op_K_printf_exp,
Op_K_next,
Op_K_exit,
Op_K_return,
@@ -1345,15 +1348,15 @@ extern NODE *do_length(int nargs);
extern NODE *do_log(int nargs);
extern NODE *do_mktime(int nargs);
extern NODE *do_sprintf(int nargs);
-extern void do_printf(int nargs, int redirtype);
+extern NODE *do_printf(int nargs, int redirtype, bool can_fatal);
extern void print_simple(NODE *tree, FILE *fp);
extern NODE *do_sqrt(int nargs);
extern NODE *do_substr(int nargs);
extern NODE *do_strftime(int nargs);
extern NODE *do_systime(int nargs);
extern NODE *do_system(int nargs);
-extern void do_print(int nargs, int redirtype);
-extern void do_print_rec(int args, int redirtype);
+extern NODE *do_print(int nargs, int redirtype, bool can_fatal);
+extern NODE *do_print_rec(int args, int redirtype, bool can_fatal);
extern NODE *do_tolower(int nargs);
extern NODE *do_toupper(int nargs);
extern NODE *do_atan2(int nargs);
diff --git a/awkgram.c b/awkgram.c
index 00b2b0b..2daae93 100644
--- a/awkgram.c
+++ b/awkgram.c
@@ -5459,7 +5459,6 @@ yylex(void)
bool inhex = false;
bool intlstr = false;
AWKNUM d;
- int prev_lasttok;
#define GET_INSTRUCTION(op) bcalloc(op, 1, sourceline)
@@ -5492,7 +5491,6 @@ yylex(void)
}
#endif
- prev_lasttok = lasttok;
lexeme = lexptr;
thisline = NULL;
if (want_regexp) {
@@ -6248,20 +6246,23 @@ make_instruction:
yylval->builtin_idx = mid;
if (! do_traditional
&& (class == LEX_PRINT || class == LEX_PRINTF)) {
- switch (prev_lasttok) {
+ switch (lasttok) {
case NEWLINE:
case LEX_ELSE:
case ':': /* from case */
case ';':
case '{':
- case '(':
case ')':
break;
default:
- if (class == LEX_PRINT)
+ if (class == LEX_PRINT) {
class = LEX_PRINT_EXP;
- if (class == LEX_PRINTF)
+ yylval->opcode = Op_K_print_exp;
+ }
+ if (class == LEX_PRINTF) {
class = LEX_PRINTF_EXP;
+ yylval->opcode =
Op_K_printf_exp;
+ }
break;
}
}
@@ -8032,7 +8033,7 @@ mk_print(INSTRUCTION *op, INSTRUCTION *exp_list,
INSTRUCTION *redir)
* which is faster for these two cases.
*/
- if (op->opcode == Op_K_print &&
+ if ((op->opcode == Op_K_print || op->opcode == Op_K_print_exp) &&
(exp_list == NULL
|| (exp_list->lasti->opcode == Op_field_spec
&& exp_list->nexti->nexti->nexti ==
exp_list->lasti
@@ -8070,7 +8071,7 @@ mk_print(INSTRUCTION *op, INSTRUCTION *exp_list,
INSTRUCTION *redir)
}
op->expr_count = 0;
- op->opcode = Op_K_print_rec;
+ op->opcode = (op->opcode == Op_K_print ? Op_K_print_rec :
Op_K_print_rec_exp);
if (redir == NULL) { /* no redircetion */
op->redir_type = redirect_none;
ret = list_create(op);
diff --git a/awkgram.y b/awkgram.y
index bc529bb..191e4a6 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -3053,7 +3053,6 @@ yylex(void)
bool inhex = false;
bool intlstr = false;
AWKNUM d;
- int prev_lasttok;
#define GET_INSTRUCTION(op) bcalloc(op, 1, sourceline)
@@ -3086,7 +3085,6 @@ yylex(void)
}
#endif
- prev_lasttok = lasttok;
lexeme = lexptr;
thisline = NULL;
if (want_regexp) {
@@ -3842,20 +3840,23 @@ make_instruction:
yylval->builtin_idx = mid;
if (! do_traditional
&& (class == LEX_PRINT || class == LEX_PRINTF)) {
- switch (prev_lasttok) {
+ switch (lasttok) {
case NEWLINE:
case LEX_ELSE:
case ':': /* from case */
case ';':
case '{':
- case '(':
case ')':
break;
default:
- if (class == LEX_PRINT)
+ if (class == LEX_PRINT) {
class = LEX_PRINT_EXP;
- if (class == LEX_PRINTF)
+ yylval->opcode = Op_K_print_exp;
+ }
+ if (class == LEX_PRINTF) {
class = LEX_PRINTF_EXP;
+ yylval->opcode =
Op_K_printf_exp;
+ }
break;
}
}
@@ -5626,7 +5627,7 @@ mk_print(INSTRUCTION *op, INSTRUCTION *exp_list,
INSTRUCTION *redir)
* which is faster for these two cases.
*/
- if (op->opcode == Op_K_print &&
+ if ((op->opcode == Op_K_print || op->opcode == Op_K_print_exp) &&
(exp_list == NULL
|| (exp_list->lasti->opcode == Op_field_spec
&& exp_list->nexti->nexti->nexti ==
exp_list->lasti
@@ -5664,7 +5665,7 @@ mk_print(INSTRUCTION *op, INSTRUCTION *exp_list,
INSTRUCTION *redir)
}
op->expr_count = 0;
- op->opcode = Op_K_print_rec;
+ op->opcode = (op->opcode == Op_K_print ? Op_K_print_rec :
Op_K_print_rec_exp);
if (redir == NULL) { /* no redircetion */
op->redir_type = redirect_none;
ret = list_create(op);
diff --git a/builtin.c b/builtin.c
index 21c6ed5..ee86f9e 100644
--- a/builtin.c
+++ b/builtin.c
@@ -94,20 +94,24 @@ fatal(_("attempt to use array `%s' in a scalar context"),
array_vname(s1)); \
/* efwrite --- like fwrite, but with error checking */
-static void
+static ssize_t
efwrite(const void *ptr,
size_t size,
size_t count,
FILE *fp,
const char *from,
struct redirect *rp,
- bool flush)
+ bool flush,
+ bool can_fatal)
{
+ ssize_t write_count = 0;
+
errno = 0;
if (rp != NULL) {
- if (rp->output.gawk_fwrite(ptr, size, count, fp,
rp->output.opaque) != count)
+ write_count = rp->output.gawk_fwrite(ptr, size, count, fp,
rp->output.opaque);
+ if (write_count != count)
goto wrerror;
- } else if (fwrite(ptr, size, count, fp) != count)
+ } else if ((write_count = fwrite(ptr, size, count, fp)) != count)
goto wrerror;
if (flush
&& ((fp == stdout && output_is_tty)
@@ -122,17 +126,20 @@ efwrite(const void *ptr,
goto wrerror;
}
}
- return;
+ return write_count;
wrerror:
- /* die silently on EPIPE to stdout */
- if (fp == stdout && errno == EPIPE)
- gawk_exit(EXIT_FATAL);
+ if (can_fatal) {
+ /* die silently on EPIPE to stdout */
+ if (fp == stdout && errno == EPIPE)
+ gawk_exit(EXIT_FATAL);
- /* otherwise die verbosely */
- fatal(_("%s to \"%s\" failed (%s)"), from,
- rp ? rp->value : _("standard output"),
- errno ? strerror(errno) : _("reason unknown"));
+ /* otherwise die verbosely */
+ fatal(_("%s to \"%s\" failed (%s)"), from,
+ rp ? rp->value : _("standard output"),
+ errno ? strerror(errno) : _("reason unknown"));
+ }
+ return -1;
}
/* do_exp --- exponential function */
@@ -1627,14 +1634,15 @@ do_sprintf(int nargs)
/* do_printf --- perform printf, including redirection */
-void
-do_printf(int nargs, int redirtype)
+NODE *
+do_printf(int nargs, int redirtype, bool can_fatal)
{
FILE *fp = NULL;
NODE *tmp;
struct redirect *rp = NULL;
int errflg; /* not used, sigh */
NODE *redir_exp = NULL;
+ ssize_t count;
if (nargs == 0) {
if (do_traditional) {
@@ -1648,7 +1656,7 @@ do_printf(int nargs, int redirtype)
DEREF(redir_exp);
decr_sp();
}
- return; /* bwk accepts it silently */
+ return make_number(0.0); /* bwk accepts it
silently */
}
fatal(_("printf: no arguments"));
}
@@ -1673,14 +1681,16 @@ do_printf(int nargs, int redirtype)
if (tmp != NULL) {
if (fp == NULL) {
DEREF(tmp);
- return;
+ return make_number(0.0);
}
- efwrite(tmp->stptr, sizeof(char), tmp->stlen, fp, "printf", rp,
true);
+ count = efwrite(tmp->stptr, sizeof(char), tmp->stlen, fp,
"printf", rp, true, can_fatal);
if (rp != NULL && (rp->flag & RED_TWOWAY) != 0)
rp->output.gawk_fflush(rp->output.fp,
rp->output.opaque);
DEREF(tmp);
} else
gawk_exit(EXIT_FATAL);
+
+ return make_number((AWKNUM) count);
}
/* do_sqrt --- do the sqrt function */
@@ -2068,8 +2078,8 @@ do_system(int nargs)
/* do_print --- print items, separated by OFS, terminated with ORS */
-void
-do_print(int nargs, int redirtype)
+NODE *
+do_print(int nargs, int redirtype, bool can_fatal)
{
struct redirect *rp = NULL;
int errflg; /* not used, sigh */
@@ -2077,6 +2087,7 @@ do_print(int nargs, int redirtype)
int i;
NODE *redir_exp = NULL;
NODE *tmp = NULL;
+ ssize_t count;
assert(nargs <= max_args);
@@ -2116,34 +2127,38 @@ do_print(int nargs, int redirtype)
if (fp == NULL) {
for (i = nargs; i > 0; i--)
DEREF(args_array[i]);
- return;
+ return make_number(0.0);
}
+ count = 0;
for (i = nargs; i > 0; i--) {
- efwrite(args_array[i]->stptr, sizeof(char),
args_array[i]->stlen, fp, "print", rp, false);
+ count += efwrite(args_array[i]->stptr, sizeof(char),
args_array[i]->stlen, fp, "print", rp, false, can_fatal);
DEREF(args_array[i]);
if (i != 1 && OFSlen > 0)
- efwrite(OFS, sizeof(char), (size_t) OFSlen,
- fp, "print", rp, false);
+ count += efwrite(OFS, sizeof(char), (size_t) OFSlen,
+ fp, "print", rp, false, can_fatal);
}
if (ORSlen > 0)
- efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp,
true);
+ count += efwrite(ORS, sizeof(char), (size_t) ORSlen, fp,
"print", rp, true, can_fatal);
if (rp != NULL && (rp->flag & RED_TWOWAY) != 0)
rp->output.gawk_fflush(rp->output.fp, rp->output.opaque);
+
+ return make_number((AWKNUM) count);
}
/* do_print_rec --- special case printing of $0, for speed */
-void
-do_print_rec(int nargs, int redirtype)
+NODE *
+do_print_rec(int nargs, int redirtype, bool can_fatal)
{
FILE *fp = NULL;
NODE *f0;
struct redirect *rp = NULL;
int errflg; /* not used, sigh */
NODE *redir_exp = NULL;
+ ssize_t count;
assert(nargs == 0);
if (redirtype != 0) {
@@ -2157,7 +2172,7 @@ do_print_rec(int nargs, int redirtype)
fp = output_fp;
if (fp == NULL)
- return;
+ return make_number(0.0);
if (! field0_valid)
(void) get_field(0L, NULL); /* rebuild record */
@@ -2167,13 +2182,15 @@ do_print_rec(int nargs, int redirtype)
if (do_lint && (f0->flags & NULL_FIELD) != 0)
lintwarn(_("reference to uninitialized field `$%d'"), 0);
- efwrite(f0->stptr, sizeof(char), f0->stlen, fp, "print", rp, false);
+ count = efwrite(f0->stptr, sizeof(char), f0->stlen, fp, "print", rp,
false, can_fatal);
if (ORSlen > 0)
- efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp,
true);
+ count += efwrite(ORS, sizeof(char), (size_t) ORSlen, fp,
"print", rp, true, can_fatal);
if (rp != NULL && (rp->flag & RED_TWOWAY) != 0)
rp->output.gawk_fflush(rp->output.fp, rp->output.opaque);
+
+ return make_number((AWKNUM) count);
}
diff --git a/eval.c b/eval.c
index 9599270..bc13db2 100644
--- a/eval.c
+++ b/eval.c
@@ -321,6 +321,9 @@ static struct optypetab {
{ "Op_K_print", "print" },
{ "Op_K_print_rec", "print" },
{ "Op_K_printf", "printf" },
+ { "Op_K_print_exp", "print" },
+ { "Op_K_print_rec_exp", "print" },
+ { "Op_K_printf_exp", "printf" },
{ "Op_K_next", "next" },
{ "Op_K_exit", "exit" },
{ "Op_K_return", "return" },
diff --git a/interpret.h b/interpret.h
index 3a9cab3..6ef254e 100644
--- a/interpret.h
+++ b/interpret.h
@@ -51,6 +51,7 @@ r_interpret(INSTRUCTION *code)
Regexp *rp;
NODE *set_array = NULL; /* array with a post-assignment routine */
NODE *set_idx = NULL; /* the index of the array element */
+ bool print_fatal = true;
/* array subscript */
@@ -967,16 +968,40 @@ arrayfor:
PUSH(r);
break;
+ case Op_K_print_exp:
+ print_fatal = false;
+ /* fall through */
case Op_K_print:
- do_print(pc->expr_count, pc->redir_type);
+ r = do_print(pc->expr_count, pc->redir_type,
print_fatal);
+ print_fatal = true;
+ if (op == Op_K_print_exp)
+ PUSH(r);
+ else
+ DEREF(r);
break;
+ case Op_K_printf_exp:
+ print_fatal = false;
+ /* fall through */
case Op_K_printf:
- do_printf(pc->expr_count, pc->redir_type);
+ r = do_printf(pc->expr_count, pc->redir_type,
print_fatal);
+ print_fatal = true;
+ if (op == Op_K_printf_exp)
+ PUSH(r);
+ else
+ DEREF(r);
break;
+ case Op_K_print_rec_exp:
+ print_fatal = false;
+ /* fall through */
case Op_K_print_rec:
- do_print_rec(pc->expr_count, pc->redir_type);
+ r = do_print_rec(pc->expr_count, pc->redir_type,
print_fatal);
+ print_fatal = true;
+ if (op == Op_K_print_rec_exp)
+ PUSH(r);
+ else
+ DEREF(r);
break;
case Op_push_re:
-----------------------------------------------------------------------
Summary of changes:
ChangeLog | 19 ++++++++++++++
awk.h | 9 ++++--
awkgram.c | 17 +++++++------
awkgram.y | 17 +++++++------
builtin.c | 77 ++++++++++++++++++++++++++++++++++++-----------------------
eval.c | 3 ++
interpret.h | 31 +++++++++++++++++++++--
7 files changed, 121 insertions(+), 52 deletions(-)
hooks/post-receive
--
gawk
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gawk-diffs] [SCM] gawk branch, print-exp, updated. gawk-4.1.0-1003-g5d35746,
Arnold Robbins <=