/***************************************************************************
 * errors.c:	Error-handling functions.
 *
 * Part of...
 *
 *	FREE:	Display free space on your disk volumes.
 *		Author:  Daniel J. Barrett, barrett@cs.umass.edu.
 *
 * This program is Freely Distributable.  Make all the copies you want
 * and give them away.  Use this code in any way you like.
***************************************************************************/

#include "free.h"
#include "version.h"

/* The user made a mistake in invoking the program.  Give 'em hell. */

void Usage(char *prog)
{
	fprintf(stderr, "%s%s%s Version %s by Daniel Jay Barrett.\n",
		HI_ON, prog, HI_OFF, VERSION);
	fprintf(stderr, "Inspired by Tom Smythe's FREE on Fish Disk 66.\n");
	fprintf(stderr, "This program is Freely Distributable.\n\n");

	fprintf(stderr,
"Usage: %s%s%s [-%c%c] [-%c BYTES] [-%c LENGTH] [volume1] [volume2] ...\n\n",
	   HI_ON, prog, HI_OFF, OPT_BLOCKS, OPT_REQUESTORS, OPT_MALLOC,
	   OPT_VOLUME_NAME_LEN);

	fprintf(stderr, "\t-%c:\tDisplay blocks instead of bytes.\n",
		OPT_BLOCKS);
	fprintf(stderr, "\t-%c:\tEnable volume requestors.\n", OPT_REQUESTORS);
	fprintf(stderr, "\t-%c:\tAllocate BYTES of memory for the output.\n",
		OPT_MALLOC);
	fprintf(stderr,
	   "\t-%c:\tSpecify the max LENGTH of a volume name (1 - %d).\n",
	   OPT_VOLUME_NAME_LEN, MAX_NAME_LEN);
	
	fprintf(stderr, "\nIf no volumes given, the environment ");
	fprintf(stderr, "variable `%s' is used.\n", ENV_VARIABLE);
	fprintf(stderr, "Example:  setenv %s \"df0:,df1:,ram:,dh0:\"\n\n",
		ENV_VARIABLE);

	fprintf(stderr, "If %s does not exist, ", ENV_VARIABLE);
	fprintf(stderr, "all known devices are listed (AmigaOS 2.0+)\n");
	fprintf(stderr, "or \"%s\" is assumed (AmigaOS pre-2.0).\n",
		DEFAULT_VOLUMES_OLD);
}


/* Given a particular error code, print the appropriate error message. */

void ErrorMsg(ERROR code)
{
	if (code)
		fprintf(stderr, "ERROR! ");
	switch (code)
	{
		case NO_ERROR:
			break;
		case ERROR_TOO_SMALL:
			fprintf(stderr, "The output is too long to fit in " \
				"%d bytes.\n" \
				"Use the -%c option to increase memory " \
				"size.\n" \
			   	"Output TRUNCATED and possibly not accurate."\
			   	"\n\n", memSize, OPT_MALLOC);
			break;
		case ERROR_CANT_MALLOC:
			fprintf(stderr, "Out of memory!\n");
			break;
		case ERROR_FORMAT_STRING:
			fprintf(stderr, "Your -%c value must be an integer" \
				" between 1 and %d\n",
				OPT_VOLUME_NAME_LEN, MAX_NAME_LEN, ".");
			break;
		case ERROR_INFO:
			fprintf(stderr, "Info() failed!\n");
			break;
		case ERROR_DOSLIST:
			fprintf(stderr, "Could not get device list.\n");
			break;
		case ERROR_IMPOSSIBLE:
		default:
		   fprintf(stderr, "This should never happen! (code=%d)\n" \
			   "There must be a bug in this program.\n",
			   code);
		   break;
	}
}


/* If the user specified an illegal value (0 or less) as the argument of
 * the option OPT_VOLUME_NAME_LEN, complain!  Then use the default value. */

void CheckVolumeNameLen(int *nameLen)
{
	if (*nameLen < 1 || *nameLen > MAX_NAME_LEN)
	{
		ErrorMsg(ERROR_FORMAT_STRING);
		*nameLen = DEFAULT_NAME_LEN;
	}
}


/* If the user specified an invalid volume name, complain!  A valid name
 * must have at least 1 character (really two, but...), and its last
 * character must be VOLUME_END_CHAR. */

int ValidDriveName(char *drive)
{
	int len = strlen(drive);

	return((len > 0)  &&  (drive[len-1] == VOLUME_END_CHAR));
}
