add CTRL+C handler

This commit is contained in:
Alexander Popov 2023-07-30 00:52:44 +03:00
parent 9750dc3438
commit 9326e6e09a
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6

View File

@ -2,9 +2,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <signal.h>
/* Helper function for error handling. */ /* Helper function for error handling. */
int check(enum sp_return result); int check(enum sp_return result);
void handle_sigint(int sig);
bool INTERRUPT = false;
int main(int argc, char **argv) { int main(int argc, char **argv) {
struct sp_port *serial_port; struct sp_port *serial_port;
@ -59,9 +63,11 @@ int main(int argc, char **argv) {
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE)); check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
puts("Connected."); puts("Connected.");
signal(SIGINT, handle_sigint);
/* Reading lines from serial port. */ /* Reading lines from serial port. */
bool reading = true; bool reading = true;
while (reading) { while (reading && !INTERRUPT) {
int pos = 0; int pos = 0;
/* Character-by-character reading. */ /* Character-by-character reading. */
@ -119,3 +125,5 @@ int check(enum sp_return result) {
return result; return result;
} }
} }
void handle_sigint(int sig) { INTERRUPT = true; }