1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
(*
//
// For testing the ATS API for jansson
// Only the ATS code here is written by Hongwei Xi
//
*)
%{^
/*
* Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include <curl/curl.h>
#define BUFFER_SIZE (256 * 1024) /* 256 KB */
/* Return the offset of the first newline in text or the length of
text if there's no newline */
struct write_result
{
char *data; int pos;
};
static
size_t
write_response
(
void *ptr, size_t size, size_t nmemb, void *stream
) {
struct write_result *result = (struct write_result *)stream;
if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
{
fprintf(stderr, "error: too small buffer\n");
return 0;
}
memcpy(result->data + result->pos, ptr, size * nmemb);
result->pos += size * nmemb;
return size * nmemb;
}
static
char *request (const char *url)
{
CURL *curl;
CURLcode status;
char *data;
long code;
curl = curl_easy_init();
data = malloc(BUFFER_SIZE);
if(!curl || !data)
return NULL;
struct write_result write_result = {
.data = data,
.pos = 0
};
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
status = curl_easy_perform(curl);
if(status != 0)
{
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
return NULL;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200)
{
fprintf(stderr, "error: server responded with code %ld\n", code);
return NULL;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
/* zero-terminate the result */
data[write_result.pos] = '\0';
return data;
}
%} // end of [%{^]
(* ****** ****** *)
#include
"share/atspre_staload.hats"
(* ****** ****** *)
//
staload
UN = "prelude/SATS/unsafe.sats"
//
(* ****** ****** *)
staload "./../SATS/jansson.sats"
(* ****** ****** *)
extern
fun request (url: string): Strptr0 = "mac#"
(* ****** ****** *)
//
extern
fun process_root
{l:addr} (root: !JSONptr l, err: &json_err): void
//
extern
fun process_root_array {l:agz} (root: !JSONptr l): void
//
(* ****** ****** *)
implement
process_root
{l} (root, err) = let
//
prval (
) = lemma_addr_param {l} ()
//
val isnot =
JSONptr_isnot_null (root)
val () =
(
if ~isnot then let
val () =
prerrln! ("ERROR: on line ", err.line, ": ", $UN.cast{string}(err.text))
val () = exit_void (1)
in
// nothing
end // end of [if]
)
val () = assert (isnot)
//
val isa = json_is_array (root)
val () =
(
if ~isa then let
val () = prerrln! ("ERROR: root is not an array")
val () = exit_void (1)
in
// nothing
end // end of [if]
)
in
process_root_array {l} (root)
end // end of [process_root]
macdef
JPNZ (x) =
assertloc (JSONptr_isnot_null ,(x))
// end of [JPNZ]
implement
process_root_array
{l} (root) = let
//
val n = json_array_size (root)
//
fun loop (
root: !JSONptr l, n: size_t, i: size_t
) : void = let
in
//
if i < n then let
//
val data =
json_array_get1_exnloc (root, i)
// end of [val]
val () = assertloc (json_is_object (data))
//
val sha =
json_object_get1_exnloc (data, "sha")
// end of [val]
val () = assertloc (json_is_string (sha))
//
val commit =
json_object_get1_exnloc (data, "commit")
// end of [val]
val () = assertloc (json_is_object (commit))
//
val message =
json_object_get1_exnloc (commit, "message")
// end of [val]
val () = assertloc (json_is_string (message))
//
val (fpf1 | sha_value) = json_string_value (sha)
val (fpf2 | message_value) = json_string_value (message)
//
val () = println! ($UN.strptr2string(sha_value))
val () = println! ()
val () = println! ($UN.strptr2string(message_value))
val () = println! ()
//
prval () = minus_addback (fpf1, sha_value | sha)
prval () = minus_addback (fpf2, message_value | message)
val () = json_decref (commit)
val () = json_decref (sha)
val () = json_decref (message)
val () = json_decref (data)
//
in
loop (root, n, succ(i))
end else () // end of [if]
//
end // end of [loop]
//
in
loop (root, n, g1int2uint(0))
end // end of [process_root_array]
(* ****** ****** *)
%{^
static
char *auxurl
(
char *urlfmt, char *USER, char *REPO
)
{
char *res ;
int bsz = 1024 ;
int err = 0 ;
res = atspre_malloc_gc (bsz) ;
err = snprintf (res, bsz, urlfmt, USER, REPO) ;
return res ;
}
%}
extern
fun auxurl : (string, string, string) -> Strptr1 = "mac#"
(* ****** ****** *)
implement
main0 (argc, argv) = let
//
// Usage: argv[0] USER REPO
//
val cmd = argv[0]
var arg1: string = "githwxi"
var arg2: string = "ATS-Postiats"
//
val () = if argc >= 2 then arg1 := argv[1]
val () = if argc >= 3 then arg2 := argv[2]
//
val FMT =
"https://api.github.com/repos/%s/%s/commits"
val url = auxurl (FMT, arg1, arg2)
//
val () = println! ("url = ", url)
//
val text = request ($UN.strptr2string (url))
val () = strptr_free (url)
//
val isnot = strptr_isnot_null (text)
//
in
//
if isnot then let
var err: json_err
val root = json_loads ($UN.strptr2string(text), 0, err)
val () = strptr_free (text)
val () = process_root (root, err)
val () = json_decref(root)
in
// nothing
end else let
prval () = strptr_free_null (text)
in
// nothing
end // end of [if]
//
end // end of [main0]
(* ****** ****** *)
(* end of [github_commits.dats] *)
|