/************************************************************************
 *									*
 *			Copyright (c) 1982, Fred Fish			*
 *			    All Rights Reserved				*
 *									*
 *	This software and/or documentation is released for public	*
 *	distribution for personal, non-commercial use only.		*
 *	Limited rights to use, modify, and redistribute are hereby	*
 *	granted for non-commercial purposes, provided that all		*
 *	copyright notices remain intact and all changes are clearly	*
 *	documented.  The author makes no warranty of any kind with	*
 *	respect to this product and explicitly disclaims any implied	*
 *	warranties of merchantability or fitness for any particular	*
 *	purpose.							*
 *									*
 ************************************************************************
 */
/* Modified:
 * 30-Apr-86 Mic Kaczmarczik
 *	     Use ctype.h macros instead of the function isdigit().
 *	     #define index to strchr if VAXC
 *
 * 28-Jul-88 Tom Hageman
 *		Use _tfind(), include "_termcap.h"
 *		check for correctness of numeric field
 *		(warning: depends on ASCII character coding)
 *
 * (See Log at end)
 */

#ifdef RCS_ID
static const char rcsid[] =
	"$Id: tgetnum.c,v 1.4 1993/04/07 01:37:42 tom Exp tom $";
#endif

/*
 *  LIBRARY FUNCTION
 *
 *	tgetnum   extract numeric option from termcap entry
 *
 *  KEY WORDS
 *
 *	termcap
 *	ce functions
 *
 *  SYNOPSIS
 *
 *	tgetnum(id)
 *	char *id;
 *
 *  DESCRIPTION
 *
 *	Returns numeric value of capability <id>, or -1 if <id>
 *	is not found.   Knows about octal numbers, which
 *	begin with 0.
 *
 */

#include "_termcap.h"

/*
 *  PSEUDO CODE
 *
 *	Begin tgetnum
 *	    Find the entry we want the in termcap entry buffer.
 *	    If found, and the entry is a number, then
 *		Initialize value to zero.
 *		If number begins with zero then
 *			Set accumulation base to 8.
 *		Else
 *		    Set accumulation base to 10.
 *		End if
 *		While there is a numeric character
 *		    Accumulate the value.
 *		End while
 *		Return value.
 *	    End if
 *	    Return failure value.
 *	End tgetnum
 *
 */

int
tgetnum(cap)
const char	*cap;
{
	register const char	*s;
	register int		value, base;

	if ((s = _tcpfind(cap)) && *s++ == '#') {
		base = 10;

		if ((value = *s++ - '0') == 0)
			base = 8;

		if ((unsigned) value < base) {

			while ((unsigned)(*s - '0') < base)
				value = base * value + (*s++ - '0');
#if !FASTCAP
			if (*s == ':' || *s == '\0')
#else
			if (*s == '\0')
#endif
				return value;
		}
	}
	return -1;
}

/*======================================================================*
 * $Log: tgetnum.c,v $
 * Revision 1.4  1993/04/07  01:37:42  tom
 * fix typo in RCS Log.
 *
 *======================================================================*/
