Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Sep 2017 05:47:55 +0000 (UTC)
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r323576 - head/contrib/one-true-awk
Message-ID:  <201709140547.v8E5ltM3033554@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: imp
Date: Thu Sep 14 05:47:55 2017
New Revision: 323576
URL: https://svnweb.freebsd.org/changeset/base/323576

Log:
  Bring in bit operation functions, ala gawk.
  
  These are from OpenBSD:
  >>> Extend awk with bitwise operations. This is an extension to the awk
  >>> spec and documented as such, but comes in handy from time to time.
  >>> The prototypes make it compatible with a similar GNU awk extension.
  >>>
  >>> ok millert@, enthusiasm from deraadt@
  
  Edited to fix cut and paste in error messages, as well as
  using tabs instead of spaces after #defines added.
  
  Obtained From: OpenBSD awk.h 1.12, lex.c 1.10, run.c 1.29
  Differential Revision: https://reviews.freebsd.org/D12361
  Sponsored by: Netflix

Modified:
  head/contrib/one-true-awk/awk.h
  head/contrib/one-true-awk/lex.c
  head/contrib/one-true-awk/run.c

Modified: head/contrib/one-true-awk/awk.h
==============================================================================
--- head/contrib/one-true-awk/awk.h	Thu Sep 14 04:51:17 2017	(r323575)
+++ head/contrib/one-true-awk/awk.h	Thu Sep 14 05:47:55 2017	(r323576)
@@ -126,6 +126,12 @@ extern Cell	*rlengthloc;	/* RLENGTH */
 #define	FTOUPPER 12
 #define	FTOLOWER 13
 #define	FFLUSH	14
+#define	FAND	15
+#define	FFOR	16
+#define	FXOR	17
+#define	FCOMPL	18
+#define	FLSHIFT	19
+#define	FRSHIFT	20
 
 /* Node:  parse tree is made of nodes, with Cell's at bottom */
 

Modified: head/contrib/one-true-awk/lex.c
==============================================================================
--- head/contrib/one-true-awk/lex.c	Thu Sep 14 04:51:17 2017	(r323575)
+++ head/contrib/one-true-awk/lex.c	Thu Sep 14 05:47:55 2017	(r323576)
@@ -47,9 +47,11 @@ Keyword keywords[] ={	/* keep sorted: binary searched 
 	{ "BEGIN",	XBEGIN,		XBEGIN },
 	{ "END",	XEND,		XEND },
 	{ "NF",		VARNF,		VARNF },
+	{ "and",	FAND,		BLTIN },
 	{ "atan2",	FATAN,		BLTIN },
 	{ "break",	BREAK,		BREAK },
 	{ "close",	CLOSE,		CLOSE },
+	{ "compl",	FCOMPL,		BLTIN },
 	{ "continue",	CONTINUE,	CONTINUE },
 	{ "cos",	FCOS,		BLTIN },
 	{ "delete",	DELETE,		DELETE },
@@ -69,13 +71,16 @@ Keyword keywords[] ={	/* keep sorted: binary searched 
 	{ "int",	FINT,		BLTIN },
 	{ "length",	FLENGTH,	BLTIN },
 	{ "log",	FLOG,		BLTIN },
+	{ "lshift",	FLSHIFT,	BLTIN },
 	{ "match",	MATCHFCN,	MATCHFCN },
 	{ "next",	NEXT,		NEXT },
 	{ "nextfile",	NEXTFILE,	NEXTFILE },
+	{ "or",		FFOR,		BLTIN },
 	{ "print",	PRINT,		PRINT },
 	{ "printf",	PRINTF,		PRINTF },
 	{ "rand",	FRAND,		BLTIN },
 	{ "return",	RETURN,		RETURN },
+	{ "rshift",	FRSHIFT,	BLTIN },
 	{ "sin",	FSIN,		BLTIN },
 	{ "split",	SPLIT,		SPLIT },
 	{ "sprintf",	SPRINTF,	SPRINTF },
@@ -87,6 +92,7 @@ Keyword keywords[] ={	/* keep sorted: binary searched 
 	{ "tolower",	FTOLOWER,	BLTIN },
 	{ "toupper",	FTOUPPER,	BLTIN },
 	{ "while",	WHILE,		WHILE },
+	{ "xor",	FXOR,		BLTIN },
 };
 
 #define	RET(x)	{ if(dbg)printf("lex %s\n", tokname(x)); return(x); }

Modified: head/contrib/one-true-awk/run.c
==============================================================================
--- head/contrib/one-true-awk/run.c	Thu Sep 14 04:51:17 2017	(r323575)
+++ head/contrib/one-true-awk/run.c	Thu Sep 14 05:47:55 2017	(r323576)
@@ -1516,6 +1516,64 @@ Cell *bltin(Node **a, int n)	/* builtin functions. a[0
 			nextarg = nextarg->nnext;
 		}
 		break;
+	case FCOMPL:
+		u = ~((int)getfval(x));
+		break;
+	case FAND:
+		if (nextarg == 0) {
+			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;
+		break;
+	case FFOR:
+		if (nextarg == 0) {
+			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;
+		break;
+	case FXOR:
+		if (nextarg == 0) {
+			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;
+		break;
+	case FLSHIFT:
+		if (nextarg == 0) {
+			WARNING("lshift 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;
+		break;
+	case FRSHIFT:
+		if (nextarg == 0) {
+			WARNING("rshift 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;
+		break;
 	case FSYSTEM:
 		fflush(stdout);		/* in case something is buffered already */
 		u = (Awkfloat) system(getsval(x)) / 256;   /* 256 is unix-dep */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201709140547.v8E5ltM3033554>