forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineendings.c
179 lines (167 loc) · 3.98 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
/**
* @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
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;
}
static int checkLineEndings (const char * fileName, Lineending validLineEnding, Key * parentKey)
{
FILE * fp;
fp = fopen (fileName, "rb");
if (fp == NULL)
{
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);
ELEKTRA_SET_ERRORF (114, parentKey, "Invalid line ending at line %lu", line);
return -2;
}
++line;
}
else if (lineEnding != found && found != NA)
{
fclose (fp);
ELEKTRA_SET_ERRORF (115, parentKey, "inconsistent line endings at line %lu", line);
return -3;
}
fc = sc;
found = NA;
}
fclose (fp);
return 0;
}
int elektraLineendingsGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
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 1; /* success */
}
/* get all keys */
KeySet * config = elektraPluginGetConfig (handle);
Key * valid = ksLookupByName (config, "/valid", 0);
Lineending validLineEnding = strToLE (keyString (valid));
int ret;
ret = checkLineEndings (keyString (parentKey), validLineEnding, parentKey);
if (ret == (-3))
{
return -1;
}
else
return 1;
}
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;
ret = checkLineEndings (keyString (parentKey), validLineEnding, parentKey);
switch (ret)
{
case (-1):
ELEKTRA_SET_ERRORF (113, parentKey, "Couldn't open file %s\n", keyString (parentKey));
return 1;
break;
case (-2):
return -1;
break;
case (-3):
return -1;
break;
case 0:
default:
return 1;
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);
}