merge fs and path libraries

This commit is contained in:
Jeremy Baxter 2023-12-24 14:24:55 +13:00
parent 44f7620a41
commit 11f6a34ebf
5 changed files with 64 additions and 106 deletions

View file

@ -1,7 +1,7 @@
include config.mk include config.mk
OBJS = csto.o callisto.o lcl.o lenviron.o lextra.o lfs.o ljson.o\ OBJS = csto.o callisto.o lcl.o lenviron.o lextra.o lfs.o ljson.o\
lpath.o lprocess.o util.o lprocess.o util.o
LIBS = liblua.a cjson.a LIBS = liblua.a cjson.a
all: csto libcallisto.a all: csto libcallisto.a
@ -23,7 +23,6 @@ lextra.o: lextra.c callisto.h util.h
lenviron.o: lenviron.c callisto.h lenviron.o: lenviron.c callisto.h
lfs.o: lfs.c callisto.h util.h lfs.o: lfs.c callisto.h util.h
ljson.o: ljson.c callisto.h ljson.o: ljson.c callisto.h
lpath.o: lpath.c callisto.h util.h
lprocess.o: lprocess.c callisto.h util.h lprocess.o: lprocess.c callisto.h util.h
util.o: util.c util.o: util.c

View file

@ -15,7 +15,6 @@ static const luaL_Reg loadedlibs[] = {
{CALLISTO_JSONLIBNAME, luaopen_json}, {CALLISTO_JSONLIBNAME, luaopen_json},
{LUA_MATHLIBNAME, luaopen_math}, {LUA_MATHLIBNAME, luaopen_math},
{LUA_OSLIBNAME, luaopen_os}, {LUA_OSLIBNAME, luaopen_os},
{CALLISTO_PATHLIBNAME, luaopen_path},
{CALLISTO_PROCLIBNAME, luaopen_process}, {CALLISTO_PROCLIBNAME, luaopen_process},
{NULL, NULL} {NULL, NULL}
}; };

View file

@ -22,7 +22,6 @@
#define CALLISTO_EXTLIBNAME "_G" /* global table */ #define CALLISTO_EXTLIBNAME "_G" /* global table */
#define CALLISTO_FSYSLIBNAME "fs" #define CALLISTO_FSYSLIBNAME "fs"
#define CALLISTO_JSONLIBNAME "json" #define CALLISTO_JSONLIBNAME "json"
#define CALLISTO_PATHLIBNAME "path"
#define CALLISTO_PROCLIBNAME "process" #define CALLISTO_PROCLIBNAME "process"
#define CALLISTO_ENVIRON "environ" #define CALLISTO_ENVIRON "environ"
@ -32,7 +31,6 @@ int luaopen_environ(lua_State *);
int luaopen_extra(lua_State *); int luaopen_extra(lua_State *);
int luaopen_fs(lua_State *); int luaopen_fs(lua_State *);
int luaopen_json(lua_State *); int luaopen_json(lua_State *);
int luaopen_path(lua_State *);
int luaopen_process(lua_State *); int luaopen_process(lua_State *);
lua_State *callisto_newstate(void); lua_State *callisto_newstate(void);

63
lfs.c
View file

@ -22,6 +22,37 @@
#include "callisto.h" #include "callisto.h"
#include "util.h" #include "util.h"
/***
* Returns the last component of the given pathname,
* removing any trailing '/' characters. If the given
* path consists entirely of '/' characters, the string
* `"/"` is returned. If *path* is an empty string,
* the string `"."` is returned.
*
* @function basename
* @usage
print("name of script file is " .. fs.basename(arg[0]))
assert(fs.basename("/etc/fstab") == "fstab")
* @tparam string path The path to process.
*/
static int
fs_basename(lua_State *L)
{
const char *ret;
char *path; /* parameter 1 (string) */
path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1));
ret = basename(path);
if (ret == NULL) /* failed? */
return lfail(L);
lua_pushstring(L, ret);
free(path);
return 1;
}
/*** /***
* Copies the contents of the file *source* to * Copies the contents of the file *source* to
* the file *target*. *target* will be overwritten * the file *target*. *target* will be overwritten
@ -86,6 +117,36 @@ finish:
return 1; return 1;
} }
/***
* Returns the parent directory of the pathname
* given. Any trailing '/' characters are not
* counted as part of the directory name.
* If the given path is an empty string or contains
* no '/' characters, the string `"."` is returned,
* signifying the current directory.
*
* @function dirname
* @usage assert(fs.dirname("/usr/local/bin") == "/usr/local")
* @tparam string path The path to process.
*/
static int
fs_dirname(lua_State *L)
{
const char *ret;
char *path; /* parameter 1 (string) */
path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1));
ret = dirname(path);
if (ret == NULL) /* failed? */
return lfail(L);
lua_pushstring(L, ret);
free(path);
return 1;
}
/*** /***
* Returns true if the given pathname exists * Returns true if the given pathname exists
* in the file system, or false if it does not. * in the file system, or false if it does not.
@ -388,7 +449,9 @@ fs_workdir(lua_State *L)
} }
static const luaL_Reg fslib[] = { static const luaL_Reg fslib[] = {
{"basename", fs_basename},
{"copy", fs_copy}, {"copy", fs_copy},
{"dirname", fs_dirname},
{"exists", fs_exists}, {"exists", fs_exists},
{"isdirectory", fs_isdirectory}, {"isdirectory", fs_isdirectory},
{"isfile", fs_isfile}, {"isfile", fs_isfile},

101
lpath.c
View file

@ -1,101 +0,0 @@
/***
* File system path manipulation.
*
* @module path
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
#include "callisto.h"
#include "util.h"
/***
* Returns the last component of the given pathname,
* removing any trailing '/' characters. If the given
* path consists entirely of '/' characters, the string
* `"/"` is returned. If *path* is an empty string,
* the string `"."` is returned.
*
* This function may return nil if the given
* pathname exceeds the system's path length
* limit (on most Linux systems this will be 4096).
*
* @function basename
* @usage path.basename(arg[0])
* @tparam string path The path to process.
*/
static int
path_basename(lua_State *L)
{
const char *ret;
char *path; /* parameter 1 (string) */
path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1));
ret = basename(path);
if (ret == NULL) /* failed? */
return lfail(L);
lua_pushstring(L, ret);
free(path);
return 1;
}
/***
* Returns the parent directory of the pathname
* given. Any trailing '/' characters are not
* counted as part of the directory name.
* If the given path is an empty string or contains
* no '/' characters, the string `"."` is returned,
* signifying the current directory.
*
* This function may return nil if the given
* pathname exceeds the system's path length
* limit (on most Linux systems this will be 4096).
*
* @function dirname
* @usage path.dirname(arg[0])
* @tparam string path The path to process.
*/
static int
path_dirname(lua_State *L)
{
const char *ret;
char *path; /* parameter 1 (string) */
path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1));
ret = dirname(path);
if (ret == NULL) /* failed? */
return lfail(L);
lua_pushstring(L, ret);
free(path);
return 1;
}
static const luaL_Reg pathlib[] = {
{"basename", path_basename},
{"dirname", path_dirname},
{NULL, NULL}
};
int
luaopen_path(lua_State *L)
{
luaL_newlib(L, pathlib);
return 1;
}