/* types */

#define AUTO            auto    /* automatic variables */
#define AGLOBAL         ;       /* global (project-scope) */
#define MODULE		static	/* external static (file-scope) */
#define PERSIST		static	/* internal static (function-scope) */
typedef signed char	ABOOL;	/* 8-bit signed quantity (replaces BOOL) */
typedef signed char	SBYTE;	/* 8-bit signed quantity (replaces Amiga BYTE) */
typedef signed short	SWORD;	/* 16-bit signed quantity (replaces Amiga WORD) */
typedef signed long	SLONG;	/* 32-bit signed quantity (same as LONG) */
#define elif		else if

#define ASM             __asm
#define REG(x)          register __ ## x

#ifdef __STORM__
    #define __inline
    #define chip
    #define EXIT_SUCCESS  0
    #define EXIT_FAILURE 20
    AGLOBAL ULONG stcl_d(STRPTR out, ULONG val);
    AGLOBAL ULONG stcl_h(STRPTR out, ULONG val);
#endif
#ifdef LATTICE
    #define CONST_STRPTR STRPTR
#endif

// defined by AmigaOS are: IMPORT, UBYTE, UWORD, ULONG.

#define STATUS_READY    0
#define STATUS_BUSY     1
#define STATUS_STOPPING 2

#define BARUPDATES      50

// page 14...
#define  GID_1_LY1  0  // root layout
#define  GID_1_BU1  1  // button 1 (other page)
#define  GID_1_TE1  2  // text editor 1
#define  GID_1_TE2  3  // text editor 1
#define  GID_1_TE3  4  // text editor 1
#define  GID_1_TE4  5  // text editor 1
#define  GID_1_TE5  6  // text editor 1
#define  GIDS_1     GID_1_TE5

// page 21
#define  GID_2_LY1  0  // root layout
#define  GID_2_ST1  1  // string 1
#define  GID_2_ST2  2  // string 2
#define  GID_2_ST3  3  // string 3
#define  GID_2_ST4  4  // string 4
#define  GID_2_ST5  5  // string 5
#define  GID_2_ST6  6  // string 6
#define  GID_2_ST7  7  // string 7
#define  GID_2_TE1  8  // text editor 1 (long description)
#define  GID_2_LB1  9  // listbrowser 1 (main dir)
#define  GID_2_LB2  10 // listbrowser 2 (sub dir)
#define  GIDS_2     GID_2_LB2

