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/host/osh_vars.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 / host / osh_vars.c` 235 lines ยท 7911 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_vars.c -- the shell's variable table and the fresh-per-exec environment (ABI section 4.5, 7.2, 9.1). 19 * The environment of a child is built from this table only; the process-global environ is read once, at 20 * osh_session_init, and never again. Names and values are raw bytes without NUL. 21 * Bookkeeping as bash 5.2 does it: PWD per POSIX; SHLVL = inherited level + 1 (non-numeric counts as 0, never below 0, 22 * above 1000 resets to 1 with a warning); OLDPWD exported, kept only when the inherited value names a directory and 23 * set by cd. `_` is not a shell variable ($_ is refused as SPECIAL_PARAM); as bash does, each program started gets 24 * _= in its environment (osh_build_envp prog). 25 */ 26 #include "osh_host.h" 27 #include "osh_priv.h" 28 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 37 static OshVar *find(const OshSession *s, const char *name) 38 { 39 for (size_t i = 0; i < s->nvars; i++) 40 if (strcmp(s->vars[i].name, name) == 0) return &s->vars[i]; 41 return NULL; 42 } 43 44 static OshVar *add(OshSession *s, const char *name) 45 { 46 if (s->nvars == s->capvars) { 47 size_t nc = s->capvars ? s->capvars * 2 : 32; 48 OshVar *nv = realloc(s->vars, nc * sizeof *nv); 49 if (!nv) return NULL; 50 s->vars = nv; 51 s->capvars = nc; 52 } 53 char *n = strdup(name); 54 if (!n) return NULL; 55 OshVar *v = &s->vars[s->nvars++]; 56 v->name = n; 57 v->value = NULL; 58 v->exported = 0; 59 return v; 60 } 61 62 const char *osh_var_get(const OshSession *s, const char *name) 63 { 64 OshVar *v = find(s, name); 65 return v ? v->value : NULL; 66 } 67 68 static int setval(OshVar *v, const char *value) 69 { 70 char *d = strdup(value); 71 if (!d) return -1; 72 free(v->value); 73 v->value = d; 74 return 0; 75 } 76 77 int osh_var_set(OshSession *s, const char *name, const char *value) 78 { 79 if (!osh_name_valid(name)) return -1; 80 OshVar *v = find(s, name); 81 if (!v && !(v = add(s, name))) return -1; 82 return setval(v, value); 83 } 84 85 int osh_var_export(OshSession *s, const char *name, const char *value) 86 { 87 if (!osh_name_valid(name)) return -1; 88 OshVar *v = find(s, name); 89 if (!v && !(v = add(s, name))) return -1; 90 if (value && setval(v, value)) return -1; 91 v->exported = 1; 92 return 0; 93 } 94 95 int osh_var_unexport(OshSession *s, const char *name) 96 { 97 OshVar *v = find(s, name); 98 if (v) v->exported = 0; 99 return 0; /* bash: `export -n` of an unset name is a silent success */ 100 } 101 102 int osh_var_unset(OshSession *s, const char *name) 103 { 104 for (size_t i = 0; i < s->nvars; i++) 105 if (strcmp(s->vars[i].name, name) == 0) { 106 free(s->vars[i].name); 107 free(s->vars[i].value); 108 s->vars[i] = s->vars[--s->nvars]; 109 return 0; 110 } 111 return 0; 112 } 113 114 static int pwd_names_cwd(const char *p) 115 { 116 struct stat a, b; 117 return p && p[0] == '/' && stat(p, &a) == 0 && stat(".", &b) == 0 && a.st_dev == b.st_dev && a.st_ino == b.st_ino; 118 } 119 120 /* Keep an imported entry whose name is not an identifier. bash never makes it a variable and never shows it in 121 * `export -p`, yet every child still receives it; dropping it would starve tools that read their own odd entries. */ 122 static int raw_keep(OshSession *s, const char *entry) 123 { 124 char **nr = realloc(s->raw_env, (s->nraw + 1) * sizeof *nr); 125 if (!nr) return -1; 126 s->raw_env = nr; 127 if (!(nr[s->nraw] = strdup(entry))) return -1; 128 s->nraw++; 129 return 0; 130 } 131 132 int osh_session_init(OshSession *s, char *const *envp) 133 { 134 memset(s, 0, sizeof *s); 135 s->fd[0] = 0; s->fd[1] = 1; s->fd[2] = 2; 136 s->tty_fd = -1; 137 s->session_epoch = 1; 138 for (; envp && *envp; envp++) { 139 const char *eq = strchr(*envp, '='); 140 if (!eq || eq == *envp) continue; 141 size_t nl = (size_t)(eq - *envp); 142 char name[256]; 143 int ident = nl < sizeof name; 144 if (ident) { memcpy(name, *envp, nl); name[nl] = 0; ident = osh_name_valid(name); } 145 if (!ident) { 146 if (raw_keep(s, *envp)) { osh_session_free(s); return -1; } 147 continue; 148 } 149 if (osh_var_export(s, name, eq + 1)) { osh_session_free(s); return -1; } 150 } 151 if (!pwd_names_cwd(osh_var_get(s, "PWD"))) { 152 char cwd[PATH_MAX]; 153 if (!getcwd(cwd, sizeof cwd) || osh_var_export(s, "PWD", cwd)) { osh_session_free(s); return -1; } 154 } 155 const char *lv = osh_var_get(s, "SHLVL"); 156 long level = (lv ? strtol(lv, NULL, 10) : 0) + 1; /* atoi semantics: junk counts as 0 */ 157 if (level < 0) level = 0; 158 if (level >= 1000) { 159 osh_diag(s->fd[2], "osh: warning: shell level (%ld) too high, resetting to 1", level); 160 level = 1; 161 } 162 char lvbuf[24]; 163 snprintf(lvbuf, sizeof lvbuf, "%ld", level); 164 if (osh_var_export(s, "SHLVL", lvbuf)) { osh_session_free(s); return -1; } 165 const char *op = osh_var_get(s, "OLDPWD"); 166 struct stat st; 167 if (!op || stat(op, &st) != 0 || !S_ISDIR(st.st_mode)) { 168 osh_var_unset(s, "OLDPWD"); 169 if (osh_var_export(s, "OLDPWD", NULL)) { osh_session_free(s); return -1; } 170 } 171 return 0; 172 } 173 174 void osh_session_free(OshSession *s) 175 { 176 for (size_t i = 0; i < s->nvars; i++) { free(s->vars[i].name); free(s->vars[i].value); } 177 free(s->vars); 178 s->vars = NULL; 179 s->nvars = s->capvars = 0; 180 for (size_t i = 0; i < s->nraw; i++) free(s->raw_env[i]); 181 free(s->raw_env); 182 s->raw_env = NULL; 183 s->nraw = 0; 184 } 185 186 void osh_envp_free(char **envp) 187 { 188 if (!envp) return; 189 for (char **p = envp; *p; p++) free(*p); 190 free(envp); 191 } 192 193 static int envp_put(char **e, size_t *n, const char *name, const char *value) 194 { 195 size_t nl = strlen(name), vl = strlen(value); 196 char *str = malloc(nl + vl + 2); 197 if (!str) return -1; 198 memcpy(str, name, nl); 199 str[nl] = '='; 200 memcpy(str + nl + 1, value, vl + 1); 201 size_t j = 0; 202 for (; j < *n; j++) 203 if (strncmp(e[j], name, nl) == 0 && e[j][nl] == '=') break; 204 if (j < *n) { free(e[j]); e[j] = str; } 205 else e[(*n)++] = str; 206 return 0; 207 } 208 209 char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov, const char *prog) 210 { 211 char **e = calloc(s->nvars + s->nraw + (size_t)nov + 2, sizeof *e); 212 if (!e) return NULL; 213 size_t n = 0; 214 for (size_t i = 0; i < s->nvars; i++) { 215 const OshVar *v = &s->vars[i]; 216 if (!v->exported || !v->value) continue; 217 size_t nl = strlen(v->name), vl = strlen(v->value); 218 char *str = malloc(nl + vl + 2); 219 if (!str) { osh_envp_free(e); return NULL; } 220 memcpy(str, v->name, nl); 221 str[nl] = '='; 222 memcpy(str + nl + 1, v->value, vl + 1); 223 e[n++] = str; 224 } 225 for (size_t i = 0; i < s->nraw; i++) { /* verbatim; their names are not identifiers so no override can match them */ 226 if (!(e[n] = strdup(s->raw_env[i]))) { osh_envp_free(e); return NULL; } 227 n++; 228 } 229 for (int k = 0; k < nov; k++) 230 if (envp_put(e, &n, ov[k].name, ov[k].value)) { osh_envp_free(e); return NULL; } 231 /* bash puts _= into every program's environment, over any inherited or assigned _ */ 232 if (prog && envp_put(e, &n, "_", prog)) { osh_envp_free(e); return NULL; } 233 e[n] = NULL; 234 return e; 235 }