Scott M. Mcdermott

UNIX Systems & Network Administrator
available for contract or salaried positions

command.h

/*
 * Command input library.
 */

#ifndef _COMMAND_H
# define _COMMAND_H

/* this global controls whether or not the library will only
 * parse as many characters as needed to get a unique
 * command and accept input after that without needing the
 * user to use the flush input character (i.e. enter) */
extern bool libcommand_use_as_many_as_unique;
extern char *libcommand_default_command;
extern char *libcommand_eof_command;

/* This is the type of the callback run when a command is
 * matched.  It is given arguments in the form of list. */
typedef bool (*cmd_fn_t)(dbll_head_t *);

/* Each command which the user program can initiate is
 * represented with this type. */
typedef struct command {
        char            *identifier;
        cmd_fn_t        callback;
        dbll_head_t     *arguments;
        const char      *shortdesc;
} command_t;

^L

/*
 * Array of defined commands by the libcommand facilities.
 * These define all of the commands which the user may
 * select to execute.  It must have a nullified command_t as
 * the last member.
 */
command_t *commands;

^L

/*
 * This is the main entrance point for the command library.
 * It loops forever getting input from the user, matching
 * the input against the list of commands, and invoking the
 * callbacks associated therewith.  The user is prompted
 * with `prompt' for the input, and readline(3) is used to
 * gather it.  The `maxchars' variable represents the
 * maximum length of the command and is actually used by
 * readline(3).  If it is `0' then there is no limit.  Using
 * a value of `1' is a good way to use single-char inputs
 * without requiring the user to `enter' after the command
 * line is input.
 */
extern void command_loop (const char *prompt, int maxchars);

/*
 * Returns a string with all the commands available,
 * followed by their short descriptions, one set per line.
 * Intended for use in help texts.
 */
extern char *command_availables (void);

#endif