/**
 * Scout - The Amiga System Monitor
 *
 *------------------------------------------------------------------
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * You must not use this source code to gain profit of any kind!
 *
 *------------------------------------------------------------------
 *
 * @author Andreas Gelhausen
 * @author Richard Körber <rkoerber@gmx.de>
 */



#include "system_headers.h"

/*********************************************************************
                           Definitionen
*********************************************************************/
#define MYLIBVERSION    37
#define MUIVERSION      8

/*********************************************************************
                         Globale Variablen
*********************************************************************/
extern struct  ExecBase *SysBase;

struct Library          *MUIMasterBase;
struct Library          *IdentifyBase;
struct Library          *CxBase;
struct Library          *LayersBase;

struct Device           *TimerBase;

struct Task             *myprocess;
struct MsgPort          *myarexxport,*ScoutPort;
BOOL   ScoutPortAdded = FALSE;

struct IOStdReq         *InputIORequest;
BOOL   input = TRUE, timer2 = TRUE;
struct timerequest      *TimerIORequest;
BYTE   TimerSignal;

BOOL   input;
char   *portname;

/*
**  MUI
*/

APTR AP_Scout;

APTR WI_Main,WI_About;
APTR WI_Memory,WI_Interrupts,WI_Semaphores;
APTR WI_InputHandler,WI_Residents,WI_Windows,WI_Expansions;
APTR WI_Fonts,WI_Vectors,WI_Mount;
APTR WI_Priority,SL_Priority;
APTR WI_Signal;
APTR WI_LowMemory;
APTR WI_Timer;
APTR WI_Allocations;

APTR memlist,intlist;
APTR resilist;
APTR inputlist;
APTR lowmemorylist;
APTR timerlist;

APTR memtext,inttext;
APTR resitext;
APTR inputtext;
APTR lowmemorytext;
APTR timertext;
APTR vectext1,vectext2,vectext3,vectext4;
APTR pritext1,pritext2,pritext3;
APTR sigtext1,sigtext2,sigtext3,sigtext4;
APTR alctext1,alctext2,alctext3;

APTR resicount,inputcount,intcount,lowmemorycount,timercount;
int  resicnt,inputcnt,intcnt,lowmemorycnt,timercnt;

APTR BT_Ports,BT_Residents,BT_Interrupts,BT_InputHandler,BT_Tasks,BT_Locks;
APTR BT_Libraries,BT_Devices,BT_Resources,BT_Memory,BT_Windows,BT_Semaphores;
APTR BT_Assigns,BT_Fonts,BT_Expansions,BT_Vectors,BT_Mount,BT_Commands;
APTR BT_System,BT_Commodities,BT_Classes,BT_LowMemory,BT_ScreenMode,BT_Allocations,BT_Timer;

APTR BT_AboutContinue;
APTR BT_MemPrint,BT_MemPriority,BT_MemMore,BT_MemExit;
APTR BT_IntUpdate,BT_IntPrint,BT_IntRemove,BT_IntMore,BT_IntExit;
APTR BT_InputUpdate,BT_InputPrint,BT_InputRemove,BT_InputPriority,BT_InputExit;
APTR BT_ResiUpdate,BT_ResiPrint,BT_ResiMore,BT_ResiExit;
APTR BT_VecUpdate,BT_VecPrint,BT_VecExit;
APTR BT_SysPrint,BT_SysExit,BT_SysUpdate;
APTR BT_PriorityOK,BT_PriorityCancel;
APTR BT_SignalOK,BT_SignalCancel;
APTR BT_LowMemoryUpdate,BT_LowMemoryPrint,BT_LowMemoryRemove,BT_LowMemoryPriority,BT_LowMemoryExit;
APTR BT_LowMemoryCause;
APTR BT_TimerUpdate,BT_TimerPrint,BT_TimerRemove,BT_TimerExit;
APTR BT_AlcUpdate,BT_AlcPrint,BT_AlcExit;

APTR AboutText;

APTR liblist_cmphook_ptr;
APTR devlist_cmphook_ptr;
APTR reslist_cmphook_ptr;
APTR tasklist_cmphook_ptr;
APTR portlist_cmphook_ptr;
APTR comlist_cmphook_ptr;
APTR asslist_cmphook_ptr;
APTR locklist_cmphook_ptr;
APTR cxlist_cmphook_ptr;
APTR classlist_cmphook_ptr;
APTR smodelist_cmphook_ptr;

APTR processorvalue, customvalue;
APTR processortext, customtext;

#define ID_QUIT  MUIV_Application_ReturnID_Quit

APTR GlobalPool;

BPTR     printfile, prevdir;
unsigned char     *tmpstr,*tmpstr2;

char UpdateText[]    = "_Update";
char RemoveText[]    = "_Remove";
char CloseText[]     = "_Close";
char PriorityText[]  = "_Priority";
char MoreText[]      = "_More";
char ExitText[]      = "E_xit";
char FreezeText[]    = "_Freeze";
char ActivateText[]  = "_Activate";
char SignalText[]    = "_Signal";
char BreakText[]     = "_Break";
char ContinueText[]  = "_Continue";
char CancelText[]    = "_Cancel";
char OKText[]        = "_OK";
char ReleaseText[]   = "Re_lease";
char ObtainText[]    = "_Obtain";
char PrintText[]     = "Pr_int";
char ToFrontText[]   = "To _Front";
char AppearText[]    = "_Appear";
char DisappearText[] = "_Disappear";
char EnableText[]    = "_Enable";
char DisableText[]   = "Di_sable";
char KillText[]      = "_Kill";
char ListChgText[]   = "List_Chg";
char UniqueText[]    = "U_nique";
char CauseText[]     = "_Cause";
char FunctionsText[] = "_Functions";

char AllocationsText[]  = "Allocations";      /*-*/
char AssignsText[]      = "Assigns";
char ClassesText[]      = "BoopsiClasses";    /*-*/
char CommoditiesText[]  = "Commodities";      /*-*/
char DevicesText[]      = "Devices";
char CommandsText[]     = "Resident Cmds";
char ExpansionsText[]   = "Expansions";
char FontsText[]        = "Fonts";
char InputHandlersText[]= "InputHandlers";
char InterruptsText[]   = "Interrupts";
char LibrariesText[]    = "Libraries";
char LocksText[]        = "Locks";
char LowMemoryText[]    = "LowMemory";        /*-*/
char MemoryText[]       = "Memory";
char MountText[]        = "Mounted Devs";
char PortsText[]        = "Ports";
char ResidentsText[]    = "Residents";
char ResourcesText[]    = "Resources";
char ScreenModeText[]   = "ScreenMode";
char SemaphoresText[]   = "Semaphores";
char SystemText[]       = "System";           /*-*/
char TasksText[]        = "Tasks";
char TimerText[]        = "Timer";            /*-*/
char VectorsText[]      = "Vectors";
char WindowsText[]      = "Windows";

#define RB CHECKIT
#define TG CHECKIT|MENUTOGGLE

const struct NewMenu menu_list[] =
{
   { NM_TITLE, "Project",  0,0,0,0},
   { NM_ITEM,  "About...", "?",0,0,(APTR) ID_ABOUT},
   { NM_ITEM,  NM_BARLABEL,0,0,0,0},
   { NM_ITEM,  "Quit",     "Q",0,0,(APTR) ID_QUIT},

   { NM_TITLE, "List",     0,0,0,0},
   { NM_ITEM,  AllocationsText,  "A",0,0,(APTR) ID_ALLOCATIONS},
   { NM_ITEM,  AssignsText,      "G",0,0,(APTR) ID_ASSIGNS},
   { NM_ITEM,  ClassesText,      "B",0,0,(APTR) ID_CLASSES},
   { NM_ITEM,  CommoditiesText,  "K",0,0,(APTR) ID_COMMODITIES},
   { NM_ITEM,  DevicesText,      "D",0,0,(APTR) ID_DEVICES},
   { NM_ITEM,  ExpansionsText,   "X",0,0,(APTR) ID_EXPANSIONS},
   { NM_ITEM,  FontsText,        "F",0,0,(APTR) ID_FONTS},
   { NM_ITEM,  InputHandlersText,"H",0,0,(APTR) ID_INPUTHANDLER},
   { NM_ITEM,  InterruptsText,   "I",0,0,(APTR) ID_INTERRUPTS},
   { NM_ITEM,  LibrariesText,    "L",0,0,(APTR) ID_LIBRARIES},
   { NM_ITEM,  LocksText,        "O",0,0,(APTR) ID_LOCKS},
   { NM_ITEM,  LowMemoryText,    "J",0,0,(APTR) ID_LOWMEMORY},
   { NM_ITEM,  MemoryText,       "M",0,0,(APTR) ID_MEMORY},
   { NM_ITEM,  MountText,        "N",0,0,(APTR) ID_MOUNT},
   { NM_ITEM,  PortsText,        "P",0,0,(APTR) ID_PORTS},
   { NM_ITEM,  ResidentsText,    "R",0,0,(APTR) ID_RESIDENTS},
   { NM_ITEM,  CommandsText,     "C",0,0,(APTR) ID_COMMANDS},
   { NM_ITEM,  ResourcesText,    "U",0,0,(APTR) ID_RESOURCES},
   { NM_ITEM,  ScreenModeText,   "Z",0,0,(APTR) ID_SCREENMODE},
   { NM_ITEM,  SemaphoresText,   "S",0,0,(APTR) ID_SEMAPHORES},
   { NM_ITEM,  SystemText,       "Y",0,0,(APTR) ID_SYSTEM},
   { NM_ITEM,  TasksText,        "T",0,0,(APTR) ID_TASKS},
   { NM_ITEM,  TimerText,        "E",0,0,(APTR) ID_TIMER},
   { NM_ITEM,  VectorsText,      "V",0,0,(APTR) ID_VECTORS},
   { NM_ITEM,  WindowsText,      "W",0,0,(APTR) ID_WINDOWS},

   { NM_TITLE, "Other",          0,0,0,0},
   { NM_ITEM,  "Flush devices",  0,0,0,(APTR) ID_FLUSHDEVS},
   { NM_ITEM,  "Flush fonts",    0,0,0,(APTR) ID_FLUSHFONTS},
   { NM_ITEM,  "Flush libraries",0,0,0,(APTR) ID_FLUSHLIBS},
   { NM_ITEM,  "Flush all",      ".",0,0,(APTR) ID_FLUSHALL},
   { NM_ITEM,  NM_BARLABEL,      0,0,0,0},
   { NM_ITEM,  "Clear reset vectors","!",0,0,(APTR) ID_CLEARRESET},

   { NM_END,   NULL,       0,0,0,0},
};

