Skip to content
Browse files

Removed PCRE dependency when linking to libutils

cf-upgrade refused to link to libutils without -lcrypto and -lpcre. The
PCRE dependency has been removed in this patch, OpenSSL dependency is
still TODO though.
  • Loading branch information...
1 parent fa9a6ad commit 99a5f54c84d0e21ba598f7d2f5899c858f17f005 @jimis jimis committed Jul 18, 2014
View
4 cf-agent/match_scope.c
@@ -26,7 +26,9 @@
#include <matching.h>
#include <eval_context.h>
-#include <string_lib.h>
+#include <string_lib.h> /* StringFromLong */
+#include <regex.h> /* CompileRegex */
+
/* Sets variables */
static int RegExMatchSubString(EvalContext *ctx, pcre *rx, const char *teststring, int *start, int *end)
View
5 cf-serverd/server_access.h
@@ -7,8 +7,9 @@
#include <platform.h>
-#include <map.h> /* StringMap */
-#include "strlist.h" /* StrList */
+#include <map.h> /* StringMap */
+#include <regex.h> /* StringMatchFull */
+#include "strlist.h" /* StrList */
/**
View
1 cf-serverd/server_classic.c
@@ -8,6 +8,7 @@
#include <net.h> /* ReceiveTransaction,SendTransaction */
#include <signals.h>
#include <string_lib.h> /* ToLowerStrInplace */
+#include <regex.h> /* StringMatchFull */
#include <lastseen.h> /* LastSaw1 */
#include <files_hashes.h> /* HashString */
#include <crypto.h> /* HavePublicKey */
View
1 cf-serverd/server_common.c
@@ -30,6 +30,7 @@ static const int CF_NOSIZE = -1;
#include <item_lib.h> /* ItemList2CSV_bound */
#include <string_lib.h> /* ToLower */
+#include <regex.h> /* StringMatchFull */
#include <crypto.h> /* EncryptString */
#include <files_names.h>
#include <files_interfaces.h>
View
2 cf-serverd/server_tls.c
@@ -37,7 +37,7 @@
#include <tls_generic.h> /* TLSSend */
#include <cf-serverd-enterprise-stubs.h>
#include <connection_info.h>
-#include <string_lib.h> /* StringMatchFull */
+#include <regex.h> /* StringMatchFull */
#include <known_dirs.h>
#include <file_lib.h> /* IsDirReal */
View
1 libenv/sysinfo.c
@@ -37,6 +37,7 @@
#include <systype.h>
#include <unix.h>
#include <string_lib.h>
+#include <regex.h> /* StringMatchFull */
#include <misc_lib.h>
#include <file_lib.h>
#include <rlist.h>
View
1 libenv/unix_iface.c
@@ -30,6 +30,7 @@
#include <misc_lib.h>
#include <communication.h>
#include <string_lib.h>
+#include <regex.h> /* StringMatchFull */
#ifdef HAVE_SYS_JAIL_H
# include <sys/jail.h>
View
4 libpromises/class.c
@@ -2,9 +2,11 @@
#include <rb-tree.h>
#include <alloc.h>
-#include <string_lib.h>
+#include <string_lib.h> /* StringHash,StringConcatenate */
+#include <regex.h> /* CompileRegex,StringMatchFullWithPrecompiledRegex */
#include <files_names.h>
+
struct ClassTable_
{
RBTree *classes;
View
1 libpromises/evalfunction.c
@@ -44,6 +44,7 @@
#include <hashes.h>
#include <unix.h>
#include <string_lib.h>
+#include <regex.h> /* CompileRegex,StringMatchWithPrecompiledRegex */
#include <net.h> /* SocketConnect */
#include <communication.h>
#include <classic.h> /* SendSocketStream */
View
2 libpromises/item_lib.c
@@ -28,7 +28,7 @@
#include <addr_lib.h>
#include <matching.h>
#include <misc_lib.h>
-#include <string_lib.h>
+#include <regex.h> /* StringMatchFull,CompileRegex,StringMatchWithPrecompiledRegex */
#include <file_lib.h>
#include <files_interfaces.h>
View
2 libpromises/matching.c
@@ -32,7 +32,7 @@
#include <scope.h>
#include <misc_lib.h>
#include <rlist.h>
-#include <string_lib.h>
+#include <regex.h> /* CompileRegex,StringMatchFull */
/* Pure, non-thread-safe */
static char *FirstBackReference(pcre *rx, const char *teststring)
View
6 libpromises/processes_select.c
@@ -24,13 +24,15 @@
#include <processes_select.h>
+#include <string.h>
+
#include <eval_context.h>
#include <files_names.h>
#include <conversion.h>
#include <matching.h>
#include <systype.h>
-#include <string_lib.h>
-#include <string.h>
+#include <string_lib.h> /* Chop */
+#include <regex.h> /* CompileRegex,StringMatchWithPrecompiledRegex,StringMatchFull */
#include <item_lib.h>
#include <pipes.h>
#include <files_interfaces.h>
View
3 libpromises/rlist.c
@@ -30,7 +30,8 @@
#include <matching.h>
#include <scope.h>
#include <fncall.h>
-#include <string_lib.h>
+#include <string_lib.h> /* StringHash */
+#include <regex.h> /* StringMatchWithPrecompiledRegex,CompileRegex */
#include <mutex.h>
#include <misc_lib.h>
#include <assoc.h>
View
3 libpromises/syntax.c
@@ -33,7 +33,8 @@
#include <matching.h>
#include <scope.h>
#include <fncall.h>
-#include <string_lib.h>
+#include <string_lib.h> /* IsStrIn */
+#include <regex.h> /* StringMatchFull */
#include <misc_lib.h>
#include <rlist.h>
#include <vars.h>
View
3 libpromises/verify_classes.c
@@ -33,7 +33,8 @@
#include <promises.h>
#include <conversion.h>
#include <logic_expressions.h>
-#include <string_lib.h>
+#include <string_lib.h> /* StringHash */
+#include <regex.h> /* StringMatchFull */
static int EvalClassExpression(EvalContext *ctx, Constraint *cp, const Promise *pp);
View
2 libpromises/verify_vars.c
@@ -26,7 +26,7 @@
#include <actuator.h>
#include <attributes.h>
-#include <string_lib.h>
+#include <regex.h> /* CompileRegex,StringMatchFullWithPrecompiledRegex */
#include <buffer.h>
#include <misc_lib.h>
#include <fncall.h>
View
1 libutils/Makefile.am
@@ -44,6 +44,7 @@ libutils_la_SOURCES = \
hash.c hash.h \
queue.c queue.h \
ring_buffer.c ring_buffer.h \
+ regex.c regex.h \
printsize.h
if !NT
View
1 libutils/json.c
@@ -28,6 +28,7 @@
#include <string_lib.h>
#include <file_lib.h>
#include <printsize.h>
+#include <regex.h>
#include <json.h>
View
203 libutils/regex.c
@@ -0,0 +1,203 @@
+/*
+ Copyright (C) CFEngine AS
+
+ This file is part of CFEngine 3 - written and maintained by CFEngine AS.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 3.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ To the extent this program is licensed as part of the Enterprise
+ versions of CFEngine, the applicable Commercial Open Source License
+ (COSL) may apply to this file if you as a licensee so wish it. See
+ included file COSL.txt.
+*/
+
+
+#include <platform.h>
+#include <regex.h>
+
+#include <alloc.h>
+#include <logging.h>
+#include <string_lib.h>
+
+
+#define STRING_MATCH_OVECCOUNT 30
+#define NULL_OR_EMPTY(str) ((str == NULL) || (str[0] == '\0'))
+
+
+pcre *CompileRegex(const char *regex)
+{
+ const char *errorstr;
+ int erroffset;
+
+ pcre *rx = pcre_compile(regex, PCRE_MULTILINE | PCRE_DOTALL,
+ &errorstr, &erroffset, NULL);
+
+ if (!rx)
+ {
+ Log(LOG_LEVEL_ERR,
+ "Regular expression error: pcre_compile() '%s' in expression '%s' (offset: %d)",
+ errorstr, regex, erroffset);
+ }
+
+ return rx;
+}
+
+bool StringMatchWithPrecompiledRegex(pcre *regex, const char *str, int *start, int *end)
+{
+ assert(regex);
+ assert(str);
+
+ int ovector[STRING_MATCH_OVECCOUNT] = { 0 };
+ int result = pcre_exec(regex, NULL, str, strlen(str),
+ 0, 0, ovector, STRING_MATCH_OVECCOUNT);
+
+ if (result)
+ {
+ if (start)
+ {
+ *start = ovector[0];
+ }
+ if (end)
+ {
+ *end = ovector[1];
+ }
+ }
+ else
+ {
+ if (start)
+ {
+ *start = 0;
+ }
+ if (end)
+ {
+ *end = 0;
+ }
+ }
+
+ return result >= 0;
+}
+
+bool StringMatch(const char *regex, const char *str, int *start, int *end)
+{
+ pcre *pattern = CompileRegex(regex);
+
+ if (pattern == NULL)
+ {
+ return false;
+ }
+
+ bool ret = StringMatchWithPrecompiledRegex(pattern, str, start, end);
+
+ pcre_free(pattern);
+ return ret;
+
+}
+
+bool StringMatchFull(const char *regex, const char *str)
+{
+ pcre *pattern = CompileRegex(regex);
+
+ if (pattern == NULL)
+ {
+ return false;
+ }
+
+ bool ret = StringMatchFullWithPrecompiledRegex(pattern, str);
+
+ pcre_free(pattern);
+ return ret;
+}
+
+bool StringMatchFullWithPrecompiledRegex(pcre *pattern, const char *str)
+{
+ int start = 0, end = 0;
+
+ if (StringMatchWithPrecompiledRegex(pattern, str, &start, &end))
+ {
+ return (start == 0) && (end == strlen(str));
+ }
+ else
+ {
+ return false;
+ }
+}
+
+Seq *StringMatchCaptures(const char *regex, const char *str)
+{
+ assert(regex);
+ assert(str);
+
+ pcre *pattern = NULL;
+ {
+ const char *errorstr;
+ int erroffset;
+ pattern = pcre_compile(regex, PCRE_MULTILINE | PCRE_DOTALL,
+ &errorstr, &erroffset, NULL);
+ }
+ assert(pattern);
+
+ if (pattern == NULL)
+ {
+ return NULL;
+ }
+
+ int captures;
+ int res = pcre_fullinfo(pattern, NULL, PCRE_INFO_CAPTURECOUNT, &captures);
+ if (res != 0)
+ {
+ pcre_free(pattern);
+ return NULL;
+ }
+
+ int *ovector = xmalloc(sizeof(int) * (captures + 1) * 3);
+
+ int result = pcre_exec(pattern, NULL, str, strlen(str),
+ 0, 0, ovector, (captures + 1) * 3);
+
+ if (result <= 0)
+ {
+ free(ovector);
+ pcre_free(pattern);
+ return NULL;
+ }
+
+ Seq *ret = SeqNew(captures + 1, free);
+ for (int i = 0; i <= captures; ++i)
+ {
+ SeqAppend(ret, xstrndup(str + ovector[2*i],
+ ovector[2*i + 1] - ovector[2 * i]));
+ }
+ free(ovector);
+ pcre_free(pattern);
+ return ret;
+}
+
+bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex)
+{
+ if (regex)
+ {
+ if (!NULL_OR_EMPTY(compareTo) && !StringMatchFull(compareTo, value))
+ {
+ return false;
+ }
+ }
+ else
+ {
+ if (!NULL_OR_EMPTY(compareTo) && strcmp(compareTo, value) != 0)
+ {
+ return false;
+ }
+ }
+ return true;
+}
View
48 libutils/regex.h
@@ -0,0 +1,48 @@
+/*
+ Copyright (C) CFEngine AS
+
+ This file is part of CFEngine 3 - written and maintained by CFEngine AS.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 3.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+
+ To the extent this program is licensed as part of the Enterprise
+ versions of CFEngine, the applicable Commercial Open Source License
+ (COSL) may apply to this file if you as a licensee so wish it. See
+ included file COSL.txt.
+*/
+
+
+#ifndef CFENGINE_REGEX_H
+#define CFENGINE_REGEX_H
+
+
+#include <platform.h>
+
+#include <pcre_include.h>
+
+#include <sequence.h> /* Seq */
+
+
+/* Try to use CompileRegex() and StringMatchWithPrecompiledRegex(). */
+pcre *CompileRegex(const char *regex);
+bool StringMatch(const char *regex, const char *str, int *start, int *end);
+bool StringMatchWithPrecompiledRegex(pcre *regex, const char *str,
+ int *start, int *end);
+bool StringMatchFull(const char *regex, const char *str);
+bool StringMatchFullWithPrecompiledRegex(pcre *regex, const char *str);
+Seq *StringMatchCaptures(const char *regex, const char *str);
+bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex);
+
+
+#endif /* CFENGINE_REGEX_H */
View
167 libutils/string_lib.c
@@ -78,9 +78,6 @@ unsigned int StringHash(const char *str, unsigned int seed, unsigned int max)
}
-#define STRING_MATCH_OVECCOUNT 30
-#define NULL_OR_EMPTY(str) ((str == NULL) || (str[0] == '\0'))
-
char ToLower(char ch)
{
if (isupper((int) ch))
@@ -399,148 +396,6 @@ char *NULLStringToEmpty(char *str)
return str;
}
-pcre *CompileRegex(const char *regex)
-{
- const char *errorstr;
- int erroffset;
-
- pcre *rx = pcre_compile(regex, PCRE_MULTILINE | PCRE_DOTALL, &errorstr, &erroffset, NULL);
-
- if (!rx)
- {
- Log(LOG_LEVEL_ERR, "Regular expression error: pcre_compile() '%s' in expression '%s' (offset: %d)",
- errorstr, regex, erroffset);
- }
-
- return rx;
-}
-
-bool StringMatchWithPrecompiledRegex(pcre *regex, const char *str, int *start, int *end)
-{
- assert(regex);
- assert(str);
-
- int ovector[STRING_MATCH_OVECCOUNT] = { 0 };
- int result = pcre_exec(regex, NULL, str, strlen(str), 0, 0, ovector, STRING_MATCH_OVECCOUNT);
-
- if (result)
- {
- if (start)
- {
- *start = ovector[0];
- }
- if (end)
- {
- *end = ovector[1];
- }
- }
- else
- {
- if (start)
- {
- *start = 0;
- }
- if (end)
- {
- *end = 0;
- }
- }
-
- return result >= 0;
-}
-
-bool StringMatch(const char *regex, const char *str, int *start, int *end)
-{
- pcre *pattern = CompileRegex(regex);
-
- if (pattern == NULL)
- {
- return false;
- }
-
- bool ret = StringMatchWithPrecompiledRegex(pattern, str, start, end);
-
- pcre_free(pattern);
- return ret;
-
-}
-
-bool StringMatchFull(const char *regex, const char *str)
-{
- pcre *pattern = CompileRegex(regex);
-
- if (pattern == NULL)
- {
- return false;
- }
-
- bool ret = StringMatchFullWithPrecompiledRegex(pattern, str);
-
- pcre_free(pattern);
- return ret;
-}
-
-bool StringMatchFullWithPrecompiledRegex(pcre *pattern, const char *str)
-{
- int start = 0, end = 0;
-
- if (StringMatchWithPrecompiledRegex(pattern, str, &start, &end))
- {
- return (start == 0) && (end == strlen(str));
- }
- else
- {
- return false;
- }
-}
-
-Seq *StringMatchCaptures(const char *regex, const char *str)
-{
- assert(regex);
- assert(str);
-
- pcre *pattern = NULL;
- {
- const char *errorstr;
- int erroffset;
- pattern = pcre_compile(regex, PCRE_MULTILINE | PCRE_DOTALL, &errorstr, &erroffset, NULL);
- }
- assert(pattern);
-
- if (pattern == NULL)
- {
- return NULL;
- }
-
- int captures;
- int res = pcre_fullinfo(pattern, NULL, PCRE_INFO_CAPTURECOUNT, &captures);
- if (res != 0)
- {
- pcre_free(pattern);
- return NULL;
- }
-
- int *ovector = xmalloc(sizeof(int) * (captures + 1) * 3);
-
- int result = pcre_exec(pattern, NULL, str, strlen(str), 0, 0, ovector, (captures + 1) * 3);
-
- if (result <= 0)
- {
- free(ovector);
- pcre_free(pattern);
- return NULL;
- }
-
- Seq *ret = SeqNew(captures + 1, free);
- for (int i = 0; i <= captures; ++i)
- {
- SeqAppend(ret, xstrndup(str + ovector[2*i], ovector[2*i + 1] - ovector[2 * i]));
- }
- free(ovector);
- pcre_free(pattern);
- return ret;
-}
-
char *StringEncodeBase64(const char *str, size_t len)
{
assert(str);
@@ -1054,25 +909,7 @@ void *MemSpanInverse(const void *mem, char c, size_t n)
return (char *)mem;
}
-bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex)
-{
- if (regex)
- {
- if (!NULL_OR_EMPTY(compareTo) && !StringMatchFull(compareTo, value))
- {
- return false;
- }
- }
- else
- {
- if (!NULL_OR_EMPTY(compareTo) && strcmp(compareTo, value) != 0)
- {
- return false;
- }
- }
- return true;
-}
-/*
+/*
* @brief extract info from input string given two types of constraints:
* - length of the extracted string is bounded
* - extracted string should stop at first element of an exclude list
@@ -1083,7 +920,7 @@ bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex)
* @param[out] obuf : the output buffer
* @retval true if string was capped, false if not
*/
-bool StringNotMatchingSetCapped(const char *isp, int limit,
+bool StringNotMatchingSetCapped(const char *isp, int limit,
const char *exclude, char *obuf)
{
size_t l = strcspn(isp, exclude);
View
11 libutils/string_lib.h
@@ -69,14 +69,6 @@ char *StringSubstring(const char *source, size_t source_len, int start, int len)
/* Allocates the result */
char *SearchAndReplace(const char *source, const char *search, const char *replace);
-/* Instead of using below in a loop use CompileRegex() and StringMatchWithPrecompiledRegex(). */
-pcre *CompileRegex(const char *regex);
-bool StringMatch(const char *regex, const char *str, int *start, int *end);
-bool StringMatchWithPrecompiledRegex(pcre *regex, const char *str, int *start, int *end);
-bool StringMatchFull(const char *regex, const char *str);
-bool StringMatchFullWithPrecompiledRegex(pcre *regex, const char *str);
-Seq *StringMatchCaptures(const char *regex, const char *str);
-
bool ReplaceStr(const char *in, char *out, int outSz, const char *from, const char *to);
bool IsStrIn(const char *str, const char *const strs[]);
@@ -159,8 +151,7 @@ void *MemSpan(const void *mem, char c, size_t n);
*/
void *MemSpanInverse(const void *mem, char c, size_t n);
-bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex);
-bool StringNotMatchingSetCapped(const char *isp, int limit,
+bool StringNotMatchingSetCapped(const char *isp, int limit,
const char *exclude, char *obuf);
/**
View
4 tests/unit/Makefile.am
@@ -38,7 +38,9 @@ libtest_la_SOURCES = cmockery.c cmockery.h schema.h test.c test.h \
libtest_la_LIBADD = ../../libcompat/libcompat.la
check_LTLIBRARIES += libstr.la
-libstr_la_SOURCES = ../../libutils/string_lib.c ../../libutils/writer.c ../../libutils/sequence.c
+libstr_la_SOURCES = ../../libutils/string_lib.c ../../libutils/regex.c \
+ ../../libutils/encode.c ../../libutils/writer.c \
+ ../../libutils/sequence.c
libstr_la_LIBADD = libtest.la
View
2 tests/unit/str_test.c
@@ -1,5 +1,7 @@
#include <cf3.defs.h>
#include <string_lib.h>
+#include <regex.h>
+#include <encode.h>
#include <conversion.h>
View
2 tests/unit/syntax_test.c
@@ -1,7 +1,7 @@
#include <test.h>
#include <syntax.h>
-#include <string_lib.h>
+#include <regex.h> /* StringMatchFull */
static void test_lookup_promise_type_agent_vars(void)
{

0 comments on commit 99a5f54

Please sign in to comment.
Something went wrong with that request. Please try again.