omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / apps / omega-projects / main.om
| 1 | note omega-projects CLI: validates slugs/paths or renders HTML index from TSV stdin. |
| 2 | note Compose: si/directives/om2.om + apps/omega-projects/lib.om + apps/omega-projects/main.om |
| 3 | |
| 4 | text op_cli_flag_slug "--check-slug" |
| 5 | text op_cli_flag_path "--check-path" |
| 6 | text op_cli_ok "OK\n" |
| 7 | text op_cli_refused "REFUSED\n" |
| 8 | text op_cli_err_read "omega-projects: stdin read error\n" |
| 9 | text op_cli_err_row "omega-projects: invalid project TSV row\n" |
| 10 | text op_cli_err_buf "omega-projects: output buffer overflow\n" |
| 11 | |
| 12 | fn op_write_all fd int buf view uchar * off ulong n ulong to bool |
| 13 | done = ulong 0 |
| 14 | while lt done n bound 65536 |
| 15 | k = write fd buf add off done sub n done |
| 16 | if le k 0 |
| 17 | ret false |
| 18 | end |
| 19 | done = add done ulong k |
| 20 | end |
| 21 | ret true |
| 22 | end |
| 23 | |
| 24 | fn op_read_stdin dst edit uchar * to long |
| 25 | got = ulong 0 |
| 26 | cap = len dst |
| 27 | while lt got cap bound 65536 |
| 28 | k = read 0 dst got sub cap got |
| 29 | if lt k 0 |
| 30 | ret -1 |
| 31 | end |
| 32 | if eq k 0 |
| 33 | break |
| 34 | end |
| 35 | got = add got ulong k |
| 36 | end |
| 37 | ret long got |
| 38 | end |
| 39 | |
| 40 | fn op_check_mode to int |
| 41 | argc = argcnt |
| 42 | if lt argc 3 |
| 43 | ret -1 |
| 44 | end |
| 45 | arg1 = new uchar 256 0 |
| 46 | arg2 = new uchar 512 0 |
| 47 | n1 = argread 1 arg1 0 256 |
| 48 | if lt n1 0 |
| 49 | ret 2 |
| 50 | end |
| 51 | n2 = argread 2 arg2 0 512 |
| 52 | if lt n2 0 |
| 53 | ret 2 |
| 54 | end |
| 55 | if op_slice_eq arg1 0 ulong n1 op_cli_flag_slug |
| 56 | if op_valid_slug arg2 0 ulong n2 |
| 57 | okw = op_write_all 1 op_cli_ok 0 len op_cli_ok |
| 58 | if not okw |
| 59 | ret 1 |
| 60 | end |
| 61 | ret 0 |
| 62 | end |
| 63 | okw = op_write_all 2 op_cli_refused 0 len op_cli_refused |
| 64 | if not okw |
| 65 | ret 1 |
| 66 | end |
| 67 | ret 1 |
| 68 | end |
| 69 | if op_slice_eq arg1 0 ulong n1 op_cli_flag_path |
| 70 | if op_valid_project_path arg2 0 ulong n2 |
| 71 | okw = op_write_all 1 op_cli_ok 0 len op_cli_ok |
| 72 | if not okw |
| 73 | ret 1 |
| 74 | end |
| 75 | ret 0 |
| 76 | end |
| 77 | okw = op_write_all 2 op_cli_refused 0 len op_cli_refused |
| 78 | if not okw |
| 79 | ret 1 |
| 80 | end |
| 81 | ret 1 |
| 82 | end |
| 83 | ret -1 |
| 84 | end |
| 85 | |
| 86 | fn main to int |
| 87 | mode_rc = op_check_mode |
| 88 | if ge mode_rc 0 |
| 89 | ret mode_rc |
| 90 | end |
| 91 | inbuf = new uchar 16384 0 |
| 92 | outbuf = new uchar 32768 0 |
| 93 | nread = op_read_stdin inbuf |
| 94 | if lt nread 0 |
| 95 | w = op_write_all 2 op_cli_err_read 0 len op_cli_err_read |
| 96 | if not w |
| 97 | ret 2 |
| 98 | end |
| 99 | ret 1 |
| 100 | end |
| 101 | pos = op_emit_index_header outbuf 0 |
| 102 | if lt pos 0 |
| 103 | w = op_write_all 2 op_cli_err_buf 0 len op_cli_err_buf |
| 104 | if not w |
| 105 | ret 2 |
| 106 | end |
| 107 | ret 1 |
| 108 | end |
| 109 | cards = ulong 0 |
| 110 | cur = ulong 0 |
| 111 | total = ulong nread |
| 112 | meta = new OpMeta 0 0 0 0 0 0 0 0 0 0 0 |
| 113 | while lt cur total bound 16384 |
| 114 | line_start = cur |
| 115 | while lt cur total bound 16384 |
| 116 | if eq get inbuf cur 10 |
| 117 | cur = add cur 1 |
| 118 | break |
| 119 | end |
| 120 | cur = add cur 1 |
| 121 | end |
| 122 | line_len = sub cur line_start |
| 123 | if eq line_len 0 |
| 124 | continue |
| 125 | end |
| 126 | if eq get inbuf line_start 10 |
| 127 | continue |
| 128 | end |
| 129 | if eq get inbuf line_start 35 |
| 130 | continue |
| 131 | end |
| 132 | rc = op_parse_tsv_line inbuf line_start line_len meta |
| 133 | if ne rc 0 |
| 134 | w = op_write_all 2 op_cli_err_row 0 len op_cli_err_row |
| 135 | if not w |
| 136 | ret 2 |
| 137 | end |
| 138 | ret 1 |
| 139 | end |
| 140 | pos = op_emit_card outbuf ulong pos inbuf meta |
| 141 | if lt pos 0 |
| 142 | w = op_write_all 2 op_cli_err_buf 0 len op_cli_err_buf |
| 143 | if not w |
| 144 | ret 2 |
| 145 | end |
| 146 | ret 1 |
| 147 | end |
| 148 | cards = add cards 1 |
| 149 | end |
| 150 | if eq cards 0 |
| 151 | pos = op_emit_index_empty outbuf ulong pos |
| 152 | if lt pos 0 |
| 153 | ret 1 |
| 154 | end |
| 155 | end |
| 156 | pos = op_emit_index_footer outbuf ulong pos |
| 157 | if lt pos 0 |
| 158 | ret 1 |
| 159 | end |
| 160 | ok = op_write_all 1 outbuf 0 ulong pos |
| 161 | if not ok |
| 162 | ret 1 |
| 163 | end |
| 164 | ret 0 |
| 165 | end |