// page 41...
#define  GID_4_LY1  0  // root layout
#define  GID_4_ST1  1  // string 1 (modulename.type)
#define  GID_4_ST2  2  // string 2 (functionname)
#define  GID_4_ST3  3  // string 3 (description)
#define  GID_4_ST4  4  // string 4 (type: returncode)
#define  GID_4_ST5  5  // string 5 (type: arg 1)
#define  GID_4_ST6  6  // string 6 (type: arg 2)
#define  GID_4_ST7  7  // string 7 (type: arg 3)
#define  GID_4_ST8  8  // string 8 (type: arg 4)
#define  GID_4_ST9  9  // string 9 (type: arg 5)
#define  GID_4_ST10 10 // string 10 (type: arg 6)
#define  GID_4_ST11 11 // string 11 (type: arg 7)
#define  GID_4_ST12 12 // string 12 (type: arg 8)
#define  GID_4_ST13 13 // string 13 (name: returncode)
#define  GID_4_ST14 14 // string 14 (name: arg 1)
#define  GID_4_ST15 15 // string 15 (name: arg 2)
#define  GID_4_ST16 16 // string 16 (name: arg 3)
#define  GID_4_ST17 17 // string 17 (name: arg 4)
#define  GID_4_ST18 18 // string 18 (name: arg 5)
#define  GID_4_ST19 19 // string 19 (name: arg 6)
#define  GID_4_ST20 20 // string 20 (name: arg 7)
#define  GID_4_ST21 21 // string 21 (name: arg 8)
#define  GID_4_ST22 22 // string 22 (reg: returncode)
#define  GID_4_ST23 23 // string 23 (reg: arg 1)
#define  GID_4_ST24 24 // string 24 (reg: arg 2)
#define  GID_4_ST25 25 // string 25 (reg: arg 3)
#define  GID_4_ST26 26 // string 26 (reg: arg 4)
#define  GID_4_ST27 27 // string 27 (reg: arg 5)
#define  GID_4_ST28 28 // string 28 (reg: arg 6)
#define  GID_4_ST29 29 // string 29 (reg: arg 7)
#define  GID_4_ST30 30 // string 30 (reg: arg 8)
#define  GID_4_IN1  31 // integer 1 (minimum version)
#define  GID_4_BU1  32 // button 1 (other page)
// page 42...
#define  GID_4_LY2  33 // root layout
#define  GID_4_TE1  34 // text editor 1 (function)
#define  GID_4_TE2  35 // text editor 2
#define  GID_4_TE3  36 // text editor 3
#define  GID_4_TE4  37 // text editor 4
#define  GID_4_TE5  38 // text editor 5
#define  GID_4_TE6  39 // text editor 6
#define  GID_4_TE7  40 // text editor 7
#define  GID_4_TE8  41 // text editor 8
#define  GID_4_TE9  42 // text editor 9
#define  GID_4_TE10 43 // text editor 10
#define  GID_4_TE11 44 // text editor 11
#define  GID_4_TE12 45 // text editor 12
#define  GID_4_TE13 46 // text editor 13
#define  GID_4_TE14 47 // text editor 14
#define  GID_4_TE15 48 // text editor 15
#define  GID_4_BU3  49 // button 3 (other page)
#define  GIDS_4     GID_4_BU3
// GID_4_ numbers cannot be arbitrarily rearranged.

#define  GID_5_LY1 0  // root layout
#define  GID_5_ST1 1  // string 1 (card)
#define  GID_5_ST2 2  // string 2 (manufacturer)
#define  GID_5_ST3 3  // string 3 (product)
#define  GID_5_ST4 4  // string 4 (description)
#define  GID_5_BU2 5  // button 2 (previous card)
#define  GID_5_BU3 6  // button 3 (next card)
#define  GID_5_CB1 7  // checkbox 1 (internal)
#define  GID_5_CB2 8  // checkbox 2 (official)
#define  GID_5_CH1 9  // chooser 1 (precedence)
#define  GID_5_IN1 10 // integer 1 (manufacturer ID)
#define  GID_5_IN2 11 // integer 2 (product ID)
#define  GIDS_5    GID_5_IN2

#define  GID_7_LY1  0 // root layout
#define  GID_7_RA1  1 // radio 1    (source)
#define  GID_7_RA2  2 // radio 2    (destination)
#define  GID_7_CB1  3 // checkbox 1 (detabulate?)
#define  GID_7_CB2  4 // checkbox 2 (unwrap words?)
#define  GID_7_IN1  5 // integer 1  (tab size)
#define  GID_7_ST1  6 // string 1   (pathname(s))
#define  GID_7_ST2  7 // string 2   (status)
#define  GID_7_BU1  8 // button 1   (pathname(s) ASL)
#define  GID_7_BU3  9 // button 3   (process)
#define  GID_7_BU4 10 // button 4   (stop)
#define  GIDS_7    GID_7_BU4

