/*
 * sprinter_compat.h — Solid-C compatibility shims.
 *
 * Pulls in standard headers and adds aliases/macros that Solid-C
 * programs expect.  Programs targeting Sprinter from Solid-C source
 * can `#include <sprinter_compat.h>` and most names "just work".
 */

#ifndef SPRINTER_COMPAT_H
#define SPRINTER_COMPAT_H

#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <mouse.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <dir.h>
#include <dos.h>               /* getdate/gettime, getdisk, absread */
#include <sprinter_exit.h>     /* _exit, atexit */

/* ---- Solid-C types ----------------------------------------------- */
typedef uint8_t  BYTE;
typedef uint8_t  TINY;
typedef uint8_t  BOOL;
typedef uint8_t  STATUS;
typedef uint16_t WORD;
typedef int      FD;

#ifndef TRUE
#define TRUE  1
#define FALSE 0
#define YES   1
#define NO    0
#endif

#ifndef OK
#define OK    0
#endif

#ifndef ERROR
#define ERROR (-1)
#endif

#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#endif

/* Solid-C's uint typedef (we expose it explicitly).  Note `uint` may
 * already be defined in some environments; guard. */
#ifndef _SOLID_UINT_DEFINED
#define _SOLID_UINT_DEFINED
typedef unsigned uint;
#endif

/* fpoint — 32-bit split file position (used by lseek/tell). */
typedef struct fpoint {
    uint16_t low;
    uint16_t high;
} f_point;

/* ---- string.h aliases -------------------------------------------- */
/* setmem(ptr, n, byte) → memset(ptr, byte, n)  — arg order swapped */
#define setmem(p, n, b)    memset((p), (b), (n))
/* movmem(src, dst, n) → memcpy(dst, src, n) — arg order swapped */
#define movmem(s, d, n)    memcpy((d), (s), (n))

/* strlwr/strupr объявлены в нашем <string.h> (include_next-цепочка). */

/* Solid-C calls perror's table 'strerr'. */
#define strerr(n)          strerror((n))

/* ---- stdlib.h additions ------------------------------------------ */
#define abort()            _exit(0xFF)

/* min/max — ФУНКЦИИ из <stdlib.h> (libc/stdlib/min.c, max.c), как и в
 * Solid-C (STDLIB.H: `int min();`).  Макро-версии здесь были удалены
 * 2026-07-06: макрос дважды вычисляет аргументы и ломает объявление
 * функции, встреченное после себя. */

/* div() and div_t come from SDCC's <stdlib.h> (already included above). */

/* sysenv() is now a real function (libc/io/env.c) that returns the WHOLE
 * environment into a caller-provided buffer.  Declared in <sprinter.h>. */

/* ---- ctype.h additions ------------------------------------------- */
#define isascii(c)         (((unsigned)(c)) < 128)

/* ---- io.h aliases (Solid-C fd shortcuts) ------------------------- */
#define seek(fd, off)      lseek((fd), (long)(off), SEEK_SET)
#define tell(fd)           ((uint16_t)lseek((fd), 0, SEEK_CUR))
#define ltell(fd)          lseek((fd), 0L, SEEK_CUR)
#define remove(name)       unlink(name)

/* crt0 разбирает argv сам — заглушка для портируемого кода. */
#define _setargv()         0

/* ---- dir.h alias ------------------------------------------------- */
#define _ffirst            ffirst

#endif
