[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] feature/aptel/dynamic-modules-rc3 9da7346 13/35: new modul
From: |
Noah Friedman |
Subject: |
[Emacs-diffs] feature/aptel/dynamic-modules-rc3 9da7346 13/35: new module: curl. basic URL fetcher working. |
Date: |
Mon, 8 May 2017 19:46:27 -0400 (EDT) |
branch: feature/aptel/dynamic-modules-rc3
commit 9da7346c9a3a46de0cc2b585b65cb5823ac89b24
Author: Aurélien Aptel <address@hidden>
Commit: Aurélien Aptel <address@hidden>
new module: curl. basic URL fetcher working.
---
modules/curl/Makefile | 15 +++++++
modules/curl/curl.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 128 insertions(+)
diff --git a/modules/curl/Makefile b/modules/curl/Makefile
new file mode 100644
index 0000000..2e7fda0
--- /dev/null
+++ b/modules/curl/Makefile
@@ -0,0 +1,15 @@
+ROOT = ../..
+
+CFLAGS = `pkg-config libcurl --cflags`
+LDFLAGS = `pkg-config libcurl --libs`
+
+all: curl.so curl.doc
+
+%.so: %.o
+ gcc -shared $(LDFLAGS) -o $@ $<
+
+%.o: %.c
+ gcc -ggdb3 -Wall -I$(ROOT)/src -I$(ROOT)/lib $(CFLAGS) -fPIC -c $<
+
+%.doc: %.c
+ $(ROOT)/lib-src/make-docfile $< > $@
diff --git a/modules/curl/curl.c b/modules/curl/curl.c
new file mode 100644
index 0000000..f305a01
--- /dev/null
+++ b/modules/curl/curl.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <curl/curl.h>
+
+#include <config.h>
+#include <lisp.h>
+
+int plugin_is_GPL_compatible;
+
+struct buffer
+{
+ char *p;
+ size_t size, capacity;
+};
+
+struct Lisp_CURL
+{
+ struct buffer buf;
+ CURL *curl;
+};
+
+#define XCURL(x) ((struct Lisp_CURL*)XSAVE_POINTER (x, 0))
+
+/* curl write callback */
+static size_t
+write_cb (void *src, size_t size, size_t nb, void *userp)
+{
+ struct buffer *buf = userp;
+ size_t total = size*nb;
+
+ if (buf->size + total > buf->capacity)
+ {
+ buf->capacity = 2 * (buf->size + total);
+ buf->p = realloc (buf->p, buf->capacity);
+ }
+
+ memcpy (buf->p + buf->size, src, total);
+ buf->size += total;
+ buf->p[buf->size] = 0;
+
+ return total;
+}
+
+
+EXFUN (Fcurl_make, 0);
+DEFUN ("curl-make", Fcurl_make, Scurl_make, 0, 0, 0,
+ doc: "Return a new CURL handle.")
+ (void)
+{
+ struct Lisp_CURL *p = calloc (sizeof (*p), 1);
+ p->buf.p = calloc (1, 1); /* so that realloc always work */
+ p->buf.capacity = 0;
+ p->curl = curl_easy_init ();
+ return make_save_ptr ((void*)p);
+}
+
+
+EXFUN (Fcurl_fetch_url, 2);
+DEFUN ("curl-fetch-url", Fcurl_fetch_url, Scurl_fetch_url, 2, 2, 0,
+ doc: "Fetch and store the content of URL using HANDLE.\n\n"
+ "Return t if successful otherwise return an error string.")
+ (Lisp_Object handle, Lisp_Object url)
+{
+ CURLcode res;
+ struct Lisp_CURL *c = XCURL (handle);
+
+ curl_easy_setopt (c->curl, CURLOPT_URL, "http://www.example.com/");
+ curl_easy_setopt (c->curl, CURLOPT_WRITEFUNCTION, write_cb);
+ curl_easy_setopt (c->curl, CURLOPT_WRITEDATA, (void*)&c->buf);
+ curl_easy_setopt (c->curl, CURLOPT_USERAGENT, "curl-in-emacs/1.0");
+ res = curl_easy_perform (c->curl);
+
+ if (res != CURLE_OK)
+ {
+ const char* error = curl_easy_strerror (res);
+ return make_string (error, strlen (error));
+ }
+
+ return Qt;
+}
+
+EXFUN (Fcurl_content, 1);
+DEFUN ("curl-content", Fcurl_content, Scurl_content, 1, 1, 0,
+ doc: "Return the content of a successful fetch made in HANDLE.")
+ (Lisp_Object handle)
+{
+ struct Lisp_CURL *c = XCURL (handle);
+ return make_string (c->buf.p, c->buf.size);
+}
+
+EXFUN (Fcurl_free, 1);
+DEFUN ("curl-free", Fcurl_free, Scurl_free, 1, 1, 0,
+ doc: "Free curl HANDLE.")
+ (Lisp_Object handle)
+{
+ struct Lisp_CURL *c = XCURL (handle);
+ free (c->buf.p);
+ curl_easy_cleanup (c->curl);
+
+ return Qt;
+}
+
+void init ()
+{
+ curl_global_init (CURL_GLOBAL_ALL);
+ /* when unloading: curl_global_cleanup(); */
+
+ defsubr (&Scurl_make);
+ defsubr (&Scurl_fetch_url);
+ defsubr (&Scurl_content);
+ defsubr (&Scurl_free);
+}
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 79e2c28 05/35: change Subr doc field from char* to Lisp_Object., (continued)
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 79e2c28 05/35: change Subr doc field from char* to Lisp_Object., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 8254721 16/35: make `load' look for and handle dynamic modules., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 092a6d2 22/35: configure.ac: dynamic modules off by default., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 0cfb066 07/35: doc.c: use DOC format sanity checks instead of bytecompiled checks on module doc files., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 4000103 20/35: yaml module: add `yaml-parse-string'., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 4e1ad37 19/35: new yaml parsing module using libyaml, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 8147699 15/35: fix docstring., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 bfe16af 10/35: new module: opaque, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 582704c 14/35: curl: use url arg., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 9330ca6 12/35: new module: elisp, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 9da7346 13/35: new module: curl. basic URL fetcher working.,
Noah Friedman <=
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 c7248ea 21/35: yaml module: rename `yaml-parse' to `yaml-parse-buffer'., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 a95e032 17/35: update modules examples with `provide' call., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 1959b65 18/35: use `load-module-suffixes' as a source of module suffixes., Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 1da9b91 24/35: opaque module: rename Qc variable to Qc_ due to macro collision, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 6e9c6b1 26/35: doc.c: fix module doc prefix, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 3a2aab6 29/35: modules/fmod: use M prefix for module global vars, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 ef45794 32/35: modules/yaml: skip TODO tests, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 9461927 33/35: modules/opaque/test.el: use `should' macro instead of assert, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 b82611b 27/35: modules/tests.py: honnor module list from args, Noah Friedman, 2017/05/08
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 2e998a7 30/35: modules/fmod: use right docstring in test, Noah Friedman, 2017/05/08