xref: /rk3399_rockchip-uboot/include/search.h (revision 7afcf3a55b5f484b3d3442053fae8186a3fb92d7)
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 
35*7afcf3a5SJoe Hershberger enum env_op {
36*7afcf3a5SJoe Hershberger 	env_op_create,
37*7afcf3a5SJoe Hershberger 	env_op_delete,
38*7afcf3a5SJoe Hershberger 	env_op_overwrite,
39*7afcf3a5SJoe Hershberger };
40*7afcf3a5SJoe 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;
50a6826fbcSWolfgang Denk } ENTRY;
51a6826fbcSWolfgang Denk 
52a6826fbcSWolfgang Denk /* Opaque type for internal use.  */
53a6826fbcSWolfgang Denk struct _ENTRY;
54a6826fbcSWolfgang Denk 
55a6826fbcSWolfgang Denk /*
56a6826fbcSWolfgang Denk  * Family of hash table handling functions.  The functions also
57a6826fbcSWolfgang Denk  * have reentrant counterparts ending with _r.  The non-reentrant
58a6826fbcSWolfgang Denk  * functions all work on a signle internal hashing table.
59a6826fbcSWolfgang Denk  */
60a6826fbcSWolfgang Denk 
61a6826fbcSWolfgang Denk /* Data type for reentrant functions.  */
62a6826fbcSWolfgang Denk struct hsearch_data {
63a6826fbcSWolfgang Denk 	struct _ENTRY *table;
64a6826fbcSWolfgang Denk 	unsigned int size;
65a6826fbcSWolfgang Denk 	unsigned int filled;
66c5983592SGerlando Falauto /*
67c5983592SGerlando Falauto  * Callback function which will check whether the given change for variable
68*7afcf3a5SJoe Hershberger  * "item" to "newval" may be applied or not, and possibly apply such change.
69c5983592SGerlando Falauto  * When (flag & H_FORCE) is set, it shall not print out any error message and
70c5983592SGerlando Falauto  * shall force overwriting of write-once variables.
71c5983592SGerlando Falauto .* Must return 0 for approval, 1 for denial.
72c5983592SGerlando Falauto  */
73*7afcf3a5SJoe Hershberger 	int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op,
74*7afcf3a5SJoe Hershberger 		int flag);
75a6826fbcSWolfgang Denk };
76a6826fbcSWolfgang Denk 
77a6826fbcSWolfgang Denk /* Create a new hashing table which will at most contain NEL elements.  */
78a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab);
79a6826fbcSWolfgang Denk 
80a6826fbcSWolfgang Denk /* Destroy current internal hashing table.  */
81c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab);
82a6826fbcSWolfgang Denk 
83a6826fbcSWolfgang Denk /*
84a6826fbcSWolfgang Denk  * Search for entry matching ITEM.key in internal hash table.  If
85a6826fbcSWolfgang Denk  * ACTION is `FIND' return found entry or signal error by returning
86a6826fbcSWolfgang Denk  * NULL.  If ACTION is `ENTER' replace existing data (if any) with
87a6826fbcSWolfgang Denk  * ITEM.data.
88a6826fbcSWolfgang Denk  * */
89a6826fbcSWolfgang Denk extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval,
90c4e0057fSJoe Hershberger 		     struct hsearch_data *__htab, int __flag);
91a6826fbcSWolfgang Denk 
92560d424bSMike Frysinger /*
93560d424bSMike Frysinger  * Search for an entry matching `MATCH'.  Otherwise, Same semantics
94560d424bSMike Frysinger  * as hsearch_r().
95560d424bSMike Frysinger  */
96560d424bSMike Frysinger extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval,
97560d424bSMike Frysinger 		    struct hsearch_data *__htab);
98a000b795SKim Phillips /*
99a000b795SKim Phillips  * Search for an entry whose key or data contains `MATCH'.  Otherwise,
100a000b795SKim Phillips  * Same semantics as hsearch_r().
101a000b795SKim Phillips  */
102a000b795SKim Phillips extern int hstrstr_r(const char *__match, int __last_idx, ENTRY ** __retval,
103a000b795SKim Phillips 		    struct hsearch_data *__htab);
104560d424bSMike Frysinger 
105a6826fbcSWolfgang Denk /* Search and delete entry matching ITEM.key in internal hash table. */
106152874b6SGerlando Falauto extern int hdelete_r(const char *__key, struct hsearch_data *__htab,
107c4e0057fSJoe Hershberger 		     int __flag);
108a6826fbcSWolfgang Denk 
109a6826fbcSWolfgang Denk extern ssize_t hexport_r(struct hsearch_data *__htab,
11037f2fe74SWolfgang Denk 		     const char __sep, char **__resp, size_t __size,
11137f2fe74SWolfgang Denk 		     int argc, char * const argv[]);
112a6826fbcSWolfgang Denk 
113348b1f1cSGerlando Falauto /*
114348b1f1cSGerlando Falauto  * nvars: length of vars array
115348b1f1cSGerlando Falauto  * vars: array of strings (variable names) to import (nvars == 0 means all)
116c5983592SGerlando Falauto  * do_apply: whether to call callback function to check the new argument,
117c5983592SGerlando Falauto  * and possibly apply changes (false means accept everything)
118348b1f1cSGerlando Falauto  */
119a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab,
120a6826fbcSWolfgang Denk 		     const char *__env, size_t __size, const char __sep,
121c4e0057fSJoe Hershberger 		     int __flag, int nvars, char * const vars[]);
122a6826fbcSWolfgang Denk 
123c4e0057fSJoe Hershberger /* Flags for himport_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 */
127a6826fbcSWolfgang Denk 
128a6826fbcSWolfgang Denk #endif /* search.h */
129