#ifdef FOR_OS_DOS
#	ifndef FILENAME_MAX
#		define FILENAME_MAX 256
#	endif
#	ifndef FOPEN_MAX
#		define FOPEN_MAX 32
#	endif
#endif

	/*** Argument checking. ***/
# ifndef LINT_ARGS
# define LINT_ARGS 1
# endif

	/*** Include Files. ***/
# ifndef FILES_INCLUDED
# include <stdio.h>

# include <string.h>
# include <ctype.h>
# ifndef IBM370
# include <malloc.h>
# endif
# include <stdlib.h>
# ifdef IBM370
# include "memory.h"
# else
# include "memory.h"
# endif
# define FILES_INCLUDED 1
# endif

# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>


	/*** Refal constants. ***/
# define PCAT 1

	/* size of a function name/compound symbol. */
# ifndef MAXWS
/*#	define MAXWS 32 */
#	define MAXWS 1024
# endif

#ifndef DEFAULT_STACK_SIZE
#	define DEFAULT_STACK_SIZE 512
#endif

#ifndef DEFAULT_TABLE_SIZE
/* #	define DEFAULT_TABLE_SIZE 1024 */
#	define DEFAULT_TABLE_SIZE (2*1024)
#endif

#ifndef DEFAULT_CODE_LIMIT
/* #	define DEFAULT_CODE_LIMIT 64 */
#	define DEFAULT_CODE_LIMIT 128
#endif

	/* External definitions. */
// # 		define EXTERNAL extern
                      // Nemytykh, 13-04-2025

# ifndef EXTERNAL
# 	ifdef DEFINE_EXTERNALS
#		if DEFINE_EXTERNALS
# 			define EXTERNAL
#		else
#			define EXTERNAL extern
#		endif
# 	else
# 		define EXTERNAL extern
# 	endif
# endif

	/* Utf definitions. Currently only for UTF-8 */
# define MAX_UTF_8 0xF7BFBFBF
# define MAX_UNICODE 0x10FFFF
 typedef unsigned int utf;


	/*** Refal structures. ***/
 typedef struct link
	{
	char ptype; /* type of the link */
	union
		{
		struct link *b;	/* bracket: ptr to the pair */
		char *f;	/* function or compound symbol: ptr to label. */
		char c;		/* symbol: actual value. */
/*		unsigned REFLONG u; /+* unicode symbol */ 
		unsigned int u;  /* unicode symbol */ 
		unsigned REFLONG n; /*  macro-digit   */
		unsigned short us_1, us_2;
		} pair;
	struct link *prec; /* ptr to preceding link */
	struct link *foll; /* ptr to following link */
	} LINK;

#ifdef FOR_OS_WINDOWSNT
// # define ri_time_strct struct _timeb;
   typedef _timeb ri_time_strct;
#endif

// Возможно нужно как-то так: typedef __timeb64 ri_time
// Видимо, скорее, так: #define ri_timestr __timeb64
#ifdef BY_GCC_MINGW_WIN  // nemyytykh, 21-02-2026
//?-   typedef REFLONG ri_time_strct;
   typedef struct __timeb64 ri_time_strct;
//   typedef struct _timeb ri_time_strct;
// # define ri_time_strct REFLONG;
#else
# ifdef FOR_OS_LINUX   
             // i.e. FOR_OS_LINUX x64, nemytykh, 05-04-2026
   typedef struct timespec ri_time_strct;
