nx_fsops.nx source
↩ module page · 74 lines · 3536 B
1// nx_fsops.nx -- CLI half of the consolidated filesystem tool (MCP name: nx_fs, tool #4 of the 15).
2// (Source named nx_fsops because nx_fs.nx is the file-I/O STDLIB.) READ-ONLY increment:
3// read <path> [maxbytes] [offset] -> bytes to stdout (truncation MARKED); DENY-LIST refuses secrets
4// lines <path> <start> [count] -> LINE-ADDRESSED window + declared envelope; composes with grep's
5// file:LINE output (the pair that byte-offset reads could not do)
6// ls <dir> -> "<t> <name>" per entry (d/f/l/o)
7// exit: 0 ok | 3 absent | 5 DENIED | 2 usage (codes surface in MCP _meta.exit_code)
8// license_tier: ORIGINAL
9import "nx_fsops_lib.nx"
10import "nx_fsops_outline.nx"
11
12const FSC_USAGE_RC: i64 = 2
13const FSC_ARG_VERB: i64 = 1 // argv slot of the verb
14const FSC_ARG_P1: i64 = 2 // argv slot of the path
15const FSC_ARG_P2: i64 = 3 // argv slot of the optional maxbytes
16const FSC_ARGC_P1: i64 = 3 // argc with a path
17const FSC_ARGC_P2: i64 = 4 // argc with path + maxbytes
18const FSC_ASCII_9: i64 = 57 // '9' (decimal parse upper bound)
19
20func fsc_atoi(s: *u8) -> i64 {
21 var v: i64 = 0
22 var i: i64 = 0
23 while s[i] != (0 as u8) {
24 let c: i64 = s[i] as i64
25 if c < FSX_ASCII_0 { return v }
26 if c > FSC_ASCII_9 { return v }
27 v = v * (10 as i64) + (c - FSX_ASCII_0)
28 i = i + 1
29 }
30 return v
31}
32
33func main(argc: i64, argv: *i64) -> i64 {
34 if argc < FSC_ARGC_P1 { fsx_puts("usage: nx_fs read <path> [maxbytes] [offset] | lines <path> <start> [count] | outline <path> | ls <dir>\n" as *u8); return FSC_USAGE_RC }
35 let verb: *u8 = argv[FSC_ARG_VERB] as *u8
36 if fsx_seq(verb, "read" as *u8) == 1 {
37 var cap: i64 = 0
38 if argc >= FSC_ARGC_P2 { cap = fsc_atoi(argv[FSC_ARG_P2] as *u8) }
39 var roff: i64 = 0
40 if argc >= 5 { roff = fsc_atoi(argv[4] as *u8) } // read <path> [maxbytes] [offset] -- windowed (seq222)
41 var r: i64 = 0
42 if roff > 0 { r = fsx_read_at(argv[FSC_ARG_P1] as *u8, cap, roff) } else { r = fsx_read(argv[FSC_ARG_P1] as *u8, cap) }
43 if r >= 0 { return 0 }
44 if r == 0 - (2 as i64) { return FSX_RC_DENIED }
45 return FSX_RC_ABSENT
46 }
47 if fsx_seq(verb, "lines" as *u8) == 1 {
48 var lstart: i64 = 1
49 if argc >= FSC_ARGC_P2 { lstart = fsc_atoi(argv[FSC_ARG_P2] as *u8) }
50 var lcount: i64 = 0
51 if argc >= 5 { lcount = fsc_atoi(argv[4] as *u8) }
52 let rl: i64 = fsx_read_lines(argv[FSC_ARG_P1] as *u8, lstart, lcount)
53 if rl >= 0 { return 0 }
54 if rl == 0 - (2 as i64) { return FSX_RC_DENIED }
55 return FSX_RC_ABSENT
56 }
57 if fsx_seq(verb, "outline" as *u8) == 1 {
58 let r3: i64 = fsx_outline(argv[FSC_ARG_P1] as *u8)
59 if r3 >= 0 { return 0 }
60 if r3 == 0 - (2 as i64) { return FSX_RC_DENIED }
61 return FSX_RC_ABSENT
62 }
63 if fsx_seq(verb, "ls" as *u8) == 1 {
64 // optional 3rd arg = skip, so a directory larger than FSX_LS_CAP is REACHABLE, not merely
65 // honestly refused. Absent => 0 => byte-identical behaviour to every existing caller.
66 var lskip: i64 = 0
67 if argc >= FSC_ARGC_P2 { lskip = fsc_atoi(argv[FSC_ARG_P2] as *u8) }
68 let r2: i64 = fsx_ls_from(argv[FSC_ARG_P1] as *u8, lskip)
69 if r2 >= 0 { return 0 }
70 return FSX_RC_ABSENT
71 }
72 fsx_puts("usage: nx_fs read <path> [maxbytes] [offset] | lines <path> <start> [count] | outline <path> | ls <dir>\n" as *u8)
73 return FSC_USAGE_RC
74}