eval.c 36 KB

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