1*83d290c5STom Rini /* SPDX-License-Identifier: LGPL-2.1+ */ 2a6826fbcSWolfgang Denk /* 3a6826fbcSWolfgang Denk * Declarations for System V style searching functions. 4a6826fbcSWolfgang Denk * Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc. 5a6826fbcSWolfgang Denk * This file is part of the GNU C Library. 6a6826fbcSWolfgang Denk */ 7a6826fbcSWolfgang Denk 8a6826fbcSWolfgang Denk /* 9a6826fbcSWolfgang Denk * Based on code from uClibc-0.9.30.3 10a6826fbcSWolfgang Denk * Extensions for use within U-Boot 11ea009d47SWolfgang Denk * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de> 12a6826fbcSWolfgang Denk */ 13a6826fbcSWolfgang Denk 1436266435SRobert P. J. Day #ifndef _SEARCH_H_ 1536266435SRobert P. J. Day #define _SEARCH_H_ 16a6826fbcSWolfgang Denk 17a6826fbcSWolfgang Denk #include <stddef.h> 18a6826fbcSWolfgang Denk 19a6826fbcSWolfgang Denk #define __set_errno(val) do { errno = val; } while (0) 20a6826fbcSWolfgang Denk 217afcf3a5SJoe Hershberger enum env_op { 227afcf3a5SJoe Hershberger env_op_create, 237afcf3a5SJoe Hershberger env_op_delete, 247afcf3a5SJoe Hershberger env_op_overwrite, 257afcf3a5SJoe Hershberger }; 267afcf3a5SJoe Hershberger 2736266435SRobert P. J. Day /* Action which shall be performed in the call to hsearch. */ 28a6826fbcSWolfgang Denk typedef enum { 29a6826fbcSWolfgang Denk FIND, 30a6826fbcSWolfgang Denk ENTER 31a6826fbcSWolfgang Denk } ACTION; 32a6826fbcSWolfgang Denk 33a6826fbcSWolfgang Denk typedef struct entry { 3484b5e802SWolfgang Denk const char *key; 35a6826fbcSWolfgang Denk char *data; 36170ab110SJoe Hershberger int (*callback)(const char *name, const char *value, enum env_op op, 37170ab110SJoe Hershberger int flags); 382598090bSJoe Hershberger int flags; 39a6826fbcSWolfgang Denk } ENTRY; 40a6826fbcSWolfgang Denk 41a6826fbcSWolfgang Denk /* Opaque type for internal use. */ 42a6826fbcSWolfgang Denk struct _ENTRY; 43a6826fbcSWolfgang Denk 44a6826fbcSWolfgang Denk /* 45a6826fbcSWolfgang Denk * Family of hash table handling functions. The functions also 46a6826fbcSWolfgang Denk * have reentrant counterparts ending with _r. The non-reentrant 4736266435SRobert P. J. Day * functions all work on a single internal hash table. 48a6826fbcSWolfgang Denk */ 49a6826fbcSWolfgang Denk 50a6826fbcSWolfgang Denk /* Data type for reentrant functions. */ 51a6826fbcSWolfgang Denk struct hsearch_data { 52a6826fbcSWolfgang Denk struct _ENTRY *table; 53a6826fbcSWolfgang Denk unsigned int size; 54a6826fbcSWolfgang Denk unsigned int filled; 55c5983592SGerlando Falauto /* 56c5983592SGerlando Falauto * Callback function which will check whether the given change for variable 5736266435SRobert P. J. Day * "__item" to "newval" may be applied or not, and possibly apply such change. 58c5983592SGerlando Falauto * When (flag & H_FORCE) is set, it shall not print out any error message and 59c5983592SGerlando Falauto * shall force overwriting of write-once variables. 6036266435SRobert P. J. Day * Must return 0 for approval, 1 for denial. 61c5983592SGerlando Falauto */ 627afcf3a5SJoe Hershberger int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op, 637afcf3a5SJoe Hershberger int flag); 64a6826fbcSWolfgang Denk }; 65a6826fbcSWolfgang Denk 6636266435SRobert P. J. Day /* Create a new hash table which will contain at most "__nel" elements. */ 67a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab); 68a6826fbcSWolfgang Denk 6936266435SRobert P. J. Day /* Destroy current internal hash table. */ 70c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab); 71a6826fbcSWolfgang Denk 72a6826fbcSWolfgang Denk /* 7336266435SRobert P. J. Day * Search for entry matching __item.key in internal hash table. If 74a6826fbcSWolfgang Denk * ACTION is `FIND' return found entry or signal error by returning 75a6826fbcSWolfgang Denk * NULL. If ACTION is `ENTER' replace existing data (if any) with 7636266435SRobert P. J. Day * __item.data. 77a6826fbcSWolfgang Denk * */ 78a6826fbcSWolfgang Denk extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval, 79c4e0057fSJoe Hershberger struct hsearch_data *__htab, int __flag); 80a6826fbcSWolfgang Denk 81560d424bSMike Frysinger /* 8236266435SRobert P. J. Day * Search for an entry matching "__match". Otherwise, Same semantics 83560d424bSMike Frysinger * as hsearch_r(). 84560d424bSMike Frysinger */ 85560d424bSMike Frysinger extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval, 86560d424bSMike Frysinger struct hsearch_data *__htab); 87560d424bSMike Frysinger 8836266435SRobert P. J. Day /* Search and delete entry matching "__key" in internal hash table. */ 89152874b6SGerlando Falauto extern int hdelete_r(const char *__key, struct hsearch_data *__htab, 90c4e0057fSJoe Hershberger int __flag); 91a6826fbcSWolfgang Denk 92a6826fbcSWolfgang Denk extern ssize_t hexport_r(struct hsearch_data *__htab, 93be11235aSJoe Hershberger const char __sep, int __flag, char **__resp, size_t __size, 9437f2fe74SWolfgang Denk int argc, char * const argv[]); 95a6826fbcSWolfgang Denk 96348b1f1cSGerlando Falauto /* 97348b1f1cSGerlando Falauto * nvars: length of vars array 98348b1f1cSGerlando Falauto * vars: array of strings (variable names) to import (nvars == 0 means all) 99348b1f1cSGerlando Falauto */ 100a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab, 101a6826fbcSWolfgang Denk const char *__env, size_t __size, const char __sep, 102ecd1446fSAlexander Holler int __flag, int __crlf_is_lf, int nvars, 103ecd1446fSAlexander Holler char * const vars[]); 104a6826fbcSWolfgang Denk 105170ab110SJoe Hershberger /* Walk the whole table calling the callback on each element */ 106170ab110SJoe Hershberger extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *)); 107170ab110SJoe Hershberger 108be11235aSJoe Hershberger /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */ 109c3f65258SGerlando Falauto #define H_NOCLEAR (1 << 0) /* do not clear hash table before importing */ 110c3f65258SGerlando Falauto #define H_FORCE (1 << 1) /* overwrite read-only/write-once variables */ 111c4e0057fSJoe Hershberger #define H_INTERACTIVE (1 << 2) /* indicate that an import is user directed */ 112be11235aSJoe Hershberger #define H_HIDE_DOT (1 << 3) /* don't print env vars that begin with '.' */ 113ea009d47SWolfgang Denk #define H_MATCH_KEY (1 << 4) /* search/grep key = variable names */ 114ea009d47SWolfgang Denk #define H_MATCH_DATA (1 << 5) /* search/grep data = variable values */ 115ea009d47SWolfgang Denk #define H_MATCH_BOTH (H_MATCH_KEY | H_MATCH_DATA) /* search/grep both */ 116ea009d47SWolfgang Denk #define H_MATCH_IDENT (1 << 6) /* search for indentical strings */ 117ea009d47SWolfgang Denk #define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */ 118be29df6aSWolfgang Denk #define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */ 119be29df6aSWolfgang Denk #define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX) 120382bee57SSimon Glass #define H_PROGRAMMATIC (1 << 9) /* indicate that an import is from env_set() */ 12194b467b1SJoe Hershberger #define H_ORIGIN_FLAGS (H_INTERACTIVE | H_PROGRAMMATIC) 122a6826fbcSWolfgang Denk 12336266435SRobert P. J. Day #endif /* _SEARCH_H_ */ 124