xref: /rk3399_rockchip-uboot/include/search.h (revision be29df6a1ac286e6c482828db28ca96e187c7e00)
1a6826fbcSWolfgang Denk /*
2a6826fbcSWolfgang Denk  * Declarations for System V style searching functions.
3a6826fbcSWolfgang Denk  * Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
4a6826fbcSWolfgang Denk  * This file is part of the GNU C Library.
5a6826fbcSWolfgang Denk  *
6a6826fbcSWolfgang Denk  * The GNU C Library is free software; you can redistribute it and/or
7a6826fbcSWolfgang Denk  * modify it under the terms of the GNU Lesser General Public
8a6826fbcSWolfgang Denk  * License as published by the Free Software Foundation; either
9a6826fbcSWolfgang Denk  * version 2.1 of the License, or (at your option) any later version.
10a6826fbcSWolfgang Denk  *
11a6826fbcSWolfgang Denk  * The GNU C Library is distributed in the hope that it will be useful,
12a6826fbcSWolfgang Denk  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a6826fbcSWolfgang Denk  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14a6826fbcSWolfgang Denk  * Lesser General Public License for more details.
15a6826fbcSWolfgang Denk  *
16a6826fbcSWolfgang Denk  * You should have received a copy of the GNU Lesser General Public
17a6826fbcSWolfgang Denk  * License along with the GNU C Library; if not, write to the Free
18a6826fbcSWolfgang Denk  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19a6826fbcSWolfgang Denk  * 02111-1307 USA.
20a6826fbcSWolfgang Denk  */
21a6826fbcSWolfgang Denk 
22a6826fbcSWolfgang Denk /*
23a6826fbcSWolfgang Denk  * Based on code from uClibc-0.9.30.3
24a6826fbcSWolfgang Denk  * Extensions for use within U-Boot
25ea009d47SWolfgang Denk  * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de>
26a6826fbcSWolfgang Denk  */
27a6826fbcSWolfgang Denk 
28a6826fbcSWolfgang Denk #ifndef _SEARCH_H
29a6826fbcSWolfgang Denk #define	_SEARCH_H 1
30a6826fbcSWolfgang Denk 
31a6826fbcSWolfgang Denk #include <stddef.h>
32a6826fbcSWolfgang Denk 
33a6826fbcSWolfgang Denk #define __set_errno(val) do { errno = val; } while (0)
34a6826fbcSWolfgang Denk 
357afcf3a5SJoe Hershberger enum env_op {
367afcf3a5SJoe Hershberger 	env_op_create,
377afcf3a5SJoe Hershberger 	env_op_delete,
387afcf3a5SJoe Hershberger 	env_op_overwrite,
397afcf3a5SJoe Hershberger };
407afcf3a5SJoe Hershberger 
41a6826fbcSWolfgang Denk /* Action which shall be performed in the call the hsearch.  */
42a6826fbcSWolfgang Denk typedef enum {
43a6826fbcSWolfgang Denk 	FIND,
44a6826fbcSWolfgang Denk 	ENTER
45a6826fbcSWolfgang Denk } ACTION;
46a6826fbcSWolfgang Denk 
47a6826fbcSWolfgang Denk typedef struct entry {
4884b5e802SWolfgang Denk 	const char *key;
49a6826fbcSWolfgang Denk 	char *data;
50170ab110SJoe Hershberger 	int (*callback)(const char *name, const char *value, enum env_op op,
51170ab110SJoe Hershberger 		int flags);
522598090bSJoe Hershberger 	int flags;
53a6826fbcSWolfgang Denk } ENTRY;
54a6826fbcSWolfgang Denk 
55a6826fbcSWolfgang Denk /* Opaque type for internal use.  */
56a6826fbcSWolfgang Denk struct _ENTRY;
57a6826fbcSWolfgang Denk 
58a6826fbcSWolfgang Denk /*
59a6826fbcSWolfgang Denk  * Family of hash table handling functions.  The functions also
60a6826fbcSWolfgang Denk  * have reentrant counterparts ending with _r.  The non-reentrant
61a6826fbcSWolfgang Denk  * functions all work on a signle internal hashing table.
62a6826fbcSWolfgang Denk  */
63a6826fbcSWolfgang Denk 
64a6826fbcSWolfgang Denk /* Data type for reentrant functions.  */
65a6826fbcSWolfgang Denk struct hsearch_data {
66a6826fbcSWolfgang Denk 	struct _ENTRY *table;
67a6826fbcSWolfgang Denk 	unsigned int size;
68a6826fbcSWolfgang Denk 	unsigned int filled;
69c5983592SGerlando Falauto /*
70c5983592SGerlando Falauto  * Callback function which will check whether the given change for variable
717afcf3a5SJoe Hershberger  * "item" to "newval" may be applied or not, and possibly apply such change.
72c5983592SGerlando Falauto  * When (flag & H_FORCE) is set, it shall not print out any error message and
73c5983592SGerlando Falauto  * shall force overwriting of write-once variables.
74c5983592SGerlando Falauto .* Must return 0 for approval, 1 for denial.
75c5983592SGerlando Falauto  */
767afcf3a5SJoe Hershberger 	int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op,
777afcf3a5SJoe Hershberger 		int flag);
78a6826fbcSWolfgang Denk };
79a6826fbcSWolfgang Denk 
80a6826fbcSWolfgang Denk /* Create a new hashing table which will at most contain NEL elements.  */
81a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab);
82a6826fbcSWolfgang Denk 
83a6826fbcSWolfgang Denk /* Destroy current internal hashing table.  */
84c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab);
85a6826fbcSWolfgang Denk 
86a6826fbcSWolfgang Denk /*
87a6826fbcSWolfgang Denk  * Search for entry matching ITEM.key in internal hash table.  If
88a6826fbcSWolfgang Denk  * ACTION is `FIND' return found entry or signal error by returning
89a6826fbcSWolfgang Denk  * NULL.  If ACTION is `ENTER' replace existing data (if any) with
90a6826fbcSWolfgang Denk  * ITEM.data.
91a6826fbcSWolfgang Denk  * */
92a6826fbcSWolfgang Denk extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval,
93c4e0057fSJoe Hershberger 		     struct hsearch_data *__htab, int __flag);
94a6826fbcSWolfgang Denk 
95560d424bSMike Frysinger /*
96560d424bSMike Frysinger  * Search for an entry matching `MATCH'.  Otherwise, Same semantics
97560d424bSMike Frysinger  * as hsearch_r().
98560d424bSMike Frysinger  */
99560d424bSMike Frysinger extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval,
100560d424bSMike Frysinger 		    struct hsearch_data *__htab);
101560d424bSMike Frysinger 
102a6826fbcSWolfgang Denk /* Search and delete entry matching ITEM.key in internal hash table. */
103152874b6SGerlando Falauto extern int hdelete_r(const char *__key, struct hsearch_data *__htab,
104c4e0057fSJoe Hershberger 		     int __flag);
105a6826fbcSWolfgang Denk 
106a6826fbcSWolfgang Denk extern ssize_t hexport_r(struct hsearch_data *__htab,
107be11235aSJoe Hershberger 		     const char __sep, int __flag, char **__resp, size_t __size,
10837f2fe74SWolfgang Denk 		     int argc, char * const argv[]);
109a6826fbcSWolfgang Denk 
110348b1f1cSGerlando Falauto /*
111348b1f1cSGerlando Falauto  * nvars: length of vars array
112348b1f1cSGerlando Falauto  * vars: array of strings (variable names) to import (nvars == 0 means all)
113c5983592SGerlando Falauto  * do_apply: whether to call callback function to check the new argument,
114c5983592SGerlando Falauto  * and possibly apply changes (false means accept everything)
115348b1f1cSGerlando Falauto  */
116a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab,
117a6826fbcSWolfgang Denk 		     const char *__env, size_t __size, const char __sep,
118c4e0057fSJoe Hershberger 		     int __flag, int nvars, char * const vars[]);
119a6826fbcSWolfgang Denk 
120170ab110SJoe Hershberger /* Walk the whole table calling the callback on each element */
121170ab110SJoe Hershberger extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
122170ab110SJoe Hershberger 
123be11235aSJoe Hershberger /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */
124c3f65258SGerlando Falauto #define H_NOCLEAR	(1 << 0) /* do not clear hash table before importing */
125c3f65258SGerlando Falauto #define H_FORCE		(1 << 1) /* overwrite read-only/write-once variables */
126c4e0057fSJoe Hershberger #define H_INTERACTIVE	(1 << 2) /* indicate that an import is user directed */
127be11235aSJoe Hershberger #define H_HIDE_DOT	(1 << 3) /* don't print env vars that begin with '.' */
128ea009d47SWolfgang Denk #define H_MATCH_KEY	(1 << 4) /* search/grep key  = variable names	     */
129ea009d47SWolfgang Denk #define H_MATCH_DATA	(1 << 5) /* search/grep data = variable values	     */
130ea009d47SWolfgang Denk #define H_MATCH_BOTH	(H_MATCH_KEY | H_MATCH_DATA) /* search/grep both     */
131ea009d47SWolfgang Denk #define H_MATCH_IDENT	(1 << 6) /* search for indentical strings	     */
132ea009d47SWolfgang Denk #define H_MATCH_SUBSTR	(1 << 7) /* search for substring matches	     */
133*be29df6aSWolfgang Denk #define H_MATCH_REGEX	(1 << 8) /* search for regular expression matches    */
134*be29df6aSWolfgang Denk #define H_MATCH_METHOD	(H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX)
135a6826fbcSWolfgang Denk 
136a6826fbcSWolfgang Denk #endif /* search.h */
137