Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
CC = gcc
CFLAGS = -Wall -g
LDFLAGS = -lreadline
OBJS = main.o parser.o builtins.o executor.o prompt.o history.o
OBJS = main.o parser.o builtins.o executor.o prompt.o history.o signals.o
TARGET = myshell

all: $(TARGET)
Expand Down
3 changes: 3 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include "builtins.h"
#include "prompt.h"
#include "history.h"
#include "signals.h"

int main() {
char *input;

setup_signal_handlers();

while (1) {
input = get_prompt(); // custom prompt with readline

Expand Down
28 changes: 28 additions & 0 deletions signals.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>

/*
Signal handler for SIGINT (Ctrl+C).
It clears the current input line and displays a new prompt.
*/
void sigint_handler(int signo) {
(void)signo;
printf("\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}

/*
Sets up the signal handlers for the shell.
- SIGINT (Ctrl+C) is caught to prevent the shell from exiting.
- SIGTSTP (Ctrl+Z) is ignored to prevent the shell from being suspended.
- SIGQUIT (Ctrl+\) is also ignored.
*/
void setup_signal_handlers(void) {
signal(SIGINT, sigint_handler);
signal(SIGTSTP, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
}
6 changes: 6 additions & 0 deletions signals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SIGNALS_H
#define SIGNALS_H

void setup_signal_handlers(void);

#endif
Loading