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_RLIST_H
26 #define CFENGINE_RLIST_H
27 
28 #include <cf3.defs.h>
29 #include <writer.h>
30 #include <json.h>
31 #include <fncall.h>
32 
33 /* NOTE: an empty Rlist is simply NULL. */
34 struct Rlist_
35 {
36     Rval val;
37     Rlist *next;
38 };
39 
40 RvalType DataTypeToRvalType(DataType datatype);
41 
42 bool RlistValueIsType(const Rlist *rlist, RvalType type);
43 char *RvalScalarValue(Rval rval);
44 FnCall *RvalFnCallValue(Rval rval);
45 Rlist *RvalRlistValue(Rval rval);
46 JsonElement *RvalContainerValue(Rval rval);
47 
48 const char *RvalTypeToString(RvalType type);
49 
50 Rval RvalNew(const void *item, RvalType type);
51 
52 /**
53  * Get new secret Rval.
54  *
55  * @note RvalDestroy() not required to be called on the returned value.
56  */
57 Rval RvalNewSecret();
58 
59 Rval RvalNewRewriter(const void *item, RvalType type, JsonElement *map);
60 Rval RvalCopy(Rval rval);
61 Rval RvalCopyRewriter(Rval rval, JsonElement *map);
62 void RvalDestroy(Rval rval);
63 JsonElement *RvalToJson(Rval rval);
64 char *RvalToString(Rval rval);
65 char *RlistToString(const Rlist *rlist);
66 void RvalWrite(Writer *writer, Rval rval);
67 void RvalWriteQuoted(Writer *writer, Rval rval);
68 unsigned RvalHash(Rval rval, unsigned seed);
69 
70 Rlist *RlistCopy(const Rlist *list);
71 Rlist *RlistCopyRewriter(const Rlist *list, JsonElement *map);
72 unsigned int RlistHash        (const Rlist *list, unsigned seed);
73 unsigned int RlistHash_untyped(const void *list, unsigned seed);
74 void RlistDestroy        (Rlist *list);
75 void RlistDestroy_untyped(void *rl);
76 void RlistDestroyEntry(Rlist **liststart, Rlist *entry);
77 char *RlistScalarValue(const Rlist *rlist);
78 char *RlistScalarValueSafe(const Rlist *rlist);
79 FnCall *RlistFnCallValue(const Rlist *rlist);
80 Rlist *RlistRlistValue(const Rlist *rlist);
81 Rlist *RlistParseShown(const char *string);
82 Rlist *RlistParseString(const char *string);
83 Rlist *RlistKeyIn(Rlist *list, const char *key);
84 int RlistLen(const Rlist *start);
85 bool RlistMatchesRegexRlist(const Rlist *list, const Rlist *search);
86 bool RlistMatchesRegex(const Rlist *list, const char *str);
87 bool RlistIsInListOfRegex(const Rlist *list, const char *str);
88 bool RlistIsNullList(const Rlist *list);
89 bool RlistContainsString(const Rlist *list, const char *string);
90 
91 Rlist *RlistAppendRval(Rlist **start, Rval rval);
92 
93 Rlist *RlistPrependScalarIdemp(Rlist **start, const char *scalar);
94 Rlist *RlistAppendScalarIdemp(Rlist **start, const char *scalar);
95 Rlist *RlistAppendScalar(Rlist **start, const char *scalar);
96 
97 Rlist *RlistPrepend(Rlist **start, const void *item, RvalType type);
98 Rlist *RlistAppend(Rlist **start, const void *item, RvalType type);
99 Rlist *RlistAppendAllTypes(Rlist **start, const void *item, RvalType type, bool all_types);
100 Rlist *RlistAppendString(Rlist **start, const char *string);
101 
102 Rlist *RlistFromSplitString(const char *string, char sep);
103 Rlist *RlistFromStringSplitLines(const char *string, bool detect_crlf);
104 Rlist *RlistFromSplitRegex(const char *string, const char *regex, size_t max_entries, bool allow_blanks);
105 Rlist *RlistFromRegexSplitNoOverflow(const char *string, const char *regex, int max);
106 Rlist *RlistFromContainer(const JsonElement *container);
107 
108 void RlistWrite(Writer *writer, const Rlist *list);
109 Rlist *RlistLast(Rlist *start);
110 void RlistFilter(Rlist **list, bool (*KeepPredicate)(void *item, void *predicate_data), void *predicate_user_data, void (*DestroyItem)(void *item));
111 void RlistReverse(Rlist **list);
112 void ScalarWrite(Writer *w, const char *s, bool quote);
113 void RlistFlatten(EvalContext *ctx, Rlist **list);
114 bool RlistEqual        (const Rlist *list1, const Rlist *list2);
115 bool RlistEqual_untyped(const void *list1, const void *list2);
116 
117 
118 #endif
119