Skip to content

Instantly share code, notes, and snippets.

@hellerve
Created April 3, 2018 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellerve/7fabbf85963c23a1032b36c23a18962b to your computer and use it in GitHub Desktop.
Save hellerve/7fabbf85963c23a1032b36c23a18962b to your computer and use it in GitHub Desktop.
A Carp CLI for commandlinefu.com
#include <string.h>
#include <stdlib.h>
#include <core.h>
static const char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String b64(String* osrc) {
String src = *osrc;
int len = strlen(src);
char *pos;
const char *end, *in;
size_t olen;
olen = 4*((len + 2) / 3)+1; /* 3-byte blocks to 4-byte */
char* out = malloc(olen);
end = src + len;
in = src;
pos = out;
while (end - in >= 3) {
*pos++ = base64_table[in[0] >> 2];
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = base64_table[in[2] & 0x3f];
in += 3;
}
if (end - in) {
*pos++ = base64_table[in[0] >> 2];
if (end - in == 1) {
*pos++ = base64_table[(in[0] & 0x03) << 4];
*pos++ = '=';
}
else {
*pos++ = base64_table[((in[0] & 0x03) << 4) |
(in[1] >> 4)];
*pos++ = base64_table[(in[1] & 0x0f) << 2];
}
*pos++ = '=';
}
out[olen-1] = '\0';
return out;
}
; needs JSON and CURL
(load "carp-json/json.carp")
(load "carp-curl/Curl.carp")
(relative-include "b64.h")
(register b64 (Fn [&String] String))
(use Array)
(def str-res @"")
(defn print-json-res []
(let-do [parsed (Json.parse &(fmt "{\"root\": %s}" &str-res))
summary (Json.string parsed "root[0].summary")
cmd (Json.string parsed "root[0].command")
url (Pattern.substitute #"\\/"
&(Json.string parsed "root[0].url")
"/"
-1)
]
(IO.println &summary)
(IO.println &(fmt " Example: %s" &cmd))
(IO.println &(fmt " URL: %s" &url))))
(defn handler [s elem-size elem-count ignored]
(do
(ignore (the Int elem-size))
(ignore (the Int elem-count))
(ignore (the (Ref String) ignored))
(set! str-res (String.append @&str-res (String.prefix-string &(str (the (Ptr Char) s)) elem-count)))
elem-count))
(defn join-sys-args []
(let-do [result @(nth &System.args 1)
len (count &System.args)]
(for [i 2 len]
(set! result (string-join result @" " @(nth &System.args i))))
result))
(defn main []
(if (< (count &System.args) 2)
(IO.println "Error: need at least two arguments.")
(do
(Curl.global-init Curl.global-default)
(let-do [curl (Curl.easy-init)
ignored @""
arg (join-sys-args)]
(Curl.easy-setopt curl Curl.opt-url
(cstr &(fmt "http://www.commandlinefu.com/commands/matching/%s/%s/sort-by-votes/json"
&(Pattern.substitute #" " &arg "%20" -1)
&(b64 &arg))))
(Curl.easy-setopt curl Curl.opt-write-function handler)
(Curl.easy-setopt curl Curl.opt-write-data &ignored)
(let-do [res (Curl.easy-perform curl)]
(if (= res Curl.ok)
(if (= "[]" &str-res)
(IO.println "No results.")
(print-json-res))
(println* "CURL Error: " (Curl.easy-strerror res)))))
(Curl.global-cleanup))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment