From 4745fcf1c78da101314252bd6ad65db5880434eb Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 12 Oct 2019 14:50:49 +0100 Subject: [PATCH] add regex compatability to utils and enable it for serenity OS --- desktop/save_complete.c | 2 +- desktop/treeview.c | 2 +- utils/config.h | 30 +++++++++++++------- utils/regex.h | 63 +++++++++++++++++++++++++++++++++++++++++ utils/utils.c | 47 ++++++++++++++++++++++++++++-- 5 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 utils/regex.h diff --git a/desktop/save_complete.c b/desktop/save_complete.c index c6f311acb..6081e627b 100644 --- a/desktop/save_complete.c +++ b/desktop/save_complete.c @@ -28,10 +28,10 @@ #include #include #include -#include #include #include "utils/config.h" +#include "utils/regex.h" #include "utils/corestrings.h" #include "utils/log.h" #include "utils/nsurl.h" diff --git a/desktop/treeview.c b/desktop/treeview.c index 258b54a52..e8b4a14c8 100644 --- a/desktop/treeview.c +++ b/desktop/treeview.c @@ -26,11 +26,11 @@ #include +#include "utils/config.h" #include "utils/utils.h" #include "utils/log.h" #include "utils/nsurl.h" #include "utils/nsoption.h" -#include "utils/config.h" #include "netsurf/bitmap.h" #include "netsurf/content.h" #include "netsurf/plotters.h" diff --git a/utils/config.h b/utils/config.h index 2f393dd13..5678beaa3 100644 --- a/utils/config.h +++ b/utils/config.h @@ -16,8 +16,8 @@ * along with this program. If not, see . */ -#ifndef _NETSURF_UTILS_CONFIG_H_ -#define _NETSURF_UTILS_CONFIG_H_ +#ifndef NETSURF_UTILS_CONFIG_H_ +#define NETSURF_UTILS_CONFIG_H_ #include @@ -35,10 +35,10 @@ char *strndup(const char *s, size_t n); #endif -#if (defined(_GNU_SOURCE) || \ - defined(__APPLE__) || \ - defined(__HAIKU__) || \ - defined(__OpenBSD__) &&\ +#if ((defined(_GNU_SOURCE) || \ + defined(__APPLE__) || \ + defined(__HAIKU__) || \ + defined(__OpenBSD__)) && \ !defined(__serenity__)) #define HAVE_STRCASESTR #else @@ -68,14 +68,18 @@ char *strchrnul(const char *s, int c); #endif #define HAVE_SYS_SELECT -#define HAVE_INETATON #define HAVE_POSIX_INET_HEADERS #if (defined(_WIN32)) -#undef HAVE_INETATON #undef HAVE_SYS_SELECT #undef HAVE_POSIX_INET_HEADERS #endif +#define HAVE_INETATON +#if (defined(_WIN32) || \ + defined(__serenity__)) +#undef HAVE_INETATON +#endif + #define HAVE_INETPTON #if (defined(_WIN32)) #undef HAVE_INETPTON @@ -113,10 +117,16 @@ char *realpath(const char *path, char *resolved_path); #endif #define HAVE_SCANDIR -#if (defined(_WIN32)) +#if (defined(_WIN32) || \ + defined(__serenity__)) #undef HAVE_SCANDIR #endif +#define HAVE_REGEX +#if (defined(__serenity__)) +#undef HAVE_REGEX +#endif + /* This section toggles build options on and off. * Simply undefine a symbol to turn the relevant feature off. * @@ -142,7 +152,7 @@ char *realpath(const char *path, char *resolved_path); #define WITH_MMAP #endif -/* amiga */ +/* IPv6 */ #if (defined(__amigaos4__) || \ defined(__AMIGA__) || \ defined(nsatari) || \ diff --git a/utils/regex.h b/utils/regex.h new file mode 100644 index 000000000..a415239d6 --- /dev/null +++ b/utils/regex.h @@ -0,0 +1,63 @@ +/* + * Copyright 2019 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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 2 of the License. + * + * NetSurf 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, see . + */ + +#ifndef NETSURF_UTILS_REGEX_H_ +#define NETSURF_UTILS_REGEX_H_ + +#include "utils/config.h" + +#ifdef HAVE_REGEX +#include +#include +#else + +#define REG_NOMATCH 1 + +#define REG_EXTENDED 1 +#define REG_ICASE (1 << 1) +#define REG_NEWLINE (1 << 2) +#define REG_NOSUB (1 << 3) + +typedef ssize_t regoff_t; + +typedef struct { + size_t re_nsub; /* Number of parenthesized subexpressions.*/ +} regex_t; + + +typedef struct { + regoff_t rm_so; /* Byte offset from start of string to start + * of substring. + */ + regoff_t rm_eo; /* Byte offset from start of string of the + * first character after the end of substring. + */ +} regmatch_t; + + +int regcomp(regex_t *restrict preg, const char *restrictregex, int cflags); + +size_t regerror(int errorcode, const regex_t *restrict preg, char *restrict errbuf, size_t errbuf_size); + +int regexec(const regex_t *restrict preg, const char *restrict string, size_t nmatch, regmatch_t pmatch[restrict], int eflags); + +void regfree(regex_t *preg); + +#endif + +#endif diff --git a/utils/utils.c b/utils/utils.c index 15c91c621..aec0116a0 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -24,7 +24,9 @@ #include #include #include +#include #include +#include #include "utils/messages.h" #include "utils/dirent.h" @@ -172,6 +174,7 @@ nserror vsnstrjoin(char **str, size_t *size, char sep, size_t nelm, va_list ap) return NSERROR_OK; } + /* exported interface documented in utils/utils.h */ nserror snstrjoin(char **str, size_t *size, char sep, size_t nelm, ...) { @@ -263,12 +266,12 @@ char *strcasestr(const char *haystack, const char *needle) #endif + #ifndef HAVE_STRNDUP /** * Duplicate up to n characters of a string. */ - char *strndup(const char *s, size_t n) { size_t len; @@ -393,6 +396,7 @@ char *strchrnul (const char *s, int c_in) #endif #ifndef HAVE_UTSNAME + #include "utils/utsname.h" int uname(struct utsname *buf) { @@ -404,9 +408,11 @@ int uname(struct utsname *buf) { return 0; } + #endif #ifndef HAVE_REALPATH + char *realpath(const char *path, char *resolved_path) { char *ret; @@ -419,8 +425,9 @@ char *realpath(const char *path, char *resolved_path) return ret; } -#ifndef HAVE_INETATON +#endif +#ifndef HAVE_INETATON int inet_aton(const char *cp, struct in_addr *inp) { @@ -470,4 +477,40 @@ int inet_pton(int af, const char *src, void *dst) #endif +#ifndef HAVE_REGEX + +#include "utils/regex.h" + +int +regcomp(regex_t *restrict preg, const char *restrictregex, int cflags) +{ + return 0; +} + +size_t +regerror(int errorcode, + const regex_t *restrict preg, + char *restrict errbuf, + size_t errbuf_size) +{ + if ((errbuf != NULL) && (errbuf_size != 0)) { + *errbuf = 0; + } + return 0; +} + +int +regexec(const regex_t *restrict preg, + const char *restrict string, + size_t nmatch, + regmatch_t pmatch[restrict], + int eflags) +{ + return REG_NOMATCH; +} + +void regfree(regex_t *preg) +{ +} + #endif