Signal handler example

Signal can report some more or less unexpected behavior to program, for example when user interrupts the program by pressing <ctrl-c>. Each signal delivered to program can be catched by declaring a simple sighandler block.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh signal_handler

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; ------------------------------------------------------------------------------
; signal_handler.ano
;
; To compile:
;
; $ ./build/ano ./examples/signal_handler/signal_handler.ano > \
;     engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		signal_handler
@ANO_SCRIPT_VERSION		0.0.1
@ANO_SCRIPT_DESCRIPTION	Simple signal handler
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; Signal can report some more or less unexpected behavior to program. Each
; signal delivered can be catched by declaring a simple sighandler block.
;

main {
	; Signal handlers need no initialization, just wait for signal.
	;
	print "Press <ctrl-c> or send HUP, INT, TERM, QUIT, USR1 or USR2" \
		" signal to this\nprocess. Waiting…\n"
}

sighandler hup {
	print "Hangup signal (SIGHUP) received.\n"
}

sighandler int {
	print "Interrupt signal (SIGINT) received.\n"
}

sighandler quit {
	print "Quit signal (SIGQUIT) received.\n"
}

sighandler term {
	print "Termination signal (SIGTERM) received.\n"
}

sighandler usr1 {
	print "User defined signal (SIGUSR1) received.\n"

	; User signal does not terminate process, so call exit() here.
	;
	exit
}

sighandler usr2 {
	print "User defined signal (SIGUSR2) received.\n"

	; User signal does not terminate process, so call exit() here.
	;
	exit
}
; ------------------------------------------------------------------------------
; signal_handler.w2c
;
; To compile:
;
; $ ./build/widget ./examples/signal_handler/signal_handler.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; signal_handler.m2c
;
; To compile:
;
; $ ./build/menu ./examples/signal_handler/signal_handler.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.