//? # define ri_time_strct struct timespec;
# endif
#endif

		/*** Refal Global variables. ***/

	EXTERNAL LINK
		*lfm,	/* list of free memory		*/
		*stock,	/* stock pointer 		*/
		*vf,	/* view field			*/
		*vfend,	/* end of view field		*/
		*b1,*b2,	/* left and right boundaries	*/
		*quap,	/* quasipoint			*/
		*nextp,	/* next active point		*/
		*precp,	/* preceding active point	*/
		**te /*[512]*/,	/* table of elements		*/
		*b,	/* boundary	of the current right-side state 	*/
		*pa,	/* parenthesis address		*/
		*rend,	/* right end			*/
		*nextb;	/* next bracket			*/

  EXTERNAL REFLONG
		nst,	/* number of steps		*/
		curk,	/* current number of k-signs	*/
		stoff,	/* transition stack offset 	*/
		teoff,	/* table of elements offset	*/
		sp,	/* top of the transition stack	*/
		nel;	/* new element	*/

	EXTERNAL ri_time_strct
			whens,        /* time when the refal-5 interpreter (or tracer) has started.	*/
			ltelaps;      /* time of the last call of TimeElapsed (rf_timeElaps) function */

/*+
# ifndef FOR_OS_WINDOWSNT
#    ifndef BY_GCC_MINGW_WIN  // nemytykh, 21-02-2026, i.e. FOR_OS_LINUX x32
   	   whens,	/+* time when the refal-5 interpreter (or tracer) has started.	*+/
   		ltelaps, /+* time of the last call of TimeElapsed (rf_timeElaps) function *+/
#    endif
# endif
*/

	EXTERNAL int memory_limit;
	EXTERNAL int size_local_stack;
	EXTERNAL int size_table_element;

  EXTERNAL LINK  **tel; /* pointer to an entry in the table of elements */
  EXTERNAL char
			*p,	        /* RASL instruction address pointer              */
			*actfun;      /* active function pointer	                     */
  EXTERNAL int	outUnprn;  /* boolen value signaling to output unprintable 
                             chars by themselves or by a special printable char. */

	typedef struct
		{
		LINK *b1;		/* pointer to left boundary */
		LINK *b2;		/* pointer to right boundary */
		REFLONG nel;		/* new element		*/
		char *ra;		/* return address	*/
		}  STACK_STRUCTURE;      /* transition stack  */

	EXTERNAL STACK_STRUCTURE * st/*[256]*/;

	/* Standard functions and structures concerned with
		imploding compound symbols.	*/

 EXTERNAL char *STOP_, *IMP_;

	/* The pointers to the lists of local and entry functions and 
		module names and their lenghts. Used for the tracer and
		function MU.  03/08/1987 */
 EXTERNAL char **local_functions, *module_table, **entry_functions;
 EXTERNAL REFLONG num_local_functions, num_modules, num_entry;

 /* Pointer to the actual version of Refal-5 system. */
  EXTERNAL char * version;

 /* Global variables containing the command line arguments. */
# ifdef PCAT
	EXTERNAL char flags[20];	/* Flags Up to 20 flags. */
	EXTERNAL char **garg;
	EXTERNAL int gargc;
	
	EXTERNAL int * gargi; // Nemytykh, 04-08-2025
	EXTERNAL int argvi;
	EXTERNAL char ** r_argv;
# endif

 /* Global variables containing the interrupt state and upper bound
    defined by an user by means of the built-in function Interrupt.
 */
   EXTERNAL unsigned REFLONG interrupt_bound;
   EXTERNAL char interrupt_st;

	struct  cshte 	/* compound symbols hashing table entry. */
		{
			char *cs;	/* address of compound symbol. */
			struct cshte *next;	/* pointer to next. */
		};
	/* Hash table is implemented with "open" hashing scheme. */
# define HASH_SIZE 101
	EXTERNAL struct cshte *csht[HASH_SIZE] ; /* initialized to NULL. */

# ifndef max
# define max(a,b) ((a > b) ? a : b)
# endif

# ifndef min
# define min(a,b) ((a < b) ? a : b)
# endif

    // 7777, Nemytykh 26-04-2025
# ifdef BY_GCC_MINGW_WIN     
#   define FOR_OS_WINDOWS_UTF8 1
# endif

/* Structure containing file and file system status. */ // 7777, Nemytykh 26-02-2026
# ifdef FOR_OS_WINDOWSNT
EXTERNAL	struct _stat st_buf;
# else
EXTERNAL	struct stat st_buf;
# endif