Savannah bug#18222 - test case
[findutils.git] / lib / regextype.c
blob768a82b2dacd40156e03c07a66045946034cc310
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 "quotearg.h"
34 #include "xalloc.h"
35 #include "error.h"
38 #if ENABLE_NLS
39 # include <libintl.h>
40 # define _(Text) gettext (Text)
41 #else
42 # define _(Text) Text
43 #endif
44 #ifdef gettext_noop
45 # define N_(String) gettext_noop (String)
46 #else
47 /* See locate.c for explanation as to why not use (String) */
48 # define N_(String) String
49 #endif
53 struct tagRegexTypeMap
55 char *name;
56 int option_val;
59 struct tagRegexTypeMap regex_map[] =
61 #ifdef FINDUTILS
62 { "findutils-default", RE_SYNTAX_EMACS|RE_DOT_NEWLINE },
63 #endif
64 { "awk", RE_SYNTAX_AWK },
65 { "egrep", RE_SYNTAX_EGREP },
66 #ifndef FINDUTILS
67 { "ed", RE_SYNTAX_ED },
68 #endif
69 { "emacs", RE_SYNTAX_EMACS },
70 { "gnu-awk", RE_SYNTAX_GNU_AWK },
71 { "grep", RE_SYNTAX_GREP },
72 { "posix-awk", RE_SYNTAX_POSIX_AWK },
73 { "posix-basic", RE_SYNTAX_POSIX_BASIC },
74 { "posix-egrep", RE_SYNTAX_POSIX_EGREP },
75 { "posix-extended", RE_SYNTAX_POSIX_EXTENDED },
76 #ifndef FINDUTILS
77 { "posix-minimal-basic", RE_SYNTAX_POSIX_MINIMAL_BASIC },
78 { "sed", RE_SYNTAX_SED },
79 /* ,{ "posix-common", _RE_SYNTAX_POSIX_COMMON } */
80 #endif
82 enum { N_REGEX_MAP_ENTRIES = sizeof(regex_map)/sizeof(regex_map[0]) };
84 int
85 get_regex_type(const char *s)
87 unsigned i;
88 size_t msglen;
89 char *buf, *p;
91 msglen = 0u;
92 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
94 if (0 == strcmp(regex_map[i].name, s))
95 return regex_map[i].option_val;
96 else
97 msglen += strlen(quote(regex_map[i].name)) + 2u;
100 /* We didn't find a match for the type of regular expression that the
101 * user indicated they wanted. Tell them what the options are.
103 p = buf = xmalloc(1u + msglen);
104 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
106 if (i > 0u)
108 strcpy(p, ", ");
109 p += 2;
111 p += sprintf(p, "%s", quote(regex_map[i].name));
114 error(1, 0, _("Unknown regular expression type %s; valid types are %s."),
115 quote(s),
116 buf);
117 /*NOTREACHED*/
118 return -1;
122 const char *
123 get_regex_type_name(unsigned int ix)
125 if (ix < N_REGEX_MAP_ENTRIES)
126 return regex_map[ix].name;
127 else
128 return NULL;
132 get_regex_type_flags(unsigned int ix)
134 if (ix < N_REGEX_MAP_ENTRIES)
135 return regex_map[ix].option_val;
136 else
137 return -1;
141 int get_regex_type_synonym(unsigned int ix)
143 unsigned i;
144 int flags;
146 if (ix >= N_REGEX_MAP_ENTRIES)
147 return -1;
149 flags = regex_map[ix].option_val;
150 for (i=0u; i<ix; ++i)
152 if (flags == regex_map[i].option_val)
154 return i;
157 return -1;