Skip to content

Commit

Permalink
PCRE -> PCRE2
Browse files Browse the repository at this point in the history
  • Loading branch information
fzwoch committed Sep 18, 2023
1 parent 1f633df commit edbce01
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 257 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scripts/homebrew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set -e
# dependencies to avoid installing Intel versions in the cross-compilation
# case.
dependencies=(
pcre
pcre2
freetype
jansson
libpng
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ ZLIB_LIBS ?= -lz
CFLAGS_c += $(ZLIB_CFLAGS)
LIBS_c += $(ZLIB_LIBS)

PCRE_CFLAGS ?= $(shell pkg-config libpcre --cflags)
PCRE_LIBS ?= $(shell pkg-config libpcre --libs)
CFLAGS_c += $(PCRE_CFLAGS)
LIBS_c += $(PCRE_LIBS)
PCRE2_CFLAGS ?= $(shell pkg-config libpcre2-8 --cflags)
PCRE2_LIBS ?= $(shell pkg-config libpcre2-8 --libs)
CFLAGS_c += $(PCRE2_CFLAGS) -DPCRE2_CODE_UNIT_WIDTH=8
LIBS_c += $(PCRE2_LIBS)

EXPAT_CFLAGS ?= $(shell pkg-config expat --cflags)
EXPAT_LIBS ?= $(shell pkg-config expat --libs)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ Make sure you have the dependencies installed:

- For *Debian/Ubuntu 16.10+*
```
sudo apt install git build-essential libsdl2-dev libjansson-dev libexpat-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libsndfile-dev libspeex-dev libspeexdsp-dev libxxf86vm-dev libpcre3-dev
sudo apt install git build-essential libsdl2-dev libjansson-dev libexpat-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libsndfile-dev libspeex-dev libspeexdsp-dev libxxf86vm-dev libpcre2-dev
```
- For *openSUSE Tumbleweed*
```
sudo zypper install -t pattern devel_C_C++
sudo zypper install git pcre-devel Mesa-libGL-devel SDL2-devel libjansson-devel libexpat-devel libcurl-devel libpng16-devel libpng16-compat-devel libjpeg8-devel libjpeg-turbo libsndfile-devel speex-devel speexdsp-devel libXxf86vm-devel
sudo zypper install git pcre2-devel Mesa-libGL-devel SDL2-devel libjansson-devel libexpat-devel libcurl-devel libpng16-devel libpng16-compat-devel libjpeg8-devel libjpeg-turbo libsndfile-devel speex-devel speexdsp-devel libXxf86vm-devel
```
- For *Fedora*
```
sudo dnf group install 'C Development Tools and Libraries'
sudo dnf install git pcre-devel mesa-libEGL-devel SDL2-devel jansson-devel expat-devel libcurl-devel libpng-devel libjpeg-turbo-devel libsndfile-devel speex-devel speexdsp-devel libXxf86vm-devel
sudo dnf install git pcre2-devel mesa-libEGL-devel SDL2-devel jansson-devel expat-devel libcurl-devel libpng-devel libjpeg-turbo-devel libsndfile-devel speex-devel speexdsp-devel libXxf86vm-devel
```

Clone the git repository:
Expand Down Expand Up @@ -197,7 +197,7 @@ Make sure you run the `brew doctor` as instructed before doing anything else.
Then run:

```
brew install sdl2 sdl2_net sdl2_image sdl2_gfx sdl2_mixer pcre jansson pkg-config speex speexdsp libsndfile
brew install sdl2 sdl2_net sdl2_image sdl2_gfx sdl2_mixer pcre2 jansson pkg-config speex speexdsp libsndfile
```

When it's done, just run `make` and it should compile without errors.
Expand Down
6 changes: 3 additions & 3 deletions build-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ NC='\e[0m'

BUILD_LOG=/tmp/ezquake-build.log

PKGS_DEB="git build-essential libsdl2-2.0-0 libsdl2-dev libjansson-dev libexpat1-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libspeex-dev libspeexdsp-dev libfreetype6-dev libsndfile1-dev libpcre3-dev libminizip-dev"
PKGS_RPM="pcre-devel mesa-libGL-devel SDL2-devel make gcc jansson-devel expat-devel libcurl-devel libpng-devel libjpeg-turbo-devel speex-devel speexdsp-devel freetype-devel libsndfile-devel libXxf86vm-devel minizip-devel"
PKGS_DEB="git build-essential libsdl2-2.0-0 libsdl2-dev libjansson-dev libexpat1-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libspeex-dev libspeexdsp-dev libfreetype6-dev libsndfile1-dev libpcre2-dev libminizip-dev"
PKGS_RPM="pcre2-devel mesa-libGL-devel SDL2-devel make gcc jansson-devel expat-devel libcurl-devel libpng-devel libjpeg-turbo-devel speex-devel speexdsp-devel freetype-devel libsndfile-devel libXxf86vm-devel minizip-devel"
PKGS_ARCH="base-devel libpng libjpeg-turbo sdl2 expat libcurl-compat freetype2 speex speexdsp jansson libsndfile minizip"
PKGS_VOID="base-devel SDL2-devel pcre-devel jansson-devel expat-devel libcurl-devel libpng-devel libjpeg-turbo-devel speex-devel speexdsp-devel freetype-devel libsndfile-devel libXxf86vm-devel minizip"
PKGS_VOID="base-devel SDL2-devel pcre2-devel jansson-devel expat-devel libcurl-devel libpng-devel libjpeg-turbo-devel speex-devel speexdsp-devel freetype-devel libsndfile-devel libXxf86vm-devel minizip"

CPU=$(uname -m | sed -e s/i.86/i386/ -e s/amd64/x86_64/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/alpha/axp/)

Expand Down
21 changes: 12 additions & 9 deletions src/EX_browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int source_unique = 0;

typedef struct info_filter_s {
char name[MAX_INFO_KEY];
pcre* regex;
pcre2_code* regex;
qbool pass;
qbool exec;
} info_filter_t;
Expand Down Expand Up @@ -3410,11 +3410,12 @@ static qbool SB_InfoFilter_Exec(info_filter_t* info_filters, int info_filter_cou
}
else {
// Rule specified, check it matches the regex
int offsets[8];

if (pcre_exec(filter->regex, NULL, value, strlen(value), 0, 0, offsets, sizeof(offsets) / sizeof(offsets[0])) >= 0) {
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(filter->regex, NULL);
if (pcre2_match(filter->regex, (PCRE2_SPTR)value, strlen(value), 0, 0, match_data, NULL) >= 0) {
pcre2_match_data_free (match_data);
return filter->pass;
}
pcre2_match_data_free (match_data);
}
}

Expand All @@ -3436,8 +3437,8 @@ static info_filter_t* SB_InfoFilter_Parse(int* count)
info_filters = Q_malloc(info_filter_count * sizeof(info_filter_t));
for (i = 0; i < info_filter_count; ++i) {
char* split;
const char* error;
int error_offset;
int error;
PCRE2_SIZE error_offset;
info_filter_t* filter = &info_filters[i];

// filters must be +x=y or -x=y
Expand All @@ -3462,9 +3463,11 @@ static info_filter_t* SB_InfoFilter_Parse(int* count)

if (split != NULL && split[1] && split[1] != '*') {
// no regex is fine
filter->regex = pcre_compile(split + 1, PCRE_CASELESS, &error, &error_offset, NULL);
filter->regex = pcre2_compile((PCRE2_SPTR)(split + 1), PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &error, &error_offset, NULL);
if (filter->regex == NULL) {
Con_Printf("Invalid rule definition: %s\n", error);
PCRE2_UCHAR error_str[256];
pcre2_get_error_message(error, error_str, sizeof(error_str));
Con_Printf("Invalid rule definition: %s\n", error_str);
continue;
}
}
Expand All @@ -3481,7 +3484,7 @@ static void SB_InfoFilter_Free(info_filter_t* info_filters, int info_filter_coun

for (i = 0; i < info_filter_count; ++i) {
if (info_filters[i].regex) {
pcre_free(info_filters[i].regex);
pcre2_code_free(info_filters[i].regex);
}
}

Expand Down
30 changes: 18 additions & 12 deletions src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "parser.h"
#include "utils.h"
#include "keys.h"
#include <pcre2.h>

typedef struct {
char name[MAX_MACRO_NAME];
Expand Down Expand Up @@ -1900,10 +1901,10 @@ static qbool is_numeric (char *c)
(*c == '.' && isdigit((int)(unsigned char)c[1])) );
}

void Re_Trigger_Copy_Subpatterns (const char *s, int* offsets, int num, cvar_t *re_sub); // QW262
void Re_Trigger_Copy_Subpatterns (const char *s, size_t* offsets, int num, cvar_t *re_sub); // QW262
extern cvar_t re_sub[10]; // QW262

void Cmd_CatchTriggerSubpatterns(const char *s, int* offsets, int num)
void Cmd_CatchTriggerSubpatterns(const char *s, size_t* offsets, int num)
{
Re_Trigger_Copy_Subpatterns(s, offsets, min(num, 10), re_sub);
}
Expand Down Expand Up @@ -2043,20 +2044,24 @@ void Cmd_If_Old (void)
result = (strstr(Cmd_Argv(3), Cmd_Argv(1)) ? 0 : 1);

} else if (!strcmp(op, "=~") || !strcmp(op, "!~")) {
pcre* regexp;
const char *error;
int error_offset;
int rc;
int offsets[99];
pcre2_code *regexp;
int error;
PCRE2_SIZE error_offset;
pcre2_match_data *match_data = NULL;
int rc;

regexp = pcre_compile (Cmd_Argv(3), 0, &error, &error_offset, NULL);
regexp = pcre2_compile ((PCRE2_SPTR)Cmd_Argv(3), PCRE2_ZERO_TERMINATED, 0, &error, &error_offset, NULL);
if (!regexp) {
Com_Printf ("Error in regexp: %s\n", error);
PCRE2_UCHAR error_str[256];
pcre2_get_error_message(error, error_str, sizeof(error_str));
Com_Printf ("Error in regexp: %s\n", error_str);
return;
}
rc = pcre_exec (regexp, NULL, Cmd_Argv(1), strlen(Cmd_Argv(1)),
0, 0, offsets, 99);
match_data = pcre2_match_data_create_from_pattern(regexp, NULL);
rc = pcre2_match (regexp, (PCRE2_SPTR)Cmd_Argv(1), strlen(Cmd_Argv(1)),
0, 0, match_data, NULL);
if (rc >= 0) {
PCRE2_SIZE *offsets = pcre2_get_ovector_pointer(match_data);
Re_Trigger_Copy_Subpatterns (Cmd_Argv(1), offsets, min(rc, 10), re_sub);
result = true;
} else
Expand All @@ -2065,7 +2070,8 @@ void Cmd_If_Old (void)
if (op[0] != '=')
result = !result;

pcre_free (regexp);
pcre2_match_data_free (match_data);
pcre2_code_free (regexp);
} else {
Com_Printf ("unknown operator: %s\n", op);
Com_Printf ("valid operators are ==, =, !=, <>, >, <, >=, <=, isin, !isin, =~, !~\n");
Expand Down
33 changes: 17 additions & 16 deletions src/fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifdef EZ_FREETYPE_SUPPORT
#include <ft2build.h>
#include FT_FREETYPE_H
#include "pcre.h"
#include "pcre2.h"
#endif
#include "quakedef.h"
#include "gl_model.h"
Expand Down Expand Up @@ -449,20 +449,20 @@ void Draw_ListFonts_f(void)
char path[MAX_OSPATH];
dir_t dir;
int i, printed;
pcre* regexp = NULL;
pcre2_code* regexp = NULL;
pcre2_match_data *match_data = NULL;

strlcpy(path, Sys_FontsDirectory(), sizeof(path));

if (Cmd_Argc() > 1) {
const char* error = NULL;
int erroffset = 0;

regexp = pcre_compile(Cmd_Argv(1), PCRE_CASELESS, &error, &erroffset, NULL);
if (error) {
if (regexp) {
pcre_free(regexp);
}
Con_Printf("Error in regular expression: %s\n", error);
int error = 0;
PCRE2_SIZE error_offset = 0;

regexp = pcre2_compile((PCRE2_SPTR)Cmd_Argv(1), PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &error, &error_offset, NULL);
if (!regexp) {
PCRE2_UCHAR error_str[256];
pcre2_get_error_message(error, error_str, sizeof(error_str));
Con_Printf("Error in regular expression: %s\n", error_str);
return;
}
}
Expand All @@ -473,19 +473,20 @@ void Draw_ListFonts_f(void)
continue;
}
if (regexp) {
int offsets[3]; // pcre manual: must be multiple of 3

if (pcre_exec(regexp, NULL, dir.files[i].name, strlen(dir.files[i].name), 0, 0, offsets, sizeof(offsets) / sizeof(offsets[0])) <= 0) {
match_data = pcre2_match_data_create_from_pattern(regexp, NULL);
if (pcre2_match(regexp, (PCRE2_SPTR)dir.files[i].name, strlen(dir.files[i].name), 0, 0, match_data, NULL) <= 0) {
pcre2_match_data_free(match_data);
continue;
}
pcre2_match_data_free(match_data);
}

Con_Printf(" %s\n", dir.files[i].name);
printed++;
}

if (regexp) {
pcre_free(regexp);
pcre2_code_free(regexp);
}
Con_Printf("Found %d/%d files in %s\n", printed, dir.numfiles, path);
}
Expand Down
24 changes: 14 additions & 10 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "quakedef.h"
#include "q_shared.h"
#include "utils.h"
#include "pcre.h"
#include "pcre2.h"
#include "parser.h"

#undef Q_malloc
Expand Down Expand Up @@ -765,22 +765,25 @@ LOCAL expr_val operator_reeq(EParser p, const expr_val e1, const expr_val e2)
expr_val mask = ToString(p, e2);
// this makes sense for "111 =~ 1.1", however we are doing str->double->str conversion
// and it can happen that the result string won't be the same as the source
pcre* regexp;
const char *error;
int error_offset;
int rc;
int offsets[99];
pcre2_code *regexp;
int error;
PCRE2_SIZE error_offset;
pcre2_match_data *match_data = NULL;
PCRE2_SIZE *offsets;
int rc;

r.type = ET_BOOL;

regexp = pcre_compile (mask.s_val, 0, &error, &error_offset, NULL);
regexp = pcre2_compile ((PCRE2_SPTR)mask.s_val, PCRE2_ZERO_TERMINATED, 0, &error, &error_offset, NULL);
if (!regexp) {
SetError(p, ERR_REGEXP);
return Get_Expr_Dummy();
}
rc = pcre_exec (regexp, NULL, strr.s_val, strlen(strr.s_val),
0, 0, offsets, 99);
match_data = pcre2_match_data_create_from_pattern(regexp, NULL);
rc = pcre2_match (regexp, (PCRE2_SPTR)strr.s_val, strlen(strr.s_val),
0, 0, match_data, NULL);
if (rc >= 0) {
offsets = pcre2_get_ovector_pointer(match_data);
if (p->re_patfnc)
p->re_patfnc(strr.s_val, offsets, rc > 99 ? 99 : rc);
r.b_val = BOOL_TRUE;
Expand All @@ -790,7 +793,8 @@ LOCAL expr_val operator_reeq(EParser p, const expr_val e1, const expr_val e2)
Q_free(e1.s_val);
Q_free(e2.s_val);

pcre_free (regexp);
pcre2_match_data_free (match_data);
pcre2_code_free (regexp);
return r;
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ typedef expr_val (* variable_val_fnc) (const char* varname);
/// \param[in] str source string
/// \param[in] offsets array of offset pairs (start1,end1,start2,end2,...)
/// \param[in] matches number of captured groups
typedef void (* subpatterns_report_fnc) (const char* str, int* offsets, int matches);
typedef void (* subpatterns_report_fnc) (const char* str, size_t* offsets, int matches);

/// expression parser's extra options
typedef struct {
Expand Down
Loading

0 comments on commit edbce01

Please sign in to comment.