#define  GID_8_LY1  0  // root layout
#define  GID_8_ST1  1  // string 1 (path)
#define  GID_8_ST2  2  // string 2 (log file)
#define  GID_8_ST3  3  // string 3 (capacity)
#define  GID_8_ST4  4  // string 4 (free)
#define  GID_8_ST5  5  // string 5 (total)
#define  GID_8_ST6  6  // string 6 (files)
#define  GID_8_ST7  7  // string 7 (dirs)
#define  GID_8_ST8  8  // string 8 (block size)
#define  GID_8_BU1  9  // button 1 (path ASL)
#define  GID_8_BU3 10  // button 3 (log file ASL)
#define  GID_8_BU4 12  // button 4 (update)
#define  GID_8_BU5 13  // button 5 (stop)
#define  GID_8_BU6 14  // button 6 (root)
#define  GID_8_BU7 15  // button 7 (parent)
#define  GID_8_LB1 16  // listbrowser 1 (duplicates)
#define  GID_8_LB2 17  // listbrowser 2 (path sizes)
#define  GID_8_CB1 18  // checkbox 1 (log to file?)
#define  GID_8_CB2 19  // checkbox 2 (include slack?)
#define  GID_8_CB3 20  // checkbox 3 (find duplicates?)
#define  GID_8_CH1 21  // chooser 1 (view as)
#define  GID_8_FG1 22  // fuel gauge 1 (status)
#define  GIDS_8    GID_8_FG1

#define GID_10_LY1  0 // root layout
#define GID_10_CB1  1 // checkbox 1   (ok)
#define GID_10_CB2  2 // checkbox 2   (changed)
#define GID_10_CB3  3 // checkbox 3   (3rd-party)
#define GID_10_CB4  4 // checkbox 4   (missing)
#define GID_10_CB5  5 // checkbox 5   (check versions?)
#define GID_10_CB9  6 // checkbox 9   (log to file?)
#define GID_10_ST1  7 // string 1     (pathname)
#define GID_10_BU1  8 // button 1     (pathname(s) ASL)
#define GID_10_BU2  9 // button 2     (update)
#define GID_10_BU3 10 // button 3     (stop)
#define GID_10_LB1 11 // listbrowser  (results)
#define GID_10_FG1 12 // fuel gauge 1 (status)
#define GIDS_10    GID_10_FG1

#define GID_11_LY1 0
#define GID_11_ST1 1
#define GID_11_ST2 2
#define GID_11_ST3 3
#define GID_11_ST4 4
#define GID_11_ST5 5
#define GID_11_ST6 6
#define GID_11_ST7 7
#define GID_11_TE1 8
#define GIDS_11    GID_11_TE1

#define GID_12_LY1 0  // root layout
#define GID_12_CH1 1  // chooser 1   (icon type)
#define GID_12_CB1 2  // checkbox 1  (optimize?)
#define GID_12_CB2 3  // checkbox 2  (planar?)
#define GID_12_ST1 4  // string 1    (pathname(s))
#define GID_12_ST2 5  // string 2    (status)
#define GID_12_BU1 6  // button 1    (pathname(s) ASL)
#define GID_12_BU3 7  // button 3    (process)
#define GID_12_BU4 8  // button 4    (stop)
#define GIDS_12    GID_12_BU4

#define BUG        0
#define COM        1
#define ENH        2

#define FONTX      8
#define FONTY      8

// ASCII values (vanillakey)
#define TAB        9 /* horizontal tab */
#define LF        10 /* linefeed */
#define CR        13 /* carriage return */
#define ESCAPE    27
#define QUOTE     34 /* " */

#define FUNCTIONS 12

// Scancodes (rawkey)
#define SCAN_GRAVE   0
#define SCAN_1       1
#define SCAN_8       8
#define SCAN_W      17
#define SCAN_E      18
#define SCAN_R      19
#define SCAN_T      20
#define SCAN_U      22
#define SCAN_I      23
#define SCAN_O      24
#define SCAN_P      25
#define SCAN_A      32
#define SCAN_S      33
#define SCAN_D      34
#define SCAN_F      35
#define SCAN_G      36
#define SCAN_H      37
#define SCAN_L      40
#define SCAN_C      51
#define SCAN_V      52
#define SCAN_B      53
#define SCAN_N      54
#define SCAN_PERIOD 57
#define SCAN_ESCAPE 69
#define SCAN_UP     76
#define SCAN_DOWN   77
#define SCAN_HELP   95

