Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/port/parse.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname port/parse.c 1999/0316
## diff -e /dev/null /n/emeliedump/1999/0316/sys/src/brazil/port/parse.c
0a
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

int
parsefields(char *lp, char **fields, int n, char *sep)
{
	int i;

	for(i=0; lp && *lp && i<n; i++){
		while(*lp && strchr(sep, *lp) != 0)
			*lp++=0;
		if(*lp == 0)
			break;
		fields[i]=lp;
		while(*lp && strchr(sep, *lp) == 0)
			lp++;
	}
	return i;
}

/*
 *  parse a command written to a device
 */
Cmdbuf*
parsecmd(char *p, int n)
{
	Cmdbuf *cb;

	cb = smalloc(sizeof(*cb));
	
	if(n > sizeof(cb->buf)-1)
		n = sizeof(cb->buf)-1;
	memmove(cb->buf, p, n);
	if(n > 0 && cb->buf[n-1] == '\n')
		n--;
	cb->buf[n] = '\0';
	cb->nf = parsefields(cb->buf, cb->f, nelem(cb->f), " ");
	return cb;
}

.
## diffname port/parse.c 2000/0308
## diff -e /n/emeliedump/1999/0316/sys/src/brazil/port/parse.c /n/emeliedump/2000/0308/sys/src/9/port/parse.c
41c
	cb->nf = getfields(cb->buf, cb->f, nelem(cb->f), 1, " ");
.
8,24d
## diffname port/parse.c 2001/0331
## diff -e /n/emeliedump/2000/0308/sys/src/9/port/parse.c /n/emeliedump/2001/0331/sys/src/9/port/parse.c
20a
	poperror();

.
19a

	if(waserror()){
		free(cb);
		nexterror();
	}
.
14c
	Cmdbuf *volatile cb;
.
## diffname port/parse.c 2001/0405
## diff -e /n/emeliedump/2001/0331/sys/src/9/port/parse.c /n/emeliedump/2001/0405/sys/src/9/port/parse.c
26c
	if(up != nil)
		poperror();
.
21c
	if(up != nil && waserror()){
.
16c
	if(up != nil)
		cb = smalloc(sizeof(*cb));
	else{
		cb = malloc(sizeof(*cb));
		if(cb == nil)
			return nil;
	}
.
## diffname port/parse.c 2001/0418
## diff -e /n/emeliedump/2001/0405/sys/src/9/port/parse.c /n/emeliedump/2001/0418/sys/src/9/port/parse.c
38c
	cb->nf = tokenize(cb->buf, cb->f, nelem(cb->f));
.
## diffname port/parse.c 2001/0527
## diff -e /n/emeliedump/2001/0418/sys/src/9/port/parse.c /n/emeliedump/2001/0527/sys/src/9/port/parse.c
38c
	cb->nf = getfields(cb->buf, cb->f, nelem(cb->f), 1, " ");
.
27c
	if(up!=nil && waserror()){
.
16,22c
	cb = smalloc(sizeof(*cb));
.
## diffname port/parse.c 2001/1108
## diff -e /n/emeliedump/2001/0527/sys/src/9/port/parse.c /n/emeliedump/2001/1108/sys/src/9/port/parse.c
32c

	cb->nf = getfields(cb->buf, cb->f, nf, 1, " ");
.
28a
	/* dump new line and null terminate */
.
16,19c
	/* count fields and allocate a big enough cmdbuf */
	for(nf = 1, sp = p; sp != nil && *sp; nf++, sp = strchr(sp+1, ' '))
		;
	sp = smalloc(sizeof(*cb) + n + 1 + nf*sizeof(char*));
	cb = (Cmdbuf*)sp;
	cb->buf = sp+sizeof(*cb);
	cb->f = (char**)(cb->buf + n + 1);
.
14a
	int nf;
	char *sp;
.
## diffname port/parse.c 2001/1117
## diff -e /n/emeliedump/2001/1108/sys/src/9/port/parse.c /n/emeliedump/2001/1117/sys/src/9/port/parse.c
39c
	cb->nf = tokenize(cb->buf, cb->f, nf-1);
	cb->f[cb->nf] = nil;

.
23,24c
	cb->f = (char**)(&cb[1]);
	cb->buf = (char*)(&cb->f[nf]);
.
18,21c
	nf = ncmdfield(p, n);

	/* allocate Cmdbuf plus string pointers plus copy of string including \0 */
	sp = smalloc(sizeof(*cb) + nf * sizeof(char*) + n + 1);
.
8a
 * Generous estimate of number of fields, including terminal nil pointer
 */
static int
ncmdfield(char *p, int n)
{
	int white, nwhite;
	char *ep;
	int nf;

	if(p == nil)
		return 1;

	nf = 0;
	ep = p+n;
	white = 1;	/* first text will start field */
	while(p < ep){
		nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0);	/* UTF is irrelevant */
		if(white && !nwhite)	/* beginning of field */
			nf++;
		white = nwhite;
	}
	return nf+1;	/* +1 for nil */
}

/*
.
## diffname port/parse.c 2001/1118
## diff -e /n/emeliedump/2001/1117/sys/src/9/port/parse.c /n/emeliedump/2001/1118/sys/src/9/port/parse.c
69a
/*
 * Look up entry in table
 */
Cmdtab*
lookupcmd(Cmdbuf *cb, Cmdtab *ct, int nct)
{
	int i;

	if(cb->nf == 0)
		error(Ebadctl);

	for(i=0; i<nct; i++, ct++){
		if(strcmp(cb->f[0], ct->cmd) != 0)
			continue;
		if(ct->narg!=0 && ct->narg!=cb->nf)
			error(Eargctl);
		return ct;
	}
	error(Ebadctl);
	return nil;
}
.
## diffname port/parse.c 2001/1120
## diff -e /n/emeliedump/2001/1118/sys/src/9/port/parse.c /n/emeliedump/2001/1120/sys/src/9/port/parse.c
88c

	cmderror(cb, "unknown control message");
.
84,85c
		if(ct->narg != 0 && ct->narg != cb->nf)
			cmderror(cb, Ecmdargs);
.
81,82c
	for(ct = ctab, i=0; i<nctab; i++, ct++){
		if(strcmp(ct->cmd, "*") !=0)	/* wildcard always matches */
		if(strcmp(ct->cmd, cb->f[0]) != 0)
.
79c
		error("empty control message");
.
76a
	Cmdtab *ct;
.
74c
lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
.
70a
 * Reconstruct original message, for error diagnostic
 */
void
cmderror(Cmdbuf *cb, char *s)
{
	int i;
	char *p, *e;

	p = up->genbuf;
	e = p+ERRMAX-10;
	p = seprint(p, e, "%s \"", s);
	for(i=0; i<cb->nf; i++){
		if(i > 0)
			p = seprint(p, e, " ");
		p = seprint(p, e, "%q", cb->f[i]);
	}
	strcpy(p, "\"");
	error(up->genbuf);
}

/*
.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].