/*
   This program may be freely
  distributed , copied,and modified so long
as a fee is not charged. However,
I reserve all rights available to
me under The Copyright Act.
If you find any bugs please feel free to contact me.
  David DeGeorge
  Princeton,New Jersey
  March 1987

USENET:  ....princeton!idacrd!dld
COMPUSERVE: 74176,3210
GENIE: DEGEORGE
*/

/* This file
some utility routines 
for makersh
N.B. form_alert and fsel_input are called by some of the below routines
if you wish to make this a TOS application you should not use these
e.g. get_files should be replaced .
*/

#include "makersh.h"
#include <osbind.h>
#include <aesbind.h>
#ifdef GEM
begin()
{
	open_work();
}
leave()
{
	close_work();
	exit(1);
}
#endif


long findstr(ptr)
long ptr;
{
	int i;
	for ( i = 0 ;i < nstrings ; i++){
         if ( stradds[i] == ptr) return((long) i);
        }
        error("findstr error",FATAL);
}		
long findbb(ptr)
long ptr;
{
	int i;
	for ( i=0 ; i < rheader.rsh_nbb ; i++)
		if( bbadds[i] == ptr) return((long) i);
           error("findbb error",FATAL);
        }


#ifdef GEM
getfiles(inaddr,outaddr)
FILE **inaddr,**outaddr;
{
	   char filename[80];
	   char mesag[80];
	   int but;
	   FILE *temp; 
	   do {
	        if(get_file("RSC",filename)==FALSE)leave();
	        
	}	
	        while ( access(filename,CANREAD) != 0);
            *inaddr = fopen(filename,"rb");
          filename[0]='\0';
         while(1){
                 if(get_file("RSH",filename)==FALSE)leave();
                 if ( access(filename,EXISTS) == 0){
       but = form_alert(2,"[2][File exists      |Overwrite?      ][yes|no|cancel]");	
                  if ( but == 2)continue;
                  if(but == 3)leave();
                }
                  if((temp = fopen(filename,"w"))== NULL){
    sprintf(mesag,"[2][Can't open|%s|for a write    ][try again|abort]",filename);
                  but = form_alert(2,mesag);
                  if (but == 2) leave();
                  continue;
        }                  
                break;
      }            
       *outaddr = temp;

}          





/* The below four routines were glombed from
   Time Oren's column on
   Compuserve
 */
  
 
get_file(extnt, got_file)
	char	*extnt, *got_file;
	{
	int	butn, ii;
	char	tmp_path[64], tmp_name[13];
	tmp_name[0] = '\0';
	tmp_path[0] = '\0';

	if (*got_file)
		parse_fname(got_file, tmp_path, tmp_name, extnt);
	if (!tmp_path[0])
		get_path(&tmp_path[0], extnt);

	fsel_input(tmp_path, tmp_name, &butn);
	if (butn)
		{
		strcpy(got_file, tmp_path);
		for (ii = 0; got_file[ii] && got_file[ii] != '*'; ii++);
		got_file[ii - 1] = '\0';
		strcat (got_file, "\\");
		strcat(got_file, tmp_name);
		return (TRUE);
		}
	else
		return (FALSE);
	}


parse_fname(full, path, name, extnt)
	char	*full, *path, *name, *extnt;
	{
	int	i, j;
	char	*s, *d;

	for (i = strlen(full); i--; )		/* scan for end of path */
		if (full[i] == '\\' || full[i] == ':')
			break;
	if (i == -1)
		strcpy(name, full);		/* "Naked" file name */
	else
		{
		strcpy(name, &full[i+1]);
		for (s = full, d = path, j = 0; j++ < i + 1;
			*d++ = *s++);
		strcpy(&path[i+1], "*.");
		strcat(path, extnt);
		}
	}



get_path(tmp_path, spec)
	char	*tmp_path, *spec;
	{
	int	cur_drv;

	cur_drv = Dgetdrv();
	tmp_path[0] = cur_drv + 'A';
	tmp_path[1] = ':';
	Dgetpath(&tmp_path[2], 0);
	if (strlen(tmp_path) > 3)
		strcat(tmp_path, "\\");
	else
		tmp_path[2] = '\0';
	strcat(tmp_path, "*.");
	strcat(tmp_path, spec);
	}



void new_ext(o_fname, n_fname, ext)
	char	*o_fname, *n_fname, *ext;
	{
	int	ii, jj;

	strcpy(n_fname, o_fname);
	for (ii = (jj = strlen(n_fname)) - 1; ii && n_fname[ii] != '.'; ii--);
	if (!ii)
		n_fname[ii = jj] = '.';
	strcpy(&n_fname[++ii], ext);
	}
#endif

