Migrated from GPL version 2 to GPL version 3
[findutils.git] / lib / regextype.c
blob73e4819bb42bae9107b160abc3c6c800d10204cb
2 /* regextype.c -- Decode the name of a regular expression syntax into am
3 option name.
5 Copyright 2005 Free Software Foundation, Inc.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 "xalloc.h"
33 #include "error.h"
36 #if ENABLE_NLS
37 # include <libintl.h>
38 # define _(Text) gettext (Text)
39 #else
40 # define _(Text) Text
41 #endif
42 #ifdef gettext_noop
43 # define N_(String) gettext_noop (String)
44 #else
45 /* See locate.c for explanation as to why not use (String) */
46 # define N_(String) String
47 #endif
51 struct tagRegexTypeMap
53 char *name;
54 int option_val;
57 struct tagRegexTypeMap regex_map[] =
59 #ifdef FINDUTILS
60 { "findutils-default", RE_SYNTAX_EMACS|RE_DOT_NEWLINE },
61 #endif
62 { "awk", RE_SYNTAX_AWK },
63 { "egrep", RE_SYNTAX_EGREP },
64 #ifndef FINDUTILS
65 { "ed", RE_SYNTAX_ED },
66 #endif
67 { "emacs", RE_SYNTAX_EMACS },
68 { "gnu-awk", RE_SYNTAX_GNU_AWK },
69 { "grep", RE_SYNTAX_GREP },
70 { "posix-awk", RE_SYNTAX_POSIX_AWK },
71 { "posix-basic", RE_SYNTAX_POSIX_BASIC },
72 { "posix-egrep", RE_SYNTAX_POSIX_EGREP },
73 { "posix-extended", RE_SYNTAX_POSIX_EXTENDED },
74 #ifndef FINDUTILS
75 { "posix-minimal-basic", RE_SYNTAX_POSIX_MINIMAL_BASIC },
76 { "sed", RE_SYNTAX_SED },
77 /* ,{ "posix-common", _RE_SYNTAX_POSIX_COMMON } */
78 #endif
80 enum { N_REGEX_MAP_ENTRIES = sizeof(regex_map)/sizeof(regex_map[0]) };
82 int
83 get_regex_type(const char *s)
85 unsigned i;
86 size_t msglen;
87 char *buf, *p;
89 msglen = 0u;
90 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
92 if (0 == strcmp(regex_map[i].name, s))
93 return regex_map[i].option_val;
94 else
95 msglen += strlen(quote(regex_map[i].name)) + 2u;
98 /* We didn't find a match for the type of regular expression that the
99 * user indicated they wanted. Tell them what the options are.
101 p = buf = xmalloc(1u + msglen);
102 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
104 if (i > 0u)
106 strcpy(p, ", ");
107 p += 2;
109 p += sprintf(p, "%s", quote(regex_map[i].name));
112 error(1, 0, _("Unknown regular expression type %s; valid types are %s."),
113 quote(s),
114 buf);
115 /*NOTREACHED*/
116 return -1;
120 const char *
121 get_regex_type_name(unsigned int ix)
123 if (ix < N_REGEX_MAP_ENTRIES)
124 return regex_map[ix].name;
125 else
126 return NULL;
130 get_regex_type_flags(unsigned int ix)
132 if (ix < N_REGEX_MAP_ENTRIES)
133 return regex_map[ix].option_val;
134 else
135 return -1;
139 int get_regex_type_synonym(unsigned int ix)
141 unsigned i;
142 int flags;
144 if (ix >= N_REGEX_MAP_ENTRIES)
145 return -1;
147 flags = regex_map[ix].option_val;
148 for (i=0u; i<ix; ++i)
150 if (flags == regex_map[i].option_val)
152 return i;
155 return -1;