I'm rewriting a shell in C and I fell on a problem.
When writing a command — for example echo "this
— we got a new prompt ("dquote>", in zsh) and we can exit it with "Ctrl + c" and get back to our last command prompt.
I'm stuck there; I just can't get out of my read function (listen on "dquote>"), I tried to write on stdout an EOF when pressing "ctrl + c" but it doesn't read it.
I switched to non-canonical mode.
I catch signal with signal(SIGINT, sig_hand);
then i execute this part of code when signal is catched:
static void sig_hand(int sig)
{
if (g_shell.is_listen_bracket) // if is the first prompt or no
putchar(4); // EOT
else
{
putstr("\n");
print_prompt();
}
}
and my read function:
int j;
char command[ARG_MAX];
char buff[3];
j = -1;
while (1)
{
bzero(buff, 3);
read(0, buff, 3);
if (buff[0] == 4 && !buff[1] && !buff[2])
return (ctrl_d(shell));
else if (isprint(buff[0]) && !buff[1] && !buff[2]) // if is between 32 and 126 (ascii)
{
command[++j] = buff[0];
putchar(buff[0]);
}
}
command[++j] = '\0';
return (strdup(command));
So my code waiting on "read(0, buff, 3);", and i want to quit it when pressing ctrl + c.
Thanks for helping !