FreeBSD/contrib/ntp/ntpq/ntpq.c
/*
 * nextvar - find the next variable in the buffer
 */
find the next variable in the buffer
find the next variable in the buffer
int
nextvar(
	size_t *datalen,
	const char **datap,
	char **vname,
	char **vvalue
	)
{
	const char *cp;
	const char *np;
	const char *cpend;
	size_t srclen;
	size_t len;
	static char name[MAXVARLEN];
	static char value[MAXVALLEN];
	cp = *datap;
	cpend = cp + *datalen;
	while (cp < cpend && (*cp == ',' || isspace((int)*cp)))
		cp++;
	if (cp >= cpend)
		return 0;
	srclen = strcspn(cp, ",=\r\n");
	srclen = min(srclen, (size_t)(cpend - cp));
	len = srclen;
	while (len > 0 && isspace((unsigned char)cp[len - 1]))
		len--;
	if (len >= sizeof(name))
	    return 0;
	if (len > 0)
		memcpy(name, cp, len);
	name[len] = '\0';
	*vname = name;
	cp += srclen;
	if (cp >= cpend || *cp == ',' || *cp == '\r' || *cp == '\n') {
		if (cp < cpend)
			cp++;
		*datap = cp;
		*datalen = size2int_sat(cpend - cp);
		*vvalue = NULL;
		return 1;
	}
	cp++;	
	while (cp < cpend && (isspace((unsigned char)*cp) && *cp != '\r' && *cp != '\n'))
		cp++;
	np = cp;
	if ('"' == *np) {
		do {
			np++;
		} while (np < cpend && '"' != *np);
		if (np < cpend && '"' == *np)
			np++;
	} else {
		while (np < cpend && ',' != *np && '\r' != *np)
			np++;
	}
	len = np - cp;
	if (np > cpend || len >= sizeof(value) ||
	    (np < cpend && ',' != *np && '\r' != *np))
		return 0;
	memcpy(value, cp, len);
	while (len > 0 && isspace((unsigned char)value[len - 1]))
		len--;
	value[len] = '\0';
	if (np < cpend && ',' == *np)
		np++;
	*datap = np;
	*datalen = size2int_sat(cpend - np);
	*vvalue = value;
	return 1;
}
int nextvar ( size_t * datalen , const char * * datap , char * * vname , char * * vvalue ) { const char * cp ; const char * np ; const char * cpend ; size_t srclen ; size_t len ; static char name [ MAXVARLEN ] ; static char value [ MAXVALLEN ] ; cp = * datap ; cpend = cp + * datalen ; while ( cp < cpend && ( * cp == ' ' || isspace ( ( int ) * cp ) ) ) cp ++ ; if ( cp >= cpend ) return 0 ; srclen = strcspn ( cp , " \r \n " ) ; srclen = min ( srclen , ( size_t ) ( cpend - cp ) ) ; len = srclen ; while ( len > 0 && isspace ( ( unsigned char ) cp [ len - 1 ] ) ) len -- ; if ( len >= sizeof ( name ) ) return 0 ; if ( len > 0 ) memcpy ( name , cp , len ) ; name [ len ] = ' \0 ' ; * vname = name ; cp += srclen ; if ( cp >= cpend || * cp == ' ' || * cp == ' \r ' || * cp == ' \n ' ) { if ( cp < cpend ) cp ++ ; * datap = cp ; * datalen = size2int_sat ( cpend - cp ) ; * vvalue = NULL ; return 1 ; } cp ++ ; while ( cp < cpend && ( isspace ( ( unsigned char ) * cp ) && * cp != ' \r ' && * cp != ' \n ' ) ) cp ++ ; np = cp ; if ( ' ' == * np ) { do { np ++ ; } while ( np < cpend && ' ' != * np ) ; if ( np < cpend && ' ' == * np ) np ++ ; } else { while ( np < cpend && ' ' != * np && ' \r ' != * np ) np ++ ; } len = np - cp ; if ( np > cpend || len >= sizeof ( value ) || ( np < cpend && ' ' != * np && ' \r ' != * np ) ) return 0 ; memcpy ( value , cp , len ) ; while ( len > 0 && isspace ( ( unsigned char ) value [ len - 1 ] ) ) len -- ; value [ len ] = ' \0 ' ; if ( np < cpend && ' ' == * np ) np ++ ; * datap = np ; * datalen = size2int_sat ( cpend - np ) ; * vvalue = value ; return 1 ; }
nextvar - find the next variable in the buffer
nextvar - find the next variable in the buffer
/*
	 * Space past commas and white space
	 */ /*
	 * Copy name until we hit a ',', an '=', a '\r' or a '\n'.  Backspace
	 * over any white space and terminate it.
	 */ /*
	 * Check if we hit the end of the buffer or a ','.  If so we are done.
	 */ /*
	 * So far, so good.  Copy out the value
	 */ /* past '=' */ /*
	 * Trim off any trailing whitespace
	 */ /*
	 * Return this.  All done.
	 */