maint: bump copyright year
[m4/ericb.git] / tests / generate.awk
blobf373d7f380a5887d13c8d3dc582910a364d53eaa
1 # Extract all examples from the manual source. -*- AWK -*-
3 # Copyright (C) 1992, 2000-2001, 2006-2010, 2013-2014, 2017 Free
4 # Software Foundation, Inc.
6 # This file is part of GNU M4.
8 # GNU M4 is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # GNU M4 is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # This script is for use with any New AWK.
23 BEGIN {
24 seq = -1;
25 status = xfail = examples = 0;
26 file = options = "";
27 print "# This file is part of the GNU m4 test suite. -*- Autotest -*-";
28 # I don't know how to get this file's name, so it's hard coded :(
29 print "# Do not edit by hand, it was generated by generate.awk.";
30 print "#";
31 print "# Copyright (C) 1992, 2000, 2001, 2006, 2007, 2008, 2009 Free";
32 print "# Software Foundation, Inc.";
33 print ;
34 print "AT_BANNER([Documentation examples.])";
35 print ;
36 print ;
39 /^@node / { # Start a new test group.
40 if (seq > 0)
41 print "AT_CLEANUP";
43 split ($0, tmp, ",");
44 node = substr(tmp[1], 7);
45 seq = 0;
48 /^@comment file: / { # Produce a data file instead of a test.
49 file = $3;
52 /^@comment options: / { # Pass additional options to m4.
53 options = $0;
54 gsub ("@comment options:", "", options);
57 /^@comment xfail$/ { # Expect the test to fail.
58 xfail = 1;
61 /^@comment examples$/ { # The test uses files from the examples dir.
62 examples = 1;
65 /^@comment ignore$/ { # This is just formatted doc text, not an actual test.
66 getline;
67 status = xfail = examples = 0;
68 options = file = "";
69 next;
72 /^@comment status: / { # Expected exit status of a test.
73 status = $3;
76 /^@example$/, /^@end example$/ { # The body of the test.
77 if (seq < 0)
78 next;
80 if ($0 ~ /^@example$/)
82 if (seq == 0)
83 new_group(node);
84 seq++;
85 printf ("echo \"$at_srcdir/%s:%d:\"\n", FILENAME, NR)
86 next;
89 if ($0 ~ /^@end example$/)
91 if (file != "")
93 if (output || error)
95 fatal("while getting file " file \
96 " found output = " output "," \
97 " found error = " error);
99 input = normalize(input);
100 printf ("AT_DATA([[%s]],\n[[%s]])\n\n", file, input);
102 else
104 new_test(input, status, output, error, options, xfail, examples);
106 status = xfail = examples = 0;
107 file = input = output = error = options = "";
108 next;
111 if ($0 ~ /^\^D$/)
112 next;
113 if ($0 ~ /^\$ @kbd/)
114 next;
116 if ($0 ~ /^@result\{\}/)
117 output = output $0 "\n";
118 else if ($0 ~ /^@error\{\}/)
119 error = error $0 "\n";
120 else
121 input = input $0 "\n";
124 END {
125 if (seq > 0)
126 print "AT_CLEANUP";
129 # We have to handle CONTENTS line per line, since anchors in AWK are
130 # referring to the whole string, not the lines.
131 function normalize(contents, i, lines, n, line, res) {
132 # Remove the Texinfo tags.
133 n = split (contents, lines, "\n");
134 # We don't want the last field which empty: it's behind the last \n.
135 for (i = 1; i < n; ++i)
137 line = lines[i];
138 gsub ("^@result[{]}", "", line);
139 gsub ("^@error[{]}", "", line);
140 gsub ("@[{]", "{", line);
141 gsub ("@}", "}", line);
142 gsub ("@@", "@", line);
143 gsub ("@tabchar[{]}", "\t", line);
144 gsub ("@w[{] }", " @\\&t@", line);
145 gsub ("m4_", "m@\\&t@4_", line);
147 # Some of the examples have improperly balanced square brackets.
148 gsub ("[[]", "@<:@", line);
149 gsub ("[]]", "@:>@", line);
151 res = res line "\n";
153 return res;
156 function new_group(node) {
157 banner = node ". ";
158 gsub (".", "-", banner);
159 printf ("\n\n");
160 printf ("## %s ##\n", banner);
161 printf ("## %s. ##\n", node);
162 printf ("## %s ##\n", banner);
163 printf ("\n");
164 printf ("AT_SETUP([%s])\n", node);
165 printf ("AT_KEYWORDS([[documentation]])\n\n");
168 function new_test(input, status, output, error, options, xfail, examples) {
169 input = normalize(input);
170 output = normalize(output);
171 error = normalize(error);
173 if (error == "ignore\n")
174 error = "ignore";
176 if (options ~ / mpeval/)
177 printf ("AT_CHECK_GMP\n");
178 if (xfail == 1)
179 printf ("AT_XFAIL_IF([:])\n");
181 if (examples == 1)
183 printf ("AT_DATA([expout1],\n[[%s]])\n", output);
184 printf ("$SED -e \"s|doc/examples|$abs_top_srcdir/doc/examples|g\" \\\n");
185 printf (" < expout1 > expout\n\n");
186 if (error)
188 printf ("AT_DATA([experr1],\n[[%s]])\n", error);
189 printf ("$SED \"s|doc/examples|$abs_top_srcdir/doc/examples|g\" \\\n");
190 printf (" < experr1 > experr\n\n");
192 options = options " -I\"$abs_top_srcdir/doc/examples\"";
195 printf ("AT_DATA([[input.m4]],\n[[%s]])\n\n", input);
196 # Some of these tests `include' files from tests/.
197 printf ("AT_CHECK_M4([[%s]], %s,", options, status);
198 if (examples == 1)
199 printf ("\n[expout]");
200 else if (output)
201 printf ("\n[[%s]]", output);
202 else
203 printf (" []");
204 if (examples == 1 && error)
205 printf (",\n[experr]");
206 else if (error)
207 printf (",\n[[%s]]", error);
208 else
209 printf (", []");
210 printf (", [[input.m4]])\n\n");
213 function fatal(msg) {
214 print "generate.awk: " FILENAME ":" NR ": " msg > "/dev/stderr"
215 exit 1