]> git.saurik.com Git - bison.git/blame - examples/extexi
maint: fix distcheck.
[bison.git] / examples / extexi
CommitLineData
1c59e0a1
AD
1# Extract all examples from the manual source. -*- AWK -*-
2
b34d96c1 3# This file is part of GNU Bison
7d424de1 4
34136e65 5# Copyright (C) 1992, 2000-2001, 2005-2006, 2009-2012 Free Software
35905f2b 6# Foundation, Inc.
1c59e0a1 7#
f16b0819 8# This program is free software: you can redistribute it and/or modify
1c59e0a1 9# it under the terms of the GNU General Public License as published by
f16b0819 10# the Free Software Foundation, either version 3 of the License, or
1c59e0a1
AD
11# (at your option) any later version.
12#
13# This program 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.
17#
18# You should have received a copy of the GNU General Public License
f16b0819 19# along with this program. If not, see <http://www.gnu.org/licenses/>.
1c59e0a1 20
b34d96c1
PE
21# This script is for use with any Awk that conforms to POSIX.
22# It was derived from a similar script tests/generate.awk in GNU m4.
1c59e0a1
AD
23#
24# Usage: extexi input-file.texi ... -- [FILES to extract]
25BEGIN {
26 if (!output_dir)
27 output_dir = ".";
28 for (argc = 1; argc < ARGC; ++argc)
29 if (ARGV[argc] == "--")
30 break;
31 for (i = argc + 1; i < ARGC; ++i)
2191bb74 32 file_wanted[basename(ARGV[i])] = ARGV[i];
1c59e0a1
AD
33 ARGC = argc;
34}
35
36/^@node / {
37 if (seq > 0)
38 print "AT_CLEANUP";
39
40 split ($0, tmp, ",");
41 node = substr(tmp[1], 7);
42 seq = 0;
43}
44
45/^@comment file: / {
2191bb74
AD
46 if (file = file_wanted[$3])
47 message(" GEN " file);
1c59e0a1 48 else
2191bb74 49 message("SKIP " $3);
1c59e0a1
AD
50}
51
f9c75dd0 52/^@(small)?example$/, /^@end (small)?example$/ {
1c59e0a1
AD
53 if (!file)
54 next;
55
f9c75dd0 56 if ($0 ~ /^@(small)?example$/)
1c59e0a1
AD
57 {
58 input = files_output[file] ? "\n" : "";
59
60 # FNR is starting at 0 instead of 1, and
61 # #line report the line number of the *next* line.
62 # => + 2.
63 # Note that recent Bison support it, but not Flex.
5215c87f 64 if (file ~ /\.[chy]*$/)
e9690142 65 input = "#line " (FNR + 1) " \"" FILENAME "\"\n";
1c59e0a1
AD
66 next;
67 }
68
f9c75dd0 69 if ($0 ~ /^@end (small)?example$/)
1c59e0a1
AD
70 {
71 if (input == "")
e9690142 72 fatal("no contents: " file);
1c59e0a1
AD
73
74 input = normalize(input);
75 # No spurious end of line: use printf.
76 if (files_output[file])
e9690142 77 # The parens around the output file seem to be required
3cf084ec
AD
78 # by awk on Mac OS X Tiger (darwin 8.4.6).
79 printf ("%s", input) >> (output_dir "/" file);
1c59e0a1 80 else
e9690142 81 printf ("%s", input) > (output_dir "/" file);
1c59e0a1
AD
82 close (output_dir "/" file);
83 files_output[file] = 1;
84
85 file = input = "";
86 next;
87 }
88
89 input = input $0 "\n";
90}
91
92
93# We have to handle CONTENTS line per line, since anchors in AWK are
94# referring to the whole string, not the lines.
95function normalize(contents, i, lines, n, line, res) {
96 # Remove the Texinfo tags.
97 n = split (contents, lines, "\n");
98 # We don't want the last field which empty: it's behind the last \n.
99 for (i = 1; i < n; ++i)
100 {
101 line = lines[i];
102
103 # Whole line commands.
104 if (line ~ /^@(c |comment|dots|end (ignore|group)|ignore|group)/)
e9690142
JD
105 # Gperf accepts empty lines as valid input!!!
106 if (file ~ /\.gperf$/)
107 continue;
108 else
109 line = "";
1c59e0a1 110
e6e704dc 111 gsub (/"@value\{VERSION\}"/, "\"" VERSION "\"", line)
1c59e0a1
AD
112 gsub (/^@result\{\}/, "", line);
113 gsub (/^@error\{\}/, "", line);
114 gsub ("@[{]", "{", line);
115 gsub ("@}", "}", line);
116 gsub ("@@", "@", line);
117 gsub ("@comment.*", "", line);
118
119 res = res line "\n";
120 }
121 return res;
122}
123
124
2191bb74
AD
125function basename(name, a, n) {
126 n = split (name, a, "/");
127 return a[n];
128}
129
1c59e0a1 130function message(msg) {
f938a7e7
PE
131 if (! message_printed[msg])
132 {
133 print "extexi: " msg > "/dev/stderr";
134 message_printed[msg] = 1;
135 }
1c59e0a1
AD
136}
137
138function fatal(msg) {
139 message(msg);
140 exit 1
141}