summaryrefslogtreecommitdiffstats
path: root/src/shared/compare-operator.c
blob: 0da28fc5c68b60c63a642202a5311acdc2bd4425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <fnmatch.h>

#include "compare-operator.h"
#include "string-util.h"

CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags flags) {
        static const struct {
                CompareOperator op;
                const char *str;
                CompareOperatorParseFlags valid_mask; /* If this operator appears when flags in mask not set, fail */
                CompareOperatorParseFlags need_mask;  /* Skip over this operator when flags in mask not set */
        } table[] = {
                { COMPARE_FNMATCH_EQUAL,    "$=",  .valid_mask = COMPARE_ALLOW_FNMATCH   },
                { COMPARE_FNMATCH_UNEQUAL,  "!$=", .valid_mask = COMPARE_ALLOW_FNMATCH   },

                { COMPARE_UNEQUAL,          "<>"                                         },
                { COMPARE_LOWER_OR_EQUAL,   "<="                                         },
                { COMPARE_GREATER_OR_EQUAL, ">="                                         },
                { COMPARE_LOWER,            "<"                                          },
                { COMPARE_GREATER,          ">"                                          },
                { COMPARE_EQUAL,            "=="                                         },
                { COMPARE_STRING_EQUAL,     "=",   .need_mask = COMPARE_EQUAL_BY_STRING  },
                { COMPARE_EQUAL,            "="                                          },
                { COMPARE_STRING_UNEQUAL,   "!=",  .need_mask = COMPARE_EQUAL_BY_STRING  },
                { COMPARE_UNEQUAL,          "!="                                         },

                { COMPARE_LOWER,            "lt",  .valid_mask = COMPARE_ALLOW_TEXTUAL   },
                { COMPARE_LOWER_OR_EQUAL,   "le",  .valid_mask = COMPARE_ALLOW_TEXTUAL   },
                { COMPARE_EQUAL,            "eq",  .valid_mask = COMPARE_ALLOW_TEXTUAL   },
                { COMPARE_UNEQUAL,          "ne",  .valid_mask = COMPARE_ALLOW_TEXTUAL   },
                { COMPARE_GREATER_OR_EQUAL, "ge",  .valid_mask = COMPARE_ALLOW_TEXTUAL   },
                { COMPARE_GREATER,          "gt",  .valid_mask = COMPARE_ALLOW_TEXTUAL   },
        };

        assert(s);

        if (!*s) /* Hmm, we already reached the end, for example because extract_first_word() and
                  * parse_compare_operator() are use on the same string? */
                return _COMPARE_OPERATOR_INVALID;

        for (size_t i = 0; i < ELEMENTSOF(table); i ++) {
                const char *e;

                if (table[i].need_mask != 0 && !FLAGS_SET(flags, table[i].need_mask))
                        continue;

                e = startswith(*s, table[i].str);
                if (e) {
                        if (table[i].valid_mask != 0 && !FLAGS_SET(flags, table[i].valid_mask))
                                return _COMPARE_OPERATOR_INVALID;

                        *s = e;
                        return table[i].op;
                }
        }

        return _COMPARE_OPERATOR_INVALID;
}

int test_order(int k, CompareOperator op) {

        switch (op) {

        case COMPARE_LOWER:
                return k < 0;

        case COMPARE_LOWER_OR_EQUAL:
                return k <= 0;

        case COMPARE_EQUAL:
                return k == 0;

        case COMPARE_UNEQUAL:
                return k != 0;

        case COMPARE_GREATER_OR_EQUAL:
                return k >= 0;

        case COMPARE_GREATER:
                return k > 0;

        default:
                return -EINVAL;
        }
}

int version_or_fnmatch_compare(
                CompareOperator op,
                const char *a,
                const char *b) {
        int r;

        switch (op) {

        case COMPARE_STRING_EQUAL:
                return streq_ptr(a, b);

        case COMPARE_STRING_UNEQUAL:
                return !streq_ptr(a, b);

        case COMPARE_FNMATCH_EQUAL:
                r = fnmatch(b, a, 0);
                return r == 0 ? true :
                        r == FNM_NOMATCH ? false : -EINVAL;

        case COMPARE_FNMATCH_UNEQUAL:
                r = fnmatch(b, a, 0);
                return r == FNM_NOMATCH ? true:
                        r == 0 ? false : -EINVAL;

        case _COMPARE_OPERATOR_ORDER_FIRST..._COMPARE_OPERATOR_ORDER_LAST:
                return test_order(strverscmp_improved(a, b), op);

        default:
                return -EINVAL;
        }
}