/*
 * euses is the first working incarnation of something
 * I called `guses' (for Gentoo uses, see
 * <http://www.xs4all.nl/~rooversj/gentoo/guses>) which
 * is a bash script I wrote that searches crudely for USE
 * flags in use.desc and use.local.desc and returns the
 * matching lines with some fancy colouring.
 * I originally intended that to be the ultimate, but I
 * opted to learn a bit of C instead and have a much
 * faster working program. Not that speed matters much in
 * this case...
 *
 * It should compile on any basic Linux system.
 *
 * Thanks to KillerFox and #gentoo-hppa for their programming clues. :)
 * 
 * This program was written by JeR in 2005, released first on May 9,
 * 2005 and is distributed under the GNU General Public License
 * version 2.
 *
 * Mail your suggestions, improvements, complaints and questions to
 * jer at xs4all dot nl
 */

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

#define MAX_LINE_LENGTH 1024

/*Color definitions: */
#define BG "\033[01;32m"	/* Bright Green */
#define FC "\033[00m"		/* Normal       */
#define BW "\033[01;29m"	/* Bright White */
#define NC "\033[00;36m"	/* Normal Cyan  */
#define BB "\033[01;34m"	/* Bright Blue  */

void usage()
{
	(void)puts("Usage:  euses <string>");
	(void)puts("euses displays USE flag descriptions in use.[local.]desc");
	(void)printf("euses %s    Copyright JeR 2005\n", VERSION);
	exit(EXIT_SUCCESS);
}

void err_no(int err_no)
{
	switch (err_no) {
	case 0:
		puts("/etc/make.conf not found\n");
		break;
	case 1:
		puts("use.desc not found\n");
		break;
	case 2:
		puts("use.local.desc not found\n");
		break;
	default:
		puts("Do you Gentoo?\n");
		break;
	}
	exit(EXIT_FAILURE);
}

void ud_format(char *use_line)
{
	int i;
	size_t len;
	len = strlen(use_line);
	(void)printf("%s", BG);
	for (i = 0; i < (int)len; ++i) {
		if (use_line[i] == ' ') {
			(void)printf("%s", FC);
		}
		(void)printf("%c", use_line[i]);
	}
	return;
}

void uld_format(char *use_line)
{
	int i;
	size_t len;
	int past_colon = 0;
	len = strlen(use_line);
	(void)printf("%s", NC);
	for (i = 0; i < (int)len; ++i) {
		if (&use_line[i] >= strstr(use_line, ":")) {
			switch (use_line[i]) {
			case ':':{
					if (past_colon == 0)
						(void)printf("%s%c%s", FC,
							     use_line[i], BG);
				}
				past_colon = 1;
				break;
			case ' ':
				(void)printf("%s%c", FC, use_line[i]);
				break;
			default:
				(void)printf("%c", use_line[i]);
				break;
			}
		} else
			printf("%c", use_line[i]);

	}
	return;
}

void getportdir(char *portdir)
{
	char etc_line[MAX_LINE_LENGTH * 2];
	FILE *ptr = NULL;
	const char *portdir_key = "PORTDIR=\"";
	const char *def_portdir = "/usr/portage";
	int i = 9;
	int j = 0;

	ptr = fopen("/etc/make.conf", "r");
	if (ptr == NULL) {
		err_no(0);
		exit(EXIT_FAILURE);
	}
/* This is turning out to be incredibly difficult: 
 * Difficult cases to parse are:
 * 
 * PORTDIR="/usr"; PORTDIR="${PORTDIR}/portage"
 * PORT\
 * and
 * DIR="/usr/portage"
 * to name but two. */

	while (feof(ptr) == 0) {
		while (fgets(etc_line, MAX_LINE_LENGTH, ptr) != NULL) {
			if (strstr(etc_line, portdir_key) != 0) {
				while (etc_line[i] != '\0') {
					if (etc_line[i] != '"')
						portdir[j++] = etc_line[i++];
					else {
						++i;
						++j;
					}
				}
				portdir[j] = '\0';
			}
		}
		continue;
	}
	(void)fclose(ptr);
	ptr = NULL;
	if (strlen(portdir) == 0)
		strncpy(portdir, def_portdir, strlen(def_portdir));
}

void ud_flag_fest(char *use_line, char *flag)
{
	int i = 0;
	int j = 0;

	while (use_line[i] != '\0') {
		while (use_line[i] != ' ') {
			flag[j++] = use_line[i++];
			continue;
		}
		break;
	}
}

void uld_flag_fest(char *use_line, char *flag)
{
	int i = 0;
	int j = 0;
	int chr_copy = 0;

	/* Remove the package name at the beginning */
	while (use_line[i] != '\0') {
		while (use_line[i] != ' ') {
			if (use_line[i] == ':') {
				chr_copy = 1;
				++i;
			} else {
			}
			if (chr_copy == 1)
				flag[j++] = use_line[i];
			++i;
			continue;
		}
		break;
	}
}

