#line 982 "../../src/builtin/snarf.m4"
/* -*- buffer-read-only: t -*- vi: set ro:
   THIS FILE IS GENERATED AUTOMATICALLY.  PLEASE DO NOT EDIT.
*/
#line 982
#ifdef HAVE_CONFIG_H
#line 982
# include <config.h>
#line 982
#endif
#line 982
#include <sys/types.h>
#line 982

#line 982
#include "mailfromd.h"
#line 982
#include "prog.h"
#line 982
#include "builtin.h"
#line 982

#line 982

#line 1022 "../../src/builtin/snarf.m4"

/* End of snarf.m4 */
#line 1 "debug.bi"
/* Debugging API for MFL.             -*- c -*-
   Copyright (C) 2006-2022 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */


#include "mflib/_register.h"
#include "srvcfg.h"

void
#line 21
bi_debug_level(eval_environ_t env)
#line 21

#line 21

#line 21 "debug.bi"
{
#line 21
	
#line 21

#line 21
long __bi_argcnt;
#line 21
char *  modname;
#line 21
        
#line 21
get_string_arg(env, 1, &modname);
#line 21
        
#line 21
get_numeric_arg(env, 0, &__bi_argcnt);
#line 21
        adjust_stack(env, __bi_argcnt + 1);
#line 21

#line 21

#line 21
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 21
		prog_trace(env, "debug_level %s",((__bi_argcnt > 0) ? modname : ""));;
#line 21

{
	mu_debug_level_t level;
	char *name = ((__bi_argcnt > 0) ? modname : NULL);
	size_t len = name ? strlen (name) : 0;
	
		if (!(mu_debug_category_level(name, len, &level) == 0))
#line 27
		(
#line 27
	env_throw_bi(env, mfe_range, "debug_level", _("invalid module name: %s"),name)
#line 27
)
#line 29
;
	
#line 30
do {
#line 30
  push(env, (STKVAL)(mft_number)(level));
#line 30
  goto endlab;
#line 30
} while (0);
}
endlab:
#line 32
        env_function_cleanup_flush(env, NULL);
#line 32
	return;
#line 32
}

void
#line 34
bi_debug_spec(eval_environ_t env)
#line 34

#line 34

#line 34 "debug.bi"
{
#line 34
	
#line 34

#line 34
long __bi_argcnt;
#line 34
char * MFL_DATASEG modnames;
#line 34
        long  showunset;
#line 34
        
#line 34
get_string_arg(env, 1, &modnames);
#line 34
        get_numeric_arg(env, 2, &showunset);
#line 34
        
#line 34
get_numeric_arg(env, 0, &__bi_argcnt);
#line 34
        adjust_stack(env, __bi_argcnt + 1);
#line 34

#line 34

#line 34
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 34
		prog_trace(env, "debug_spec %s %lu",((__bi_argcnt > 0) ? modnames : ""), ((__bi_argcnt > 1) ? showunset : 0));;
#line 34

{
	mu_stream_t str;
	size_t off;
	int rc;
	char *names = ((__bi_argcnt > 0) ? modnames : NULL);
	
	rc = mu_memory_stream_create(&str, MU_STREAM_RDWR);
		if (!(rc == 0))
#line 42
		(
#line 42
	env_throw_bi(env, mfe_failure, "debug_spec", "cannot create stream: %s",mu_strerror(rc))
#line 42
)
#line 44
;
	rc = mu_debug_format_spec(str, (names && names[0]) ? names : NULL,
				  ((__bi_argcnt > 1) ? showunset : 0));
	if (rc == 0) {
		mu_off_t size;
		char *s;
		size_t n;
		
		mu_stream_seek(str, 0, MU_SEEK_SET, NULL);
		mu_stream_size(str, &size);
		s = (char*) env_data_ref(env, (off = heap_reserve(env, size + 1)));
		rc = mu_stream_read(str, s, size, &n);
		s[n] = 0;
	}
	
	mu_stream_destroy(&str);
		if (!(rc == 0))
#line 60
		(
#line 60
	env_throw_bi(env, mfe_failure, "debug_spec", "%s",mu_strerror(rc))
#line 60
)
#line 62
;
	
#line 63
do {
#line 63
  push(env, (STKVAL) (mft_size) (off));
#line 63
  goto endlab;
#line 63
} while (0);
}
endlab:
#line 65
        env_function_cleanup_flush(env, NULL);
#line 65
	return;
#line 65
}

