/*
 *  This program will strip a window of nodes linked to the UserData field.
 *  These nodes *must* be of the size given below (in the absence of some
 *  other type of node).
 *
 *  The nodes are used by MENUTEXT to link new menu text definitions to an
 *  open window; if this program is not run before the window is closed,
 *  the memory will never be freed and fragmentation will result.
 *
 *  An extension which I may include later is actually dis-allowing all
 *  CLOSEWINDOW messages from reaching the window until this program is
 *  run. Easy, but intimidating for someone who isn't familiar with the
 *  workings of the program.
 *
 *  ((c)) 1987, John Russell. Unlimited distribution, but donations to
 *  starving programmers welcome.
 *
 */

#include "intuition/intuition.h"
#include "exec/memory.h"
#include "stdio.h"

void copy();

char *name = "John Russell";
char *address = "5 Alderdice Place";
char *province = "St. John's, Newfoundland, Canada";
char *postal_code = "A1B 2P8";
char *phone = "(709) 726-7847";

struct IntuitionBase *IntuitionBase;

struct node {
    struct node *next;
    char text[46];
    char *old_text;
    short old_item_width;
    struct MenuItem *altered;
} *ptr,*next;

main(argc, argv)
int argc;
char *argv[];
{
    struct Window *window;
    struct Screen *screen;

    if ((IntuitionBase=(struct IntuitionBase *)
       OpenLibrary("intuition.library",0L))==NULL) {
           puts("Where is Intuition gone???\n");
           exit(0);
    }

    if (argc != 2) {
        puts("You must supply the name of a window. The name is case-sensitive,\n\
but only the first word in the name is considered.");
        exit(0);
    }

    Forbid();

    screen = IntuitionBase->FirstScreen; /* address of Workbench screen structure */

    while (screen) {     /* do all screens */

        window = screen->FirstWindow;

        while (window) {    /* do all windows */

            if (compare(argv[1],window->Title)==0) {  /* search for name */

                ptr = window->UserData;

                while (ptr) {   /* restore menu to original condition */

                    next = ptr->next;

                    printf("Deleting menutext entry \"%s\"\n",ptr->text);
                    printf("Replacing original entry \"%s\"\n\n",ptr->old_text);

                    ptr->altered->ItemFill->IText = ptr->old_text;
                    ptr->altered->Width = ptr->old_item_width;

                    FreeMem(ptr,sizeof(struct node));
                    ptr = next;
                }
                window->UserData = NULL; /* it was all a dream... */
            }
        window = window->NextWindow;
        }
    screen = screen->NextScreen;
    }

    quit:
    Permit();
    CloseLibrary(IntuitionBase);

}

compare(string1,string2) /* if spaces in windowname, only check first word */
char *string1,*string2;
{
    while ((*string1 != '\0') && (*string1 != ' ')) {
        if (*string1 != *string2) /* space and null both end conditions */
            return(1);
        string1++;
        string2++;
    }
    return(0);  /* return weird values like strcmp() */
}
