Created
April 7, 2011 21:32
-
-
Save dom96/908782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Module for handling and raising signals. Wraps ``<signals.h>``. | |
type | |
TSignal = enum | |
SIGINT = 2 | |
proc signal*(sig: cint, func: pointer) {.importc: "signal", header: "<signal.h>".} | |
template atSignal*(s: TSignal, actions: stmt): stmt = | |
proc callback(sig: cint) = | |
actions | |
signal(int(s), callback) | |
when isMainModule: | |
atSignal(SIGINT): | |
echo("Called back!") | |
quit() | |
while True: | |
nil |
Here's the same example using sigaction instead of signal, and using the Nim posix
module. I learn that signal
is deprecated and sigaction
should be used instead:
import posix
type
StdSignal* = enum
SIGHUP = 1 ## 1. Hangup
SIGINT ## 2. Terminal interrupt signal
# https://gist.github.com/dom96/908782
template onSignal*(sigs: varargs[StdSignal]; actions: untyped): untyped =
# https://www.gnu.org/software/libc/manual/html_node/Sigaction-Function-Example.html#Sigaction-Function-Example
proc newHandler(signum: cint) {.noconv.} =
echo "\n[Error] Standard signal " & $StdSignal(signum) & " detected"
`actions`
var
newAction: SigAction
newAction.sa_handler = newHandler
discard sigemptyset(newAction.sa_mask)
newAction.sa_flags = 0
for sig in sigs:
discard sigaction(cint(sig), newAction, nil)
when isMainModule:
onSignal(SIGFPE, SIGINT):
quit()
echo "Hit Ctrl-C to end this program .."
discard pause() # Hang the program over here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a working version of above code on Nim devel as of 2019/05/02: