Work around Savannah bug #17877, which manifests on systems which use
[findutils.git] / lib / regexprops.c
blob06371cc6afb3bd270d6545355c1a0223d6c11e93
1 /* regexprops.c -- document the properties of the regular expressions
2 understood by gnulib.
4 Copyright 2005 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by James Youngman, <jay@gnu.org>. */
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <errno.h>
30 #include "regex.h"
31 #include "regextype.h"
34 /* Name this program was run with. */
35 char *program_name;
37 static void output(const char *s, int escape)
39 (void) escape;
41 fputs(s, stdout);
45 static void newline(void)
47 output("\n", 0);
50 static void content(const char *s)
52 output(s, 1);
55 static void literal(const char *s)
57 output(s, 0);
60 static void directive(const char *s)
62 output(s, 0);
65 static void enum_item(const char *s)
67 newline();
68 directive("@item ");
69 literal(s);
70 newline();
73 static void begin_subsection(const char *name,
74 const char *next,
75 const char *prev,
76 const char *up)
78 (void) next;
79 (void) prev;
80 (void) up;
82 newline();
84 directive("@node ");
85 content(name);
86 content(" regular expression syntax");
87 newline();
89 directive("@subsection ");
90 output("@samp{", 0);
91 content(name);
92 output("}", 0);
93 content(" regular expression syntax");
94 newline();
97 static void begintable_markup(char const *markup)
99 newline();
100 directive("@table ");
101 literal(markup);
102 newline();
105 static void endtable()
107 newline();
108 directive("@end table");
109 newline();
112 static void beginenum()
114 newline();
115 directive("@enumerate");
116 newline();
119 static void endenum()
121 newline();
122 directive("@end enumerate");
123 newline();
126 static void newpara()
128 content("\n\n");
132 static void
133 describe_regex_syntax(int options)
135 newpara();
136 content("The character @samp{.} matches any single character");
137 if ( (options & RE_DOT_NEWLINE) == 0 )
139 content(" except newline");
141 if (options & RE_DOT_NOT_NULL)
143 if ( (options & RE_DOT_NEWLINE) == 0 )
144 content(" and");
145 else
146 content(" except");
148 content(" the null character");
150 content(". ");
151 newpara();
153 if (!(options & RE_LIMITED_OPS))
155 begintable_markup("@samp");
156 if (options & RE_BK_PLUS_QM)
158 enum_item("\\+");
159 content("indicates that the regular expression should match one"
160 " or more occurrences of the previous atom or regexp. ");
161 enum_item("\\?");
162 content("indicates that the regular expression should match zero"
163 " or one occurrence of the previous atom or regexp. ");
164 enum_item("+ and ? ");
165 content("match themselves. ");
167 else
169 enum_item("+");
170 content("indicates that the regular expression should match one"
171 " or more occurrences of the previous atom or regexp. ");
172 enum_item("?");
173 content("indicates that the regular expression should match zero"
174 " or one occurrence of the previous atom or regexp. ");
175 enum_item("\\+");
176 literal("matches a @samp{+}");
177 enum_item("\\?");
178 literal("matches a @samp{?}. ");
180 endtable();
183 newpara();
185 content("Bracket expressions are used to match ranges of characters. ");
186 literal("Bracket expressions where the range is backward, for example @samp{[z-a]}, are ");
187 if (options & RE_NO_EMPTY_RANGES)
188 content("invalid");
189 else
190 content("ignored");
191 content(". ");
193 if (options & RE_BACKSLASH_ESCAPE_IN_LISTS)
194 literal("Within square brackets, @samp{\\} can be used to quote "
195 "the following character. ");
196 else
197 literal("Within square brackets, @samp{\\} is taken literally. ");
199 if (options & RE_CHAR_CLASSES)
200 content("Character classes are supported; for example "
201 "@samp{[[:digit:]]} will match a single decimal digit. ");
202 else
203 literal("Character classes are not supported, so for example "
204 "you would need to use @samp{[0-9]} "
205 "instead of @samp{[[:digit:]]}. ");
207 if (options & RE_HAT_LISTS_NOT_NEWLINE)
209 literal("Non-matching lists @samp{[^@dots{}]} do not ever match newline. ");
211 newpara();
212 if (options & RE_NO_GNU_OPS)
214 content("GNU extensions are not supported and so "
215 "@samp{\\w}, @samp{\\W}, @samp{\\<}, @samp{\\>}, @samp{\\b}, @samp{\\B}, @samp{\\`}, and @samp{\\'} "
216 "match "
217 "@samp{w}, @samp{W}, @samp{<}, @samp{>}, @samp{b}, @samp{B}, @samp{`}, and @samp{'} respectively. ");
219 else
221 content("GNU extensions are supported:");
222 beginenum();
223 enum_item("@samp{\\w} matches a character within a word");
224 enum_item("@samp{\\W} matches a character which is not within a word");
225 enum_item("@samp{\\<} matches the beginning of a word");
226 enum_item("@samp{\\>} matches the end of a word");
227 enum_item("@samp{\\b} matches a word boundary");
228 enum_item("@samp{\\B} matches characters which are not a word boundary");
229 enum_item("@samp{\\`} matches the beginning of the whole input");
230 enum_item("@samp{\\'} matches the end of the whole input");
231 endenum();
234 newpara();
237 if (options & RE_NO_BK_PARENS)
239 literal("Grouping is performed with parentheses @samp{()}. ");
241 if (options & RE_UNMATCHED_RIGHT_PAREN_ORD)
242 literal("An unmatched @samp{)} matches just itself. ");
244 else
246 literal("Grouping is performed with backslashes followed by parentheses @samp{\\(}, @samp{\\)}. ");
249 if (options & RE_NO_BK_REFS)
251 content("A backslash followed by a digit matches that digit. ");
253 else
255 literal("A backslash followed by a digit acts as a back-reference and matches the same thing as the previous grouped expression indicated by that number. For example @samp{\\2} matches the second group expression. The order of group expressions is determined by the position of their opening parenthesis ");
256 if (options & RE_NO_BK_PARENS)
257 literal("@samp{(}");
258 else
259 literal("@samp{\\(}");
260 content(". ");
264 newpara();
265 if (!(options & RE_LIMITED_OPS))
267 if (options & RE_NO_BK_VBAR)
268 literal("The alternation operator is @samp{|}. ");
269 else
270 literal("The alternation operator is @samp{\\|}. ");
272 newpara();
274 if (options & RE_CONTEXT_INDEP_ANCHORS)
276 literal("The characters @samp{^} and @samp{$} always represent the beginning and end of a string respectively, except within square brackets. Within brackets, @samp{^} can be used to invert the membership of the character class being specified. ");
278 else
280 literal("The character @samp{^} only represents the beginning of a string when it appears:");
281 beginenum();
282 enum_item("\nAt the beginning of a regular expression");
283 enum_item("After an open-group, signified by ");
284 if (options & RE_NO_BK_PARENS)
286 literal("@samp{(}");
288 else
290 literal("@samp{\\(}");
292 newline();
293 if (!(options & RE_LIMITED_OPS))
295 if (options & RE_NEWLINE_ALT)
296 enum_item("After a newline");
298 if (options & RE_NO_BK_VBAR )
299 enum_item("After the alternation operator @samp{|}");
300 else
301 enum_item("After the alternation operator @samp{\\|}");
303 endenum();
305 newpara();
306 literal("The character @samp{$} only represents the end of a string when it appears:");
307 beginenum();
308 enum_item("At the end of a regular expression");
309 enum_item("Before an close-group, signified by ");
310 if (options & RE_NO_BK_PARENS)
312 literal("@samp{)}");
314 else
316 literal("@samp{\\)}");
318 if (!(options & RE_LIMITED_OPS))
320 if (options & RE_NEWLINE_ALT)
321 enum_item("Before a newline");
323 if (options & RE_NO_BK_VBAR)
324 enum_item("Before the alternation operator @samp{|}");
325 else
326 enum_item("Before the alternation operator @samp{\\|}");
328 endenum();
330 newpara();
331 if (!(options & RE_LIMITED_OPS) )
333 if ((options & RE_CONTEXT_INDEP_OPS)
334 && !(options & RE_CONTEXT_INVALID_OPS))
336 literal("The characters @samp{*}, @samp{+} and @samp{?} are special anywhere in a regular expression. ");
338 else
340 if (options & RE_BK_PLUS_QM)
341 literal("@samp{\\*}, @samp{\\+} and @samp{\\?} ");
342 else
343 literal("@samp{*}, @samp{+} and @samp{?} ");
345 if (options & RE_CONTEXT_INVALID_OPS)
347 content("are special at any point in a regular expression except the following places, where they are not allowed:");
349 else
351 content("are special at any point in a regular expression except:");
354 beginenum();
355 enum_item("At the beginning of a regular expression");
356 enum_item("After an open-group, signified by ");
357 if (options & RE_NO_BK_PARENS)
359 literal("@samp{(}");
361 else
363 literal("@samp{\\(}");
365 if (!(options & RE_LIMITED_OPS))
367 if (options & RE_NEWLINE_ALT)
368 enum_item("After a newline");
370 if (options & RE_NO_BK_VBAR)
371 enum_item("After the alternation operator @samp{|}");
372 else
373 enum_item("After the alternation operator @samp{\\|}");
375 endenum();
380 newpara();
381 if (options & RE_INTERVALS)
383 if (options & RE_NO_BK_BRACES)
385 literal("Intervals are specified by @samp{@{} and @samp{@}}. ");
386 if (options & RE_INVALID_INTERVAL_ORD)
388 literal("Invalid intervals are treated as literals, for example @samp{a@{1} is treated as @samp{a\\@{1}");
390 else
392 literal("Invalid intervals such as @samp{a@{1z} are not accepted. ");
395 else
397 literal("Intervals are specified by @samp{\\@{} and @samp{\\@}}. ");
398 if (options & RE_INVALID_INTERVAL_ORD)
400 literal("Invalid intervals are treated as literals, for example @samp{a\\@{1} is treated as @samp{a@{1}");
402 else
404 literal("Invalid intervals such as @samp{a\\@{1z} are not accepted. ");
410 newpara();
411 if (options & RE_NO_POSIX_BACKTRACKING)
413 content("Matching succeeds as soon as the whole pattern is matched, meaning that the result may not be the longest possible match. ");
415 else
417 content("The longest possible match is returned; this applies to the regular expression as a whole and (subject to this constraint) to subexpressions within groups. ");
419 newpara();
424 static void
425 menu(void)
427 int i, options;
428 const char *name;
430 output("@menu\n", 0);
431 for (i=0;
432 options = get_regex_type_flags(i),
433 name=get_regex_type_name(i);
434 ++i)
436 output("* ", 0);
437 output(name, 0);
438 content(" regular expression syntax");
439 output("::", 0);
440 newline();
442 output("@end menu\n", 0);
446 static void
447 describe_all(const char *up)
449 const char *name, *next, *previous;
450 int options;
451 int i, parent;
453 menu();
455 previous = "";
457 for (i=0;
458 options = get_regex_type_flags(i),
459 name=get_regex_type_name(i);
460 ++i)
462 next = get_regex_type_name(i+1);
463 if (NULL == next)
464 next = "";
465 begin_subsection(name, next, previous, up);
466 parent = get_regex_type_synonym(i);
467 if (parent >= 0)
469 content("This is a synonym for ");
470 content(get_regex_type_name(parent));
471 content(".");
473 else
475 describe_regex_syntax(options);
477 previous = name;
483 int main (int argc, char *argv[])
485 const char *up = "";
486 program_name = argv[0];
488 if (argc > 1)
489 up = argv[1];
491 describe_all(up);
492 return 0;