/*********************************************************************
                         Requester-Daten
*********************************************************************/

/*********************************************************************
                           WinFree-Handling
   Die Abkopplung einiger Windows wird hiermit realisiert!
*********************************************************************/
struct WinFree *WinFree_ToDo;
struct WinFlag *WinFlag_ToDo;

struct WinFree FirstWinFree = {
   (APTR) NULL, (APTR) NULL, NULL,
   &FirstWinFree, &FirstWinFree
};

void RemoveWinFree (void) {
   if (SingleDetailWinFree == WinFree_ToDo) {
      SingleDetailWinFree = NULL;
   }

   SetWindowClose (WinFree_ToDo->wf_Window,FALSE);
   if (WinFree_ToDo->wf_Pool) tbDeletePool(WinFree_ToDo->wf_Pool);

   WinFree_ToDo->wf_Next->wf_Previous = WinFree_ToDo->wf_Previous;
   WinFree_ToDo->wf_Previous->wf_Next = WinFree_ToDo->wf_Next;

   tbFreeVecPooled(GlobalPool, WinFree_ToDo);
}

struct WinFree *AllocWinFree (void) {
   struct WinFree *result;

   if (result = tbAllocVecPooled(GlobalPool, sizeof (struct WinFree))) {
      result->wf_Next = &FirstWinFree;
      result->wf_Previous = FirstWinFree.wf_Previous;
      result->wf_Previous->wf_Next = result;
      result->wf_Next->wf_Previous = result;
   }
   return (result);
}

void FreeMemoryOfAllWinFrees (void) {
   struct WinFree *lauf = FirstWinFree.wf_Next, *next;

   while (lauf != &FirstWinFree) {
      next = lauf->wf_Next;

      if (lauf->wf_Pool) tbDeletePool(lauf->wf_Pool);
      tbFreeVecPooled(GlobalPool, lauf);

      lauf = next;
   }
}

/*********************************************************************
                                 Fail
   Diese Routine wird zum Schluß des Programmes aufgerufen!
*********************************************************************/
static VOID fail1 (VOID) {
   Forbid();
   if (FindPort ("AMITCP")) {
      Permit();
      failtcp();
   } else {
      Permit();
   }

   if (TimerIORequest) {
      if (!timer2) {
         if (timer_ticking) {
            AbortIO ((struct IORequest *) TimerIORequest);
            WaitIO ((struct IORequest *) TimerIORequest);
         }
         CloseDevice ((struct IORequest *) TimerIORequest);
      }
      DeleteIORequest(TimerIORequest);
   }

   if (InputIORequest) {
      if (!input) {
         CloseDevice ((struct IORequest *) InputIORequest);
      }
      DeleteIORequest(InputIORequest);
   }

   if (ScoutPort) {
      if (ScoutPortAdded) RemPort (ScoutPort);
      DeleteMsgPort(ScoutPort);
   }

   if (prevdir) CurrentDir(prevdir);

   FreeLibraries();
   FreeDevices();
   FreeResources();

   FreeTasks();
   FreePorts();
   FreeCommands();

   FreeExpansions();
   FreeMemory();
   FreeResidents();

   FreeAssigns();
   FreeLocks();
   FreeMounts();

   FreeInputHandler();
   FreeInterrupts();

   FreeFonts();
   FreeSemaphores();
   FreeWindows();

   FreeTimer();
   FreeLowMemory();
   FreeSMode();
   FreeCx();
   FFreeClass();
   FreeSystem();

   tbDeletePool(GlobalPool);

   if (RexxSysBase) CloseLibrary ((struct Library *)RexxSysBase);
   if (ExpansionBase) CloseLibrary ((struct Library *)ExpansionBase);
   if (IdentifyBase) CloseLibrary (IdentifyBase);
   if (CxBase) CloseLibrary (CxBase);
   if (AslBase) CloseLibrary (AslBase);
   if (DiskfontBase) CloseLibrary (DiskfontBase);
   if (UtilityBase) CloseLibrary (UtilityBase);
   if (IconBase) CloseLibrary (IconBase);
   if (IntuitionBase) CloseLibrary ((struct Library *) IntuitionBase);
   if (GfxBase) CloseLibrary ((struct Library *) GfxBase);
   if (LayersBase) CloseLibrary (LayersBase);
}

static VOID fail (VOID) {
   if (cheattask) RemoveCheatTask();

   if (AP_Scout) {
      struct DiskObject *dob = NULL;

      get(AP_Scout, MUIA_Application_DiskObject, &dob);
      if (dob) FreeDiskObject(dob);

      MUI_DisposeObject (AP_Scout);
      AP_Scout = NULL;
   }

   FreeMemoryOfAllWinFrees();

   DeleteCustomClasses();

   if (MUIMasterBase) CloseLibrary (MUIMasterBase);

   fail1();
}

struct Library *MyOpenLibrary (char *libname, ULONG version) {
   struct   Library  *libbase;

   if (!(libbase = OpenLibrary(libname,version))) {
      aprintf ("Failed to open %s version %ld!\n", libname, version);
   }
   return (libbase);
}

/*********************************************************************
                                 Init
   Diese Routine wird am Beginn des Programmes aufgerufen!
*********************************************************************/
static int init1 (VOID) {
   GfxBase = (struct GfxBase *) OpenLibrary (GRAPHICSNAME, MYLIBVERSION);
   IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", MYLIBVERSION);
   IconBase = OpenLibrary (ICONNAME, MYLIBVERSION);
   UtilityBase = OpenLibrary (UTILITYNAME, MYLIBVERSION);

   if ((!GfxBase) || (!IntuitionBase) || (!IconBase) || (!UtilityBase))
      return (FALSE);

   if (!(AslBase = MyOpenLibrary ("asl.library",MYLIBVERSION)))
      return (FALSE);

   if (!(DiskfontBase = MyOpenLibrary ("diskfont.library", MYLIBVERSION)))
      return (FALSE);

   IdentifyBase = OpenLibrary("identify.library", IDENTIFYVERSION);

   if (!(CxBase = MyOpenLibrary ("commodities.library", MYLIBVERSION)))
      return (FALSE);

   if (!(ExpansionBase = (struct ExpansionBase *)MyOpenLibrary (EXPANSIONNAME, MYLIBVERSION)))
      return (FALSE);

   if (!(RexxSysBase = (struct RxsLib *)MyOpenLibrary (RXSNAME, 0)))
      return (FALSE);

   if (!(LayersBase = MyOpenLibrary ("layers.library", MYLIBVERSION)))
      return (FALSE);

   GlobalPool = tbCreatePool(MEMF_CLEAR, 4096, 4096);

   if (tmpstr = tbAllocPooled(GlobalPool, MAXTEXTLENGTH + 1)) {
      if (tmpstr2 = tbAllocPooled(GlobalPool, MAXTEXTLENGTH + 1)) {
         //
      } else {
         return (FALSE);
      }
   } else {
      return (FALSE);
   }
   myprocess = FindTask (NULL);
   prevdir = CurrentDir (((struct Process *) myprocess)->pr_HomeDir);

   if (ScoutPort = CreateMsgPort()) {
      ScoutPort->mp_Node.ln_Name = "scout.reply";
      ScoutPort->mp_Node.ln_Pri = 1;

      TimerSignal = ScoutPort->mp_SigBit;

      AddPort (ScoutPort);
      ScoutPortAdded = TRUE;

      if (InputIORequest = CreateIORequest(ScoutPort, sizeof(struct IOStdReq))) {
         if (input = OpenDevice ("input.device", 0, (struct IORequest *) InputIORequest, 0)) {
            aprintf ("Failed to open %s!\n", "input.device");
            return (FALSE);
         }
      } else {
         return (FALSE);
      }

      if (TimerIORequest = CreateIORequest(ScoutPort, sizeof(struct timerequest))) {
         if (timer2 = OpenDevice (TIMERNAME, 0, (struct IORequest *) TimerIORequest, 0)) {
            aprintf ("Failed to open %s!\n", TIMERNAME);
            return (FALSE);
         }
      } else {
         return (FALSE);
      }
   } else {
      return (FALSE);
   }

   return (TRUE);
}

static int init2 (VOID) {
   if (!(MUIMasterBase = MyOpenLibrary (MUIMASTER_NAME,MUIVERSION)))
      return (FALSE);

   if (!CreateCustomClasses())
      return (FALSE);

   return (TRUE);
}

/*********************************************************************
                        Allerlei Funktionen
*********************************************************************/
void reverse (char s[]) {
   int c, i, j;
   for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
      c = s[i];
      s[i] = s[j];
      s[j] = c;
   }
}

char * ltob (ULONG n, int len) {
   static char s[34];
   int i;
   i = 0;

   do {
      s[i++] = n % 2 + '0';
      n /= 2;
   } while (--len > 0);
   s[i] = '\0';
   reverse (s);
   return ((char *) &s);
}

void healstring (char s[]) {
   int i = 0;

   while (s[i++] != '\0') {
      if ((s[i] > '\xfe') && (s[i] <= '\x7')) {
         s[i] = '\0';
      } else if ((s[i] > '\x7') && (s[i] <= '\x11')) {
         s[i] = ' ';
      }
   }
}

char * nonetest (char s[]) {
   int i = 0;
   char *none = "<none>";
   unsigned char *t = (unsigned char *) s;

   if ((s <= (char *) 0x200) || (s[0] == 0))
      return (none);

   while (t[i] != 0) {
      if (!(((t[i] > 8) && (t[i] < 128)) || ((t[i] > 159) && (t[i] != 215) && (t[i] != 247) && (t[i] != 255))))
         return (none);
      i++;
   }
   return (s);
}

int GetRamPointerCount (struct Library *lib) {
   int   negsize;
   int   count = 0, offset = 0;
   APTR  value;
   char  *adr;

   adr = (char *) lib;
   negsize = lib->lib_NegSize;

   while (((offset += 6) <= negsize) && *(WORD *)(adr - offset)) {
      if (*(WORD *)(adr - offset) == 0x4ef9) {
         value = (APTR) *(LONG *)(adr - offset + 2);
         if ((points2ram(value)) || (!value))
            count += 1;
      }
   }

   if (offset >= negsize) {
      return (count);
   } else {
      return (-1);
   }
}

/*********************************************************************
                               Memory
*********************************************************************/

/*********************************************************************
                              Interrupts
*********************************************************************/

/*********************************************************************
                             InputHandler
*********************************************************************/

/*********************************************************************
                              Residents
*********************************************************************/

/*********************************************************************
                               Vectors
*********************************************************************/

