Make cli commands yoinkable into cosmo shell

This lets our system() and popen() commands function sort of like
BusyBox and ToyBox. By default the Cosmopolitan Shell is lightweight.
But if you use STATIC_YOINK then you can pull the individual commands
you want into the linkage, and they'll be included in a single binary.
For example the demo binary embeds `tr` and `sed` and ends up ~140kb.
This commit is contained in:
Justine Tunney 2022-10-14 16:37:29 -07:00
parent 5af19b7eed
commit 3d2d04b25e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
32 changed files with 897 additions and 276 deletions

23
third_party/sed/cmd.c vendored Normal file
View file

@ -0,0 +1,23 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "third_party/sed/cmd.h"
int main(int argc, char *argv[]) {
return _sed(argc, argv);
}

10
third_party/sed/cmd.h vendored Normal file
View file

@ -0,0 +1,10 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_SED_CMD_H_
#define COSMOPOLITAN_THIRD_PARTY_SED_CMD_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int _sed(int, char *[]);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_SED_CMD_H_ */

View file

@ -165,7 +165,7 @@ compile(void)
uselabel();
if (appendnum > 0)
appends = xmalloc(sizeof(struct s_appends) * appendnum);
match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
g_match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
}
#define EATSPACE() do { \

View file

@ -10,7 +10,7 @@ COSMOPOLITAN_C_START_
extern struct s_command *prog;
extern struct s_appends *appends;
extern regmatch_t *match;
extern regmatch_t *g_match;
extern size_t maxnsub;
extern u_long linenum;
extern size_t appendnum;

View file

