1 /*
2 Copyright 2021 Northern.tech AS
3
4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 3.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19 To the extent this program is licensed as part of the Enterprise
20 versions of CFEngine, the applicable Commercial Open Source License
21 (COSL) may apply to this file if you as a licensee so wish it. See
22 included file COSL.txt.
23 */
24
25 #ifndef CFENGINE_VAR_EXPRESSIONS_H
26 #define CFENGINE_VAR_EXPRESSIONS_H
27
28 #include <platform.h>
29
30 #include <string_expressions.h>
31 #include <policy.h>
32 #include <string_lib.h> /* StringContains() */
33
34 /**
35 VarRef is immutable, which means that after allocated the members never
36 change, until all of it is freed.
37
38 @TODO constify all pointers returned from VarRef initializers (VarRefCopy,
39 VarRefParse, etc)
40 */
41 typedef struct
42 {
43 char *ns;
44 char *scope;
45 char *lval;
46 char **indices;
47 size_t num_indices;
48
49 /* TODO performance: when using VarRefCopy() we just need to allocate one
50 * big chunk with malloc() and all pointers can point in there. This
51 * whole struct can be a single malloc'ed chunk with array[] space in
52 * the end, if our use allows it (no changes after creation). */
53 /* bool single_alloc; */
54 /* char space[]; */
55 } VarRef;
56
57 VarRef *VarRefCopy(const VarRef *ref);
58 VarRef *VarRefCopyLocalized(const VarRef *ref);
59 VarRef *VarRefCopyIndexless(const VarRef *ref);
60
61 bool is_this_not_special(const char *scope, const char *lval);
62
63 VarRef *VarRefParse(const char *var_ref_string);
64
65 VarRef *VarRefParseFromBundle(const char *var_ref_string, const Bundle *bundle);
66 VarRef *VarRefParseFromScope(const char *var_ref_string, const char *scope);
67 VarRef *VarRefParseFromNamespaceAndScope(const char *qualified_name,
68 const char *_ns, const char *_scope,
69 char ns_separator, char scope_separator);
70 VarRef VarRefConst(const char *ns, const char *scope, const char *lval);
71
72 void VarRefDestroy (VarRef *ref);
73 void VarRefDestroy_untyped(void *ref);
74
75 char *VarRefToString(const VarRef *ref, bool qualified);
76
77 void VarRefSetMeta(VarRef *ref, bool enabled);
78
79 bool VarRefIsQualified(const VarRef *ref);
80 void VarRefQualify(VarRef *ref, const char *ns, const char *scope);
81 void VarRefAddIndex(VarRef *ref, const char *index);
82
83 int VarRefCompare(const VarRef *a, const VarRef *b);
84
85 bool VarRefEqual_untyped(const void *a, const void *b);
86
87 unsigned int VarRefHash_untyped(const void *ref,
88 unsigned int seed);
89
StringContainsUnresolved(const char * str)90 static inline bool StringContainsUnresolved(const char *str)
91 {
92 // clang-format off
93 return (StringContains(str, "$(")
94 || StringContains(str, "${")
95 || StringContains(str, "@{")
96 || StringContains(str, "@("));
97 // clang-format on
98 }
99
100 /** Whether #str is of form "@{something}"/"@(something)" or not. */
StringIsBareNonScalarRef(const char * str)101 static inline bool StringIsBareNonScalarRef(const char *str)
102 {
103 assert(str != NULL);
104 const size_t str_len = strlen(str);
105 return ((str[0] == '@') &&
106 (((str[1] == '(') && (str[str_len - 1] == ')')) ||
107 ((str[1] == '{') && (str[str_len - 1] == '}'))));
108 }
109
110 #endif
111