/*	GenML

	Outils de génération d'entrée de MountList.

	Création:		Jean-Michel Bezeau	-	1 avril 1991
	Modification:	Jean-Michel Bezeau	-	21 juin 1991
		ajout du support de 2.0 et correction d'un petit bug
		On peux maintenant passer un nom contenant le ":"

	Modification:	Jean-Michel Bezeau	-	26 septembre 1994
		Recompilation avec SAS/C

*/

#include <exec/types.h>
#include <proto/exec.h>

#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/filehandler.h>
#include <proto/dos.h>

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

char *version	= "\0$VER: GenML 1.3 (26-9-94)";
char *createur	= "GenML © Jean-Michel Bezeau 1991,1994";
char *format	= "FORMAT: %s HANDLER/A\n";

/* ------------------------------------------------------------------------	*/
/*
 *  Convert a BSTR into a normal string.. copying the string into buf.
 */

void btos (UBYTE *buf, BSTR bstr)
{
register UBYTE *str;
	str = (UBYTE*) BADDR (bstr);
	strncpy (buf, str+1, *str);
	buf[*str] = 0;

	return;
}

/* ------------------------------------------------------------------------	*/
void AffDosList (ULONG type)
{
BPTR		 bdev;
struct DosInfo	*dinfo;
struct RootNode	*root;
struct DosList	*dosl;
char		 nom[256];

	root	= (struct RootNode	*) DOSBase->dl_Root;
	dinfo 	= (struct DosInfo	*) BADDR(root->rn_Info);
	bdev 	= (BPTR) dinfo->di_DevInfo;

	while ( dosl = (struct DosList*) BADDR(bdev) )
	{
		btos (nom, dosl->dol_Name);

		if (dosl->dol_Type == type)
			printf ("\t%s\n", nom);

		bdev = dosl->dol_Next;
	}

	printf ("\n");

	return;
}

/* ------------------------------------------------------------------------	*/
struct DosList	*ChercheDosList (char *nomdev, ULONG type)
{
BPTR		 bdev;
struct DosInfo	*dinfo;
struct RootNode	*root;
struct DosList	*dosl;
struct DosList	*dlist;
char		 nom[256];

	strtok(nomdev, ":");	// Va remplacer le : par \0 si nécessaire

	if (DOSBase->dl_lib.lib_Version >= 36)
	{
		// 2.0 friendly
		dlist = LockDosList (LDF_DEVICES|LDF_READ);
		dosl = FindDosEntry (dlist, nomdev, LDF_DEVICES);
		UnLockDosList (LDF_DEVICES|LDF_READ);
	}
	else
	{
		Forbid();
		root	= (struct RootNode	*) DOSBase->dl_Root;
		dinfo 	= (struct DosInfo	*) BADDR(root->rn_Info);
		bdev 	= (BPTR) dinfo->di_DevInfo;

		while ( dosl = (struct DosList*) BADDR(bdev) )
		{
			btos (nom, dosl->dol_Name);

			if (dosl->dol_Type == type)
				if (stricmp (nom, nomdev) == NULL)
					break;

			bdev = dosl->dol_Next;
		}
		Permit ();
		if (dosl->dol_Type != DLT_DEVICE)
			dosl = NULL;
	}

	return dosl;
}

