| 1 | /* regexprops.c -- document the properties of the regular expressions
|
|---|
| 2 | understood by gnulib.
|
|---|
| 3 |
|
|---|
| 4 | Copyright 2005 Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 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.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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. */
|
|---|
| 19 |
|
|---|
| 20 | /* Written by James Youngman, <[email protected]>. */
|
|---|
| 21 |
|
|---|
| 22 | #if HAVE_CONFIG_H
|
|---|
| 23 | # include <config.h>
|
|---|
| 24 | #endif
|
|---|
| 25 |
|
|---|
| 26 | #include <stdio.h>
|
|---|
| 27 | #include <unistd.h>
|
|---|
| 28 | #include <errno.h>
|
|---|
| 29 |
|
|---|
| 30 | #include "regex.h"
|
|---|
| 31 | #include "regextype.h"
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | /* Name this program was run with. */
|
|---|
| 35 | char *program_name;
|
|---|
| 36 |
|
|---|
| 37 | static void output(const char *s, int escape)
|
|---|
| 38 | {
|
|---|
| 39 | (void) escape;
|
|---|
| 40 |
|
|---|
| 41 | fputs(s, stdout);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | static void newline(void)
|
|---|
| 46 | {
|
|---|
| 47 | output("\n", 0);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | static void content(const char *s)
|
|---|
| 51 | {
|
|---|
| 52 | output(s, 1);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | static void literal(const char *s)
|
|---|
| 56 | {
|
|---|
| 57 | output(s, 0);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | static void directive(const char *s)
|
|---|
| 61 | {
|
|---|
| 62 | output(s, 0);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | static void enum_item(const char *s)
|
|---|
| 66 | {
|
|---|
| 67 | newline();
|
|---|
| 68 | directive("@item ");
|
|---|
| 69 | literal(s);
|
|---|
| 70 | newline();
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static void begin_subsection(const char *name,
|
|---|
| 74 | const char *next,
|
|---|
| 75 | const char *prev,
|
|---|
| 76 | const char *up)
|
|---|
| 77 | {
|
|---|
| 78 | (void) next;
|
|---|
| 79 | (void) prev;
|
|---|
| 80 | (void) up;
|
|---|
| 81 |
|
|---|
| 82 | newline();
|
|---|
| 83 |
|
|---|
| 84 | directive("@node ");
|
|---|
| 85 | content(name);
|
|---|
| 86 | content(" regular expression syntax");
|
|---|
| 87 | newline();
|
|---|
| 88 |
|
|---|
| 89 | directive("@subsection ");
|
|---|
| 90 | output("@samp{", 0);
|
|---|
| 91 | content(name);
|
|---|
| 92 | output("}", 0);
|
|---|
| 93 | content(" regular expression syntax");
|
|---|
| 94 | newline();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static void begintable_markup(char const *markup)
|
|---|
| 98 | {
|
|---|
| 99 | newline();
|
|---|
| 100 | directive("@table ");
|
|---|
| 101 | literal(markup);
|
|---|
| 102 | newline();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | static void endtable()
|
|---|
| 106 | {
|
|---|
| 107 | newline();
|
|---|
| 108 | directive("@end table");
|
|---|
| 109 | newline();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | static void beginenum()
|
|---|
| 113 | {
|
|---|
| 114 | newline();
|
|---|
| 115 | directive("@enumerate");
|
|---|
| 116 | newline();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | static void endenum()
|
|---|
| 120 | {
|
|---|
| 121 | newline();
|
|---|
| 122 | directive("@end enumerate");
|
|---|
| 123 | newline();
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | static void newpara()
|
|---|
| 127 | {
|
|---|
| 128 | content("\n\n");
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 | static void
|
|---|
| 133 | describe_regex_syntax(int options)
|
|---|
| 134 | {
|
|---|
| 135 | newpara();
|
|---|
| 136 | content("The character @samp{.} matches any single character");
|
|---|
| 137 | if ( (options & RE_DOT_NEWLINE) == 0 )
|
|---|
| 138 | {
|
|---|
| 139 | content(" except newline");
|
|---|
| 140 | }
|
|---|
| 141 | if (options & RE_DOT_NOT_NULL)
|
|---|
| 142 | {
|
|---|
| 143 | if ( (options & RE_DOT_NEWLINE) == 0 )
|
|---|
| 144 | content(" and");
|
|---|
| 145 | else
|
|---|
| 146 | content(" except");
|
|---|
| 147 |
|
|---|
| 148 | content(" the null character");
|
|---|
| 149 | }
|
|---|
| 150 | content(". ");
|
|---|
| 151 | newpara();
|
|---|
| 152 |
|
|---|
| 153 | if (!(options & RE_LIMITED_OPS))
|
|---|
| 154 | {
|
|---|
| 155 | begintable_markup("@samp");
|
|---|
| 156 | if (options & RE_BK_PLUS_QM)
|
|---|
| 157 | {
|
|---|
| 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. ");
|
|---|
| 166 | }
|
|---|
| 167 | else
|
|---|
| 168 | {
|
|---|
| 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{?}. ");
|
|---|
| 179 | }
|
|---|
| 180 | endtable();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | newpara();
|
|---|
| 184 |
|
|---|
| 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(". ");
|
|---|
| 192 |
|
|---|
| 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. ");
|
|---|
| 198 |
|
|---|
| 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:]]}. ");
|
|---|
| 206 |
|
|---|
| 207 | if (options & RE_HAT_LISTS_NOT_NEWLINE)
|
|---|
| 208 | {
|
|---|
| 209 | literal("Non-matching lists @samp{[^@dots{}]} do not ever match newline. ");
|
|---|
| 210 | }
|
|---|
| 211 | newpara();
|
|---|
| 212 | if (options & RE_NO_GNU_OPS)
|
|---|
| 213 | {
|
|---|
| 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. ");
|
|---|
| 218 | }
|
|---|
| 219 | else
|
|---|
| 220 | {
|
|---|
| 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();
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | newpara();
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | if (options & RE_NO_BK_PARENS)
|
|---|
| 238 | {
|
|---|
| 239 | literal("Grouping is performed with parentheses @samp{()}. ");
|
|---|
| 240 |
|
|---|
| 241 | if (options & RE_UNMATCHED_RIGHT_PAREN_ORD)
|
|---|
| 242 | literal("An unmatched @samp{)} matches just itself. ");
|
|---|
| 243 | }
|
|---|
| 244 | else
|
|---|
| 245 | {
|
|---|
| 246 | literal("Grouping is performed with backslashes followed by parentheses @samp{\\(}, @samp{\\)}. ");
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | if (options & RE_NO_BK_REFS)
|
|---|
| 250 | {
|
|---|
| 251 | content("A backslash followed by a digit matches that digit. ");
|
|---|
| 252 | }
|
|---|
| 253 | else
|
|---|
| 254 | {
|
|---|
| 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(". ");
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 | newpara();
|
|---|
| 265 | if (!(options & RE_LIMITED_OPS))
|
|---|
| 266 | {
|
|---|
| 267 | if (options & RE_NO_BK_VBAR)
|
|---|
| 268 | literal("The alternation operator is @samp{|}. ");
|
|---|
| 269 | else
|
|---|
| 270 | literal("The alternation operator is @samp{\\|}. ");
|
|---|
| 271 | }
|
|---|
| 272 | newpara();
|
|---|
| 273 |
|
|---|
| 274 | if (options & RE_CONTEXT_INDEP_ANCHORS)
|
|---|
| 275 | {
|
|---|
| 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. ");
|
|---|
| 277 | }
|
|---|
| 278 | else
|
|---|
| 279 | {
|
|---|
| 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)
|
|---|
| 285 | {
|
|---|
| 286 | literal("@samp{(}");
|
|---|
| 287 | }
|
|---|
| 288 | else
|
|---|
| 289 | {
|
|---|
| 290 | literal("@samp{\\(}");
|
|---|
| 291 | }
|
|---|
| 292 | newline();
|
|---|
| 293 | if (!(options & RE_LIMITED_OPS))
|
|---|
| 294 | {
|
|---|
| 295 | if (options & RE_NEWLINE_ALT)
|
|---|
| 296 | enum_item("After a newline");
|
|---|
| 297 |
|
|---|
| 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{\\|}");
|
|---|
| 302 | }
|
|---|
| 303 | endenum();
|
|---|
| 304 |
|
|---|
| 305 | newpara();
|
|---|
| 306 | literal("The character @samp{$} only represents the end of a string when it appears:");
|
|---|
|
|---|