summary refs log tree commit diff stats
path: root/lib/posix/posix.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/posix/posix.nim')
-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..19b068b60 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: untyped): stmt = 
+  ## 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 
+    )
/a> 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226