/* Font remover.  File "remfont.c".
 * (C) Adrian Aylward 1991
 *
 * You may freely copy, use, and modify this file.
 *
 * This program removes a font from memory - or rather, makes it no longer
 * available to OpenFont, and ensures that it will be unloaded if and when
 * the last of its acessors closes it.
 *
 * The program was tested using Lattice C V5.05.  It has various Lattice
 * dependencies.
 *
 * This is version 1.0.
 */

# include <dos.h>
# include <exec/exec.h>
# include <graphics/gfxbase.h>
# include <graphics/text.h>
# include <proto/dos.h>
# include <proto/exec.h>
# include <proto/graphics.h>
# include <string.h>
# include <stdio.h>

/* External data (initialised to zero) */

char *argfontname, *argpointsizes;

int optbold, optitalic, optunderlined, optextended;

int retcode;

int fontsize;

char namebuf[100];

int pointc, pointv[100];

struct TextAttr ftattr =
{   NULL, 0, 0, 0 };

struct TextFont *ftextf;

/* Routines */

extern void stub(void);
extern void chkabort(void);

/* Main program */

void main(int argc, char **argv)
{   char *s;
    int i, ch;

    /* Parse arguments.  No workbench startup */

    if (argc == 0) goto tidyexit;
    argv++;
    argc--;
    if (argc == 0 || (argc == 1 && strcmp(*argv, "?") == 0)) goto query;

    while (argc)
    {   s = *argv;
        if (*s != '-') break;
        argv++;
        argc--;
        if (strcmp(s, "--") == 0) break;
        s++;
        for (;;)
        {   ch = *s++;
            if (ch == 0) break;
            switch (ch)
            {   case 'B': case 'b':
                    optbold = 1;
                    continue;

                case 'I': case 'i':
                    optitalic = 1;
                    continue;

                case 'U': case 'u':
                    optunderlined = 1;
                    continue;

                case 'X': case 'x':
                    optextended = 1;
                    continue;

                default:
                    fprintf(stderr, "remfont: unknown option \"-%c\"", ch);
                    goto badusage;
            }
        }

        if (*s == '-' && *(s + 1) == 0) break;
    }

    if (argc != 2) goto badargs;
    argfontname = argv[0];
    argpointsizes = argv[1];

    s = argpointsizes;
    while (*s)
    {   i = 0;
        for (;;)
        {   ch = *s;
            if (ch == 0) break;
            s++;
            if (ch == ',') break;
            if (ch >= '0' && ch <= '9')
                i = i * 10 + (ch - '0');
            else
                goto badargs;
        }
        if (i != 0)
        {   if (pointc == 100) goto badargs;
            pointv[pointc++] = i;
        }
    }

    /* Build the font descriptor (TextAttr structure) */

    if (strlen(argfontname) >= 95) goto badargs;
    strcpy(namebuf, argfontname);
    strcat(namebuf, ".font");
    ftattr.ta_Name = namebuf;

    if (optbold)       ftattr.ta_Style |= FSF_BOLD;
    if (optitalic)     ftattr.ta_Style |= FSF_ITALIC;
    if (optunderlined) ftattr.ta_Style |= FSF_UNDERLINED;
    if (optextended)   ftattr.ta_Style |= FSF_EXTENDED;

    /* Try to open each of the fonts.  If we find EXACTLY what we want,
     * then remove it and close it again */

    GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0);
    if (GfxBase == NULL)
    {   fprintf(stderr, "remfont: can't open graphics.libary\n");
        goto errorexit;
    }

    for (i = 0 ; i < pointc; i++)
    {   ftattr.ta_YSize = pointv[i];
        ftextf = OpenFont(&ftattr);
        if (ftextf != NULL)
        {   if (strcmp(ftextf->tf_Message.mn_Node.ln_Name,
                           ftattr.ta_Name) == 0 &&
                ftextf->tf_YSize == ftattr.ta_YSize &&
                ftextf->tf_Style == ftattr.ta_Style)
                RemFont(ftextf);
            CloseFont(ftextf);
        }
    }

    CloseLibrary((struct Library *) GfxBase);
    goto tidyexit;

    /* Argument errors and usage query */

query:
    fprintf(stderr, "Font remover.  RemFont version 1.0\n"
                    "\n"
                    "  Usage:\n"
                    "\n"
                    "    remfont -options font nn,nn,...\n"
                    "\n"
                    "      -b              Bold\n"
                    "      -i              Italic\n"
                    "      -u              Underlined\n"
                    "      -x              Extended\n"
                    "\n"
                    "  For example:\n"
                    "\n"
                    "    remfont topaz 8,11\n"
                    "\n"
                    "  N.B. the font name is case sensitive\n");
    goto tidyexit;

badargs:
    fprintf(stderr, "remfont: arguments bad or value missing\n");
badusage:
    retcode = 20;
    fprintf(stderr, ".  Usage:\n"
                    "    remfont -options font nn,nn,...\n");
    goto tidyexit;

    /* Tidy up and exit */

errorexit:
    retcode = 20;

tidyexit:
    exit(retcode);
}

/* Dummy stub routine */

void stub(void)
{   return;
}

/* Dummy check abort routine */

void chkabort(void)
{   return;
}

/* End of file "remfont.c" */
