summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFederico Ceratto <federico.ceratto@gmail.com>2016-04-10 17:14:38 +0100
committerFederico Ceratto <federico.ceratto@gmail.com>2016-04-10 17:14:38 +0100
commit8b1faad5a7583e3885d3a6ab6ccb3607ab95957a (patch)
treee14bcea7dd6eaa53868658c2495d37c610d378ef
parentd6cf109952d71fafbd878920f3d4b4a297a57ad1 (diff)
downloadNim-8b1faad5a7583e3885d3a6ab6ccb3607ab95957a.tar.gz
Add signal handler
A signal handler to run some code when Unix signals are received
-rw-r--r--lib/posix/posix.nim14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim
index e932d2845..35e666baf 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: "<signal.h>".} 
+ 
+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 
+    )