citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 055af01935ad12bcbece7622037131e4a74d3ede
parent ac9d1b2839d250dc9f992fbe7c543f7ea1786e31
Author: Kyle Milz <kyle@0x30.net>
Date:   Sat, 30 Jul 2016 19:49:00 -0600

src: let af_unix throw exceptions on errors

Diffstat:
Msrc/af_unix.cc | 3++-
Msrc/term_main.cc | 23+++++++++++++++++------
2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/af_unix.cc b/src/af_unix.cc @@ -20,6 +20,7 @@ #include <cstring> // memset, strcpy #include <fcntl.h> // fcntl, F_GETFL #include <iostream> +#include <stdexcept> #include <unistd.h> // close #include "af_unix.h" @@ -112,7 +113,7 @@ af_unix::write_all(uint8_t *buf, size_t bytes_total) n = write(m_fd, buf + bytes_wrote, bytes_left); if (n < 0) - err(1, "write()"); + throw std::runtime_error("write failed"); bytes_wrote += n; bytes_left -= n; diff --git a/src/term_main.cc b/src/term_main.cc @@ -2,16 +2,17 @@ // Original idea from Max Zunti. // #include <cassert> +#include <csignal> // sigaction +#include <cstdlib> // exit #include <iostream> #include <ncurses.h> #include <queue> -#include <stdlib.h> // exit +#include <stdexcept> // runtime_error #include <time.h> // clock_gettime, nanosleep #include "af_unix.h" #include "runtime.hh" - class CursesViewer : public RuntimeProcess { public: CursesViewer(af_unix &socket); @@ -89,7 +90,7 @@ CursesViewer::get_keyboard() int ch = getch(); if (ch == 'q') - exit(0); + throw std::runtime_error("quit"); else if (ch == 'l' && m_tu < (m_num_tus - 1)) m_tu++; else if (ch == 'h' && m_tu > 0) @@ -223,7 +224,7 @@ main(int argc, char *argv[]) if (has_colors() == FALSE) { endwin(); printf("Your terminal does not support color\n"); - exit(1); + std::exit(1); } start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); @@ -235,12 +236,22 @@ main(int argc, char *argv[]) printw("Waiting for connection on /tmp/citrun.socket\n"); refresh(); + // Block SIGPIPE so that write() will generate errors. + sigset_t mask_set; + sigemptyset(&mask_set); + sigaddset(&mask_set, SIGPIPE); + sigprocmask(SIG_BLOCK, &mask_set, NULL); + af_unix *client = listen_sock.accept(); if (client == NULL) errx(1, "client was NULL"); - CursesViewer conn(*client); - conn.loop(); + try { + CursesViewer conn(*client); + conn.loop(); + } catch (const std::exception &e) { + std::cerr << "ERROR: " << e.what(); + } endwin();