Author: imp
Date: Thu Sep 14 05:48:23 2017
New Revision: 323577
URL: https://svnweb.freebsd.org/changeset/base/323577

Log:
  Implement gawk multiple-arg extension to and, or, and xor.
  
  gawk allows multiple arguemnts to bit-wiste and, or and xor
  functions. Implement an arbitrary number of arguments for these
  functions. Also, use NULL in preference to 0 to match rest of file.
  
  Sponsored by: Netflix
  Differential Revision: https://reviews.freebsd.org/D12361

Modified:
  head/contrib/one-true-awk/run.c
  head/usr.bin/awk/awk.1

Modified: head/contrib/one-true-awk/run.c
==============================================================================
--- head/contrib/one-true-awk/run.c     Thu Sep 14 05:47:55 2017        
(r323576)
+++ head/contrib/one-true-awk/run.c     Thu Sep 14 05:48:23 2017        
(r323577)
@@ -1476,7 +1476,7 @@ Cell *bltin(Node **a, int n)      /* builtin functions. 
a[0
 {
        Cell *x, *y;
        Awkfloat u;
-       int t;
+       int t, i;
        Awkfloat tmp;
        char *p, *buf;
        Node *nextarg;
@@ -1520,40 +1520,52 @@ Cell *bltin(Node **a, int n)    /* builtin functions. 
a[0
                u = ~((int)getfval(x));
                break;
        case FAND:
-               if (nextarg == 0) {
+               if (nextarg == NULL) {
                        WARNING("and requires two arguments; returning 0");
                        u = 0;
                        break;
                }
-               y = execute(a[1]->nnext);
-               u = ((int)getfval(x)) & ((int)getfval(y));
-               tempfree(y);
-               nextarg = nextarg->nnext;
+               i = ((int)getfval(x));
+               while (nextarg != NULL) {
+                       y = execute(nextarg);
+                       i &= (int)getfval(y);
+                       tempfree(y);
+                       nextarg = nextarg->nnext;
+               }
+               u = i;
                break;
        case FFOR:
-               if (nextarg == 0) {
+               if (nextarg == NULL) {
                        WARNING("or requires two arguments; returning 0");
                        u = 0;
                        break;
                }
-               y = execute(a[1]->nnext);
-               u = ((int)getfval(x)) | ((int)getfval(y));
-               tempfree(y);
-               nextarg = nextarg->nnext;
+               i = ((int)getfval(x));
+               while (nextarg != NULL) {
+                       y = execute(nextarg);
+                       i |= (int)getfval(y);
+                       tempfree(y);
+                       nextarg = nextarg->nnext;
+               }
+               u = i;
                break;
        case FXOR:
-               if (nextarg == 0) {
+               if (nextarg == NULL) {
                        WARNING("xor requires two arguments; returning 0");
                        u = 0;
                        break;
                }
-               y = execute(a[1]->nnext);
-               u = ((int)getfval(x)) ^ ((int)getfval(y));
-               tempfree(y);
-               nextarg = nextarg->nnext;
+               i = ((int)getfval(x));
+               while (nextarg != NULL) {
+                       y = execute(nextarg);
+                       i ^= (int)getfval(y);
+                       tempfree(y);
+                       nextarg = nextarg->nnext;
+               }
+               u = i;
                break;
        case FLSHIFT:
-               if (nextarg == 0) {
+               if (nextarg == NULL) {
                        WARNING("lshift requires two arguments; returning 0");
                        u = 0;
                        break;
@@ -1564,7 +1576,7 @@ Cell *bltin(Node **a, int n)      /* builtin functions. 
a[0
                nextarg = nextarg->nnext;
                break;
        case FRSHIFT:
-               if (nextarg == 0) {
+               if (nextarg == NULL) {
                        WARNING("rshift requires two arguments; returning 0");
                        u = 0;
                        break;

Modified: head/usr.bin/awk/awk.1
==============================================================================
--- head/usr.bin/awk/awk.1      Thu Sep 14 05:47:55 2017        (r323576)
+++ head/usr.bin/awk/awk.1      Thu Sep 14 05:48:23 2017        (r323577)
@@ -690,12 +690,15 @@ and returns its exit status.
 .Bl -tag -width "lshift(a, b)"
 .It Fn compl x
 Returns the bitwise complement of integer argument x.
-.It Fn and x y
-Performs a bitwise AND on integer arguments x and y.
-.It Fn or x y
-Performs a bitwise OR on integer arguments x and y.
-.It Fn xor x y
-Performs a bitwise Exclusive-OR on integer arguments x and y.
+.It Fn and v1 v2 ...
+Performs a bitwise AND on all arguments provided, as integers.
+There must be at least two values.
+.It Fn or v1 v2 ...
+Performs a bitwise OR on all arguments provided, as integers.
+There must be at least two values.
+.It Fn xor v1 v2 ...
+Performs a bitwise Exclusive-OR on all arguments provided, as integers.
+There must be at least two values.
 .It Fn lshift x n
 Returns integer argument x shifted by n bits to the left.
 .It Fn rshift x n
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to