#!/usr/bin/env bash set -euo pipefail root=$(realpath "$1") here=$(cd "$(dirname "$0")" && pwd) for f in src/emutli.c src/emutli1.c src/CMakeLists.txt include/emutli.h include/net.h; do test -f "$root/$f" || { echo "Unsupported source layout: $f" >&2; exit 1; }; done if grep -q MARS_PCAP_PORT_V1 "$root/src/CMakeLists.txt"; then echo 'Already patched' >&2; exit 1; fi for f in "$root/src/emutli.c" "$root/src/emutli1.c"; do grep -Fq '#include "net.h"' "$f" && grep -Fq 'socket(AF_IPX' "$f" || { echo "Unexpected source version: $f" >&2; exit 1; } sed -i '0,/#include "net.h"/s//#include "net.h"\n#include "mars_pcap_shim.h"/' "$f" sed -Ei 's/\b(socket|bind|getsockname|setsockopt|sendto|recvfrom|ioctl)[[:space:]]*\(/mars_\1(/g' "$f" done grep -Fq '#include "config.h"' "$root/include/net.h" || exit 1 sed -i '0,/#include "config.h"/s//#include "config.h"\n#undef SIOCIPXNCPCONN\n#undef CALL_NWCONN_OVER_SOCKET\n#define CALL_NWCONN_OVER_SOCKET 0/' "$root/include/net.h" find "$root/src" "$root/include" -type f \( -name '*.c' -o -name '*.h' \) -exec sed -Ei 's@/proc/net/ipx(_interface|_route)?(["\x27])@/run/mars-pcap/ipx\1\2@g' {} + if grep -En '\bsocket[[:space:]]*\([[:space:]]*(AF|PF)_IPX\b' "$root"/src/emutli.c "$root"/src/emutli1.c; then echo 'Unported direct IPX socket call' >&2; exit 1; fi cp "$here/src/shim.h" "$root/include/mars_pcap_shim.h" cat >> "$root/src/CMakeLists.txt" <<'CMAKE' # MARS_PCAP_PORT_V1 set(MARS_PCAP_LIBRARY "" CACHE FILEPATH "PCAP socket adapter") if(NOT EXISTS "${MARS_PCAP_LIBRARY}") message(FATAL_ERROR "MARS_PCAP_LIBRARY is required") endif() foreach(t nwserv nwconn ncpserv nwclient nwbind nwrouted dbmtool) if(TARGET ${t}) target_link_libraries(${t} "${MARS_PCAP_LIBRARY}") endif() endforeach() CMAKE printf 'Patched MARS_NWE transport.\n' # Live admin adapter in nwbind's own serialized event loop. f="$root/src/nwbind.c" for anchor in 'int main(int argc, char *argv[])' ' set_sig();' 'if (t_rcvudata(ncp_fd, &ud, &rcv_flags) > -1)' ' nw_exit_dbm();'; do [[ $(grep -Fc "$anchor" "$f") == 1 ]] || { echo "Unsupported nwbind anchor: $anchor" >&2; exit 1; } done cp "$here/upstream-adapter/"mars_{admin,scripts,acl}.inc "$root/src/" sed -i '1i #ifndef _GNU_SOURCE\n#define _GNU_SOURCE\n#endif' "$f" sed -i '/^int main(int argc, char \*argv\[\])/i #include "mars_admin.inc"' "$f" sed -i '/^ set_sig();/a\ ma_init();' "$f" sed -i 's/if (t_rcvudata(ncp_fd, \&ud, \&rcv_flags) > -1)/if (ma_wait(ncp_fd) \&\& t_rcvudata(ncp_fd, \&ud, \&rcv_flags) > -1)/' "$f" sed -i '/^ nw_exit_dbm();/i\ ma_close();' "$f" cat >> "$root/src/CMakeLists.txt" <<'CMAKE' find_package(PkgConfig REQUIRED) pkg_check_modules(MARS_JSON REQUIRED IMPORTED_TARGET json-c) if(NOT TARGET nwbind) message(FATAL_ERROR "nwbind target missing") endif() target_link_libraries(nwbind PkgConfig::MARS_JSON) CMAKE # Keep original spool file on a non-zero print subprocess exit. f="$root/src/nwqconn.c" [[ $(grep -Fc 'if (0 != (k=ext_pclose(fp))) {' "$f") == 1 ]] || { echo 'Unsupported nwqconn print hook' >&2; exit 1; } sed -i '/if (0 != (k=ext_pclose(fp))) {/a\ is_ok=0; /* keep spool file on failed submission */' "$f" printf 'Patched live Bindery administration and print error handling.\n' # Extend upstream ftrustee, using its own original volume/trustee routines. f="$root/src/ftrustee.c" [[ $(grep -Fc 'int main(int argc, char *argv[])' "$f") == 1 && $(grep -Fc ' localinit();' "$f") == 1 ]] || { echo 'Unsupported ftrustee layout' >&2; exit 1; } cp "$here/upstream-adapter/mars_trustee.inc" "$root/src/" sed -i '/^int main(int argc, char \*argv\[\])/i #include "mars_trustee.inc"' "$f" sed -i '/^ localinit();/a\ if (argc > 1 \&\& !strcmp(argv[1], "--mars-admin")) return mt_admin(argc, argv);' "$f" # Modern glibc removed sys_errlist/sys_nerr; strerror preserves error text. sed -i -e 's/errnum > 0 \&\& errnum < _sys_nerr/errnum > 0/g' -e 's/errnum >= 0 \&\& errnum < _sys_nerr/errnum >= 0/g' -e 's/_sys_errlist\[errnum\]/strerror(errnum)/g' "$root/src/tools.c" # Use libc poll declarations with the native admin adapter; retain the old # select-based helper under a private name instead of interposing libc poll. python3 - "$root" <<'PY' import pathlib,sys,re root=pathlib.Path(sys.argv[1]) p=root/'include/emutli.h';s=p.read_text();s=re.sub(r'struct pollfd \{.*?#define POLLPRI[^\n]*', '#include ', s, flags=re.S);s=re.sub(r'extern int poll\([^;]+;', '',s);p.write_text(s) p=root/'src/emutli.c';s=p.read_text().replace('int poll(', 'int mars_legacy_poll(');p.write_text(s) PY # LOCALSTATEDIR is absolute in our build; avoid //var (network-path DESTDIR error). sed -i 's@"/${CMAKE_INSTALL_LOCALSTATEDIR}/@"${CMAKE_INSTALL_LOCALSTATEDIR}/@g' "$root/cmake/modules/MarsNweInstallDirs.cmake" # Upstream renamed the printer helper to _prn without updating installation. sed -i 's@install(FILES prn DESTINATION ${MARS_NWE_INSTALL_FULL_LIBEXECDIR})@install(FILES _prn DESTINATION ${MARS_NWE_INSTALL_FULL_LIBEXECDIR} RENAME prn)@' "$root/opt/CMakeLists.txt"