summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2015-12-15 23:09:12 +0200
committerTobias Klauser <tklauser@distanz.ch>2015-12-17 10:02:35 +0100
commit31cab05faab92a2f0062b4833547083621d85678 (patch)
treed68c51ecf69f457654ad49ee805833407125d166
parent52726714c29c3442d3675da4fe888d565988d5db (diff)
cpp: Allow to pass additional cpp options
Extend cpp_exec function with args parameter to specify additional cpp options (like -D). Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--bpf_parser.y2
-rw-r--r--cpp.c39
-rw-r--r--cpp.h2
-rw-r--r--trafgen_parser.y2
4 files changed, 33 insertions, 12 deletions
diff --git a/bpf_parser.y b/bpf_parser.y
index 003c031..7331cc5 100644
--- a/bpf_parser.y
+++ b/bpf_parser.y
@@ -745,7 +745,7 @@ int compile_filter(char *file, int verbose, int bypass, int format,
memset(tmp_file, 0, sizeof(tmp_file));
if (invoke_cpp) {
- ret = cpp_exec(file, tmp_file, sizeof(tmp_file));
+ ret = cpp_exec(file, tmp_file, sizeof(tmp_file), NULL);
if (ret) {
fprintf(stderr, "Failed to invoke C preprocessor!\n");
goto exit;
diff --git a/cpp.c b/cpp.c
index d4ec2a6..8e03209 100644
--- a/cpp.c
+++ b/cpp.c
@@ -5,25 +5,46 @@
#include "proc.h"
#include "xmalloc.h"
-int cpp_exec(char *in_file, char *out_file, size_t out_len)
+static size_t argv_len(char **argv)
{
+ size_t len = 0;
+
+ for (; argv && *argv; argv++)
+ len++;
+
+ return len;
+}
+
+int cpp_exec(char *in_file, char *out_file, size_t out_len, char **argv)
+{
+ size_t argc = 7 + argv_len(argv);
char *tmp = xstrdup(in_file);
- char *argv[7] = {
- "cpp",
- "-I", ETCDIRE_STRING,
- "-o", out_file,
- in_file,
- NULL,
- };
+ char **cpp_argv;
int ret = 0;
char *base;
+ unsigned int i = 0;
base = basename(tmp);
slprintf(out_file, out_len, "/tmp/.tmp-%u-%s", rand(), base);
- if (proc_exec("cpp", argv))
+ cpp_argv = xmalloc(argc * sizeof(char *));
+
+ cpp_argv[i++] = "cpp";
+
+ for (; argv && *argv; argv++, i++)
+ cpp_argv[i] = *argv;
+
+ cpp_argv[i++] = "-I";
+ cpp_argv[i++] = ETCDIRE_STRING;
+ cpp_argv[i++] = "-o";
+ cpp_argv[i++] = out_file;
+ cpp_argv[i++] = in_file;
+ cpp_argv[i++] = NULL;
+
+ if (proc_exec("cpp", cpp_argv))
ret = -1;
+ xfree(cpp_argv);
xfree(tmp);
return ret;
}
diff --git a/cpp.h b/cpp.h
index a3e295d..45c42ac 100644
--- a/cpp.h
+++ b/cpp.h
@@ -1,6 +1,6 @@
#ifndef CPP_H
#define CPP_H
-extern int cpp_exec(char *in_file, char *out_file, size_t out_len);
+extern int cpp_exec(char *in_file, char *out_file, size_t out_len, char **argv);
#endif
diff --git a/trafgen_parser.y b/trafgen_parser.y
index 44751ab..8a0f3b7 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -599,7 +599,7 @@ void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp
our_cpu = cpu;
if (invoke_cpp) {
- if (cpp_exec(file, tmp_file, sizeof(tmp_file))) {
+ if (cpp_exec(file, tmp_file, sizeof(tmp_file), NULL)) {
fprintf(stderr, "Failed to invoke C preprocessor!\n");
goto err;
}