aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-10-10 10:52:12 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-11-24 17:44:57 +0100
commitf2580ca3a5ec951216fe7cb2c092b13e8ffbdbe4 (patch)
tree1489a1e67a7183e082a2c6487ed66fa3223d23cc
parent67f9fa1121b6f9721143f85c1f7b8aa9a579e470 (diff)
afm-udb: remove comments and join lines of unit files
Change-Id: I9d901e074925a666190d5e3007a66aea81ef6253 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afm-udb.c61
1 files changed, 60 insertions, 1 deletions
diff --git a/src/afm-udb.c b/src/afm-udb.c
index e199ae1..e3d5d77 100644
--- a/src/afm-udb.c
+++ b/src/afm-udb.c
@@ -264,6 +264,65 @@ error:
}
/*
+ * read a unit file
+ */
+static int read_unit_file(const char *path, char **content, size_t *length)
+{
+ int rc, st;
+ char c, *read, *write;
+
+ /* read the file */
+ rc = getfile(path, content, length);
+ if (rc >= 0) {
+ /* removes any comment and join lines */
+ st = 0;
+ read = write = *content;
+ for (;;) {
+ do { c = *read++; } while (c == '\r');
+ if (!c)
+ break;
+ switch (st) {
+ case 0:
+ if (c == ';' || c == '#') {
+ st = 3; /* removes lines starting with ; or # */
+ break;
+ }
+ if (c == '\n')
+ break; /* removes empty lines */
+enter_state_1:
+ st = 1;
+ /*@fallthrough@*/
+ case 1:
+ if (c == '\\')
+ st = 2;
+ else {
+ *write++ = c;
+ if (c == '\n')
+ st = 0;
+ }
+ break;
+ case 2:
+ if (c == '\n')
+ c = ' ';
+ else
+ *write++ = '\\';
+ goto enter_state_1;
+ case 3:
+ if (c == '\n')
+ st = 0;
+ break;
+ }
+ }
+ if (st == 1)
+ *write++ = '\n';
+ *write = 0;
+ *length = (size_t)(write - *content);
+ *content = realloc(*content, *length + 1);
+ }
+ return rc;
+}
+
+/*
* called for each unit
*/
static int update_cb(void *closure, const char *name, const char *path, int isuser)
@@ -284,7 +343,7 @@ static int update_cb(void *closure, const char *name, const char *path, int isus
return 0;
/* reads the file */
- rc = getfile(path, &content, &length);
+ rc = read_unit_file(path, &content, &length);
if (rc < 0)
return rc;