aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lua_api.c9
-rw-r--r--src/lush.c81
-rw-r--r--src/lush.h3
3 files changed, 89 insertions, 4 deletions
diff --git a/src/lua_api.c b/src/lua_api.c
index 2f967e6..21f5d39 100644
--- a/src/lua_api.c
+++ b/src/lua_api.c
@@ -325,6 +325,13 @@ static int l_set_prompt(lua_State *L) {
return 0;
}
+static int l_alias(lua_State *L) {
+ const char *alias = luaL_checkstring(L, 1);
+ const char *command = luaL_checkstring(L, 2);
+ lush_add_alias(alias, command);
+ return 0;
+}
+
// -- register Lua functions --
void lua_register_api(lua_State *L) {
@@ -361,6 +368,8 @@ void lua_register_api(lua_State *L) {
lua_setfield(L, -2, "unsetenv");
lua_pushcfunction(L, l_set_prompt);
lua_setfield(L, -2, "setPrompt");
+ lua_pushcfunction(L, l_alias);
+ lua_setfield(L, -2, "alias");
// set the table as global
lua_setglobal(L, "lush");
}
diff --git a/src/lush.c b/src/lush.c
index 83b5f08..bd8d4e8 100644
--- a/src/lush.c
+++ b/src/lush.c
@@ -16,6 +16,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "lush.h"
+#include "hashmap.h"
#include "help.h"
#include "history.h"
#include "lauxlib.h"
@@ -43,6 +44,20 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// initialize prompt format
char *prompt_format = NULL;
+// -- aliasing --
+hashmap_t *aliases = NULL;
+
+void lush_add_alias(const char *alias, const char *command) {
+ // make a new map if one doesnt exist
+ if (aliases == NULL) {
+ aliases = hm_new_hashmap();
+ }
+
+ hm_set(aliases, (char *)alias, (char *)command);
+}
+
+char *lush_get_alias(char *alias) { return hm_get(aliases, alias); }
+
// -- builtin functions --
char *builtin_strs[] = {"cd", "help", "exit", "time"};
@@ -196,7 +211,8 @@ static char *format_prompt_string(const char *input, const char *username,
// Allocate memory for the new string
char *result = (char *)malloc(new_size);
if (!result) {
- return NULL; // Handle memory allocation failure
+ perror("malloc failed");
+ return NULL;
}
// Replace placeholders in the input string and build the result string
@@ -260,6 +276,7 @@ static char *get_prompt() {
free(cwd);
return prompt;
}
+
size_t get_stripped_length(const char *str) {
size_t len = 0;
size_t i = 0;
@@ -471,6 +488,60 @@ char *lush_read_line() {
return buffer;
}
+// -- static helper for resolving aliases --
+static char *lush_resolve_aliases(char *line) {
+ // Allocate memory for the new string
+ char *result = (char *)malloc(BUFFER_SIZE);
+ if (!result) {
+ perror("malloc failed");
+ return NULL;
+ }
+
+ // Create a copy of the input line for tokenization
+ char *line_copy = strdup(line);
+ if (!line_copy) {
+ perror("strdup failed");
+ free(result);
+ return NULL;
+ }
+
+ // Start building the result string
+ char *dest = result;
+ char *arg = strtok(line_copy, " ");
+ while (arg != NULL) {
+ // Check shell aliases
+ if (aliases != NULL) {
+ char *alias = hm_get(aliases, arg);
+ if (alias != NULL) {
+ // Replace alias
+ strcpy(dest, alias);
+ dest += strlen(alias);
+ } else {
+ // No alias found, use the original token
+ strcpy(dest, arg);
+ dest += strlen(arg);
+ }
+ } else {
+ // No aliases set, just use the original token
+ strcpy(dest, arg);
+ dest += strlen(arg);
+ }
+
+ // Add a space after each token (if it's not the last one)
+ arg = strtok(NULL, " ");
+ if (arg != NULL) {
+ *dest++ = ' ';
+ }
+ }
+
+ *dest = '\0'; // Null-terminate the result string
+
+ // Clean up
+ free(line_copy);
+
+ return result;
+}
+
char **lush_split_pipes(char *line) {
char **commands = calloc(16, sizeof(char *));
if (!commands) {
@@ -685,7 +756,6 @@ int lush_run(lua_State *L, char ***commands, int num_commands) {
return ((*builtin_func[i])(L, commands));
}
}
-
return lush_execute_pipeline(commands, num_commands);
}
@@ -722,7 +792,8 @@ int main(int argc, char *argv[]) {
free(line);
continue;
}
- char **commands = lush_split_pipes(line);
+ char *expanded_line = lush_resolve_aliases(line);
+ char **commands = lush_split_pipes(expanded_line);
char ***args = lush_split_args(commands, &status);
if (status == -1) {
fprintf(stderr, "lush: Expected end of quoted string\n");
@@ -737,11 +808,13 @@ int main(int argc, char *argv[]) {
free(prompt);
free(args);
free(commands);
+ free(expanded_line);
free(line);
}
lua_close(L);
if (prompt_format != NULL)
free(prompt_format);
-
+ if (aliases != NULL)
+ free(aliases);
return 0;
}
diff --git a/src/lush.h b/src/lush.h
index ea575c7..27cf5db 100644
--- a/src/lush.h
+++ b/src/lush.h
@@ -20,6 +20,9 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <lua.h>
+void lush_add_alias(const char *alias, const char *command);
+char *lush_get_alias(char *alias);
+
int lush_cd(lua_State *L, char ***args);
int lush_help(lua_State *L, char ***args);
int lush_exit(lua_State *L, char ***args);