aboutsummaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-23 22:42:15 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-23 22:42:15 -0400
commitd2ef94104e1fc47b7e344c97a55dc8cef106f94b (patch)
treefba7f77b8e4f1d1c1ee38d38d127046292224f8a /parse.c
parent89c427172a25bdb7802d142c03f3504a53a304fc (diff)
Convert used URLs to hashes
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/parse.c b/parse.c
index b3f9a0c3..3734cf5c 100644
--- a/parse.c
+++ b/parse.c
@@ -12,11 +12,12 @@
#include <signal.h>
#include "ast.h"
+#include "cordhelpers.h"
#include "stdlib/integers.h"
-#include "stdlib/text.h"
+#include "stdlib/patterns.h"
#include "stdlib/tables.h"
+#include "stdlib/text.h"
#include "stdlib/util.h"
-#include "cordhelpers.h"
// The cache of {filename -> parsed AST} will hold at most this many entries:
#ifndef PARSE_CACHE_SIZE
@@ -2368,6 +2369,20 @@ PARSER(parse_use) {
what = USE_SHARED_OBJECT;
} else {
what = USE_MODULE;
+
+ // When `use`ing a URL, convert it to a hash:
+ Text_t text = Text$from_str(name);
+ Array_t m = Text$matches(text, Pattern("{url}"));
+ if (m.length >= 0) {
+ text = Text$trim(text, Pattern("http{0-1 s}://"), true, false);
+ FILE *shasum = popen(heap_strf("echo -n '%s' | sha256sum", Text$as_c_string(text)), "r");
+ const int HASH_LEN = 32;
+ char *hash = GC_MALLOC_ATOMIC(HASH_LEN + 1);
+ size_t just_read = fread(hash, sizeof(char), HASH_LEN, shasum);
+ if (just_read < HASH_LEN)
+ errx(1, "Failed to get SHA sum for 'use': %s", name);
+ name = hash;
+ }
}
return NewAST(ctx->file, start, pos, Use, .path=name, .what=what);
}