9P-CMDBUF(3)

πŸ“¦ version 0_git20260208, in alpine 3.24, arch rolling, nixos 26.05, nixpkgs unstable

🌸 NAME 🌸

Cmdbuf, parsecmd, respondcmderror, lookupcmd β€” control message parsing, bestie. This is the 9P-CMDBUF(3) page from the Library Functions Manual, and it's literally all about parsing textual control messages. Iconic behavior.

✨ SYNOPSIS ✨

#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

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

typedef struct Cmdtab
{
    int     index;
    char    *cmd;
    int     narg;
};

Cmdbuf      *parsecmd(char *p, int n)
Cmdtab      *lookupcmd(Cmdbuf *cb, Cmdtab *tab, int ntab)
void        respondcmderror(Req *r, Cmdbuf *cb, char *fmt, ...)

πŸ’… DESCRIPTION πŸ’…

These data structures and functions provide parsing of textual control messages. Periodt.

parsecmd treats the n bytes at p (which need not be NUL-terminated) as a UTF string and splits it using tokenize (see getfields(3)). It returns a Cmdbuf structure holding pointers to each field in the message. Slay.

lookupcmd walks through the array ctab, which has ntab entries, looking for the first Cmdtab that matches the parsed command. Tiny thing: the synopsis parameter is named tab, while the description says ctab β€” we contain multitudes. If the parsed command is empty, lookupcmd returns nil immediately.

⚠️ Okay so the original matching sentence is lowkey corrupted. It literally says: β€œA Cmdtab matches the command if cmd is equal to cb->f[0] or if cmd is Once a matching Cmdtab has been found, if narg is not zero, then the parsed command must have exactly narg fields (including the command string itself).” I am not gonna invent the missing words, bestie. Here are the facts that are actually clear:
  • A Cmdtab matches the command if cmd is equal to cb->f[0] β€” that part is clear.
  • Once a matching Cmdtab has been found, if narg is not zero, then the parsed command must have exactly narg fields (including the command string itself).
  • If the command has the wrong number of arguments, lookupcmd returns nil.
  • Otherwise, it returns a pointer to the Cmdtab entry.
  • If lookupcmd does not find a matching command at all, it returns nil.
  • Whenever lookupcmd returns nil, it sets the system error string.

respondcmderror responds β€” the page literally says resoponds, typo and all, we love that β€” to request r with an error of the form fmt: cmd, where fmt is the formatted string and cmd is a reconstruction of the parsed command. fmt is often simply %r.

πŸŽ€ EXAMPLES πŸŽ€

This interface is not used in any distributed 9P servers. Not one. It was lifted from the Plan 9 kernel. Almost any Plan 9 kernel driver (/sys/src/9/*/dev*.c on Plan 9) is a good example. Go look, bestie.

πŸ“ SOURCE πŸ“

/src/lib9p/parse.c β€” that's the source, no cap.

πŸ‘€ SEE ALSO πŸ‘€

9p(3) β€” go read that next, bestie 🫢


And the footer? Just 9P-CMDBUF(3) again, no date, no version, no extra crumbs. That's the whole page, bestie β€” go parse some control messages πŸ’–βœ¨