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

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


# include "stdio.h"
# include "ctype.h"
# include "mkey.h"

static int	eof = 0;
static long	lp, lim;
static int	alph, used, prevc;
static char	*p, key[100];
extern int	minlen, keycount, labels, wholefile;
extern char	*iglist;

void dofile(FILE *f, char *name)
{

	/* read file f & spit out keys & ptrs */
	char	line[MAXLINE], *s;
	int	c;

	alph = used = prevc = eof = 0;
	lp = 0;
	if (wholefile == 0) {
		while (lim = grec(line, f)) {
# if D1
			fprintf(stderr, "line: /%s", line);
# endif
			used = alph = 0;
			p = key;
			for (s = line; (c = *s) && (used < keycount); s++)
				chkey(c, name);
			lp += lim;
			if (used) 
				putchar('\n');
		}
	} else {
		p = key;
		used = alph = 0;
		while ((c = getc(f)) != EOF && used < keycount)
			chkey (c, name);
		if (used) 
			putchar('\n');
	}
	fclose(f);
}

int outkey(char *ky, int lead, int trail)
{
	int	n;
	n = strlen(ky);
	if (n < minlen) 
		return 0;
	if (n < 3) {
		if (trail == '.') 
			return 0;
		if (mindex(".%,!#$%&'();+:*", lead) != 0) 
			return 0;
	}
	if (isdigit(ky[0]))
		if (ky[0] != '1' || (ky[1] != '9' && ky[1] != '8') || n != 4) 
			return 0;
	if (common(ky))
		return 0;
	return 1;
}

long grec(char *s, FILE *f)
{
	char	tm[MAXLINE];
	int	curtype = 0;
	long	len = 0L, tlen = 0L;
	extern int	wholefile;
	int	lineno = 0;
	if (eof) 
		return 0;
	*s = 0;
	while (fgets(tm, MAXLINE, f)) {
		int	i, allblank = 1;
		int	tmlen = strlen(tm);
		lineno++;
		if (tmlen == MAXLINE - 1 && tm[MAXLINE - 2] != '\n') {
			fprintf(stderr, "mkey: line %d too long\n", lineno);
			exit(1);
		}
		tlen += tmlen;
		if (tm[0] == '%' || tm[0] == '.')
			curtype = tm[1];
		if (tlen < MAXLINE && mindex(iglist, curtype) == 0)
			strcat(s, tm);
		len = tlen;
		for (i = 0; allblank && tm[i] != '\n'; i++)
			if (!isspace(tm[i]))
				allblank = 0;
		if (wholefile == 0 && allblank && len > 0)
			return len;
		if (wholefile > 0 && len >= MAXLINE) {
			fseek(f, 0L, 2);
			return ftell(f);
		}
	}
	eof = 1;
	return s[0] ? len : 0L;
}

void chkey(int c, char *name)
{
	if ((isalpha(c) || isdigit(c)) && prevc != '\\') {
		if (alph++ < 6)
			*p++ = c;
	} else {
		*p = 0;
		for (p = key; *p; p++)
			if (isupper(*p))
				*p = _tolower(*p);
		if (outkey(p = key, prevc, c)) {
			if (used == 0) {
				if (labels) {
					if (wholefile == 0)
						printf("%s:%ld,%ld\t", name, lp, lim);
					else
						printf("%s\t", name);
				}
			} else
				putchar(' ');
			fputs(key, stdout);
			if (ferror(stdout))
				err("error writing out keys");
			used++;
		}
		prevc = c;
		alph = 0;
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.