search.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Declarations for System V style searching functions.
  3. * Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
  4. * This file is part of the GNU C Library.
  5. *
  6. * SPDX-License-Identifier: LGPL-2.1+
  7. */
  8. /*
  9. * Based on code from uClibc-0.9.30.3
  10. * Extensions for use within U-Boot
  11. * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de>
  12. */
  13. #ifndef _SEARCH_H
  14. #define _SEARCH_H 1
  15. #include <stddef.h>
  16. #define __set_errno(val) do { errno = val; } while (0)
  17. enum env_op {
  18. env_op_create,
  19. env_op_delete,
  20. env_op_overwrite,
  21. };
  22. /* Action which shall be performed in the call the hsearch. */
  23. typedef enum {
  24. FIND,
  25. ENTER
  26. } ACTION;
  27. typedef struct entry {
  28. const char *key;
  29. char *data;
  30. int (*callback)(const char *name, const char *value, enum env_op op,
  31. int flags);
  32. int flags;
  33. } ENTRY;
  34. /* Opaque type for internal use. */
  35. struct _ENTRY;
  36. /*
  37. * Family of hash table handling functions. The functions also
  38. * have reentrant counterparts ending with _r. The non-reentrant
  39. * functions all work on a signle internal hashing table.
  40. */
  41. /* Data type for reentrant functions. */
  42. struct hsearch_data {
  43. struct _ENTRY *table;
  44. unsigned int size;
  45. unsigned int filled;
  46. /*
  47. * Callback function which will check whether the given change for variable
  48. * "item" to "newval" may be applied or not, and possibly apply such change.
  49. * When (flag & H_FORCE) is set, it shall not print out any error message and
  50. * shall force overwriting of write-once variables.
  51. .* Must return 0 for approval, 1 for denial.
  52. */
  53. int (*change_ok)(const ENTRY *__item, const char *newval, enum env_op,
  54. int flag);
  55. };
  56. /* Create a new hashing table which will at most contain NEL elements. */
  57. extern int hcreate_r(size_t __nel, struct hsearch_data *__htab);
  58. /* Destroy current internal hashing table. */
  59. extern void hdestroy_r(struct hsearch_data *__htab);
  60. /*
  61. * Search for entry matching ITEM.key in internal hash table. If
  62. * ACTION is `FIND' return found entry or signal error by returning
  63. * NULL. If ACTION is `ENTER' replace existing data (if any) with
  64. * ITEM.data.
  65. * */
  66. extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval,
  67. struct hsearch_data *__htab, int __flag);
  68. /*
  69. * Search for an entry matching `MATCH'. Otherwise, Same semantics
  70. * as hsearch_r().
  71. */
  72. extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval,
  73. struct hsearch_data *__htab);
  74. /* Search and delete entry matching ITEM.key in internal hash table. */
  75. extern int hdelete_r(const char *__key, struct hsearch_data *__htab,
  76. int __flag);
  77. extern ssize_t hexport_r(struct hsearch_data *__htab,
  78. const char __sep, int __flag, char **__resp, size_t __size,
  79. int argc, char * const argv[]);
  80. /*
  81. * nvars: length of vars array
  82. * vars: array of strings (variable names) to import (nvars == 0 means all)
  83. * do_apply: whether to call callback function to check the new argument,
  84. * and possibly apply changes (false means accept everything)
  85. */
  86. extern int himport_r(struct hsearch_data *__htab,
  87. const char *__env, size_t __size, const char __sep,
  88. int __flag, int nvars, char * const vars[]);
  89. /* Walk the whole table calling the callback on each element */
  90. extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
  91. /* Flags for himport_r(), hexport_r(), hdelete_r(), and hsearch_r() */
  92. #define H_NOCLEAR (1 << 0) /* do not clear hash table before importing */
  93. #define H_FORCE (1 << 1) /* overwrite read-only/write-once variables */
  94. #define H_INTERACTIVE (1 << 2) /* indicate that an import is user directed */
  95. #define H_HIDE_DOT (1 << 3) /* don't print env vars that begin with '.' */
  96. #define H_MATCH_KEY (1 << 4) /* search/grep key = variable names */
  97. #define H_MATCH_DATA (1 << 5) /* search/grep data = variable values */
  98. #define H_MATCH_BOTH (H_MATCH_KEY | H_MATCH_DATA) /* search/grep both */
  99. #define H_MATCH_IDENT (1 << 6) /* search for indentical strings */
  100. #define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */
  101. #define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */
  102. #define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX)
  103. #endif /* search.h */