WIP: silence build warnings
[findutils/ericb.git] / lib / regextype.c
blobcc16b6e1e77eee77a3bb44dc2a00e4e85ada0271
2 /* regextype.c -- Decode the name of a regular expression syntax into am
3 option name.
5 Copyright 2005, 2010 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 # include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdlib.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
50 struct tagRegexTypeMap
52 char *name;
53 int context;
54 int option_val;
57 struct tagRegexTypeMap regex_map[] =
59 { "findutils-default", CONTEXT_FINDUTILS, RE_SYNTAX_EMACS|RE_DOT_NEWLINE },
60 { "awk", CONTEXT_ALL, RE_SYNTAX_AWK },
61 { "egrep", CONTEXT_ALL, RE_SYNTAX_EGREP },
62 { "ed", CONTEXT_GENERIC, RE_SYNTAX_ED },
63 { "emacs", CONTEXT_ALL, RE_SYNTAX_EMACS },
64 { "gnu-awk", CONTEXT_ALL, RE_SYNTAX_GNU_AWK },
65 { "grep", CONTEXT_ALL, RE_SYNTAX_GREP },
66 { "posix-awk", CONTEXT_ALL, RE_SYNTAX_POSIX_AWK },
67 { "posix-basic", CONTEXT_ALL, RE_SYNTAX_POSIX_BASIC },
68 { "posix-egrep", CONTEXT_ALL, RE_SYNTAX_POSIX_EGREP },
69 { "posix-extended", CONTEXT_ALL, RE_SYNTAX_POSIX_EXTENDED },
70 { "posix-minimal-basic", CONTEXT_GENERIC, RE_SYNTAX_POSIX_MINIMAL_BASIC },
71 { "sed", CONTEXT_GENERIC, RE_SYNTAX_SED },
72 /* ,{ "posix-common", CONTEXT_GENERIC, _RE_SYNTAX_POSIX_COMMON } */
74 enum { N_REGEX_MAP_ENTRIES = sizeof (regex_map)/sizeof (regex_map[0]) };
76 int
77 get_regex_type (const char *s)
79 unsigned i;
80 size_t msglen;
81 char *buf, *p;
83 msglen = 0u;
84 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
86 if (0 == strcmp (regex_map[i].name, s))
87 return regex_map[i].option_val;
88 else
89 msglen += strlen (quote (regex_map[i].name)) + 2u;
92 /* We didn't find a match for the type of regular expression that the
93 * user indicated they wanted. Tell them what the options are.
95 p = buf = xmalloc (1u + msglen);
96 for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
98 if (i > 0u)
100 strcpy (p, ", ");
101 p += 2;
103 p += sprintf (p, "%s", quote (regex_map[i].name));
106 error (EXIT_FAILURE, 0,
107 _("Unknown regular expression type %s; valid types are %s."),
108 quote (s),
109 buf);
110 /*NOTREACHED*/
111 return -1;
115 const char *
116 get_regex_type_name (unsigned int ix)
118 if (ix < N_REGEX_MAP_ENTRIES)
119 return regex_map[ix].name;
120 else
121 return NULL;
125 get_regex_type_flags (unsigned int ix)
127 if (ix < N_REGEX_MAP_ENTRIES)
128 return regex_map[ix].option_val;
129 else
130 return -1;
133 unsigned int get_regex_type_context (unsigned int ix)
135 if (ix < N_REGEX_MAP_ENTRIES)
136 return regex_map[ix].context;
137 else
138 return 0u;
142 get_regex_type_synonym (unsigned int ix)
144 unsigned i;
145 int flags;
147 if (ix >= N_REGEX_MAP_ENTRIES)
148 return -1;
150 flags = regex_map[ix].option_val;
151 for (i=0u; i<ix; ++i)
153 if (flags == regex_map[i].option_val)
155 return i;
158 return -1;