#
/*
*	SCCS File Type - 
*
* 	What identification string
*/
static char *_S_edcomgetsc = "@(#)edcom_gets.c	1.11";
#include "KERcalls.h"
#include "NKSRdefs.h"
#include "NKedit.h"

char *encode();

/*****************************************************************************

PURPOSE
		Getname returns a pointer to a verified user
		input character string from a namearray struct list.

DESCRIPTION
		Getname asks for the new name and checks for
		input errors and its validity in a passed list
		of the structure type namearray before returning
		the pointer to the calling routine.

CALLS           prtable, match, strcopy, blankout

*****************************************************************************/
char *getname(list,num)
namearray list[];
int     num;
{
	extern  gaastruct Gaarecord;
	char    name[LINELENGTH];               /* location to receive group input */

	while(FOREVER)
	{
		printf ("Enter name (max 8 char): ");
		scaner(name);

		if ( strlen(name) > NAMELENGTH )
			printf ("Name too long.\n");

		else if ( name[0] == ENDSTRING )
			;                     /* no name entered */

		else if ( prtable(name,NAMELENGTH) == FOUND )
			printf ("Non printable character in name.\n");

		else if ( match(name,list,num) == FOUND)
			printf ("Name already defined.\n");

		else
			break;
	}
	return(name);
}


/*****************************************************************************

PURPOSE
		Check if input name has already been used
		in namearray structure list.

CALLED BY       getname

CALLS           strcopy

*****************************************************************************/
match(name,list,num)
register char name[];
namearray     list[];
register int  num;
{
	register int     nindex;                /* array increment */

	/* index through list */
	for(nindex =0; nindex < num; nindex++)
	{
		if (strlcmp(name,list[nindex].Name,NAMELENGTH) == EQUAL)
			return (ON);
	}
	return(OFF);
}



/*****************************************************************************

PURPOSE
		Read password from standard input, verify and encode
		into current record

DESCRIPTION
		Getpassw asks for the password,and
		asks for the password again to verify,
		check errors, encode and return.

CALLS           strcmp, encode,

*****************************************************************************/
char *getpassw()
{
	extern  int   termOD;                   /* open descriptor of tty */
	char    passw[LINELENGTH];              /* location to receive group input */
	char    vpassw[LINELENGTH];             /* location to receive verification input */
	register int     fildes = 1;            /* file descriptor of tty */
	int blockNo[2];
	int id;
	ioStatus errs;
	int bytes;
	int status;
	p_block null_Pblk;

	null_Pblk.location.domain  = null_domain;
	null_Pblk.location.space   = null_space;
	null_Pblk.location.address = 0;
	null_Pblk.b_size           = 0;

	/* turn off echo */
	status = K_device_function (termOD, GETTERMMODES, blockNo, &null_Pblk,
		&null_Pblk, 0, &errs, &bytes);
	blockNo[1] = blockNo[1] | 040000;
	status = K_device_function (termOD, SETTERMMODES1, blockNo, &null_Pblk,
		&null_Pblk, 0, &errs, &bytes);


	while(FOREVER)
	{
		printf ("Enter password (max 10 char) : ");
		scaner(passw);

		/*
		 * check length and for carriage return
		 */
		if ( strlen(passw) > PASSLENGTH )
			printf ("Password too long.\n\n");

		else if ( passw[0] == ENDSTRING )
			;                     /* no name entered */

		/*
		 * ask for password again to verify first input
		 */
		else
		{
			printf ("\nEnter password again to verify : ");
			scaner(vpassw);

			if ( strcmp(passw,vpassw) != EQUAL)
				printf ("Password not verified.\n\n");

			else
				break;
		}
	}
	/* reset echo to original mode */
	blockNo[1] = blockNo[1] & 0137777;
	status = K_device_function (termOD, SETTERMMODES1, blockNo, &null_Pblk,
		&null_Pblk, 0, &errs, &bytes);

	return(encode(passw));
}


/*****************************************************************************

PURPOSE
		Encode the groups password

CALLED BY       getpassw

CALLS           none

*****************************************************************************/
char *encode(password)
char    *password;
{
	char 	*upass;

	upass = password;
	return(upass);
}

