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 * 6*eee479cfSWolfgang 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 15a6826fbcSWolfgang Denk #ifndef _SEARCH_H 16a6826fbcSWolfgang Denk #define _SEARCH_H 1 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 28a6826fbcSWolfgang Denk /* Action which shall be performed in the call the 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 48a6826fbcSWolfgang Denk * functions all work on a signle internal hashing 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 587afcf3a5SJoe Hershberger * "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. 61c5983592SGerlando Falauto .* 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 67a6826fbcSWolfgang Denk /* Create a new hashing table which will at most contain NEL elements. */ 68a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab); 69a6826fbcSWolfgang Denk 70a6826fbcSWolfgang Denk /* Destroy current internal hashing table. */ 71c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab); 72a6826fbcSWolfgang Denk 73a6826fbcSWolfgang Denk /* 74a6826fbcSWolfgang Denk * 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 77a6826fbcSWolfgang Denk * 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 /* 83560d424bSMike Frysinger * 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 89a6826fbcSWolfgang Denk /* Search and delete entry matching ITEM.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) 100c5983592SGerlando Falauto * do_apply: whether to call callback function to check the new argument, 101c5983592SGerlando Falauto * and possibly apply changes (false means accept everything) 102348b1f1cSGerlando Falauto */ 103a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab, 104a6826fbcSWolfgang Denk const char *__env, size_t __size, const char __sep, 105c4e0057fSJoe Hershberger int __flag, int nvars, char * const vars[]); 106a6826fbcSWolfgang Denk 107170ab110SJoe Hershberger /* Walk the whole table calling the callback on each element */ 108170ab110SJoe Hershberger extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *)); 109170ab110SJoe Hershberger 110be11235aSJoe Hershberger /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */ 111c3f65258SGerlando Falauto #define H_NOCLEAR (1 << 0) /* do not clear hash table before importing */ 112c3f65258SGerlando Falauto #define H_FORCE (1 << 1) /* overwrite read-only/write-once variables */ 113c4e0057fSJoe Hershberger #define H_INTERACTIVE (1 << 2) /* indicate that an import is user directed */ 114be11235aSJoe Hershberger #define H_HIDE_DOT (1 << 3) /* don't print env vars that begin with '.' */ 115ea009d47SWolfgang Denk #define H_MATCH_KEY (1 << 4) /* search/grep key = variable names */ 116ea009d47SWolfgang Denk #define H_MATCH_DATA (1 << 5) /* search/grep data = variable values */ 117ea009d47SWolfgang Denk #define H_MATCH_BOTH (H_MATCH_KEY | H_MATCH_DATA) /* search/grep both */ 118ea009d47SWolfgang Denk #define H_MATCH_IDENT (1 << 6) /* search for indentical strings */ 119ea009d47SWolfgang Denk #define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */ 120be29df6aSWolfgang Denk #define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */ 121be29df6aSWolfgang Denk #define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX) 122a6826fbcSWolfgang Denk 123a6826fbcSWolfgang Denk #endif /* search.h */ 124