/
umud/DOC/
umud/DOC/U/
umud/DOC/U/U-examples/
umud/DOC/internals/
umud/DOC/wizard/
umud/MISC/
umud/MISC/dbchk/
umud/RWHO/rwhod/
/*
	Smash a flat OIF file into a series of set commands, one per line.
Basically keeps track of what object we're reading, and transforms str, cmd
and owner list type attributes into set commands. Everything else is ditched.
It doesn't know that, for example, you can't just 'set' passwords and things.
It blindly goes ahead and does it.

	Some crudities: Static buffers. It assumes no line of the input OIF
will be bigger than MAXLINE and no object ID will be bigger than MAXOID.
It exits if it finds something too big.

*/

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

/* My libraries want this. Your mileage may vary. */

#define index(a,b) strchr((a),(b))

char	*malloc();
char	*getsep();

#define MAXLINE 1024
#define MAXOID 64

#define ATTR_STR	1
#define ATTR_CMD	2
#define ATTR_OWN	4
#define ATTR_U		8

static	char line[MAXLINE];
static	char oid[MAXOID];

main(ac,av)
int	ac;
char	*av[];
{
	int	onearg = 0;
	int	len,i;
	char	*p;
	int	types = 0;
	FILE	*infile = stdin;
	FILE	*outfile = stdout;

	if(ac < 2)
		exit(usage());

	/* Parse out what types we're doing */

	for(i = 0, p = av[1]; *p && i < 3; i++,p++){
		switch(*p){
		case 's':
			types |= ATTR_STR;
			break;
		case 'c':
			types |= ATTR_CMD;
			break;
		case 'o':
			types |= ATTR_OWN;
			break;
		case 'U':
			types |= ATTR_U;
			break;
		case '?':
			exit(flags());
		default:
			exit(usage());
		}
	}

	if(!types)
		exit(usage());

	/* Figure out input/output files. */

	while(--ac > 1){
		if(av[ac][0] != '-'){
			onearg++;
			continue;
		}
		switch(av[ac][1]){
		case 'i':
			if(!onearg)
				exit(usage());
			onearg = 0;
			infile = fopen(av[ac+1],"r");
			if(infile == (FILE *)0){
				fputs("Could not open input file.\n",stderr);
				exit(usage());
			}
			break;
		case 'o':
			if(!onearg)
				exit(usage());
			onearg = 0;
			outfile = fopen(av[ac+1],"w");
			if(outfile == (FILE *)0){
				fputs("Could not open output file.\n",stderr);
				exit(usage());
			}
			break;
		default:
			exit(usage());
			break;
		}
	}

	/* Loop across the input file. */

	while(fgets(line,MAXLINE,infile) != (char *)0){
		len = strlen(line);
		if(line[len-1] != '\n'){
			fputs("line too long in input file.\n",stderr);
			exit(1);
		}
		line[len-1] = '\0';
		if(!strncmp("object ",line,7)){
			if(strlen(line+7) > MAXOID - 1){
				fputs("OID too long.\n",stderr);
				exit(1);
			}
			strcpy(oid,line+7);
			continue;
		}
		/* See if it's an attribute we're handling */

		if(((types & ATTR_STR) && !strncmp("str ",line,4)) 
				|| ((types & ATTR_CMD) && !strncmp("cmd ",line,4))
				|| ((types & ATTR_U) && !strncmp("U ",line,2))){
			fputs("set ",outfile);
			fputs(oid,outfile);
			fputs(" ",outfile);
			fputs(line,outfile);
			fputs("\n",outfile);
			continue;
		}
		if((types & ATTR_OWN) && !strncmp("lst own=",line,7)){
			char *own,*end;

			/* Walk the owners list */

			own = line + 8;
			while(*own){
				end = index(own,';');
				if(end)
					*end = '\0';
				fputs("set ",outfile);
				fputs(oid,outfile);
				fputs(" own +",outfile);
				fputs(own,outfile);
				fputs("\n",outfile);
				if(end){
					*end++ = ';';
					own = end;
				} else
					break;
			}
		}
	}
}
usage()
{
	fputs("usage: oif2set s|c|o|U|? [-i infile] [-o outfile]\n",stderr);
	return(1);
}

flags()
{
	fputs("flags: c -- cmds, s -- strs, o -- owner lists, U - U code\n",
		stderr);
	return(1);
}