#define GREY              0
#define BLACK             1
#define WHITE             2
#define RED               3
#define BLUE              4
#define LIGHTGREY         5
#define ORANGE            6
#define YELLOW            7
#define LIGHTGREEN       14

#define TITLEBARTEXT      "Report+ 5.4"

#define FIRSTQUALIFIER 0x61
#define LASTQUALIFIER  0x67
#define KEYUP          0x80 /* key release */

#define MN_PROJECT        0
#define MN_HELP           1

#define IN_NEW            0
#define IN_OPEN           1
#define IN_SAVE           3
#define IN_SAVEAS         4
#define IN_QUIT           6

#define IN_MANUAL         0
#define IN_ABOUT          2

AGLOBAL void report1(void);
AGLOBAL void report_gt_loop(ULONG class, struct Gadget* addr, UWORD code, UWORD qual);
AGLOBAL void report_ra_loop(ULONG gid);
AGLOBAL void report_exit(void);
AGLOBAL void report_init(void);
AGLOBAL void report_close(void);
AGLOBAL void config(void);
AGLOBAL void newreport(ABOOL display);
AGLOBAL void openreport(void);
AGLOBAL void savereport(ABOOL saveas);
AGLOBAL void loadreport(void);
AGLOBAL void getversion(STRPTR filename, STRPTR output);

AGLOBAL void aminet_init(void);
AGLOBAL void aminet_die(void);
AGLOBAL void aminet1(void);
AGLOBAL void aminet_loop(ULONG gid);
AGLOBAL void aminet_exit(void);
AGLOBAL void newaminet(ABOOL display);
AGLOBAL void openaminet(void);
AGLOBAL void saveaminet(ABOOL saveas);
AGLOBAL void loadaminet(void);
AGLOBAL ULONG Hook2Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void acse1(void);
AGLOBAL void acse2(void);
AGLOBAL void acse3(void);
AGLOBAL void acse_exit(void);
AGLOBAL void decrypt(void);
AGLOBAL void acse_loop(ULONG class, struct Gadget* addr, UWORD code);

AGLOBAL void autodoc_init(void);
AGLOBAL void autodoc1(void);
AGLOBAL void autodoc2(void);
AGLOBAL void newautodoc(ABOOL display);
AGLOBAL void openautodoc(void);
AGLOBAL void loadautodoc(void);
AGLOBAL void saveautodoc(ABOOL saveas);
AGLOBAL void autodoc_loop(ULONG gid);
AGLOBAL ULONG Hook4Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void manuf1(void);
AGLOBAL void manuf_loop(ULONG gid);
AGLOBAL void manuf_config(void);
AGLOBAL void manuf_die(void);
AGLOBAL ULONG Hook5Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void iff1(void);
AGLOBAL void iff_loop(ULONG class, struct Gadget* addr, UWORD code, UWORD qual);
AGLOBAL void iff_config(void);
AGLOBAL void iff_die(void);

AGLOBAL void eol1(void);
AGLOBAL void eol2(void);
AGLOBAL void eolconvert(ABOOL gui);
AGLOBAL void eol_loop(ULONG gid);
AGLOBAL void eol_die(void);
AGLOBAL ULONG Hook7Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void size_init(void);
AGLOBAL void size_die(void);
AGLOBAL void size1(void);
AGLOBAL void updatesize(void);
AGLOBAL void size_loop(ULONG gid);
AGLOBAL void size_exit(void);
AGLOBAL ULONG Hook8Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void batt_init(void);
AGLOBAL void batt1(void);
AGLOBAL void batt_loop(ULONG class, struct Gadget* addr, UWORD code, UWORD qual, SWORD mousex, SWORD mousey);
AGLOBAL void batt_open(void);
AGLOBAL void batt_save(ABOOL saveas);
AGLOBAL void writebatt(ABOOL verify);

