Skip to content

Commit

Permalink
Removed PCRE dependency when linking to libutils
Browse files Browse the repository at this point in the history
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
jimis committed Jul 18, 2014
1 parent fa9a6ad commit 99a5f54
Show file tree
Hide file tree
Showing 25 changed files with 290 additions and 190 deletions.
4 changes: 3 additions & 1 deletion cf-agent/match_scope.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@


#include <matching.h> #include <matching.h>
#include <eval_context.h> #include <eval_context.h>
#include <string_lib.h> #include <string_lib.h> /* StringFromLong */
#include <regex.h> /* CompileRegex */



/* Sets variables */ /* Sets variables */
static int RegExMatchSubString(EvalContext *ctx, pcre *rx, const char *teststring, int *start, int *end) static int RegExMatchSubString(EvalContext *ctx, pcre *rx, const char *teststring, int *start, int *end)
Expand Down
5 changes: 3 additions & 2 deletions cf-serverd/server_access.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@


#include <platform.h> #include <platform.h>


#include <map.h> /* StringMap */ #include <map.h> /* StringMap */
#include "strlist.h" /* StrList */ #include <regex.h> /* StringMatchFull */
#include "strlist.h" /* StrList */




/** /**
Expand Down
1 change: 1 addition & 0 deletions cf-serverd/server_classic.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <net.h> /* ReceiveTransaction,SendTransaction */ #include <net.h> /* ReceiveTransaction,SendTransaction */
#include <signals.h> #include <signals.h>
#include <string_lib.h> /* ToLowerStrInplace */ #include <string_lib.h> /* ToLowerStrInplace */
#include <regex.h> /* StringMatchFull */
#include <lastseen.h> /* LastSaw1 */ #include <lastseen.h> /* LastSaw1 */
#include <files_hashes.h> /* HashString */ #include <files_hashes.h> /* HashString */
#include <crypto.h> /* HavePublicKey */ #include <crypto.h> /* HavePublicKey */
Expand Down
1 change: 1 addition & 0 deletions cf-serverd/server_common.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static const int CF_NOSIZE = -1;


#include <item_lib.h> /* ItemList2CSV_bound */ #include <item_lib.h> /* ItemList2CSV_bound */
#include <string_lib.h> /* ToLower */ #include <string_lib.h> /* ToLower */
#include <regex.h> /* StringMatchFull */
#include <crypto.h> /* EncryptString */ #include <crypto.h> /* EncryptString */
#include <files_names.h> #include <files_names.h>
#include <files_interfaces.h> #include <files_interfaces.h>
Expand Down
2 changes: 1 addition & 1 deletion cf-serverd/server_tls.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <tls_generic.h> /* TLSSend */ #include <tls_generic.h> /* TLSSend */
#include <cf-serverd-enterprise-stubs.h> #include <cf-serverd-enterprise-stubs.h>
#include <connection_info.h> #include <connection_info.h>
#include <string_lib.h> /* StringMatchFull */ #include <regex.h> /* StringMatchFull */
#include <known_dirs.h> #include <known_dirs.h>
#include <file_lib.h> /* IsDirReal */ #include <file_lib.h> /* IsDirReal */


Expand Down
1 change: 1 addition & 0 deletions libenv/sysinfo.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <systype.h> #include <systype.h>
#include <unix.h> #include <unix.h>
#include <string_lib.h> #include <string_lib.h>
#include <regex.h> /* StringMatchFull */
#include <misc_lib.h> #include <misc_lib.h>
#include <file_lib.h> #include <file_lib.h>
#include <rlist.h> #include <rlist.h>
Expand Down
1 change: 1 addition & 0 deletions libenv/unix_iface.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <misc_lib.h> #include <misc_lib.h>
#include <communication.h> #include <communication.h>
#include <string_lib.h> #include <string_lib.h>
#include <regex.h> /* StringMatchFull */


#ifdef HAVE_SYS_JAIL_H #ifdef HAVE_SYS_JAIL_H
# include <sys/jail.h> # include <sys/jail.h>
Expand Down
4 changes: 3 additions & 1 deletion libpromises/class.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


#include <rb-tree.h> #include <rb-tree.h>
#include <alloc.h> #include <alloc.h>
#include <string_lib.h> #include <string_lib.h> /* StringHash,StringConcatenate */
#include <regex.h> /* CompileRegex,StringMatchFullWithPrecompiledRegex */
#include <files_names.h> #include <files_names.h>



