From 1312f602450da58f31a8ca81322257ec61f584b9 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 15 Nov 2024 21:19:08 -0800 Subject: [PATCH] Strongly link tr and sed into system() and popen() --- Makefile | 8 ++++---- libc/system/BUILD.mk | 2 ++ libc/system/cocmd.c | 13 +++++-------- libc/system/system.c | 4 +--- third_party/sed/defs.h | 1 + third_party/sed/extern.h | 1 + third_party/sed/shade.h | 31 +++++++++++++++++++++++++++++++ third_party/tr/extern.h | 2 ++ third_party/tr/tr.c | 11 ++++------- 9 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 third_party/sed/shade.h diff --git a/Makefile b/Makefile index a2f9cf4850c..c29c238aba0 100644 --- a/Makefile +++ b/Makefile @@ -274,6 +274,9 @@ include libc/BUILD.mk #─┘ include libc/sock/BUILD.mk #─┐ include net/http/BUILD.mk # ├──ONLINE RUNTIME include third_party/musl/BUILD.mk # │ You can communicate with the network +include third_party/regex/BUILD.mk # │ +include third_party/tr/BUILD.mk # │ +include third_party/sed/BUILD.mk # │ include libc/system/BUILD.mk # │ include libc/x/BUILD.mk # │ include dsp/scale/BUILD.mk # │ @@ -294,8 +297,7 @@ include third_party/libcxx/BUILD.mk # │ include third_party/openmp/BUILD.mk # │ include third_party/pcre/BUILD.mk # │ include third_party/less/BUILD.mk # │ -include net/https/BUILD.mk # │ -include third_party/regex/BUILD.mk #─┘ +include net/https/BUILD.mk #─┘ include third_party/tidy/BUILD.mk include third_party/BUILD.mk include third_party/nsync/testing/BUILD.mk @@ -314,8 +316,6 @@ include third_party/double-conversion/test/BUILD.mk include third_party/lua/BUILD.mk include third_party/tree/BUILD.mk include third_party/zstd/BUILD.mk -include third_party/tr/BUILD.mk -include third_party/sed/BUILD.mk include third_party/awk/BUILD.mk include third_party/hiredis/BUILD.mk include third_party/make/BUILD.mk diff --git a/libc/system/BUILD.mk b/libc/system/BUILD.mk index 75a43ea401b..00d4aaae923 100644 --- a/libc/system/BUILD.mk +++ b/libc/system/BUILD.mk @@ -35,6 +35,8 @@ LIBC_SYSTEM_A_DIRECTDEPS = \ LIBC_STR \ LIBC_SYSV \ THIRD_PARTY_MUSL \ + THIRD_PARTY_SED \ + THIRD_PARTY_TR \ LIBC_SYSTEM_A_DEPS := \ $(call uniq,$(foreach x,$(LIBC_SYSTEM_A_DIRECTDEPS),$($(x)))) diff --git a/libc/system/cocmd.c b/libc/system/cocmd.c index 3eebcf30ebd..36bbbf5bad0 100644 --- a/libc/system/cocmd.c +++ b/libc/system/cocmd.c @@ -743,15 +743,12 @@ static int TryBuiltin(bool wantexec) { return Usleep(); if (!strcmp(args[0], "toupper")) return Toupper(); - if (_weaken(_tr) && !strcmp(args[0], "tr")) { - return Fake(_weaken(_tr), wantexec); - } - if (_weaken(_sed) && !strcmp(args[0], "sed")) { - return Fake(_weaken(_sed), wantexec); - } - if (_weaken(_awk) && !strcmp(args[0], "awk")) { + if (!strcmp(args[0], "tr")) + return Fake(_tr, wantexec); + if (!strcmp(args[0], "sed")) + return Fake(_sed, wantexec); + if (_weaken(_awk) && strcmp(args[0], "awk")) return Fake(_weaken(_awk), wantexec); - } if (_weaken(_curl) && !strcmp(args[0], "curl")) { return Fake(_weaken(_curl), wantexec); } diff --git a/libc/system/system.c b/libc/system/system.c index fddb4a0ddad..6755d1f063d 100644 --- a/libc/system/system.c +++ b/libc/system/system.c @@ -38,9 +38,7 @@ * provides Bourne-like syntax on all platforms, including Windows. Many * builtin commands are included, e.g. exit, cd, rm, [, cat, wait, exec, * env, echo, read, true, test, kill, touch, rmdir, mkdir, false, mktemp - * and usleep. It's also possible to __static_yoink() the symbols `_tr`, - * `_sed`, `_awk`, and `_curl` for the tr, sed, awk and curl commands if - * you're using the Cosmopolitan mono-repo. + * sed, tr, and usleep. * * If you just have a program name and arguments, and you don't need the * full power of a UNIX-like shell, then consider using the Cosmopolitan diff --git a/third_party/sed/defs.h b/third_party/sed/defs.h index 54ac79922b0..84cba233742 100644 --- a/third_party/sed/defs.h +++ b/third_party/sed/defs.h @@ -3,6 +3,7 @@ #include "libc/calls/typedef/u.h" #include "libc/limits.h" #include "third_party/regex/regex.h" +#include "third_party/sed/shade.h" COSMOPOLITAN_C_START_ /* diff --git a/third_party/sed/extern.h b/third_party/sed/extern.h index 0c190c9bc6a..fbeb0497a57 100644 --- a/third_party/sed/extern.h +++ b/third_party/sed/extern.h @@ -4,6 +4,7 @@ #include "libc/stdio/stdio.h" #include "third_party/regex/regex.h" #include "third_party/sed/defs.h" +#include "third_party/sed/shade.h" COSMOPOLITAN_C_START_ extern struct s_command *prog; diff --git a/third_party/sed/shade.h b/third_party/sed/shade.h new file mode 100644 index 00000000000..ceea8d4b545 --- /dev/null +++ b/third_party/sed/shade.h @@ -0,0 +1,31 @@ +#ifndef COSMOPOLITAN_THIRD_PARTY_SED_SHADE_H_ +#define COSMOPOLITAN_THIRD_PARTY_SED_SHADE_H_ + +#define prog _sed_prog +#define appends_ _sed_appends_ +#define g_match _sed_g_match +#define maxnsub _sed_maxnsub +#define linenum _sed_linenum +#define appendnum _sed_appendnum +#define aflag _sed_aflag +#define eflag _sed_eflag +#define nflag _sed_nflag +#define fname _sed_fname +#define outfname _sed_outfname +#define infile _sed_infile +#define outfile _sed_outfile +#define rflags _sed_rflags +#define cfclose _sed_cfclose +#define compile _sed_compile +#define cspace _sed_cspace +#define cu_fgets _sed_cu_fgets +#define mf_fgets _sed_mf_fgets +#define lastline _sed_lastline +#define process _sed_process +#define resetstate _sed_resetstate +#define strregerror _sed_strregerror +#define xmalloc _sed_xmalloc +#define xrealloc _sed_xrealloc +#define xcalloc _sed_xcalloc + +#endif /* COSMOPOLITAN_THIRD_PARTY_SED_SHADE_H_ */ diff --git a/third_party/tr/extern.h b/third_party/tr/extern.h index 31b1ed88445..14995931e6e 100644 --- a/third_party/tr/extern.h +++ b/third_party/tr/extern.h @@ -3,6 +3,8 @@ #include "libc/limits.h" COSMOPOLITAN_C_START_ +#define next _tr_next + typedef struct { enum { STRING1, STRING2 } which; enum { EOS, INFINITE, NORMAL, RANGE, SEQUENCE, SET } state; diff --git a/third_party/tr/tr.c b/third_party/tr/tr.c index fd1fe6e1100..d9c7f572a4a 100644 --- a/third_party/tr/tr.c +++ b/third_party/tr/tr.c @@ -42,8 +42,8 @@ #include "third_party/tr/cmd.h" #include "third_party/tr/extern.h" -int delete[NCHARS], squeeze[NCHARS]; -int translate[NCHARS] = { +static int delete[NCHARS], squeeze[NCHARS]; +static int translate[NCHARS] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ASCII */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, @@ -78,8 +78,8 @@ int translate[NCHARS] = { 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, }; -STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL }; -STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL }; +static STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL }; +static STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL }; static void setup(int *, char *, STR *, int); static void usage(void); @@ -90,9 +90,6 @@ _tr(int argc, char *argv[]) int ch, cnt, lastch, *p; int cflag, dflag, sflag; - if (pledge("stdio", NULL) == -1) - err(1, "pledge"); - cflag = dflag = sflag = 0; while ((ch = getopt(argc, argv, "Ccds")) != -1) switch(ch) {