home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_10 / 9n10026a < prev    next >
Text File  |  1991-08-14  |  2KB  |  56 lines

  1.  
  2. /* --------------------------------------------------------------
  3.  
  4. FUNCTION LOOKUP: 
  5.  
  6. A.  Define the table of valid commands, in lowercase.  Corresponding
  7.     to each command there is a pointer to the function to be called
  8.     to implement that command. Note that the commands are in 
  9.     alphabetical order.
  10.  
  11. B.  Search the command table looking for the supplied string. If there 
  12.     is an exact match return the corresponding function's address. If 
  13.     the next entry is greater than the one we are looking for it must 
  14.     not exist since the table is sorted in alpha order by command.
  15.     Therefore, we return the address of an error handling function.
  16.  
  17. C.  If we search the entire table and don't find a match we also 
  18.     return the error function's address.
  19.  
  20. -------------------------------------------------------------- */
  21.  
  22. void (*lookup(const char *pcmd))(void)
  23. {
  24. /*A*/   static const struct {
  25.                 char *pcommand;
  26.                 void (*pcmd_fun)(void);
  27.         } cmd_table[] = {
  28.                 {"add node", add_node},
  29.                 {"count nodes", count_nodes},
  30.                 {"display node", display_node},
  31.                 {"dump ascending", dump_asc_nodes},
  32.                 {"dump descending", dump_des_nodes},
  33.                 {"exit", myexit},
  34.                 {"help", help},
  35.                 {"remove node", remove_node},
  36.         };
  37.  
  38.         void bad_cmd(void);
  39.  
  40.         int i;
  41.         int comp;
  42.  
  43. /*B*/   for (i = 0; i < NUMELEM(cmd_table); ++i) {
  44.                 comp = strncmp(pcmd, cmd_table[i].pcommand, strlen(pcmd));
  45.                 if (comp == 0) {
  46.                         return cmd_table[i].pcmd_fun;
  47.                 }
  48.                 else if (comp < 1) {
  49.                         return &bad_cmd;
  50.                 }
  51.         }
  52.  
  53. /*C*/   return &bad_cmd;
  54. }
  55.  
  56.