xref: /OK3568_Linux_fs/u-boot/include/search.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Declarations for System V style searching functions.
3*4882a593Smuzhiyun  * Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
4*4882a593Smuzhiyun  * This file is part of the GNU C Library.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * SPDX-License-Identifier:	LGPL-2.1+
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Based on code from uClibc-0.9.30.3
11*4882a593Smuzhiyun  * Extensions for use within U-Boot
12*4882a593Smuzhiyun  * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de>
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifndef _SEARCH_H_
16*4882a593Smuzhiyun #define _SEARCH_H_
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <stddef.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define __set_errno(val) do { errno = val; } while (0)
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun enum env_op {
23*4882a593Smuzhiyun 	env_op_create,
24*4882a593Smuzhiyun 	env_op_delete,
25*4882a593Smuzhiyun 	env_op_overwrite,
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Action which shall be performed in the call to hsearch.  */
29*4882a593Smuzhiyun typedef enum {
30*4882a593Smuzhiyun 	FIND,
31*4882a593Smuzhiyun 	ENTER
32*4882a593Smuzhiyun } ACTION;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun typedef struct entry {
35*4882a593Smuzhiyun 	const char *key;
36*4882a593Smuzhiyun 	char *data;
37*4882a593Smuzhiyun 	int (*callback)(const char *name, const char *value, enum env_op op,
38*4882a593Smuzhiyun 		int flags);
39*4882a593Smuzhiyun 	int flags;
40*4882a593Smuzhiyun } ENTRY;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* Opaque type for internal use.  */
43*4882a593Smuzhiyun struct _ENTRY;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * Family of hash table handling functions.  The functions also
47*4882a593Smuzhiyun  * have reentrant counterparts ending with _r.  The non-reentrant
48*4882a593Smuzhiyun  * functions all work on a single internal hash table.
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* Data type for reentrant functions.  */
52*4882a593Smuzhiyun struct hsearch_data {
53*4882a593Smuzhiyun 	struct _ENTRY *table;
54*4882a593Smuzhiyun 	unsigned int size;
55*4882a593Smuzhiyun 	unsigned int filled;
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun  * Callback function which will check whether the given change for variable
58*4882a593Smuzhiyun  * "__item" to "newval" may be applied or not, and possibly apply such change.
59*4882a593Smuzhiyun  * When (flag & H_FORCE) is set, it shall not print out any error message and
60*4882a593Smuzhiyun  * shall force overwriting of write-once variables.
61*4882a593Smuzhiyun  * Must return 0 for approval, 1 for denial.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 	int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op,
64*4882a593Smuzhiyun 		int flag);
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* Create a new hash table which will contain at most "__nel" elements.  */
68*4882a593Smuzhiyun extern int hcreate_r(size_t __nel, struct hsearch_data *__htab);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* Destroy current internal hash table.  */
71*4882a593Smuzhiyun extern void hdestroy_r(struct hsearch_data *__htab);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * Search for entry matching __item.key in internal hash table.  If
75*4882a593Smuzhiyun  * ACTION is `FIND' return found entry or signal error by returning
76*4882a593Smuzhiyun  * NULL.  If ACTION is `ENTER' replace existing data (if any) with
77*4882a593Smuzhiyun  * __item.data.
78*4882a593Smuzhiyun  * */
79*4882a593Smuzhiyun extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval,
80*4882a593Smuzhiyun 		     struct hsearch_data *__htab, int __flag);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun  * Search for an entry matching "__match".  Otherwise, Same semantics
84*4882a593Smuzhiyun  * as hsearch_r().
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval,
87*4882a593Smuzhiyun 		    struct hsearch_data *__htab);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* Search and delete entry matching "__key" in internal hash table. */
90*4882a593Smuzhiyun extern int hdelete_r(const char *__key, struct hsearch_data *__htab,
91*4882a593Smuzhiyun 		     int __flag);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun extern ssize_t hexport_r(struct hsearch_data *__htab,
94*4882a593Smuzhiyun 		     const char __sep, int __flag, char **__resp, size_t __size,
95*4882a593Smuzhiyun 		     int argc, char * const argv[]);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * nvars: length of vars array
99*4882a593Smuzhiyun  * vars: array of strings (variable names) to import (nvars == 0 means all)
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun extern int himport_r(struct hsearch_data *__htab,
102*4882a593Smuzhiyun 		     const char *__env, size_t __size, const char __sep,
103*4882a593Smuzhiyun 		     int __flag, int __crlf_is_lf, int nvars,
104*4882a593Smuzhiyun 		     char * const vars[]);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* Walk the whole table calling the callback on each element */
107*4882a593Smuzhiyun extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */
110*4882a593Smuzhiyun #define H_NOCLEAR	(1 << 0) /* do not clear hash table before importing */
111*4882a593Smuzhiyun #define H_FORCE		(1 << 1) /* overwrite read-only/write-once variables */
112*4882a593Smuzhiyun #define H_INTERACTIVE	(1 << 2) /* indicate that an import is user directed */
113*4882a593Smuzhiyun #define H_HIDE_DOT	(1 << 3) /* don't print env vars that begin with '.' */
114*4882a593Smuzhiyun #define H_MATCH_KEY	(1 << 4) /* search/grep key  = variable names	     */
115*4882a593Smuzhiyun #define H_MATCH_DATA	(1 << 5) /* search/grep data = variable values	     */
116*4882a593Smuzhiyun #define H_MATCH_BOTH	(H_MATCH_KEY | H_MATCH_DATA) /* search/grep both     */
117*4882a593Smuzhiyun #define H_MATCH_IDENT	(1 << 6) /* search for indentical strings	     */
118*4882a593Smuzhiyun #define H_MATCH_SUBSTR	(1 << 7) /* search for substring matches	     */
119*4882a593Smuzhiyun #define H_MATCH_REGEX	(1 << 8) /* search for regular expression matches    */
120*4882a593Smuzhiyun #define H_MATCH_METHOD	(H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX)
121*4882a593Smuzhiyun #define H_PROGRAMMATIC	(1 << 9) /* indicate that an import is from env_set() */
122*4882a593Smuzhiyun #define H_ORIGIN_FLAGS	(H_INTERACTIVE | H_PROGRAMMATIC)
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun #endif /* _SEARCH_H_ */
125