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

/* GENREAL PURPOSE SUBROUTINES */
	/*  findplace - place in string of char 
	    upstring - convert string to upper case 
	    strmatch - see if strings match for shortest's length 
	    ropenfile - open file for reading */
#include	"equs.c"
#include	"data.c"

	/* return place of char in a string, reutrn 0 if not found */
findplace (chr,stptr)
	char chr;
	char *stptr;
	{ 
	int i;
	
	
	for (i=1; *stptr; i++)
		{
		if (*stptr == chr) return (i);
		stptr++;
		}
	return (0);
	}

	
	/* convert string to upper case */
upstring (ptr)
	char *ptr;
	{	
	while (*ptr)
		{
		*ptr = toupper (*ptr);
		ptr++;
		}
	}

/* return true if strings match for length of the shorter one */
char	strmatch (p1,p2)
	char	*p1,*p2;
{	
	while ((*p1) && (*p2))
		{
		if (*p1 != *p2) return (FALSE);
		p1++;
		p2++;
		}
	return (TRUE);	
} /* end of strmatch */

/* opens file using passed in string and returns fd.  On failure 
    returns a -1  */
int	ropenfile (fname)
	char *fname;
{	int	fd;
	char	result,usename[15];
	strcpy (usename,fname);
	if ((fd = open (usename,0)) != -1) return (fd);
	PA.PAptr [2] = ms11ptr; 
	curpos (PROMPTLINE - 2,PROMCOL + strlen (ms11ptr));
	putz (usename);
	  while (1)
		{
		PA.PAptr [1] = ms12ptr; 
		PA.PAptr [0] = ms13ptr; 
		doPrompt (3,PA);
		while (1)
			{
			result = inpPrompt (PROMPTLINE,strlen (ms13ptr));
			if ((result == CR) | (result == LF)) 
				goto roperet;  /* fd is still -1 */
			if ((result <= 'D') & (result >= 'A'))
				{
				strcpy (usename," :");
				usename [0] = result;
				strcat (usename,fname);
				if ((fd = open (usename,0)) != -1)
					goto roperet;
				strcpy (usename,fname);
				PA.PAptr [2] = ms92ptr;
				}
			else
				{ clrprompt (3);
				  PA.PAptr [2] = ms13aptr;
				}
			}  /* get drive char loop */
		} /* open not successful loop*/	
	roperet:
	return (fd);
}  /* end of ropenfile */


/* get a valid file name or return empty string */
GTfilename (str)
	char	*str;
{	GTSfileName (str,PROMPTLINE);
}




/* get a valid file name or return empty string,
	with slectable prompt line */
GTSfilename (str,line)
	char	*str,line;
	
{	int	i;
	char 	*chk,invalString [20],hold; /* invalid CPM file name chars */
	
	strcpy (invalString,"<>=,:;*?&![]()/$");
gfstart:
	intostring (str,14);
	upstring (str);
	chk =str;
	if (strlen (str) == 0) return;
   /* see if string valid */
	/* if drive char, see if valid */
	if (*(str + 1) == ':')
		{ if ((*str < 'A') | (*str > 'D')) goto badname;
		  if (strlen (str) <3) goto badname;
		  chk = (str + 2);
		}	/* if drive char */
	/* check for invalid chars */
	for (i = 0; i < strlen (chk); i++)
		{
		  if (findplace (*(chk + i),invalString)) goto badname;
		  if ((*(str + i) > '~') | (*(chk + i) < '!')) goto badname;
		}
	return;

badname:
	curpos (line -1,0);
	putz (clrtoeolseq);
	curpos (line,0);
	putz (clrtoeolseq);
	PA.PAptr [1] = ms95ptr;
	PA.PAptr [0] = ms38ptr;
	dosPrompt (2,PA,line);
	ptch (BELL);
	hold = inpPrompt (line,strlen (ms38ptr));
	if ((hold == 8CR) | (hold == ESC))
		{ str [0] = '\0';
		  return;
		}
	ungtch (hold);
	goto gfstart;

}  /* end of GTfilename */

