eval.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. // ----------------------------------------------------------------------------
  4. // data structures for storing the parsed expression in memory
  5. typedef struct eval_value {
  6. int type;
  7. union {
  8. NETDATA_DOUBLE number;
  9. EVAL_VARIABLE *variable;
  10. struct eval_node *expression;
  11. };
  12. } EVAL_VALUE;
  13. typedef struct eval_node {
  14. int id;
  15. unsigned char operator;
  16. int precedence;
  17. int count;
  18. EVAL_VALUE ops[];
  19. } EVAL_NODE;
  20. // these are used for EVAL_NODE.operator
  21. // they are used as internal IDs to identify an operator
  22. // THEY ARE NOT USED FOR PARSING OPERATORS LIKE THAT
  23. #define EVAL_OPERATOR_NOP '\0'
  24. #define EVAL_OPERATOR_EXPRESSION_OPEN '('
  25. #define EVAL_OPERATOR_EXPRESSION_CLOSE ')'
  26. #define EVAL_OPERATOR_NOT '!'
  27. #define EVAL_OPERATOR_PLUS '+'
  28. #define EVAL_OPERATOR_MINUS '-'
  29. #define EVAL_OPERATOR_AND '&'
  30. #define EVAL_OPERATOR_OR '|'
  31. #define EVAL_OPERATOR_GREATER_THAN_OR_EQUAL 'G'
  32. #define EVAL_OPERATOR_LESS_THAN_OR_EQUAL 'L'
  33. #define EVAL_OPERATOR_NOT_EQUAL '~'
  34. #define EVAL_OPERATOR_EQUAL '='
  35. #define EVAL_OPERATOR_LESS '<'
  36. #define EVAL_OPERATOR_GREATER '>'
  37. #define EVAL_OPERATOR_MULTIPLY '*'
  38. #define EVAL_OPERATOR_DIVIDE '/'
  39. #define EVAL_OPERATOR_SIGN_PLUS 'P'
  40. #define EVAL_OPERATOR_SIGN_MINUS 'M'
  41. #define EVAL_OPERATOR_ABS 'A'
  42. #define EVAL_OPERATOR_IF_THEN_ELSE '?'
  43. // ----------------------------------------------------------------------------
  44. // forward function definitions
  45. static inline void eval_node_free(EVAL_NODE *op);
  46. static inline EVAL_NODE *parse_full_expression(const char **string, int *error);
  47. static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error);
  48. static inline NETDATA_DOUBLE eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error);
  49. static inline void print_parsed_as_node(BUFFER *out, EVAL_NODE *op, int *error);
  50. static inline void print_parsed_as_constant(BUFFER *out, NETDATA_DOUBLE n);
  51. // ----------------------------------------------------------------------------
  52. // evaluation of expressions
  53. static inline NETDATA_DOUBLE eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABLE *v, int *error) {
  54. static uint32_t this_hash = 0, now_hash = 0, after_hash = 0, before_hash = 0, status_hash = 0, removed_hash = 0, uninitialized_hash = 0, undefined_hash = 0, clear_hash = 0, warning_hash = 0, critical_hash = 0;
  55. NETDATA_DOUBLE n;
  56. if(unlikely(this_hash == 0)) {
  57. this_hash = simple_hash("this");
  58. now_hash = simple_hash("now");
  59. after_hash = simple_hash("after");
  60. before_hash = simple_hash("before");
  61. status_hash = simple_hash("status");
  62. removed_hash = simple_hash("REMOVED");
  63. uninitialized_hash = simple_hash("UNINITIALIZED");
  64. undefined_hash = simple_hash("UNDEFINED");
  65. clear_hash = simple_hash("CLEAR");
  66. warning_hash = simple_hash("WARNING");
  67. critical_hash = simple_hash("CRITICAL");
  68. }
  69. if(unlikely(v->hash == this_hash && !strcmp(v->name, "this"))) {
  70. n = (exp->myself)?*exp->myself:NAN;
  71. buffer_strcat(exp->error_msg, "[ $this = ");
  72. print_parsed_as_constant(exp->error_msg, n);
  73. buffer_strcat(exp->error_msg, " ] ");
  74. return n;
  75. }
  76. if(unlikely(v->hash == after_hash && !strcmp(v->name, "after"))) {
  77. n = (exp->after && *exp->after)?*exp->after:NAN;
  78. buffer_strcat(exp->error_msg, "[ $after = ");
  79. print_parsed_as_constant(exp->error_msg, n);
  80. buffer_strcat(exp->error_msg, " ] ");
  81. return n;
  82. }
  83. if(unlikely(v->hash == before_hash && !strcmp(v->name, "before"))) {
  84. n = (exp->before && *exp->before)?*exp->before:NAN;
  85. buffer_strcat(exp->error_msg, "[ $before = ");
  86. print_parsed_as_constant(exp->error_msg, n);
  87. buffer_strcat(exp->error_msg, " ] ");
  88. return n;
  89. }
  90. if(unlikely(v->hash == now_hash && !strcmp(v->name, "now"))) {
  91. n = now_realtime_sec();
  92. buffer_strcat(exp->error_msg, "[ $now = ");
  93. print_parsed_as_constant(exp->error_msg, n);
  94. buffer_strcat(exp->error_msg, " ] ");
  95. return n;
  96. }
  97. if(unlikely(v->hash == status_hash && !strcmp(v->name, "status"))) {
  98. n = (exp->status)?*exp->status:RRDCALC_STATUS_UNINITIALIZED;
  99. buffer_strcat(exp->error_msg, "[ $status = ");
  100. print_parsed_as_constant(exp->error_msg, n);
  101. buffer_strcat(exp->error_msg, " ] ");
  102. return n;
  103. }
  104. if(unlikely(v->hash == removed_hash && !strcmp(v->name, "REMOVED"))) {
  105. n = RRDCALC_STATUS_REMOVED;
  106. buffer_strcat(exp->error_msg, "[ $REMOVED = ");
  107. print_parsed_as_constant(exp->error_msg, n);
  108. buffer_strcat(exp->error_msg, " ] ");
  109. return n;
  110. }
  111. if(unlikely(v->hash == uninitialized_hash && !strcmp(v->name, "UNINITIALIZED"))) {
  112. n = RRDCALC_STATUS_UNINITIALIZED;
  113. buffer_strcat(exp->error_msg, "[ $UNINITIALIZED = ");
  114. print_parsed_as_constant(exp->error_msg, n);
  115. buffer_strcat(exp->error_msg, " ] ");
  116. return n;
  117. }
  118. if(unlikely(v->hash == undefined_hash && !strcmp(v->name, "UNDEFINED"))) {
  119. n = RRDCALC_STATUS_UNDEFINED;
  120. buffer_strcat(exp->error_msg, "[ $UNDEFINED = ");
  121. print_parsed_as_constant(exp->error_msg, n);
  122. buffer_strcat(exp->error_msg, " ] ");
  123. return n;
  124. }
  125. if(unlikely(v->hash == clear_hash && !strcmp(v->name, "CLEAR"))) {
  126. n = RRDCALC_STATUS_CLEAR;
  127. buffer_strcat(exp->error_msg, "[ $CLEAR = ");
  128. print_parsed_as_constant(exp->error_msg, n);
  129. buffer_strcat(exp->error_msg, " ] ");
  130. return n;
  131. }
  132. if(unlikely(v->hash == warning_hash && !strcmp(v->name, "WARNING"))) {
  133. n = RRDCALC_STATUS_WARNING;
  134. buffer_strcat(exp->error_msg, "[ $WARNING = ");
  135. print_parsed_as_constant(exp->error_msg, n);
  136. buffer_strcat(exp->error_msg, " ] ");
  137. return n;
  138. }
  139. if(unlikely(v->hash == critical_hash && !strcmp(v->name, "CRITICAL"))) {
  140. n = RRDCALC_STATUS_CRITICAL;
  141. buffer_strcat(exp->error_msg, "[ $CRITICAL = ");
  142. print_parsed_as_constant(exp->error_msg, n);
  143. buffer_strcat(exp->error_msg, " ] ");
  144. return n;
  145. }
  146. if(exp->rrdcalc && health_variable_lookup(v->name, v->hash, exp->rrdcalc, &n)) {
  147. buffer_sprintf(exp->error_msg, "[ ${%s} = ", v->name);
  148. print_parsed_as_constant(exp->error_msg, n);
  149. buffer_strcat(exp->error_msg, " ] ");
  150. return n;
  151. }
  152. *error = EVAL_ERROR_UNKNOWN_VARIABLE;
  153. buffer_sprintf(exp->error_msg, "[ undefined variable '%s' ] ", v->name);
  154. return NAN;
  155. }
  156. static inline NETDATA_DOUBLE eval_value(EVAL_EXPRESSION *exp, EVAL_VALUE *v, int *error) {
  157. NETDATA_DOUBLE n;
  158. switch(v->type) {
  159. case EVAL_VALUE_EXPRESSION:
  160. n = eval_node(exp, v->expression, error);
  161. break;
  162. case EVAL_VALUE_NUMBER:
  163. n = v->number;
  164. break;
  165. case EVAL_VALUE_VARIABLE:
  166. n = eval_variable(exp, v->variable, error);
  167. break;
  168. default:
  169. *error = EVAL_ERROR_INVALID_VALUE;
  170. n = 0;
  171. break;
  172. }
  173. return n;
  174. }
  175. static inline int is_true(NETDATA_DOUBLE n) {
  176. if(isnan(n)) return 0;
  177. if(isinf(n)) return 1;
  178. if(n == 0) return 0;
  179. return 1;
  180. }
  181. NETDATA_DOUBLE eval_and(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  182. return is_true(eval_value(exp, &op->ops[0], error)) && is_true(eval_value(exp, &op->ops[1], error));
  183. }
  184. NETDATA_DOUBLE eval_or(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  185. return is_true(eval_value(exp, &op->ops[0], error)) || is_true(eval_value(exp, &op->ops[1], error));
  186. }
  187. NETDATA_DOUBLE eval_greater_than_or_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  188. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  189. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  190. return isgreaterequal(n1, n2);
  191. }
  192. NETDATA_DOUBLE eval_less_than_or_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  193. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  194. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  195. return islessequal(n1, n2);
  196. }
  197. NETDATA_DOUBLE eval_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  198. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  199. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  200. if(isnan(n1) && isnan(n2)) return 1;
  201. if(isinf(n1) && isinf(n2)) return 1;
  202. if(isnan(n1) || isnan(n2)) return 0;
  203. if(isinf(n1) || isinf(n2)) return 0;
  204. return considered_equal_ndd(n1, n2);
  205. }
  206. NETDATA_DOUBLE eval_not_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  207. return !eval_equal(exp, op, error);
  208. }
  209. NETDATA_DOUBLE eval_less(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  210. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  211. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  212. return isless(n1, n2);
  213. }
  214. NETDATA_DOUBLE eval_greater(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  215. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  216. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  217. return isgreater(n1, n2);
  218. }
  219. NETDATA_DOUBLE eval_plus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  220. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  221. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  222. if(isnan(n1) || isnan(n2)) return NAN;
  223. if(isinf(n1) || isinf(n2)) return INFINITY;
  224. return n1 + n2;
  225. }
  226. NETDATA_DOUBLE eval_minus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  227. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  228. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  229. if(isnan(n1) || isnan(n2)) return NAN;
  230. if(isinf(n1) || isinf(n2)) return INFINITY;
  231. return n1 - n2;
  232. }
  233. NETDATA_DOUBLE eval_multiply(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  234. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  235. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  236. if(isnan(n1) || isnan(n2)) return NAN;
  237. if(isinf(n1) || isinf(n2)) return INFINITY;
  238. return n1 * n2;
  239. }
  240. NETDATA_DOUBLE eval_divide(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  241. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  242. NETDATA_DOUBLE n2 = eval_value(exp, &op->ops[1], error);
  243. if(isnan(n1) || isnan(n2)) return NAN;
  244. if(isinf(n1) || isinf(n2)) return INFINITY;
  245. return n1 / n2;
  246. }
  247. NETDATA_DOUBLE eval_nop(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  248. return eval_value(exp, &op->ops[0], error);
  249. }
  250. NETDATA_DOUBLE eval_not(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  251. return !is_true(eval_value(exp, &op->ops[0], error));
  252. }
  253. NETDATA_DOUBLE eval_sign_plus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  254. return eval_value(exp, &op->ops[0], error);
  255. }
  256. NETDATA_DOUBLE eval_sign_minus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  257. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  258. if(isnan(n1)) return NAN;
  259. if(isinf(n1)) return INFINITY;
  260. return -n1;
  261. }
  262. NETDATA_DOUBLE eval_abs(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  263. NETDATA_DOUBLE n1 = eval_value(exp, &op->ops[0], error);
  264. if(isnan(n1)) return NAN;
  265. if(isinf(n1)) return INFINITY;
  266. return ABS(n1);
  267. }
  268. NETDATA_DOUBLE eval_if_then_else(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  269. if(is_true(eval_value(exp, &op->ops[0], error)))
  270. return eval_value(exp, &op->ops[1], error);
  271. else
  272. return eval_value(exp, &op->ops[2], error);
  273. }
  274. static struct operator {
  275. const char *print_as;
  276. char precedence;
  277. char parameters;
  278. char isfunction;
  279. NETDATA_DOUBLE (*eval)(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error);
  280. } operators[256] = {
  281. // this is a random access array
  282. // we always access it with a known EVAL_OPERATOR_X
  283. [EVAL_OPERATOR_AND] = { "&&", 2, 2, 0, eval_and },
  284. [EVAL_OPERATOR_OR] = { "||", 2, 2, 0, eval_or },
  285. [EVAL_OPERATOR_GREATER_THAN_OR_EQUAL] = { ">=", 3, 2, 0, eval_greater_than_or_equal },
  286. [EVAL_OPERATOR_LESS_THAN_OR_EQUAL] = { "<=", 3, 2, 0, eval_less_than_or_equal },
  287. [EVAL_OPERATOR_NOT_EQUAL] = { "!=", 3, 2, 0, eval_not_equal },
  288. [EVAL_OPERATOR_EQUAL] = { "==", 3, 2, 0, eval_equal },
  289. [EVAL_OPERATOR_LESS] = { "<", 3, 2, 0, eval_less },
  290. [EVAL_OPERATOR_GREATER] = { ">", 3, 2, 0, eval_greater },
  291. [EVAL_OPERATOR_PLUS] = { "+", 4, 2, 0, eval_plus },
  292. [EVAL_OPERATOR_MINUS] = { "-", 4, 2, 0, eval_minus },
  293. [EVAL_OPERATOR_MULTIPLY] = { "*", 5, 2, 0, eval_multiply },
  294. [EVAL_OPERATOR_DIVIDE] = { "/", 5, 2, 0, eval_divide },
  295. [EVAL_OPERATOR_NOT] = { "!", 6, 1, 0, eval_not },
  296. [EVAL_OPERATOR_SIGN_PLUS] = { "+", 6, 1, 0, eval_sign_plus },
  297. [EVAL_OPERATOR_SIGN_MINUS] = { "-", 6, 1, 0, eval_sign_minus },
  298. [EVAL_OPERATOR_ABS] = { "abs(",6,1, 1, eval_abs },
  299. [EVAL_OPERATOR_IF_THEN_ELSE] = { "?", 7, 3, 0, eval_if_then_else },
  300. [EVAL_OPERATOR_NOP] = { NULL, 8, 1, 0, eval_nop },
  301. [EVAL_OPERATOR_EXPRESSION_OPEN] = { NULL, 8, 1, 0, eval_nop },
  302. // this should exist in our evaluation list
  303. [EVAL_OPERATOR_EXPRESSION_CLOSE] = { NULL, 99, 1, 0, eval_nop }
  304. };
  305. #define eval_precedence(operator) (operators[(unsigned char)(operator)].precedence)
  306. static inline NETDATA_DOUBLE eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
  307. if(unlikely(op->count != operators[op->operator].parameters)) {
  308. *error = EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS;
  309. return 0;
  310. }
  311. NETDATA_DOUBLE n = operators[op->operator].eval(exp, op, error);
  312. return n;
  313. }
  314. // ----------------------------------------------------------------------------
  315. // parsed-as generation
  316. static inline void print_parsed_as_variable(BUFFER *out, EVAL_VARIABLE *v, int *error) {
  317. (void)error;
  318. buffer_sprintf(out, "${%s}", v->name);
  319. }
  320. static inline void print_parsed_as_constant(BUFFER *out, NETDATA_DOUBLE n) {
  321. if(unlikely(isnan(n))) {
  322. buffer_strcat(out, "nan");
  323. return;
  324. }
  325. if(unlikely(isinf(n))) {
  326. buffer_strcat(out, "inf");
  327. return;
  328. }
  329. char b[100+1], *s;
  330. snprintfz(b, 100, NETDATA_DOUBLE_FORMAT, n);
  331. s = &b[strlen(b) - 1];
  332. while(s > b && *s == '0') {
  333. *s ='\0';
  334. s--;
  335. }
  336. if(s > b && *s == '.')
  337. *s = '\0';
  338. buffer_strcat(out, b);
  339. }
  340. static inline void print_parsed_as_value(BUFFER *out, EVAL_VALUE *v, int *error) {
  341. switch(v->type) {
  342. case EVAL_VALUE_EXPRESSION:
  343. print_parsed_as_node(out, v->expression, error);
  344. break;
  345. case EVAL_VALUE_NUMBER:
  346. print_parsed_as_constant(out, v->number);
  347. break;
  348. case EVAL_VALUE_VARIABLE:
  349. print_parsed_as_variable(out, v->variable, error);
  350. break;
  351. default:
  352. *error = EVAL_ERROR_INVALID_VALUE;
  353. break;
  354. }
  355. }
  356. static inline void print_parsed_as_node(BUFFER *out, EVAL_NODE *op, int *error) {
  357. if(unlikely(op->count != operators[op->operator].parameters)) {
  358. *error = EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS;
  359. return;
  360. }
  361. if(operators[op->operator].parameters == 1) {
  362. if(operators[op->operator].print_as)
  363. buffer_sprintf(out, "%s", operators[op->operator].print_as);
  364. //if(op->operator == EVAL_OPERATOR_EXPRESSION_OPEN)
  365. // buffer_strcat(out, "(");
  366. print_parsed_as_value(out, &op->ops[0], error);
  367. //if(op->operator == EVAL_OPERATOR_EXPRESSION_OPEN)
  368. // buffer_strcat(out, ")");
  369. }
  370. else if(operators[op->operator].parameters == 2) {
  371. buffer_strcat(out, "(");
  372. print_parsed_as_value(out, &op->ops[0], error);
  373. if(operators[op->operator].print_as)
  374. buffer_sprintf(out, " %s ", operators[op->operator].print_as);
  375. print_parsed_as_value(out, &op->ops[1], error);
  376. buffer_strcat(out, ")");
  377. }
  378. else if(op->operator == EVAL_OPERATOR_IF_THEN_ELSE && operators[op->operator].parameters == 3) {
  379. buffer_strcat(out, "(");
  380. print_parsed_as_value(out, &op->ops[0], error);
  381. if(operators[op->operator].print_as)
  382. buffer_sprintf(out, " %s ", operators[op->operator].print_as);
  383. print_parsed_as_value(out, &op->ops[1], error);
  384. buffer_strcat(out, " : ");
  385. print_parsed_as_value(out, &op->ops[2], error);
  386. buffer_strcat(out, ")");
  387. }
  388. if(operators[op->operator].isfunction)
  389. buffer_strcat(out, ")");
  390. }
  391. // ----------------------------------------------------------------------------
  392. // parsing expressions
  393. // skip spaces
  394. static inline void skip_spaces(const char **string) {
  395. const char *s = *string;
  396. while(isspace(*s)) s++;
  397. *string = s;
  398. }
  399. // what character can appear just after an operator keyword
  400. // like NOT AND OR ?
  401. static inline int isoperatorterm_word(const char s) {
  402. if(isspace(s) || s == '(' || s == '$' || s == '!' || s == '-' || s == '+' || isdigit(s) || !s)
  403. return 1;
  404. return 0;
  405. }
  406. // what character can appear just after an operator symbol?
  407. static inline int isoperatorterm_symbol(const char s) {
  408. if(isoperatorterm_word(s) || isalpha(s))
  409. return 1;
  410. return 0;
  411. }
  412. // return 1 if the character should never appear in a variable
  413. static inline int isvariableterm(const char s) {
  414. if(isalnum(s) || s == '.' || s == '_')
  415. return 0;
  416. return 1;
  417. }
  418. // ----------------------------------------------------------------------------
  419. // parse operators
  420. static inline int parse_and(const char **string) {
  421. const char *s = *string;
  422. // AND
  423. if((s[0] == 'A' || s[0] == 'a') && (s[1] == 'N' || s[1] == 'n') && (s[2] == 'D' || s[2] == 'd') && isoperatorterm_word(s[3])) {
  424. *string = &s[4];
  425. return 1;
  426. }
  427. // &&
  428. if(s[0] == '&' && s[1] == '&' && isoperatorterm_symbol(s[2])) {
  429. *string = &s[2];
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. static inline int parse_or(const char **string) {
  435. const char *s = *string;
  436. // OR
  437. if((s[0] == 'O' || s[0] == 'o') && (s[1] == 'R' || s[1] == 'r') && isoperatorterm_word(s[2])) {
  438. *string = &s[3];
  439. return 1;
  440. }
  441. // ||
  442. if(s[0] == '|' && s[1] == '|' && isoperatorterm_symbol(s[2])) {
  443. *string = &s[2];
  444. return 1;
  445. }
  446. return 0;
  447. }
  448. static inline int parse_greater_than_or_equal(const char **string) {
  449. const char *s = *string;
  450. // >=
  451. if(s[0] == '>' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
  452. *string = &s[2];
  453. return 1;
  454. }
  455. return 0;
  456. }
  457. static inline int parse_less_than_or_equal(const char **string) {
  458. const char *s = *string;
  459. // <=
  460. if (s[0] == '<' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
  461. *string = &s[2];
  462. return 1;
  463. }
  464. return 0;
  465. }
  466. static inline int parse_greater(const char **string) {
  467. const char *s = *string;
  468. // >
  469. if(s[0] == '>' && isoperatorterm_symbol(s[1])) {
  470. *string = &s[1];
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. static inline int parse_less(const char **string) {
  476. const char *s = *string;
  477. // <
  478. if(s[0] == '<' && isoperatorterm_symbol(s[1])) {
  479. *string = &s[1];
  480. return 1;
  481. }
  482. return 0;
  483. }
  484. static inline int parse_equal(const char **string) {
  485. const char *s = *string;
  486. // ==
  487. if(s[0] == '=' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
  488. *string = &s[2];
  489. return 1;
  490. }
  491. // =
  492. if(s[0] == '=' && isoperatorterm_symbol(s[1])) {
  493. *string = &s[1];
  494. return 1;
  495. }
  496. return 0;
  497. }
  498. static inline int parse_not_equal(const char **string) {
  499. const char *s = *string;
  500. // !=
  501. if(s[0] == '!' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
  502. *string = &s[2];
  503. return 1;
  504. }
  505. // <>
  506. if(s[0] == '<' && s[1] == '>' && isoperatorterm_symbol(s[2])) {
  507. *string = &s[2];
  508. }
  509. return 0;
  510. }
  511. static inline int parse_not(const char **string) {
  512. const char *s = *string;
  513. // NOT
  514. if((s[0] == 'N' || s[0] == 'n') && (s[1] == 'O' || s[1] == 'o') && (s[2] == 'T' || s[2] == 't') && isoperatorterm_word(s[3])) {
  515. *string = &s[3];
  516. return 1;
  517. }
  518. if(s[0] == '!') {
  519. *string = &s[1];
  520. return 1;
  521. }
  522. return 0;
  523. }
  524. static inline int parse_multiply(const char **string) {
  525. const char *s = *string;
  526. // *
  527. if(s[0] == '*' && isoperatorterm_symbol(s[1])) {
  528. *string = &s[1];
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. static inline int parse_divide(const char **string) {
  534. const char *s = *string;
  535. // /
  536. if(s[0] == '/' && isoperatorterm_symbol(s[1])) {
  537. *string = &s[1];
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. static inline int parse_minus(const char **string) {
  543. const char *s = *string;
  544. // -
  545. if(s[0] == '-' && isoperatorterm_symbol(s[1])) {
  546. *string = &s[1];
  547. return 1;
  548. }
  549. return 0;
  550. }
  551. static inline int parse_plus(const char **string) {
  552. const char *s = *string;
  553. // +
  554. if(s[0] == '+' && isoperatorterm_symbol(s[1])) {
  555. *string = &s[1];
  556. return 1;
  557. }
  558. return 0;
  559. }
  560. static inline int parse_open_subexpression(const char **string) {
  561. const char *s = *string;
  562. // (
  563. if(s[0] == '(') {
  564. *string = &s[1];
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. #define parse_close_function(x) parse_close_subexpression(x)
  570. static inline int parse_close_subexpression(const char **string) {
  571. const char *s = *string;
  572. // )
  573. if(s[0] == ')') {
  574. *string = &s[1];
  575. return 1;
  576. }
  577. return 0;
  578. }
  579. static inline int parse_variable(const char **string, char *buffer, size_t len) {
  580. const char *s = *string;
  581. // $
  582. if(*s == '$') {
  583. size_t i = 0;
  584. s++;
  585. if(*s == '{') {
  586. // ${variable_name}
  587. s++;
  588. while (*s && *s != '}' && i < len)
  589. buffer[i++] = *s++;
  590. if(*s == '}')
  591. s++;
  592. }
  593. else {
  594. // $variable_name
  595. while (*s && !isvariableterm(*s) && i < len)
  596. buffer[i++] = *s++;
  597. }
  598. buffer[i] = '\0';
  599. if (buffer[0]) {
  600. *string = s;
  601. return 1;
  602. }
  603. }
  604. return 0;
  605. }
  606. static inline int parse_constant(const char **string, NETDATA_DOUBLE *number) {
  607. char *end = NULL;
  608. NETDATA_DOUBLE n = str2ndd(*string, &end);
  609. if(unlikely(!end || *string == end)) {
  610. *number = 0;
  611. return 0;
  612. }
  613. *number = n;
  614. *string = end;
  615. return 1;
  616. }
  617. static inline int parse_abs(const char **string) {
  618. const char *s = *string;
  619. // ABS
  620. if((s[0] == 'A' || s[0] == 'a') && (s[1] == 'B' || s[1] == 'b') && (s[2] == 'S' || s[2] == 's') && s[3] == '(') {
  621. *string = &s[3];
  622. return 1;
  623. }
  624. return 0;
  625. }
  626. static inline int parse_if_then_else(const char **string) {
  627. const char *s = *string;
  628. // ?
  629. if(s[0] == '?') {
  630. *string = &s[1];
  631. return 1;
  632. }
  633. return 0;
  634. }
  635. static struct operator_parser {
  636. unsigned char id;
  637. int (*parse)(const char **);
  638. } operator_parsers[] = {
  639. // the order in this list is important!
  640. // the first matching will be used
  641. // so place the longer of overlapping ones
  642. // at the top
  643. { EVAL_OPERATOR_AND, parse_and },
  644. { EVAL_OPERATOR_OR, parse_or },
  645. { EVAL_OPERATOR_GREATER_THAN_OR_EQUAL, parse_greater_than_or_equal },
  646. { EVAL_OPERATOR_LESS_THAN_OR_EQUAL, parse_less_than_or_equal },
  647. { EVAL_OPERATOR_NOT_EQUAL, parse_not_equal },
  648. { EVAL_OPERATOR_EQUAL, parse_equal },
  649. { EVAL_OPERATOR_LESS, parse_less },
  650. { EVAL_OPERATOR_GREATER, parse_greater },
  651. { EVAL_OPERATOR_PLUS, parse_plus },
  652. { EVAL_OPERATOR_MINUS, parse_minus },
  653. { EVAL_OPERATOR_MULTIPLY, parse_multiply },
  654. { EVAL_OPERATOR_DIVIDE, parse_divide },
  655. { EVAL_OPERATOR_IF_THEN_ELSE, parse_if_then_else },
  656. /* we should not put in this list the following:
  657. *
  658. * - NOT
  659. * - (
  660. * - )
  661. *
  662. * these are handled in code
  663. */
  664. // termination
  665. { EVAL_OPERATOR_NOP, NULL }
  666. };
  667. static inline unsigned char parse_operator(const char **string, int *precedence) {
  668. skip_spaces(string);
  669. int i;
  670. for(i = 0 ; operator_parsers[i].parse != NULL ; i++)
  671. if(operator_parsers[i].parse(string)) {
  672. if(precedence) *precedence = eval_precedence(operator_parsers[i].id);
  673. return operator_parsers[i].id;
  674. }
  675. return EVAL_OPERATOR_NOP;
  676. }
  677. // ----------------------------------------------------------------------------
  678. // memory management
  679. static inline EVAL_NODE *eval_node_alloc(int count) {
  680. static int id = 1;
  681. EVAL_NODE *op = callocz(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
  682. op->id = id++;
  683. op->operator = EVAL_OPERATOR_NOP;
  684. op->precedence = eval_precedence(EVAL_OPERATOR_NOP);
  685. op->count = count;
  686. return op;
  687. }
  688. static inline void eval_node_set_value_to_node(EVAL_NODE *op, int pos, EVAL_NODE *value) {
  689. if(pos >= op->count)
  690. fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
  691. op->ops[pos].type = EVAL_VALUE_EXPRESSION;
  692. op->ops[pos].expression = value;
  693. }
  694. static inline void eval_node_set_value_to_constant(EVAL_NODE *op, int pos, NETDATA_DOUBLE value) {
  695. if(pos >= op->count)
  696. fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
  697. op->ops[pos].type = EVAL_VALUE_NUMBER;
  698. op->ops[pos].number = value;
  699. }
  700. static inline void eval_node_set_value_to_variable(EVAL_NODE *op, int pos, const char *variable) {
  701. if(pos >= op->count)
  702. fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
  703. op->ops[pos].type = EVAL_VALUE_VARIABLE;
  704. op->ops[pos].variable = callocz(1, sizeof(EVAL_VARIABLE));
  705. op->ops[pos].variable->name = strdupz(variable);
  706. op->ops[pos].variable->hash = simple_hash(op->ops[pos].variable->name);
  707. }
  708. static inline void eval_variable_free(EVAL_VARIABLE *v) {
  709. freez(v->name);
  710. freez(v);
  711. }
  712. static inline void eval_value_free(EVAL_VALUE *v) {
  713. switch(v->type) {
  714. case EVAL_VALUE_EXPRESSION:
  715. eval_node_free(v->expression);
  716. break;
  717. case EVAL_VALUE_VARIABLE:
  718. eval_variable_free(v->variable);
  719. break;
  720. default:
  721. break;
  722. }
  723. }
  724. static inline void eval_node_free(EVAL_NODE *op) {
  725. if(op->count) {
  726. int i;
  727. for(i = op->count - 1; i >= 0 ;i--)
  728. eval_value_free(&op->ops[i]);
  729. }
  730. freez(op);
  731. }
  732. // ----------------------------------------------------------------------------
  733. // the parsing logic
  734. // helper function to avoid allocations all over the place
  735. static inline EVAL_NODE *parse_next_operand_given_its_operator(const char **string, unsigned char operator_type, int *error) {
  736. EVAL_NODE *sub = parse_one_full_operand(string, error);
  737. if(!sub) return NULL;
  738. EVAL_NODE *op = eval_node_alloc(1);
  739. op->operator = operator_type;
  740. eval_node_set_value_to_node(op, 0, sub);
  741. return op;
  742. }
  743. // parse a full operand, including its sign or other associative operator (e.g. NOT)
  744. static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error) {
  745. char variable_buffer[EVAL_MAX_VARIABLE_NAME_LENGTH + 1];
  746. EVAL_NODE *op1 = NULL;
  747. NETDATA_DOUBLE number;
  748. *error = EVAL_ERROR_OK;
  749. skip_spaces(string);
  750. if(!(**string)) {
  751. *error = EVAL_ERROR_MISSING_OPERAND;
  752. return NULL;
  753. }
  754. if(parse_not(string)) {
  755. op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_NOT, error);
  756. op1->precedence = eval_precedence(EVAL_OPERATOR_NOT);
  757. }
  758. else if(parse_plus(string)) {
  759. op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_SIGN_PLUS, error);
  760. op1->precedence = eval_precedence(EVAL_OPERATOR_SIGN_PLUS);
  761. }
  762. else if(parse_minus(string)) {
  763. op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_SIGN_MINUS, error);
  764. op1->precedence = eval_precedence(EVAL_OPERATOR_SIGN_MINUS);
  765. }
  766. else if(parse_abs(string)) {
  767. op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_ABS, error);
  768. op1->precedence = eval_precedence(EVAL_OPERATOR_ABS);
  769. }
  770. else if(parse_open_subexpression(string)) {
  771. EVAL_NODE *sub = parse_full_expression(string, error);
  772. if(sub) {
  773. op1 = eval_node_alloc(1);
  774. op1->operator = EVAL_OPERATOR_EXPRESSION_OPEN;
  775. op1->precedence = eval_precedence(EVAL_OPERATOR_EXPRESSION_OPEN);
  776. eval_node_set_value_to_node(op1, 0, sub);
  777. if(!parse_close_subexpression(string)) {
  778. *error = EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION;
  779. eval_node_free(op1);
  780. return NULL;
  781. }
  782. }
  783. }
  784. else if(parse_variable(string, variable_buffer, EVAL_MAX_VARIABLE_NAME_LENGTH)) {
  785. op1 = eval_node_alloc(1);
  786. op1->operator = EVAL_OPERATOR_NOP;
  787. eval_node_set_value_to_variable(op1, 0, variable_buffer);
  788. }
  789. else if(parse_constant(string, &number)) {
  790. op1 = eval_node_alloc(1);
  791. op1->operator = EVAL_OPERATOR_NOP;
  792. eval_node_set_value_to_constant(op1, 0, number);
  793. }
  794. else if(**string)
  795. *error = EVAL_ERROR_UNKNOWN_OPERAND;
  796. else
  797. *error = EVAL_ERROR_MISSING_OPERAND;
  798. return op1;
  799. }
  800. // parse an operator and the rest of the expression
  801. // precedence processing is handled here
  802. static inline EVAL_NODE *parse_rest_of_expression(const char **string, int *error, EVAL_NODE *op1) {
  803. EVAL_NODE *op2 = NULL;
  804. unsigned char operator;
  805. int precedence;
  806. operator = parse_operator(string, &precedence);
  807. skip_spaces(string);
  808. if(operator != EVAL_OPERATOR_NOP) {
  809. op2 = parse_one_full_operand(string, error);
  810. if(!op2) {
  811. // error is already reported
  812. eval_node_free(op1);
  813. return NULL;
  814. }
  815. EVAL_NODE *op = eval_node_alloc(operators[operator].parameters);
  816. op->operator = operator;
  817. op->precedence = precedence;
  818. if(operator == EVAL_OPERATOR_IF_THEN_ELSE && op->count == 3) {
  819. skip_spaces(string);
  820. if(**string != ':') {
  821. eval_node_free(op);
  822. eval_node_free(op1);
  823. eval_node_free(op2);
  824. *error = EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE;
  825. return NULL;
  826. }
  827. (*string)++;
  828. skip_spaces(string);
  829. EVAL_NODE *op3 = parse_one_full_operand(string, error);
  830. if(!op3) {
  831. eval_node_free(op);
  832. eval_node_free(op1);
  833. eval_node_free(op2);
  834. // error is already reported
  835. return NULL;
  836. }
  837. eval_node_set_value_to_node(op, 2, op3);
  838. }
  839. eval_node_set_value_to_node(op, 1, op2);
  840. // precedence processing
  841. // if this operator has a higher precedence compared to its next
  842. // put the next operator on top of us (top = evaluated later)
  843. // function recursion does the rest...
  844. if(op->precedence > op1->precedence && op1->count == 2 && op1->operator != '(' && op1->ops[1].type == EVAL_VALUE_EXPRESSION) {
  845. eval_node_set_value_to_node(op, 0, op1->ops[1].expression);
  846. op1->ops[1].expression = op;
  847. op = op1;
  848. }
  849. else
  850. eval_node_set_value_to_node(op, 0, op1);
  851. return parse_rest_of_expression(string, error, op);
  852. }
  853. else if(**string == ')') {
  854. ;
  855. }
  856. else if(**string) {
  857. eval_node_free(op1);
  858. op1 = NULL;
  859. *error = EVAL_ERROR_MISSING_OPERATOR;
  860. }
  861. return op1;
  862. }
  863. // high level function to parse an expression or a sub-expression
  864. static inline EVAL_NODE *parse_full_expression(const char **string, int *error) {
  865. EVAL_NODE *op1 = parse_one_full_operand(string, error);
  866. if(!op1) {
  867. *error = EVAL_ERROR_MISSING_OPERAND;
  868. return NULL;
  869. }
  870. return parse_rest_of_expression(string, error, op1);
  871. }
  872. // ----------------------------------------------------------------------------
  873. // public API
  874. int expression_evaluate(EVAL_EXPRESSION *expression) {
  875. expression->error = EVAL_ERROR_OK;
  876. buffer_reset(expression->error_msg);
  877. expression->result = eval_node(expression, (EVAL_NODE *)expression->nodes, &expression->error);
  878. if(unlikely(isnan(expression->result))) {
  879. if(expression->error == EVAL_ERROR_OK)
  880. expression->error = EVAL_ERROR_VALUE_IS_NAN;
  881. }
  882. else if(unlikely(isinf(expression->result))) {
  883. if(expression->error == EVAL_ERROR_OK)
  884. expression->error = EVAL_ERROR_VALUE_IS_INFINITE;
  885. }
  886. else if(unlikely(expression->error == EVAL_ERROR_UNKNOWN_VARIABLE)) {
  887. // although there is an unknown variable
  888. // the expression was evaluated successfully
  889. expression->error = EVAL_ERROR_OK;
  890. }
  891. if(expression->error != EVAL_ERROR_OK) {
  892. expression->result = NAN;
  893. if(buffer_strlen(expression->error_msg))
  894. buffer_strcat(expression->error_msg, "; ");
  895. buffer_sprintf(expression->error_msg, "failed to evaluate expression with error %d (%s)", expression->error, expression_strerror(expression->error));
  896. return 0;
  897. }
  898. return 1;
  899. }
  900. EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error) {
  901. const char *s = string;
  902. int err = EVAL_ERROR_OK;
  903. EVAL_NODE *op = parse_full_expression(&s, &err);
  904. if(*s) {
  905. if(op) {
  906. eval_node_free(op);
  907. op = NULL;
  908. }
  909. err = EVAL_ERROR_REMAINING_GARBAGE;
  910. }
  911. if (failed_at) *failed_at = s;
  912. if (error) *error = err;
  913. if(!op) {
  914. unsigned long pos = s - string + 1;
  915. error("failed to parse expression '%s': %s at character %lu (i.e.: '%s').", string, expression_strerror(err), pos, s);
  916. return NULL;
  917. }
  918. BUFFER *out = buffer_create(1024);
  919. print_parsed_as_node(out, op, &err);
  920. if(err != EVAL_ERROR_OK) {
  921. error("failed to re-generate expression '%s' with reason: %s", string, expression_strerror(err));
  922. eval_node_free(op);
  923. buffer_free(out);
  924. return NULL;
  925. }
  926. EVAL_EXPRESSION *exp = callocz(1, sizeof(EVAL_EXPRESSION));
  927. exp->source = strdupz(string);
  928. exp->parsed_as = strdupz(buffer_tostring(out));
  929. buffer_free(out);
  930. exp->error_msg = buffer_create(100);
  931. exp->nodes = (void *)op;
  932. return exp;
  933. }
  934. void expression_free(EVAL_EXPRESSION *expression) {
  935. if(!expression) return;
  936. if(expression->nodes) eval_node_free((EVAL_NODE *)expression->nodes);
  937. freez((void *)expression->source);
  938. freez((void *)expression->parsed_as);
  939. buffer_free(expression->error_msg);
  940. freez(expression);
  941. }
  942. const char *expression_strerror(int error) {
  943. switch(error) {
  944. case EVAL_ERROR_OK:
  945. return "success";
  946. case EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION:
  947. return "missing closing parenthesis";
  948. case EVAL_ERROR_UNKNOWN_OPERAND:
  949. return "unknown operand";
  950. case EVAL_ERROR_MISSING_OPERAND:
  951. return "expected operand";
  952. case EVAL_ERROR_MISSING_OPERATOR:
  953. return "expected operator";
  954. case EVAL_ERROR_REMAINING_GARBAGE:
  955. return "remaining characters after expression";
  956. case EVAL_ERROR_INVALID_VALUE:
  957. return "invalid value structure - internal error";
  958. case EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS:
  959. return "wrong number of operands for operation - internal error";
  960. case EVAL_ERROR_VALUE_IS_NAN:
  961. return "value is unset";
  962. case EVAL_ERROR_VALUE_IS_INFINITE:
  963. return "computed value is infinite";
  964. case EVAL_ERROR_UNKNOWN_VARIABLE:
  965. return "undefined variable";
  966. case EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE:
  967. return "missing second sub-expression of inline conditional";
  968. default:
  969. return "unknown error";
  970. }
  971. }