BPredicate.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @file BPredicate.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object that parses and evaluates a logical expression.
  32. * Allows the user to define custom functions than can be
  33. * used in the expression.
  34. *
  35. * Syntax and semantics for logical expressions:
  36. *
  37. * - true
  38. * Logical true constant. Evaluates to 1.
  39. *
  40. * - false
  41. * Logical false constant. Evaluates to 0.
  42. *
  43. * - NOT expression
  44. * Logical negation. If the expression evaluates to error, the
  45. * negation evaluates to error.
  46. *
  47. * - expression OR expression
  48. * Logical disjunction. The second expression is only evaluated
  49. * if the first expression evaluates to false. If a sub-expression
  50. * evaluates to error, the disjunction evaluates to error.
  51. *
  52. * - expression AND expression
  53. * Logical conjunction. The second expression is only evaluated
  54. * if the first expression evaluates to true. If a sub-expression
  55. * evaluates to error, the conjunction evaluates to error.
  56. *
  57. * - function(arg, ..., arg)
  58. * Evaluation of a user-provided function (function is the name of the
  59. * function, [a-zA-Z0-9_]+).
  60. * If the function with the given name does not exist, it evaluates to
  61. * error.
  62. * Arguments are evaluated from left to right. Each argument can either
  63. * be a logical expression or a string (characters enclosed in double
  64. * quotes, without any double quote).
  65. * If an argument is encountered, but all needed arguments have already
  66. * been evaluated, the function evaluates to error.
  67. * If an argument is of wrong type, it is not evaluated and the function
  68. * evaluates to error.
  69. * If an argument evaluates to error, the function evaluates to error.
  70. * If after all arguments have been evaluated, the function needs more
  71. * arguments, it evaluates to error.
  72. * Then the handler function is called. If it returns anything other
  73. * than 1 and 0, the function evaluates to error. Otherwise it evaluates
  74. * to what the handler function returned.
  75. */
  76. #ifndef BADVPN_PREDICATE_BPREDICATE_H
  77. #define BADVPN_PREDICATE_BPREDICATE_H
  78. #include <misc/debug.h>
  79. #include <structure/BAVL.h>
  80. #include <base/DebugObject.h>
  81. #define PREDICATE_TYPE_BOOL 1
  82. #define PREDICATE_TYPE_STRING 2
  83. #define PREDICATE_MAX_NAME 16
  84. #define PREDICATE_MAX_ARGS 16
  85. /**
  86. * Handler function called when evaluating a custom function in the predicate.
  87. *
  88. * @param user value passed to {@link BPredicateFunction_Init}
  89. * @param args arguments to the function. Points to an array of pointers (as many as the
  90. * function has arguments), where each pointer points to either to an int or
  91. * a zero-terminated string (depending on the type of the argument).
  92. * @return 1 for true, 0 for false, -1 for error
  93. */
  94. typedef int (*BPredicate_callback) (void *user, void **args);
  95. /**
  96. * Object that parses and evaluates a logical expression.
  97. * Allows the user to define custom functions than can be
  98. * used in the expression.
  99. */
  100. typedef struct {
  101. DebugObject d_obj;
  102. void *root;
  103. BAVL functions_tree;
  104. #ifndef NDEBUG
  105. int in_function;
  106. #endif
  107. } BPredicate;
  108. /**
  109. * Object that represents a custom function in {@link BPredicate}.
  110. */
  111. typedef struct {
  112. DebugObject d_obj;
  113. BPredicate *p;
  114. char name[PREDICATE_MAX_NAME + 1];
  115. int args[PREDICATE_MAX_ARGS];
  116. int num_args;
  117. BPredicate_callback callback;
  118. void *user;
  119. BAVLNode tree_node;
  120. } BPredicateFunction;
  121. /**
  122. * Initializes the object.
  123. *
  124. * @param p the object
  125. * @param str logical expression
  126. * @return 1 on success, 0 on failure
  127. */
  128. int BPredicate_Init (BPredicate *p, char *str) WARN_UNUSED;
  129. /**
  130. * Frees the object.
  131. * Must have no custom functions.
  132. * Must not be called from function handlers.
  133. *
  134. * @param p the object
  135. */
  136. void BPredicate_Free (BPredicate *p);
  137. /**
  138. * Evaluates the logical expression.
  139. * Must not be called from function handlers.
  140. *
  141. * @param p the object
  142. * @return 1 for true, 0 for false, -1 for error
  143. */
  144. int BPredicate_Eval (BPredicate *p);
  145. /**
  146. * Registers a custom function for {@link BPredicate}.
  147. * Must not be called from function handlers.
  148. *
  149. * @param o the object
  150. * @param p predicate to register the function for
  151. * @param args array of argument types. Each type is either PREDICATE_TYPE_BOOL or PREDICATE_TYPE_STRING.
  152. * @param num_args number of arguments for the function. Must be >=0 and <=PREDICATE_MAX_ARGS.
  153. * @param callback handler to call to evaluate the function
  154. * @param user value to pass to handler
  155. */
  156. void BPredicateFunction_Init (BPredicateFunction *o, BPredicate *p, char *name, int *args, int num_args, BPredicate_callback callback, void *user);
  157. /**
  158. * Removes a custom function for {@link BPredicate}.
  159. * Must not be called from function handlers.
  160. *
  161. * @param o the object
  162. */
  163. void BPredicateFunction_Free (BPredicateFunction *o);
  164. #endif