#include <dos.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <utility/tagitem.h>
#include <dos/dostags.h>
#include <dos/var.h>
#include <libraries/dosextens.h>
#include <libraries/locale.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <clib/locale_protos.h>
#include <clib/dos_protos.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "AXsh.h"

int tolower(int);

extern int Columns;
extern char currentdir[];
extern char *commands[MAXCONFIGLINES];
extern int Numberofcmds;

void fcomp(char *keyname)
{
	struct	FileInfoBlock fib;
	BPTR 	mylock;
	char	found[108],temppi[ARGLEN],name1[108],name2[108],name3[108];
	int		len,i,q;
	BOOL	done=FALSE,dir=FALSE;

	found[0]='\0';
	strcpy(temppi,keyname);
	i = strlen(temppi);
	while(i>=0 && temppi[i]!='/' && temppi[i]!=':')
		i--;
	q = i+1;
	lowercase(name1, temppi+q);
	len = strlen(name1);
	temppi[q] = '\0';

	if(temppi[0])
	{
		if(traverse(temppi))	/* name1 is the original name */
		{
			printf("\a");		/* name2 is current entry in lowercase */
			return;				/* name3 is what we have so far */
		}						/* found is what we have in lowercase */
	}
	else	strcpy(temppi,currentdir);

	if(mylock=Lock(temppi,ACCESS_READ))
	{
		Examine(mylock,&fib);
		while(ExNext(mylock,&fib))
		{
			lowercase(name2,fib.fib_FileName);
			if(!strncmp(name2,name1,len))
			{
				if(found[0])
				{
					i=len;
					while(found[i]==name2[i] && found[i] && name2[i])	i++;
					if(found[i])	/* filenames aren't identical */
					{
						found[i]='\0';
						strcpy(name3,fib.fib_FileName);
						name3[i]='\0';
						done=FALSE;
					}
				}
				else								/* the first match */
				{
					strcpy(name3,fib.fib_FileName);
					strcpy(found,name2);			/* name2 is already in lowercase */
					if(fib.fib_DirEntryType>0)	dir=TRUE;
					done=TRUE;
					if(!name1[0])	break;
				}
			}
		}
		UnLock(mylock);
		if(dir && done)		strcat(name3,"/");	/* we have a directory */
		if(!dir && done)	strcat(name3," ");	/* we have a complete filename */
		if(found[0])
			strcpy(keyname+q,name3);
	}
	if(!done)
		printf("\a");
}

void fcomplist(char *keyname)
{
	struct	FileInfoBlock fib;
	BPTR 	mylock;
	char	temppi[ARGLEN],name1[ARGLEN],name2[ARGLEN];
	int		len,i,q,num=0;

	strcpy(temppi,keyname);
	i=strlen(temppi);
	while(i>=0 && temppi[i]!='/' && temppi[i]!=':')	i--;
	q=i+1;
	lowercase(name1,temppi+q);
	len=strlen(name1);
	temppi[q]='\0';

	if(temppi[0])
	{
		if(traverse(temppi))	/* name1 is the original name */
		{
			printf("\a");
			return;
		}
	}
	else
		strcpy(temppi,currentdir);

	if(mylock=Lock(temppi,ACCESS_READ))
	{
		Examine(mylock,&fib);
		while(ExNext(mylock,&fib))
		{
			lowercase(name2,fib.fib_FileName);
			if(!strncmp(name2,name1,len))
			{
				num += strlen(fib.fib_FileName)+2;
				if(fib.fib_DirEntryType>0)
				{
					if(num > Columns)
					{
						printf("\n%s/ ",fib.fib_FileName);
						num = strlen(fib.fib_FileName)+2;
					}
					else
						printf("%s/ ",fib.fib_FileName);
				}
				else
				{
					if(num > Columns)
					{
						printf("\n%s  ",fib.fib_FileName);
						num = strlen(fib.fib_FileName)+2;
					}
					else
						printf("%s  ",fib.fib_FileName);
				}
			}
		}
		UnLock(mylock);
		if(num)
			printf("\n");
	}
}


void ccomp(char *name)
{
	char	found[MAXCOMLEN],name1[MAXCOMLEN];
	int		len=strlen(name),i,j;
	BOOL	done=FALSE;

	if(len>=MAXCOMLEN)	/* wont process longer commandnames than maximum.. */
	{
		printf("\a");
		return;
	}
	lowercase(name1,name);

	found[0] = '\0';
	for(j=0;j<Numberofcmds;j++)
	{
		if(!strncmp(name1,commands[j],len))
		{
			if(commands[j][len]==0)		/* exact command name */
			{
				strcpy(found,name1);
				done=TRUE;
				break;
			}
			if(found[0])
			{
				i=len;
				while(found[i]==commands[j][i])	i++;
				if(found[i])	/* the commands are not identical */
				{
					found[i]='\0';
					done=FALSE;
				}
			}
			else
			{
				strcpy(found,commands[j]);
				done=TRUE;
				if(!name1[0])	break;
			}
		}
	}
	if(found[0])
	{
		strcpy(name, found);
		if(done)
			strcat(name," ");	/* we have a complete name */
	}
	else
	{
		fcomp(name);/*printf("\a");*/

		if(found[0])
		{
			i=0;
			while(tolower(name[i])==tolower(found[i]))
				i++;
			name[i]='\0';
		}
		return;
	}
}

void ccomplist(char *name)
{
	char	name1[MAXCOMLEN];
	int		len=strlen(name),j,num=0;

	if(len>=MAXCOMLEN)	/* wont process longer commandnames than maximum.. */
	{
		printf("\a");
		return;
	}
	lowercase(name1,name);

	for(j=0;j<Numberofcmds;j++)
	{
		if(!strncmp(name1,commands[j],len))
		{
			num += strlen(commands[j])+2;
			if(num > Columns)
			{
				printf("\n%s  ",commands[j]);
				num = strlen(commands[j])+2;
			}
			else
				printf("%s  ",commands[j]);
		}
	}
	if(num)
		printf("\n");
	fcomplist(name);
}

