Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

cmdparam.c File Reference


Detailed Description

Parameters, commands, and counter hooks.

Header
/u/nn/cvsroot/lissom/src/cmdparam.c,v 1.167 2003/01/15 08:07:46 jbednar Exp

This file provides general-purpose handling for COMMANDS and COUNTER HOOKS, based on external support for PARAMETERS.

PARAMETERS are global or static variables stored in a container called a blackboard. The blackboard provides a standard interface for defining parameters and setting/viewing their values via interactive displays, command-line arguments, and command prompts. Upper and/or lower bounds can be provided for each parameter to provide automatic error checking.

COMMANDS are a standardized method for executing operations requiring a set of arguments. The commands can come from a string (e.g. read at a command prompt, or from a text entry widget) or from a text file, called a command file. A common command "set" is provided to give a value to the specified parameter(s). Commands may specify a minimum number of arguments required, and may accept a variable number of arguments via an argc,argv mechanism like for shell commands.

COUNTER HOOKS are a way to delay execution of a command until a specified value is reached for a user-specified counter. Any number of separate hooklists can be defined using the command "hook_list", so a program can have different hooks for different stages in processing for a given counter value. Hooks themselves can be defined using the command "hook" to declare the hooklist to which it applies, the counter range and step to which it applies, and the command to be executed. If the counter value has gaps (e.g. if some saved state was restored, and the counter with it), only commands specifically declared to execute during the catchup period are called when the next valid counter value is found.

The code is loosely object-oriented around the following objects:

    Fn. prefix  Struct name                    Description
    ----------  -----------------------------  -------------------------

    cmddef_     CommandDefinition              a command definition
    cmddefs_    CommandDefinitionBlackboard    container for cmddefs

    command_    Command                        a command instance w/args

    hook_       CounterHookDefinition          a counter hook
    hooklist_   CounterHooklist                container for hooks
    hooklists_  CounterHooklists               container for hooklists

    cmdparam_   (just globals right now)       container for all objects
    

Other than the parameter implementation, it's just C code, so these distinctions aren't enforced, but it shouldn't be too difficult to translate the structs and global functions into the corresponding C++ classes and member functions, and that would make the code clearer, safer, shorter, and easier to extend and debug.

Definition in file cmdparam.c.

#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <stack>
#include <vector>
#include <strstream.h>
#include <fstream>
#include <signal.h>
#include <setjmp.h>
#include "ipc.h"
#include "cmdparam.h"
#include "stringutils.h"
#include "polymorph.h"

Include dependency graph for cmdparam.c:

Include dependency graph

Go to the source code of this file.

Compounds

struct  cmdobj_call
struct  cmdobj_clear_hooks
struct  cmdobj_define_param
struct  cmdobj_define_param_set
struct  cmdobj_dotimes
struct  cmdobj_echo
struct  cmdobj_exec
struct  cmdobj_exec_catchup
struct  cmdobj_exec_file
struct  cmdobj_for
struct  cmdobj_hook
struct  cmdobj_if
struct  cmdobj_param_default
struct  cmdobj_print
struct  cmdobj_quit
struct  cmdobj_read_args
struct  cmdobj_select_param_set
struct  cmdobj_set
struct  cmdobj_system
struct  Command
struct  CommandDefinition
struct  CommandDefinitionBlackboard
struct  CounterHookDefinition
struct  CounterHooklist
struct  CounterHooklists
struct  PrereqList

Defines

#define PARENTPE   (0)
#define AMPARENTPE   (ipc_my_process()==PARENTPE)
#define CMDDEF_MAX_NUM   64
 Maximum number of commands definable.

#define CMDDEF_MAX_NAME_LENGTH   64
 Min num of unique chars in command name.

#define CMDDEF_MAX_PREREQS   2
 Maximum number of prerequisite commands.

#define CMD_CALL(cmd)   (*((cmd).def->function))((cmd).argc, (cmd).argv)
#define HOOKLIST_MAX_NUM   128
 Maximum number of hooks per hooklist.

#define HOOKLIST_MAX_NAME_LENGTH   32
#define HOOKLISTS_MAX_NUM   4
#define HAS_CURRENT_HOOK(hooklist)   ((hooklist).current < (hooklist).num)
#define CURRENT_HOOK(hooklist)   ((hooklist).defs[(hooklist).current])
#define nexthooknum   (hooklists.lists[hooklist].num)

Typedefs

typedef void(* signal_handler_type )(int)

Functions

BlackboardType blackboard_for_commands (&blackboard,"cmd")
 Command argument blackboard.

