| 1 | /* |
| 2 | Authors: |
| 3 | Tomas Halman <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2019 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <string.h> |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include "evaluator.h" |
| 29 | #include "lib/util/string_array.h" |
| 30 | #include "common/common.h" |
| 31 | |
| 32 | enum e_state { |
| 33 | E_STATE_INVALID = 0, |
| 34 | E_STATE_BEGIN, |
| 35 | E_STATE_STRING, |
| 36 | E_STATE_SUBEXPRESSION, |
| 37 | E_STATE_OPERATOR, |
| 38 | E_STATE_UNARY_NOT, |
| 39 | E_STATE_END, |
| 40 | }; |
| 41 | |
| 42 | enum e_operator { |
| 43 | E_OPERATOR_INVALID = 0, |
| 44 | E_OPERATOR_AND, |
| 45 | E_OPERATOR_OR, |
| 46 | E_OPERATOR_NOT |
| 47 | }; |
| 48 | |
| 49 | struct evaluator { |
| 50 | const char *expression; |
| 51 | const char *cursor; |
| 52 | char *token; |
| 53 | size_t tokensize; |
| 54 | int depth; |
| 55 | enum e_state state; |
| 56 | }; |
| 57 | |
| 58 | struct e_map { |
| 59 | char *name; |
| 60 | enum e_operator value; |
| 61 | }; |
| 62 | |
| 63 | static struct e_map operators[] = { |
| 64 | {"and", E_OPERATOR_AND}, |
| 65 | {"or", E_OPERATOR_OR}, |
| 66 | {"not", E_OPERATOR_NOT}, |
| 67 | {NULL, E_OPERATOR_INVALID} |
| 68 | }; |
| 69 | |
| 70 | static errno_t evaluator_state_machine(struct evaluator *self, |
| 71 | int depth, |
| 72 | const char **features, |
| 73 | bool *_result); |
| 74 | |
| 75 | /* |
| 76 | * This function reads new item/token from expression. |
| 77 | * It is then stored in self->token. Expected tokens are |
| 78 | * '(', ')', group of letters (used for operators) or |
| 79 | * strings (characters enclosed by quotation marks). |
| 80 | * |
| 81 | * Strings are copied into self->token with quotation |
| 82 | * marks and they are used for feature's names. |
| 83 | * |
| 84 | */ |
| 85 | static errno_t evaluator_next_token(struct evaluator *self) |
| 86 | { |
| 87 | const char *p; |
| 88 | |
| 89 | while (isblank(*self->cursor)) { |
| 90 | self->cursor++; |
| 91 | } |
| 92 | |
| 93 | memset(self->token, 0, self->tokensize); |
| 94 | switch(tolower(*self->cursor)) { |
| 95 | case '(': |
| 96 | case ')': |
| 97 | strncpy(self->token, self->cursor, 1); |
| 98 | self->cursor++; |
| 99 | return EOK; |
| 100 | case '"': |
| 101 | p = strchr(&(self->cursor[1]), '"'); |
| 102 | if (p != NULL) { |
| 103 | strncpy(self->token, self->cursor, p - self->cursor + 1); |
| 104 | self->cursor = ++p; |
| 105 | return EOK; |
| 106 | } |
| 107 | self->cursor = &self->expression[self->tokensize - 1]; |
| 108 | return EINVAL; |
| 109 | case '\0': |
| 110 | return EOK; |
| 111 | default: |
| 112 | p = self->cursor; |
| 113 | while (isalpha(*p)) { |
| 114 | ++p; |
| 115 | } |
| 116 | strncpy(self->token, self->cursor, p - self->cursor); |
| 117 | self->cursor = p; |
| 118 | return EOK; |
| 119 | } |
| 120 | return EOK; |
| 121 | } |
| 122 | |
| 123 | static enum e_operator evaluator_operator(const char *token) |
| 124 | { |
| 125 | int i; |
| 126 | |
| 127 | if (token == NULL) { |
| 128 | return E_OPERATOR_INVALID; |
| 129 | } |
| 130 | |
| 131 | for (i = 0; operators[i].name != NULL; ++i) { |
| 132 | if (strcasecmp(token, operators[i].name) == 0) { |
| 133 | return operators[i].value; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return E_OPERATOR_INVALID; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * This function return the state in which the |
| 142 | * state should change by evaluating self->token |
| 143 | * |
| 144 | * '"feature"' -> E_STATE_STRING -- i. e. feature name |
| 145 | * '(' -> E_STATE_SUBEXPRESSION -- sub expression |
| 146 | * ')' -> E_STATE_END -- end of subexpression |
| 147 | * 'letters' -> this should be an operator, find which one |
| 148 | * state might be E_STATE_OPERATOR or |
| 149 | * E_STATE_UNARY_NOT then |
| 150 | */ |
| 151 | static enum e_state evaluator_token_to_state(const char *token) |
| 152 | { |
| 153 | enum e_operator operator; |
| 154 | |
| 155 | switch (tolower(token[0])) { |
| 156 | case '"': |
| 157 | return E_STATE_STRING; |
| 158 | case '(': |
| 159 | return E_STATE_SUBEXPRESSION; |
| 160 | case ')': |
| 161 | return E_STATE_END; |
| 162 | default: |
| 163 | operator = evaluator_operator(token); |
| 164 | if (operator != E_OPERATOR_INVALID) { |
| 165 | if (operator == E_OPERATOR_NOT) { |
| 166 | return E_STATE_UNARY_NOT; |
| 167 | } |
| 168 | return E_STATE_OPERATOR; |
| 169 | } |
| 170 | } |
| 171 | return E_STATE_INVALID; |
| 172 | } |
| 173 | |
| 174 | static errno_t evaluator_get_feature(const char *token, |
| 175 | const char **features, |
| 176 | bool *_result) |
| 177 | { |
| 178 | char *feature_name; |
| 179 | int len; |
| 180 | |
| 181 | *_result = false; |
| 182 | if (features == NULL || token == NULL) { |
| 183 | return EINVAL; |
| 184 | } |
| 185 | |
| 186 | len = strlen(token) - 2; |
| 187 | feature_name = format("%.*s", len, &token[1]); |
| 188 | if (feature_name == NULL) { |
| 189 | return ENOMEM; |
| 190 | } |
| 191 | |
| 192 | *_result = string_array_has_value((char **)features, feature_name); |
| 193 | free(feature_name); |
| 194 | |
| 195 | return EOK; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Handle state machine in E_STATE_BEGIN state |
| 200 | */ |
| 201 | static errno_t evaluator_handle_begin_state(struct evaluator *self, |
| 202 | int depth, |
| 203 | const char **features, |
| 204 | enum e_state nextstate, |
| 205 | bool *result, |
| 206 | bool *negation, |
| 207 | enum e_operator *operator) |
| 208 | { |
| 209 | |
| 210 | bool sub_result; |
| 211 | errno_t ret; |
| 212 | |
| 213 | switch (nextstate) { |
| 214 | case E_STATE_STRING: |
| 215 | return evaluator_get_feature(self->token, features, result); |
| 216 | case E_STATE_UNARY_NOT: |
| 217 | *negation = !*negation; |
| 218 | return EOK; |
| 219 | case E_STATE_SUBEXPRESSION: |
| 220 | ret = evaluator_state_machine(self, depth + 1, features, |
| 221 | &sub_result); |
| 222 | if (ret != EOK) { |
| 223 | return ret; |
| 224 | } |
| 225 | switch(*operator) { |
| 226 | case E_OPERATOR_AND: |
| 227 | *result = *result && (*negation ? !sub_result : sub_result); |
| 228 | *negation = false; |
| 229 | break; |
| 230 | case E_OPERATOR_OR: |
| 231 | *result = *result || (*negation ? !sub_result : sub_result); |
| 232 | *negation = false; |
| 233 | break; |
| 234 | case E_OPERATOR_INVALID: /* no operator yet */ |
| 235 | *result = (*negation ? !sub_result : sub_result); |
| 236 | *negation = false; |
| 237 | break; |
| 238 | default: |
| 239 | return EINVAL; |
| 240 | } |
| 241 | return EOK; |
| 242 | default: |
| 243 | return EINVAL; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Handle state machine in E_STATE_OPERATOR and E_STATE_UNARY_NOT state |
| 249 | * when the next state is going to be E_STATE_STRING |
| 250 | */ |
| 251 | static errno_t evaluator_handle_operator_state_string(struct evaluator *self, |
| 252 | const char **features, |
| 253 | bool *result, |
| 254 | bool *negation, |
| 255 | enum e_operator *operator) |
| 256 | { |
| 257 | errno_t ret; |
| 258 | bool sub_result; |
| 259 | |
| 260 | switch(*operator) { |
| 261 | case E_OPERATOR_AND: |
| 262 | ret = evaluator_get_feature(self->token, features, &sub_result); |
| 263 | if (ret != EOK) { |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | *result = *result && (*negation ? !sub_result : sub_result); |
| 268 | *negation = false; |
| 269 | break; |
| 270 | case E_OPERATOR_OR: |
| 271 | ret = evaluator_get_feature(self->token, features, &sub_result); |
| 272 | if (ret != EOK) { |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | *result = *result || (*negation ? !sub_result : sub_result); |
| 277 | *negation = false; |
| 278 | break; |
| 279 | case E_OPERATOR_INVALID: /* no operator yet */ |
| 280 | ret = evaluator_get_feature(self->token, features, &sub_result); |
| 281 | if (ret != EOK) { |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | *result = *negation ? !sub_result : sub_result; |
| 286 | *negation = false; |
| 287 | break; |
| 288 | default: |
| 289 | return EINVAL; |
| 290 | } |
| 291 | return EOK; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Handle state machine in E_STATE_OPERATOR and E_STATE_UNARY_NOT state |
| 296 | * when the next state is going to be E_STATE_SUBEXPRESSION |
| 297 | */ |
| 298 | static errno_t evaluator_handle_operator_state_subexpression(struct evaluator *self, |
| 299 | int depth, |
| 300 | const char **features, |
| 301 | bool *result, |
| 302 | bool *negation, |
| 303 | enum e_operator *operator) |
| 304 | { |
| 305 | bool sub_result; |
| 306 | errno_t ret; |
| 307 | |
| 308 | ret = evaluator_state_machine(self, depth + 1, features, &sub_result); |
| 309 | if (ret != EOK) { |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | switch(*operator) { |
| 314 | case E_OPERATOR_AND: |
| 315 | *result = *result && (*negation ? !sub_result : sub_result); |
| 316 | *negation = false; |
| 317 | break; |
| 318 | case E_OPERATOR_OR: |
| 319 | *result = *result || (*negation ? !sub_result : sub_result); |
| 320 | *negation = false; |
| 321 | break; |
| 322 | case E_OPERATOR_INVALID: /* no operator yet */ |
| 323 | *result = (*negation ? !sub_result : sub_result); |
| 324 | *negation = false; |
| 325 | break; |
| 326 | default: |
| 327 | return EINVAL; |
| 328 | } |
| 329 | return EOK; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Handle state machine in E_STATE_OPERATOR and E_STATE_UNARY_NOT state |
| 334 | */ |
| 335 | static errno_t evaluator_handle_operator_state(struct evaluator *self, |
| 336 | int depth, |
| 337 | const char **features, |
| 338 | enum e_state nextstate, |
| 339 | bool *result, |
| 340 | bool *negation, |
| 341 | enum e_operator *operator) |
| 342 | { |
| 343 | switch (nextstate) { |
| 344 | case E_STATE_STRING: |
| 345 | return evaluator_handle_operator_state_string(self, features, result, negation, operator); |
| 346 | case E_STATE_UNARY_NOT: |
| 347 | *negation = !*negation; |
| 348 | return EOK; |
| 349 | case E_STATE_SUBEXPRESSION: |
| 350 | return evaluator_handle_operator_state_subexpression(self, depth, features, result, negation, operator); |
| 351 | case E_STATE_END: |
| 352 | if (depth == 0) { |
| 353 | return EINVAL; /* too many ) */ |
| 354 | } |
| 355 | return EOK; |
| 356 | default: |
| 357 | return EINVAL; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Handle state machine in E_STATE_SUBEXPRESSION and E_STATE_STRING state |
| 363 | */ |
| 364 | static errno_t evaluator_handle_expression_state(struct evaluator *self, |
| 365 | int depth, |
| 366 | const char **features, |
| 367 | enum e_state nextstate, |
| 368 | bool *result, |
| 369 | bool *negation, |
| 370 | enum e_operator *operator) |
| 371 | { |
| 372 | switch (nextstate) { |
| 373 | case E_STATE_OPERATOR: |
| 374 | *operator = evaluator_operator(self->token); |
| 375 | return EOK; |
| 376 | case E_STATE_END: |
| 377 | if (depth == 0) { |
| 378 | return EINVAL; /* too many ) */ |
| 379 | } |
| 380 | return EOK; |
| 381 | default: |
| 382 | return EINVAL; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static errno_t evaluator_state_machine(struct evaluator *self, |
| 387 | int depth, |
| 388 | const char **features, |
| 389 | bool *_result) |
| 390 | { |
| 391 | if (self == NULL || _result == NULL) { |
| 392 | return EINVAL; |
| 393 | } |
| 394 | |
| 395 | enum e_state state = E_STATE_BEGIN; |
| 396 | enum e_state nextstate; |
| 397 | bool result = false; |
| 398 | bool negation = false; |
| 399 | enum e_operator operator = E_OPERATOR_INVALID; |
| 400 | errno_t ret; |
| 401 | |
| 402 | do { |
| 403 | ret = evaluator_next_token(self); |
| 404 | if (ret != EOK) { |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | if (self->token[0] != '\0') { |
| 409 | nextstate = evaluator_token_to_state(self->token); |
| 410 | switch(state) { |
| 411 | case E_STATE_BEGIN: |
| 412 | ret = evaluator_handle_begin_state(self, depth, features, |
| 413 | nextstate, &result, |
| 414 | &negation, &operator); |
| 415 | if (ret != EOK) { |
| 416 | return ret; |
| 417 | } |
| 418 | break; |
| 419 | case E_STATE_UNARY_NOT: |
| 420 | case E_STATE_OPERATOR: |
| 421 | ret = evaluator_handle_operator_state(self, depth, features, |
| 422 | nextstate, &result, |
| 423 | &negation, &operator); |
| 424 | if (ret != EOK) { |
| 425 | return ret; |
| 426 | } |
| 427 | break; |
| 428 | case E_STATE_SUBEXPRESSION: |
| 429 | case E_STATE_STRING: |
| 430 | ret = evaluator_handle_expression_state(self, depth, features, |
| 431 | nextstate, &result, |
| 432 | &negation, &operator); |
| 433 | if (ret != EOK) { |
| 434 | return ret; |
| 435 | } |
| 436 | if (nextstate == E_STATE_END) { |
| 437 | *_result = result; |
| 438 | return ret; |
| 439 | } |
| 440 | break; |
| 441 | case E_STATE_END: |
| 442 | if (depth == 0) { |
| 443 | return EINVAL; /* too many ) */ |
| 444 | } |
| 445 | break; |
| 446 | case E_STATE_INVALID: |
| 447 | return EINVAL; |
| 448 | } |
| 449 | state = nextstate; |
| 450 | } |
| 451 | } while (self->token[0] != '\0'); |
| 452 | |
| 453 | if (depth != 0) { |
| 454 | return EINVAL; /* too many ) */ |
| 455 | } |
| 456 | |
| 457 | if (state == E_STATE_OPERATOR || |
| 458 | state == E_STATE_UNARY_NOT || |
| 459 | state == E_STATE_BEGIN) { |
| 460 | |
| 461 | return EINVAL; /* expression can't end in this state */ |
| 462 | } |
| 463 | |
| 464 | *_result = result; |
| 465 | |
| 466 | return EOK; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Set an expression to be evaluated. |
| 471 | * Returns EOK or an error on failure (ENOMEM). |
| 472 | */ |
| 473 | static errno_t evaluator_set_expression(struct evaluator *self, |
| 474 | const char *expression) |
| 475 | { |
| 476 | if (self == NULL || expression == NULL) { |
| 477 | return EINVAL; |
| 478 | } |
| 479 | |
| 480 | self->expression = expression; |
| 481 | self->cursor = self->expression; |
| 482 | self->tokensize = strlen(expression) + 1; |
| 483 | if (self->token == NULL) { |
| 484 | free(self->token); |
| 485 | } |
| 486 | |
| 487 | self->token = malloc(self->tokensize); |
| 488 | if (self->token == NULL) { |
| 489 | return ENOMEM; |
| 490 | } |
| 491 | |
| 492 | return EOK; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Evaluate expression with set of features |
| 497 | * Result is stored in _result variable. |
| 498 | * Returns EOK or an error on failure. |
| 499 | */ |
| 500 | static errno_t evaluator_evaluate(struct evaluator *self, |
| 501 | const char **features, |
| 502 | bool *_result) |
| 503 | { |
| 504 | if (self == NULL || features == NULL || _result == NULL) { |
| 505 | return EINVAL; |
| 506 | } |
| 507 | |
| 508 | self->cursor = self->expression; |
| 509 | return evaluator_state_machine(self, 0, features, _result); |
| 510 | } |
| 511 | |
| 512 | |
| 513 | errno_t evaluate(const char *expression, const char *features[], bool *_result) |
| 514 | { |
| 515 | struct evaluator *evaluator; |
| 516 | errno_t ret; |
| 517 | |
| 518 | evaluator = malloc_zero(struct evaluator); |
| 519 | if (evaluator == NULL) { |
| 520 | return ENOMEM; |
| 521 | } |
| 522 | |
| 523 | ret = evaluator_set_expression(evaluator, expression); |
| 524 | if (ret != EOK) { |
| 525 | goto done; |
| 526 | } |
| 527 | |
| 528 | ret = evaluator_evaluate(evaluator, features, _result); |
| 529 | |
| 530 | done: |
| 531 | if (evaluator->token != NULL) { |
| 532 | free(evaluator->token); |
| 533 | } |
| 534 | |
| 535 | free(evaluator); |
| 536 | return ret; |
| 537 | } |