/* This code is meant to take a file of groups and there members and
expand it out so that it is a file of groupnames with specific host or host
expressions as members. S. Thorne */

#include <stdio.h>
#include <strings.h>
#define TRUE 1
#define FALSE 0
#define NULL 0

char *infilename, *outfilename;

char * 
get_acllines(char *groupname, char *filename)
{
FILE *file;
char line[512],*return_str = NULL,*end;
int len;

len = strlen(groupname);

infile = fopen(filename,"r");
if (infile)
  {
    while (line = fgets(line,500,infile))
      {
	if (strncmp(line,groupname,len) == 0)
	  {
	    /* found a line for this group... so add it to output */
	    while (!isspace(*cp)) cp++;
	    if (*cp != '\0')
	      {
		*cp = '\0';
		cp++;
	      }
	    if (return_str)
	      return_str = realloc(return_str,strlen(return_str) + 
				   strlen(cp) + 1);
	    else
	      return_str = malloc(return_str,strlen(cp) + 1);
	    end = index(return_str,'\0');
	    bcopy(cp,end,strlen(cp) +1);
	  } 
      }
    fclose(infile);
  }
else
  printf("couldn't open file: %s\n",filename);
return return_str;
}

char *
expand(char *groupname)  /* this is recursive... */
{
char *acl,*end,*return_str = NULL,*tmp_str,token[50];
int negacl = FALSE, group,len;


if (acl = get_acllines(groupname,outfilename))
  {
    printf("we already did this one\n");
    return;
  }
if (acl != get_acllines(groupname,outfilename))
  {
    printf("couldn't find this one\n");
    return;
  }

cp = acl;
while (*cp != '\0') {         /** for each token in acl **/
  while (isspace(*cp)) cp++;  /* skip white space */
  if (*cp == '-')             /* check if neg */
    {                     /* maybe shouldn't strip off - */
      cp++;
      negacl = TRUE;
    }
  else
    negacl = FALSE;
  if (*cp == '#')
    group = TRUE, cp++;
  else
    group = FALSE;
 
  ptr = cp;
  while (!isspace(*ptr) && (*ptr != '\0')) ptr++; /* move to next token */
  strncpy(token,cp,ptr - cp);
  
if (group) 
  {
    tmp_str = expand(token);

    /* if group call expand  & add to end of return string */
    /* if double negative found drop */
    continue;
}
len = strlen(token);
  if (return_str)
	      return_str = realloc(return_str,strlen(return_str) + 
				   len + 1);
	    else
	      return_str = malloc(return_str,len + 1);

if (strpbrk(token,"*$"))  /* wild */
  {
    /* if wild expression add to end of return string */

  }
else                      /* exact */
  {
    /* if exact move to front of return string */

  }

	/*    if (return_str)
	      return_str = realloc(return_str,strlen(return_str) + 
				   strlen(cp) + 1);
	    else
	      return_str = malloc(return_str,strlen(cp) + 1);
	    end = index(return_str,'\0');
	    bcopy(cp,end,strlen(cp) +1); 
	    or strcat???*/
      while (!isspace(*cp) && (*cp != '\0')) cp++; /* move to next token */
}
return return_str;
}

main(argc, argv)
int     argc;
char    *argv[];
{
FILE *infile,*outfile;
char line[512],*cp,*string;

if (argc < 3)
  {
    printf("Give an input filename & output filename as args.");
    exit();
  }
infilename = argv[1];
outfilename = argv[2];
/* for each line in infile -- find groupname and call expand */
infile = fopen(infilename,"r");
outfile = fopen(outfilename,"a");
if (infile)
  {
    while (line = fgets(line,500,infile))
      {
	while (!isspace(*cp)) cp++;
	if (*cp != '\0')
	  {
	    *cp = '\0';
	    cp++;
	    string = expand(line);
	    if (string) {
	      fputs(string,outfile);
	      fputs("\n",outfile); 
	      free(string);
	    }
	    /* do we have to open & close this each time? */
	  }
      }
    fclose(infile);
    fclose(outfile);
  }
else
  printf("couldn't open input file: %s\n",infilename);

}