cmdstat cmd_call (int argc, const char *argv[])
cmdstat cmd_clear_hooks (int argc, const char *argv[])
cmdstat cmd_define_param (int argc, const char *argv[])
cmdstat cmd_define_param_set (int argc, const char *argv[])
cmdstat cmd_select_param_set (int argc, const char *argv[])
cmdstat cmd_param_default (int argc, const char *argv[])
cmdstat cmd_dotimes (int argc, const char *argv[])
cmdstat cmd_exec (int argc, const char *argv[])
cmdstat cmd_exec_catchup (int argc, const char *argv[])
cmdstat cmd_exec_file (int argc, const char *argv[])
cmdstat cmd_for (int argc, const char *argv[])
cmdstat cmd_hook (int argc, const char *argv[])
cmdstat cmd_if (int argc, const char *argv[])
cmdstat cmd_echo (int argc, const char *argv[])
cmdstat cmd_print (int argc, const char *argv[])
cmdstat cmd_quit (int argc, const char *argv[])
cmdstat cmd_read_args (int argc, const char *argv[])
 This is a hack, but should be reasonably safe and clear.

cmdstat cmd_set (int argc, const char *argv[])
 Allows control of any parameter in blackboard from command file.

cmdstat cmd_system (int argc, const char *argv[])
size_t cmdparam_identifier_length (const char *str)
 Returns the length of the prefix of the given string which consists entirely of characters allowed in identifiers (usually parameter and command names).

void ipc_init_hook (void)
 These declarations are placed here rather than in ipc.c so that it will be independent of this file.

int cmddef_compare (const void *hook1, const void *hook2)
 Used by cmddefs_sort to compare elements.

void cmddefs_sort (void)
int cmddefs_get_line_from_file (void)
 CmdDefs_LineGenerator for files.

int command_check_usage (Command *command)
cmdstat command_exec (Command *cmd)
 Execute the given command, keeping track of failures and time taken.

CommandDefinition * cmddefs_lookup (const char *name)
 Looks up the given string as a command name, returning a pointer to the command definition or NULL if the command was not found.

int command_parse_line (Command *command, char *string)
const char * command_parse_token (char **remaining)
 Parse a string for a single token, where a token is anything enclosed in single or double quotes or separated by whitespace; anything between '#' and the end of the string is ignored.

void command_print_to_str (const Command *cmd, char *str, size_t nchars)
 Outputs textual representation of command into the given string.

void cmddef_print_usage (FILE *fp, const CommandDefinition *cmddef)
 Print usage string to a file, making it up if necessary.

void cmddef_print_usage_to_str (const CommandDefinition *cmddef, char *str, size_t nchars)
 Print usage info to the given string, making it up if necessary.

void cmddefs_activate_prereqs (void)
void cmddefs_activate_command_prereq (const char *name, const char *prereq)
 Put the prerequisite info for a command into its definition; takes effect immediately.

int hook_compare (const void *hook1, const void *hook2)
 Used by hooklists_sort to compare elements; order is used to make sure sort is stable.

void hook_copy (CounterHookDefinition *source, CounterHookDefinition *dest)
 Make an identical copy of the first hook into the second.

void hooklists_log (void)
 Print a list of the currently-defined counter hooks.

int hook_parse_specifier (CounterHookDefinition *hook, Parse &parser)
 Parse the counter specifier for a hook, returning True if successful.

void hook_print_to_str (CounterHookDefinition *hook, char *hooklistname, char *str, size_t nchars)
 Print a list of the currently-defined counter hooks.

void hooklists_sort (HooklistNum hooklist)
 Sort the list of hooks in ascending order of start counter, starting at the current hook so that place is not lost.

void command_exec_sigint_handler (int sig)
void cmdparam_init_hook (void)
 This routine must be called before using parameters or commands, and no commands should be defined after it is called.

void cmddefs_define_command (const char *name, CmdWrapper *function, int minargs, int exec_for_catchups)
 Add command to list of known commands.

void cmddefs_define_command_doc (const char *name, const char *usage, const char *doc)
 Define help string for command.

void cmddefs_define_command_prereq (const char *name, const char *prereq)
 Put a prerequisite for a command into a temporary holding area while the other commands are defined; doesn't take effect until activated later.

void cmddefs_declare_prereq_status (const char *name, cmdstat status)
 Ordinary prerequisites are handled automatically, but if a command makes mutually recursive calls to other commands which require it as a prerequisite, the command may call this routine beforehand to declare that its useful work is done, and thus that the prerequisite is satisfied.

cmdstat cmddefs_exec_by_name (const char *name, int argc, const char *argv[])
char * consume_quote (char **remaining)
bool isquote (char character)
int command_parse_line (Command *command, char *const cmdtxt)
 Parses the given string for a command and fills the given command instance structure if one was found.

