#include <stdint.h>


typedef enum
{
    WAIT        =   0,    /* wait for child processes to return   */
    MAX_PATHLEN = 260,
    MAX_CMDLEN  = 500,    /* the max command line length *       */
    DIRSEP = '/',
} Varia;

typedef enum
{
    REQUIRE_ZERO    = 0,
    NOT_CHECKED     = 2
} ReturnCheck;

typedef enum
{
    IS_FILE   = 1,          // see scanner/lexer: same values as O_FILE etc.
    IS_DIR    = 2,
    IS_SUBDIR = 4,
    IS_ALL    = 8
} Dir_attributes;

typedef enum
{
    IS_IFDIR  = 1,
    IS_IFCHR  = 2,
    IS_IFREG  = 4,
    IS_IREAD  = 8,
    IS_IWRITE = 16,
    IS_IEXEC  = 32
} IS_attributes;


typedef enum
{
    NORMAL_FILE     = 0x00,
    READONLY_FILE   = 0x01,
    HIDDEN_FILE     = 0x02,
    SYSTEM_FILE     = 0x04,
    VOLUME_ID       = 0x08,
    SUBDIR          = 0x10,
    ARCHIVED        = 0x20
} File_attributes;

typedef enum                        // update rsstype.c when changing ExprType
{
    e_null          = 0,

    /* rss + compiler: */
    e_int           = (1 << 0),     /* 1: int-type    expression */
    e_str           = (1 << 1),     /* 2: string-type expression */
    e_list          = (1 << 2),     /* 4: list-type   expression */

    /* compiler only: */
    e_bool          = (1 << 3),     /* 8: bool-type   expression */

    e_typeMask      = (e_int | e_list | e_str | e_bool),

    e_const         = (1 << 4),     /* 0x10 immediate value */
    e_var           = (1 << 5),     /* 0x20 variable */
    e_reg           = (1 << 6),     /* 0x40 register */
    e_stack         = (1 << 7),     /* 0x80 value available on the stack */

    e_pre_inc_dec   = (1 << 8),     /* pre-inc or pre-dec        */
    e_post_inc_dec  = (1 << 9)      /* post-inc or post-dec      */
} ExprType;


    /* when the ordering or entries of the following enum changes, update
            un/data.c, 
            comp/parser/data.c and 
            exec/builtin/data.c 
        as well en rebuild all programs. It also changes the contents of
        the .bim file, so it also requires at least a minor version upgrade.
    */
typedef enum                                /* names of rss-functions */
{
    /* 0 */
    f_arg_head,
    f_arg_tail,
    f_ascii_int,                            /* return int */
    f_ascii_str,                            /* return str */

    /* 4 */
    f_backtick,                             
    f_c_base,
    f_c_ext,
    f_c_path,

    /* 8 */
    f_chdir,
    f_cmd_head,
    f_cmd_tail,
    f_echo,

    /* c */
    f_element,
    f_eval,
    f_exec,
    f_execute,                              /* only used by the compiler */

    /* 10 */
    f_exists,
    f_fgets,
    f_fprintf,
    f_g_base,

    /* 14 */
    f_g_dext,
    f_g_ext,
    f_g_path,
    f_getch,

    /* 18 */
    f_getenv,
    f_getpid,
    f_gets,
    f_listlen,

    /* 1c */
    f_makelist,
    f_printf,
    f_putenv,
    f_stat,

    /* 20 */
    f_str_el,
    f_strfind,
    f_strformat,
    f_strlen,

    /* 24 */
    f_strlwr,
    f_resize,
    f_strtok,
    f_strupr,

    /* 28 */
    f_substr,
    f_system,
    f_trim,
    f_trimleft,

    /* 2c */
    f_trimright,
    
    f_strchr,
    f_listfind,
    f_listunion,

    /* 30 */
    f_listConst,

    /* 17 left for new opcodes until f_hlt */

    f_hlt = f_trimright + 21                /* dummy marker for non-existing */
} FunNr;

    /* update icm-exec and icmun's data files if the next enum changes */
typedef enum
{                       /* hexnr:     */
    op_jmp,             /*     00     */
    op_jmp_false,       /*     01     */
    op_jmp_true,        /*     02     */
    op_push_1_jmp_end,  /*     03     */
    op_push_0,          /*     04     */
    op_push_imm,        /*     05     */
    op_push_strconst,   /*     06     */
    op_push_var,        /*     07     */
    op_push_reg,        /*     08     */
    op_pop_var,         /*     09     */
    op_umin,            /*     0a     */
    op_atoi,            /*     0b     */
    op_itoa,            /*     0c     */
    op_atol,            /*     0d     */
    op_mul,             /*     0e     */
    op_div,             /*     0f     */
    op_mod,             /*     10     */
    op_add,             /*     11     */
    op_sub,             /*     12     */
    op_eq,              /*     13     */
    op_neq,             /*     14     */
    op_sm,              /*     15     */
    op_gr,              /*     16     */
    op_younger,         /*     17     */
    op_older,           /*     18     */
    op_smeq,            /*     19     */
    op_greq,            /*     1a     */
    op_call_rss,        /*     1b     */
    op_asp,             /*     1c     */
    op_exit,            /*     1d     */
    op_copy_var,        /*     1e     */
    op_inc,             /*     1f     */
    op_dec,             /*     20     */
    op_call,            /*     21     */
    op_frame,           /*     22     */
    op_ret,             /*     23     */
    op_pop_reg,         /*     24     */
    op_band,            /*     25     */
    op_bor,             /*     26     */
    op_bnot,            /*     27     */
    op_xor,             /*     28     */
    op_shl,             /*     29     */
    op_shr,             /*     2a     */

    op_hlt = op_shr + 10/*     ..     */
} Opcode;

struct find_t_                               /* abbreviated variant */
{
    char name[MAX_PATHLEN];
    unsigned attrib;                             /* returned attribute */
};

typedef struct
{
    unsigned attrib;                             /* requested attribute */
    struct find_t_ find;
} IcmakeFind;


typedef struct
{
    char version[4];
    int32_t offset[4];
} BinHeader;                              /* see header structure at BOF */

typedef struct
{
    uint16_t size;
    char **element;                          /* used as (char *) by icmcomp */
} List;

typedef struct
{
    union
    {
        uint16_t nShared;              /* allocation count */
        uint16_t address;            /* addr. of 1st byte of a function */
    };

    union
    {
        char *str;
        List list;
    };
} TextData;

typedef struct                          /* defined variable */
{
    ExprType type;                      /* maybe stringconst, int, list */
    union
    {
        int16_t intValue;               /* value of an int */
        TextData *data;                 /* intermediate alloc. structure */
    };      
} Variable;



