[master] 3a263d9 use Python instead of awk

Guillaume Quintard guillaume at varnish-software.com
Wed Jun 8 12:17:06 CEST 2016


commit 3a263d940cf24d36a85cb835291c3062677dd87b
Author: Guillaume Quintard <guillaume at varnish-software.com>
Date:   Wed Jun 8 12:02:39 2016 +0200

    use Python instead of awk

diff --git a/doc/sphinx/Makefile.am b/doc/sphinx/Makefile.am
index 696b8ef..b9713d9 100644
--- a/doc/sphinx/Makefile.am
+++ b/doc/sphinx/Makefile.am
@@ -180,8 +180,8 @@ VTCSYN_SRC = $(top_srcdir)/bin/varnishtest/vtc.c \
 	     $(top_srcdir)/bin/varnishtest/vtc_http.c \
 	     $(top_srcdir)/bin/varnishtest/vtc_logexp.c \
 	     $(top_srcdir)/bin/varnishtest/vtc_varnish.c
-include/vtc-syntax.rst: vtc-syntax.awk $(VTCSYN_SRC)
-	awk -f $(top_srcdir)/doc/sphinx/vtc-syntax.awk $(VTCSYN_SRC) > $@
+include/vtc-syntax.rst: vtc-syntax.py $(VTCSYN_SRC)
+	$(PYTHON) $(top_srcdir)/doc/sphinx/vtc-syntax.py $(VTCSYN_SRC) > $@
 BUILT_SOURCES += include/vtc-syntax.rst
 
 .PHONY: reference
diff --git a/doc/sphinx/vtc-syntax.awk b/doc/sphinx/vtc-syntax.awk
deleted file mode 100644
index 58ce4ad..0000000
--- a/doc/sphinx/vtc-syntax.awk
+++ /dev/null
@@ -1,43 +0,0 @@
-# end of paragraph if we hit '*/'
-$0 ~ "*/" {
-	p = 0;
-}
-
-# if in paragraph and no section is announced,
-# concatenate
-p && $0 !~ "[ /]* SECTION: " {
-	cl[section] = cl[section]  gensub(/ \* ?/, "", "1", $0) "\n";
-}
-
-# section announcement
-$0 ~ "[ /]* SECTION: " {
-	section = $3;
-	sl[len++] = section;
-	if ($4) {
-		tl[section] = gensub(/[\t ]*\/?\* SECTION: [^ ]+ +/, "", "1", $0);
-	} else {
-		tl[section] = "";
-	}
-	p = 1;
-}
-
-# sort sections, underline titles, print
-END {
-	asort(sl);
-	for (i in sl) {
-		section = sl[i]
-		print(tl[section]);
-		a = section
-		c = gsub(/\./, "", a);
-		if (c == 0)
-			r = "-";
-		else if (c == 1)
-			r = "~"
-		else if (c == 2)
-			r = "."
-		else
-			r = "."
-		print(gensub(/./, r, "g", tl[section]));
-		print(cl[section]);
-	}
-}
diff --git a/doc/sphinx/vtc-syntax.py b/doc/sphinx/vtc-syntax.py
new file mode 100644
index 0000000..f0601b3
--- /dev/null
+++ b/doc/sphinx/vtc-syntax.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+#-
+# Copyright (c) 2006-2016 Varnish Software AS
+# All rights reserved.
+#
+# Author: Guillaume Quintard <guillaume.quintard at gmail.com>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# Generate various .c and .h files for the VSL query expression parser
+# and the interfaces for it.
+
+from __future__ import print_function
+import sys;
+import re;
+
+def parse_file(fn):
+	p = False
+	section = ""
+	resec = re.compile("[ /]\* SECTION: ")
+	cl = {}
+	tl = {}
+	sl = []
+
+	f = open(fn, 'r')
+
+	for l in f:
+		if "*/" in l:
+			p = 0
+		if resec.match(l):
+			a = l.split()
+			section = a[2]
+			sl.append(section)
+			cl[section] = []
+			if len(a) > 3:
+				tl[section] = re.sub(r'^[\t ]*\/?\* SECTION: [^ ]+ +',
+						"", l)
+			else:
+				tl[section] = ""
+			p = 1
+		elif p:
+			cl[section].append(re.sub(r'^ \* ?',"", l))
+	f.close()
+
+	sl.sort()
+	for section in sl:
+		print(tl[section], end='');
+		a = section
+		c = section.count('.')
+		if c == 0:
+			r = "-"
+		elif c ==1: 
+			r = "~"
+		else:
+			r = "."
+		print(re.sub(r'.', r, tl[section]), end='')
+		print("".join(cl[section]))
+
+if __name__ == "__main__":
+	for fn in sys.argv[1:]:
+		parse_file(fn)



More information about the varnish-commit mailing list