code wiki / (root) / nx_glprobe.nx

nx_glprobe.nx source

↩ module page · 77 lines · 3677 B

1// nx_glprobe.nx -- CLI over nx_glprobe_lib (the GL-context probe ruler). Holds NO table of its own: the 2// classifier, the state and remedy strings and the bare-refusal detector all live in the lib, so this organ, 3// the emitted JS and nx_glprobe_gate cannot disagree. 4// usage: nx_glprobe classify <gl2 0|1> <gl1 0|1> <gpu 0|1> -> JSON {state, remedy} 5// nx_glprobe js -> the emitted probe, verbatim 6// nx_glprobe scan <path> -> JSON {bare_refusals, composes_probe} 7// exit: 0 ok | 2 usage | 3 unreadable 8// license_tier: ORIGINAL No hw writes (Rule 26). 9import "nx_syscalls.nx" 10import "nx_glprobe_lib.nx" 11 12func gp_puts(s: *u8) -> i64 { sys_write(1, s, glp_slen(s)); return 0 } 13func gp_pn(v: i64) -> i64 { 14 var m: i64 = v 15 if m < 0 { gp_puts("-" as *u8); m = 0 - m } 16 let t: *u8 = sys_mmap(24) 17 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 let o: *u8 = sys_mmap(24) 21 var i: i64 = 0 22 while i < k { o[i] = t[k - 1 - i]; i = i + 1 } 23 sys_write(1, o, k) 24 return 0 25} 26// a flag is 0 or 1 and nothing else: a stray value REFUSES rather than reading as 0 27func gp_flag(s: *u8) -> i64 { 28 if s[1] != (0 as u8) { return 0 - 1 } 29 if s[0] == (48 as u8) { return 0 } 30 if s[0] == (49 as u8) { return 1 } 31 return 0 - 1 32} 33func gp_verb(s: *u8, v: *u8) -> i64 { 34 var i: i64 = 0 35 while s[i] != (0 as u8) { if s[i] != v[i] { return 0 } i = i + 1 } 36 if v[i] != (0 as u8) { return 0 } 37 return 1 38} 39 40func main(argc: i64, argv: *i64) -> i64 { 41 if argc < 2 { gp_puts("usage: nx_glprobe classify <gl2> <gl1> <gpu> | js | scan <path>\n" as *u8); return 2 } 42 let verb: *u8 = argv[1] as *u8 43 if gp_verb(verb, "js" as *u8) == 1 { gp_puts(glp_js()); return 0 } 44 if gp_verb(verb, "classify" as *u8) == 1 { 45 if argc < 5 { gp_puts("usage: nx_glprobe classify <gl2 0|1> <gl1 0|1> <gpu 0|1>\n" as *u8); return 2 } 46 let gl2: i64 = gp_flag(argv[2] as *u8) 47 let gl1: i64 = gp_flag(argv[3] as *u8) 48 let gpu: i64 = gp_flag(argv[4] as *u8) 49 if gl2 < 0 { gp_puts("REFUSED: gl2 must be 0 or 1\n" as *u8); return 2 } 50 if gl1 < 0 { gp_puts("REFUSED: gl1 must be 0 or 1\n" as *u8); return 2 } 51 if gpu < 0 { gp_puts("REFUSED: gpu must be 0 or 1\n" as *u8); return 2 } 52 let st: i64 = glp_classify(gl2, gl1, gpu) 53 gp_puts("{\"organ\":\"nx_glprobe\",\"gl2\":" as *u8); gp_pn(gl2) 54 gp_puts(",\"gl1\":" as *u8); gp_pn(gl1) 55 gp_puts(",\"gpu\":" as *u8); gp_pn(gpu) 56 gp_puts(",\"state_id\":" as *u8); gp_pn(st) 57 gp_puts(",\"state\":\"" as *u8); gp_puts(glp_state_name(st)) 58 gp_puts("\",\"remedy\":\"" as *u8); gp_puts(glp_remedy(st)) 59 gp_puts("\"}\n" as *u8) 60 return 0 61 } 62 if gp_verb(verb, "scan" as *u8) == 1 { 63 if argc < 3 { gp_puts("usage: nx_glprobe scan <path>\n" as *u8); return 2 } 64 let lp: *i64 = sys_mmap(16) as *i64 65 let b: *u8 = sys_read_file(argv[2] as *u8, lp) 66 if (b as i64) == 0 { gp_puts("UNREADABLE: " as *u8); gp_puts(argv[2] as *u8); gp_puts("\n" as *u8); return 3 } 67 let n: i64 = lp[0] 68 gp_puts("{\"organ\":\"nx_glprobe\",\"path\":\"" as *u8); gp_puts(argv[2] as *u8) 69 gp_puts("\",\"bytes\":" as *u8); gp_pn(n) 70 gp_puts(",\"bare_refusals\":" as *u8); gp_pn(glp_bare_count(b, n)) 71 gp_puts(",\"composes_probe\":" as *u8); gp_pn(glp_composes(b, n)) 72 gp_puts("}\n" as *u8) 73 return 0 74 } 75 gp_puts("usage: nx_glprobe classify <gl2> <gl1> <gpu> | js | scan <path>\n" as *u8) 76 return 2 77}