cmdstat cmddefs_exec_str (const char *cmdtxt)
 Parse and execute the command specified in the given string.

int cmddefs_exec_batch (CmdDefs_LineGenerator fn, const char *description)
 Reads commands iteratively and executes them in sequence.

int cmddefs_exec_file (const char *filename)
 Reads commands from a command file and executes them.

int cmddefs_num_total (void)
 Returns current size of command blackboard.

int my_strncasecmp (const char *str1, const char *str2, size_t n)
 Not all platforms have strncasecmp, so this version is used instead.

void cmddefs_print_all (FILE *fp)
 Print summary of command definitions to a file.

void cmddefs_print_doc_for_index (FILE *fp, int idx)
void cmddefs_print_doc (FILE *fp, const char *name)
 Print documentation string for a command to a file.

void cmddefs_print_all_doc (FILE *file)
char * cmddefs_completion_generator (char *text, int state)
 For use with functions like readline(); returns a dynamically-allocated string with the next possible completion for the given partial word.

int hooklists_define_list (const char *listname, const char *countername)
 Defines a new hooklist and returns its number.

void hooklists_run_list (HooklistNum hooklist, int counter, int catchuponly)
 Run the counter hooks in the given list which specify this counter value.

void hooklists_reset_list (HooklistNum hooklist)
 Reset current hook of the given list to the beginning.

void hooklists_empty_list (HooklistNum hooklistnum)
 Remove all the hooks defined in the given list.

char * cmdparam_dupstr (const char *str)
 Allocate a copy of a string; must be freed when done using it.

int cmdparams_print_doc (FILE *fp, const char *name)
 Print documentation string for any known object to a file.

char * params_completion_generator (char *text, int state)
 Wrapper for the blackboard's completion generator.

char * cmdparams_completion_generator (char *text, int state)
 Returns possible completion for command (if any) or parameter (if any).

int cmdparam_save_current_state (const char *filename)
string trim_quotes (const string &s)
 Returns a copy of the given string with enclosing single or double quotes removed.

string filter_backquotes (const string arg)
bool cmdparams_set_single (BlackboardType &params, const string &arg, bool &seterror, bool mark, bool verbose, bool copysetfn)
 Implementation of cmd_set for one argument.

cmdstat cmdparams_set (BlackboardType &params, StringArgs &arglist, bool mark, bool warn, bool verbose, bool copysetfns)

Variables

CounterHooklists hooklists
BlackboardType global_blackboard
 Top-level parameter def blackboard.

BlackboardTypeblackboard_ptr = &global_blackboard
 Pointer to current blackboard.

CommandDefinitionBlackboard command_definitions
 Command def blackboard.

int commands_succeeded = 0
int commands_failed = 0
int command_num_called = 0
int hook_definition_counter = 0
int cmdparam_warn_unknown = true
double command_msg_time = 1.0
FILE * cmddefs_file = NULL
string commandpath = "../command/ ../../command/ ../samples/ ../ /usr/local/lissom/command/"
std::stack< StringParser::arglistcmddefs_exec_file_arglists
 Stack of arguments for cmd_read_args.

char cmddefs_line_buffer [CMD_MAX_LINE_LENGTH+1+sizeof(int)]
PrereqList prereq_storage
sigjmp_buf jmp_env


Function Documentation

char* cmddefs_completion_generator char *    text,
int    state
 

For use with functions like readline(); returns a dynamically-allocated string with the next possible completion for the given partial word.

Free should be called on the string when done with it.

Definition at line 1454 of file cmdparam.c.

References cmdparam_dupstr(), and command_definitions.

Referenced by cmdparams_completion_generator().

void cmddefs_declare_prereq_status const char *    name,
cmdstat    status
 

Ordinary prerequisites are handled automatically, but if a command makes mutually recursive calls to other commands which require it as a prerequisite, the command may call this routine beforehand to declare that its useful work is done, and thus that the prerequisite is satisfied.

This prevents a vicious circle of attempted prerequisite satisfaction.

Definition at line 724 of file cmdparam.c.

References cmddefs_lookup().

Referenced by Eyes::uninit().

int cmddefs_exec_batch CmdDefs_LineGenerator    fn,
const char *    description
 

Reads commands iteratively and executes them in sequence.

The command lines come from cmddefs_line_buffer, which the given function should place a string into once it is called with no arguments on a single PE. The fn should return True ("done") when there is no more input available.

Each line may be up to CMD_MAX_LINE_LENGTH total, and may contain up to one command with up to CMD_MAX_ARGUMENTS arguments, up to CMD_MAX_LINE_LENGTH total.

The description should be something like "file filename..." or "prompt".

