nx_multi_fetch.nx source
↩ module page · 49 lines · 2380 B
1// nx_multi_fetch.nx -- fetch SEVERAL sources about one capability area to
2// /tmp/src0.txt, /tmp/src1.txt, /tmp/src2.txt (transient research-time input).
3// Feeds the live corroborated landscape (R5). Fetcher tree only. expect_exit: 0.
4
5import "nx_syscalls.nx"
6import "nx_x509_trust_store.nx"
7import "nx_trust_store_load_from_certdata.nx"
8import "nx_https_fetch_follow.nx"
9import "nx_html_to_text.nx"
10const K_MAGIC_8388608: i64 = 8388608
11const K_MAGIC_4194304: i64 = 4194304
12
13func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func wn(v: i64) -> i64 {
15 let b: *u8 = sys_mmap(28); var m: i64 = v
16 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
17 let t: *u8 = sys_mmap(28); var k: i64 = 0
18 if m == 0 { t[0] = 48 as u8; k = 1 }
19 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
20 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
21 sys_write(1, b, k); return 0
22}
23
24func fetch_one(url: *u8, path: *u8, store: *TrustStore, raw: *u8, txt: *u8) -> i64 {
25 let st: *i64 = (sys_mmap(8)) as *i64
26 let n: i64 = nx_https_fetch_follow(url, store, raw, K_MAGIC_8388608, 6, st)
27 w(url); w(" status=" as *u8); wn(st[0]); w(" raw=" as *u8); wn(n)
28 if n <= 0 { w(" FAIL\n" as *u8); return 0 }
29 let tn: i64 = nx_html_to_text(raw, n, txt, K_MAGIC_8388608)
30 let fd: i64 = sys_openat_wr(path, 420)
31 if fd < 0 { w(" OPEN-FAIL\n" as *u8); return 0 }
32 var wr: i64 = 0
33 while wr < tn { let c: i64 = sys_write(fd, txt + wr, tn - wr); if c <= 0 { break } wr = wr + c }
34 sys_close(fd)
35 w(" -> " as *u8); w(path); w(" (" as *u8); wn(wr); w(" bytes)\n" as *u8)
36 return wr
37}
38
39func main() -> i64 {
40 let r: i64 = nx_trust_store_load_from_certdata("/tmp/mozilla_certdata.txt" as *u8, 300, K_MAGIC_4194304)
41 if r <= 0 { w("STORE-FAIL\n" as *u8); return 1 }
42 let store: *TrustStore = r as *TrustStore
43 let raw: *u8 = sys_mmap(K_MAGIC_8388608)
44 let txt: *u8 = sys_mmap(K_MAGIC_8388608)
45 fetch_one("https://thecadhub.com/blog/best-3d-printing-software/" as *u8, "/tmp/src0.txt" as *u8, store, raw, txt)
46 fetch_one("https://en.wikipedia.org/wiki/Fused_filament_fabrication" as *u8, "/tmp/src1.txt" as *u8, store, raw, txt)
47 fetch_one("https://en.wikipedia.org/wiki/3D_printing" as *u8, "/tmp/src2.txt" as *u8, store, raw, txt)
48 return 0
49}