Skip to content

Commit 1312f60

Browse files
committed
Strongly link tr and sed into system() and popen()
1 parent cafdb45 commit 1312f60

File tree

9 files changed

+51
-22
lines changed

9 files changed

+51
-22
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ include libc/BUILD.mk #─┘
274274
include libc/sock/BUILD.mk #─┐
275275
include net/http/BUILD.mk # ├──ONLINE RUNTIME
276276
include third_party/musl/BUILD.mk # │ You can communicate with the network
277+
include third_party/regex/BUILD.mk #
278+
include third_party/tr/BUILD.mk #
279+
include third_party/sed/BUILD.mk #
277280
include libc/system/BUILD.mk #
278281
include libc/x/BUILD.mk #
279282
include dsp/scale/BUILD.mk #
@@ -294,8 +297,7 @@ include third_party/libcxx/BUILD.mk # │
294297
include third_party/openmp/BUILD.mk #
295298
include third_party/pcre/BUILD.mk #
296299
include third_party/less/BUILD.mk #
297-
include net/https/BUILD.mk #
298-
include third_party/regex/BUILD.mk #─┘
300+
include net/https/BUILD.mk #─┘
299301
include third_party/tidy/BUILD.mk
300302
include third_party/BUILD.mk
301303
include third_party/nsync/testing/BUILD.mk
@@ -314,8 +316,6 @@ include third_party/double-conversion/test/BUILD.mk
314316
include third_party/lua/BUILD.mk
315317
include third_party/tree/BUILD.mk
316318
include third_party/zstd/BUILD.mk
317-
include third_party/tr/BUILD.mk
318-
include third_party/sed/BUILD.mk
319319
include third_party/awk/BUILD.mk
320320
include third_party/hiredis/BUILD.mk
321321
include third_party/make/BUILD.mk

libc/system/BUILD.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ LIBC_SYSTEM_A_DIRECTDEPS = \
3535
LIBC_STR \
3636
LIBC_SYSV \
3737
THIRD_PARTY_MUSL \
38+
THIRD_PARTY_SED \
39+
THIRD_PARTY_TR \
3840

3941
LIBC_SYSTEM_A_DEPS := \
4042
$(call uniq,$(foreach x,$(LIBC_SYSTEM_A_DIRECTDEPS),$($(x))))

libc/system/cocmd.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -743,15 +743,12 @@ static int TryBuiltin(bool wantexec) {
743743
return Usleep();
744744
if (!strcmp(args[0], "toupper"))
745745
return Toupper();
746-
if (_weaken(_tr) && !strcmp(args[0], "tr")) {
747-
return Fake(_weaken(_tr), wantexec);
748-
}
749-
if (_weaken(_sed) && !strcmp(args[0], "sed")) {
750-
return Fake(_weaken(_sed), wantexec);
751-
}
752-
if (_weaken(_awk) && !strcmp(args[0], "awk")) {
746+
if (!strcmp(args[0], "tr"))
747+
return Fake(_tr, wantexec);
748+
if (!strcmp(args[0], "sed"))
749+
return Fake(_sed, wantexec);
750+
if (_weaken(_awk) && strcmp(args[0], "awk"))
753751
return Fake(_weaken(_awk), wantexec);
754-
}
755752
if (_weaken(_curl) && !strcmp(args[0], "curl")) {
756753
return Fake(_weaken(_curl), wantexec);
757754
}

libc/system/system.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
* provides Bourne-like syntax on all platforms, including Windows. Many
3939
* builtin commands are included, e.g. exit, cd, rm, [, cat, wait, exec,
4040
* env, echo, read, true, test, kill, touch, rmdir, mkdir, false, mktemp
41-
* and usleep. It's also possible to __static_yoink() the symbols `_tr`,
42-
* `_sed`, `_awk`, and `_curl` for the tr, sed, awk and curl commands if
43-
* you're using the Cosmopolitan mono-repo.
41+
* sed, tr, and usleep.
4442
*
4543
* If you just have a program name and arguments, and you don't need the
4644
* full power of a UNIX-like shell, then consider using the Cosmopolitan

third_party/sed/defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "libc/calls/typedef/u.h"
44
#include "libc/limits.h"
55
#include "third_party/regex/regex.h"
6+
#include "third_party/sed/shade.h"
67
COSMOPOLITAN_C_START_
78

89
/*

third_party/sed/extern.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "libc/stdio/stdio.h"
55
#include "third_party/regex/regex.h"
66
#include "third_party/sed/defs.h"
7+
#include "third_party/sed/shade.h"
78
COSMOPOLITAN_C_START_
89

910
extern struct s_command *prog;

third_party/sed/shade.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef COSMOPOLITAN_THIRD_PARTY_SED_SHADE_H_
2+
#define COSMOPOLITAN_THIRD_PARTY_SED_SHADE_H_
3+
4+
#define prog _sed_prog
5+
#define appends_ _sed_appends_
6+
#define g_match _sed_g_match
7+
#define maxnsub _sed_maxnsub
8+
#define linenum _sed_linenum
9+
#define appendnum _sed_appendnum
10+
#define aflag _sed_aflag
11+
#define eflag _sed_eflag
12+
#define nflag _sed_nflag
13+
#define fname _sed_fname
14+
#define outfname _sed_outfname
15+
#define infile _sed_infile
16+
#define outfile _sed_outfile
17+
#define rflags _sed_rflags
18+
#define cfclose _sed_cfclose
19+
#define compile _sed_compile
20+
#define cspace _sed_cspace
21+
#define cu_fgets _sed_cu_fgets
22+
#define mf_fgets _sed_mf_fgets
23+
#define lastline _sed_lastline
24+
#define process _sed_process
25+
#define resetstate _sed_resetstate
26+
#define strregerror _sed_strregerror
27+
#define xmalloc _sed_xmalloc
28+
#define xrealloc _sed_xrealloc
29+
#define xcalloc _sed_xcalloc
30+
31+
#endif /* COSMOPOLITAN_THIRD_PARTY_SED_SHADE_H_ */

third_party/tr/extern.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "libc/limits.h"
44
COSMOPOLITAN_C_START_
55

6+
#define next _tr_next
7+
68
typedef struct {
79
enum { STRING1, STRING2 } which;
810
enum { EOS, INFINITE, NORMAL, RANGE, SEQUENCE, SET } state;

third_party/tr/tr.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
#include "third_party/tr/cmd.h"
4343
#include "third_party/tr/extern.h"
4444

45-
int delete[NCHARS], squeeze[NCHARS];
46-
int translate[NCHARS] = {
45+
static int delete[NCHARS], squeeze[NCHARS];
46+
static int translate[NCHARS] = {
4747
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ASCII */
4848
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
4949
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
@@ -78,8 +78,8 @@ int translate[NCHARS] = {
7878
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
7979
};
8080

81-
STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
82-
STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
81+
static STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
82+
static STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
8383

8484
static void setup(int *, char *, STR *, int);
8585
static void usage(void);
@@ -90,9 +90,6 @@ _tr(int argc, char *argv[])
9090
int ch, cnt, lastch, *p;
9191
int cflag, dflag, sflag;
9292

93-
if (pledge("stdio", NULL) == -1)
94-
err(1, "pledge");
95-
9693
cflag = dflag = sflag = 0;
9794
while ((ch = getopt(argc, argv, "Ccds")) != -1)
9895
switch(ch) {

0 commit comments

Comments
 (0)