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_ITEM_LIB_H
26 #define CFENGINE_ITEM_LIB_H
27 
28 #include <cf3.defs.h>
29 #include <writer.h>
30 #include <file_lib.h>
31 
32 struct Item_
33 {
34     char *name;
35     char *classes;
36     int counter;
37     time_t time;
38     Item *next;
39 };
40 
41 typedef enum
42 {
43     ITEM_MATCH_TYPE_LITERAL_START,
44     ITEM_MATCH_TYPE_LITERAL_COMPLETE,
45     ITEM_MATCH_TYPE_LITERAL_SOMEWHERE,
46     ITEM_MATCH_TYPE_REGEX_COMPLETE,
47     ITEM_MATCH_TYPE_LITERAL_START_NOT,
48     ITEM_MATCH_TYPE_LITERAL_COMPLETE_NOT,
49     ITEM_MATCH_TYPE_LITERAL_SOMEWHERE_NOT,
50     ITEM_MATCH_TYPE_REGEX_COMPLETE_NOT
51 } ItemMatchType;
52 
53 void PrintItemList(const Item *list, Writer *w);
54 void PrependFullItem(Item **liststart, const char *itemstring, const char *classes, int counter, time_t t);
55 Item *ReturnItemIn(Item *list, const char *item);
56 Item *ReturnItemInClass(Item *list, const char *item, const char *classes);
57 Item *ReturnItemAtIndex(Item *list, int index);
58 Item *EndOfList(Item *start);
59 void PrependItemList(Item **liststart, const char *itemstring);
60 void InsertAfter(Item **filestart, Item *ptr, const char *string);
61 bool RawSaveItemList(const Item *liststart, const char *filename, NewLineMode new_line_mode);
62 Item *RawLoadItemList(const char *filename);
63 Item *SplitStringAsItemList(const char *string, char sep);
64 Item *SplitString(const char *string, char sep);
65 bool DeleteItemGeneral(Item **filestart, const char *string, ItemMatchType type);
66 bool DeleteItemLiteral(Item **filestart, const char *string);
67 bool DeleteItemStarting(Item **list, const char *string);
68 bool DeleteItemNotStarting(Item **list, const char *string);
69 bool DeleteItemMatching(Item **list, const char *string);
70 bool DeleteItemNotMatching(Item **list, const char *string);
71 bool DeleteItemContaining(Item **list, const char *string);
72 bool DeleteItemNotContaining(Item **list, const char *string);
73 size_t ListLen(const Item *list);
74 bool IsItemIn(const Item *list, const char *item);
75 bool ListsCompare(const Item *list1, const Item *list2);
76 bool ListSubsetOfList(const Item *list1, const Item *list2);
77 bool IsMatchItemIn(const Item *list, const char *item);
78 Item *ConcatLists(Item *list1, Item *list2);
79 void CopyList(Item **dest, const Item *source);
80 void IdempItemCount(Item **liststart, const char *itemstring, const char *classes);
81 Item *IdempPrependItem(Item **liststart, const char *itemstring, const char *classes);
82 Item *IdempPrependItemClass(Item **liststart, const char *itemstring, const char *classes);
83 Item *ReverseItemList(Item *list); /* Eats list, spits it out reversed. */
84 Item *PrependItem(Item **liststart, const char *itemstring, const char *classes);
85 /* Warning: AppendItem()'s cost is proportional to list length; it is
86  * usually cheaper to build a list using PrependItem, then reverse it;
87  * building it with AppendItem() is quadratic in length. */
88 void AppendItem(Item **liststart, const char *itemstring, const char *classes);
89 void DeleteItemList(Item *item);
90 void DeleteItem(Item **liststart, Item *item);
91 void IncrementItemListCounter(Item *ptr, const char *string);
92 void SetItemListCounter(Item *ptr, const char *string, int value);
93 char *ItemList2CSV(const Item *list);
94 size_t ItemList2CSV_bound(const Item *list, char *buf, size_t buf_size, char separator);
95 size_t ItemListSize(const Item *list);
96 bool IsInterfaceAddress(const Item *ip_addresses, const char *adr);
97 
98 #endif
99