From f6b3019796bee672fe8569db1198cca7252b2020 Mon Sep 17 00:00:00 2001 From: Ronsor Date: Wed, 21 Dec 2022 16:12:02 -0800 Subject: [PATCH] Make `curl` example into a complete tool (#706) You can now handily use `curl` with popen()/system() by placing `STATIC_YOINK("_curl")` in your main source file. --- Makefile | 1 + libc/runtime/cocmd.c | 2 + tool/curl/cmd.c | 5 +++ tool/curl/cmd.h | 10 +++++ {examples => tool/curl}/curl.c | 6 +-- tool/curl/curl.mk | 74 ++++++++++++++++++++++++++++++++++ tool/net/net.mk | 1 + tool/tool.mk | 1 + 8 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 tool/curl/cmd.c create mode 100644 tool/curl/cmd.h rename {examples => tool/curl}/curl.c (98%) create mode 100644 tool/curl/curl.mk diff --git a/Makefile b/Makefile index bf31c1940..d1d72a8aa 100644 --- a/Makefile +++ b/Makefile @@ -193,6 +193,7 @@ include third_party/python/python.mk include tool/build/emucrt/emucrt.mk include tool/build/emubin/emubin.mk include tool/build/build.mk +include tool/curl/curl.mk include examples/examples.mk include examples/pyapp/pyapp.mk include examples/pylife/pylife.mk diff --git a/libc/runtime/cocmd.c b/libc/runtime/cocmd.c index 3f935d9f8..7039f287d 100644 --- a/libc/runtime/cocmd.c +++ b/libc/runtime/cocmd.c @@ -38,6 +38,7 @@ #include "third_party/awk/cmd.h" #include "third_party/sed/cmd.h" #include "third_party/tr/cmd.h" +#include "tool/curl/cmd.h" /** * @fileoverview Cosmopolitan Command Interpreter @@ -417,6 +418,7 @@ static int TryBuiltin(void) { if (_weaken(_tr) && !strcmp(args[0], "tr")) return Fake(_weaken(_tr)); if (_weaken(_sed) && !strcmp(args[0], "sed")) return Fake(_weaken(_sed)); if (_weaken(_awk) && !strcmp(args[0], "awk")) return Fake(_weaken(_awk)); + if (_weaken(_curl) && !strcmp(args[0], "curl")) return Fake(_weaken(_curl)); return -1; } diff --git a/tool/curl/cmd.c b/tool/curl/cmd.c new file mode 100644 index 000000000..6cefc2c3e --- /dev/null +++ b/tool/curl/cmd.c @@ -0,0 +1,5 @@ +#include "tool/curl/cmd.h" + +int main(int argc, char *argv[]) { + return _curl(argc, argv); +} diff --git a/tool/curl/cmd.h b/tool/curl/cmd.h new file mode 100644 index 000000000..041aad1c4 --- /dev/null +++ b/tool/curl/cmd.h @@ -0,0 +1,10 @@ +#ifndef COSMOPOLITAN_TOOL_CURL_CMD_H_ +#define COSMOPOLITAN_TOOL_CURL_CMD_H_ +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +int _curl(int, char *[]); + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_TOOL_CURL_CMD_H_ */ diff --git a/examples/curl.c b/tool/curl/curl.c similarity index 98% rename from examples/curl.c rename to tool/curl/curl.c index 88d1c41e4..9de66ad37 100644 --- a/examples/curl.c +++ b/tool/curl/curl.c @@ -52,12 +52,10 @@ #include "third_party/mbedtls/pk.h" #include "third_party/mbedtls/ssl.h" #include "third_party/mbedtls/ssl_ticket.h" +#include "tool/curl/cmd.h" /** * @fileoverview Downloads HTTP URL to stdout. - * - * make -j8 o//examples/curl.com - * o//examples/curl.com http://justine.lol/ape.html */ #define HasHeader(H) (!!msg.headers[H].a) @@ -121,7 +119,7 @@ static wontreturn void PrintUsage(FILE *f, int rc) { exit(rc); } -int main(int argc, char *argv[]) { +int _curl(int argc, char *argv[]) { if (!NoDebug()) ShowCrashReports(); /* diff --git a/tool/curl/curl.mk b/tool/curl/curl.mk new file mode 100644 index 000000000..c76f60979 --- /dev/null +++ b/tool/curl/curl.mk @@ -0,0 +1,74 @@ +#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ +#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ + +PKGS += TOOL_CURL + +TOOL_CURL_ARTIFACTS += TOOL_CURL_A +TOOL_CURL = $(TOOL_CURL_DEPS) $(TOOL_CURL_A) +TOOL_CURL_A = o/$(MODE)/tool/curl/curl.a +TOOL_CURL_FILES := $(wildcard tool/curl/*) +TOOL_CURL_HDRS = $(filter %.h,$(TOOL_CURL_FILES)) +TOOL_CURL_INCS = $(filter %.inc,$(TOOL_CURL_FILES)) +TOOL_CURL_SRCS = $(filter %.c,$(TOOL_CURL_FILES)) +TOOL_CURL_OBJS = $(TOOL_CURL_SRCS:%.c=o/$(MODE)/%.o) + +TOOL_CURL_DIRECTDEPS = \ + LIBC_CALLS \ + LIBC_DNS \ + LIBC_FMT \ + LIBC_INTRIN \ + LIBC_LOG \ + LIBC_MEM \ + LIBC_NEXGEN32E \ + LIBC_NT_IPHLPAPI \ + LIBC_NT_KERNEL32 \ + LIBC_RUNTIME \ + LIBC_SOCK \ + LIBC_STDIO \ + LIBC_STR \ + LIBC_STUBS \ + LIBC_SYSV \ + LIBC_SYSV_CALLS \ + LIBC_TIME \ + LIBC_THREAD \ + LIBC_TINYMATH \ + LIBC_X \ + LIBC_ZIPOS \ + NET_HTTP \ + NET_HTTPS \ + THIRD_PARTY_GETOPT \ + THIRD_PARTY_MBEDTLS + +TOOL_CURL_DEPS := \ + $(call uniq,$(foreach x,$(TOOL_CURL_DIRECTDEPS),$($(x)))) + +TOOL_CURL_CHECKS = \ + $(TOOL_CURL_A).pkg \ + $(TOOL_CURL_HDRS:%=o/$(MODE)/%.ok) + +$(TOOL_CURL_A): \ + tool/curl/ \ + $(TOOL_CURL_A).pkg \ + $(TOOL_CURL_OBJS) + +$(TOOL_CURL_A).pkg: \ + $(TOOL_CURL_OBJS) \ + $(foreach x,$(TOOL_CURL_DIRECTDEPS),$($(x)_A).pkg) + +o/$(MODE)/tool/curl/curl.com.dbg: \ + $(TOOL_CURL) \ + o/$(MODE)/tool/curl/cmd.o \ + o/$(MODE)/tool/curl/curl.o \ + $(CRT) \ + $(APE_NO_MODIFY_SELF) + @$(APELINK) + +TOOL_CURL_LIBS = $(TOOL_CURL_A) +TOOL_CURL_BINS = $(TOOL_CURL_COMS) $(TOOL_CURL_COMS:%=%.dbg) +TOOL_CURL_COMS = o/$(MODE)/tool/curl/curl.com +$(TOOL_CURL_OBJS): $(BUILD_FILES) tool/curl/curl.mk + +.PHONY: o/$(MODE)/tool/curl +o/$(MODE)/tool/curl: \ + $(TOOL_CURL_BINS) \ + $(TOOL_CURL_CHECKS) diff --git a/tool/net/net.mk b/tool/net/net.mk index b29a02bec..d7e624de0 100644 --- a/tool/net/net.mk +++ b/tool/net/net.mk @@ -15,6 +15,7 @@ TOOL_NET_BINS = \ $(TOOL_NET_COMS:%=%.dbg) TOOL_NET_COMS = \ + o/$(MODE)/tool/net/curl.com \ o/$(MODE)/tool/net/dig.com \ o/$(MODE)/tool/net/redbean.com \ o/$(MODE)/tool/net/redbean-demo.com \ diff --git a/tool/tool.mk b/tool/tool.mk index b2bde1561..066f9df15 100644 --- a/tool/tool.mk +++ b/tool/tool.mk @@ -5,6 +5,7 @@ o/$(MODE)/tool: \ o/$(MODE)/tool/args \ o/$(MODE)/tool/build \ + o/$(MODE)/tool/curl \ o/$(MODE)/tool/decode \ o/$(MODE)/tool/hash \ o/$(MODE)/tool/lambda \