Plan 9 from Bell Labs’s /usr/web/sources/contrib/anothy/src/cmd/ident.c

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


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <regexp.h>

/*
 * A stand-alone work-alike to the RCS ident program, but matching
 * any $@: text $ pattern, not caring about 'RCS keywords'.
 */

int	search(Reprog*, char*);
int	check(Reprog*, char[]);
char	*delim;

void
usage(void)
{
	fprint(2, "usage: %s file [ files... ]\n", argv0);
	exits("usage");
}

void
main(int argc, char *argv[])
{
	int i;
	delim = "$";
	char *pattern = "\\$([a-zA-Z0-9]+): ([^$]+) \\$";
//	sprint(pattern, "%c[a-zA-Z0-9]+: [^%c]+ %c", delim, delim);
	Reprog *r;

	ARGBEGIN{
		default:
			usage();
	}ARGEND

	/* For now. */
	if (argc == 0)
		usage();

	r = regcomp(pattern);

	if (!argv)
		search(r, 0);
	else
		for (i=0; i< argc; i++) {
			search(r, argv[i]);
		}
	exits(0);
}

int
search(Reprog *r, char *name)
{
	int c, fd, collecting;
	char buf[512];
	Biobuf b;

	if (name == 0) {
		name = "stdin";
		fd = 0;
	} else
		fd = open(name, OREAD);

	if (fd < 0) {
		fprint(2, "%s: can't open %s: %r\n", argv0, name);
		exits("open");
	}
	print("%s:\n", name);

	Binit(&b, fd, OREAD);

	collecting = 0;
	while((c = Bgetc(&b)) != Beof) {
		if(! (collecting < sizeof buf)) {
			memset(buf, 0, sizeof buf);
			collecting = 0;
		}
		if(collecting) {
			if(c == '$') {
				buf[collecting] = c;
				if(!check(r, buf))
					Bungetc(&b);
				memset(buf, 0, sizeof buf);
				collecting = 0;
			} else {
				buf[collecting] = c;
				collecting++;
			}
		} else {
			if(c == '$') {
				buf[collecting] = c;
				collecting++;
			}
		}
	}
	Bterm(&b);
	return 0;
}

int
check(Reprog *r, char buf[])
{
	int len;
	Resub match[3];
#ifdef PLAN9PORT
	match[0].s.sp = match[0].e.ep = 0;
	if(regexec(r, buf, match, 2)) {
		len = match[0].e.ep - match[0].s.sp;
		print("	%*s\n", len, match[0].s.sp);
#else
	match[0].sp = match[0].ep = 0;
	if(regexec(r, buf, match, 2)) {
		len = match[0].ep - match[0].sp;
		print("	%*s\n", len, match[0].sp);
#endif
		return 1;
	}
	return 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 [email protected].