@ -1,6 +1,4 @@
// clang-format off
/* $NetBSD: main.c,v 1.36 2020/05/15 22:39:54 christos Exp $ */
/*-
* Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson.
* Copyright (c) 1992 Diomidis Spinellis.
@ -34,85 +32,20 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "libc/calls/makedev.h"
#include "libc/calls/weirdtypes.h"
#include "libc/intrin/newbie.h"
#include "libc/calls/typedef/u.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sock/select.h"
#include "libc/sysv/consts/endian.h"
#include "libc/calls/calls.h"
#include "libc/calls/weirdtypes.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/mlock.h"
#include "libc/sysv/consts/msync.h"
#include "libc/sysv/consts/posix.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/madv.h"
#include "libc/sysv/consts/mfd.h"
#include "libc/sysv/consts/mremap.h"
#include "libc/intrin/newbie.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/rlimit.h"
#include "libc/calls/struct/rusage.h"
#include "libc/calls/sysparam.h"
#include "libc/calls/weirdtypes.h"
#include "libc/limits.h"
#include "libc/sysv/consts/endian.h"
#include "libc/sysv/consts/prio.h"
#include "libc/sysv/consts/rlim.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/sysv/consts/rusage.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/stat.macros.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/consts/utime.h"
#include "libc/calls/calls.h"
#include "libc/runtime/runtime.h"
#include "libc/time/time.h"
#include "libc/log/bsd.h"
#include "libc/errno.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/fd.h"
#include "libc/sysv/consts/o.h"
// MISSING #include <libgen.h>
#include "libc/limits.h"
#include "libc/sysv/consts/_posix.h"
#include "libc/str/locale.h"
#include "third_party/regex/regex.h"
#define _WITH_GETLINE
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/stdio/lock.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/temp.h"
#include "libc/mem/alg.h"
#include "libc/fmt/conv.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/log/bsd.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/temp.h"
#include "libc/sysv/consts/exit.h"
#include "third_party/gdtoa/gdtoa.h"
#include "libc/mem/alg.h"
#include "libc/stdio/stdio.h"
#include "libc/str/locale.h"
#include "libc/str/str.h"
#include "libc/calls/calls.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "libc/sysv/consts/s.h"
#include "third_party/getopt/getopt.h"
#include "third_party/sed/cmd.h"
#include "third_party/sed/defs.h"
#include "third_party/sed/extern.h"
// clang-format off
/*
* Linked list of units (strings and files) to be compiled
@ -169,7 +102,7 @@ static void add_file(char *);
static void usage(void) wontreturn;
int
main(int argc, char *argv[])
_sed(int argc, char *argv[])
{
int c, fflag;
char *temp_arg;
@ -260,7 +193,7 @@ main(int argc, char *argv[])
static void
usage(void)
{
(void)fprintf(stderr,
(void)(fprintf)(stderr,
"Usage: %s [-aElnru] command [file ...]\n"
"\t%s [-aElnru] [-e command] [-f command_file] [-I[extension]]\n"
"\t [-i[extension]] [file ...]\n", program_invocation_name,

View file

@ -1,6 +1,4 @@
// clang-format off
/* $NetBSD: misc.c,v 1.15 2014/06/26 02:14:32 christos Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993
@ -33,37 +31,10 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "libc/calls/makedev.h"
#include "libc/calls/weirdtypes.h"
#include "libc/intrin/newbie.h"
#include "libc/calls/typedef/u.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sock/select.h"
#include "libc/sysv/consts/endian.h"
#include "libc/log/bsd.h"
#include "libc/limits.h"
#include "libc/sysv/consts/_posix.h"
#include "third_party/regex/regex.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/stdio/lock.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/temp.h"
#include "libc/mem/alg.h"
#include "libc/fmt/conv.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/temp.h"
#include "libc/sysv/consts/exit.h"
#include "third_party/gdtoa/gdtoa.h"
#include "libc/mem/alg.h"
#include "libc/str/str.h"
#include "third_party/sed/defs.h"
#include "third_party/sed/extern.h"
// clang-format off
/*
* malloc with result test
@ -104,6 +75,7 @@ xcalloc(size_t c, size_t n)
err(1, "calloc(%zu, %zu)", c, n);
return p;
}
/*
* Return a string for a regular expression error passed. This is overkill,
* because of the silly semantics of regerror (we can never know the size of

View file

@ -1,6 +1,4 @@
// clang-format off
/* $NetBSD: process.c,v 1.53 2020/05/15 22:39:54 christos Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993, 1994
@ -33,72 +31,25 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "libc/calls/makedev.h"
#include "libc/calls/weirdtypes.h"
#include "libc/intrin/newbie.h"
#include "libc/calls/typedef/u.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sock/select.h"
#include "libc/sysv/consts/endian.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/stat.macros.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/consts/utime.h"
#include "libc/time/time.h"
#include "libc/calls/calls.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/winsize.h"
#include "libc/sysv/consts/fd.h"
#include "libc/sysv/consts/fio.h"
#include "libc/sysv/consts/modem.h"
#include "libc/sysv/consts/pty.h"
#include "libc/sysv/consts/sio.h"
#include "libc/sysv/consts/termios.h"
#include "libc/str/unicode.h"
#include "libc/mem/gc.internal.h"
#include "libc/calls/calls.h"
#include "libc/str/str.h"
#include "libc/log/bsd.h"
#include "libc/errno.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/fd.h"
#include "libc/sysv/consts/o.h"
#include "libc/limits.h"
#include "libc/sysv/consts/_posix.h"
#include "third_party/regex/regex.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/stdio/lock.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/temp.h"
#include "libc/mem/alg.h"
#include "libc/fmt/conv.h"
#include "libc/log/bsd.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/temp.h"
#include "libc/sysv/consts/exit.h"
#include "third_party/gdtoa/gdtoa.h"
#include "libc/mem/alg.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/calls/calls.h"
#include "libc/calls/weirdtypes.h"
#include "libc/str/unicode.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "third_party/getopt/getopt.h"
#include "libc/str/str.h"
#include "libc/time/time.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/consts/termios.h"
#include "third_party/sed/defs.h"
#include "third_party/sed/extern.h"
// clang-format off
static SPACE HS, PS, SS, YS;
#define pd PS.deleted
@ -107,6 +58,7 @@ static SPACE HS, PS, SS, YS;
#define psanl PS.append_newline
#define hs HS.space
#define hsl HS.len
#define match g_match
static __inline int applies(struct s_command *);
static void do_tr(struct s_tr *);

View file

@ -3,20 +3,13 @@
PKGS += THIRD_PARTY_SED
THIRD_PARTY_SED_SRCS = $(THIRD_PARTY_SED_A_SRCS)
THIRD_PARTY_SED_HDRS = $(THIRD_PARTY_SED_A_HDRS)
THIRD_PARTY_SED_INCS = $(THIRD_PARTY_SED_A_INCS)
THIRD_PARTY_SED_BINS = $(THIRD_PARTY_SED_COMS) $(THIRD_PARTY_SED_COMS:%=%.dbg)
THIRD_PARTY_SED_COMS = o/$(MODE)/third_party/sed/sed.com
THIRD_PARTY_SED_ARTIFACTS += THIRD_PARTY_SED_A
THIRD_PARTY_SED = $(THIRD_PARTY_SED_A_DEPS) $(THIRD_PARTY_SED_A)
THIRD_PARTY_SED_A = o/$(MODE)/third_party/sed/sed.a
THIRD_PARTY_SED_A_FILES := $(wildcard third_party/sed/*)
THIRD_PARTY_SED_A_HDRS = $(filter %.h,$(THIRD_PARTY_SED_A_FILES))
THIRD_PARTY_SED_A_INCS = $(filter %.inc,$(THIRD_PARTY_SED_A_FILES))
THIRD_PARTY_SED_A_SRCS = $(filter %.c,$(THIRD_PARTY_SED_A_FILES))
THIRD_PARTY_SED_A_OBJS = $(THIRD_PARTY_SED_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_SED_FILES := $(wildcard third_party/sed/*)
THIRD_PARTY_SED_HDRS = $(filter %.h,$(THIRD_PARTY_SED_FILES))
THIRD_PARTY_SED_INCS = $(filter %.inc,$(THIRD_PARTY_SED_FILES))
THIRD_PARTY_SED_SRCS = $(filter %.c,$(THIRD_PARTY_SED_FILES))
THIRD_PARTY_SED_OBJS = $(THIRD_PARTY_SED_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_SED_A_DIRECTDEPS = \
LIBC_FMT \
@ -36,30 +29,29 @@ THIRD_PARTY_SED_A_DIRECTDEPS = \
THIRD_PARTY_SED_A_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_SED_A_DIRECTDEPS),$($(x))))
THIRD_PARTY_SED_A_CHECKS = \
THIRD_PARTY_SED_CHECKS = \
$(THIRD_PARTY_SED_A).pkg \
$(THIRD_PARTY_SED_A_HDRS:%=o/$(MODE)/%.ok)
$(THIRD_PARTY_SED_HDRS:%=o/$(MODE)/%.ok)
$(THIRD_PARTY_SED_A): \
third_party/sed/ \
$(THIRD_PARTY_SED_A).pkg \
$(THIRD_PARTY_SED_A_OBJS)
$(THIRD_PARTY_SED_OBJS)
$(THIRD_PARTY_SED_A).pkg: \
$(THIRD_PARTY_SED_A_OBJS) \
$(THIRD_PARTY_SED_OBJS) \
$(foreach x,$(THIRD_PARTY_SED_A_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/third_party/sed/sed.com.dbg: \
$(THIRD_PARTY_SED) \
o/$(MODE)/third_party/sed/main.o \
o/$(MODE)/third_party/sed/cmd.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
THIRD_PARTY_SED_LIBS = $(foreach x,$(THIRD_PARTY_SED_ARTIFACTS),$($(x)))
THIRD_PARTY_SED_SRCS = $(foreach x,$(THIRD_PARTY_SED_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_SED_CHECKS = $(foreach x,$(THIRD_PARTY_SED_ARTIFACTS),$($(x)_CHECKS))
THIRD_PARTY_SED_OBJS = $(foreach x,$(THIRD_PARTY_SED_ARTIFACTS),$($(x)_OBJS))
THIRD_PARTY_SED_BINS = $(THIRD_PARTY_SED_COMS) $(THIRD_PARTY_SED_COMS:%=%.dbg)
THIRD_PARTY_SED_COMS = o/$(MODE)/third_party/sed/sed.com
THIRD_PARTY_SED_LIBS = $(THIRD_PARTY_SED_A)
$(THIRD_PARTY_SED_OBJS): $(BUILD_FILES) third_party/sed/sed.mk
.PHONY: o/$(MODE)/third_party/sed