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 * 6eee479cfSWolfgang Denk * SPDX-License-Identifier: LGPL-2.1+ 7a6826fbcSWolfgang Denk */ 8a6826fbcSWolfgang Denk 9a6826fbcSWolfgang Denk /* 10a6826fbcSWolfgang Denk * Based on code from uClibc-0.9.30.3 11a6826fbcSWolfgang Denk * Extensions for use within U-Boot 12ea009d47SWolfgang Denk * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de> 13a6826fbcSWolfgang Denk */ 14a6826fbcSWolfgang Denk 1536266435SRobert P. J. Day #ifndef _SEARCH_H_ 1636266435SRobert P. J. Day #define _SEARCH_H_ 17a6826fbcSWolfgang Denk 18a6826fbcSWolfgang Denk #include <stddef.h> 19a6826fbcSWolfgang Denk 20a6826fbcSWolfgang Denk #define __set_errno(val) do { errno = val; } while (0) 21a6826fbcSWolfgang Denk 227afcf3a5SJoe Hershberger enum env_op { 237afcf3a5SJoe Hershberger env_op_create, 247afcf3a5SJoe Hershberger env_op_delete, 257afcf3a5SJoe Hershberger env_op_overwrite, 267afcf3a5SJoe Hershberger }; 277afcf3a5SJoe Hershberger 2836266435SRobert P. J. Day /* Action which shall be performed in the call to hsearch. */ 29a6826fbcSWolfgang Denk typedef enum { 30a6826fbcSWolfgang Denk FIND, 31a6826fbcSWolfgang Denk ENTER 32a6826fbcSWolfgang Denk } ACTION; 33a6826fbcSWolfgang Denk 34a6826fbcSWolfgang Denk typedef struct entry { 3584b5e802SWolfgang Denk const char *key; 36a6826fbcSWolfgang Denk char *data; 37170ab110SJoe Hershberger int (*callback)(const char *name, const char *value, enum env_op op, 38170ab110SJoe Hershberger int flags); 392598090bSJoe Hershberger int flags; 40a6826fbcSWolfgang Denk } ENTRY; 41a6826fbcSWolfgang Denk 42a6826fbcSWolfgang Denk /* Opaque type for internal use. */ 43a6826fbcSWolfgang Denk struct _ENTRY; 44a6826fbcSWolfgang Denk 45a6826fbcSWolfgang Denk /* 46a6826fbcSWolfgang Denk * Family of hash table handling functions. The functions also 47a6826fbcSWolfgang Denk * have reentrant counterparts ending with _r. The non-reentrant 4836266435SRobert P. J. Day * functions all work on a single internal hash table. 49a6826fbcSWolfgang Denk */ 50a6826fbcSWolfgang Denk 51a6826fbcSWolfgang Denk /* Data type for reentrant functions. */ 52a6826fbcSWolfgang Denk struct hsearch_data { 53a6826fbcSWolfgang Denk struct _ENTRY *table; 54a6826fbcSWolfgang Denk unsigned int size; 55a6826fbcSWolfgang Denk unsigned int filled; 56c5983592SGerlando Falauto /* 57c5983592SGerlando Falauto * Callback function which will check whether the given change for variable 5836266435SRobert P. J. Day * "__item" to "newval" may be applied or not, and possibly apply such change. 59c5983592SGerlando Falauto * When (flag & H_FORCE) is set, it shall not print out any error message and 60c5983592SGerlando Falauto * shall force overwriting of write-once variables. 6136266435SRobert P. J. Day * Must return 0 for approval, 1 for denial. 62c5983592SGerlando Falauto */ 637afcf3a5SJoe Hershberger int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op, 647afcf3a5SJoe Hershberger int flag); 65a6826fbcSWolfgang Denk }; 66a6826fbcSWolfgang Denk 6736266435SRobert P. J. Day /* Create a new hash table which will contain at most "__nel" elements. */ 68a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab); 69a6826fbcSWolfgang Denk 7036266435SRobert P. J. Day /* Destroy current internal hash table. */ 71c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab); 72a6826fbcSWolfgang Denk 73a6826fbcSWolfgang Denk /* 7436266435SRobert P. J. Day * Search for entry matching __item.key in internal hash table. If 75a6826fbcSWolfgang Denk * ACTION is `FIND' return found entry or signal error by returning 76a6826fbcSWolfgang Denk * NULL. If ACTION is `ENTER' replace existing data (if any) with 7736266435SRobert P. J. Day * __item.data. 78a6826fbcSWolfgang Denk * */ 79a6826fbcSWolfgang Denk extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval, 80c4e0057fSJoe Hershberger struct hsearch_data *__htab, int __flag); 81a6826fbcSWolfgang Denk 82560d424bSMike Frysinger /* 8336266435SRobert P. J. Day * Search for an entry matching "__match". Otherwise, Same semantics 84560d424bSMike Frysinger * as hsearch_r(). 85560d424bSMike Frysinger */ 86560d424bSMike Frysinger extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval, 87560d424bSMike Frysinger struct hsearch_data *__htab); 88560d424bSMike Frysinger 8936266435SRobert P. J. Day /* Search and delete entry matching "__key" in internal hash table. */ 90152874b6SGerlando Falauto extern int hdelete_r(const char *__key, struct hsearch_data *__htab, 91c4e0057fSJoe Hershberger int __flag); 92a6826fbcSWolfgang Denk 93a6826fbcSWolfgang Denk extern ssize_t hexport_r(struct hsearch_data *__htab, 94be11235aSJoe Hershberger const char __sep, int __flag, char **__resp, size_t __size, 9537f2fe74SWolfgang Denk int argc, char * const argv[]); 96a6826fbcSWolfgang Denk 97348b1f1cSGerlando Falauto /* 98348b1f1cSGerlando Falauto * nvars: length of vars array 99348b1f1cSGerlando Falauto * vars: array of strings (variable names) to import (nvars == 0 means all) 100348b1f1cSGerlando Falauto */ 101a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab, 102a6826fbcSWolfgang Denk const char *__env, size_t __size, const char __sep, 103ecd1446fSAlexander Holler int __flag, int __crlf_is_lf, int nvars, 104ecd1446fSAlexander Holler char * const vars[]); 105a6826fbcSWolfgang Denk 106170ab110SJoe Hershberger /* Walk the whole table calling the callback on each element */ 107170ab110SJoe Hershberger extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *)); 108170ab110SJoe Hershberger 109be11235aSJoe Hershberger /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */ 110c3f65258SGerlando Falauto #define H_NOCLEAR (1 << 0) /* do not clear hash table before importing */ 111c3f65258SGerlando Falauto #define H_FORCE (1 << 1) /* overwrite read-only/write-once variables */ 112c4e0057fSJoe Hershberger #define H_INTERACTIVE (1 << 2) /* indicate that an import is user directed */ 113be11235aSJoe Hershberger #define H_HIDE_DOT (1 << 3) /* don't print env vars that begin with '.' */ 114ea009d47SWolfgang Denk #define H_MATCH_KEY (1 << 4) /* search/grep key = variable names */ 115ea009d47SWolfgang Denk #define H_MATCH_DATA (1 << 5) /* search/grep data = variable values */ 116ea009d47SWolfgang Denk #define H_MATCH_BOTH (H_MATCH_KEY | H_MATCH_DATA) /* search/grep both */ 117ea009d47SWolfgang Denk #define H_MATCH_IDENT (1 << 6) /* search for indentical strings */ 118ea009d47SWolfgang Denk #define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */ 119be29df6aSWolfgang Denk #define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */ 120be29df6aSWolfgang Denk #define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX) 121*382bee57SSimon Glass #define H_PROGRAMMATIC (1 << 9) /* indicate that an import is from env_set() */ 12294b467b1SJoe Hershberger #define H_ORIGIN_FLAGS (H_INTERACTIVE | H_PROGRAMMATIC) 123a6826fbcSWolfgang Denk 12436266435SRobert P. J. Day #endif /* _SEARCH_H_ */ 125