ack/lang/basic/lib/trap.c

58 lines
868 B
C
Raw Permalink Normal View History

#include <stdlib.h>
#include <stdio.h>
1984-11-29 14:22:02 +00:00
#include <signal.h>
#include <setjmp.h>
#include "lib.h"
1984-11-29 14:22:02 +00:00
1991-09-19 10:38:40 +00:00
#ifndef NSIG
#define NSIG _NSIG
#endif
1984-11-29 14:22:02 +00:00
/* Trap handling */
int _trpline; /* BASIC return label */
jmp_buf trpbuf;
1984-11-29 14:22:02 +00:00
2016-12-12 20:15:25 +00:00
void _trpset(int nr)
1984-11-29 14:22:02 +00:00
{
/*debug printf("trap set to %d\n",nr);*/
_trpline = nr;
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _trpfatal(int i)
1984-11-29 14:22:02 +00:00
{
extern int _errsym, _erlsym;
1984-11-29 14:22:02 +00:00
_errsym = i;
1984-11-29 14:22:02 +00:00
_setline();
if (_trpline == 0)
printf("LINE %d: FATAL ERROR: trap %d\n", _erlsym, i);
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf("trap occurred %d return %d\n", i, _trpline);
1984-11-29 14:22:02 +00:00
#endif
_trap();
}
2016-12-12 20:15:25 +00:00
void _ini_trp(void)
1984-11-29 14:22:02 +00:00
{
/* initialize trap routines */
1993-11-17 16:27:44 +00:00
int i;
1984-11-29 14:22:02 +00:00
for (i = 0; i < NSIG; i++)
signal(i, _trpfatal);
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _settrap(int nr)
1984-11-29 14:22:02 +00:00
{
_trpline = nr;
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _trap(void)
1984-11-29 14:22:02 +00:00
{
int line;
if (_trpline == 0)
exit(-1);
line = _trpline;
_trpline = 0; /* should be reset by user */
1984-11-29 14:22:02 +00:00
_ini_trp();
longjmp(trpbuf, line);
1984-11-29 14:22:02 +00:00
}