
/* --------------------------------------------------------------

FUNCTION LOOKUP: 

A.  Define the table of valid commands, in lowercase.  Corresponding
    to each command there is a pointer to the function to be called
    to implement that command. Note that the commands are in 
    alphabetical order.

B.  Search the command table looking for the supplied string. If there 
    is an exact match return the corresponding function's address. If 
    the next entry is greater than the one we are looking for it must 
    not exist since the table is sorted in alpha order by command.
    Therefore, we return the address of an error handling function.

C.  If we search the entire table and don't find a match we also 
    return the error function's address.

-------------------------------------------------------------- */

void (*lookup(const char *pcmd))(void)
{
/*A*/   static const struct {
                char *pcommand;
                void (*pcmd_fun)(void);
        } cmd_table[] = {
                {"add node", add_node},
                {"count nodes", count_nodes},
                {"display node", display_node},
                {"dump ascending", dump_asc_nodes},
                {"dump descending", dump_des_nodes},
                {"exit", myexit},
                {"help", help},
                {"remove node", remove_node},
        };

        void bad_cmd(void);

        int i;
        int comp;

/*B*/   for (i = 0; i < NUMELEM(cmd_table); ++i) {
                comp = strncmp(pcmd, cmd_table[i].pcommand, strlen(pcmd));
                if (comp == 0) {
                        return cmd_table[i].pcmd_fun;
                }
                else if (comp < 1) {
                        return &bad_cmd;
                }
        }

/*C*/   return &bad_cmd;
}

