-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_error.c
120 lines (110 loc) · 3.13 KB
/
math_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* math_error - a simple libcalc math error routine
*
* Copyright (C) 1999 Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 1994/08/03 05:08:22
* File existed as early as: 1994
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
/*
* Your program MUST provide a function called math_error. This is called
* by the math routines on an error condition, such as malloc failures or a
* division by zero. The routine is called in the manner of printf, with a
* format string and optional arguments.
*
* By default, this routine simply prints a message to stderr and then exits.
*
* If one sets up calc_matherr_jmpbuf, and then sets calc_use_matherr_jmpbuf
* to non-zero then this routine will longjmp back with the return value of
* calc_use_matherr_jmpbuf. In addition, the last calc error message will be
* found in calc_use_matherr_jmpbuf_msg. This error is not printed to sttderr.
*
* For example:
*
* #include <setjmp.h>
* #include "lib_calc.h"
*
* int error;
*
* ...
*
* if ((error = setjmp(calc_matherr_jmpbuf)) != 0) {
*
* (* reinitialize calc after a longjmp *)
* reinitialize();
*
* (* report the error *)
* printf("Ouch: %s\n", calc_err_msg);
* }
* calc_use_matherr_jmpbuf = 1;
*/
#include <stdio.h>
#include <setjmp.h>
#include "args.h"
#include "calc.h"
#include "lib_calc.h"
/*
* math_error - print a math error and exit
*/
void
math_error(char *fmt, ...)
{
va_list ap;
/*
* format the error
*/
#ifdef VARARGS
va_start(ap);
#else
va_start(ap, fmt);
#endif
vsnprintf(calc_err_msg, MAXERROR, fmt, ap);
va_end(ap);
calc_err_msg[MAXERROR] = '\0';
/*
* if we should longjmp, so do
*/
if (calc_use_matherr_jmpbuf != 0) {
if (conf->calc_debug & CALCDBG_RUNSTATE)
printf("math_error: longjmp calc_matherr_jmpbuf\n");
longjmp(calc_matherr_jmpbuf, calc_use_matherr_jmpbuf);
}
/*
* print error message and edit
*/
(void) fflush(stdout);
(void) fflush(stderr);
fprintf(stderr, "%s\n\n", calc_err_msg);
/*
* if interactive, return to main via longjmp()
*/
if (calc_use_scanerr_jmpbuf != 0) {
if (conf->calc_debug & CALCDBG_RUNSTATE)
printf("math_error: longjmp calc_scanerr_jmpbuf\n");
longjmp(calc_scanerr_jmpbuf, calc_use_scanerr_jmpbuf);
}
/*
* exit
*/
if (conf->calc_debug & CALCDBG_RUNSTATE)
printf("math_error: about to exit\n");
libcalc_call_me_last();
exit(40);
}