/* Loesung von Jan Siersch */ #include #include #include #include #include #include "morse.h" #define DIT '.' #define DAH '-' #define LETTER_SIZE 8 volatile int sig = 0; volatile pid_t pid = 0; void sigAction(int _sig, siginfo_t *info, void *context) { sig = _sig; pid = info->si_pid; } int main(void) { // register action for the necessary signals int signals[] = {SIGUSR1, SIGUSR2, SIGTERM}; struct sigaction sigAct = {{0}}; sigAct.sa_sigaction = sigAction; sigAct.sa_flags = SA_SIGINFO; for (int i = 0; i < (sizeof(signals) / sizeof(int)); ++i) { sigaction(signals[i], &sigAct, 0); } // stores the currently received letter char letter[LETTER_SIZE] = {'\0'}; int index = 0; // wait for signals for(;;) { // try to avoid losing signals if (!sig) pause(); // make sure that letters are terminated at some point if (index > LETTER_SIZE - 1) { puts("input too long for a letter"); fflush(stdout); exit(EXIT_FAILURE); } // interprete the signal switch(sig) { case SIGUSR1: letter[index++] = DIT; break; case SIGUSR2: letter[index++] = DAH; break; case SIGTERM: letter[index++] = '\0'; index = 0; // decode and show letter unsigned char result = decode(letter); if (!result) { perror("could not decode letter"); exit(EXIT_FAILURE); } putchar(result); break; default: perror("unknown signal"); exit(EXIT_FAILURE); break; } fflush(stdout); sig = 0; // signal that we are ready for the next part of the letter if (kill(pid, SIGINT)) { perror("could not send receipt"); exit(EXIT_FAILURE); } } return EXIT_SUCCESS; }