Skip to content

Commit

Permalink
SCP: PCRE2 support.
Browse files Browse the repository at this point in the history
Add PCRE2 support as an alternative to the original PCRE. PCRE2 is
actively maintained and has a cleaner API than its predecessor.

- Use a function interface around regular expressions to isolate and
  reduce the preprocessor #if/#endif forest. Abide by a single function
  declaration/implementation approach, vs. multiple function
  implentations within an #if/#endif forest.

- Clean up the code so that work isn't recreated or duplicated, such as
  compiling regexps.

Platform notes:

- cmake: PCRE2 is the default RegEx support. To revert to PCRE, add
  "-DPREFER_PCRE" to the cmake configuration command line.

- makefile: PCRE remains the default RegEx support out of respect for
  tradition (cue Chaim Topol singing "Tradition".) Enable PCRE2 by
  defining "USE_PCRE2=<some value>" from the make command line or shell
  environment.

- Visual Studio Projects: Untouched. Externally built libraries provide
  regular expression support residing in a specific place in the file
  system, which is outside the control of SIMH proper.

sim_defs.h:

- Change EXPECT::size from int32 to size_t for increased
  cross-platform/64-bit compatibility (i.e., array indices and offsets
  should be size_t, where practicable.)

- Add typedefs for EXPECT regex support independence: sim_regex_t,
  sim_re_capture_t.

- Add regular expression context to EXPTAB that maintains state when
  processing the captured matches, making the function interface clean.
  Tracks intermediate state while processing captured matches.
  • Loading branch information
bscottm committed Sep 27, 2024
1 parent 2437b13 commit f008ac7
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .travis/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ install_linux() {
sudo apt-get install -ym libegl1-mesa-dev libgles2-mesa-dev
sudo apt-get install -ym libsdl2-dev libfreetype6-dev libsdl2-ttf-dev
sudo apt-get install -ym libpcap-dev libvdeplug-dev
sudo apt-get install -ym libpcre2-dev libpcre2-8-0
sudo apt-get install -ym cmake cmake-data
}

