Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/refer/getq.c

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


# include "refer.h"
# define BSIZ 2000

int getq(char *v[], int nitem)
{
	static char buff[BSIZ];
	static int eof = 0;
	char *p;
	int c, n = 0, las = 0;

	if (eof) 
		return -1;
	p = buff;
	while ( (c = (sinput ? *sinput++ : getchar()) ) > 0) {
		if (c == '\n')
			break;
		if (isalpha(c) || isdigit(c)) {
			if (las == 0) {
				if (n >= nitem)
					err("Cannot handle more than %d query items", nitem);
				v[n++] = p;
				las = 1;
			}
			if (las++ <= 6)
				*p++ = c;
		} else {
			if (las > 0)
				*p++ = 0;
			las = 0;
		}
	}
	*p = 0;
	assert(p < buff + BSIZ);
	if (c <= 0) {
		if (sinput == 0) 
			eof = 1;
		else 
			sinput--;
	}
	n = keycomp(v, n);
# if D1
	fprintf(stderr, "no. keys %d\n", n);
	for (c = 0; c < n; c++)
		fprintf(stderr, "keys X%sX\n", v[c]);
# endif
	return n;
}

int keycomp(char *v[], int n) /* compress keys */
{
	int i, j, k;

	for (i = j = 0; i < n; i++) {
		for (k = 0; k < j; k++)
			if (strcmp(v[i], v[k]) == 0)
				break;
		if (k < j) /* found it */
			continue;
		v[j++] = v[i];
	}
	return j;
}


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].