aboutsummaryrefslogtreecommitdiff
path: root/arch/casiowin/core/src/assert/assert.c
blob: 36237b7d7d7c89a5c114086cb2d422bdb6ef9703 (plain)
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
/* ****************************************************************************
 * assert/assert.c -- Make an assertion failed pop-up.
 *
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of the 'casiowin/core' module in libcarrot, an
 * experimental modular libc project.
 *
 * This file is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This file 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 Lesser Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this file. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern void __assert_PopUpWin _OF((int __n)) __THROW;
extern void __assert_Print    _OF((const unsigned char *__str)) __THROW;
extern void __assert_locate   _OF((int __x, int __y)) __THROW;
extern void __assert_putdisp  _OF((void)) __THROW;

/**
 *	__assert_fail:
 *	Make an assertion failed pop-up.
 *
 *	@arg	expr	the expression that failed.
 *	@arg	file	the name/path to the source file.
 *	@arg	line	the line number.
 *	@arg	func	the function name.
 */

void __assert_fail(const char *expr, const char *file,
	unsigned int line, const char *func)
{
	char f[22]; size_t n, fn;
	(void)func;

	/* prepare the top line */
	sprintf(f, "%d", line);
	n = strlen(f); fn = strlen(file);
	if (fn <= 21 - 1 - n) sprintf(f, "%s:%d", file, line);
	else {
		size_t newfn = 21 - 3 - 1 - n;
		file = &file[fn - newfn];
		fn = newfn;
		sprintf(f, "...%s:%d", file, line);
	}

	/* make the pop-up window */
	__assert_PopUpWin(4);
	__assert_locate(3, 2); __assert_Print((unsigned char*)f);
	__assert_locate(3, 3); __assert_Print((unsigned char*)"Assertion failed:");
	__assert_locate(4, 4); __assert_Print((unsigned char*)expr);
	__assert_locate(5, 3); __assert_Print((unsigned char*)"Exiting now.");
	__assert_putdisp();

	/* exit */
	abort();
}