<commit_before>/***********************************************************************
**
**  REBOL [R3] Language Interpreter and Run-time Environment
**
**  Copyright 2012 REBOL Technologies
**  REBOL is a trademark of REBOL Technologies
**
**  Licensed under the Apache License, Version 2.0 (the "License");
**  you may not use this file except in compliance with the License.
**  You may obtain a copy of the License at
**
**  http://www.apache.org/licenses/LICENSE-2.0
**
**  Unless required by applicable law or agreed to in writing, software
**  distributed under the License is distributed on an "AS IS" BASIS,
**  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
**  See the License for the specific language governing permissions and
**  limitations under the License.
**
************************************************************************
**
**  Title: POSIX Exit and Error Functions
**  Purpose:
**		...
**
***********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "reb-host.h"


/***********************************************************************
**
*/	void OS_Exit(int code)
/*
**		Called in cases where REBOL needs to quit immediately
**		without returning from the main() function.
**
***********************************************************************/
{
	//OS_Call_Device(RDI_STDIO, RDC_CLOSE); // close echo
	OS_Quit_Devices(0);
#ifndef REB_CORE
	OS_Destroy_Graphics();
#endif
	exit(code);
}

static const void * backtrace_buf [1024];
/***********************************************************************
**
** coverity[+kill]
*/	void OS_Crash(const REBYTE *title, const REBYTE *content)
/*
**		Tell user that REBOL has crashed. This function must use
**		the most obvious and reliable method of displaying the
**		crash message.
**
**		If the title is NULL, then REBOL is running in a server mode.
**		In that case, we do not want the crash message to appear on
**		the screen, because the system may be unattended.
**
**		On some systems, the error may be recorded in the system log.
**
***********************************************************************/
{
	// Echo crash message if echo file is open:
	///PUTE(content);
	OS_Call_Device(RDI_STDIO, RDC_CLOSE); // close echo

	// A title tells us we should alert the user:
	if (title) {
		fputs(cs_cast(title), stderr);
		fputs(":\n", stderr);
	}
	fputs(cs_cast(content), stderr);
	fputs("\n\n", stderr);
#ifdef backtrace  // A GNU extension
	fputs("Backtrace:\n", stderr);
	int n_backtrace = backtrace(backtrace_buf, sizeof(backtrace_buf)/sizeof(backtrace_buf[0]));
	backtrace_symbols_fd(backtrace_buf, n_backtrace, STDERR_FILENO);
#endif
	exit(100);
}


/***********************************************************************
**
*/	REBCHR *OS_Form_Error(int errnum, REBCHR *str, int len)
/*
**		Translate OS error into a string. The str is the string
**		buffer and the len is the length of the buffer.
**
***********************************************************************/
{
	// strerror() is not thread-safe, but strerror_r is. Unfortunately, at
	// least in glibc, there are two different protocols for strerror_r(),
	// depending on whether you are using the POSIX-compliant
	// implementation or the GNU implementation. The convoluted test below
	// is the inversion of the actual test recommended by glibc to discern
	// the version of strerror_r() provided. As other, non-glibc
	// implementations (such as OS X's libSystem) also provide the
	// POSIX-compliant version, we invert the test: explicitly use the
	// older GNU implementation when we are sure about it, and use the
	// more modern POSIX-compliant version otherwise. Finally, we only
	// attempt this feature detection when using glibc (__GNU_LIBRARY__),
	// as this particular combination of the (more widely standardised)
	// _POSIX_C_SOURCE and _XOPEN_SOURCE defines might mean something<commit_msg>Use EXIT_FAILURE as less arbitrary exit code for hard crashes<commit_after>/***********************************************************************
**
**  REBOL [R3] Language Interpreter and Run-time Environment
**
**  Copyright 2012 REBOL Technologies
**  REBOL is a trademark of REBOL Technologies
**
**  Licensed under the Apache License, Version 2.0 (the "License");
**  you may not use this file except in compliance with the License.
**  You may obtain a copy of the License at
**
**  http://www.apache.org/licenses/LICENSE-2.0
**
**  Unless required by applicable law or agreed to in writing, software
**  distributed under the License is distributed on an "AS IS" BASIS,
**  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
**  See the License for the specific language governing permissions and
**  limitations under the License.
**
************************************************************************
**
**  Title: POSIX Exit and Error Functions
**  Purpose:
**		...
**
***********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "reb-host.h"


/***********************************************************************
**
*/	void OS_Exit(int code)
/*
**		Called in cases where REBOL needs to quit immediately
**		without returning from the main() function.
**
***********************************************************************/
{
	//OS_Call_Device(RDI_STDIO, RDC_CLOSE); // close echo
	OS_Quit_Devices(0);
#ifndef REB_CORE
	OS_Destroy_Graphics();
#endif
	exit(code);
}

static const void * backtrace_buf [1024];
/***********************************************************************
**
** coverity[+kill]
*/	void OS_Crash(const REBYTE *title, const REBYTE *content)
/*
**		Tell user that REBOL has crashed. This function must use
**		the most obvious and reliable method of displaying the
**		crash message.
**
**		If the title is NULL, then REBOL is running in a server mode.
**		In that case, we do not want the crash message to appear on
**		the screen, because the system may be unattended.
**
**		On some systems, the error may be recorded in the system log.
**
***********************************************************************/
{
	// Echo crash message if echo file is open:
	///PUTE(content);
	OS_Call_Device(RDI_STDIO, RDC_CLOSE); // close echo

	// A title tells us we should alert the user:
	if (title) {
		fputs(cs_cast(title), stderr);
		fputs(":\n", stderr);
	}
	fputs(cs_cast(content), stderr);
	fputs("\n\n", stderr);
#ifdef backtrace  // A GNU extension
	fputs("Backtrace:\n", stderr);
	int n_backtrace = backtrace(backtrace_buf, sizeof(backtrace_buf)/sizeof(backtrace_buf[0]));
	backtrace_symbols_fd(backtrace_buf, n_backtrace, STDERR_FILENO);
#endif

	exit(EXIT_FAILURE);
}


/***********************************************************************
**
*/	REBCHR *OS_Form_Error(int errnum, REBCHR *str, int len)
/*
**		Translate OS error into a string. The str is the string
**		buffer and the len is the length of the buffer.
**
***********************************************************************/
{
	// strerror() is not thread-safe, but strerror_r is. Unfortunately, at
	// least in glibc, there are two different protocols for strerror_r(),
	// depending on whether you are using the POSIX-compliant
	// implementation or the GNU implementation. The convoluted test below
	// is the inversion of the actual test recommended by glibc to discern
	// the version of strerror_r() provided. As other, non-glibc
	// implementations (such as OS X's libSystem) also provide the
	// POSIX-compliant version, we invert the test: explicitly use the
	// older GNU implementation when we are sure about it, and use the
	// more modern POSIX-compliant version otherwise. Finally, we only
	// attempt this feature detection when using glibc (__GNU_LIBRARY__),
	// as this particular combination of the (more widely standardised)
	// _POSIX_C_SOURCE and _XOPEN_SOURCE defines might mean something
{'fraction_non_alphanumeric': 0.24507042253521127, 'fraction_numerical': 0.0038732394366197184, 'mean_word_length': 4.888735314443677, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2196199', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2393, 'n_words': 884}