MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / setpath

Function setpath

lib/lua/src/loadlib.c:288–318  ·  view source on GitHub ↗

** Set a path */

Source from the content-addressed store, hash-verified

286** Set a path
287*/
288static void setpath (lua_State *L, const char *fieldname,
289 const char *envname,
290 const char *dft) {
291 const char *dftmark;
292 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
293 const char *path = getenv(nver); /* try versioned name */
294 if (path == NULL) /* no versioned environment variable? */
295 path = getenv(envname); /* try unversioned name */
296 if (path == NULL || noenv(L)) /* no environment variable? */
297 lua_pushstring(L, dft); /* use default */
298 else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
299 lua_pushstring(L, path); /* nothing to change */
300 else { /* path contains a ";;": insert default path in its place */
301 size_t len = strlen(path);
302 luaL_Buffer b;
303 luaL_buffinit(L, &b);
304 if (path < dftmark) { /* is there a prefix before ';;'? */
305 luaL_addlstring(&b, path, dftmark - path); /* add it */
306 luaL_addchar(&b, *LUA_PATH_SEP);
307 }
308 luaL_addstring(&b, dft); /* add default */
309 if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
310 luaL_addchar(&b, *LUA_PATH_SEP);
311 luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
312 }
313 luaL_pushresult(&b);
314 }
315 setprogdir(L);
316 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
317 lua_pop(L, 1); /* pop versioned variable name ('nver') */
318}
319
320/* }================================================================== */
321

Callers 1

luaopen_packageFunction · 0.85

Calls 9

lua_pushfstringFunction · 0.85
noenvFunction · 0.85
lua_pushstringFunction · 0.85
luaL_buffinitFunction · 0.85
luaL_addlstringFunction · 0.85
luaL_addstringFunction · 0.85
luaL_pushresultFunction · 0.85
setprogdirFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected