packaging: remove old options to find hidden files
[platform/upstream/findutils.git] / lib / regextype.c
1
2 /* regextype.c -- Decode the name of a regular expression syntax into am
3                   option name.
4
5    Copyright 2005, 2010, 2011 Free Software Foundation, Inc.
6
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.
11
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.
16
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/>.
19 */
20 /* Written by James Youngman, <jay@gnu.org>. */
21
22 /* config.h must be included first. */
23 #include <config.h>
24
25 /* system headers. */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 /* gnulib headers. */
31 #include "error.h"
32 #include "gettext.h"
33 #include "quote.h"
34 #include "regex.h"
35 #include "regextype.h"
36 #include "xalloc.h"
37
38
39 #if ENABLE_NLS
40 # include <libintl.h>
41 # define _(Text) gettext (Text)
42 #else
43 # define _(Text) Text
44 #endif
45 #ifdef gettext_noop
46 # define N_(String) gettext_noop (String)
47 #else
48 /* See locate.c for explanation as to why not use (String) */
49 # define N_(String) String
50 #endif
51
52
53 struct tagRegexTypeMap
54 {
55   const char *name;
56   int  context;
57   int  option_val;
58 };
59
60 struct tagRegexTypeMap regex_map[] =
61   {
62    { "findutils-default",     CONTEXT_FINDUTILS, RE_SYNTAX_EMACS|RE_DOT_NEWLINE  },
63    { "awk",                   CONTEXT_ALL,       RE_SYNTAX_AWK                   },
64    { "egrep",                 CONTEXT_ALL,       RE_SYNTAX_EGREP                 },
65    { "ed",                    CONTEXT_GENERIC,   RE_SYNTAX_ED                    },
66    { "emacs",                 CONTEXT_ALL,       RE_SYNTAX_EMACS                 },
67    { "gnu-awk",               CONTEXT_ALL,       RE_SYNTAX_GNU_AWK               },
68    { "grep",                  CONTEXT_ALL,       RE_SYNTAX_GREP                  },
69    { "posix-awk",             CONTEXT_ALL,       RE_SYNTAX_POSIX_AWK             },
70    { "posix-basic",           CONTEXT_ALL,       RE_SYNTAX_POSIX_BASIC           },
71    { "posix-egrep",           CONTEXT_ALL,       RE_SYNTAX_POSIX_EGREP           },
72    { "posix-extended",        CONTEXT_ALL,       RE_SYNTAX_POSIX_EXTENDED        },
73    { "posix-minimal-basic",   CONTEXT_GENERIC,   RE_SYNTAX_POSIX_MINIMAL_BASIC    },
74    { "sed",                   CONTEXT_GENERIC,   RE_SYNTAX_SED                   },
75    /*    ,{ "posix-common",   CONTEXT_GENERIC,  _RE_SYNTAX_POSIX_COMMON   } */
76   };
77 enum { N_REGEX_MAP_ENTRIES = sizeof (regex_map)/sizeof (regex_map[0]) };
78
79 int
80 get_regex_type (const char *s)
81 {
82   unsigned i;
83   size_t msglen;
84   char *buf, *p;
85
86   msglen = 0u;
87   for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
88     {
89       if (0 == strcmp (regex_map[i].name, s))
90         return regex_map[i].option_val;
91       else
92         msglen += strlen (quote (regex_map[i].name)) + 2u;
93     }
94
95   /* We didn't find a match for the type of regular expression that the
96    * user indicated they wanted.  Tell them what the options are.
97    */
98   p = buf = xmalloc (1u + msglen);
99   for (i=0u; i<N_REGEX_MAP_ENTRIES; ++i)
100     {
101       if (i > 0u)
102         {
103           strcpy (p, ", ");
104           p += 2;
105         }
106       p += sprintf (p, "%s", quote (regex_map[i].name));
107     }
108
109   error (EXIT_FAILURE, 0,
110          _("Unknown regular expression type %s; valid types are %s."),
111          quote (s),
112          buf);
113   /*NOTREACHED*/
114   return -1;
115 }
116
117
118 const char *
119 get_regex_type_name (unsigned int ix)
120 {
121   if (ix < N_REGEX_MAP_ENTRIES)
122     return regex_map[ix].name;
123   else
124     return NULL;
125 }
126
127 int
128 get_regex_type_flags (unsigned int ix)
129 {
130   if (ix < N_REGEX_MAP_ENTRIES)
131     return regex_map[ix].option_val;
132   else
133     return -1;
134 }
135
136 unsigned int get_regex_type_context (unsigned int ix)
137 {
138   if (ix < N_REGEX_MAP_ENTRIES)
139     return regex_map[ix].context;
140   else
141     return 0u;
142 }
143
144 int
145 get_regex_type_synonym (unsigned int ix)
146 {
147   unsigned i;
148   int flags;
149
150   if (ix >= N_REGEX_MAP_ENTRIES)
151     return -1;
152
153   flags = regex_map[ix].option_val;
154   for (i=0u; i<ix; ++i)
155     {
156       if (flags == regex_map[i].option_val)
157         {
158           return i;
159         }
160     }
161   return -1;
162 }