From 54de1d6ca94bc0f7ffac6ad721933caf4db46ed5 Mon Sep 17 00:00:00 2001 From: Thomas Ulmer Date: Mon, 2 Mar 2026 19:39:44 -0800 Subject: changes to compile with musl (avoid sbrk) --- Makefile | 19 ++++--- brk_shim.c | 44 +++++++++++++++ brk_shim.h | 4 ++ ex.c | 3 +- ex_re.c | 2 +- ex_subr.c | 4 +- exrecover.c | 5 +- libuxre/re.h | 2 +- libuxre/regex.h | 153 --------------------------------------------------- libuxre/uxre_regex.h | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 219 insertions(+), 169 deletions(-) create mode 100644 brk_shim.c create mode 100644 brk_shim.h delete mode 100644 libuxre/regex.h create mode 100644 libuxre/uxre_regex.h diff --git a/Makefile b/Makefile index be2666a..8ad3d20 100644 --- a/Makefile +++ b/Makefile @@ -79,11 +79,11 @@ # Destinations for installation. $(PRESERVEDIR) is used for recovery files. # It will get mode 1777. # -PREFIX=/home/tmu/.local +PREFIX ?=/usr/local BINDIR = $(PREFIX)/bin LIBEXECDIR = $(PREFIX)/libexec MANDIR = $(PREFIX)/share/man -PRESERVEDIR=/tmp/ex-preserve/ +PRESERVEDIR = /tmp/ex-preserve/ # # DESTDIR is prepended to the installation paths. It is mostly useful @@ -94,7 +94,7 @@ DESTDIR = # # A BSD-like install program. GNU install will fit well here, too. # -INSTALL=/usr/bin/install +INSTALL =/usr/bin/install # # Compiler and linker flags. @@ -247,16 +247,16 @@ OBJS = ex.o ex_addr.o ex_cmds.o ex_cmds2.o ex_cmdsub.o \ ex_set.o ex_subr.o ex_tagio.o ex_temp.o ex_tty.o ex_unix.o \ ex_v.o ex_vadj.o ex_vget.o ex_vmain.o ex_voper.o \ ex_vops.o ex_vops2.o ex_vops3.o ex_vput.o ex_vwind.o \ - printf.o ex_version.o $(MALLOC) + printf.o ex_version.o $(MALLOC) brk_shim.o HDRS = ex.h ex_argv.h ex_re.h ex_temp.h ex_tty.h ex_tune.h ex_vars.h \ - ex_vis.h libterm/libterm.h + ex_vis.h libterm/libterm.h brk_shim.h SRC1 = ex.c ex_addr.c ex_cmds.c ex_cmds2.c ex_cmdsub.c SRC2 = ex_data.c ex_get.c ex_io.c ex_put.c ex_re.c SRC3 = ex_set.c ex_subr.c ex_tagio.c ex_temp.c ex_tty.c ex_unix.c SRC4 = ex_v.c ex_vadj.c ex_vget.c ex_vmain.c ex_voper.c SRC5 = ex_vops.c ex_vops2.c ex_vops3.c ex_vput.c ex_vwind.c SRC6 = printf.c expreserve.c exrecover.c ex_version.c -SRC7 = mapmalloc.c malloc.c +SRC7 = mapmalloc.c brk_shim.c #malloc.c .SUFFIXES: .o .c .c.o: ; $(CC) $(CCFLAGS) -c $< @@ -271,8 +271,8 @@ $(TLIB): libterm/termcap.c libterm/tgoto.c libterm/tputs.c libterm/libterm.h @cd libterm && $(MAKE) CC="$(CC)" \ COPT="$(CFLAGS) $(WARN) $(CPPFLAGS) $(OSTYPE)" -exrecover: exrecover.o $(MALLOC) - $(CC) -o exrecover $(LDFLAGS) exrecover.o $(MALLOC) $(LDADD) +exrecover: exrecover.o $(MALLOC) brk_shim.o + $(CC) -o exrecover $(LDFLAGS) exrecover.o brk_shim.o $(MALLOC) $(LDADD) expreserve: expreserve.o $(CC) -o expreserve $(LDFLAGS) expreserve.o $(LDADD) @@ -403,6 +403,7 @@ ex_vwind.o: libterm/libterm.h expreserve.o: config.h exrecover.o: config.h ex.h ex_proto.h ex_temp.h ex_tty.h ex_tune.h ex_vars.h exrecover.o: libterm/libterm.h -malloc.o: config.h +# malloc.o: config.h mapmalloc.o: config.h printf.o: config.h +brk_shim.o: brk_shim.h diff --git a/brk_shim.c b/brk_shim.c new file mode 100644 index 0000000..1680a18 --- /dev/null +++ b/brk_shim.c @@ -0,0 +1,44 @@ +#include "brk_shim.h" + +#include + +/* + * The idea is to just allocate a large buffer and rely on lazy page + * loading to not use more physical memory than necessary. + */ + +#ifdef LARGEF +#define SHIMSIZE 0x4000000 +/* 64MB */ +#else +#define SHIMSIZE 0x200000 +/* 2MB */ +#endif + +static char shim_space[SHIMSIZE]; +static intptr_t shim_size = 0; + +void *shim_sbrk(intptr_t increment) { + void* current = (void*)(shim_space + shim_size); + shim_size += increment; + if (shim_size < 0) { + shim_size = 0; + return (void*)-1; + } else if (shim_size > SHIMSIZE) { + shim_size = SHIMSIZE; + return (void*)-1; + } + return current; +} + +int shim_brk(void* addr) { + if (addr < (void*)shim_space || + addr > (void*)(shim_space + SHIMSIZE)) { + errno = EINVAL; + return -1; + } + shim_size = ((void*)shim_space) - addr; + return 0; +} + + diff --git a/brk_shim.h b/brk_shim.h new file mode 100644 index 0000000..3e81758 --- /dev/null +++ b/brk_shim.h @@ -0,0 +1,4 @@ +#include + +int shim_brk(void *addr); +void *shim_sbrk(intptr_t increment); diff --git a/ex.c b/ex.c index a6b5121..caf88f2 100644 --- a/ex.c +++ b/ex.c @@ -87,6 +87,7 @@ static char sccsid[] = "@(#)ex.c 1.37 (gritter) 8/4/05"; #include "ex_argv.h" #include "ex_temp.h" #include "ex_tty.h" +#include "brk_shim.h" #ifdef TRACE char tttrace[] = { '/','d','e','v','/','t','t','y','x','x',0 }; @@ -529,7 +530,7 @@ argend: * this as ed does, saving a little core, but it will probably * not often make much difference. */ - fendcore = (line *) sbrk(0); + fendcore = (line *) shim_sbrk(0); endcore = fendcore - 2; #ifdef SIGTSTP diff --git a/ex_re.c b/ex_re.c index 653a8bf..99d4ad8 100644 --- a/ex_re.c +++ b/ex_re.c @@ -84,7 +84,7 @@ static char sccsid[] = "@(#)ex_re.c 1.60 (gritter) 8/6/05"; #ifdef UXRE -#include "regex.h" +#include "uxre_regex.h" char *braslist[NBRA]; char *braelist[NBRA]; diff --git a/ex_subr.c b/ex_subr.c index 663a78e..3d9c090 100644 --- a/ex_subr.c +++ b/ex_subr.c @@ -490,12 +490,12 @@ morelines(void) pg = 4096; pg /= sizeof (line); } - if ((char *)sbrk(pg * sizeof (line)) == (char *)-1) + if ((char *)shim_sbrk(pg * sizeof (line)) == (char *)-1) return (-1); endcore += pg; return (0); #else /* !_SC_PAGESIZE */ - if (sbrk(1024 * sizeof (line)) == (char *)-1) + if (shim_sbrk(1024 * sizeof (line)) == (char *)-1) return (-1); endcore += 1024; return (0); diff --git a/exrecover.c b/exrecover.c index 17ba06f..e2fa677 100644 --- a/exrecover.c +++ b/exrecover.c @@ -106,6 +106,7 @@ extern int vsprintf(char *, const char *, va_list); #include "ex.h" #include "ex_temp.h" #include "ex_tty.h" +#include "brk_shim.h" #include #include @@ -220,7 +221,7 @@ main(int argc, char *argv[]) /* * Initialize as though the editor had just started. */ - fendcore = (line *) sbrk(0); + fendcore = (line *) shim_sbrk(0); dot = zero = dol = fendcore; one = zero + 1; endcore = fendcore - 2; @@ -259,7 +260,7 @@ main(int argc, char *argv[]) /* * Allocate space for the line pointers from the temp file. */ - if ((char *) sbrk(H.Flines * sizeof (line)) == (char *) -1) + if ((char *) shim_sbrk(H.Flines * sizeof (line)) == (char *) -1) /* * Good grief. */ diff --git a/libuxre/re.h b/libuxre/re.h index 64df2d0..9e0241e 100644 --- a/libuxre/re.h +++ b/libuxre/re.h @@ -35,7 +35,7 @@ /* #define __fnm_collate lc_collate */ /* */ #include -#include "regex.h" +#include "uxre_regex.h" /* #include */ #include diff --git a/libuxre/regex.h b/libuxre/regex.h deleted file mode 100644 index 8dbd028..0000000 --- a/libuxre/regex.h +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Changes by Gunnar Ritter, Freiburg i. Br., Germany, November 2002. - * - * Sccsid @(#)regex.h 1.13 (gritter) 2/6/05 - */ -/* UNIX(R) Regular Expresssion Library - * - * Note: Code is released under the GNU LGPL - * - * Copyright (C) 2001 Caldera International, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to: - * Free Software Foundation, Inc. - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef LIBUXRE_REGEX_H -#define LIBUXRE_REGEX_H -/* from unixsrc:usr/src/common/head/regex.h /main/uw7_nj/1 */ - -#include /* really only want [s]size_t */ - - /* - * Official regexec() flags. - */ -#define REG_NOTBOL 0x000001 /* start of string does not match ^ */ -#define REG_NOTEOL 0x000002 /* end of string does not match $ */ - - /* - * Additional regexec() flags. - */ -#define REG_NONEMPTY 0x000004 /* do not match empty at start of string */ - - /* - * Extensions to provide individual control over each - * of the differences between basic and extended REs. - */ -#define REG_OR 0x0000001 /* enable | operator */ -#define REG_PLUS 0x0000002 /* enable + operator */ -#define REG_QUEST 0x0000004 /* enable ? operator */ -#define REG_BRACES 0x0000008 /* use {m,n} (instead of \{m,n\}) */ -#define REG_PARENS 0x0000010 /* use (...) [instead of \(...\)] */ -#define REG_ANCHORS 0x0000020 /* ^ and $ are anchors anywhere */ -#define REG_NOBACKREF 0x0000040 /* disable \digit */ -#define REG_NOAUTOQUOTE 0x0000080 /* no automatic quoting of REG_BADRPTs */ - - /* - * Official regcomp() flags. - */ -#define REG_EXTENDED (REG_OR | REG_PLUS | REG_QUEST | REG_BRACES | \ - REG_PARENS | REG_ANCHORS | \ - REG_NOBACKREF | REG_NOAUTOQUOTE) -#define REG_ICASE 0x0000100 /* ignore case */ -#define REG_NOSUB 0x0000200 /* only success/fail for regexec() */ -#define REG_NEWLINE 0x0000400 /* take \n as line separator for ^ and $ */ - - /* - * Additional regcomp() flags. - * Some of these assume that int is >16 bits! - * Beware: 0x20000000 and above are used in re.h. - */ -#define REG_ONESUB 0x0000800 /* regexec() only needs pmatch[0] */ -#define REG_MTPARENFAIL 0x0001000 /* take empty \(\) or () as match failure */ -#define REG_MTPARENBAD 0x0002000 /* disallow empty \(\) or () */ -#define REG_BADRANGE 0x0004000 /* accept [m-a] ranges as [ma] */ -#define REG_ODDRANGE 0x0008000 /* oawk oddity: [m-a] means [m] */ -#define REG_SEPRANGE 0x0010000 /* disallow [a-m-z] style ranges */ -#define REG_BKTQUOTE 0x0020000 /* allow \ in []s to quote \, -, ^ or ] */ -#define REG_BKTEMPTY 0x0040000 /* allow empty []s (w/BKTQUOTE, BKTESCAPE) */ -#define REG_ANGLES 0x0080000 /* enable \<, \> operators */ -#define REG_ESCNL 0x0100000 /* take \n as newline character */ -#define REG_NLALT 0x0200000 /* take newline as alternation */ -#define REG_ESCSEQ 0x0400000 /* otherwise, take \ as start of C escapes */ -#define REG_BKTESCAPE 0x0800000 /* allow \ in []s to quote next anything */ -#define REG_NOBRACES 0x1000000 /* disable {n,m} */ -#define REG_ADDITIVE 0x2000000 /* a+*b means + and * additive, ^+ is valid */ -#define REG_NOI18N 0x4000000 /* disable I18N features ([::] etc.) */ -#define REG_OLDESC 0x8000000 /* recognize \b \f \n \r \t \123 only */ -#define REG_AVOIDNULL 0x10000000/* avoid null subexpression matches */ -#define REG_OLDBRE (REG_BADRANGE | REG_ANGLES | REG_ESCNL) -#define REG_OLDERE (REG_OR | REG_PLUS | REG_QUEST | REG_NOBRACES | \ - REG_PARENS | REG_ANCHORS | REG_ODDRANGE | \ - REG_NOBACKREF | REG_ADDITIVE | REG_NOAUTOQUOTE) - - /* - * Error return values. - */ -#define REG_ENOSYS (-1) /* unsupported */ -#define REG_NOMATCH 1 /* regexec() failed to match */ -#define REG_BADPAT 2 /* invalid regular expression */ -#define REG_ECOLLATE 3 /* invalid collating element construct */ -#define REG_ECTYPE 4 /* invalid character class construct */ -#define REG_EEQUIV 5 /* invalid equivalence class construct */ -#define REG_EBKTCHAR 6 /* invalid character in [] construct */ -#define REG_EESCAPE 7 /* trailing \ in pattern */ -#define REG_ESUBREG 8 /* number in \digit invalid or in error */ -#define REG_EBRACK 9 /* [] imbalance */ -#define REG_EMPTYSUBBKT 10 /* empty sub-bracket construct */ -#define REG_EMPTYPAREN 11 /* empty \(\) or () [REG_MTPARENBAD] */ -#define REG_NOPAT 12 /* no (empty) pattern */ -#define REG_EPAREN 13 /* \(\) or () imbalance */ -#define REG_EBRACE 14 /* \{\} or {} imbalance */ -#define REG_BADBR 15 /* contents of \{\} or {} invalid */ -#define REG_ERANGE 16 /* invalid endpoint in expression */ -#define REG_ESPACE 17 /* out of memory */ -#define REG_BADRPT 18 /* *,+,?,\{\} or {} not after r.e. */ -#define REG_BADESC 19 /* invalid escape sequence (e.g. \0) */ -#define REG_ILLSEQ 20 /* illegal byte sequence */ - -typedef struct -{ - size_t re_nsub; /* only advertised member */ - unsigned long re_flags; /* augmented regcomp() flags */ - struct re_dfa_ *re_dfa; /* DFA engine */ - struct re_nfa_ *re_nfa; /* NFA engine */ - struct re_coll_ *re_col; /* current collation info */ - int re_mb_cur_max; /* MB_CUR_MAX acceleration */ - void *re_more; /* just in case... */ -} regex_t; - -typedef ssize_t regoff_t; - -typedef struct -{ - regoff_t rm_so; - regoff_t rm_eo; -} regmatch_t; - -#ifdef __cplusplus -extern "C" { -#endif - -int regcomp(regex_t *, const char *, int); -int regexec(const regex_t *, const char *, size_t, regmatch_t *, int); -size_t regerror(int, const regex_t *, char *, size_t); -void regfree(regex_t *); - -#ifdef __cplusplus -} -#endif - -#endif /* !LIBUXRE_REGEX_H */ diff --git a/libuxre/uxre_regex.h b/libuxre/uxre_regex.h new file mode 100644 index 0000000..068c1f9 --- /dev/null +++ b/libuxre/uxre_regex.h @@ -0,0 +1,152 @@ +/* + * Changes by Gunnar Ritter, Freiburg i. Br., Germany, November 2002. + * + * Sccsid @(#)regex.h 1.13 (gritter) 2/6/05 + */ +/* UNIX(R) Regular Expresssion Library + * + * Note: Code is released under the GNU LGPL + * + * Copyright (C) 2001 Caldera International, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to: + * Free Software Foundation, Inc. + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef LIBUXRE_REGEX_H +#define LIBUXRE_REGEX_H +/* from unixsrc:usr/src/common/head/regex.h /main/uw7_nj/1 */ + +#include /* really only want [s]size_t */ + + /* + * Official regexec() flags. + */ +#define REG_NOTBOL 0x000001 /* start of string does not match ^ */ +#define REG_NOTEOL 0x000002 /* end of string does not match $ */ + + /* + * Additional regexec() flags. + */ +#define REG_NONEMPTY 0x000004 /* do not match empty at start of string */ + + /* + * Extensions to provide individual control over each + * of the differences between basic and extended REs. + */ +#define REG_OR 0x0000001 /* enable | operator */ +#define REG_PLUS 0x0000002 /* enable + operator */ +#define REG_QUEST 0x0000004 /* enable ? operator */ +#define REG_BRACES 0x0000008 /* use {m,n} (instead of \{m,n\}) */ +#define REG_PARENS 0x0000010 /* use (...) [instead of \(...\)] */ +#define REG_ANCHORS 0x0000020 /* ^ and $ are anchors anywhere */ +#define REG_NOBACKREF 0x0000040 /* disable \digit */ +#define REG_NOAUTOQUOTE 0x0000080 /* no automatic quoting of REG_BADRPTs */ + + /* + * Official regcomp() flags. + */ +#define REG_EXTENDED (REG_OR | REG_PLUS | REG_QUEST | REG_BRACES | \ + REG_PARENS | REG_ANCHORS | \ + REG_NOBACKREF | REG_NOAUTOQUOTE) +#define REG_ICASE 0x0000100 /* ignore case */ +#define REG_NOSUB 0x0000200 /* only success/fail for regexec() */ +#define REG_NEWLINE 0x0000400 /* take \n as line separator for ^ and $ */ + + /* + * Additional regcomp() flags. + * Some of these assume that int is >16 bits! + * Beware: 0x20000000 and above are used in re.h. + */ +#define REG_ONESUB 0x0000800 /* regexec() only needs pmatch[0] */ +#define REG_MTPARENFAIL 0x0001000 /* take empty \(\) or () as match failure */ +#define REG_MTPARENBAD 0x0002000 /* disallow empty \(\) or () */ +#define REG_BADRANGE 0x0004000 /* accept [m-a] ranges as [ma] */ +#define REG_ODDRANGE 0x0008000 /* oawk oddity: [m-a] means [m] */ +#define REG_SEPRANGE 0x0010000 /* disallow [a-m-z] style ranges */ +#define REG_BKTQUOTE 0x0020000 /* allow \ in []s to quote \, -, ^ or ] */ +#define REG_BKTEMPTY 0x0040000 /* allow empty []s (w/BKTQUOTE, BKTESCAPE) */ +#define REG_ANGLES 0x0080000 /* enable \<, \> operators */ +#define REG_ESCNL 0x0100000 /* take \n as newline character */ +#define REG_NLALT 0x0200000 /* take newline as alternation */ +#define REG_ESCSEQ 0x0400000 /* otherwise, take \ as start of C escapes */ +#define REG_BKTESCAPE 0x0800000 /* allow \ in []s to quote next anything */ +#define REG_NOBRACES 0x1000000 /* disable {n,m} */ +#define REG_ADDITIVE 0x2000000 /* a+*b means + and * additive, ^+ is valid */ +#define REG_NOI18N 0x4000000 /* disable I18N features ([::] etc.) */ +#define REG_OLDESC 0x8000000 /* recognize \b \f \n \r \t \123 only */ +#define REG_AVOIDNULL 0x10000000/* avoid null subexpression matches */ +#define REG_OLDBRE (REG_BADRANGE | REG_ANGLES | REG_ESCNL) +#define REG_OLDERE (REG_OR | REG_PLUS | REG_QUEST | REG_NOBRACES | \ + REG_PARENS | REG_ANCHORS | REG_ODDRANGE | \ + REG_NOBACKREF | REG_ADDITIVE | REG_NOAUTOQUOTE) + + /* + * Error return values. + */ +#define REG_ENOSYS (-1) /* unsupported */ +#define REG_NOMATCH 1 /* regexec() failed to match */ +#define REG_BADPAT 2 /* invalid regular expression */ +#define REG_ECOLLATE 3 /* invalid collating element construct */ +#define REG_ECTYPE 4 /* invalid character class construct */ +#define REG_EEQUIV 5 /* invalid equivalence class construct */ +#define REG_EBKTCHAR 6 /* invalid character in [] construct */ +#define REG_EESCAPE 7 /* trailing \ in pattern */ +#define REG_ESUBREG 8 /* number in \digit invalid or in error */ +#define REG_EBRACK 9 /* [] imbalance */ +#define REG_EMPTYSUBBKT 10 /* empty sub-bracket construct */ +#define REG_EMPTYPAREN 11 /* empty \(\) or () [REG_MTPARENBAD] */ +#define REG_NOPAT 12 /* no (empty) pattern */ +#define REG_EPAREN 13 /* \(\) or () imbalance */ +#define REG_EBRACE 14 /* \{\} or {} imbalance */ +#define REG_BADBR 15 /* contents of \{\} or {} invalid */ +#define REG_ERANGE 16 /* invalid endpoint in expression */ +#define REG_ESPACE 17 /* out of memory */ +#define REG_BADRPT 18 /* *,+,?,\{\} or {} not after r.e. */ +#define REG_BADESC 19 /* invalid escape sequence (e.g. \0) */ +#define REG_ILLSEQ 20 /* illegal byte sequence */ + +typedef struct +{ + size_t re_nsub; /* only advertised member */ + unsigned long re_flags; /* augmented regcomp() flags */ + struct re_dfa_ *re_dfa; /* DFA engine */ + struct re_nfa_ *re_nfa; /* NFA engine */ + struct re_coll_ *re_col; /* current collation info */ + int re_mb_cur_max; /* MB_CUR_MAX acceleration */ + void *re_more; /* just in case... */ +} regex_t; + +typedef ssize_t regoff_t; + +typedef struct +{ + regoff_t rm_so; + regoff_t rm_eo; +} regmatch_t; + +#ifdef __cplusplus +extern "C" { +#endif + +int regcomp(regex_t *, const char *, int); +int regexec(const regex_t *, const char *, size_t, regmatch_t *, int); +size_t regerror(int, const regex_t *, char *, size_t); +void regfree(regex_t *); + +#ifdef __cplusplus +} +#endif + +#endif /* !LIBUXRE_REGEX_H */ -- cgit v1.2.3