** Set a path. (If using the default path, assume it is a string ** literal in C and create it as an external string.) */
| 272 | ** literal in C and create it as an external string.) |
| 273 | */ |
| 274 | static void setpath (lua_State *L, const char *fieldname, |
| 275 | const char *envname, |
| 276 | const char *dft) { |
| 277 | const char *dftmark; |
| 278 | const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); |
| 279 | const char *path = getenv(nver); /* try versioned name */ |
| 280 | if (path == NULL) /* no versioned environment variable? */ |
| 281 | path = getenv(envname); /* try unversioned name */ |
| 282 | if (path == NULL || noenv(L)) /* no environment variable? */ |
| 283 | lua_pushexternalstring(L, dft, strlen(dft), NULL, NULL); /* use default */ |
| 284 | else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) |
| 285 | lua_pushstring(L, path); /* nothing to change */ |
| 286 | else { /* path contains a ";;": insert default path in its place */ |
| 287 | size_t len = strlen(path); |
| 288 | luaL_Buffer b; |
| 289 | luaL_buffinit(L, &b); |
| 290 | if (path < dftmark) { /* is there a prefix before ';;'? */ |
| 291 | luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */ |
| 292 | luaL_addchar(&b, *LUA_PATH_SEP); |
| 293 | } |
| 294 | luaL_addstring(&b, dft); /* add default */ |
| 295 | if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ |
| 296 | luaL_addchar(&b, *LUA_PATH_SEP); |
| 297 | luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark)); |
| 298 | } |
| 299 | luaL_pushresult(&b); |
| 300 | } |
| 301 | setprogdir(L); |
| 302 | lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ |
| 303 | lua_pop(L, 1); /* pop versioned variable name ('nver') */ |
| 304 | } |
| 305 | |
| 306 | /* }================================================================== */ |
| 307 |
no test coverage detected