int print_ud(int ud_match, int colour, char *string, char *use_line)
{
	if (colour == 1) {
		if (ud_match == 0) {
			printf(" %s*%s Found '%s%s%s' in %suse.desc%s:\n",
			       BG, FC, BW, string, FC, BB, FC);
			ud_match = 1;
		}
		(void)ud_format(use_line);
	} else
		printf("      use.desc: %s", use_line);
	return ud_match;
}

int match_ud(char *string,
	     char *use_line, char *ud_path, int ud_match, int colour)
{
	FILE *ptr = NULL;
	char ud_flag[MAX_LINE_LENGTH];	/* The UD flag from use_line */
	char *match_chr;	/* Pointer to first matching character */

	ptr = fopen(ud_path, "r");
	if (ptr == 0) {
		err_no(1);
		exit(EXIT_FAILURE);
	}
	while (feof(ptr) == 0) {
		while (fgets(use_line, MAX_LINE_LENGTH, ptr) != NULL) {
			if (use_line[0] == '#')
				continue;

			(void)ud_flag_fest(use_line, ud_flag);
			match_chr = strstr(ud_flag, string);
			memset(ud_flag, 0, sizeof(ud_flag));

			if (match_chr != 0)
				ud_match = print_ud(ud_match, colour, string,
						    use_line);
		}
	}
	(void)fclose(ptr);
	ptr = NULL;
	return ud_match;
}

int print_uld(int uld_match, int colour, char *string, char *use_line)
{
	if (colour == 1) {
		if (uld_match == 0) {
			printf
			    (" %s*%s Found %s%s%s in %suse.local.desc%s:\n",
			     BG, FC, BW, string, FC, BB, FC);
			uld_match = 1;
		}
		(void)uld_format(use_line);
	} else {
		printf("use.local.desc: %s", use_line);
	}
	return uld_match;
}

int match_uld(char *string, char *use_line, char *uld_path, int uld_match,
	      int colour)
{
	FILE *ptr = NULL;
	char uld_flag[MAX_LINE_LENGTH];	/* The ULD flag from use_line */
	char *match_chr;	/* Pointer to first matching character */

	ptr = fopen(uld_path, "r");
	if (ptr == NULL) {
		err_no(2);
		exit(EXIT_FAILURE);
	}
	while (feof(ptr) == 0) {
		while (fgets(use_line, MAX_LINE_LENGTH, ptr) != NULL) {
			if (use_line[0] == '#')
				continue;

			(void)uld_flag_fest(use_line, uld_flag);

			match_chr = strstr(uld_flag, string);
			memset(uld_flag, 0, sizeof(uld_flag));
			if (match_chr != 0)
				uld_match = print_uld(uld_match, colour, string,
						      use_line);
		}
	}
	(void)fclose(ptr);
	ptr = NULL;
	return uld_match;
}

int main(int argc, char **argv)
{
	int ud_match = 0;	/* True when at least one line in  UD matches */
	int uld_match = 0;	/* True when at least one line in ULD matches */
	char ud_path[MAX_LINE_LENGTH * 2];	/* Path to UD  */
	char uld_path[MAX_LINE_LENGTH * 2];	/* Path to ULD */
	const char *ud_file = "/profiles/use.desc";
	const char *uld_file = "/profiles/use.local.desc";
	int colour = 0;		/* True if tty, otherwise False */
	char use_line[MAX_LINE_LENGTH];	/* A single line from UD or ULD */
	char path[MAX_LINE_LENGTH];	/* Stores a path */
	char *portdir = path;	/* Points to stored path */
	char ud_flag[MAX_LINE_LENGTH];	/* The UD flag from use_line */
	char uld_flag[MAX_LINE_LENGTH];	/* The ULD flag from use_line */

	memset(ud_path, 0, sizeof(ud_path));
	memset(uld_path, 0, sizeof(uld_path));
	memset(ud_flag, 0, sizeof(ud_flag));
	memset(uld_flag, 0, sizeof(uld_flag));

	/* Check in /etc/make.conf where Portage is */
	getportdir(portdir);

	strcat(ud_path, portdir);
	strcat(ud_path, ud_file);
	strcat(uld_path, portdir);
	strcat(uld_path, uld_file);

	if (isatty(STDOUT_FILENO) == 1)
		colour = 1;

	if (argc < 2)
		usage();

	/* Search through use.desc: */
	ud_match = match_ud(argv[1], use_line, ud_path, ud_match, colour);

	/* Search through use.local.desc: */
	uld_match = match_uld(argv[1], use_line, uld_path, uld_match, colour);

	if (ud_match != 0 || uld_match != 0)
		exit(EXIT_SUCCESS);
	else
		exit(EXIT_FAILURE);
}


syntax highlighted by Code2HTML, v. 0.9.1