#include "defs.h"

/// "Prototypes"

Prototype BOOL InitC(void);
Prototype void ExitC(void);

///
/// "Init"

/* ----------------------------------- InitC -----------------------------------

 Library startup code (C entry point).

*/

__geta4 BOOL
InitC(void)
{
    UBYTE options[255];
    UWORD ascii;

    InitSemaphore(&ISpellSemaphore);

    // valid characters: A-Z, a-z and national characters (äöü...)

    for (ascii = 0; ascii < 256; ++ascii)

        IsLetter[ascii] = ((ascii >= 'A') && (ascii <= 'Z')) || ((ascii >= 'a') && (ascii <= 'z')) || ((ascii >= 192) && (ascii != 215) && (ascii != 247));

    Online = FALSE;

    // read options

    if (GetVar("WORDS.prefs", options, sizeof(options), GVF_GLOBAL_ONLY) != -1) {

        struct RDArgs *rdArgs, *args;

        if (rdArgs = AllocDosObject(DOS_RDARGS, NULL)) {

            ULONG argArray[] = { 0, 0, 0, 0, 0 };

            // make LF-terminated copy of options string (required by dos/readArgs):

            strcat(options, "\12");

            rdArgs->RDA_Source.CS_Buffer = options;
            rdArgs->RDA_Source.CS_Length = strlen(options);
            rdArgs->RDA_Source.CS_CurChr = 0;
            rdArgs->RDA_DAList           = 0;
            rdArgs->RDA_Buffer           = NULL;

            if (args = ReadArgs("LANGUAGE/K,QUICKCHECK/K,BOUNDARYCHARS/K,BEEP/N,ARGS/F", argArray, rdArgs)) {

                if (argArray[2]) {                   // BOUNDARYCHARS/K

                    // boundary characters (e.g. ') are either letter (if next character is a letter) or space

                    UBYTE *isBoundary = (UBYTE *)argArray[2];

                    while (*isBoundary)

                        IsBoundary[(UWORD)*isBoundary++] = TRUE;
                }

                if (argArray[3]) {                   // BEEP/N

                    Volume = *(ULONG *)argArray[3];

                    if (Volume)
                        Online = TRUE;
                }

                if (argArray[4]) {                   // ARGS/F

                    UBYTE *isChar;

                    // additional characters to be considered as "letter" ? Syntax: -w "..."

                    if (isChar = strstr((UBYTE *)argArray[4], "-w ")) {

                        while (*isChar) {

                            if (*isChar++ == 34) {

                                while (*isChar && (*isChar != 34))

                                    IsLetter[(UWORD)*isChar++] = TRUE;

                                break;
                            }
                        }
                    }
                }

                FreeArgs(args);
            }

            FreeDosObject(DOS_RDARGS, rdArgs);
        }
    }

    // trigger keys (online spellchecking)

    IsTrigger[' '] = TRUE;
    IsTrigger[','] = TRUE;
    IsTrigger['.'] = TRUE;
    IsTrigger[';'] = TRUE;
    IsTrigger[')'] = TRUE;
    IsTrigger[']'] = TRUE;
    IsTrigger['}'] = TRUE;

    return(TRUE);
}

///
/// "Exit"

/* ----------------------------------- ExitC -----------------------------------

 Library cleanup code

*/

__geta4 void
ExitC(void)
{
    ;
}

///
