PARSEFIELDS(10.2)
NAME
parsefields, parsecmd − parse strings and device commands
SYNOPSIS
intparsefields(char ∗s, char ∗∗fields, int n, char ∗sep);
Cmdbuf∗parsecmd(char ∗a, int n);
DESCRIPTION
Parsefields breaks the null-terminated string s into at most n null-terminated strings and places pointers to the start of those strings in the array fields. It returns the number of fields. A field is a maximal sequence of non-separator characters. The field separators are listed in sep. Parsefields overwrites the separator characters in the input string s.
Parsecmd is an interface to parsefields 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 function. Parsecmd converts the byte array (which might not be null-terminated) to a null-terminated string, trimming any trailing new line, before invoking parsefields with space as the only field separator. It returns a pointer to a dynamically-allocated Cmdbuf structure, which holds a copy of the string as modified by parsefields, and the resulting fields; it is defined as follows:
typedef
struct Cmdbuf
{
charbuf[128];
char∗f[16];
intnf;
} Cmdbuf;
The array f holds the field pointers; nf gives the number of fields. Cmdbuf is allocated by smalloc (see malloc(10.2)), and the caller is responsible for freeing it using free.
SOURCE
/os/port/utils.c
/emu/dev.c
Plan 9 — April 13, 2000