/* ------------------------------------------------------------------------	*/
void AfficheMountList (struct DosList *dosl)
{
struct FileSysStartupMsg *fssm;
struct DosEnvec			*denv;
BOOL	fs;
char	chaine[256];

	fssm = BADDR(dosl->dol_misc.dol_handler.dol_Startup);

	/* On commence l'affichage des données	*/
	btos(chaine, dosl->dol_Name);
	printf ("%s:\n", chaine);

	if (((UBYTE*)fssm)[0] == '\0' && fssm != NULL)
	{
		btos(chaine, fssm->fssm_Device);
		printf ("\tDevice = %s\n", chaine);
		printf ("\tUnit = %d\n", fssm->fssm_Unit);

		btos(chaine, dosl->dol_misc.dol_handler.dol_Handler);
		if (chaine[0])
			printf ("\tFileSystem = %s\n", chaine);
		fs = TRUE;
		denv = BADDR(fssm->fssm_Environ);
	}
	else
	{
		btos(chaine, dosl->dol_misc.dol_handler.dol_Handler);
		if (chaine[0])
			printf ("\tHandler = %s\n", chaine);
		fs = FALSE;
	}

	printf ("\tPriority = %d\n", dosl->dol_misc.dol_handler.dol_Priority);

	printf ("\tStackSize = %d\n", dosl->dol_misc.dol_handler.dol_StackSize);

	if (dosl->dol_misc.dol_handler.dol_GlobVec)
		printf ("\tGlobVec = %d\n", dosl->dol_misc.dol_handler.dol_GlobVec);

	if (dosl->dol_misc.dol_handler.dol_Startup && fs)
	{
		btos (chaine, dosl->dol_misc.dol_handler.dol_Startup);
		if (strlen(chaine) > 0)
			printf ("\tStartup = \"%s\"\n", chaine);
	}

	if (fs)
	{
		if (denv->de_TableSize >= DE_SIZEBLOCK)
		{
			/* Rien pour le moment!	= 128	*/
		}
		if (denv->de_TableSize >= DE_SECORG)
		{
			/* Rien pour le moment! = 0		*/
		}
		if (denv->de_TableSize >= DE_NUMHEADS)
		{
			printf ("\tSurfaces = %d\n", denv->de_Surfaces);
		}
		if (denv->de_TableSize >= DE_SECSPERBLK)
		{
			/* Rien pour le moment! = 1		*/
		}
		if (denv->de_TableSize >= DE_BLKSPERTRACK)
		{
			printf ("\tBlocksPerTrack = %d\n", denv->de_BlocksPerTrack);
		}
		if (denv->de_TableSize >= DE_RESERVEDBLKS)
		{
			printf ("\tReserved = %d\n", denv->de_Reserved);
		}
		if (denv->de_TableSize >= DE_PREFAC)
		{
			printf ("\tPreAlloc = %d\n", denv->de_PreAlloc);
		}
		if (denv->de_TableSize >= DE_INTERLEAVE)
		{
			printf ("\tInterleave = %d\n", denv->de_Interleave);
		}
		if (denv->de_TableSize >= DE_LOWCYL)
		{
			printf ("\tLowCyl = %d ;", denv->de_LowCyl);
		}
		if (denv->de_TableSize >= DE_UPPERCYL)
		{
			printf ("\tHighCyl = %d\n", denv->de_HighCyl);
		}
		if (denv->de_TableSize >= DE_NUMBUFFERS)
		{
			printf ("\tBuffers = %d\n", denv->de_NumBuffers);
		}
		if (denv->de_TableSize >= DE_BUFMEMTYPE)
		{
			printf ("\tBufMemType = %d\n", denv->de_BufMemType);
		}
		if (denv->de_TableSize >= DE_MAXTRANSFER)
		{
			printf ("\tMaxTransfer = %d\n", denv->de_MaxTransfer);
		}
		if (denv->de_TableSize >= DE_MASK)
		{
			printf ("\tMask = %d\n", denv->de_Mask);
		}
		if (denv->de_TableSize >= DE_BOOTPRI)
		{
			printf ("\tBootPri = %d\n", denv->de_BootPri);
		}
		if (denv->de_TableSize >= DE_DOSTYPE)
		{
			printf ("\tDosType = 0x%X\n", denv->de_DosType);
		}
		if (denv->de_TableSize >= DE_BAUD)
		{
			printf ("\tBaud = %d\n", denv->de_Baud);
		}
		if (denv->de_TableSize >= DE_CONTROL)
		{
			printf ("\tControl = %d\n", denv->de_Control);
		}
		if (denv->de_TableSize >= DE_BOOTBLOCKS)
		{
			printf ("\tBootBlocks = %d\n", denv->de_BootBlocks);
		}
	}

	printf ("#\n");

	return;
}

/* ------------------------------------------------------------------------	*/
void main (LONG argc, char *argv[])
{
struct DosList	*dosl;
	if (argc != 2 || argv[1][0] == '?')
	{
		printf ("%s%s\n", version, createur);
		printf (format, argv[0]);
		return;
	}

	DOSBase = (struct DosLibrary*)OpenLibrary ("dos.library", 0L);

	if (DOSBase)
	{
		dosl = ChercheDosList (argv[1], DLT_DEVICE);
		if (dosl == NULL)
			printf ("Handler \"%s:\" introuvable\n", argv[1]);
		else
			AfficheMountList (dosl);

		CloseLibrary (DOSBase);
	}

	return;
}
