Recommend that people check they are using the latest version before reporting a...
[findutils.git] / lib / regextype.c
blobbc60bf9feea03333e1f48a59ffa5f786969a3d47
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 2, or (at your option)
10 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, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Written by James Youngman, <jay@gnu.org>. */
23 #if HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <string.h>
28 #include <stdio.h>
30 #include "regextype.h"
31 #include "regex.h"
32 #include "quote.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;
58 struct tagRegexTypeMap regex_map[] =
60 #ifdef FINDUTILS
61 { "findutils-default", RE_SYNTAX_EMACS|RE_DOT_NEWLINE },
62 #endif
63 { "awk", RE_SYNTAX_AWK },
64 { "egrep", RE_SYNTAX_EGREP },
65 #ifndef FINDUTILS
66 { "ed", RE_SYNTAX_ED },
67 #endif
68 { "emacs", RE_SYNTAX_EMACS },
69 { "gnu-awk", RE_SYNTAX_GNU_AWK },
70 { "grep", RE_SYNTAX_GREP },
71 { "posix-awk", RE_SYNTAX_POSIX_AWK },
72 { "posix-basic", RE_SYNTAX_POSIX_BASIC },
73 { "posix-egrep", RE_SYNTAX_POSIX_EGREP },
74 { "posix-extended", RE_SYNTAX_POSIX_EXTENDED },
75 #ifndef FINDUTILS
76 { "posix-minimal-basic", RE_SYNTAX_POSIX_MINIMAL_BASIC },
77 { "sed", RE_SYNTAX_SED },
78 /* ,{ "posix-common", _RE_SYNTAX_POSIX_COMMON } */
79 #endif
81 enum { N_REGEX_MAP_ENTRIES = sizeof(regex_map)/sizeof(regex_map[0]) };
83 int
84 get_regex_type(const char *s)
86 unsigned i;
87 size_t msglen;
88 char *buf, *p;
90 msglen = 0u;
91 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
93 if (0 == strcmp(regex_map[i].name, s))
94 return regex_map[i].option_val;
95 else
96 msglen += strlen(quote(regex_map[i].name)) + 2u;
99 /* We didn't find a match for the type of regular expression that the
100 * user indicated they wanted. Tell them what the options are.
102 p = buf = xmalloc(1u + msglen);
103 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
105 if (i > 0u)
107 strcpy(p, ", ");
108 p += 2;
110 p += sprintf(p, "%s", quote(regex_map[i].name));
113 error(1, 0, _("Unknown regular expression type %s; valid types are %s."),
114 quote(s),
115 buf);
116 /*NOTREACHED*/
117 return -1;
121 const char *
122 get_regex_type_name(unsigned int ix)
124 if (ix < N_REGEX_MAP_ENTRIES)
125 return regex_map[ix].name;
126 else
127 return NULL;
131 get_regex_type_flags(unsigned int ix)
133 if (ix < N_REGEX_MAP_ENTRIES)
134 return regex_map[ix].option_val;
135 else
136 return -1;
140 int get_regex_type_synonym(unsigned int ix)
142 unsigned i;
143 int flags;
145 if (ix >= N_REGEX_MAP_ENTRIES)
146 return -1;
148 flags = regex_map[ix].option_val;
149 for (i=0u; i<ix; ++i)
151 if (flags == regex_map[i].option_val)
153 return i;
156 return -1;