aboutsummaryrefslogtreecommitdiffstats
path: root/src/hush.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hush.c')
-rw-r--r--src/hush.c38
1 files changed, 35 insertions, 3 deletions
diff --git a/src/hush.c b/src/hush.c
index 557ae1e..0d25244 100644
--- a/src/hush.c
+++ b/src/hush.c
@@ -14,6 +14,7 @@
#include "config.h"
#include "lexer.h"
#include "command.h"
+#include "variables.h"
FILE *open_input(int argc, char **argv) {
FILE *input_file = (argc > 1) ? fopen(argv[1], "r") : stdin;
@@ -34,10 +35,38 @@ static size_t get_command_len(char *cmdstr) {
return len;
}
+void variable_substitution(command *cmds) {
+ for (command *cmd = cmds; cmd; cmd = cmd->next) {
+ fprintf(stderr, "Before substitution\n");
+ print_commands(stderr, cmd);
+ for (size_t i=0; i < MAX_ARGUMENT_CNT + 1; i++) {
+ if (is_variable(cmd->args[i])) {
+ cmd->args[i] = get_variable(cmd->args[i]);
+ }
+ }
+
+ if (is_variable(cmd->outfile)) {
+ cmd->outfile = get_variable(cmd->outfile);
+ }
+
+ if (is_variable(cmd->infile)) {
+ cmd->infile = get_variable(cmd->infile);
+ }
+
+ fprintf(stderr, "After substitution\n");
+ print_commands(stderr, cmd);
+ }
+}
+
int main(int argc, char **argv) {
FILE *input_file = open_input(argc, argv);
char buffer[MAX_LINE_LEN];
+ if (!init_variable_store()) {
+ fprintf(stderr, "ERROR: Failed to initialize variable store\n");
+ exit(EXIT_FAILURE);
+ }
+
fprintf(stdout, "$ ");
while (fgets(buffer, MAX_LINE_LEN, input_file)) {
char *cmdstr = trim(buffer);
@@ -68,7 +97,9 @@ int main(int argc, char **argv) {
// print_parsed_command(stdout, parsed_cmd);
if (parsed_cmd->type == TKN_VARKEY) {
- /* handle variable creation */
+ if (!add_variable(parsed_cmd->text, parsed_cmd->next->text)) {
+ fprintf(stderr, "ERROR: Failed to create variable\n");
+ }
goto free_tokens;
}
@@ -79,6 +110,7 @@ int main(int argc, char **argv) {
goto free_tokens;
}
+ variable_substitution(cmds);
for (command *cmd = cmds; cmd; cmd = cmd->next) {
pid_t result = execute_command(cmd);
@@ -91,9 +123,7 @@ int main(int argc, char **argv) {
for (command *cmd = cmds; cmd; cmd = cmd->next) {
if (cmd->pid > 0) {
- fprintf(stderr, "Waiting for %d\n", cmd->pid);
waitpid(cmd->pid, NULL, 0);
- fprintf(stderr, "%d completed\n", cmd->pid);
}
}
@@ -107,4 +137,6 @@ int main(int argc, char **argv) {
draw_prompt:
fprintf(stdout, "$ ");
}
+
+ destroy_variable_store();
}