Definition at line 1133 of file cmdparam.c.

References CMD_EMPTY, CMD_MAX_LINE_LENGTH, cmddefs_exec_str(), and ipc_barrier().

Referenced by cmddefs_exec_file(), and main().

int cmddefs_get_line_from_file void   
 

CmdDefs_LineGenerator for files.

Concatenates as long as it keeps finding lines ending in a backslash

Definition at line 1213 of file cmdparam.c.

size_t cmdparam_identifier_length const char *    str
 

Returns the length of the prefix of the given string which consists entirely of characters allowed in identifiers (usually parameter and command names).

Uses C-language indentifier rules.

Definition at line 1896 of file cmdparam.c.

Referenced by cmddefs_define_command().

int cmdparams_print_doc FILE *    fp,
const char *    name
 

Print documentation string for any known object to a file.

Returns True if object was found.

Definition at line 1925 of file cmdparam.c.

References command_definitions, ParameterDefinition< T >::doc_string(), and ParameterDefinition< T >::is_valid().

bool cmdparams_set_single BlackboardType   params,
const string &    arg,
bool &    seterror,
bool    mark,
bool    verbose,
bool    copysetfn
 

Implementation of cmd_set for one argument.

Returns true if the input had the proper form, regardless of the result. On exit, 'seterror' is set to false if the set failed, and is otherwise not changed.

Definition at line 2060 of file cmdparam.c.

References ParameterMap< ParamType >::set_matching(), and trim_quotes().

Referenced by command_parse_line().

int command_parse_line Command *    command,
char *const    cmdtxt
 

Parses the given string for a command and fills the given command instance structure if one was found.

The given string is overwritten, as with strtok.

Any arguments past CMD_MAX_ARGUMENTS are ignored (but warned about).

Returns number of commands successfully processed (0 or 1).

Definition at line 922 of file cmdparam.c.

References CMD_MAX_ARGUMENTS, cmddefs_lookup(), cmdparams_set_single(), and command_parse_token().

Referenced by cmddefs_exec_str().

const char * command_parse_token char **    remaining
 

Parse a string for a single token, where a token is anything enclosed in single or double quotes or separated by whitespace; anything between '#' and the end of the string is ignored.

The "remaining" pointer is modified to point to the unparsed portion remaining after this token.

The string pointed to by remaining is also modified by inserting a null at the end of the token (as in strtok). This solves various memory lifetime problems that would otherwise require using a full C++ class for the command arguments.

Definition at line 859 of file cmdparam.c.

Referenced by command_parse_line().

void command_print_to_str const Command *    cmd,
char *    str,
size_t    nchars
 

Outputs textual representation of command into the given string.

Useful for printing.

Definition at line 1295 of file cmdparam.c.

Referenced by command_exec(), and hook_print_to_str().

void hook_print_to_str CounterHookDefinition *    hook,
char *    hooklistname,
char *    str,
size_t    nchars
 

Print a list of the currently-defined counter hooks.

The format is currently suitable for cmddefs_exec_str() for convenience.

Definition at line 1837 of file cmdparam.c.

References command_print_to_str().

Referenced by hooklists_log().

int hooklists_define_list const char *    listname,
const char *    countername
 

Defines a new hooklist and returns its number.

   Example C usage: 

   (at start of program) 
     HooklistNum before_input;
     before_input = hooklists_define_list("before_input");

   (at location where hooks should execute)
     hooklists_run_list(before_input,t,False);
     
   Now the command
     hook before_input        5 set exc_rad 1

   will cause parameter exc_rad to be changed to 1 whenever
   hooklists_run_list is called with t=5.
   

Returns the number assigned to this list.

Definition at line 1506 of file cmdparam.c.

void hooklists_log void   
 

Print a list of the currently-defined counter hooks.

The format is currently suitable for cmddefs_exec_str() for convenience.

Definition at line 1870 of file cmdparam.c.

References CMD_MAX_LINE_LENGTH, hook_print_to_str(), and ipc_log().

void hooklists_run_list HooklistNum    hooklist,
int    counter,
int    catchuponly
 

Run the counter hooks in the given list which specify this counter value.

Earlier values not yet run are called only if their definition specifies that.

This routine should be called at most once for each counter value, from the location where you want hooks from the given list to be called.

Definition at line 1739 of file cmdparam.c.

References command_exec().

char* params_completion_generator char *    text,
int    state
 

Wrapper for the blackboard's completion generator.

Only completes from the global blackboard, not any sub-boards defined elsewhere.

Definition at line 1948 of file cmdparam.c.


Generated on Mon Jan 20 02:35:51 2003 for RF-LISSOM by doxygen1.3-rc2