diff options
author | scratko <m@scratko.xyz> | 2024-06-23 22:35:56 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-06-23 23:40:20 +0300 |
commit | 821b146c54330cecba3ed2cc03a0f71ad83e540f (patch) | |
tree | 866fa9808cba7703aba4ce8c81a59c9b2eae76a8 /shell.c | |
parent | ef4604d9c146dbab1a653f66209103b74e6feb36 (diff) | |
download | shell-821b146c54330cecba3ed2cc03a0f71ad83e540f.tar.gz shell-821b146c54330cecba3ed2cc03a0f71ad83e540f.tar.bz2 shell-821b146c54330cecba3ed2cc03a0f71ad83e540f.zip |
Running processes in a single pgid.
Changing foreground group to the group of running processes (if readline doesn't
end with &).
Diffstat (limited to 'shell.c')
-rw-r--r-- | shell.c | 92 |
1 files changed, 68 insertions, 24 deletions
@@ -23,6 +23,22 @@ MAKE_QUEUE_PUSH(p_queue, pid, int) enum modes { word_separation, whole_word }; +static void change_terminal_settings(struct termios *cur_terminal_settings, + struct termios *save_terminal_settings) +{ + tcgetattr(0, save_terminal_settings); + memcpy(cur_terminal_settings, save_terminal_settings, + sizeof(*save_terminal_settings)); + cur_terminal_settings->c_lflag &= ~(ICANON | ECHO); + tcsetattr(0, TCSANOW, cur_terminal_settings); +} + +static void restore_terminal_settings(struct termios *cur_terminal_settings, + struct termios *save_terminal_settings) +{ + tcsetattr(0, TCSANOW, save_terminal_settings); +} + static void show_invitation() { printf("> "); @@ -59,6 +75,7 @@ static void init_params(struct param_type *params, enum modes *current_mode) params->last_execution_status = 0; params->pipeline = 0; params->new_readline = 1; + params->general_pgid = -1; *current_mode = word_separation; } @@ -90,6 +107,17 @@ static void reset_params(struct param_type *params, init_params(params, current_mode); } +static void prepare_for_next_command_on_line(struct param_type *params, + struct w_queue *word_chain, + struct c_queue *cmdlines, + struct dynamic_array *tmp_word) +{ + w_queue_clear(word_chain); + c_queue_clear(cmdlines); + dynarr_reset_array(tmp_word); + clear_filename(params); +} + static void add_letter(int ch, struct dynamic_array *tmp_word, struct param_type *params) { @@ -180,8 +208,11 @@ static void postprocessing(struct w_queue *word_chain, struct c_queue *cmdlines, { clean_up_memory(word_chain, cmdlines, params); close_files(input_fd, output_fd); - if(params->tokens != '&') + if(params->tokens != '&') { wait_for_process_to_complete(pid_store); + signal(SIGTTOU, SIG_IGN); + tcsetpgrp(0, getpid()); + } p_queue_clear(pid_store); } @@ -218,6 +249,15 @@ int is_stream_redirection_set(const struct param_type *params) params->streams.output_stream_to_append; } +static void identify_general_pgid(struct param_type *params, int pid) +{ + if(params->general_pgid == -1) + params->general_pgid = pid; + if(params->tokens != '&') + /* foreground group */ + tcsetpgrp(0, params->general_pgid); +} + static void make_pipeline(struct w_queue *word_chain, struct c_queue *cmdlines, struct param_type *params, int input_fd, int output_fd) @@ -233,12 +273,19 @@ static void make_pipeline(struct w_queue *word_chain, struct c_queue *cmdlines, set_signal_disposition(params); pid = fork(); + /* parent process */ + if(pid) + identify_general_pgid(params, pid); + if(pid == 0) { close(fd[0]); dup2(fd[1], 1); close(fd[1]); if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd, 1, 1); + /* change pgid */ + setpgid(getpid(), params->general_pgid == -1 ? + getpid() : params->general_pgid); execvp(cmdline[0], cmdline); perror(cmdline[0]); exit(1); @@ -270,6 +317,10 @@ static void make_pipeline(struct w_queue *word_chain, struct c_queue *cmdlines, if(c_queue_is_empty(cmdlines)) if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd, 1, 0); + + /* change pgid */ + setpgid(getpid(), params->general_pgid); + execvp(cmdline[0], cmdline); perror(cmdline[0]); exit(1); @@ -359,10 +410,17 @@ static void run_external_program(struct w_queue *word_chain, perror("fork error"); exit(1); } + /* parent process */ + if(pid) + identify_general_pgid(params, pid); + /* child process */ if(pid == 0) { if(is_stream_redirection_set(params)) change_streams(input_fd, output_fd, 0, 0); + /* change pgid */ + setpgid(getpid(), params->general_pgid == -1 ? + getpid() : params->general_pgid); execvp(cmdline[0], cmdline); params->last_execution_status = 1; perror(cmdline[0]); @@ -440,10 +498,13 @@ static void echo_characters(int ch) static void generate_readline(struct readline_type *readline) { + struct termios cur_terminal_settings, save_terminal_settings; enum keys found_key = none; int match_result; char ch; + change_terminal_settings(&cur_terminal_settings, &save_terminal_settings); + while((ch = getchar()) && !readline_termination_condition(ch, readline)) { if(ch == tab) { if(suggestions_for_filename(readline)) { @@ -494,6 +555,7 @@ static void generate_readline(struct readline_type *readline) /* print '\n' to end of readline */ if(ch == new_line) echo_characters(ch); + restore_terminal_settings(&cur_terminal_settings, &save_terminal_settings); } static void command_processing(struct param_type *params, @@ -546,13 +608,15 @@ static void command_processing(struct param_type *params, clean: error_identification(params); last_token = params->tokens; - reset_params(params, current_mode, word_chain, cmdlines, tmp_word, - readline); if(last_token == '&' || last_token == 0 || ch == new_line) { + reset_params(params, current_mode, word_chain, cmdlines, tmp_word, + readline); show_invitation(); generate_readline(readline); - } + } else + prepare_for_next_command_on_line(params, word_chain, cmdlines, + tmp_word); } static void word_separation_processing(int ch, struct param_type *params, @@ -651,35 +715,17 @@ static void whole_word_processing(int ch, struct param_type *params, add_letter(ch, tmp_word, params); } -static void change_terminal_settings(struct termios *cur_terminal_settings, - struct termios *save_terminal_settings) -{ - tcgetattr(0, save_terminal_settings); - memcpy(cur_terminal_settings, save_terminal_settings, - sizeof(*save_terminal_settings)); - cur_terminal_settings->c_lflag &= ~(ICANON | ECHO); - tcsetattr(0, TCSANOW, cur_terminal_settings); -} - -static void restore_terminal_settings(struct termios *cur_terminal_settings, - struct termios *save_terminal_settings) -{ - tcsetattr(0, TCSANOW, save_terminal_settings); -} - int main() { char ch; int i; struct param_type params; - struct termios cur_terminal_settings, save_terminal_settings; struct w_queue word_chain; struct c_queue cmdlines; struct dynamic_array tmp_word; struct readline_type readline; enum modes current_mode = word_separation; - change_terminal_settings(&cur_terminal_settings, &save_terminal_settings); w_queue_init(&word_chain); c_queue_init(&cmdlines); dynarr_create_array(&tmp_word); @@ -710,8 +756,6 @@ int main() &tmp_word); i = params.new_readline ? 0 : i + 1; } - - restore_terminal_settings(&cur_terminal_settings, &save_terminal_settings); putchar(new_line); dynarr_clear(&tmp_word); readline_clear(&readline); |