/*********************************************************************
                               Allocations
*********************************************************************/

/*********************************************************************
                               System
*********************************************************************/

/*********************************************************************
*********************************************************************/

__asm __saveds int priority_callfunc(register __a1 char **contents) {
   unsigned char  *pristring;
   int pri=0,length;

   get (pritext3,MUIA_String_Contents,&pristring);

        if (((length = stcd_i (pristring,&pri)) == strlen (pristring)) && \
                (pri >= -128) && (pri <= 127)) {
      set (SL_Priority,MUIA_Slider_Level,pri);
      set (WI_Priority,MUIA_Window_ActiveObject,BT_PriorityOK);
   } else {
      set (WI_Priority,MUIA_Window_ActiveObject,pritext3);
   }
   
   return (pri);
}

struct Hook priority_callhook = {
 {NULL, NULL},
 (ULONG (* )())priority_callfunc,
 NULL, NULL
};

BOOL GetPriority (char *nodename, char *pritext, LONG *pri) {
   BOOL  running = TRUE, result = FALSE;
   LONG  oldpri;
   ULONG signal;

   IsDec (pritext, &oldpri);

   WI_Priority = WindowObject,

   MUIA_Window_Title, "SCOUT: Change priority",
   MUIA_Window_ID, MakeID('P','R','I','O'),
   WindowContents, VGroup,
      Child, MyVSpace(2),
      Child, pritext1 = MyTextObject(),
      Child, MyVSpace(2),
      Child, SL_Priority = SliderObject,
         MUIA_Slider_Min, -128,
         MUIA_Slider_Max, 127,
         MUIA_Slider_Level, 0,
         MUIA_Slider_Quiet, TRUE,
         MUIA_FixHeightTxt, " ",
      End,
      Child, MyVSpace(2),
      Child, HGroup, MUIA_Group_SameWidth, TRUE,
         Child, HGroup,
            Child, MyLabel2 ("Min:"),
            Child, MyTextObject3(ESC "c -128 "), 
         End,
         Child, HGroup,
            Child, MyLabel2 ("Max:"),
            Child, MyTextObject3(ESC "c127"), 
         End,
         Child, HGroup,
            Child, MyLabel2 ("Old:"),
            Child, pritext2 = MyTextObject(), 
         End,
         Child, HGroup,
            Child, MyLabel2 ("New:"),
            Child, pritext3 = StringObject,
               MUIA_String_BufferPos, 1,
               MUIA_String_Accept , "-0123456879",
               MUIA_String_Integer, oldpri,
               MUIA_String_MaxLen, 5, StringFrame,
            End,
         End,
      End,
      Child, MyVSpace(2),
      Child, HGroup, MUIA_Group_SameWidth, TRUE,
         Child, BT_PriorityOK     = KeyButtonA (OKText    ,ID_PRIORITYOK),
         Child, BT_PriorityCancel = KeyButtonA (CancelText,ID_PRIORITYEXIT),
      End,
   End, End;

   if (WI_Priority) {
      MySetContents (pritext1, ESC "c Please select a new priority for \n'%s'! ", nodename);
      MySetContents (pritext2, ESC "c%ld",oldpri);
      set (SL_Priority,MUIA_Slider_Level,oldpri);
      set (pritext3,MUIA_Text_Contents,tmpstr);

      ApplicationSleep();

      DoMethod (AP_Scout,OM_ADDMEMBER,WI_Priority);
      DoMethod (WI_Priority,MUIM_Window_SetCycleChain,SL_Priority,pritext3,BT_PriorityCancel,BT_PriorityOK,NULL);
      SetCloseRequest (WI_Priority,(int) ID_PRIORITYEXIT);

      DoMethod (SL_Priority,MUIM_Notify,MUIA_Slider_Level,MUIV_EveryTime,pritext3,4,MUIM_SetAsString,MUIA_String_Contents,"%ld",MUIV_TriggerValue);
      DoMethod (pritext3,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,pritext3,3,MUIM_CallHook,&priority_callhook,MUIV_TriggerValue);

      SetWindowOpen (WI_Priority,SL_Priority,FALSE);

      while (running) {
         switch (DoMethod(AP_Scout,MUIM_Application_Input,&signal)) {
            case ID_PRIORITYOK:
               get (SL_Priority,MUIA_Slider_Level,pri);
               result = TRUE;
               running = FALSE;
               break;

            case ID_PRIORITYEXIT:
               running = FALSE;
               break;

            default:
               break;
         }
         if (running && signal)
            Wait (signal);
      }

      SetWindowClose (WI_Priority,FALSE);
      AwakeApplication();

      DoMethod (AP_Scout,OM_REMMEMBER,WI_Priority);
      MUI_DisposeObject (WI_Priority);
      WI_Priority = NULL;
   }
   return (result);
}

BOOL GetSignal (char *taskname, char *waitsignal, char *resultstring) {
   BOOL  running = TRUE, result = FALSE;
   LONG  signal;
   char  *textptr;

   WI_Signal = WindowObject,

   MUIA_Window_Title, "SCOUT: Send signal",
   MUIA_Window_ID, MakeID('S','E','N','D'),
   WindowContents, VGroup,
      Child, MyVSpace(2),
      Child, sigtext1 = MyTextObject(),
      Child, MyVSpace(2),
      Child, HGroup, MUIA_Group_SameWidth, TRUE,
         Child, HGroup,
            Child, KeyLabel("Signal:", 's'),
            Child, sigtext2 = StringObject,
               MUIA_ControlChar, 's',
               MUIA_String_MaxLen, 10, StringFrame,
               MUIA_String_EditHook, &hexstring_edithook,
            End,
         End,
         Child, HGroup,
            Child, MyLabel ("SigWait:"),
            Child, sigtext3 = MyTextObject(), 
         End,
      End,
      Child, MyVSpace(2),
      Child, HGroup, MUIA_Group_SameWidth, TRUE,
         Child, BT_SignalOK     = KeyButtonA (OKText    ,ID_SIGNALOK),
         Child, BT_SignalCancel = KeyButtonA (CancelText,ID_SIGNALEXIT),
      End,
   End, End;

   if (WI_Signal) {
      MySetContents (sigtext1, ESC "c Please select the signal for \n'%s'! ", taskname);
      set (sigtext2, MUIA_String_Contents, waitsignal);
      MySetContents (sigtext3, ESC "c%s", waitsignal);

      ApplicationSleep();

      DoMethod (AP_Scout,OM_ADDMEMBER,WI_Signal);
      DoMethod (WI_Signal,MUIM_Window_SetCycleChain,sigtext2,BT_SignalOK,BT_SignalCancel,NULL);
      SetCloseRequest (WI_Signal,(int) ID_SIGNALEXIT);

      SetWindowOpen (WI_Signal,sigtext2,FALSE);

      while (running) {
         switch (DoMethod(AP_Scout,MUIM_Application_Input,&signal)) {
            case ID_SIGNALOK:
               get (sigtext2,MUIA_String_Contents,&textptr);
               strcpy (resultstring, textptr);
               running = FALSE;
               result = TRUE;
               break;

            case ID_SIGNALEXIT:
               running = FALSE;
               break;

            default:
               break;
         }
         if (running && signal)
            Wait (signal);
      }

      SetWindowClose (WI_Signal,FALSE);
      AwakeApplication();

      DoMethod (AP_Scout,OM_REMMEMBER,WI_Signal);
      MUI_DisposeObject (WI_Signal);
      WI_Signal = NULL;
   }
   return (result);
}

/*********************************************************************
                             LowMemory
*********************************************************************/

/*********************************************************************
                             Timer
*********************************************************************/

/*********************************************************************
 BEGIN:                        MAIN
*********************************************************************/

