xref: /freebsd/contrib/ntp/sntp/libopts/option-xat-attribute.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert /*   -*- buffer-read-only: t -*- vi: set ro:
2*2b15cb3dSCy Schubert  *
3*2b15cb3dSCy Schubert  *  DO NOT EDIT THIS FILE   (stdin.c)
4*2b15cb3dSCy Schubert  *
5*2b15cb3dSCy Schubert  *  It has been AutoGen-ed  August  8, 2014 at 04:09:04 PM by AutoGen 5.18.4pre11
6*2b15cb3dSCy Schubert  *  From the definitions    stdin
7*2b15cb3dSCy Schubert  *  and the template file   str2enum
8*2b15cb3dSCy Schubert  *
9*2b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
10*2b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
11*2b15cb3dSCy Schubert  * are met:
12*2b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
13*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
14*2b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
15*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
16*2b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
17*2b15cb3dSCy Schubert  * 3. Neither the name ``Bruce Korb'' nor the name of any other
18*2b15cb3dSCy Schubert  *    contributor may be used to endorse or promote products derived
19*2b15cb3dSCy Schubert  *    from this software without specific prior written permission.
20*2b15cb3dSCy Schubert  *
21*2b15cb3dSCy Schubert  * str2enum IS PROVIDED BY Bruce Korb ``AS IS'' AND ANY EXPRESS
22*2b15cb3dSCy Schubert  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23*2b15cb3dSCy Schubert  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*2b15cb3dSCy Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL Bruce Korb OR ANY OTHER CONTRIBUTORS
25*2b15cb3dSCy Schubert  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*2b15cb3dSCy Schubert  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*2b15cb3dSCy Schubert  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28*2b15cb3dSCy Schubert  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*2b15cb3dSCy Schubert  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30*2b15cb3dSCy Schubert  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31*2b15cb3dSCy Schubert  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*2b15cb3dSCy Schubert  */
33*2b15cb3dSCy Schubert #include "option-xat-attribute.h"
34*2b15cb3dSCy Schubert /* ANSI-C code produced by gperf version 3.0.3 */
35*2b15cb3dSCy Schubert /* Command-line: gperf option-xat-attribute.gp  */
36*2b15cb3dSCy Schubert /* Computed positions: -k'1' */
37*2b15cb3dSCy Schubert 
38*2b15cb3dSCy Schubert 
39*2b15cb3dSCy Schubert 
40*2b15cb3dSCy Schubert # if 0 /* gperf build options: */
41*2b15cb3dSCy Schubert // %struct-type
42*2b15cb3dSCy Schubert // %language=ANSI-C
43*2b15cb3dSCy Schubert // %includes
44*2b15cb3dSCy Schubert // %global-table
45*2b15cb3dSCy Schubert // %omit-struct-type
46*2b15cb3dSCy Schubert // %readonly-tables
47*2b15cb3dSCy Schubert // %compare-strncmp
48*2b15cb3dSCy Schubert //
49*2b15cb3dSCy Schubert // %define slot-name               xat_name
50*2b15cb3dSCy Schubert // %define hash-function-name      option_xat_attribute_hash
51*2b15cb3dSCy Schubert // %define lookup-function-name    find_option_xat_attribute_name
52*2b15cb3dSCy Schubert // %define word-array-name         option_xat_attribute_table
53*2b15cb3dSCy Schubert // %define initializer-suffix      ,XAT_COUNT_CMD
54*2b15cb3dSCy Schubert //
55*2b15cb3dSCy Schubert # endif
56*2b15cb3dSCy Schubert 
57*2b15cb3dSCy Schubert #include "option-xat-attribute.h"
58*2b15cb3dSCy Schubert typedef struct {
59*2b15cb3dSCy Schubert     char const *    xat_name;
60*2b15cb3dSCy Schubert     option_xat_attribute_enum_t xat_id;
61*2b15cb3dSCy Schubert } option_xat_attribute_map_t;
62*2b15cb3dSCy Schubert #include <string.h>
63*2b15cb3dSCy Schubert 
64*2b15cb3dSCy Schubert /* maximum key range = 6, duplicates = 0 */
65*2b15cb3dSCy Schubert 
66*2b15cb3dSCy Schubert static unsigned int
67*2b15cb3dSCy Schubert option_xat_attribute_hash (register const char *str, register unsigned int len)
68*2b15cb3dSCy Schubert {
69*2b15cb3dSCy Schubert   static const unsigned char asso_values[] =
70*2b15cb3dSCy Schubert     {
71*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
72*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
73*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
74*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
75*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
76*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
77*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
78*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
79*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
80*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10, 0,
81*2b15cb3dSCy Schubert      10,10,10,10,10,10,10, 5,10, 0,
82*2b15cb3dSCy Schubert      10,10,10,10,10,10, 0, 0,10, 0,
83*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
84*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
85*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
86*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
87*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
88*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
89*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
90*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
91*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
92*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
93*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
94*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
95*2b15cb3dSCy Schubert      10,10,10,10,10,10,10,10,10,10,
96*2b15cb3dSCy Schubert      10,10,10,10,10,10
97*2b15cb3dSCy Schubert     };
98*2b15cb3dSCy Schubert   return len + asso_values[(unsigned char)str[0]];
99*2b15cb3dSCy Schubert }
100*2b15cb3dSCy Schubert 
101*2b15cb3dSCy Schubert static const option_xat_attribute_map_t option_xat_attribute_table[] =
102*2b15cb3dSCy Schubert   {
103*2b15cb3dSCy Schubert     {"",XAT_COUNT_CMD}, {"",XAT_COUNT_CMD},
104*2b15cb3dSCy Schubert     {"",XAT_COUNT_CMD}, {"",XAT_COUNT_CMD},
105*2b15cb3dSCy Schubert     {"type",     XAT_CMD_TYPE},
106*2b15cb3dSCy Schubert     {"words",    XAT_CMD_WORDS},
107*2b15cb3dSCy Schubert     {"cooked",   XAT_CMD_COOKED},
108*2b15cb3dSCy Schubert     {"members",  XAT_CMD_MEMBERS},
109*2b15cb3dSCy Schubert     {"uncooked", XAT_CMD_UNCOOKED},
110*2b15cb3dSCy Schubert     {"keep",     XAT_CMD_KEEP}
111*2b15cb3dSCy Schubert   };
112*2b15cb3dSCy Schubert 
113*2b15cb3dSCy Schubert static inline const option_xat_attribute_map_t *
114*2b15cb3dSCy Schubert find_option_xat_attribute_name (register const char *str, register unsigned int len)
115*2b15cb3dSCy Schubert {
116*2b15cb3dSCy Schubert   if (len <= 8 && len >= 4)
117*2b15cb3dSCy Schubert     {
118*2b15cb3dSCy Schubert       register int key = (int)option_xat_attribute_hash (str, len);
119*2b15cb3dSCy Schubert 
120*2b15cb3dSCy Schubert       if (key <= 9 && key >= 0)
121*2b15cb3dSCy Schubert         {
122*2b15cb3dSCy Schubert           register const char *s = option_xat_attribute_table[key].xat_name;
123*2b15cb3dSCy Schubert 
124*2b15cb3dSCy Schubert           if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')
125*2b15cb3dSCy Schubert             return &option_xat_attribute_table[key];
126*2b15cb3dSCy Schubert         }
127*2b15cb3dSCy Schubert     }
128*2b15cb3dSCy Schubert   return 0;
129*2b15cb3dSCy Schubert }
130*2b15cb3dSCy Schubert 
131*2b15cb3dSCy Schubert /**
132*2b15cb3dSCy Schubert  * Convert a command (keyword) to a option_xat_attribute_enum_t enumeration value.
133*2b15cb3dSCy Schubert  *
134*2b15cb3dSCy Schubert  * @param[in] str   a string that should start with a known key word.
135*2b15cb3dSCy Schubert  * @param[in] len   the provided length of the keyword at \a str.
136*2b15cb3dSCy Schubert  * @returns the enumeration value.
137*2b15cb3dSCy Schubert  * If not found, that value is XAT_INVALID_CMD.
138*2b15cb3dSCy Schubert  */
139*2b15cb3dSCy Schubert option_xat_attribute_enum_t
140*2b15cb3dSCy Schubert find_option_xat_attribute_cmd(char const * str, size_t len)
141*2b15cb3dSCy Schubert {
142*2b15cb3dSCy Schubert     option_xat_attribute_map_t const * map;
143*2b15cb3dSCy Schubert 
144*2b15cb3dSCy Schubert     map = find_option_xat_attribute_name(str, (unsigned int)len);
145*2b15cb3dSCy Schubert     return (map == NULL) ? XAT_INVALID_CMD : map->xat_id;
146*2b15cb3dSCy Schubert }
147*2b15cb3dSCy Schubert 
148*2b15cb3dSCy Schubert /* end of option-xat-attribute.c */
149