Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/osh/osh_parse_ref.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / osh / osh_parse_ref.c` 209 lines ยท 8417 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * osh_parse_ref.c -- independent reference parser (see osh_parse_ref.h). Recursive descent over the token 19 * array: list -> pipeline -> command. The grammar and every refusal are taken from the spec text, in token order: 20 * - a command starts at a word or a redirection operator; any other token where one is needed is 21 * SYNTAX_EMPTY_CMD at that token (a `;` `|` `&&` `||` with no command before it) 22 * - NL after `|` `&&` `||` is skipped; NL after a command or after `;` ends the list (the NL is consumed) 23 * - capacities are checked when the thing they count is about to be added, before it is written 24 * - at the end of the tokens: after `|` `&&` `||` need more input (SYNTAX_EOF at offset n with eoi); after a 25 * redirection operator SYNTAX_REDIR_TARGET at offset n with eoi (else need more); otherwise the list is done 26 */ 27 #include "osh_parse_ref.h" 28 29 #include 30 31 enum { K_WORD = 1, K_PIPE, K_OR, K_AND, K_SEMI, K_LT, K_GT, K_APPEND, K_NL, K_DUP_OUT, K_DUP_IN }; 32 33 typedef struct { 34 const uint8_t *in; 35 size_t n; 36 const OshRefLex *lx; 37 int eoi; 38 OshRefParse *o; 39 unsigned i; /* next token */ 40 unsigned pc; /* commands in the open pipeline */ 41 int bad; /* the outcome is decided */ 42 } P; 43 44 static unsigned ntok(const P *p) { return p->lx->ntok; } 45 static const OshRefTok *tk(const P *p, unsigned j) { return &p->lx->tok[j]; } 46 static int is_redir(uint64_t k) { return k == K_LT || k == K_GT || k == K_APPEND || k == K_DUP_OUT || k == K_DUP_IN; } 47 static int starts_cmd(const P *p, unsigned j) { return tk(p, j)->kind == K_WORD || is_redir(tk(p, j)->kind); } 48 49 static void refuse(P *p, unsigned code, uint64_t off) 50 { 51 p->o->status = code; 52 p->o->err_off = off; 53 p->bad = 1; 54 } 55 56 /* the bytes of in[s..e) without backslash-newline pairs, up to 8 of them plus a terminator; *over set if more */ 57 static void sig(const P *p, uint64_t s, uint64_t e, char out[9], int *over) 58 { 59 size_t k = 0; 60 *over = 0; 61 for (uint64_t q = s; q < e; q++) { 62 if (p->in[q] == '\\' && q + 1 < e && p->in[q + 1] == '\n') { q++; continue; } 63 if (k == 8) { *over = 1; break; } 64 out[k++] = (char)p->in[q]; 65 } 66 out[k] = 0; 67 } 68 69 static unsigned reserved_code(const char *w) 70 { 71 static const struct { const char *w; unsigned code; } R[] = { 72 { "if", 233 }, { "then", 233 }, { "elif", 233 }, { "else", 233 }, { "fi", 233 }, 73 { "for", 234 }, { "while", 234 }, { "until", 234 }, { "do", 234 }, { "done", 234 }, { "in", 234 }, { "select", 234 }, 74 { "case", 235 }, { "esac", 235 }, { "function", 237 }, { "!", 238 }, { "time", 239 }, { "coproc", 239 }, 75 }; 76 for (size_t q = 0; q < sizeof R / sizeof R[0]; q++) if (strcmp(R[q].w, w) == 0) return R[q].code; 77 return 0; 78 } 79 80 static int all_digits(const P *p, uint64_t s, uint64_t len) 81 { 82 for (uint64_t q = 0; q < len; q++) if (p->in[s + q] < '0' || p->in[s + q] > '9') return 0; 83 return 1; 84 } 85 86 /* one command; p->i is at a word or a redirection operator */ 87 static void command(P *p) 88 { 89 const OshRefTok *first = tk(p, p->i); 90 OshRefParse *o = p->o; 91 if (p->pc >= 8) { refuse(p, 205, first->start); return; } 92 if (p->pc == 0 && o->npipe >= OSH_PREF_PIPES) { refuse(p, 204, first->start); return; } 93 if (o->ncmd >= OSH_PREF_CMDS) { refuse(p, 203, first->start); return; } 94 unsigned c = o->ncmd++; 95 if (p->pc == 0) { 96 o->pipe[o->npipe][0] = c; o->pipe[o->npipe][1] = 0; o->pipe[o->npipe][2] = 0; o->pipe[o->npipe][3] = p->i; 97 o->npipe++; 98 } 99 unsigned pi = o->npipe - 1; 100 memset(o->cmd[c], 0, sizeof o->cmd[c]); 101 o->cmd[c][0] = p->i; 102 o->cmd[c][5] = pi; 103 o->cmd[c][6] = p->pc; 104 o->pipe[pi][1] = ++p->pc; 105 int any_word = 0; 106 while (p->i < ntok(p)) { 107 const OshRefTok *t = tk(p, p->i); 108 if (t->kind == K_WORD) { 109 if ((t->flags & 16) && !any_word) { 110 char nm[9]; int over; 111 sig(p, t->start, t->aux - 1, nm, &over); 112 if (!over && strcmp(nm, "IFS") == 0) { refuse(p, 240, t->start); return; } 113 if (o->cmd[c][2] >= 16) { refuse(p, 207, t->start); return; } 114 o->cmd[c][2]++; 115 } else { 116 if (!any_word && (t->flags & ~32ULL) == 0) { 117 char w[9]; int over; 118 sig(p, t->start, t->start + t->len, w, &over); 119 unsigned rc = over ? 0 : reserved_code(w); 120 if (rc) { refuse(p, rc, t->start); return; } 121 } 122 if (o->cmd[c][3] >= 32) { refuse(p, 206, t->start); return; } 123 o->cmd[c][3]++; 124 any_word = 1; 125 } 126 p->i++; 127 } else if (is_redir(t->kind)) { 128 if (o->cmd[c][4] >= 8) { refuse(p, 208, t->start); return; } 129 o->cmd[c][4]++; 130 p->i++; 131 if (p->i >= ntok(p)) { 132 if (p->eoi) refuse(p, 242, p->n); 133 else { o->status = 100; p->bad = 1; } 134 return; 135 } 136 const OshRefTok *g = tk(p, p->i); 137 if (g->kind != K_WORD) { refuse(p, 242, g->start); return; } 138 if (t->kind == K_DUP_OUT || t->kind == K_DUP_IN) { 139 if (g->flags != 0) { refuse(p, 231, g->start); return; } 140 if (g->len == 1 && g->start < p->n && p->in[g->start] >= '0' && p->in[g->start] <= '2') { /* fine */ } 141 else if (all_digits(p, g->start, g->len)) { refuse(p, 232, g->start); return; } 142 else { refuse(p, 231, g->start); return; } 143 } 144 p->i++; 145 } else { 146 break; 147 } 148 } 149 o->cmd[c][1] = p->i; 150 } 151 152 static void skip_nl(P *p) { while (p->i < ntok(p) && tk(p, p->i)->kind == K_NL) p->i++; } 153 154 /* the tokens ran out after a `|` `&&` `||` */ 155 static void run_out(P *p) 156 { 157 if (p->eoi) refuse(p, 248, p->n); 158 else { p->o->status = 100; p->bad = 1; } 159 } 160 161 void osh_parse_ref(const uint8_t *in, size_t n, const OshRefLex *lx, int eoi, OshRefParse *out) 162 { 163 memset(out, 0, sizeof *out); 164 P p = { in, n, lx, eoi, out, 0, 0, 0 }; 165 if (p.i >= ntok(&p)) { out->next_tok = p.i; return; } 166 if (tk(&p, p.i)->kind == K_NL) { out->next_tok = ++p.i; return; } /* a blank line is a complete, empty list */ 167 if (!starts_cmd(&p, p.i)) { refuse(&p, 241, tk(&p, p.i)->start); return; } 168 for (;;) { 169 /* pipeline */ 170 command(&p); 171 if (p.bad) return; 172 while (p.i < ntok(&p) && tk(&p, p.i)->kind == K_PIPE) { 173 p.i++; 174 skip_nl(&p); 175 if (p.i >= ntok(&p)) { run_out(&p); return; } 176 if (!starts_cmd(&p, p.i)) { refuse(&p, 241, tk(&p, p.i)->start); return; } 177 command(&p); 178 if (p.bad) return; 179 } 180 unsigned pi = out->npipe - 1; 181 p.pc = 0; 182 if (p.i >= ntok(&p)) { 183 if (!eoi) { out->status = 100; return; } 184 out->pipe[pi][2] = 0; 185 out->next_tok = p.i; 186 return; 187 } 188 const OshRefTok *t = tk(&p, p.i); 189 if (t->kind == K_NL) { 190 out->pipe[pi][2] = 1; 191 out->next_tok = ++p.i; 192 return; 193 } 194 if (t->kind == K_SEMI) { 195 out->pipe[pi][2] = 1; 196 p.i++; 197 if (p.i >= ntok(&p)) { out->next_tok = p.i; return; } 198 if (tk(&p, p.i)->kind == K_NL) { out->next_tok = ++p.i; return; } 199 if (!starts_cmd(&p, p.i)) { refuse(&p, 241, tk(&p, p.i)->start); return; } 200 continue; 201 } 202 /* && or || */ 203 out->pipe[pi][2] = t->kind == K_AND ? 2 : 3; 204 p.i++; 205 skip_nl(&p); 206 if (p.i >= ntok(&p)) { run_out(&p); return; } 207 if (!starts_cmd(&p, p.i)) { refuse(&p, 241, tk(&p, p.i)->start); return; } 208 } 209 }