From 25e9758a29fadb02aa132feaae6f12b125a76315 Mon Sep 17 00:00:00 2001 From: crc Date: Fri, 18 Jan 2019 17:33:31 +0000 Subject: [PATCH] new example: using pipes and curl to make network requests FossilOrigin-Name: 4ee196650050b118929e3c2af90a3145321f7cb76e57a9c9e8b8b49f5354a0a3 --- RELEASE_NOTES.md | 4 +++- example/net_fetch.forth | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 example/net_fetch.forth diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 65d87f6..3f55253 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -18,13 +18,14 @@ ## Interfaces - use strlcpy, strlcat instead of strcpy and strcat +- include copies of the above for users of glibc - add retro-compiler ### retro ### retro-compiler -- new interface for creating turnkey executables +- new interface for creating turnkey executables on bsd, linux ### retro-ri @@ -48,5 +49,6 @@ - add DisplayNames.forth - add KeyValueStore.forth - switch to dvorak key bindings in Roo.forth +- add net_fetch.forth ## Final Notes diff --git a/example/net_fetch.forth b/example/net_fetch.forth new file mode 100644 index 0000000..2ba305d --- /dev/null +++ b/example/net_fetch.forth @@ -0,0 +1,32 @@ +# net:fetch + +This is a simple wrapper over `curl` (https://curl.haxx.se). It +uses the `unix:popen` word to initiate a pipe to curl and stores +the returned data into a buffer. + +This takes in three values: + +- the URL to fetch +- the destination buffer +- a maximum number of bytes to read + +The buffer should be at least one cell longer than the max to +allow for the NULL termination. + +## The Code + +~~~ +:net:fetch (san-n) + [ [ buffer:set + 'curl_-s_%s s:format file:R unix:popen ] dip + [ dup file:read 0; buffer:add ] times unix:pclose + buffer:start s:length ] buffer:preserve ; +~~~ + +## Test Case + +``` +'Data d:create #256001 allot +'https://www.osnews.com Data #300009 net:fetch +'%n_bytes_read\n s:format s:put +```