File: math_error.c

package info (click to toggle)
apcalc 2.12.1.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,548 kB
  • ctags: 4,129
  • sloc: ansic: 53,374; makefile: 5,589; awk: 96; sed: 33; sh: 13
file content (118 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download
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
/*
 * 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.
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * @(#) $Revision: 29.3 $
 * @(#) $Id: math_error.c,v 29.3 2006/06/02 09:52:22 chongo Exp $
 * @(#) $Source: /usr/local/src/cmd/calc/RCS/math_error.c,v $
 *
 * 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_jmp_buf, and then sets calc_jmp to non-zero then
 * this routine will longjmp back (with the value of calc_jmp) instead.
 * In addition, the last calc error message will be found in calc_error;
 * this error is not printed to sttderr.
 *
 * For example:
 *
 *	#include <setjmp.h>
 *
 *	extern jmp_buf calc_jmp_buf;
 *	extern int calc_jmp;
 *	extern char *calc_error;
 *	int error;
 *
 *	...
 *
 *	if ((error = setjmp(calc_jmp_buf)) != 0) {
 *
 *		(* reinitialize calc after a longjmp *)
 *		reinitialize();
 *
 *		(* report the error *)
 *		printf("Ouch: %s\n", calc_error);
 *	}
 *	calc_jmp = 1;
 */


#include <stdio.h>
#include <setjmp.h>
#include "args.h"
#include "calc.h"
#include "math_error.h"


/*
 * error jump point we will longjmp to this jmp_buf if calc_jmp is non-zero
 */
jmp_buf calc_jmp_buf;
int calc_jmp = 0;		/* non-zero => use calc_jmp_buf */
char calc_error[MAXERROR+1];	/* last calc error message */


/*
 * 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_error, MAXERROR, fmt, ap);
	va_end(ap);
	calc_error[MAXERROR] = '\0';

	/*
	 * if we should longjmp, so do
	 */
	if (calc_jmp != 0) {
		longjmp(calc_jmp_buf, calc_jmp);
	}

	/*
	 * print error message and edit
	 */
	(void) fflush(stdout);
	(void) fflush(stderr);
	fprintf(stderr, "%s\n", calc_error);
	fputc('\n', stderr);
	libcalc_call_me_last();
	exit(1);
}