]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add getline_wrapped() to parse config files
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 29 Nov 2011 20:05:43 +0000 (18:05 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 29 Nov 2011 20:05:43 +0000 (18:05 -0200)
Basically copied from module-init-tools

Makefile.am
libkmod/libkmod-private.h
libkmod/libkmod-util.c [new file with mode: 0644]

index 4a0f09bdf0db2991a4a2734b92a1de29baa472aa..9da04547916c2de43cd842ace6382096c4a80ad9 100644 (file)
@@ -33,6 +33,7 @@ libkmod_libkmod_la_SOURCES =\
        libkmod/libkmod.c \
        libkmod/libkmod-list.c \
        libkmod/libkmod-loaded.c \
+       libkmod/libkmod-util.c \
        libkmod/libkmod-module.c
 
 EXTRA_DIST += libkmod/libkmod.sym
index 20da56106488ab6ea9c69d7922931ed96581d73f..b22896dc845b2b07bfbf240c9fd25dcd213ca67e 100644 (file)
@@ -2,6 +2,7 @@
 #define _LIBKMOD_PRIVATE_H_
 
 #include <stdbool.h>
+#include <stdio.h>
 #include <syslog.h>
 
 #include "macro.h"
@@ -53,4 +54,6 @@ struct kmod_list *kmod_list_remove_data(struct kmod_list *list,
 
 const char *kmod_get_dirname(struct kmod_ctx *ctx) __attribute__((nonnull(1)));
 
+char *getline_wrapped(FILE *fp, unsigned int *linenum);
+
 #endif
diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c
new file mode 100644 (file)
index 0000000..a1f5fa1
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * libkmod - interface to kernel module operations
+ *
+ * Copyright (C) 2011  ProFUSION embedded systems
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "libkmod.h"
+#include "libkmod-private.h"
+
+/*
+ * Read one logical line from a configuration file.
+ *
+ * Line endings may be escaped with backslashes, to form one logical line from
+ * several physical lines.  No end of line character(s) are included in the
+ * result.
+ *
+ * If linenum is not NULL, it is incremented by the number of physical lines
+ * which have been read.
+ */
+char *getline_wrapped(FILE *fp, unsigned int *linenum)
+{
+       int size = 256;
+       int i = 0;
+       char *buf = malloc(size);
+       for(;;) {
+               int ch = getc_unlocked(fp);
+
+               switch(ch) {
+               case EOF:
+                       if (i == 0) {
+                               free(buf);
+                               return NULL;
+                       }
+                       /* else fall through */
+
+               case '\n':
+                       if (linenum)
+                               (*linenum)++;
+                       if (i == size)
+                               buf = realloc(buf, size + 1);
+                       buf[i] = '\0';
+                       return buf;
+
+               case '\\':
+                       ch = getc_unlocked(fp);
+
+                       if (ch == '\n') {
+                               if (linenum)
+                                       (*linenum)++;
+                               continue;
+                       }
+                       /* else fall through */
+
+               default:
+                       buf[i++] = ch;
+
+                       if (i == size) {
+                               size *= 2;
+                               buf = realloc(buf, size);
+                       }
+               }
+       }
+}