/*
 * ctlwords.c
 * From: Luigi Rizzo <luigi@labinfo.iet.unipi.it>
 */

#if 0
 used to extract and create tokens for mgp
 Reads globals.c, produces ctlwords.h.
 Lines started by  /*CTL*/ and containing an identifier CTL_*
 generate tokens with increasing value.
 The perl code follows:

BEGIN {
	counter = 0;	# 0 origin
	printf("/* generated by ctlwords.awk. do not edit by hand. */\n");
}
/^\/\*CTL\*\// {
	word = substr($0, index($0, "CTL_"));
	word = substr(word, 0, index(word, ",") - 1);
	printf("#define\t%s\t%d\n", word, counter++);
}

#endif

#include <stdio.h>
#include <string.h>

main(int argc, char *argv[])
{
	char buf[1024];
	char *p, *q;
	int i = 0, l ;

	printf("/* Generated by ctlwords.c from globals.c. Do not edit. */\n");
	while (fgets(buf, sizeof(buf)-1, stdin)) {
		if (strlen(buf) > 12 /* there must be enough data... */
		 && !strncmp(buf, "/*CTL*/", 7)) { /* found marker */
			p = strstr(buf+7, "CTL_");
			if (p) {
				for (q = p; *q && (isalnum(*q) || *q=='_'); q++)
					;
				*q = '\0'; /* must exist */
				printf("#define %s\t%d\n", p, i++);
			}
		}
	}
	exit(0);
}