__asm __saveds ULONG main (register __a0 struct WBStartup *msg) {
   BOOL  RETURN_FLAG = FALSE;
   ULONG RETURN_CODE = RETURN_OK;
   struct Library *miamilib = NULL;

   if (! init1()) {
      fail1();
      return (RETURN_FAIL);
   }

   if ((opts.User || opts.Password) && (! opts.Host)) {
      aprintf ("You didn't specify the HOST!\n");
      RETURN_CODE = RETURN_FAIL;
      RETURN_FLAG = TRUE;

   } else if (opts.Host) {
      Forbid();
      if (FindPort ("AMITCP") != NULL || (miamilib = OpenLibrary("miami.library",0L)) != NULL) {
         Permit();
         if(miamilib) CloseLibrary(miamilib);
         if (opts.Command) {
            RETURN_CODE = netshellclient();
            RETURN_FLAG = TRUE;
         } else if (! (clientstate = ConnectToServer())) {
            RETURN_CODE = RETURN_FAIL;
            RETURN_FLAG = TRUE;
         }
      } else {
         Permit();
         aprintf ("You have to start AmiTCP 4.0+ first!\n");
         RETURN_CODE = RETURN_FAIL;
         RETURN_FLAG = TRUE;
      }

   } else if (opts.Command) {
      shellstate = TRUE;
      RETURN_CODE = ExecuteCommand (opts.Command);
      RETURN_FLAG = TRUE;

   } else {
      Forbid();
      if (FindPort ("AMITCP") && isNetCall()) {
         Permit();
         RETURN_CODE = netdaemon();
         RETURN_FLAG = TRUE;
      } else {
         Permit();
      }
   }

   if (RETURN_FLAG) {
      fail1();
      return (RETURN_CODE);
   }

   if (! init2()) {
      fail();
      return (RETURN_FAIL);
   }

   if ((opts.CpuDisplay) && (! clientstate)) {
      updatetimestate = (ULONG) *opts.CpuDisplay;
   }

   if (opts.SortLibrariesType)
      libsortstate = *opts.SortLibrariesType;
   liblist_cmphook_ptr = LibSortList[libsortstate];

   if (opts.SortDevicesType)
      devsortstate = *opts.SortDevicesType;
   devlist_cmphook_ptr = LibSortList[devsortstate];

   if (opts.SortResourcesType)
      ressortstate = *opts.SortResourcesType;
   reslist_cmphook_ptr = LibSortList[ressortstate];

   if (opts.SortTasksType)
      tasksortstate = *opts.SortTasksType;
   tasklist_cmphook_ptr = TaskSortList[tasksortstate];

   if (opts.SortPortsType)
      portsortstate = *opts.SortPortsType;
   portlist_cmphook_ptr = PortSortList[portsortstate];

   if (opts.SortCommandsType)
      comsortstate = *opts.SortCommandsType;
   comlist_cmphook_ptr = ComSortList[comsortstate];

   if (opts.SortAssignsType)
      asssortstate = *opts.SortAssignsType;
   asslist_cmphook_ptr = AssSortList[asssortstate];

   if (opts.SortLocksType)
      locksortstate = *opts.SortLocksType;
   locklist_cmphook_ptr = LockSortList[locksortstate];

   if (opts.SortCommoditiesType)
      cxsortstate = *opts.SortCommoditiesType;
   cxlist_cmphook_ptr = CxSortList[cxsortstate];

   if (opts.SortClassesType)
      classsortstate = *opts.SortClassesType;
   classlist_cmphook_ptr = ClassSortList[classsortstate];

   if (opts.SortScreenmodeType)
      smodesortstate = *opts.SortScreenmodeType;
   smodelist_cmphook_ptr = SModeSortList[smodesortstate];

   if ((opts.IntervalTime) && (IsReal (opts.IntervalTime))) {
      if (strlen (opts.IntervalTime) <= 6) {
         cpuinterval_callfunc (&opts.IntervalTime);
         strcpy (updatetimetext, opts.IntervalTime);
      }
   }

   if (! GetApplication()) {
      aprintf ("Failed to create application!\n");
      fail();
      return (RETURN_FAIL);
   }

   Forbid();
   if (portname = FindMyARexxPort ("SCOUT")) {
      myarexxport = FindPort (portname);
   } else {
      portname = " < ERROR > ";
      myarexxport = NULL;
   }

   if ((opts.PortName) && (myarexxport)) {
      portname = opts.PortName;
      myarexxport->mp_Node.ln_Name = opts.PortName;
   }
   Permit();

   if (opts.Iconified) {
      set (AP_Scout, MUIA_Application_Iconified, TRUE);
   }
/*
** Everything's ready, lets launch the application. We will
** open the master window now at this old position.
*/

   DoMethod (AP_Scout,MUIM_Application_Load,MUIV_Application_Load_ENV);

   SetWindowOpen (WI_Main,NULL,FALSE);

/*
** Now the ARexx startup script will be started. Herewith it's
** possible to open window at the beginning.
*/

   if (opts.Startup) {
      BPTR startuplock;
      if (startuplock = Lock (opts.Startup, ACCESS_READ)) {
         UnLock (startuplock);
         if (! SendStartupMsg ("REXX", opts.Startup, TRUE))
            aprintf ("Failed to send port 'REXX' scriptname '%s'!\n", opts.Startup);
      } else {
         if (! SendStartupMsg (myarexxport->mp_Node.ln_Name, opts.Startup, FALSE))
            aprintf ("Failed to send port '%s' message \"%s\"!\n", myarexxport->mp_Node.ln_Name, opts.Startup);
      }
   }

/*
** This is the main loop. As you can see, it does just nothing.
** Everything is handled by MUI, no work for the programmer.
*/

   {
      BOOL     running = TRUE;
      LONG     pos, id; // für MultipleLockSelect
      ULONG    signal, received = 0;
      STRPTR   cxhelp;
      struct   ListEntry      *entryp = 0;
      struct   Task           *tmptask;
      struct   Node           *node;
      ULONG    reid;

      while (running) {
         reid = DoMethod(AP_Scout,MUIM_Application_Input,&signal);
         if(reid==MUIV_Application_ReturnID_Quit)
           running = FALSE;

         else if((reid>ID_FIRSTID) && (reid<ID_LASTID)) switch (reid) {
            case ID_LIBPRINT:
               PrintLibraries (NULL);
               break;

            case ID_LIBRARIES:
            case ID_LIBUPDATE:
               LibrariesWindow (TRUE);
               break;

            case ID_LIBREMOVE:
               if (entryp = GetActiveEntry (liblist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\n'%s'?", ((struct LibEntry *) entryp)->lib_name)) {
                     MyDoCommand ("RemoveLibrary \"%s\"", ((struct LibEntry *) entryp)->lib_name);
                     ShowLibraries ();
                  }
               }
               break;

            case ID_LIBCLOSE:
               if (entryp = GetActiveEntry (liblist)) {
                  int tmpint;
                  long tmplong;

                  if ((IsDec (((struct LibEntry *) entryp)->lib_ocnt, &tmplong)) && (tmplong)) {
                     if (tmpint = MyRequest ("Once|All|Cancel", "Do you really want to close\n '%s'?", ((struct LibEntry *) entryp)->lib_name)) {
                        if (tmpint == 1) {
                           MyDoCommand ("CloseLibrary \"%s\"", ((struct LibEntry *) entryp)->lib_name);
                           tmplong--;
                        } else {
                           while (tmplong) {
                              if (! MyDoCommand ("CloseLibrary \"%s\"", ((struct LibEntry *) entryp)->lib_name)) {
                                 break;
                              }
                              tmplong--;
                           }
                        }
                        _sprintf (((struct LibEntry *) entryp)->lib_ocnt, "%3ld ", tmplong);
                        RedrawActiveEntry (liblist);
                     }
                  } else {
                     MyRequest ("Continue", "The OpenCount of \'%s\' is already zero!", ((struct LibEntry *) entryp)->lib_name);
                  }
               }
               break;

            case ID_LIBPRIORITY:
               if (entryp = GetActiveEntry (liblist)) {
                  LONG tmppri;

                  if (GetPriority (((struct LibEntry *) entryp)->lib_name, ((struct LibEntry *) entryp)->lib_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority LIBRARY \"%s\" %ld", ((struct LibEntry *) entryp)->lib_name, tmppri)) {
                        _sprintf (((struct LibEntry *) entryp)->lib_pri, "%4ld ", tmppri);
                     }
                     ShowLibraries ();
                  }
               }
               break;

            case ID_LIBFUNCTIONS:
               if (node = HandleNodeDetails (liblist, "library"))
                  GetLDRFctList ((struct Library *) node,'L');
               break;

            case ID_LIBMORE:
               if (node = HandleNodeDetails (liblist, "library"))
                  GetLibMore ((struct Library *) node);
               break;

            case ID_LIBEXIT:
               LibrariesWindow (FALSE);
               break;

            case ID_LIBLV_ACTIVE:
               if (entryp = GetActiveEntry (liblist)) {
                  MySetContents (libtext, "%s \"%-.30s\"", ((struct LibEntry *) entryp)->lib_address, ((struct LibEntry *) entryp)->lib_name);

                  set (BT_LibRemove, MUIA_Disabled, FALSE);
                  set (BT_LibClose, MUIA_Disabled, FALSE);
                  set (BT_LibPriority, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_LibMore, MUIA_Disabled, FALSE);
                     set (BT_LibFunctions, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_DEVPRINT:
               PrintDevices (NULL);
               break;

            case ID_DEVICES:
            case ID_DEVUPDATE:
               DevicesWindow (TRUE);
               break;

            case ID_DEVREMOVE:
               if (entryp = GetActiveEntry (devlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct LibEntry *) entryp)->lib_name)) {
                     MyDoCommand ("RemoveDevice \"%s\"", ((struct LibEntry *) entryp)->lib_name);
                     ShowDevices ();
                  }
               }
               break;

            case ID_DEVPRIORITY:
               if (entryp = GetActiveEntry (devlist)) {
                  LONG tmppri;

                  if (GetPriority (((struct LibEntry *) entryp)->lib_name, ((struct LibEntry *) entryp)->lib_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority DEVICE \"%s\" %ld", ((struct LibEntry *) entryp)->lib_name, tmppri)) {
                        _sprintf (((struct LibEntry *) entryp)->lib_pri, "%4ld ", tmppri);
                     }
                     ShowDevices ();
                  }
               }
               break;

            case ID_DEVFUNCTIONS:
               if (node = HandleNodeDetails (devlist, "device"))
                  GetLDRFctList ((struct Library *) node,'D');
               break;

            case ID_DEVMORE:
               if (node = HandleNodeDetails (devlist, "device"))
                  GetDevMore ((struct Library *) node);
               break;

            case ID_DEVEXIT:
               DevicesWindow (FALSE);
               break;

            case ID_DEVLV_ACTIVE:
               if (entryp = GetActiveEntry (devlist)) {
                  MySetContents (devtext, "%s \"%s\"", ((struct LibEntry *) entryp)->lib_address, ((struct LibEntry *) entryp)->lib_name);

                  set (BT_DevRemove, MUIA_Disabled, FALSE);
                  set (BT_DevPriority, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_DevMore, MUIA_Disabled, FALSE);
                     set (BT_DevFunctions, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_RESPRINT:
               PrintResources (NULL);
               break;

            case ID_RESOURCES:
            case ID_RESUPDATE:
               ResourcesWindow (TRUE);
               break;

            case ID_RESREMOVE:
               if (entryp = GetActiveEntry (reslist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct LibEntry *) entryp)->lib_name)) {
                     MyDoCommand ("RemoveResource \"%s\"", ((struct LibEntry *) entryp)->lib_name);
                     ShowResources ();
                  }
               }
               break;

            case ID_RESPRIORITY:
               if (entryp = GetActiveEntry (reslist)) {
                  LONG tmppri;

                  if (GetPriority (((struct LibEntry *) entryp)->lib_name, ((struct LibEntry *) entryp)->lib_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority RESOURCE \"%s\" %ld", ((struct LibEntry *) entryp)->lib_name, tmppri)) {
                        _sprintf (((struct LibEntry *) entryp)->lib_pri, "%4ld ", tmppri);
                     }
                     ShowResources ();
                  }
               }
               break;

            case ID_RESFUNCTIONS:
               if (node = HandleNodeDetails (reslist, "resource"))
                  GetLDRFctList ((struct Library *) node,'R');
               break;

            case ID_RESMORE:
               if (node = HandleNodeDetails (reslist, "resource"))
                  GetResMore ((struct Library *) node);
               break;

            case ID_RESEXIT:
               ResourcesWindow (FALSE);
               break;

            case ID_RESLV_ACTIVE:
               if (entryp = GetActiveEntry (reslist)) {
                  MySetContents (restext, "%s \"%s\"", ((struct LibEntry *) entryp)->lib_address, ((struct LibEntry *) entryp)->lib_name);

                  set (BT_ResRemove, MUIA_Disabled, FALSE);
                  set (BT_ResPriority, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_ResMore, MUIA_Disabled, FALSE);
                     set (BT_ResFunctions, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_TASKPRINT:
               PrintTasks (NULL);
               break;

            case ID_TASKS:
            case ID_TASKUPDATE:
               TasksWindow (TRUE);
               break;

            case ID_CPUUSAGE:
               if (! clientstate) {
                  CheckCPUUsage();
               }
               break;

            case ID_TASKREMOVE:
               if (entryp = GetActiveEntry (tasklist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\ntask \'%s\'?", ((struct TaskEntry *) entryp)->ts_name)) {
                     BOOL withports;

                     withports = MyRequest("Yes|No", "Also remove ports, windows, etc. allocated by task\n\'%s\'?", ((struct TaskEntry *) entryp)->ts_name);

                     if (MyDoCommand ("RemoveTask %s", ((struct TaskEntry *) entryp)->ts_address, withports)) {
                        RemoveActiveEntry (tasklist);
                        SetCountText (taskcount, --taskcnt);
                     } else {
                        ShowTasks();
                     }
                  }
               }
               break;

            case ID_TASKFREEZE:
               if (entryp = GetActiveEntry (tasklist)) {
                  if (MyDoCommand ("FreezeTask %s", ((struct TaskEntry *) entryp)->ts_address)) {
                     strcpy (((struct TaskEntry *) entryp)->ts_state, "frozen");
                     RedrawActiveEntry (tasklist);
                  } else {
                     ShowTasks();
                  }
               }
               break;

            case ID_TASKACTIVATE:
               if (entryp = GetActiveEntry (tasklist)) {
                  if (MyDoCommand ("ActivateTask %s", ((struct TaskEntry *) entryp)->ts_address)) {
                     strcpy (((struct TaskEntry *) entryp)->ts_state, "wait");
                     RedrawActiveEntry (tasklist);
                  } else {
                     ShowTasks();
                  }
               }
               break;

            case ID_TASKSIGNAL:
               if (entryp = GetActiveEntry (tasklist)) {
                  char tmptext[12];

                  if (GetSignal (((struct TaskEntry *) entryp)->ts_name, ((struct TaskEntry *) entryp)->ts_sigwait, tmptext)) {
                     if (MyDoCommand ("SignalTask %s %s", ((struct TaskEntry *) entryp)->ts_address, tmptext)) {
                        Delay (25);
                        RedrawActiveEntry (tasklist);
                     } else {
                        ShowTasks();
                     }
                  }
               }
               break;

            case ID_TASKBREAK:
               if (entryp = GetActiveEntry (tasklist)) {
                  if (MyDoCommand ("BreakTask %s", ((struct TaskEntry *) entryp)->ts_address)) {
                     Delay (25);
                     RedrawActiveEntry (tasklist);
                  } else {
                     ShowTasks();
                  }
               }
               break;

            case ID_TASKPRIORITY:
               if (entryp = GetActiveEntry (tasklist)) {
                  long tmppri = 0;

                  if (GetPriority (((struct TaskEntry *) entryp)->ts_name, ((struct TaskEntry *) entryp)->ts_pri, &tmppri)) {
                     if (MyDoCommand ("SetTaskPri %s %ld", ((struct TaskEntry *) entryp)->ts_address, tmppri)) {
                        _sprintf (((struct TaskEntry *) entryp)->ts_pri, "%4ld ", tmppri);
                        RedrawActiveEntry (tasklist);
                     } else {
                        ShowTasks();
                     }
                  }
               }
               break;

            case ID_TASKMORE:
               if (entryp = GetActiveEntry (tasklist)) {
                  Forbid();
                  if (tmptask = MyFindTask (((struct TaskEntry *) entryp)->ts_address)) {
                     if (((struct Node *) tmptask)->ln_Type == NT_PROCESS) {
                        GetProcessMore ((struct Process *) tmptask);
                     } else {
                        GetTaskMore ((struct Task *) tmptask);
                     }
                     Permit();
                  } else {
                     Permit();
                     MyRequest ("Continue", "Sorry, can't find that %s!", "task");
                     ShowTasks();
                  }
               }
               break;

            case ID_TASKEXIT:
               TasksWindow (FALSE);
               break;

            case ID_TASKLV_ACTIVE:
               if (entryp = GetActiveEntry (tasklist)) {
                  MySetContentsHealed (tasktext, "%s \"%s\"", ((struct TaskEntry *) entryp)->ts_address, ((struct TaskEntry *) entryp)->ts_name);
                  set (BT_TaskRemove, MUIA_Disabled, FALSE);
                  set (BT_TaskFreeze, MUIA_Disabled, FALSE);
                  set (BT_TaskActivate, MUIA_Disabled, FALSE);
                  set (BT_TaskPriority, MUIA_Disabled, FALSE);
                  set (BT_TaskSignal, MUIA_Disabled, FALSE);
                  set (BT_TaskBreak, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_TaskMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_CXPRINT:
               PrintCx (NULL);
               break;

            case ID_COMMODITIES:
            case ID_CXUPDATE:
               CxWindow (TRUE);
               break;

            case ID_CXREMOVE:
               if (entryp = GetActiveEntry (cxlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\ncommodity \'%s\'?", ((struct CxEntry *) entryp)->cxe_name)) {
                     if (MyDoCommand ("RemoveCx %s", ((struct CxEntry *) entryp)->cxe_address)) {
                        RemoveActiveEntry (cxlist);
                        SetCountText (cxcount, --cxcnt);
                     } else {
                        ShowCx();
                     }
                  }
               }
               break;

            case ID_CXPRIORITY:
               if (entryp = GetActiveEntry (cxlist)) {
                  long tmppri = 0;

                  if (GetPriority (((struct CxEntry *) entryp)->cxe_name, ((struct CxEntry *) entryp)->cxe_pri, &tmppri)) {
                     if (MyDoCommand ("SetCxPri %s %ld", ((struct CxEntry *) entryp)->cxe_address, tmppri)) {
                        _sprintf (((struct CxEntry *) entryp)->cxe_pri, "%4ld ", tmppri);
                        RedrawActiveEntry (cxlist);
                     } else {
                        ShowCx();
                     }
                  }
               }
               break;

            case ID_CXMORE:
               if (entryp = GetActiveEntry (cxlist))
                  GetCxMore((struct MyCxObj *) ((struct CxEntry *)entryp)->cxe_adr);
               break;

            case ID_CXEXIT:
               CxWindow (FALSE);
               break;

            case ID_CXAPPEAR:
               cxhelp = "Appear";
               goto cx_setup;
            case ID_CXDISAPPEAR:
               cxhelp = "Disappear";
               goto cx_setup;
            case ID_CXENABLE:
               cxhelp = "Enable";
               goto cx_setup;
            case ID_CXDISABLE:
               cxhelp = "Disable";
               goto cx_setup;
            case ID_CXKILL:
               cxhelp = "Kill";
               goto cx_setup;
            case ID_CXLISTCHG:
               cxhelp = "ListChg";
               goto cx_setup;
            case ID_CXUNIQUE:
               cxhelp = "Unique";
cx_setup:
               if (entryp = GetActiveEntry (cxlist)) {
                  MyDoCommand ("Cx%s \"%s\"", cxhelp, ((struct CxEntry *) entryp)->cxe_name);
                  Delay(20);
                  CxWindow (TRUE);
               }
               break;

            case ID_CXLV_ACTIVE:
               if (entryp = GetActiveEntry (cxlist)) {
                  struct CxEntry *cxe = (struct CxEntry *)entryp;

                  MySetContentsHealed (cxtext, "%s \"%s\"", cxe->cxe_address, cxe->cxe_name);

                  set (BT_CxAppear, MUIA_Disabled, !(cxe->cxe_flags_int & COF_SHOW_HIDE));
                  set (BT_CxDisappear, MUIA_Disabled, !(cxe->cxe_flags_int & COF_SHOW_HIDE));
                  set (BT_CxEnable, MUIA_Disabled, FALSE);
                  set (BT_CxDisable, MUIA_Disabled, FALSE);
                  set (BT_CxKill, MUIA_Disabled, FALSE);
                  set (BT_CxListChg, MUIA_Disabled, FALSE);
                  set (BT_CxUnique, MUIA_Disabled, FALSE);
                  set (BT_CxPriority, MUIA_Disabled, FALSE);
                  set (BT_CxRemove, MUIA_Disabled, FALSE);
                  if (! clientstate) {
                     set (BT_CxMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_CLASSPRINT:
               PrintClass (NULL);
               break;

            case ID_CLASSES:
            case ID_CLASSUPDATE:
               ClassWindow (TRUE);
               break;

            case ID_CLASSREMOVE:
               if (entryp = GetActiveEntry (classlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\nclass \'%s\'?", ((struct ClassEntry *) entryp)->cse_classname)) {
                     if (MyDoCommand ("RemoveClass %s", ((struct ClassEntry *) entryp)->cse_address)) {
                        RemoveActiveEntry (classlist);
                        SetCountText (classcount, --classcnt);
                     } else {
                        ShowClass();
                     }
                  }
               }
               break;

            case ID_CLASSMORE:
               if (entryp = GetActiveEntry (classlist))
                  GetClassMore((struct IClass *) ((struct ClassEntry *)entryp)->cse_adr);
               break;

            case ID_CLASSEXIT:
               ClassWindow (FALSE);
               break;

            case ID_CLASSLV_ACTIVE:
               if (entryp = GetActiveEntry (classlist)) {
                  MySetContentsHealed (classtext, "%s \"%s\"", ((struct ClassEntry *) entryp)->cse_address, ((struct ClassEntry *) entryp)->cse_classname);

                  set (BT_ClassRemove, MUIA_Disabled, FALSE);
                  if (! clientstate) {
                     set (BT_ClassMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_SMODEPRINT:
               PrintSMode (NULL);
               break;

            case ID_SCREENMODE:
            case ID_SMODEUPDATE:
               SModeWindow (TRUE);
               break;

            case ID_SMODEMORE:
               if (entryp = GetActiveEntry (smodelist))
                  GetSModeMore((struct SModeEntry *)entryp);
               break;

            case ID_SMODEEXIT:
               SModeWindow (FALSE);
               break;

            case ID_SMODELV_ACTIVE:
               if (entryp = GetActiveEntry (smodelist)) {
                  MySetContentsHealed (smodetext, "%s \"%s\"", ((struct SModeEntry *) entryp)->sme_id, ((struct SModeEntry *) entryp)->sme_name);
                  if (! clientstate) {
                     set (BT_SModeMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_LOCKPRINT:
               PrintLocks (NULL);
               break;

            case ID_LOCKS:
            case ID_LOCKUPDATE:
               LocksWindow (TRUE);
               break;

            case ID_LOCKREMOVE:
               {
               int tmpint = 1;
               id = -1;
               pos = -42;

               for (;;) {
                  DoMethod (locklist,MUIM_List_NextSelected,&id);
                  if ((id == -1) || (tmpint == 0)) break;
                  DoMethod (locklist,MUIM_List_GetEntry,id,&entryp);
                  if (entryp) {
                     if (tmpint != 2) { // ungleich ALL
                        tmpint = MyRequest ("Yes|All|No|Abort", "Do you really want to remove lock\n\'%s\'?", ((struct LockEntry *) entryp)->lock_path);
                     }
                     get (locklist,MUIA_List_Active,&pos);
                     if (pos == id) {
                        set (locklist,MUIA_List_Active,MUIV_List_Active_Off);
                     }
                     if ((tmpint == 1) || (tmpint == 2)) { // YES oder ALL
                        if (MyDoCommand ("RemoveLock %s", ((struct LockEntry *) entryp)->lock_address)) {
                           DoMethod (locklist, MUIM_List_Remove, id);
                           id -= 1;
                        }
                     } else if (tmpint == 3) {
                        DoMethod (locklist,MUIM_List_Select,id,MUIV_List_Select_Off,NULL);
                     }
                  }
               }
               }
               break;

            case ID_LOCKEXIT:
               LocksWindow (FALSE);
               break;

            case ID_LOCKLV_ACTIVE:
               if (entryp = GetActiveEntry (locklist)) {
                  MySetContents (locktext, "%s \"%s\"", ((struct LockEntry *) entryp)->lock_address, ((struct LockEntry *) entryp)->lock_path);
                  set (BT_LockRemove, MUIA_Disabled, FALSE);
               }
               break;

            case ID_PORTPRINT:
               PrintPorts (NULL);
               break;

            case ID_PORTS:
            case ID_PORTUPDATE:
               PortsWindow (TRUE);
               break;

            case ID_PORTREMOVE:
               if (entryp = GetActiveEntry (portlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove port\n\'%s\'?", ((struct PortEntry *) entryp)->pt_name)) {
                     if (MyDoCommand ("RemovePort %s", ((struct PortEntry *) entryp)->pt_address)) {
                        RemoveActiveEntry (portlist);
                        SetCountText (portcount, --portcnt);
                     } else {
                        ShowPorts();
                     }
                  }
               }
               break;

            case ID_PORTPRIORITY:
               if (entryp = GetActiveEntry (portlist)) {
                  LONG tmppri;

                  if (GetPriority (((struct PortEntry *) entryp)->pt_name, ((struct PortEntry *) entryp)->pt_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority PORT \"%s\" %ld", ((struct PortEntry *) entryp)->pt_name, tmppri)) {
                        _sprintf (((struct PortEntry *) entryp)->pt_pri, "%4ld ", tmppri);
                     }
                     ShowPorts();
                  }
               }
               break;

            case ID_PORTMORE:
               if (node = HandleNodeDetails (portlist, "port"))
                  GetPortMore ((struct MsgPort *) node);
               break;

            case ID_PORTEXIT:
               PortsWindow (FALSE);
               break;

            case ID_PORTLV_ACTIVE:
               if (entryp = GetActiveEntry (portlist)) {
                  MySetContents (porttext, "%s \"%s\"", ((struct PortEntry *) entryp)->pt_address, ((struct PortEntry *) entryp)->pt_name);
                  set (BT_PortRemove, MUIA_Disabled, FALSE);
                  set (BT_PortPriority, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_PortMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_MEMPRINT:
               PrintMemory (NULL);
               break;

            case ID_MEMORY:
               MemoryWindow (TRUE);
               break;

            case ID_MEMPRIORITY:
               if (entryp = GetActiveEntry (memlist)) {
                  LONG tmppri;

                  if (GetPriority (((struct MemoEntry *) entryp)->mem_name, ((struct MemoEntry *) entryp)->mem_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority MEMORY \"%s\" %ld", ((struct MemoEntry *) entryp)->mem_name, tmppri)) {
                        _sprintf (((struct MemoEntry *) entryp)->mem_pri, "%4ld ", tmppri);
                     }
                     ShowMemory();
                  }
               }
               break;

            case ID_MEMMORE:
               if (node = HandleNodeDetails (memlist, "memory"))
                  GetMemMore ((struct MemHeader *) node);
               break;

            case ID_MEMEXIT:
               MemoryWindow (FALSE);
               break;

            case ID_MEMLV_ACTIVE:
               if (entryp = GetActiveEntry (memlist)) {
                  MySetContents (memtext, "%s \"%s\"", ((struct MemoEntry *) entryp)->mem_address, ((struct MemoEntry *) entryp)->mem_name);

                  set (BT_MemPriority, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_MemMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_MOUNTPRINT:
               PrintMounts (NULL);
               break;

            case ID_MOUNT:
            case ID_MOUNTUPDATE:
               MountWindow (TRUE);
               break;

            case ID_MOUNTMORE:
               if (entryp = GetActiveEntry (mountlist)) {
                  GetMountMore ((struct DeviceNode *) ((struct MountEntry *) entryp)->mt_adr);
               }
               break;

            case ID_MOUNTEXIT:
               MountWindow (FALSE);
               break;

            case ID_MOUNTLV_ACTIVE:
               if (entryp = GetActiveEntry (mountlist)) {
                  MySetContents (mounttext, "%s \"%s\"", ((struct MountEntry *) entryp)->mt_address, ((struct MountEntry *) entryp)->mt_name);

                  if (! clientstate) {
                     set (BT_MountMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_COMPRINT:
               PrintCommands (NULL);
               break;

            case ID_COMMANDS:
            case ID_COMUPDATE:
               CommandsWindow (TRUE);
               break;

            case ID_COMREMOVE:
               if ((((struct CommandEntry *) entryp)->cm_adr != NULL) && (entryp = GetActiveEntry (comlist))) {

                  if (((struct CommandEntry *) entryp)->cm_uc[0] == 'I') {
                     MyRequest ("Continue", "Can not remove INTERNAL commands!");
                  } else if (((struct CommandEntry *) entryp)->cm_uc[0] == 'S') {
                     MyRequest ("Continue", "Can not remove SYSTEM commands!");
                  } else if (((struct CommandEntry *) entryp)->cm_uc[0] == 'D') {
                     MyRequest ("Continue", "Can not remove DISABLED commands!");
                  } else {
                     if (MyRequest ("Yes|No", "Do you really want to remove command\n\'%s\'?", ((struct CommandEntry *) entryp)->cm_name)) {
                        if (MyDoCommand ("RemoveCommand %s", ((struct CommandEntry *) entryp)->cm_address)) {
                           SetCountText (comcount, --comcnt);
                           RemoveActiveEntry (comlist);
                           while (TRUE) {
                              if ((entryp = GetActiveEntry (comlist)) && (((struct CommandEntry *) entryp)->cm_address[0] == ' ')) {
                                 RemoveActiveEntry (comlist);
                              } else {
                                 break;
                              }
                           }
                        }
                     }
                  }
               }
               break;

            case ID_COMEXIT:
               CommandsWindow (FALSE);
               break;

            case ID_COMLV_ACTIVE:
               if (entryp = GetActiveEntry (comlist)) {
                  if (((struct CommandEntry *) entryp)->cm_adr == NULL) {
                     MySetContents (comtext, "");
                  } else {
                     MySetContents (comtext, "%s \"%s\"", ((struct CommandEntry *) entryp)->cm_address, ((struct CommandEntry *) entryp)->cm_name);
                  }
                  set (BT_ComRemove, MUIA_Disabled, FALSE);
               }
               break;

            case ID_INTPRINT:
               PrintInterrupts (NULL);
               break;

            case ID_INTERRUPTS:
            case ID_INTUPDATE:
               InterruptsWindow (TRUE);
               break;

            case ID_INTREMOVE:
               if (entryp = GetActiveEntry (intlist)) {
                  if (((struct IntEntry *) entryp)->int_kind[0] == 'H') {
                     MyRequest ("Resume", "Sorry, I can't remove an\ninterrupt handler!");
                  } else {
                     if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct IntEntry *) entryp)->int_name)) {
                        MyDoCommand ("RemoveInterrupt \"%s\"", ((struct IntEntry *) entryp)->int_name);
                        ShowInterrupts();
                     }
                  }
               }
               break;

            case ID_INTMORE:
               if (entryp = GetActiveEntry (intlist)) {
                  GetIntMore ((struct IntEntry *) entryp);
               }
               break;

            case ID_INTEXIT:
               InterruptsWindow (FALSE);
               break;

            case ID_INTLV_ACTIVE:
               if (entryp = GetActiveEntry (intlist)) {
                  MySetContents (inttext, "%s \"%s\"", ((struct IntEntry *) entryp)->int_address, ((struct IntEntry *) entryp)->int_name);
                  set (BT_IntRemove, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_IntMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_SEMPRINT:
               PrintSemaphores (NULL);
               break;

            case ID_SEMAPHORES:
            case ID_SEMUPDATE:
               SemaphoresWindow (TRUE);
               break;

            case ID_SEMOBTAIN:
               if (entryp = GetActiveEntry (semlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to obtain semaphore\n\'%s\'?", ((struct SemEntry *) entryp)->sm_name)) {
                     if (MyDoCommand ("ObtainSemaphore %s", ((struct SemEntry *) entryp)->sm_address)) {
                        ShowSemaphores();
                     } else {
                        ShowSemaphores();
                     }
                  }
               }
               break;

            case ID_SEMRELEASE:
               if (entryp = GetActiveEntry (semlist)) {
                  long  tmplong;

                  if ((IsDec (((struct SemEntry *) entryp)->sm_nestcount, &tmplong)) && (tmplong)) {
                     if (MyRequest ("Yes|No", "Do you really want to release semaphore\n\'%s\'?", ((struct SemEntry *) entryp)->sm_name)) {
                        MyDoCommand ("ReleaseSemaphore %s", ((struct SemEntry *) entryp)->sm_address);
                        ShowSemaphores();
                     }
                  } else {
                     MyRequest ("Continue", "Sorry, the nestcount is already zero!");
                  }
               }
               break;

            case ID_SEMREMOVE:
               if (entryp = GetActiveEntry (semlist)) {
                  long tmplong;

                  if ((IsDec (((struct SemEntry *) entryp)->sm_nestcount, &tmplong)) && (tmplong)) {
                     MyRequest ("Continue", "Sorry, the nestcount is not zero!");
                  } else {
                     if (MyRequest ("Yes|No", "Do you really want to remove semaphore\n\'%s\'?", ((struct SemEntry *) entryp)->sm_name)) {
                        MyDoCommand ("RemoveSemaphore %s", ((struct SemEntry *) entryp)->sm_address);
                        ShowSemaphores();
                     }
                  }
               }
               break;

            case ID_SEMEXIT:
               SemaphoresWindow (FALSE);
               break;

            case ID_SEMLV_ACTIVE:
               if (entryp = GetActiveEntry (semlist)) {
                  MySetContents (semtext, "%s \"%s\"", ((struct SemEntry *) entryp)->sm_address, ((struct SemEntry *) entryp)->sm_name);
                  set (BT_SemObtain, MUIA_Disabled, FALSE);
                  set (BT_SemRelease, MUIA_Disabled, FALSE);
                  set (BT_SemRemove, MUIA_Disabled, FALSE);
               }
               break;

            case ID_INPUTPRINT:
               PrintInputHandlers (NULL);
               break;

            case ID_INPUTHANDLER:
            case ID_INPUTUPDATE:
               InputHandlerWindow (TRUE);
               break;

            case ID_INPUTREMOVE:
               if (entryp = GetActiveEntry (inputlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct InputEntry *) entryp)->input_name)) {
                     if (MyDoCommand ("RemoveInputhandler %s", ((struct InputEntry *) entryp)->input_address)) {
                        SetCountText (inputcount, --inputcnt);
                        RemoveActiveEntry (inputlist);
                     } else {
                        ShowInputHandler();
                     }
                  }
               }
               break;

            case ID_INPUTPRIORITY:
               if (entryp = GetActiveEntry (inputlist)) {
                  LONG tmppri;

                  if (GetPriority (((struct InputEntry *) entryp)->input_name, ((struct InputEntry *) entryp)->input_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority INPUTHANDLER \"%s\" %ld", ((struct InputEntry *) entryp)->input_name, tmppri)) {
                        _sprintf (((struct InputEntry *) entryp)->input_pri, "%4ld ", tmppri);
                     }
                     ShowInputHandler();
                  }
               }
               break;

            case ID_INPUTEXIT:
               InputHandlerWindow (FALSE);
               break;

            case ID_INPUTLV_ACTIVE:
               if (entryp = GetActiveEntry (inputlist)) {
                  MySetContents (inputtext, "%s \"%s\"", ((struct InputEntry *) entryp)->input_address, ((struct InputEntry *) entryp)->input_name);
                  set (BT_InputRemove, MUIA_Disabled, FALSE);
                  set (BT_InputPriority, MUIA_Disabled, FALSE);
               }
               break;

            case ID_EXPPRINT:
               PrintExpansions (NULL);
               break;

            case ID_EXPANSIONS:
               ExpansionsWindow (TRUE);
               break;

            case ID_EXPMORE:
               if (entryp = GetActiveEntry (explist)) {
                  GetExpansionMore ((struct ConfigDev *) ((struct ExpEntry *) entryp)->exp_adr);
               }
               break;

            case ID_EXPEXIT:
               ExpansionsWindow (FALSE);
               break;

            case ID_EXPLV_ACTIVE:
               if (entryp = GetActiveEntry (explist)) {
                  _sprintf(tmpstr,"Size=%s Flags=%s ID=%s:%s SN=%s",
                      ((struct ExpEntry *)entryp)->exp_boardsize,
                      ((struct ExpEntry *)entryp)->exp_flags,
                      ((struct ExpEntry *)entryp)->exp_manufacturer,
                      ((struct ExpEntry *)entryp)->exp_product,
                      ((struct ExpEntry *)entryp)->exp_serialnumber);
                  set (exptext,MUIA_Text_Contents,tmpstr);

                  if (!clientstate) {
                     set (BT_ExpMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_RESIPRINT:
               PrintResidents (NULL);
               break;

            case ID_RESIDENTS:
            case ID_RESIUPDATE:
               ResidentsWindow (TRUE);
               break;

            case ID_RESIMORE:
               if (entryp = GetActiveEntry (resilist)) {
                  GetResiMore (((struct ResiEntry *) entryp)->rt_adr);
               }
               break;

            case ID_RESIEXIT:
               ResidentsWindow (FALSE);
               break;

            case ID_RESILV_ACTIVE:
               if (entryp = GetActiveEntry (resilist)) {
                  MySetContentsHealed (resitext, "%s \"%s\"", ((struct ResiEntry *) entryp)->rt_address, ((struct ResiEntry *) entryp)->rt_name);

                  if (! clientstate) {
                     set (BT_ResiMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_WINPRINT:
               PrintWindows (NULL);
               break;

            case ID_WINDOWS:
            case ID_WINUPDATE:
               WindowsWindow (TRUE);
               break;

            case ID_WINCLOSE:
               if (entryp = GetActiveEntry (winlist)) {
                  if (((struct WinEntry *) entryp)->wn_type == 'S') {
                     if ((! clientstate) && (((struct Screen *) ((struct WinEntry *) entryp)->wn_adr)->FirstWindow)) {
                        if (MyRequest ("Yes|No", "Do you really want to close this\n%s?", "screen with all its windows")) {
                           MyDoCommand ("CloseScreen %s", ((struct WinEntry *) entryp)->wn_address + 2);
                           ShowWindows();
                        }
                     } else {
                        if (MyRequest ("Yes|No", "Do you really want to close this\n%s?", "screen")) {
                           MyDoCommand ("CloseScreen %s", ((struct WinEntry *) entryp)->wn_address + 2);
                           ShowWindows();
                        }
                     }
                  } else {
                     if (MyRequest ("Yes|No", "Do you really want to close this\n%s?", "window")) {
                        MyDoCommand ("CloseWindow %s", ((struct WinEntry *) entryp)->wn_address);
                        ShowWindows();
                     }
                  }
               }
               break;

            case ID_WINTOFRONT:
               if (entryp = GetActiveEntry (winlist)) {
                  if (((struct WinEntry *) entryp)->wn_type == 'S') {
                     MyDoCommand ("PopToFront %s", ((struct WinEntry *) entryp)->wn_address + 2);
                  } else {
                     MyDoCommand ("PopToFront %s", ((struct WinEntry *) entryp)->wn_address);
                  }
               }
               break;

            case ID_WINMORE:
               if (entryp = GetActiveEntry (winlist)) {
                  if (((struct WinEntry *) entryp)->wn_type == 'W') {
                     struct Window *tmpwindow;

                     if (tmpwindow = MyFindWindow (((struct WinEntry *) entryp)->wn_address))
                        GetWindowMore (tmpwindow);
                     else
                        MyRequest ("Continue", "Sorry, can't find that %s!", "window");
                  } else {
                     struct Screen *tmpscreen;

                     if (tmpscreen = MyFindScreen (((struct WinEntry *) entryp)->wn_address + 2))
                        GetScreenMore (tmpscreen);
                     else
                        MyRequest ("Continue", "Sorry, can't find that %s!", "screen");
                  }
               }
               break;

            case ID_WINEXIT:
               WindowsWindow (FALSE);
               break;

            case ID_WINLV_ACTIVE:
               if (entryp = GetActiveEntry (winlist)) {
                  if (((struct WinEntry *) entryp)->wn_type == 'S') {
                     MySetContentsHealed (wintext, "%s \"%s\"", ((struct WinEntry *) entryp)->wn_address, ((struct WinEntry *) entryp)->wn_title);
                  } else {
                     MySetContentsHealed (wintext, "%s \"%s\"", ((struct WinEntry *) entryp)->wn_address, ((struct WinEntry *) entryp)->wn_title);
                  }
                  set (BT_WinClose, MUIA_Disabled, FALSE);
                  set (BT_WinToFront, MUIA_Disabled, FALSE);

                  if (! clientstate) {
                     set (BT_WinMore, MUIA_Disabled, FALSE);
                  }
               }
               break;

            case ID_ALCPRINT:
               PrintAllocations (NULL);
               break;

            case ID_ALLOCATIONS:
            case ID_ALCUPDATE:
               AllocationsWindow (TRUE);
               break;

            case ID_ALCEXIT:
               AllocationsWindow (FALSE);
               break;

            case ID_ASSPRINT:
               PrintAssigns (NULL);
               break;

            case ID_ASSIGNS:
            case ID_ASSUPDATE:
               AssignsWindow (TRUE);
               break;

            case ID_ASSREMOVE:
               if (entryp = GetActiveEntry (asslist)) {
                  if (((struct AssEntry *) entryp)->ass_path[0] == ' ') {
                     if (MyRequest ("Yes|No", "Do you really want to remove directory\n\'%s\' from assign '%s:\'?", ((struct AssEntry *) entryp)->ass_path +4, ((struct AssEntry *) entryp)->ass_name)) {
                        if (MyDoCommand ("RemoveAssignList %s $%lx", ((struct AssEntry *) entryp)->ass_name, ((struct AssEntry *) entryp)->ass_adr)) {
                           SetCountText (asscount, --asscnt);
                           RemoveActiveEntry (asslist);
                        } else {
                           ShowAssigns();
                        }
                     }
                  } else {
                     if (MyRequest ("Yes|No", "Do you really want to remove assign\n\'%s:\'?", ((struct AssEntry *) entryp)->ass_name)) {
                        if (MyDoCommand ("RemoveAssign %s", ((struct AssEntry *) entryp)->ass_name)) {
                           SetCountText (asscount, --asscnt);
                           RemoveActiveEntry (asslist);
                           while (TRUE) {
                              if ((entryp = GetActiveEntry (asslist)) && (((struct AssEntry *) entryp)->ass_path[0] == ' ')) {
                                 RemoveActiveEntry (asslist);
                              } else {
                                 break;
                              }
                           }
                        } else {
                           ShowAssigns();
                        }
                     }
                  }
               }
               break;

            case ID_ASSEXIT:
               AssignsWindow (FALSE);
               break;

            case ID_ASSLV_ACTIVE:
               if (entryp = GetActiveEntry (asslist)) {
                  if (((struct AssEntry *) entryp)->ass_path[0] != ' ') {
                     MySetContents (asstext, "%s \"%s\"", ((struct AssEntry *) entryp)->ass_address, ((struct AssEntry *) entryp)->ass_path);
                  } else {
                     MySetContents (asstext, "%s \"%s\"", ((struct AssEntry *) entryp)->ass_address, ((struct AssEntry *) entryp)->ass_path +4);
                  }
                  set (BT_AssRemove, MUIA_Disabled, FALSE);
               }
               break;

            case ID_FONTPRINT:
               PrintFonts (NULL);
               break;

            case ID_FONTS:
            case ID_FONTUPDATE:
               FontsWindow (TRUE);
               break;

            case ID_FONTCLOSE:
               if (entryp = GetActiveEntry (fontlist)) {
                  int tmpint, tmpint2;

                  if ((IsDec (((struct FontEntry *) entryp)->font_count, (long *) &tmpint2)) && (tmpint2)) {
                     if (tmpint = MyRequest ("Once|All|Cancel", "Do you really want to close\n\"%s\"?", ((struct FontEntry *) entryp)->font_name)) {
                        if (tmpint == 1) {
                           MyDoCommand ("CloseFont %s", ((struct FontEntry *) entryp)->font_address);
                        } else {
                           while (tmpint2) {
                              if (! MyDoCommand ("CloseFont %s", ((struct FontEntry *) entryp)->font_address)) {
                                 break;
                              }
                              tmpint2--;
                           }
                        }
                        ShowFonts ();
                     }
                  } else {
                     MyRequest ("Continue", "Sorry, the count of accessors is already zero!");
                  }
               }
               break;

            case ID_FONTREMOVE:
               if (entryp = GetActiveEntry (fontlist)) {
                  long  tmplong;

                  if ((IsDec (((struct FontEntry *) entryp)->font_count, &tmplong)) && (tmplong)) {
                     MyRequest ("Continue", "Sorry, the count of accessors is not zero!");
                  } else {
                     if (MyRequest ("Yes|No", "Do you really want to remove\n\"%s\" from system?", ((struct FontEntry *) entryp)->font_name)) {
                        MyDoCommand ("RemoveFont %s", ((struct FontEntry *) entryp)->font_address);
                        ShowFonts ();
                     }
                  }
               }
               break;

            case ID_FONTEXIT:
               FontsWindow (FALSE);
               break;

            case ID_FONTLV_ACTIVE:
               if (entryp = GetActiveEntry (fontlist)) {
                  MySetContents (fonttext, "%s \"%s\"", ((struct FontEntry *) entryp)->font_address, ((struct FontEntry *) entryp)->font_name);
                  set (BT_FontClose, MUIA_Disabled, FALSE);
                  set (BT_FontRemove, MUIA_Disabled, FALSE);
                  set (LB_FontExample, FDATTR_Font, ((struct FontEntry *) entryp)->font_adr);
               }
               break;

            case ID_VECPRINT:
               PrintVectors (NULL);
               break;

            case ID_VECTORS:
            case ID_VECUPDATE:
               VectorsWindow (TRUE);
               break;

            case ID_VECEXIT:
               VectorsWindow (FALSE);
               break;

            case ID_SYSPRINT:
               PrintSystem (NULL);
               break;

            case ID_SYSTEM:
            case ID_SYSUPDATE:
               SystemWindow (TRUE);
               break;

            case ID_SYSEXIT:
               SystemWindow (FALSE);
               break;

            case ID_FLUSHDEVS:
               MyDoCommand ("FLUSHDEVS");
               break;

            case ID_FLUSHFONTS:
               MyDoCommand ("FLUSHFONTS");
               break;

            case ID_FLUSHALL:
               MyDoCommand ("FLUSHALL");
               break;

            case ID_FLUSHLIBS:
               MyDoCommand ("FLUSHLIBS");
               break;

            case ID_REMOVEWINFREE:
               if (WinFree_ToDo) {
                  RemoveWinFree ();
                  WinFree_ToDo = NULL;
               }
               break;

            case ID_FLAGWINDOW:
               if (WinFlag_ToDo) {
                  GetFlagsMore (WinFlag_ToDo->wf_title2, WinFlag_ToDo->wf_fieldname, WinFlag_ToDo->wf_flags, WinFlag_ToDo->wf_longflag, WinFlag_ToDo->wf_mlongflag, WinFlag_ToDo->wf_fieldtype);
                  WinFlag_ToDo = NULL;
               }
               break;               

            case ID_CLEARRESET:
               ClearResetVectors();
               break;

            case ID_ABOUT:
               AboutWindow (TRUE);
               break;

            case ID_ABOUTCONTINUE:
               AboutWindow (FALSE);
               break;

            case ID_LIBSORT:
                                   get (CY_LibSort,MUIA_Cycle_Active,&libsortstate);
               liblist_cmphook_ptr = LibSortList[libsortstate];
               ShowLibraries();
               break;

            case ID_DEVSORT:
                                   get (CY_DevSort,MUIA_Cycle_Active,&devsortstate);
               devlist_cmphook_ptr = LibSortList[devsortstate];
               ShowDevices();
               break;

            case ID_RESSORT:
                                   get (CY_ResSort,MUIA_Cycle_Active,&ressortstate);
               reslist_cmphook_ptr = LibSortList[ressortstate];
               ShowResources();
               break;

            case ID_TASKSORT:
                                   get (CY_TaskSort,MUIA_Cycle_Active,&tasksortstate);
               tasklist_cmphook_ptr = TaskSortList[tasksortstate];
               ShowTasks();
               break;

            case ID_CXSORT:
                                   get (CY_CxSort,MUIA_Cycle_Active,&cxsortstate);
               cxlist_cmphook_ptr = CxSortList[cxsortstate];
               ShowCx();
               break;

            case ID_CLASSSORT:
                                   get (CY_ClassSort,MUIA_Cycle_Active,&classsortstate);
               classlist_cmphook_ptr = ClassSortList[classsortstate];
               ShowClass();
               break;

            case ID_SMODESORT:
                                   get (CY_SModeSort,MUIA_Cycle_Active,&smodesortstate);
               smodelist_cmphook_ptr = SModeSortList[smodesortstate];
               ShowSMode();
               break;

            case ID_PORTSORT:
                                   get (CY_PortSort,MUIA_Cycle_Active,&portsortstate);
               portlist_cmphook_ptr = PortSortList[portsortstate];
               ShowPorts();
               break;

            case ID_COMSORT:
                                   get (CY_ComSort,MUIA_Cycle_Active,&comsortstate);
               comlist_cmphook_ptr = ComSortList[comsortstate];
               ShowCommands();
               break;

            case ID_ASSSORT:
                                   get (CY_AssSort,MUIA_Cycle_Active,&asssortstate);
               asslist_cmphook_ptr = AssSortList[asssortstate];
               ShowAssigns();
               break;

            case ID_LOCKSORT:
                                   get (CY_LockSort,MUIA_Cycle_Active,&locksortstate);
               locklist_cmphook_ptr = LockSortList[locksortstate];
               ShowLocks();
               break;

            case ID_LOWMEMORYPRINT:
               PrintLowMemory (NULL);
               break;

            case ID_LOWMEMORY:
            case ID_LOWMEMORYUPDATE:
               LowMemoryWindow (TRUE);
               break;

/*
            case ID_LOWMEMORYCAUSE:
               if (entryp = GetActiveEntry (lowmemorylist)) {
                  MyDoCommand ("CauseLowMemory %s", ((struct LowMemoryEntry *) entryp)->lowmemory_address);
               }
               break;
*/

            case ID_LOWMEMORYREMOVE:
               if (entryp = GetActiveEntry (lowmemorylist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct LowMemoryEntry *) entryp)->lowmemory_name)) {
                     if (MyDoCommand ("RemoveLowMemory %s", ((struct LowMemoryEntry *) entryp)->lowmemory_address)) {
                        SetCountText (lowmemorycount, --lowmemorycnt);
                        RemoveActiveEntry (lowmemorylist);
                     } else {
                        ShowLowMemory();
                     }
                  }
               }
               break;

            case ID_LOWMEMORYPRIORITY:
               if (entryp = GetActiveEntry (lowmemorylist)) {
                  LONG tmppri;

                  if (GetPriority (((struct LowMemoryEntry *) entryp)->lowmemory_name, ((struct LowMemoryEntry *) entryp)->lowmemory_pri, &tmppri)) {
                     if (MyDoCommand ("SetPriority LOWMEMORY \"%s\" %ld", ((struct LowMemoryEntry *) entryp)->lowmemory_name, tmppri)) {
                        _sprintf (((struct LowMemoryEntry *) entryp)->lowmemory_pri, "%4ld ", tmppri);
                     }
                     ShowLowMemory();
                  }
               }
               break;

            case ID_LOWMEMORYEXIT:
               LowMemoryWindow (FALSE);
               break;

            case ID_LOWMEMORYLV_ACTIVE:
               if (entryp = GetActiveEntry (lowmemorylist)) {
                  MySetContents (lowmemorytext, "%s \"%s\"", ((struct LowMemoryEntry *) entryp)->lowmemory_address, ((struct LowMemoryEntry *) entryp)->lowmemory_name);
//*                  set (BT_LowMemoryCause, MUIA_Disabled, FALSE);
                  set (BT_LowMemoryRemove, MUIA_Disabled, FALSE);
                  set (BT_LowMemoryPriority, MUIA_Disabled, FALSE);
               }
               break;

            case ID_TIMERPRINT:
               PrintTimer (NULL);
               break;

            case ID_TIMER:
            case ID_TIMERUPDATE:
               TimerWindow (TRUE);
               break;

/*
            case ID_TIMERREMOVE:
               if (entryp = GetActiveEntry (timerlist)) {
                  if (MyRequest ("Yes|No", "Do you really want to remove\n\'%s\'?", ((struct TimerEntry *) entryp)->timer_name)) {
                     if (MyDoCommand ("RemoveTimer %s", ((struct TimerEntry *) entryp)->timer_address)) {
                        SetCountText (timercount, --timercnt);
                        RemoveActiveEntry (timerlist);
                     } else {
                        ShowTimer();
                     }
                  }
               }
               break;
*/
            case ID_TIMEREXIT:
               TimerWindow (FALSE);
               break;

            case ID_TIMERLV_ACTIVE:
               if (entryp = GetActiveEntry (timerlist)) {
                  MySetContents (timertext, "%s \"%s\"", ((struct TimerEntry *) entryp)->timer_address, ((struct TimerEntry *) entryp)->timer_name);
//*                  set (BT_TimerRemove, MUIA_Disabled, FALSE);
               }
               break;

            default:
               break;
         }

         if (running && signal) {
            signal = (signal | 0x0000f000 | 0x01<<TimerSignal);
            received = Wait (signal);
         }         

//         aprintf ("\nSIGNAL: $%08lx  RECEIVED: $%08lx\n", signal, received);

         if (0x7000 & received) {
            running = FALSE;
            aprintf ("***Break\n");
         }

         if (0x8000 & received)
            set (AP_Scout,MUIA_Application_Iconified,FALSE);

         if (0x01<<TimerSignal & received) {
//            tmpint++;
            GetMsg(ScoutPort);
            HandleTimerRequest (TRUE);
         }

         signal = (signal & 0xffff0fff & 0x01<<TimerSignal);
      }
   }

   if (opts.Host) {
      SendDaemon (CMD_END);
   }
   fail();
   return (RETURN_OK);
}

/*********************************************************************
 END:                           MAIN
*********************************************************************/