AGLOBAL void files1(void);
AGLOBAL void files_loop(ULONG gid);
AGLOBAL void files_init(void);
AGLOBAL void files_exit(void);
AGLOBAL void files_die(void);
AGLOBAL ULONG Hook10Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void agdb1(void);
AGLOBAL void agdb_loop(ULONG gid);
AGLOBAL void newagdb(ABOOL display);
AGLOBAL void openagdb(void);
AGLOBAL void saveagdb(ABOOL saveas);
AGLOBAL void loadagdb(void);
AGLOBAL ULONG Hook11Func(struct Hook *h, VOID *o, VOID *msg);

AGLOBAL void icon1(void);
AGLOBAL void icon2(void);
AGLOBAL void iconconvert(ABOOL gui);
AGLOBAL void icon_loop(ULONG gid);
AGLOBAL void icon_do(void);
AGLOBAL void icon_die(void);
AGLOBAL ULONG Hook12Func(struct Hook* h, VOID* o, VOID* msg);

// rp.c
AGLOBAL void getdate(void);
AGLOBAL void loop(void);
AGLOBAL void navigate(void);
AGLOBAL void rq(STRPTR text);
AGLOBAL void cleanexit(SBYTE rc);
AGLOBAL void closewindow(void);
AGLOBAL void FreeNameNodes(struct List* ListPtr);
AGLOBAL ABOOL asl(STRPTR pattern);
AGLOBAL void readordie(STRPTR pathname);
AGLOBAL void writeout(STRPTR pathname); // remember write() is reserved
AGLOBAL ABOOL readin(STRPTR pathname);
AGLOBAL void verynewwindow(SWORD width, SWORD height, STRPTR title, ULONG idcmp);
AGLOBAL void setgadget(WORD leftx, WORD topy, WORD width, WORD height, STRPTR text,
    ULONG flags);
AGLOBAL void append(STRPTR pathname);
AGLOBAL void clearlist(struct List*);
AGLOBAL ABOOL aboutloop(void);
AGLOBAL VOID DrawGels(void);
AGLOBAL void AddNameToTail(struct List* ListPtr, STRPTR name);
AGLOBAL void drawgadgets(WORD gadgets);
AGLOBAL void parse(STRPTR terminator);
AGLOBAL void parsetoend(void);
AGLOBAL ABOOL saveasl(STRPTR message, STRPTR pattern);
AGLOBAL void handlemenus(UWORD code);
AGLOBAL void gadtools(void);
AGLOBAL void lockscreen(void);
AGLOBAL void unlockscreen(void);
AGLOBAL void getfillcolour(void);
AGLOBAL void multiasl(STRPTR pattern);
AGLOBAL void convert(ABOOL gui);
AGLOBAL void checkabort(ABOOL gui);
AGLOBAL void InitHook(struct Hook* hook, ULONG (*func)(), void* data);
AGLOBAL ULONG Hook0Func(struct Hook* h, VOID* o, VOID* msg);
AGLOBAL ULONG ASM hookEntry(REG(a0) struct Hook *h, REG(a2) VOID *o, REG(a1) VOID *msg);
AGLOBAL void helpabout(void);
AGLOBAL void clearreactionlist(struct List* ListPtr);
AGLOBAL void openwindow(void);
AGLOBAL ABOOL ra_checkbreak(void);
AGLOBAL void sender(void);
AGLOBAL void setbar(ULONG max);

#define SENDERWIDTH     442 /* page  1 */
#define SENDERHEIGHT    102 /* page  1 */
#define REPORTWIDTH     640 /* page 11 */
#define REPORTHEIGHT    500 /* page 11 */
#define ACSE1WIDTH      640 /* page 31 */
#define ACSE1HEIGHT     200 /* page 31 */
#define ACSE2WIDTH      450 /* page 32 */
#define ACSE2HEIGHT     176 /* page 32 */
#define IFF1WIDTH       640 /* page 61 */
#define IFF1HEIGHT      410 /* page 61 */
#define BATT1WIDTH      640 /* page 91 */
#define BATT1HEIGHT     166 /* page 91 */