Expand All @@ -37,6 +38,7 @@ install_mingw64() {
mingw-w64-x86_64-gcc \
mingw-w64-x86_64-make \
mingw-w64-x86_64-pcre \
mingw-w64-x86_64-pcre2 \
mingw-w64-x86_64-freetype \
mingw-w64-x86_64-SDL2 \
mingw-w64-x86_64-SDL2_ttf \
Expand All @@ -50,6 +52,7 @@ install_ucrt64() {
mingw-w64-ucrt-x86_64-gcc \
mingw-w64-ucrt-x86_64-make \
mingw-w64-ucrt-x86_64-pcre \
mingw-w64-ucrt-x86_64-pcre2 \
mingw-w64-ucrt-x86_64-freetype \
mingw-w64-ucrt-x86_64-SDL2 \
mingw-w64-ucrt-x86_64-SDL2_ttf \
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ option(WITH_REGEX
TRUE)
option(PREFER_PCRE
"Prefer (=1)/ignore (=0) Prefer PCRE over PCRE2 (def: ignore)"
TRUE)
FALSE)
option(WITH_NETWORK
"Enable (=1)/disable (=0) simulator networking support. (def: enabled)"
TRUE)
Expand Down
1 change: 1 addition & 0 deletions README-CMake.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ following the table.
| `BUILD_SHARED_DEPS` | platform-specific | Build dependencies as shared libraries/DLLs on Windows. Does nothing on Linux/macOS. Disabled by default on Windows to ensure that the simulators link against static libraries. |
| `WITH_ASYNC` | enabled | Asynchronous I/O and threading support. |
| `WITH_REGEX` | enabled | PCRE regular expression support. |
| `PREFER_PCRE` | disabled | Prefer the original PCRE regular expression library over PCRE2. The default is PCRE2, which is more actively maintained. |
| `WITH_NETWORK` | enabled | Simulator networking support. `WITH_PCAP`, `WITH_SLIRP`, `WITH_VDE` and `WITH_TAP` only have meaning if `WITH_NETWORK` is enabled. |
| `WITH_PCAP` | enabled | libpcap (packet capture) support. |
| `WITH_SLIRP` | enabled | SLIRP UDP network support. |
Expand Down
8 changes: 2 additions & 6 deletions cmake/CMake-Maintainers.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ The top-level `CMakeLists.txt` drives CMake's configure/generate phase, which ha
is separate for the time being for functional clarity.
- `thread_lib` (`pthreads-dep.cmake`): Platform threading support. Usually empty for Linux and macOS
platforms, adds the _PThreads4W_ external dependency for Windows native and XP platforms.
- `simh_regexp`: Regular expression support, if the `WITH_REGEX` option is `True`. PCRE is effectively
the only regular expression library that ends up in the `simh_regexp` interface library at the
moment. There is support for PCRE2, but it requires changes to `scp.c` to actually make it useful.
- `simh_regexp`: Regular expression support, if the `WITH_REGEX` option is `True`.
- `simh_video`: Graphics, input controller and sound support, based on [SDL2][libsdl2], if the
`WITH_VIDEO` option is `True`. Empty interface library if `WITH_VIDEO` is `False`. `simh_video` also
pulls in `libpng` and `SDL_ttf`, along with their dependencies.
Expand Down Expand Up @@ -352,9 +350,7 @@ SIMH includes six `find_package` scripts:
capture library; `libpcap` and it's Win32 equivalent are dynamically loaded at run time. The headers are
only needed for `libpcap`'s function prototypes.

- `FindPCRE.cmake`, `FindPCRE2.cmake`: Locate PCRE and PCRE2 libraries and headers. SIMH does not support
PCRE2 yet, however, finding the PCRE2 headers and library are included to make PCRE2 support easier at
some point in the future.
- `FindPCRE.cmake`, `FindPCRE2.cmake`: Locate PCRE and PCRE2 libraries and headers.

- `FindPTW.cmake`: "PTW" is shorthand for the Windows PThreads4w POSIX threads compatibility library.

Expand Down
3 changes: 1 addition & 2 deletions cmake/dep-link.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ if (WITH_REGEX)
ELSEIF (NOT PREFER_PCRE AND PCRE2_FOUND)
target_compile_definitions(simh_regexp INTERFACE HAVE_PCRE2_H)
target_include_directories(simh_regexp INTERFACE ${PCRE2_INCLUDE_DIRS})
if (NOT WIN32)
target_link_libraries(simh_regexp INTERFACE ${PCRE2_LIBRARY})
else ()
if (WIN32)
## Use static linkage (vice DLL) on Windows:
target_compile_definitions(simh_regexp INTERFACE PCRE2_STATIC)
endif ()
Expand Down
13 changes: 11 additions & 2 deletions cmake/dep-locate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if (WITH_REGEX)
find_package(PCRE)
endif ()
else ()
## There isn't a difference between the vcpkg and LEGACY strategy.
## SIMH provides its own FindPCRE2.cmake module...
find_package(PCRE2)
endif ()
endif ()
Expand Down Expand Up @@ -111,7 +113,7 @@ include (ExternalProject)

# Source URLs (to make it easy to update versions):
set(ZLIB_SOURCE_URL "https://github.com/madler/zlib/archive/v1.2.13.zip")
set(PCRE2_SOURCE_URL "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.zip")
set(PCRE2_SOURCE_URL "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.42/pcre2-10.42.zip")
## PCRE needs multiple URLs to chase a working SF mirror:
list(APPEND PCRE_SOURCE_URL
"https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.zip/download?use_mirror=cytranet"
Expand Down Expand Up @@ -178,10 +180,17 @@ IF (WITH_REGEX AND NOT (PCRE_FOUND OR PCRE2_FOUND OR TARGET unofficial::pcre::pc
if (NOT PREFER_PCRE)
set(PCRE_URL ${PCRE2_SOURCE_URL})
list(APPEND PCRE_CMAKE_ARGS
-DPCRE2_BUILD_PCREGREP:Bool=Off
-DPCRE2_BUILD_PCRE2_8:Bool=On
-DPCRE2_BUILD_PCRE2GREP:Bool=Off
-DPCRE2_STATIC_RUNTIME:Bool=On
-DPCRE2_SUPPORT_LIBEDIT:Bool=Off
-DPCRE2_SUPPORT_LIBREADLINE:Bool=Off
)
if (WIN32)
list(APPEND PCRE_CMAKE_ARGS
-DBUILD_STATIC_LIBS:Bool=On
)
endif ()

# IF(MSVC)
# list(APPEND PCRE_CMAKE_ARGS -DINSTALL_MSVC_PDB=On)
Expand Down
41 changes: 33 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,39 @@ ifeq (${WIN32},) #*nix Environments (&& cygwin)
LIBEXT = $(LIBEXTSAVE)
endif
endif
# Find PCRE RegEx library.
ifneq (,$(call find_include,pcre))
ifneq (,$(call find_lib,pcre))
OS_CCDEFS += -DHAVE_PCRE_H
OS_LDFLAGS += -lpcre
$(info using libpcre: $(call find_lib,pcre) $(call find_include,pcre))
ifeq ($(LD_SEARCH_NEEDED),$(call need_search,pcre))
OS_LDFLAGS += -L$(dir $(call find_lib,pcre))
## RegEx: Prefer the PCRE library over PCRE2 for historical reasons. Only detect
## and use PCRE2 if USE_PCRE2 is set as a command line variable or in the
## environment.
FALLBACK_PCRE=yes
ifdef USE_PCRE2
# Find the PCRE2 RegEx library
ifneq (,$(call find_include,pcre2))
PCRE2_LIB=pcre2-8
ifneq (,$(call find_lib,${PCRE2_LIB}))
OS_CCDEFS += -DHAVE_PCRE2_H
OS_LDFLAGS += -l${PCRE2_LIB}
$(info using libpcre2-8: $(call find_lib,${PCRE2_LIB}) $(call find_include,pcre2))
ifeq ($(LD_SEARCH_NEEDED),$(call need_search,${PCRE2_LIB}))
OS_LDFLAGS += -L$(dir $(call find_lib,${{PCRE2_LIB}}))
endif
FALLBACK_PCRE=
endif
endif
ifneq ($(FALLBACK_PCRE),)
$(info *** Info *** PCRE2 not detected, falling back to PCRE)
endif
endif
# Find PCRE RegEx library, either because we didn't want PCRE2 or we didn't
# find PCRE2.
ifneq (${FALLBACK_PCRE},)
ifneq (,$(call find_include,pcre))
ifneq (,$(call find_lib,pcre))
OS_CCDEFS += -DHAVE_PCRE_H
OS_LDFLAGS += -lpcre
$(info using libpcre: $(call find_lib,pcre) $(call find_include,pcre))
ifeq ($(LD_SEARCH_NEEDED),$(call need_search,pcre))
OS_LDFLAGS += -L$(dir $(call find_lib,pcre))
endif
endif
endif
endif
Expand Down
Loading

0 comments on commit f008ac7

Please sign in to comment.