aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--.lush/scripts/example.lua5
-rw-r--r--src/lua_api.c42
2 files changed, 47 insertions, 0 deletions
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua
index 8da0435..04cb516 100644
--- a/.lush/scripts/example.lua
+++ b/.lush/scripts/example.lua
@@ -44,3 +44,8 @@ print(cwd)
lush.cd("~/.lush/scripts")
lush.exec('cat "example.lua" | grep "hello" | sort | uniq')
lush.cd(cwd)
+
+-- exists allows you to check if a file or directory exists
+if lush.exists("~/.lush/scripts/example.lua") then
+ print("example.lua exists")
+end
diff --git a/src/lua_api.c b/src/lua_api.c
index 4dc2149..9f4bdc8 100644
--- a/src/lua_api.c
+++ b/src/lua_api.c
@@ -154,6 +154,46 @@ static int l_cd(lua_State *L) {
return 1;
}
+static int l_exists(lua_State *L) {
+ bool rc;
+ uid_t uid = getuid();
+ struct passwd *pw = getpwuid(uid);
+ if (!pw) {
+ perror("retrieve home dir");
+ rc = false;
+ lua_pushboolean(L, rc);
+ return 1;
+ }
+
+ const char *check_item = luaL_checkstring(L, 1);
+ if (check_item == NULL) {
+ // passed nothing
+ rc = false;
+ lua_pushboolean(L, rc);
+ return 1;
+ } else {
+ char path[PATH_MAX];
+ char extended_path[PATH_MAX];
+ char *tilda = strchr(check_item, '~');
+ if (tilda) {
+ strcpy(path, pw->pw_dir);
+ strcat(path, tilda + 1);
+ } else {
+ strcpy(path, check_item);
+ }
+ char *exp_path = realpath(path, extended_path);
+ // if the path doesnt exist
+ if (!exp_path) {
+ rc = false;
+ lua_pushboolean(L, rc);
+ return 1;
+ }
+ }
+ rc = true;
+ lua_pushboolean(L, rc);
+ return 1;
+}
+
// -- register Lua functions --
void lua_register_api(lua_State *L) {
@@ -168,6 +208,8 @@ void lua_register_api(lua_State *L) {
lua_setfield(L, -2, "debug");
lua_pushcfunction(L, l_cd);
lua_setfield(L, -2, "cd");
+ lua_pushcfunction(L, l_exists);
+ lua_setfield(L, -2, "exists");
// set the table as global
lua_setglobal(L, "lush");
}
ded tag 0.4.1 for changeset 71388899ac09Gravatar Enno Boland (tox) 1-0/+1 2010-06-08next will be 0.4.1Gravatar Enno Boland (tox) 1-1/+1 2010-06-03reverting sessiontimeGravatar Enno Boland (tox) 1-1/+1 2010-05-30Added tag 0.4 for changeset ac8e058003edGravatar Enno Boland (tox) 1-0/+1 2010-05-28Adding Nibbles patchGravatar Enno Boland (tox) 2-14/+8 2010-05-26typoGravatar Enno Boland (tox) 1-2/+2 2010-05-26Applying Nibbles download patch. Thanks!Gravatar Enno Boland (tox) 2-0/+20 2010-05-25changing user agent string as suggested by Marvin VekGravatar Enno Boland (tox) 1-1/+1 2010-05-24removing spatial navigation.Gravatar Enno Boland (tox) 1-1/+0 2010-05-24implementing downloading.Gravatar Enno Boland (tox) 1-1/+9 2010-05-24enabling spatial-navigationGravatar Enno Boland (tox) 1-0/+1 2010-05-18AtomHiLight is set correctly for links.Gravatar Enno Boland (tox) 1-1/+21 2010-05-17changing xpropsGravatar Enno Boland (tox) 2-32/+35 2010-05-17dl is not needed anymoreGravatar Enno Boland (tox) 2-4/+1 2010-05-17removing context-menu, downloading.Gravatar Enno Boland (tox) 2-122/+8 2010-05-15changing sessiontime to 3600Gravatar Enno Boland (tox) 1-1/+1 2010-05-11fix download/history bugGravatar Enno Boland (tox) 1-5/+0 2010-05-09Fix NOBACKGROUND meaningGravatar pancake 2-2/+2 2010-05-09changing default value of NOBACKGROUNDGravatar Enno Boland (tox) 1-1/+1 2010-05-09added js-fix by Troels Henriksen. Thanks :)Gravatar Enno Boland (tox) 1-9/+17 2010-05-08Do not set cookie session time if set to 0Gravatar pancake 2-2/+5 2010-05-06implementing naive file locking.Gravatar Enno Boland (tox) 1-0/+7 2010-05-06fixing compilerwarning in newer webkit versionsGravatar Enno Boland (tox) 1-2/+1 2010-05-06fixing config.mkGravatar Enno Boland (tox) 1-1/+1 2010-05-02Adding small fix by Alex Puterbaugh. Thanks.Gravatar Enno Boland (tox) 2-7/+6 2010-04-08remove empty linesGravatar pancake 1-3/+1 2010-03-29whoops... linking OoGravatar Enno Boland (tox) 1-3/+3 2010-03-26typofix.Gravatar Enno Boland (tox) 2-5/+5 2010-03-25reimplementing cookies. Now we need some file locking.Gravatar Enno Boland (tox) 1-2/+14 2010-03-25updating manpage, reformating help output.Gravatar Enno Boland (tox) 2-9/+19 2010-03-24fixing argument parsing.Gravatar Enno Boland (tox) 1-0/+2 2010-03-24fixing downloadsGravatar Enno Boland (tox) 1-8/+6 2010-03-24gotheaders will now be called correctly.Gravatar Enno Boland (tox) 1-1/+6