From 8b1faad5a7583e3885d3a6ab6ccb3607ab95957a Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Sun, 10 Apr 2016 17:14:38 +0100 Subject: [PATCH 1/2] Add signal handler A signal handler to run some code when Unix signals are received --- lib/posix/posix.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index e932d2845a..35e666baf9 100644 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -2627,3 +2627,17 @@ proc utimes*(path: cstring, times: ptr array [2, Timeval]): int {. ## Returns zero on success. ## ## For more information read http://www.unix.com/man-page/posix/3/utimes/. + +proc handle_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {.importc: "signal", header: "".} + +template onSignal*(signals: varargs[cint], body: stmt): stmt {.immediate.} = + ## Setup code to be executed when Unix signals are received. Example: + ## from posix import SIGINT, SIGTERM + ## onSignal([SIGINT, SIGTERM]): + ## echo "bye" + + for s in signals: + handle_signal(s, + proc (sig: cint) {.noconv.} = + body + ) From 6d256ba76ed15f1d8ac91c7367593e7f3b47c88b Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Mon, 11 Apr 2016 12:07:45 +0100 Subject: [PATCH 2/2] Update posix.nim --- lib/posix/posix.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index 35e666baf9..19b068b601 100644 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -2630,10 +2630,10 @@ proc utimes*(path: cstring, times: ptr array [2, Timeval]): int {. proc handle_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {.importc: "signal", header: "".} -template onSignal*(signals: varargs[cint], body: stmt): stmt {.immediate.} = +template onSignal*(signals: varargs[cint], body: untyped): stmt = ## Setup code to be executed when Unix signals are received. Example: ## from posix import SIGINT, SIGTERM - ## onSignal([SIGINT, SIGTERM]): + ## onSignal(SIGINT, SIGTERM): ## echo "bye" for s in signals: