xref: /rk3399_rockchip-uboot/include/search.h (revision 170ab11075d3be56e89d6444abf1148329130f4b)
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
25a6826fbcSWolfgang Denk  * Copyright (C) 2010 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;
50*170ab110SJoe Hershberger 	int (*callback)(const char *name, const char *value, enum env_op op,
51*170ab110SJoe Hershberger 		int flags);
52a6826fbcSWolfgang Denk } ENTRY;
53a6826fbcSWolfgang Denk 
54a6826fbcSWolfgang Denk /* Opaque type for internal use.  */
55a6826fbcSWolfgang Denk struct _ENTRY;
56a6826fbcSWolfgang Denk 
57a6826fbcSWolfgang Denk /*
58a6826fbcSWolfgang Denk  * Family of hash table handling functions.  The functions also
59a6826fbcSWolfgang Denk  * have reentrant counterparts ending with _r.  The non-reentrant
60a6826fbcSWolfgang Denk  * functions all work on a signle internal hashing table.
61a6826fbcSWolfgang Denk  */
62a6826fbcSWolfgang Denk 
63a6826fbcSWolfgang Denk /* Data type for reentrant functions.  */
64a6826fbcSWolfgang Denk struct hsearch_data {
65a6826fbcSWolfgang Denk 	struct _ENTRY *table;
66a6826fbcSWolfgang Denk 	unsigned int size;
67a6826fbcSWolfgang Denk 	unsigned int filled;
68c5983592SGerlando Falauto /*
69c5983592SGerlando Falauto  * Callback function which will check whether the given change for variable
707afcf3a5SJoe Hershberger  * "item" to "newval" may be applied or not, and possibly apply such change.
71c5983592SGerlando Falauto  * When (flag & H_FORCE) is set, it shall not print out any error message and
72c5983592SGerlando Falauto  * shall force overwriting of write-once variables.
73c5983592SGerlando Falauto .* Must return 0 for approval, 1 for denial.
74c5983592SGerlando Falauto  */
757afcf3a5SJoe Hershberger 	int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op,
767afcf3a5SJoe Hershberger 		int flag);
77a6826fbcSWolfgang Denk };
78a6826fbcSWolfgang Denk 
79a6826fbcSWolfgang Denk /* Create a new hashing table which will at most contain NEL elements.  */
80a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab);
81a6826fbcSWolfgang Denk 
82a6826fbcSWolfgang Denk /* Destroy current internal hashing table.  */
83c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab);
84a6826fbcSWolfgang Denk 
85a6826fbcSWolfgang Denk /*
86a6826fbcSWolfgang Denk  * Search for entry matching ITEM.key in internal hash table.  If
87a6826fbcSWolfgang Denk  * ACTION is `FIND' return found entry or signal error by returning
88a6826fbcSWolfgang Denk  * NULL.  If ACTION is `ENTER' replace existing data (if any) with
89a6826fbcSWolfgang Denk  * ITEM.data.
90a6826fbcSWolfgang Denk  * */
91a6826fbcSWolfgang Denk extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval,
92c4e0057fSJoe Hershberger 		     struct hsearch_data *__htab, int __flag);
93a6826fbcSWolfgang Denk 
94560d424bSMike Frysinger /*
95560d424bSMike Frysinger  * Search for an entry matching `MATCH'.  Otherwise, Same semantics
96560d424bSMike Frysinger  * as hsearch_r().
97560d424bSMike Frysinger  */
98560d424bSMike Frysinger extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval,
99560d424bSMike Frysinger 		    struct hsearch_data *__htab);
100a000b795SKim Phillips /*
101a000b795SKim Phillips  * Search for an entry whose key or data contains `MATCH'.  Otherwise,
102a000b795SKim Phillips  * Same semantics as hsearch_r().
103a000b795SKim Phillips  */
104a000b795SKim Phillips extern int hstrstr_r(const char *__match, int __last_idx, ENTRY ** __retval,
105a000b795SKim Phillips 		    struct hsearch_data *__htab);
106560d424bSMike Frysinger 
107a6826fbcSWolfgang Denk /* Search and delete entry matching ITEM.key in internal hash table. */
108152874b6SGerlando Falauto extern int hdelete_r(const char *__key, struct hsearch_data *__htab,
109c4e0057fSJoe Hershberger 		     int __flag);
110a6826fbcSWolfgang Denk 
111a6826fbcSWolfgang Denk extern ssize_t hexport_r(struct hsearch_data *__htab,
112be11235aSJoe Hershberger 		     const char __sep, int __flag, char **__resp, size_t __size,
11337f2fe74SWolfgang Denk 		     int argc, char * const argv[]);
114a6826fbcSWolfgang Denk 
115348b1f1cSGerlando Falauto /*
116348b1f1cSGerlando Falauto  * nvars: length of vars array
117348b1f1cSGerlando Falauto  * vars: array of strings (variable names) to import (nvars == 0 means all)
118c5983592SGerlando Falauto  * do_apply: whether to call callback function to check the new argument,
119c5983592SGerlando Falauto  * and possibly apply changes (false means accept everything)
120348b1f1cSGerlando Falauto  */
121a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab,
122a6826fbcSWolfgang Denk 		     const char *__env, size_t __size, const char __sep,
123c4e0057fSJoe Hershberger 		     int __flag, int nvars, char * const vars[]);
124a6826fbcSWolfgang Denk 
125*170ab110SJoe Hershberger /* Walk the whole table calling the callback on each element */
126*170ab110SJoe Hershberger extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
127*170ab110SJoe Hershberger 
128be11235aSJoe Hershberger /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */
129c3f65258SGerlando Falauto #define H_NOCLEAR	(1 << 0) /* do not clear hash table before importing */
130c3f65258SGerlando Falauto #define H_FORCE		(1 << 1) /* overwrite read-only/write-once variables */
131c4e0057fSJoe Hershberger #define H_INTERACTIVE	(1 << 2) /* indicate that an import is user directed */
132be11235aSJoe Hershberger #define H_HIDE_DOT	(1 << 3) /* don't print env vars that begin with '.' */
133a6826fbcSWolfgang Denk 
134a6826fbcSWolfgang Denk #endif /* search.h */
135