/*****************************************************************************

PURPOSE
		Getadmin returns a vaild user id number
		from namearray struct namelist.

DESCRIPTION
		Getadmin asks for the user name, verifies that
		it is a valid by checking the Namelist structure,
		and returns the user id number.

CALLS           namematch

*****************************************************************************/
getadmin(type)
register char    *type;
{
	char     name[LINELENGTH];       /* location to receive user input */
	register int     result;

	while(FOREVER)
	{
		printf ("\nEnter user name of %s (max 8 char): ",type);
		scaner(name);

		if ( strlen(name) > NAMELENGTH )
			printf ("Name too long.\n\n");

		else if ( name[0] == ENDSTRING ); /* no name entered */

		else if ( prtable(name,NAMELENGTH) == FOUND )
			printf ("Non printable character in name.\n\n");

		else
		{
			if ((result = namematch(name)) == NOTEQUAL)
				printf ("User name not found.\n\n");
			else
				break;
		}
	}
	return(result);
}
/*****************************************************************************

PURPOSE
		check if username is in the uaadatabase

CALLED BY       getadmin

CALLS           none

*****************************************************************************/
namematch(name)
register char    name[];
{
	extern  int        Numofnames;
	extern  namearray  Namelist[];
	register int    nindex;               /* array increment */

	/* index through Namelist */
	for(nindex =0; nindex < Numofnames; nindex++)

		if(strlcmp(name,Namelist[nindex].Name,NAMELENGTH) == EQUAL)
			return(Namelist[nindex].Uid);
	return(NOTEQUAL);
}


/*****************************************************************************

PURPOSE
		Getid returns a unique and valid user input id number

DESCRIPTION
		Getid reads a number from stdin to be defined as a
		identification number, checks that it is
		smaller than the max positive integer, checks that it
		is a unique identification number in the passed namearray
		structure list, and checks that it has non-unique low bits
		before returning it to the calling routine.

CALLED BY       uaadd, dpeadd

CALLS           yesorno, getdec

*****************************************************************************/
getid(list,num,type)
namearray list[];
register int  num;
char    *type;
{
	register int     anumber;       /* if a number read, returned as ON */
	register int     i;             /* index */
	int     again = ON;             /* continue flag */
	int     id;                     /* input  id */
	char    c;                      /* nondigit read from input */

	while(again)
	{
		/* read standard in for number or carriage return */
		printf ("Enter %s identification number: ",type);
		do
			anumber = getdec(&id,&c);
		while ((anumber != FOUND) && (c != NEWLINE));

		/* number entered */
		if ((c == NEWLINE) && (anumber == FOUND))
		{
		    /*
		     * check if number out of range
		     */
		    if (id < ZERO)
			    printf ("Number too large.\n\n");

		    /*
		     * check if duplicate number or non-unique low bits
		     */
		    else
		    {
			again = OFF;
			for (i=0; i < num; i++)
			{
				if(list[i].Uid == id)
				{
					printf("%d identification number is already assigned.\n\n",id);
					again = ON;
					break;
				}
				else if ((LOWMODE & list[i].Uid) == (LOWMODE & id))
				{
					printf("Low bits are not unique.\n");
					printf("Do you want a different id number (y or n):");
					if (yesorno() == YES)
						again = ON;
					break;
				}
			}
		    }
		}

		/* non numberic value entered */
		else
			printf("Numeric value needed.\n");
	}
	return(id);
}

/*****************************************************************************

PURPOSE
		Getgid returns the groupid associated with the user
		input group name.

DESCRIPTION
		Getgid asks for the group name, compares it with
		Grouplist for validity, and returns the group
		id associated with it.

CALLED BY       uaadd

CALLS           prtable, groupmatch,

*****************************************************************************/
getgid()
{
	char    groupname[LINELENGTH];          /* location to receive user input */
	int     result;

	while(FOREVER)
	{
		printf ("\nEnter login name of group (max 8 char): ");
		scaner(groupname);

		if ( strlen(groupname) > NAMELENGTH )
			printf ("Name too long.\n\n");

		else if ( groupname[0] == ENDSTRING )
			;                     /* no name entered */

		else if ( prtable(groupname,NAMELENGTH) == FOUND )
			printf ("Non printable character in name.\n\n");

		else
		{
			if ((result = groupmatch(groupname)) == NOTEQUAL)
				printf ("Group name not found.\n\n");
			else
				break;
		}
	}
	return(result);
}

/*****************************************************************************

PURPOSE
		check if groupname is in the gaadatabase

CALLED BY       getgid

CALLS           none

*****************************************************************************/
groupmatch(groupname)
register char    groupname[];
{
	extern  int        Numofgroups;
	extern  namearray Grouplist[];
	register int    nindex;               /* array increment */

	/* index through Grouplist */
	for(nindex =0; nindex < Numofgroups; nindex++)

		if(strlcmp(groupname,Grouplist[nindex].Name,
		NAMELENGTH) == EQUAL)
			return(Grouplist[nindex].Uid);
	return(NOTEQUAL);
}