void
#line 67
bi_debug(eval_environ_t env)
#line 67

#line 67

#line 67 "debug.bi"
{
#line 67
	
#line 67

#line 67

#line 67
char *  spec;
#line 67
        
#line 67
get_string_arg(env, 0, &spec);
#line 67
        
#line 67

#line 67
        adjust_stack(env, 1);
#line 67

#line 67

#line 67
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 67
		prog_trace(env, "debug %s",spec);;
#line 67

{
	struct mu_locus_range loc = MU_LOCUS_RANGE_INITIALIZER;

        env_get_locus(env, &loc);
	mu_stream_ioctl(mu_strerr, MU_IOCTL_LOGSTREAM,
			MU_IOCTL_LOGSTREAM_SET_LOCUS_RANGE, &loc);
	mu_debug_parse_spec(spec);
	mu_stream_ioctl(mu_strerr, MU_IOCTL_LOGSTREAM,
			MU_IOCTL_LOGSTREAM_SET_LOCUS_RANGE, NULL);
}

#line 78
        env_function_cleanup_flush(env, NULL);
#line 78
	return;
#line 78
}

void
#line 80
bi_program_trace(eval_environ_t env)
#line 80

#line 80

#line 80 "debug.bi"
{
#line 80
	
#line 80

#line 80

#line 80
char *  name;
#line 80
        
#line 80
get_string_arg(env, 0, &name);
#line 80
        
#line 80

#line 80
        adjust_stack(env, 1);
#line 80

#line 80

#line 80
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 80
		prog_trace(env, "program_trace %s",name);;
#line 80

{
	enable_prog_trace(name);
}

#line 84
        env_function_cleanup_flush(env, NULL);
#line 84
	return;
#line 84
}

void
#line 86
bi_cancel_program_trace(eval_environ_t env)
#line 86

#line 86

#line 86 "debug.bi"
{
#line 86
	
#line 86

#line 86

#line 86
char *  name;
#line 86
        
#line 86
get_string_arg(env, 0, &name);
#line 86
        
#line 86

#line 86
        adjust_stack(env, 1);
#line 86

#line 86

#line 86
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 86
		prog_trace(env, "cancel_program_trace %s",name);;
#line 86

{
	disable_prog_trace(name);
}

#line 90
        env_function_cleanup_flush(env, NULL);
#line 90
	return;
#line 90
}	

void
#line 92
bi_stack_trace(eval_environ_t env)
#line 92

#line 92

#line 92 "debug.bi"
{
#line 92
	
#line 92

#line 92

#line 92

#line 92

#line 92

#line 92
        adjust_stack(env, 0);
#line 92

#line 92

#line 92
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 92
		prog_trace(env, "stack_trace");;
#line 92

{
	runtime_stack_trace(env);
}

#line 96
        env_function_cleanup_flush(env, NULL);
#line 96
	return;
#line 96
}

void
#line 98
bi__reg(eval_environ_t env)
#line 98

#line 98

#line 98 "debug.bi"
{
#line 98
	
#line 98

#line 98

#line 98
long  what;
#line 98
        
#line 98
get_numeric_arg(env, 0, &what);
#line 98
        
#line 98

#line 98
        adjust_stack(env, 1);
#line 98

#line 98

#line 98
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 98
		prog_trace(env, "_reg %lu",what);;
#line 98

{
	prog_counter_t regval = env_register_read(env, what);
	
#line 101
do {
#line 101
  push(env, (STKVAL)(mft_number)(regval));
#line 101
  goto endlab;
#line 101
} while (0);
}
endlab:
#line 103
        env_function_cleanup_flush(env, NULL);
#line 103
	return;
#line 103
}

void
#line 105
bi__expand_dataseg(eval_environ_t env)
#line 105

#line 105

#line 105 "debug.bi"
{
#line 105
	
#line 105

#line 105

#line 105
long  words;
#line 105
        
#line 105
get_numeric_arg(env, 0, &words);
#line 105
        
#line 105

#line 105
        adjust_stack(env, 1);
#line 105

#line 105

#line 105
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 105
		prog_trace(env, "_expand_dataseg %lu",words);;
#line 105

{
		if (!(expand_dataseg(env, words) == 0))
#line 107
		(
#line 107
	env_throw_bi(env, mfe_failure, "_expand_dataseg", _("out of stack space; increase #pragma stacksize"))
#line 107
)
#line 109
;
}

#line 111
        env_function_cleanup_flush(env, NULL);
#line 111
	return;
#line 111
}

