classify/sigmenu.cpp

Go to the documentation of this file.
00001 
00019 /* =================
00020  Include Files and Type Defines
00021  ==================== */
00022 #include "sigmenu.h"
00023 #include "oldlist.h"
00024 #include "emalloc.h"
00025 #include "secname.h"
00026 
00027 #include <signal.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 
00031 #define MAX_COMMAND_LENGTH  128
00032 
00037 typedef struct
00038 {
00039   int ItemNum;
00040   char *ItemLabel;
00041   int_void ItemFunc;
00042 } SIG_MENU_ITEM;
00043 
00044 /* =================
00045         Global Data Definitions and Declarations
00046  ==================== */
00047 static LIST SignalMenus[NSIG];   /* must be initialized to NIL */
00048 
00049 /* =================
00050  Private Function Prototypes
00051  ==================== */
00052 void MainSignalHandler(int Signal); 
00053 
00054 SIG_MENU_ITEM *NewSignalMenuItem (int ItemNum,
00055 const char ItemLabel[], int_void ItemFunc);
00056 
00057 int ItemCompare(void *arg1,   //SIG_MENU_ITEM         *Item1,
00058                 void *arg2);  //SIG_MENU_ITEM         *Item2);
00059 
00060 /* =================
00061      Public Code
00062  ==================== */
00063 /* =============================== */
00081 void
00082 AddSignalMenuItem (int Signal,
00083 int ItemNum, const char ItemLabel[], int_void ItemFunc) {
00084   #if 0
00085   #ifndef SECURE_NAMES
00086   SIG_MENU_ITEM *NewItem;
00087 
00088   /* check for a valid Signal */
00089   if (Signal >= NSIG || Signal <= 0) {
00090     cprintf ("Illegal signal (%d) specified for menu item!\n", Signal);
00091     return;
00092   }
00093 
00094   /* if this is the first item for this signal, indicate that the
00095      appropriate signal handler has been enabled */
00096   if (SignalMenus[Signal] == NIL)
00097     cprintf ("Signal handler enabled for signal %d.\n", Signal);
00098 
00099   /* add the new menu item to the appropriate list of menu items */
00100   NewItem = NewSignalMenuItem (ItemNum, ItemLabel, ItemFunc);
00101   SignalMenus[Signal] = s_adjoin (SignalMenus[Signal], NewItem, ItemCompare);
00102 
00103   /* set up the trap for the appropriate signal */
00104   signal(Signal, MainSignalHandler); 
00105   #endif
00106   #endif
00107 }                                /* AddSignalMenuItem */
00108 
00109 
00110 /* =================
00111     Private Code
00112  ==================== */
00113 /* =============================== */
00128 void MainSignalHandler(int Signal) { 
00129   #ifndef SECURE_NAMES
00130   int Command;
00131   char CommandLine[MAX_COMMAND_LENGTH];
00132   char *Params;
00133   LIST Items;
00134   SIG_MENU_ITEM *MenuItem;
00135 
00136   while (TRUE) {
00137     Command = -1;
00138     cprintf ("\nMAIN SIGNAL HANDLER FOR SIGNAL %d\n", Signal);
00139     cprintf ("0. Resume normal operation\n");
00140 
00141     Items = SignalMenus[Signal];
00142     iterate(Items) { 
00143       MenuItem = (SIG_MENU_ITEM *) first (Items);
00144       cprintf ("%d. %s\n", MenuItem->ItemNum, MenuItem->ItemLabel);
00145     }
00146     cprintf ("\nEnter Selection: ");
00147 
00148     while (fgets (CommandLine, MAX_COMMAND_LENGTH, stdin) == NULL
00149       || strlen (CommandLine) <= 0);
00150 
00151     Command = strtol (CommandLine, &Params, 10);
00152     if (CommandLine == Params) {
00153       cprintf ("\nIllegal command! - Try again.\n");
00154       continue;
00155     }
00156 
00157     if (Command == 0)
00158       signal(Signal, MainSignalHandler); 
00159 
00160     Items = SignalMenus[Signal];
00161     iterate(Items) { 
00162       MenuItem = (SIG_MENU_ITEM *) first (Items);
00163       if (Command == MenuItem->ItemNum) {
00164         if ((*MenuItem->ItemFunc) ( /*Params */ ) == SIG_RESUME)
00165           signal(Signal, MainSignalHandler); 
00166         break;
00167       }
00168     }
00169     if (Items == NIL)
00170       cprintf ("\nIllegal command! - Try again.\n");
00171   }
00172   #endif
00173 }                                /* MainSignalHandler */
00174 
00175 
00176 /* =============================== */
00187 SIG_MENU_ITEM *
00188 NewSignalMenuItem (int ItemNum, const char ItemLabel[], int_void ItemFunc) {
00189   SIG_MENU_ITEM *NewItem;
00190 
00191   NewItem = (SIG_MENU_ITEM *) Emalloc (sizeof (SIG_MENU_ITEM));
00192   NewItem->ItemNum = ItemNum;
00193   NewItem->ItemFunc = ItemFunc;
00194   NewItem->ItemLabel = (char *) Emalloc (strlen (ItemLabel) + 1);
00195   strcpy (NewItem->ItemLabel, ItemLabel);
00196   return (NewItem);
00197 
00198 }                                /* NewSignalMenuItem */
00199 
00200 
00201 /* =============================== */
00219 int ItemCompare(void *arg1,    //SIG_MENU_ITEM         *Item1,
00220                 void *arg2) {  //SIG_MENU_ITEM         *Item2)
00221   SIG_MENU_ITEM *Item1 = (SIG_MENU_ITEM *) arg1;
00222   SIG_MENU_ITEM *Item2 = (SIG_MENU_ITEM *) arg2;
00223 
00224   if (Item1->ItemNum < Item2->ItemNum)
00225     return (-1);
00226   else if (Item1->ItemNum == Item2->ItemNum)
00227     return (0);
00228   else if (Item1->ItemNum > Item2->ItemNum)
00229     return (1);
00230   else
00231     return 0;
00232 }                                /* ItemCompare */

Generated on Wed Feb 28 19:49:10 2007 for Tesseract by  doxygen 1.5.1