aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua_api.c
blob: 8314e2acce3f19cdae8f8a91eba3a4c965e7f68e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright (c) 2024, Lance Borden
All rights reserved.

This software is licensed under the BSD 3-Clause License.
You may obtain a copy of the license at:
https://opensource.org/licenses/BSD-3-Clause

Redistribution and use in source and binary forms, with or without
modification, are permitted under the conditions stated in the BSD 3-Clause
License.

THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "lua_api.h"
#include "history.h"
#include "lush.h"
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

// global for checking if debug_mode is toggled
static bool debug_mode = false;

// -- script execution --
void lua_load_script(lua_State *L, const char *script, char **args) {
	char script_path[512];
	// check if script is in the current directory
	if (access(script, F_OK) == 0) {
		snprintf(script_path, sizeof(script_path), "%s", script);
	} else {
		const char *home_dir = getenv("HOME");
		if (home_dir != NULL) {
			snprintf(script_path, sizeof(script_path), "%s/.lush/scripts/%s",
					 home_dir, script);

			if (access(script_path, F_OK) != 0) {
				// script not in either location
				fprintf(stderr, "[C] Script not found: %s\n", script);
				return;
			}
		} else {
			// HOME not set
			fprintf(stderr, "[C] HOME directory is not set.\n");
			return;
		}
	}
	// add args global if args were passed
	if (args[0] != NULL) {
		lua_newtable(L);
		for (int i = 0; args[i]; i++) {
			lua_pushstring(L, args[i]);
			// i + 1 since Lua is 1 based indexed
			lua_rawseti(L, -2, i + 1);
		}
		lua_setglobal(L, "args");
	}
	// if we got here the file exists
	if (luaL_dofile(L, script_path) != LUA_OK) {
		printf("[C] Error reading script\n");
		luaL_error(L, "Error: %s\n", lua_tostring(L, -1));
		// remove error from stack
		lua_pop(L, 1);
	}
}

// -- C funtions --
static int execute_command(lua_State *L, const char *line) {
	int status = 0;
	char **commands = lush_split_pipes((char *)line);
	char ***args = lush_split_args(commands, &status);
	if (status == -1) {
		fprintf(stderr, "lush: Expected end of quoted string\n");
	} else if (lush_run(L, args, status) == 0) {
		exit(1);
	}

	for (int i = 0; args[i]; i++) {
		free(args[i]);
	}
	lush_push_history(line);
	free(args);
	free(commands);
	return status;
}

static char *get_expanded_path(const char *check_item) {
	uid_t uid = getuid();
	struct passwd *pw = getpwuid(uid);
	if (!pw) {
		perror("retrieve home dir");
		return NULL;
	}

	if (check_item == NULL) {
		// passed nothing
		return NULL;
	}

	char 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, NULL);
	// if the path doesnt exist
	if (!exp_path) {
		return NULL;
	}

	return exp_path;
}

// -- Lua wrappers --
static int l_execute_command(lua_State *L) {
	const char *command = luaL_checkstring(L, 1);
	int status = execute_command(L, command);
	bool rc = status != -1 ? true : false;

	if (debug_mode) {
		if (rc)
			printf("Executed: %s, success\n", command);
		else
			printf("Executed: %s, failed\n", command);
	}

	lua_pushboolean(L, rc);
	return 1;
}

static int l_get_cwd(lua_State *L) {
	char *cwd = getcwd(NULL, 0);
	lua_pushstring(L, cwd);
	free(cwd);
	return 1;
}

static int l_debug(lua_State *L) {
	if (lua_isboolean(L, 1)) {
		debug_mode = lua_toboolean(L, 1);
	}
	return 0;
}

static int l_cd(lua_State *L) {
	const char *newdir = luaL_checkstring(L, 1);
	char *exp_path = get_expanded_path(newdir);
	// if the path doesnt exist
	if (exp_path == NULL) {
		lua_pushboolean(L, false);
		return 1;
	}

	if (chdir(exp_path) != 0) {
		perror("lush: cd");
		free(exp_path);
		lua_pushboolean(L, false);
	}

	lua_pushboolean(L, true);
	free(exp_path);
	return 1;
}

static int l_exists(lua_State *L) {
	const char *check_item = luaL_checkstring(L, 1);
	char *exp_path = get_expanded_path(check_item);
	// if the path doesnt exist
	if (exp_path == NULL) {
		lua_pushboolean(L, false);
		return 1;
	}
	lua_pushboolean(L, true);
	free(exp_path);
	return 1;
}

static int l_is_file(lua_State *L) {
	const char *check_item = luaL_checkstring(L, 1);
	char *exp_path = get_expanded_path(check_item);
	// if the path doesnt exist
	if (exp_path == NULL) {
		lua_pushboolean(L, false);
		return 1;
	}
	struct stat path_stat;
	stat(exp_path, &path_stat);
	bool rc = S_ISREG(path_stat.st_mode);
	lua_pushboolean(L, rc);
	free(exp_path);
	return 1;
}

static int l_is_dir(lua_State *L) {
	const char *check_item = luaL_checkstring(L, 1);
	char *exp_path = get_expanded_path(check_item);
	// if the path doesnt exist
	if (exp_path == NULL) {
		lua_pushboolean(L, false);
		return 1;
	}
	struct stat path_stat;
	stat(exp_path, &path_stat);
	bool rc = S_ISDIR(path_stat.st_mode);
	lua_pushboolean(L, rc);
	free(exp_path);
	return 1;
}

static int l_is_readable(lua_State *L) {
	const char *check_item = luaL_checkstring(L, 1);
	char *exp_path = get_expanded_path(check_item);
	// if the path doesnt exist
	if (exp_path == NULL) {
		lua_pushboolean(L, false);
		return 1;
	}
	bool rc = access(exp_path, R_OK) == 0;
	lua_pushboolean(L, rc);
	free(exp_path);
	return 1;
}

static int l_is_writeable(lua_State *L) {
	const char *check_item = luaL_checkstring(L, 1);
	char *exp_path = get_expanded_path(check_item);
	// if the path doesnt exist
	if (exp_path == NULL) {
		lua_pushboolean(L, false);
		return 1;
	}
	bool rc = access(exp_path, W_OK) == 0;
	lua_pushboolean(L, rc);
	free(exp_path);
	return 1;
}

// -- register Lua functions --

void lua_register_api(lua_State *L) {
	// global table for api functions
	lua_newtable(L);

	lua_pushcfunction(L, l_execute_command);
	lua_setfield(L, -2, "exec");
	lua_pushcfunction(L, l_get_cwd);
	lua_setfield(L, -2, "getcwd");
	lua_pushcfunction(L, l_debug);
	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");
	lua_pushcfunction(L, l_is_file);
	lua_setfield(L, -2, "isFile");
	lua_pushcfunction(L, l_is_dir);
	lua_setfield(L, -2, "isDirectory");
	lua_pushcfunction(L, l_is_readable);
	lua_setfield(L, -2, "isReadable");
	lua_pushcfunction(L, l_is_writeable);
	lua_setfield(L, -2, "isWriteable");
	// set the table as global
	lua_setglobal(L, "lush");
}