Reputation: 102
tcgetattr and tcsetattr in job contol
Hello everyone. I'm currently recreating a shell in C, which include jobcontrol.
I'm now recoding the fg built-in.
According to this and this documentation, it seems they save the termios in the job structure when creating new job, then restauring it with tcsetattr
.
I am trying to do (almost) the same. here is my code:
When I am creating a new job in my linked list:
static t_jobs_lst *new(void)
{
t_jobs_lst *job;
if (!(job = (t_jobs_lst *)malloc(sizeof(t_jobs_lst))))
return (NULL);
job->command = NULL;
job->job_number = g_shell.jobs.index++;
job->pid = 0;
job->childs = NULL;
job->current = '+';
job->state = "Running";
tcgetattr(STDERR_FILENO, &job->tmodes);
job->prev = NULL;
job->next = NULL;
jobs_insert(job);
return (job);
}
And when i try to set it in foreground:
int change_grp(t_shell *shell, int converted, int cont)
{
t_jobs_lst *searched;
int wpid;
int status;
signal(SIGTTOU, SIG_IGN);
searched = job_search(shell, converted);
if (tcsetpgrp(STDERR_FILENO, searched->pid) < 0)
return (EXIT_FAILURE);
ft_printf("%s\n", searched->command);
/* Send the job a continue signal, if necessary. */
if (cont)
{
tcsetattr(STDERR_FILENO, TCSADRAIN, &searched->tmodes); //causing bug in termcaps when uncommented
if (kill(-searched->pid, SIGCONT) < 0)
return (EXIT_FAILURE);
}
wpid = 0;
status = 0;
while (!wpid)
{
pause();
wpid = waitpid(searched->pid, &status, WNOHANG);
}
if (tcsetpgrp(0, shell->pid) < 0)
return (EXIT_FAILURE);
/* Restore the shell's terminal modes. */
tcgetattr (STDERR_FILENO, &searched->tmodes);
tcsetattr (STDERR_FILENO, TCSADRAIN, &shell->prev_term);
job_delete(shell, searched->pid);
return (EXIT_SUCCESS);
}
My question is: when tcsetattr(STDERR_FILENO, TCSADRAIN, &searched->tmodes); //causing bug in termcaps when uncommented
is uncommented, it seems to break my termcaps, as you can see in the following picture.
If this line is commented, I cannot type anything, only hit CTRL-C will give me the hand back on the shell.
I should be able to type AND to hit CTRL-C when i want to stop it, as said in the first link.
If the process group is launched as a background job, the shell should remain in the foreground itself and continue to read commands from the terminal.
What am I missing ?
Upvotes: 0
Views: 228