I currently making a project to write my own shell, a very simple one, but i got stuck on how I can differentiate a SIGINT signal received inside a heredoc and while interactive mode.
In bash, consider this input:
cat << eof
it will open input lines until the "eof" delimiter is meet or a signal is detected.
and this is how the bash behave receiving a SIGINT outside a heredoc:
the question is, how bash handles these signals differently? the heredoc is run as a child process from the shell, so they share the same signals handlers, since they will have different behaviors (in heredoc SIGINT ends the process, in interactive mode SIGINT is ignored)
how bash handles these signals differently?
There are several ways to handle signals differently in the same application.
signal(SIG_INT, handler_for_ABC)
and do a signal(SIG_INT, normal_handler)
upon returning to "wait for user" state of your application.enum
data type. And in the handler you just switch(app_state) { case ABC: do_abc_handling(); break; default do_normal_handling()}
. Just do not forget to reassign this global flag as needed.pthread_self()
) and use it to determine which set of handlers are appropriate in this case.There are several other ways to have a single-entry with multiple paths approaches. Just choose whatever is more convenient to you.