BPredicate.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @file BPredicate.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Object that parses and evaluates a logical expression.
  25. * Allows the user to define custom functions than can be
  26. * used in the expression.
  27. *
  28. * Syntax and semantics for logical expressions:
  29. *
  30. * - true
  31. * Logical true constant. Evaluates to 1.
  32. *
  33. * - false
  34. * Logical false constant. Evaluates to 0.
  35. *
  36. * - NOT expression
  37. * Logical negation. If the expression evaluates to error, the
  38. * negation evaluates to error.
  39. *
  40. * - expression OR expression
  41. * Logical disjunction. The second expression is only evaluated
  42. * if the first expression evaluates to false. If a sub-expression
  43. * evaluates to error, the disjunction evaluates to error.
  44. *
  45. * - expression AND expression
  46. * Logical conjunction. The second expression is only evaluated
  47. * if the first expression evaluates to true. If a sub-expression
  48. * evaluates to error, the conjunction evaluates to error.
  49. *
  50. * - function(arg, ..., arg)
  51. * Evaluation of a user-provided function (function is the name of the
  52. * function, [a-zA-Z0-9_]+).
  53. * If the function with the given name does not exist, it evaluates to
  54. * error.
  55. * Arguments are evaluated from left to right. Each argument can either
  56. * be a logical expression or a string (characters enclosed in double
  57. * quotes, without any double quote).
  58. * If an argument is encountered, but all needed arguments have already
  59. * been evaluated, the function evaluates to error.
  60. * If an argument is of wrong type, it is not evaluated and the function
  61. * evaluates to error.
  62. * If an argument evaluates to error, the function evaluates to error.
  63. * If after all arguments have been evaluated, the function needs more
  64. * arguments, it evaluates to error.
  65. * Then the handler function is called. If it returns anything other
  66. * than 1 and 0, the function evaluates to error. Otherwise it evaluates
  67. * to what the handler function returned.
  68. */
  69. #ifndef BADVPN_PREDICATE_BPREDICATE_H
  70. #define BADVPN_PREDICATE_BPREDICATE_H
  71. #include <misc/debug.h>
  72. #include <structure/BAVL.h>
  73. #include <base/DebugObject.h>
  74. #define PREDICATE_TYPE_BOOL 1
  75. #define PREDICATE_TYPE_STRING 2
  76. #define PREDICATE_MAX_NAME 16
  77. #define PREDICATE_MAX_ARGS 16
  78. /**
  79. * Handler function called when evaluating a custom function in the predicate.
  80. *
  81. * @param user value passed to {@link BPredicateFunction_Init}
  82. * @param args arguments to the function. Points to an array of pointers (as many as the
  83. * function has arguments), where each pointer points to either to an int or
  84. * a zero-terminated string (depending on the type of the argument).
  85. * @return 1 for true, 0 for false, -1 for error
  86. */
  87. typedef int (*BPredicate_callback) (void *user, void **args);
  88. /**
  89. * Object that parses and evaluates a logical expression.
  90. * Allows the user to define custom functions than can be
  91. * used in the expression.
  92. */
  93. typedef struct {
  94. DebugObject d_obj;
  95. void *root;
  96. BAVL functions_tree;
  97. #ifndef NDEBUG
  98. int in_function;
  99. #endif
  100. } BPredicate;
  101. /**
  102. * Object that represents a custom function in {@link BPredicate}.
  103. */
  104. typedef struct {
  105. DebugObject d_obj;
  106. BPredicate *p;
  107. char name[PREDICATE_MAX_NAME + 1];
  108. int args[PREDICATE_MAX_ARGS];
  109. int num_args;
  110. BPredicate_callback callback;
  111. void *user;
  112. BAVLNode tree_node;
  113. } BPredicateFunction;
  114. /**
  115. * Initializes the object.
  116. *
  117. * @param p the object
  118. * @param str logical expression
  119. * @return 1 on success, 0 on failure
  120. */
  121. int BPredicate_Init (BPredicate *p, char *str) WARN_UNUSED;
  122. /**
  123. * Frees the object.
  124. * Must have no custom functions.
  125. * Must not be called from function handlers.
  126. *
  127. * @param p the object
  128. */
  129. void BPredicate_Free (BPredicate *p);
  130. /**
  131. * Evaluates the logical expression.
  132. * Must not be called from function handlers.
  133. *
  134. * @param p the object
  135. * @return 1 for true, 0 for false, -1 for error
  136. */
  137. int BPredicate_Eval (BPredicate *p);
  138. /**
  139. * Registers a custom function for {@link BPredicate}.
  140. * Must not be called from function handlers.
  141. *
  142. * @param o the object
  143. * @param p predicate to register the function for
  144. * @param args array of argument types. Each type is either PREDICATE_TYPE_BOOL or PREDICATE_TYPE_STRING.
  145. * @param num_args number of arguments for the function. Must be >=0 and <=PREDICATE_MAX_ARGS.
  146. * @param callback handler to call to evaluate the function
  147. * @param user value to pass to handler
  148. */
  149. void BPredicateFunction_Init (BPredicateFunction *o, BPredicate *p, char *name, int *args, int num_args, BPredicate_callback callback, void *user);
  150. /**
  151. * Removes a custom function for {@link BPredicate}.
  152. * Must not be called from function handlers.
  153. *
  154. * @param o the object
  155. */
  156. void BPredicateFunction_Free (BPredicateFunction *o);
  157. #endif