[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] feature/aptel/dynamic-modules-rc3 9da7346 13/25: new modul
From: |
Teodor Zlatanov |
Subject: |
[Emacs-diffs] feature/aptel/dynamic-modules-rc3 9da7346 13/25: new module: curl. basic URL fetcher working. |
Date: |
Wed, 04 Feb 2015 22:56:14 +0000 |
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(+), 0 deletions(-)
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 19705b8 06/25: alloc.c: protect doc field from GC., (continued)
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 19705b8 06/25: alloc.c: protect doc field from GC., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 fc0f6ec 02/25: add `load-module' function to emacs lisp., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 0cfb066 07/25: doc.c: use DOC format sanity checks instead of bytecompiled checks on module doc files., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 79e2c28 05/25: change Subr doc field from char* to Lisp_Object., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 b8286d2 08/25: doc.c: don't prefix doc path by ../etc/ for modules, Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 1b98be3 09/25: move fmod module in modules/fmod/., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 772cb16 01/25: add libltdl configure option/dependency., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 bfe16af 10/25: new module: opaque, Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 89c8287 11/25: opaque: add comments., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 9330ca6 12/25: new module: elisp, Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 9da7346 13/25: new module: curl. basic URL fetcher working.,
Teodor Zlatanov <=
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 582704c 14/25: curl: use url arg., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 8147699 15/25: fix docstring., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 a95e032 17/25: update modules examples with `provide' call., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 1959b65 18/25: use `load-module-suffixes' as a source of module suffixes., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 8254721 16/25: make `load' look for and handle dynamic modules., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 4000103 20/25: yaml module: add `yaml-parse-string'., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 c7248ea 21/25: yaml module: rename `yaml-parse' to `yaml-parse-buffer'., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 092a6d2 22/25: configure.ac: dynamic modules off by default., Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 4e1ad37 19/25: new yaml parsing module using libyaml, Teodor Zlatanov, 2015/02/04
- [Emacs-diffs] feature/aptel/dynamic-modules-rc3 1da9b91 24/25: opaque module: rename Qc variable to Qc_ due to macro collision, Teodor Zlatanov, 2015/02/04