GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / lib / lib_misc.c
blobe5a2a02463c925a9092d80f1b4b8b7907cbf9d2f
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * Misc. library routines File: lib_misc.c
5 *
6 * Miscellaneous library routines.
7 *
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
9 *
10 *********************************************************************
12 * Copyright 2000,2001,2002,2003
13 * Broadcom Corporation. All rights reserved.
15 * This software is furnished under license and may be used and
16 * copied only in accordance with the following terms and
17 * conditions. Subject to these conditions, you may download,
18 * copy, install, use, modify and distribute modified or unmodified
19 * copies of this software in source and/or binary form. No title
20 * or ownership is transferred hereby.
22 * 1) Any source code used, modified or distributed must reproduce
23 * and retain this copyright notice and list of conditions
24 * as they appear in the source file.
26 * 2) No right is granted to use any trade name, trademark, or
27 * logo of Broadcom Corporation. The "Broadcom Corporation"
28 * name may not be used to endorse or promote products derived
29 * from this software without the prior written permission of
30 * Broadcom Corporation.
32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44 * THE POSSIBILITY OF SUCH DAMAGE.
45 ********************************************************************* */
48 #include "lib_types.h"
49 #include "lib_malloc.h"
50 #define _LIB_NO_MACROS_
51 #include "lib_string.h"
54 /* *********************************************************************
55 * lib_parseipaddr(ipaddr,dest)
57 * Parse an IP address.
59 * Input parameters:
60 * ipaddr - string of IP address
61 * dest - pointer to 4 bytes to receive binary IP address
63 * Return value:
64 * 0 if ok
65 * -1 if ip address is invalid
66 ********************************************************************* */
68 int lib_parseipaddr(const char *ipaddr,uint8_t *dest)
70 int a,b,c,d;
71 char *x;
73 /* make sure it's all digits and dots. */
74 x = (char *) ipaddr;
75 while (*x) {
76 if ((*x == '.') || ((*x >= '0') && (*x <= '9'))) {
77 x++;
78 continue;
80 return -1;
83 x = (char *) ipaddr;
84 a = lib_atoi(ipaddr);
85 x = lib_strchr(x,'.');
86 if (!x) return -1;
87 b = lib_atoi(x+1);
88 x = lib_strchr(x+1,'.');
89 if (!x) return -1;
90 c = lib_atoi(x+1);
91 x = lib_strchr(x+1,'.');
92 if (!x) return -1;
93 d = lib_atoi(x+1);
95 if ((a < 0) || (a > 255)) return -1;
96 if ((b < 0) || (b > 255)) return -1;
97 if ((c < 0) || (c > 255)) return -1;
98 if ((d < 0) || (d > 255)) return -1;
100 dest[0] = (uint8_t) a;
101 dest[1] = (uint8_t) b;
102 dest[2] = (uint8_t) c;
103 dest[3] = (uint8_t) d;
105 return 0;
109 /* *********************************************************************
110 * lib_lookup(list,str)
112 * Look up an element on a {string,value} list.
114 * Input parameters:
115 * list - list to search
116 * str - string to find on the list
118 * Return value:
119 * 0 if string was not found
120 * else number associated with this string
121 ********************************************************************* */
123 int lib_lookup(const cons_t *list,char *str)
125 while (list->str) {
126 if (lib_strcmp(list->str,str) == 0) return list->num;
127 list++;
130 return 0;
134 /* *********************************************************************
135 * lib_findinlist(list,str)
137 * Like lib_lookup but returns cons structure instead of value
139 * Input parameters:
140 * list - list of associations
141 * str - what to find
143 * Return value:
144 * cons_t or null if not found
145 ********************************************************************* */
147 static const cons_t *lib_findinlist(const cons_t *list,char *str)
149 while (list->str) {
150 if (lib_strcmp(list->str,str) == 0) return list;
151 list++;
153 return NULL;
157 /* *********************************************************************
158 * lib_setoptions(list,str,flags)
160 * Set or reset one or more bits in a flags variable based
161 * on the list of valid bits and a string containing what
162 * to change. flags starts off as a default value.
164 * The input string is a comma-separated list of options,
165 * optionally prefixed by "no_" or "no" to invert the
166 * sense of the option. negative values in the table
167 * remove options, positive add options (you can't use
168 * bit 31 as an option for this reason).
170 * Input parameters:
171 * list - list of valid options
172 * str - options to parse
173 * flags - pointer to variable to be modified
175 * Return value:
176 * number of options we did not understand, 0=ok
177 ********************************************************************* */
179 int lib_setoptions(const cons_t *list,char *str,unsigned int *flags)
181 char *dupstr;
182 char *x;
183 char *ptr;
184 const cons_t *val;
185 int newbits;
186 int errors = 0;
188 if (!list || !str || !flags) return 0;
190 dupstr = lib_strdup(str);
191 if (!dupstr) return 0;
193 ptr = dupstr;
195 while (*ptr) {
196 if ((x = lib_strchr(ptr,','))) {
197 *x = '\0';
200 val = lib_findinlist(list,ptr);
201 newbits = 0;
202 if (!val) {
203 if (lib_memcmp(ptr,"no_",3) == 0) {
204 val = lib_findinlist(list,ptr+3);
206 else if (lib_memcmp(ptr,"no",2) == 0) {
207 val = lib_findinlist(list,ptr+2);
209 if (val) newbits = ~((unsigned int) (val->num));
210 else errors++;
212 else {
213 newbits = (val->num);
216 /* if new bits are negative, it's an AND mask
217 otherwise it's an OR mask */
219 if (newbits < 0) *flags &= (unsigned int) newbits;
220 else *flags |= (unsigned int) newbits;
222 if (x) ptr = x+1;
223 else break;
226 KFREE(dupstr);
228 return errors;