forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.c
154 lines (126 loc) · 3.39 KB
/
line.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @file
*
* @brief A plugin that reads configuration files and saves keys on a line by line basis *
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include "line.h"
#include <kdbease.h>
#include <kdberrors.h>
#include <errno.h>
#include <stddef.h>
// The definition `_WITH_GETLINE` is required for FreeBSD
#define _WITH_GETLINE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline KeySet * elektraLineContract (void)
{
return ksNew (30, keyNew ("system:/elektra/modules/line", KEY_VALUE, "line plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/line/exports", KEY_END),
keyNew ("system:/elektra/modules/line/exports/get", KEY_FUNC, elektraLineGet, KEY_END),
keyNew ("system:/elektra/modules/line/exports/set", KEY_FUNC, elektraLineSet, KEY_END),
#include "readme_line.c"
keyNew ("system:/elektra/modules/line/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
}
int elektraLineRead (FILE * fp, KeySet * returned)
{
char * value = NULL;
size_t len = 0;
ssize_t n = 0;
Key * read = NULL;
// Read in each line
while ((n = getline (&value, &len, fp)) != -1)
{
// Remove trailing newline
if (value[n - 1] == '\n')
{
value[n - 1] = '\0';
}
read = keyDup (ksTail (returned), KEY_CP_ALL);
if (elektraArrayIncName (read) == -1)
{
elektraFree (value);
keyDel (read);
return -1;
}
keySetString (read, value);
ksAppendKey (returned, read);
}
elektraFree (value);
return 1;
}
int elektraLineGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
/* get all keys */
if (!strcmp (keyName (parentKey), "system:/elektra/modules/line"))
{
KeySet * moduleConfig = elektraLineContract ();
ksAppend (returned, moduleConfig);
ksDel (moduleConfig);
return 1;
}
int errnosave = errno;
FILE * fp = fopen (keyString (parentKey), "r");
if (!fp)
{
ELEKTRA_SET_ERROR_GET (parentKey);
errno = errnosave;
return -1;
}
Key * b = keyNew (keyName (parentKey), KEY_END);
ksAppendKey (returned, keyDup (b, KEY_CP_ALL)); // start with parentKey
keyAddName (b, "#"); // start point for our array
ksAppendKey (returned, b);
int ret = elektraLineRead (fp, returned);
// get rid of startpoint, if it was an empty file
keyDel (ksLookup (returned, b, KDB_O_POP));
if (ret == -1)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Could not increment array from %s", keyName (ksTail (returned)));
ret = -1;
}
else if (feof (fp) == 0)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, "Invalid line encountered: not at the end of file");
ret = -1;
}
fclose (fp);
return ret; /* success */
}
int elektraLineSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
/* set all keys */
int errnosave = errno;
FILE * fp = fopen (keyString (parentKey), "w");
if (!fp)
{
ELEKTRA_SET_ERROR_SET (parentKey);
errno = errnosave;
return -1;
}
Key * cur;
if (!ksLookup (returned, parentKey, 0))
{
// ignore parentKey if found
ksRewind (returned);
}
while ((cur = ksNext (returned)) != 0)
{
fprintf (fp, "%s\n", keyString (cur));
}
fclose (fp);
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("line",
ELEKTRA_PLUGIN_GET, &elektraLineGet,
ELEKTRA_PLUGIN_SET, &elektraLineSet,
ELEKTRA_PLUGIN_END);
}