diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-01 14:53:14 -0400 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2025-11-01 14:53:14 -0400 |
| commit | ddcf611815c145b1fadca042e65648a7a81cc497 (patch) | |
| tree | 9e11e5a372f2df528d162d17050eada50ad1db56 /src/hush.c | |
| parent | 406889ed5c780f0e28703b143c72bbf035280b25 (diff) | |
| download | hush-ddcf611815c145b1fadca042e65648a7a81cc497.tar.gz | |
Added variable support
Diffstat (limited to 'src/hush.c')
| -rw-r--r-- | src/hush.c | 38 |
1 files changed, 35 insertions, 3 deletions
@@ -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(); } |