8f953b727110bb30406e4117d0a72faa991a09e7
[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-2011 Free Software Foundation, Inc.
5 #
6 # This file is part of GNU M4.
7 #
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.
12 #
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.
17 #
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/>.
20
21 # This script was designed under GNU awk, but hopefully portable to
22 # other implementations.
23
24 FILE=${1-/dev/null}
25 : ${AWK=awk}
26
27 $AWK '
28
29 BEGIN {
30   node = "";
31   seq = -1;
32   count = 0;
33   file = "NONE";
34   status = 0;
35   options = "";
36   xout = "";
37   xerr = "";
38 }
39
40 /^@node / {
41   if (seq > 0)
42     printf(" --  %d file%s", seq, seq == 1 ? "" : "s");
43   if (seq >= 0)
44     printf("\n");
45
46   split($0, tmp, ",");
47   node = substr(tmp[1], 7);
48   if (length(node) > 10)
49     printf("Node: %s - truncated", node);
50   else
51     printf("Node: %s ", node);
52   gsub(" ", "_", node);
53   node = tolower(substr(node, 1, 10));
54   seq = 0;
55 }
56
57 /^@comment ignore$/ {
58   getline;
59   status = 0;
60   options = "";
61   xout = "";
62   xout = "";
63   next;
64 }
65
66 /^@comment status: / {
67   status = $3;
68 }
69
70 /^@comment options: / {
71   options = $0;
72   gsub ("@comment options:", "", options);
73 }
74
75 /^@comment xout: / {
76   xout = $0;
77   gsub ("@comment xout: ", "", xout);
78 }
79
80 /^@comment xerr: / {
81   xerr = $0;
82   gsub ("@comment xerr: ", "", xerr);
83 }
84
85 /^@example$/, /^@end example$/ {
86   if (seq < 0)
87     next;
88   if ($0 ~ /^@example$/) {
89     if (count > 0)
90       close (file);
91     seq++;
92     count++;
93     file = sprintf("%03d.%s", count, node);
94     printf("dnl @ %s:%d: Origin of test\n"\
95            "dnl @ expected status: %d\n"\
96            "dnl @ extra options: %s\n"\
97            "dnl @ Copyright (C) 2006, 2007, 2008, 2009 Free Software\n"\
98            "dnl @ Foundation, Inc.\n"\
99            "dnl @ This file is free software; the Free Software Foundation\n"\
100            "dnl @ gives unlimited permission to copy and/or distribute it\n"\
101            "dnl @ with or without modifications, as long as this notice\n"\
102            "dnl @ is preserved.\n", FILENAME, NR, status, options) > file;
103     if (xout)
104         printf("dnl @ expected output: %s\n", xout) > file;
105     if (xerr)
106         printf("dnl @ expected error: %s\n", xerr) > file;
107     status = 0;
108     options = "";
109     xout = "";
110     xerr = "";
111     next;
112   }
113   if ($0 ~ /^@end example$/) {
114     next;
115   }
116   if ($0 ~ /^\^D$/)
117     next;
118   if ($0 ~ /^\$ @kbd/)
119     next;
120   if ($0 ~ /^@result\{\}/ || $0 ~ /^@error\{\}/)
121     prefix = "dnl ";
122   else
123     prefix = "";
124   gsub("@@", "@", $0);
125   gsub("@[{]", "{", $0);
126   gsub("@}", "}", $0);
127   gsub("@w[{] }", " ", $0);
128   gsub("@tabchar[{]}", "\t", $0);
129   printf("%s%s\n", prefix, $0) >> file;
130 }
131
132 END {
133   printf("\n");
134   if (count > 0)
135     close(file);
136 }
137 ' $FILE