struct ClassTable_ struct ClassTable_
{ {
RBTree *classes; RBTree *classes;
Expand Down
1 change: 1 addition & 0 deletions libpromises/evalfunction.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <hashes.h> #include <hashes.h>
#include <unix.h> #include <unix.h>
#include <string_lib.h> #include <string_lib.h>
#include <regex.h> /* CompileRegex,StringMatchWithPrecompiledRegex */
#include <net.h> /* SocketConnect */ #include <net.h> /* SocketConnect */
#include <communication.h> #include <communication.h>
#include <classic.h> /* SendSocketStream */ #include <classic.h> /* SendSocketStream */
Expand Down
2 changes: 1 addition & 1 deletion libpromises/item_lib.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <addr_lib.h> #include <addr_lib.h>
#include <matching.h> #include <matching.h>
#include <misc_lib.h> #include <misc_lib.h>
#include <string_lib.h> #include <regex.h> /* StringMatchFull,CompileRegex,StringMatchWithPrecompiledRegex */
#include <file_lib.h> #include <file_lib.h>
#include <files_interfaces.h> #include <files_interfaces.h>


Expand Down
2 changes: 1 addition & 1 deletion libpromises/matching.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <scope.h> #include <scope.h>
#include <misc_lib.h> #include <misc_lib.h>
#include <rlist.h> #include <rlist.h>
#include <string_lib.h> #include <regex.h> /* CompileRegex,StringMatchFull */


/* Pure, non-thread-safe */ /* Pure, non-thread-safe */
static char *FirstBackReference(pcre *rx, const char *teststring) static char *FirstBackReference(pcre *rx, const char *teststring)
Expand Down
6 changes: 4 additions & 2 deletions libpromises/processes_select.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@


#include <processes_select.h> #include <processes_select.h>


#include <string.h>

#include <eval_context.h> #include <eval_context.h>
#include <files_names.h> #include <files_names.h>
#include <conversion.h> #include <conversion.h>
#include <matching.h> #include <matching.h>
#include <systype.h> #include <systype.h>
#include <string_lib.h> #include <string_lib.h> /* Chop */
#include <string.h> #include <regex.h> /* CompileRegex,StringMatchWithPrecompiledRegex,StringMatchFull */
#include <item_lib.h> #include <item_lib.h>
#include <pipes.h> #include <pipes.h>
#include <files_interfaces.h> #include <files_interfaces.h>
Expand Down
3 changes: 2 additions & 1 deletion libpromises/rlist.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#include <matching.h> #include <matching.h>
#include <scope.h> #include <scope.h>
#include <fncall.h> #include <fncall.h>
#include <string_lib.h> #include <string_lib.h> /* StringHash */
#include <regex.h> /* StringMatchWithPrecompiledRegex,CompileRegex */
#include <mutex.h> #include <mutex.h>
#include <misc_lib.h> #include <misc_lib.h>
#include <assoc.h> #include <assoc.h>
Expand Down
3 changes: 2 additions & 1 deletion libpromises/syntax.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
#include <matching.h> #include <matching.h>
#include <scope.h> #include <scope.h>
#include <fncall.h> #include <fncall.h>
#include <string_lib.h> #include <string_lib.h> /* IsStrIn */
#include <regex.h> /* StringMatchFull */
#include <misc_lib.h> #include <misc_lib.h>
#include <rlist.h> #include <rlist.h>
#include <vars.h> #include <vars.h>
Expand Down
3 changes: 2 additions & 1 deletion libpromises/verify_classes.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
#include <promises.h> #include <promises.h>
#include <conversion.h> #include <conversion.h>
#include <logic_expressions.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); static int EvalClassExpression(EvalContext *ctx, Constraint *cp, const Promise *pp);
Expand Down
2 changes: 1 addition & 1 deletion libpromises/verify_vars.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


#include <actuator.h> #include <actuator.h>
#include <attributes.h> #include <attributes.h>
#include <string_lib.h> #include <regex.h> /* CompileRegex,StringMatchFullWithPrecompiledRegex */
#include <buffer.h> #include <buffer.h>
#include <misc_lib.h> #include <misc_lib.h>
#include <fncall.h> #include <fncall.h>
Expand Down
1 change: 1 addition & 0 deletions libutils/Makefile.am
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ libutils_la_SOURCES = \
hash.c hash.h \ hash.c hash.h \
queue.c queue.h \ queue.c queue.h \
ring_buffer.c ring_buffer.h \ ring_buffer.c ring_buffer.h \
regex.c regex.h \
printsize.h printsize.h


if !NT if !NT
Expand Down
1 change: 1 addition & 0 deletions libutils/json.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <string_lib.h> #include <string_lib.h>
#include <file_lib.h> #include <file_lib.h>
#include <printsize.h> #include <printsize.h>
#include <regex.h>


#include <json.h> #include <json.h>


Expand Down
203 changes: 203 additions & 0 deletions libutils/regex.c
Original file line number Original file line Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 99a5f54

Please sign in to comment.