Imported Upstream version 1.4.19
[platform/upstream/m4.git] / checks / get-them
1 #!/bin/sh
2 # -*- AWK -*-
3 # Extract all examples from the manual source.
4 # Copyright (C) 1992, 2005-2014, 2016-2017, 2020-2021 Free Software
5 # Foundation, Inc.
6 #
7 # This file is part of GNU M4.
8 #
9 # GNU M4 is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # GNU M4 is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22 # This script was designed under GNU awk, but hopefully portable to
23 # other implementations.
24
25 FILE=${1-/dev/null}
26 : ${AWK=awk}
27
28 $AWK '
29
30 BEGIN {
31   node = "";
32   seq = -1;
33   count = 0;
34   file = "NONE";
35   status = 0;
36   options = "";
37   xout = "";
38   xerr = "";
39 }
40
41 /^@node / {
42   if (seq > 0)
43     printf(" --  %d file%s", seq, seq == 1 ? "" : "s");
44   if (seq >= 0)
45     printf("\n");
46
47   split($0, tmp, ",");
48   node = substr(tmp[1], 7);
49   if (length(node) > 10)
50     printf("Node: %s - truncated", node);
51   else
52     printf("Node: %s ", node);
53   gsub(" ", "_", node);
54   node = tolower(substr(node, 1, 10));
55   seq = 0;
56 }
57
58 /^@comment ignore$/ {
59   getline;
60   status = 0;
61   options = "";
62   xout = "";
63   xout = "";
64   next;
65 }
66
67 /^@comment status: / {
68   status = $3;
69 }
70
71 /^@comment options: / {
72   options = $0;
73   gsub ("@comment options:", "", options);
74 }
75
76 /^@comment xout: / {
77   xout = $0;
78   gsub ("@comment xout: ", "", xout);
79 }
80
81 /^@comment xerr: / {
82   xerr = $0;
83   gsub ("@comment xerr: ", "", xerr);
84 }
85
86 /^@example$/, /^@end example$/ {
87   if (seq < 0)
88     next;
89   if ($0 ~ /^@example$/) {
90     if (count > 0)
91       close (file);
92     seq++;
93     count++;
94     file = sprintf("%03d.%s", count, node);
95     printf("dnl @ %s:%d: Origin of test\n"\
96            "dnl @ expected status: %d\n"\
97            "dnl @ extra options: %s\n"\
98            "dnl @ Copyright (C) 2006, 2007, 2008, 2009 Free Software\n"\
99            "dnl @ Foundation, Inc.\n"\
100            "dnl @ This file is free software; the Free Software Foundation\n"\
101            "dnl @ gives unlimited permission to copy and/or distribute it\n"\
102            "dnl @ with or without modifications, as long as this notice\n"\
103            "dnl @ is preserved.\n", FILENAME, NR, status, options) > file;
104     if (xout)
105         printf("dnl @ expected output: %s\n", xout) > file;
106     if (xerr)
107         printf("dnl @ expected error: %s\n", xerr) > file;
108     status = 0;
109     options = "";
110     xout = "";
111     xerr = "";
112     next;
113   }
114   if ($0 ~ /^@end example$/) {
115     next;
116   }
117   if ($0 ~ /^\^D$/)
118     next;
119   if ($0 ~ /^\$ @kbd/)
120     next;
121   if ($0 ~ /^@result\{\}/ || $0 ~ /^@error\{\}/)
122     prefix = "dnl ";
123   else
124     prefix = "";
125   gsub("@@", "@", $0);
126   gsub("@[{]", "{", $0);
127   gsub("@}", "}", $0);
128   gsub("@w[{] }", " ", $0);
129   gsub("@tabchar[{]}", "\t", $0);
130   printf("%s%s\n", prefix, $0) >> file;
131 }
132
133 END {
134   printf("\n");
135   if (count > 0)
136     close(file);
137 }
138 ' $FILE