Made Texinfo style corrections suggested by Karl Berry.
[findutils.git] / lib / regextype.c
blob658eff4609fabe3533cf51c05b990d6ff39173fd
1 /* regextype.c -- Decode the name of a regular expression syntax into am
2 option name.
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 <string.h>
27 #include <stdio.h>
29 #include "regextype.h"
30 #include "regex.h"
31 #include "quote.h"
32 #include "quotearg.h"
33 #include "xalloc.h"
34 #include "error.h"
37 #if ENABLE_NLS
38 # include <libintl.h>
39 # define _(Text) gettext (Text)
40 #else
41 # define _(Text) Text
42 #endif
43 #ifdef gettext_noop
44 # define N_(String) gettext_noop (String)
45 #else
46 /* See locate.c for explanation as to why not use (String) */
47 # define N_(String) String
48 #endif
52 struct tagRegexTypeMap
54 char *name;
55 int option_val;
57 struct tagRegexTypeMap regex_map[] =
59 { "emacs", RE_SYNTAX_EMACS },
60 { "posix-awk", RE_SYNTAX_POSIX_AWK },
61 { "posix-basic", RE_SYNTAX_POSIX_BASIC },
62 { "posix-egrep", RE_SYNTAX_POSIX_EGREP },
63 { "posix-extended", RE_SYNTAX_POSIX_EXTENDED }
64 /* new and undocumented entries below... */
65 ,{ "posix-minimal-basic", RE_SYNTAX_POSIX_MINIMAL_BASIC }
67 enum { N_REGEX_MAP_ENTRIES = sizeof(regex_map)/sizeof(regex_map[0]) };
69 int
70 get_regex_type(const char *s)
72 unsigned i;
73 size_t msglen;
74 char *buf, *p;
76 msglen = 0u;
77 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
79 if (0 == strcmp(regex_map[i].name, s))
80 return regex_map[i].option_val;
81 else
82 msglen += strlen(quote(regex_map[i].name)) + 2u;
85 /* We didn't find a match for the type of regular expression that the
86 * user indicated they wanted. Tell them what the options are.
88 p = buf = xmalloc(1u + msglen);
89 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
91 if (i > 0u)
93 strcpy(p, ", ");
94 p += 2;
96 p += sprintf(p, "%s", quote(regex_map[i].name));
99 error(1, 0, _("Unknown regular expression type %s; valid types are %s."),
100 quote(s),
101 buf);
102 /*NOTREACHED*/
103 return -1;
107 const char *
108 get_regex_type_name(int ix)
110 if (ix < N_REGEX_MAP_ENTRIES)
111 return regex_map[ix].name;
112 else
113 return NULL;
117 get_regex_type_flags(int ix)
119 if (ix < N_REGEX_MAP_ENTRIES)
120 return regex_map[ix].option_val;
121 else
122 return -1;