futility(1) An idiot admires complexity


     PARSECMD(9)                                           PARSECMD(9)

     NAME
          parsecmd, cmderror, lookupcmd - parse device commands

     SYNOPSIS
          Cmdbuf* parsecmd(char *a, int n)

          void    cmderror(Cmdbuf *cb, char *s)

          Cmdtab* lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)

     DESCRIPTION
          Parsecmd is an interface to tokenize (see getfields(2)),
          that safely parses a command, with blank-separated fields,
          as might be written to a device's ctl file.  The buffer a
          and count n can be those passed to the driver's write func-
          tion.  Parsecmd converts the byte array (which might not be
          null-terminated) to a null-terminated string, trimming any
          trailing new line, before invoking tokenize to break the
          string into arguments, interpreting blank and tab as field
          separators when they are not quoted (in the style of rc(1)).
          It returns a pointer to a dynamically-allocated Cmdbuf
          structure, which holds a copy of the string and the result-
          ing fields; it is defined as follows:

               typedef
               struct Cmdbuf
               {
                     char  *buf;
                     char  **f;
                     int   nf;
               } Cmdbuf;

          The array f holds the field pointers; nf gives the number of
          fields.  Cmdbuf is allocated by smalloc (see malloc(9)), and
          the caller is responsible for freeing it using free.
          Cmderror prepends the given format with the original com-
          mand, then calls error(9).

          Command strings may be turned into a (typically enumerated)
          integer with lookupcmd. The catchall `*' matches any text.
          Unrecognized commands, or commands given an unacceptable
          number of arguments generate a call to error. The definition
          is as follows

               typedef
               struct Cmdtab
               {
                     int   index;  /* used by client to switch on result */
                     char  *cmd;   /* command name */
                     int   narg;   /* expected #args; 0 ==> variadic */

     PARSECMD(9)                                           PARSECMD(9)

               } Cmdtab;

          The integer index is the number returned on command match.
          The string cmd is the command name, and narg is 0 (indicat-
          ing a variadic function) or the number of arguments.

     SOURCE
          /sys/src/9/port/parse.c