/* sml3_assert.c: assert()-Funktion mit Fehlerausgabe */

/* Copyright 2012-2026 Kurt Nienhaus
 *
 * This file is part of libsammel3.
 * libsammel3 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 2 of the License, or
 * (at your option) any later version.
 * libsammel3 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 libsammel3.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include "config.h"
#include "sml3_assert.h"

void _SML3_assert(const char *, const char *, int, int, ...);


/* SML3_assert [thread-sicher]:
 * prueft Bedingung, wenn falsch, gibt Fehlermeldung nach stderr aus und ruft abort() auf
 * 1.Arg: Bedingung
 * 2.Arg: Format (oder NULL = keine Fehlerausgabe)
 * weitere Arg: Formatvariablen (falls 2.Arg != NULL)
 * #define SML3_assert(condit, ...)  _SML3_assert(__FILE__, SML3_funcname, __LINE__, condit, __VA_ARGS__)
 */
void
_SML3_assert(const char *datei, const char *funktion, int zeile, int condit, ...)
{
  va_list ap;
  char *fmt;

  if (condit) { return; }

  if (funktion != NULL && *funktion != '\0') {
    fprintf(stderr, "abort at [%s:%s():%d]", (datei == NULL ? "(nofile)" : datei), funktion, zeile);
  } else {
    fprintf(stderr, "abort at [%s:%d]", (datei == NULL ? "(nofile)" : datei), zeile);
  }

  va_start(ap, condit);
  fmt = va_arg(ap, char *);
  if (fmt != NULL && *fmt != '\0') {
    fprintf(stderr, ": ");
    vfprintf(stderr, fmt, ap);
  }
  va_end(ap);
  fprintf(stderr, "\n");

  abort();
} /* Ende _SML3_assert */
