1# SYNOPSIS
2#
3#   AX_AT_CHECK_PATTERN(COMMANDS, [STATUS], [STDOUT-RE], [STDERR-RE], [RUN-IF-FAIL], [RUN-IF-PASS])
4#   AX_AT_DIFF_PATTERN(PATTERN-FILE, TEST-FILE, [STATUS=0], [DIFFERENCES])
5#   AX_AT_DATA_CHECK_PATTERN_AWK(FILENAME)
6#
7# DESCRIPTION
8#
9#   AX_AT_CHECK_PATTERN() executes a test similar to AT_CHECK(), except
10#   that stdout and stderr are awk regular expressions (REs).
11#
12#   NOTE: as autoconf uses [] for quoting, the use of [brackets] in the RE
13#   arguments STDOUT-RE and STDERR-RE can be awkward and require careful
14#   extra quoting, or quadrigraphs '@<:@' (for '[') and '@:>@' (for ']').
15#
16#   awk is invoked via $AWK, which defaults to "awk" if unset or empty.
17#
18#   Implemented using AT_CHECK() with a custom value for $at_diff that
19#   invokes diff with an awk post-processor.
20#
21#
22#   AX_AT_DIFF_PATTERN() checks that the PATTERN-FILE applies to TEST-FILE.
23#   If there are differences, STATUS will be 1 and they should be DIFFERENCES.
24#
25#
26#   AX_AT_DATA_CHECK_PATTERN_AWK() creates FILENAME with the contents of
27#   the awk script used.
28#
29#
30#   The latest version of this macro and a supporting test suite are at:
31#   https://github.com/lukem/pyrediff
32#
33#
34# LICENSE
35#
36#   Copyright (c) 2013-2017 Luke Mewburn <luke@mewburn.net>
37#
38#   Copying and distribution of this file, with or without modification,
39#   are permitted in any medium without royalty provided the copyright
40#   notice and this notice are preserved.  This file is offered as-is,
41#   without any warranty.
42
43#serial 13
44
45m4_define([_AX_AT_CHECK_PATTERN_AWK],
46[[BEGIN { exitval=0 }
47
48function set_mode(m)
49{
50	mode=m
51	lc=0
52	rc=0
53}
54
55function mismatch()
56{
57	print mode
58	for (i = 0; i < lc; i++) {
59		print ll@<:@i@:>@
60	}
61	print "---"
62	for (i = 0; i < rc; i++) {
63		print rl@<:@i@:>@
64	}
65	set_mode("")
66	exitval=1
67}
68
69function change_mode(m)
70{
71	if (lc > rc) {
72		mismatch()
73	}
74	set_mode(m)
75}
76
77@S|@1 ~ /^@<:@0-9@:>@+(,@<:@0-9@:>@+)?@<:@ad@:>@@<:@0-9@:>@+(,@<:@0-9@:>@+)?@S|@/ {
78	change_mode("")
79	print
80	exitval=1
81	next
82}
83
84@S|@1 ~ /^@<:@0-9@:>@+(,@<:@0-9@:>@+)?@<:@c@:>@@<:@0-9@:>@+(,@<:@0-9@:>@+)?@S|@/ {
85	change_mode(@S|@1)
86	next
87}
88
89mode == "" {
90	print @S|@0
91	next
92}
93
94@S|@1 == "<" {
95	ll@<:@lc@:>@ = @S|@0
96	lc = lc + 1
97	next
98}
99
100@S|@1 == "---" {
101	next
102}
103
104@S|@1 == ">" {
105	rl@<:@rc@:>@ = @S|@0
106	rc = rc + 1
107	if (rc > lc) {
108		mismatch()
109		next
110	}
111	pat = "^" substr(ll@<:@rc-1@:>@, 3) "@S|@"
112	str = substr(@S|@0, 3)
113	if (str !~ pat) {
114		mismatch()
115	}
116	next
117}
118
119{
120	print "UNEXPECTED LINE: " @S|@0
121	exit 10
122}
123
124END {
125	change_mode("")
126	exit exitval
127}
128]])
129
130
131m4_defun([_AX_AT_CHECK_PATTERN_PREPARE], [dnl
132dnl Can't use AC_PROG_AWK() in autotest.
133AS_VAR_IF([AWK], [], [AWK=awk])
134
135AS_REQUIRE_SHELL_FN([ax_at_diff_pattern],
136  [AS_FUNCTION_DESCRIBE([ax_at_diff_pattern], [PATTERN OUTPUT],
137    [Diff PATTERN OUTPUT and elide change lines where the RE pattern matches])],
138  [diff "$[]1" "$[]2" | $AWK '_AX_AT_CHECK_PATTERN_AWK'])
139])dnl _AX_AT_CHECK_PATTERN_PREPARE
140
141
142m4_defun([AX_AT_CHECK_PATTERN], [dnl
143AS_REQUIRE([_AX_AT_CHECK_PATTERN_PREPARE])
144_ax_at_check_pattern_prepare_original_at_diff="$at_diff"
145at_diff='ax_at_diff_pattern'
146AT_CHECK(m4_expand([$1]), [$2], m4_expand([$3]), m4_expand([$4]),
147        [at_diff="$_ax_at_check_pattern_prepare_original_at_diff";$5],
148        [at_diff="$_ax_at_check_pattern_prepare_original_at_diff";$6])
149])dnl AX_AT_CHECK_PATTERN
150
151
152m4_defun([AX_AT_DIFF_PATTERN], [dnl
153AS_REQUIRE([_AX_AT_CHECK_PATTERN_PREPARE])
154AT_CHECK([ax_at_diff_pattern $1 $2], [$3], [$4])
155])dnl AX_AT_DIFF_PATTERN
156
157
158m4_defun([AX_AT_DATA_CHECK_PATTERN_AWK], [dnl
159m4_if([$1], [], [m4_fatal([$0: argument 1: empty filename])])dnl
160AT_DATA([$1], [dnl
161# check_pattern.awk
162# Generated by AX_AT_DATA_CHECK_PATTERN_AWK()
163# from https://github.com/lukem/pyrediff
164#
165# awk script to process the output of "diff PATTERN OUTPUT" removing lines
166# where the difference is a PATTERN line that exactly matches an OUTPUT line.
167#
168#
169# Copyright (c) 2013-2017 Luke Mewburn <luke@mewburn.net>
170#
171# Copying and distribution of this file, with or without modification,
172# are permitted in any medium without royalty provided the copyright
173# notice and this notice are preserved.  This file is offered as-is,
174# without any warranty.
175#
176]
177_AX_AT_CHECK_PATTERN_AWK)
178])dnl AX_AT_DATA_CHECK_PATTERN_AWK
179