forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineendings.c
189 lines (172 loc) · 5.32 KB
/
lineendings.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @file
*
* @brief Source for lineendings plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include "lineendings.h"
#include <kdberrors.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define LF_BYTE 0x0A
#define CR_BYTE 0x0D
#define ERROR(asWarning, warning, error, key, reason, va) \
if (asWarning) \
warning (key, reason, va); \
else \
error (key, reason, va);
typedef enum
{
NA,
CR,
LF,
CRLF,
LFCR,
NUM_TYPES
} Lineending;
static inline char * LEString (Lineending index)
{
static char * strings[] = { "NA", "CR", "LF", "CRLF", "LFCR" };
if (index > NUM_TYPES) return NULL;
return strings[index];
}
static Lineending strToLE (const char * str)
{
uint8_t counter = 0;
for (; counter < NUM_TYPES; ++counter)
{
if (!strcmp (LEString (counter), str)) return counter;
}
return NA;
}
/**
* Check the line endings for inconsistencies and invalid values
* @param fileName[in] The absolute path of the file to check
* @param validLineEnding[in] The line ending that should be considered as valid
* @param errorsAsWarnings[in] Produce warnings instead of errors
* @retval 0 if everything was ok, -1 if file not found, -2 if invalid line ending detected, -3 if inconsistent line ending detected
*/
static int checkLineEndings (const char * fileName, Lineending validLineEnding, Key * parentKey, bool errorsAsWarnings)
{
FILE * fp;
fp = fopen (fileName, "rb");
if (fp == NULL)
{
ERROR (errorsAsWarnings, ELEKTRA_ADD_RESOURCE_WARNINGF, ELEKTRA_SET_RESOURCE_ERRORF, parentKey, "Couldn't open file %s\n",
keyString (parentKey))
return -1;
}
Lineending lineEnding = NA;
Lineending found = NA;
uint8_t fc, sc;
unsigned long line = 1;
fc = sc = 0;
(void) fread (&fc, 1, 1, fp);
while (!feof (fp))
{
(void) fread (&sc, 1, 1, fp);
switch (fc)
{
case LF_BYTE:
if (sc == CR_BYTE)
found = LFCR;
else if (sc == LF_BYTE)
found = LF;
else if (sc)
found = LF;
break;
case CR_BYTE:
if (sc == LF_BYTE)
found = CRLF;
else if (sc == CR_BYTE)
found = CR;
else if (sc)
found = CR;
break;
}
if (found == CRLF || found == LFCR)
{
(void) fread (&sc, 1, 1, fp);
}
if (lineEnding == NA && found != NA)
{
lineEnding = found;
if (validLineEnding != NA && lineEnding != validLineEnding)
{
fclose (fp);
ERROR (errorsAsWarnings, ELEKTRA_ADD_VALIDATION_SYNTACTIC_WARNINGF, ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF,
parentKey, "Invalid line ending at line %lu", line)
return -2;
}
}
else if (lineEnding != found && found != NA)
{
fclose (fp);
ERROR (errorsAsWarnings, ELEKTRA_ADD_VALIDATION_SYNTACTIC_WARNINGF, ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF,
parentKey, "Inconsistent line endings at line %lu", line)
return -3;
}
if (found != NA) line++;
fc = sc;
found = NA;
}
fclose (fp);
return 0;
}
int elektraLineendingsGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/lineendings"))
{
KeySet * contract = ksNew (
30, keyNew ("system:/elektra/modules/lineendings", KEY_VALUE, "lineendings plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/lineendings/exports", KEY_END),
keyNew ("system:/elektra/modules/lineendings/exports/get", KEY_FUNC, elektraLineendingsGet, KEY_END),
keyNew ("system:/elektra/modules/lineendings/exports/set", KEY_FUNC, elektraLineendingsSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/lineendings/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS; /* success */
}
/* get all keys */
KeySet * config = elektraPluginGetConfig (handle);
Key * valid = ksLookupByName (config, "/valid", 0);
Lineending validLineEnding = strToLE (keyString (valid));
(void) checkLineEndings (keyString (parentKey), validLineEnding, parentKey, true);
/* Always return ELEKTRA_PLUGIN_STATUS_SUCCESS. We don't want kdbGet() to fail because of validation problems. */
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int elektraLineendingsSet (Plugin * handle, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
KeySet * config = elektraPluginGetConfig (handle);
Key * valid = ksLookupByName (config, "/valid", 0);
Lineending validLineEnding = strToLE (keyString (valid));
int ret = checkLineEndings (keyString (parentKey), validLineEnding, parentKey, false);
switch (ret)
{
case (-1):
case (-2):
case (-3):
return ELEKTRA_PLUGIN_STATUS_ERROR;
break;
case 0:
default:
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
break;
}
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("lineendings",
ELEKTRA_PLUGIN_GET, &elektraLineendingsGet,
ELEKTRA_PLUGIN_SET, &elektraLineendingsSet,
// ELEKTRA_PLUGIN_ERROR, &elektraLineendingsError,
ELEKTRA_PLUGIN_END);
}