commands(3)
NAME
commands - read and obey 0erminated commands
SYNTAX
#include <commands.h>
int commands(&ss,c);
substdio ss;
struct commands c[];
DESCRIPTION
commands reads a series of commands from ss and follows
the corresponding instructions listed in c.
- commands returns 0 when it reaches end of file. It
returns -1, setting errno appropriately, if it runs out of
memory or has trouble reading ss. - is removed.
- Each command is terminated by 0 ,atheal final lines are
ignored. If a command ends with - Each command contains a verb, consisting of zero or more
non-space characters, followed optionally by one or more
spaces and an argument. - Recognized verbs are listed in c. c is an array of one or
more struct commands. Each struct has three components: char *verb; void *action(); and void *flush(). - verb points to a -terminated string, interpreted without
regard to case. If the command verb matches this string,
commands calls action(arg), followed by flush() if flush is not the zero pointer. Here arg is a pointer to a - -terminated string containing the argument (with any
- inside the argument replaced by 0, or the empty string
if there is no argument. - Exception: In the last struct in c, verb is the 0 pointer. commands uses this action for all unrecognized verbs.
ENGINEERING NOTES
The format accepted by commands has been copied from one
Internet protocol to another. It is used in SMTP, FTP,
POP, etc.
This does not mean it is a good format. It violates basic
engineering principles. It has produced a continuing
string of interoperability problems.
See http://pobox.com/~djb/proto/design.html for further
comments.
EXAMPLE
- If c is initialized to
- { { "help", usage, empty }
, { "do", doit, 0 }
, { 0, syntax, empty }
} - and commands receives the input
Do this
undo that
HELP- then commands will call doit("this"), syntax("that"), empty(), usage(""), and empty().