Table of Contents

Name

command_init, command_register, command_get, command_read, command_close - command parser

Synopsis


#include <command.h>command *command_init(string name);void command_register(command
*c, string cmd, string argtypes, string help);string *command_get(command
*c);void command_close(command *c);

Description

These routines allow you to define a set of commands that can be used to interactively marshall a set of functions with checked and optional arguments. All information is stored in a data structure command (which should remain opaque).

After obtaining this data structure with command_init (the name is merely used to allow multiple commands to live peacefully together when doing disk I/O operations), commands (or actions if you wish) need to be registered with command_register. In addition to the command, the argument types also need to be supplied. Currently only integer, real, string and optional arguments are allowed, designated by the characters "irs." resp. If no arguments are allowed, the argtypes string should be left blank (but not NULL).

The command parser returns a valid argv like array of strings from command_get, but it is currentlyu left to the user to parse these correctly, despite the earlier registration requirements (e.g. they lack a function pointer).

command_close is called to free up all memory associated with the command parser.

Example

A good full example can be found in the TESTBED section of command.c, producing a commandtest executable with which features can be excersized. Here is a brief outline:
  #include <command.h>
  ...
  void do_a(int), do_b(int, double);
  ...
  string *argv;
  ..
  command cmd = command_init(name);                 /* initialize */
  command_register(cmd,"a","i",  "foo bar");        /* register commands
*/
  command_register(cmd,"b","ir", "fum bar");
  command_register(cmd,"quit","","alternate form of quit");
  while((argv=command_get(cmd))) {                  /* loop getting arguments
*/
    na = xstrlen(argv,sizeof(string))-1;
    if (streq(argv[0],"quit")) {
    break;
    } else if (streq(argv[0],"a")) {
      do_a(natoi(argv[1]));
    } else if (streq(argv[0],"b")) {
      do_b(natoi(argv[1]), natof(argv[2]));
    } 
    freestrings(argv);
  }
  command_close(cmd);

Files

NEMO/src/kernel/io/command.c

See Also

getparam(3NEMO) , promptparam(3NEMO)

Caveats

There is no programming language associates with this command parser. The user is responsible to correctly matching commands to actions.

History


24-dec-2003    V1.0 - written for SIRTF’s map2 project        PJT
18-feb-2004    V1.1 - implemented command_read            PJT


Table of Contents