#define MEDFIELD         32
#define LONGFIELD        64
#define VLONGFIELD      254
#define LONGESTFIELD  16536

AGLOBAL struct SharedStruct
{   UWORD function;
    TEXT  thisfile[VLONGFIELD + 1], /* <= 254 characters per pathname */
          thatfile[VLONGFIELD + 1],
          pathname[VLONGFIELD + 1];
};

/* struct NameNode is simply an extended Node structure: a Node structure
followed by 96 bytes which hold the name of the node. This is so that the
NameNode structure can hold everything relevant to the node. */
AGLOBAL struct NameNode
{   struct Node nn_Node;
    TEXT        nn_Data[96];
    /* It must be long enough that you can hold a long pathname, longer than
    will fit on the display, because it needs to be accurately compared
    with the built-in list of system files. It needs to be at least as
    long as the longest of those built-in files, plus 5 extra characters
    for the .info extension. */
};
AGLOBAL struct PathnameNode
{   struct Node nn_Node;
    TEXT        nn_Data[256];
    // Used to hold complete pathnames.
};
AGLOBAL struct ReportStruct
{   ULONG floppies;
    UWORD agnus, denise, cpu, model, type, severity, subsystem;
    TEXT     chipram[5],
             fastram[5],
                hard[MEDFIELD + 1],
               debug[LONGFIELD + 1],
           developer[LONGFIELD + 1],
           expansion[LONGFIELD + 1],
             summary[LONGFIELD + 1],
                  ks[6],
                  wb[6],
                  pv[LONGFIELD + 1],
                  pp[LONGFIELD + 1],
           thirdname[MEDFIELD + 1],
            thirdver[MEDFIELD + 1],
          thirdmanuf[MEDFIELD + 1],
            lastname[MEDFIELD + 1],
           firstname[MEDFIELD + 1],
             company[MEDFIELD + 1],
               phone[MEDFIELD + 1],
               email[MEDFIELD + 1],
               refer[VLONGFIELD + 1],
             referid[VLONGFIELD + 1],
                path[VLONGFIELD + 1],
              config[VLONGFIELD + 1],
           textfield[6][4096],
              output[VLONGFIELD + 1];
};
AGLOBAL struct AminetStruct
{   TEXT  amishort[41], // `short' is a reserved keyword, of course
          uploader[41],
          author[41],
          requires[41],
          version[41],
          distribution[41],
          type[12], // ____/_____
          replaces[41],
          output[VLONGFIELD + 1],
          textfield[4096];
    ULONG maintype,
          subtype;
};
AGLOBAL struct AutodocStruct
{   ULONG version;
    TEXT      library[MEDFIELD + 1],        /* eg. workbench.library */
             funcname[MEDFIELD + 1],
          description[VLONGFIELD + 1],
               output[VLONGFIELD + 1],
             function[4096],
              example[4096],
                notes[4096],
                 bugs[4096],
              seealso[4096];
    struct
    {   TEXT name[MEDFIELD + 1],
             type[MEDFIELD + 1],
             reg[MEDFIELD + 1],
             desc[4096];
    } var[9];
};
AGLOBAL struct IFFStruct
{   ABOOL cd, rkm, obsolete, private, proposal, reserved, standard, unregistered, raw;
    TEXT  pathname[VLONGFIELD + 1];
};
AGLOBAL struct AGDBStruct
{   TEXT title[MEDFIELD + 1], // `short' is a reserved keyword, of course
         publisher[MEDFIELD + 1],
         players[MEDFIELD + 1],
         hdinstallable[MEDFIELD + 1],
         compatibility[MEDFIELD + 1],
         gametype[MEDFIELD + 1],
         output[VLONGFIELD + 1],
         textfield[4096];
};
AGLOBAL struct IconStruct
{   ULONG optimize, planar, // these must be ULONG not ABOOL nor BOOL!
          type;             // this must be ULONG not UWORD!
    TEXT  pathname[VLONGFIELD + 1];
};