void
#line 113
bi__wd(eval_environ_t env)
#line 113

#line 113

#line 113 "debug.bi"
{
#line 113
	
#line 113

#line 113
long __bi_argcnt;
#line 113
long  seconds;
#line 113
        
#line 113
get_numeric_arg(env, 1, &seconds);
#line 113
        
#line 113
get_numeric_arg(env, 0, &__bi_argcnt);
#line 113
        adjust_stack(env, __bi_argcnt + 1);
#line 113

#line 113

#line 113
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 113
		prog_trace(env, "_wd %lu",((__bi_argcnt > 0) ? seconds : 0));;
#line 113

{
	mu_wd(((__bi_argcnt > 0) ? seconds : 0));
}

#line 117
        env_function_cleanup_flush(env, NULL);
#line 117
	return;
#line 117
}

void
#line 119
bi_callout_transcript(eval_environ_t env)
#line 119

#line 119

#line 119 "debug.bi"
{
#line 119
	
#line 119

#line 119
long __bi_argcnt;
#line 119
long  val;
#line 119
        
#line 119
get_numeric_arg(env, 1, &val);
#line 119
        
#line 119
get_numeric_arg(env, 0, &__bi_argcnt);
#line 119
        adjust_stack(env, __bi_argcnt + 1);
#line 119

#line 119

#line 119
	if (builtin_module_trace(BUILTIN_IDX_debug))
#line 119
		prog_trace(env, "callout_transcript %lu",((__bi_argcnt > 0) ? val : 0));;
#line 119

{
	int oldval = smtp_transcript;
	if ((__bi_argcnt > 0))
		smtp_transcript = val;
	
#line 124
do {
#line 124
  push(env, (STKVAL)(mft_number)(oldval));
#line 124
  goto endlab;
#line 124
} while (0);
}
endlab:
#line 126
        env_function_cleanup_flush(env, NULL);
#line 126
	return;
#line 126
}

 
#line 136

#line 982 "../../src/builtin/snarf.m4"

#line 982

#line 982

#line 982
void
#line 982
debug_init_builtin(void)
#line 982
{
#line 982
	
#line 982
	#line 21 "debug.bi"
va_builtin_install_ex("debug_level", bi_debug_level, 0, dtype_number, 1, 1, 0|0, dtype_string);
#line 34 "debug.bi"
va_builtin_install_ex("debug_spec", bi_debug_spec, 0, dtype_string, 2, 2, 0|0, dtype_string, dtype_number);
#line 67 "debug.bi"
va_builtin_install_ex("debug", bi_debug, 0, dtype_unspecified, 1, 0, 0|0, dtype_string);
#line 80 "debug.bi"
va_builtin_install_ex("program_trace", bi_program_trace, 0, dtype_unspecified, 1, 0, 0|0, dtype_string);
#line 86 "debug.bi"
va_builtin_install_ex("cancel_program_trace", bi_cancel_program_trace, 0, dtype_unspecified, 1, 0, 0|0, dtype_string);
#line 92 "debug.bi"
va_builtin_install_ex("stack_trace", bi_stack_trace, 0, dtype_unspecified, 0, 0, 0|0, dtype_unspecified);
#line 98 "debug.bi"
va_builtin_install_ex("_reg", bi__reg, 0, dtype_number, 1, 0, 0|0, dtype_number);
#line 105 "debug.bi"
va_builtin_install_ex("_expand_dataseg", bi__expand_dataseg, 0, dtype_unspecified, 1, 0, 0|0, dtype_number);
#line 113 "debug.bi"
va_builtin_install_ex("_wd", bi__wd, 0, dtype_unspecified, 1, 1, 0|0, dtype_number);
#line 119 "debug.bi"
va_builtin_install_ex("callout_transcript", bi_callout_transcript, 0, dtype_number, 1, 1, 0|0, dtype_number);

#line 982 "../../src/builtin/snarf.m4"
	
#line 982
/* Declare mailutils_set_debug_level as an alias to 'debug'.
   I do that manually, because there is no m4 magic for that
   (so far it is the only built-in alias). */
#line 982
      va_builtin_install_ex("mailutils_set_debug_level",
#line 982
			    bi_debug, 0, dtype_unspecified, 1, 0, 0,
#line 982
			    dtype_string);
#line 982

#line 982
}
#line 982 "../../src/builtin/snarf.m4"

