120 lines · 2.1 KB · Text
RawDownload
.TH 9PCMDBUF 2.SH NAMECmdbuf, parsecmd, respondcmderror, lookupcmd \- control message parsing.SH SYNOPSIS.ft L.nf#include <u.h>#include <libc.h>#include <fcall.h>#include <thread.h>#include <9p.h>.fi.PP.ft L.nf.ta \w'\fL1234'u +\w'\fL12345678'utypedef 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, ...).fi.SH DESCRIPTIONThese data structures and functions provide parsing of textual control messages..PP.I Parsecmdtreats the.I nbytes at.I p(which need not be NUL-terminated) as a UTF string and splits itusing.I tokenize(see.IR getfields (2)).It returns a.B Cmdbufstructure holding pointers to each field in the message.It is the caller's responsibility tofree this structure when it is no longer needed..PP.I Lookupcmdwalks through the array.IR ctab ,which has.I ntabentries,looking for the first.B Cmdtabthat matches the parsed command.(If the parsed command is empty,.I lookupcmdreturns nil immediately.)A.B Cmdtabmatches the command if.I cmdis equal to.IB cb -> f [0]or if.I cmdis .LR * .Once a matching.B Cmdtabhas been found, if.I nargis not zero, then the parsed commandmust have exactly.I nargfields (including the command string itself).If the command has the wrong number of arguments,.I lookupcmdreturns nil.Otherwise, it returns a pointer to the.B Cmdtabentry.If.I lookupcmddoes not find a matching command at all,it returns nil.Whenever.I lookupcmdreturns nil, it sets the system error string..PP.I Respondcmderrorresoponds to request.I rwith an error of the form`\fIfmt\fB:\fI cmd\fR,'where.I fmtis the formatted string and.I cmdis a reconstruction of the parsed command.Fmtis often simply.B "%r" ..SH EXAMPLESThis interface is not used in any distributed 9P servers.It was lifted from the Plan 9 kernel.Almost any kernel driver.RB ( /sys/src/9/*/dev*.c )is a good example..SH SOURCE.B /sys/src/lib9p/parse.c.SH SEE ALSO.IR 9p (2)