
/* #######################################################################
   ###                                                                 ###
   ### Edited for MOT/BBS; 07-FEB-92 (Ed2)                             ###
   ###                                                                 ###
   ### Revision History:                                               ###
   ### Rev 0  - 26-Oct-91 Module Creation David C. Hughes Jr.          ###
   ###                                                                 ###
   ####################################################################### */

#include "amp.h"          /* definitions we need */
extern void main();

CONST
unsigned int is[] = {
 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
 0x004, 0x104, 0x104, 0x104, 0x104, 0x104, 0x004, 0x004,
 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
 0x140, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
 0x459, 0x459, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
 0x0D0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
 0x253, 0x253, 0x253, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x0D0,
 0x0D0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
 0x073, 0x073, 0x073, 0x0D0, 0x0D0, 0x0D0, 0x0D0, 0x004};

#define ALNUM     1
#define ALPHA     2
#define CNTRL     4
#define DIGIT     8
#define GRAPH    16
#define LOWER    32
#define PRINT    64
#define PUNCT   128
#define BLANK   256
#define UPPER   512
#define XDIGIT 1024

NOLOCAL
unsigned short int isalnum(c) 
unsigned short int c;
{
   return(is[c] & ALNUM);     /* 'a'-'z', 'A'-'Z', '0'-'9' */
}

NOLOCAL
unsigned short int isalpha(c)
unsigned short int c;
{
   return(is[c] & ALPHA);     /* 'a'-'z', 'A'-'Z' */
}

NOLOCAL
unsigned short int isdigit(c)
unsigned short int c;
{
   return(is[c] & DIGIT);    /* '0'-'9' */
}

NOLOCAL
unsigned short int isspace(c)
unsigned short int c;
{
   return (is[c] & BLANK);    /* HT, LF, VT, FF, CR, ' ' */
}

NOLOCAL
void putc(token)
char token;
{
   switch (token) {
          case '\n': wrbytsci((unsigned) 0x0D);   /* do CR LF trick */
                     wrbytsci((unsigned) 0x0A); 
                     break;
          default  : wrbytsci((unsigned) token);
          }
}

void print_buffer(ptr)
char *ptr;
{
    while (*ptr)
      putc(*ptr++);             /* dump asciiZ string to I/O */
}

unsigned int strlen(s)
char *s;
{
   unsigned short int i;

   i = 0;
   while (*s++) i++;
   return (i);
}

char *strcpy(s, t) 
char *s, *t;
{
   char *d;

   d = s;
   while (*s++ = *t++);
   return (d);
}

void reverse(s)
char *s; 
{
   char *j;
   char c;

   j = s + strlen(s) - 1;
   while(s < j)
   {
       c = *s;
       *s++ = *j;
       *j-- = c;
   }
}

void itoab(i_number, dest_ptr, c_base) 
unsigned int i_number; 
char *dest_ptr; 
unsigned int c_base; 
{
    unsigned short int lowbit;
    char *dest_start;

    dest_start = dest_ptr;
    c_base >>= 1;
    do 
    {
       lowbit = i_number & 1;
       i_number >>= 1;                                 /* logical shift right */
       *dest_ptr = ((i_number % c_base) << 1) + lowbit;
       if (*dest_ptr < 10)
          *dest_ptr += '0'; 
       else 
          *dest_ptr += 55;
       ++dest_ptr;
    } while(i_number /= c_base);
    *dest_ptr = 0;                         /* terminate dest string /w null */
    reverse(dest_start);
}

/* ### atoi(s) - convert s to integer. ### */
int atoi(s)
char *s; 
{
   unsigned short int sign;
   int  n;

   while (isspace(*s)) 
      ++s;
   sign = 1;
   switch(*s) {
       case '-': sign = -1;
       case '+': ++s;
       }
   n = 0;
   while (isdigit(*s))
      n = 10 * n + *s++ - '0';
   return (sign * n);
}

/* ################################################################
   ### SPRINTF Function.  String formatting routing.            ###
   ###                                                          ###
   ### Requires the following:                                  ###
   ###    dest    -> pointer to destination buffer.             ###
   ###    control -> pointer to control string.                 ###
   ###    first_arg..argN = 16 bit integer/pointer arguments.   ###
   ################################################################ */
unsigned short sprintf(dest, control, first_arg)
char *dest;
char *control;
unsigned int first_arg;
{
   unsigned int arg;
   unsigned int *arg_ptr;
   unsigned short int left, pad, len, maxchr, width;
   char *sptr, str[17];

   arg_ptr = &first_arg;   
   while (*control) 
   {
       if(*control != '%')
       {
          *dest++ = *control++;        /* copy to output buffer */
          continue;
       }
       else 
          ++control;
       if (*control == '%') 
       {
          *dest++ = *control++;        /* copy to output buffer */
          continue;
       }
       if (*control == '-') 
       {
          left = 1; 
          ++control;
       } 
       else 
          left = 0;
       if (*control == '0') 
          pad = '0'; 
       else 
          pad = ' ';
       if (isdigit(*control))
       {
          width = atoi(control++);
          while (isdigit(*control)) 
              ++control;
       }
       else 
          width = 0;
       if (*control == '.') 
       {
           maxchr = atoi(++control);
           while (isdigit(*control)) 
                 ++control;
       }
       else 
           maxchr = 0;
       arg = *arg_ptr++;
       sptr = str;
       switch(*control++) {
             case 's': sptr = (char *)arg;
                       break;
             case 'b': itoab(arg, str, 2);
                       break;
             case 'd': itoab(arg, str, 10);     /* note: unsigned only */
                       break;
             case 'x': itoab(arg, str, 16);
                       break;
             default:  return (FALSE);
       }
       len = strlen(sptr);
       if (maxchr && maxchr < len) 
           len = maxchr;
       if (width > len) 
           width = width - len; 
       else 
           width = 0;
       if (!left) 
           while (width--)
              *dest++ = pad;        /* pad to output buffer */
       while (len--) 
          *dest++ = *sptr++;        /* copy to output buffer */
       if (left) 
           while(width--) 
              *dest++ = pad;        /* pad to output buffer */
   }
   *dest = NULL;                    /* terminate the string */
   return (TRUE);
}


/* NOTE:  Due to a compiler limitation, the CONST initializers will not 
          allow the entire font table to be combined into one table. [DCHJR] */
CONST
unsigned int numeric_tbl[32][7] = {
	  { 0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, /* ### Character 0x20,   */ 
	  { 0x30,0x30,0x30,0x30,0x00,0x30,0x00 }, /* ### Character 0x21, ! */
	  { 0xCC,0xCC,0x00,0x00,0x00,0x00,0x00 }, /* ### Character 0x22, " */
	  { 0x6C,0xFE,0x6C,0x6C,0xFE,0x6C,0x00 }, /* ### Character 0x23, # */
	  { 0x30,0xFC,0xB0,0xFC,0x34,0xFC,0x30 }, /* ### Character 0x24, $ */
	  { 0xC6,0x0C,0x18,0x30,0x60,0xC6,0x00 }, /* ### Character 0x25, % */
	  { 0x78,0xCC,0x78,0x72,0xCE,0xCC,0x76 }, /* ### Character 0x26, & */
	  { 0x18,0x30,0x00,0x00,0x00,0x00,0x00 }, /* ### Character 0x27, ' */
	  { 0x02,0x0C,0x18,0x18,0x18,0x0C,0x02 }, /* ### Character 0x28, ( */
	  { 0x80,0x60,0x30,0x30,0x30,0x60,0x80 }, /* ### Character 0x29, ) */
	  { 0x00,0x66,0x3C,0x7E,0x3C,0x66,0x00 }, /* ### Character 0x2a, * */
	  { 0x00,0x30,0x30,0xFC,0x30,0x30,0x00 }, /* ### Character 0x2b, + */
	  { 0x00,0x00,0x00,0x00,0x30,0x60,0x00 }, /* ### Character 0x2c, , */
	  { 0x00,0x00,0x00,0xFE,0x00,0x00,0x00 }, /* ### Character 0x2d, - */
	  { 0x00,0x00,0x00,0x00,0x70,0x70,0x00 }, /* ### Character 0x2e, . */
     	  { 0x02,0x0C,0x18,0x30,0x60,0x80,0x00 }, /* ### Character 0x2f, / */
	  { 0x7C,0xC6,0xCE,0xE6,0xC6,0x7C,0x00 }, /* ### Character 0x30, 0 */
	  { 0x18,0x38,0x18,0x18,0x18,0x3C,0x00 }, /* ### Character 0x31, 1 */
	  { 0x7C,0x86,0x0C,0x18,0x30,0xFE,0x00 }, /* ### Character 0x32, 2 */
	  { 0x7C,0x06,0x1E,0x06,0x06,0x7C,0x00 }, /* ### Character 0x33, 3 */
	  { 0x1C,0x6C,0xCC,0xFE,0x0C,0x0C,0x00 }, /* ### Character 0x34, 4 */
	  { 0xFE,0xC0,0xFC,0x06,0xC6,0x7C,0x00 }, /* ### Character 0x35, 5 */
	  { 0x7C,0xC0,0xC0,0xFC,0xC6,0x7C,0x00 }, /* ### Character 0x36, 6 */
	  { 0xFE,0x0C,0x18,0x30,0x60,0xC0,0x00 }, /* ### Character 0x37, 7 */
	  { 0x7C,0xC6,0x7C,0xC6,0xC6,0x7C,0x00 }, /* ### Character 0x38, 8 */
	  { 0x7E,0x46,0x7E,0x06,0x0C,0x18,0x00 }, /* ### Character 0x39, 9 */
	  { 0x00,0x70,0x70,0x00,0x70,0x70,0x00 }, /* ### Character 0x3a, : */
	  { 0x00,0x38,0x38,0x00,0x38,0x30,0x60 }, /* ### Character 0x3b, ; */
	  { 0x00,0x06,0x1C,0x70,0x1C,0x06,0x00 }, /* ### Character 0x3c, < */
	  { 0x00,0x00,0xFE,0x00,0xFE,0x00,0x00 }, /* ### Character 0x3d, = */
	  { 0x00,0xC0,0x70,0x1C,0x70,0xC0,0x00 }, /* ### Character 0x3e, > */
	  { 0x7C,0xC6,0x06,0x3C,0x30,0x00,0x30 }  /* ### Character 0x3f, ? */
        };

CONST
unsigned int alpha_tbl[64][7] = {
	  { 0x7C,0xC6,0xDE,0xDC,0xC0,0x7E,0x00 }, /* ### Character 0x40, @ */
	  { 0x38,0x6C,0xC6,0xFE,0xC6,0xC6,0x00 }, /* ### Character 0x41, A */
	  { 0xFC,0x66,0x7C,0x66,0x66,0xFC,0x00 }, /* ### Character 0x42, B */
	  { 0x7C,0xC6,0xC0,0xC0,0xC6,0x7C,0x00 }, /* ### Character 0x43, C */
	  { 0xFC,0x66,0x66,0x66,0x66,0xFC,0x00 }, /* ### Character 0x44, D */
	  { 0xFE,0xC0,0xF8,0xC0,0xC0,0xFE,0x00 }, /* ### Character 0x45, E */
	  { 0xFE,0xC0,0xFC,0xC0,0xC0,0xC0,0x00 }, /* ### Character 0x46, F */
	  { 0x7C,0xC6,0xC0,0xCE,0xC6,0x7C,0x00 }, /* ### Character 0x47, G */
	  { 0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0x00 }, /* ### Character 0x48, H */
	  { 0x3C,0x18,0x18,0x18,0x18,0x3C,0x00 }, /* ### Character 0x49, I */
	  { 0x06,0x06,0x06,0x06,0xC6,0x7C,0x00 }, /* ### Character 0x4a, J */
	  { 0xC6,0xCC,0xF0,0xF0,0xCC,0xC6,0x00 }, /* ### Character 0x4b, K */
	  { 0xC0,0xC0,0xC0,0xC0,0xC0,0xFE,0x00 }, /* ### Character 0x4c, L */
	  { 0xC6,0xEE,0xD6,0xC6,0xC6,0xC6,0x00 }, /* ### Character 0x4d, M */
	  { 0xC6,0xE6,0xF6,0xDE,0xCE,0xC6,0x00 }, /* ### Character 0x4e, N */
	  { 0x7C,0xC6,0xC6,0xC6,0xC6,0x7C,0x00 }, /* ### Character 0x4f, O */
	  { 0xF8,0xCC,0xF8,0xC0,0xC0,0xC0,0x00 }, /* ### Character 0x50, P */
	  { 0x7C,0xC6,0xC6,0xD6,0xCC,0x76,0x00 }, /* ### Character 0x51, Q */
	  { 0xFC,0xC6,0xFC,0xF8,0xCC,0xC6,0x00 }, /* ### Character 0x52, R */
	  { 0x7C,0xC0,0x78,0x0C,0x84,0x78,0x00 }, /* ### Character 0x53, S */
	  { 0xFC,0x30,0x30,0x30,0x30,0x30,0x00 }, /* ### Character 0x54, T */
	  { 0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00 }, /* ### Character 0x55, U */
	  { 0xC6,0xC6,0xC6,0xC6,0x6C,0x38,0x00 }, /* ### Character 0x56, V */
	  { 0xC6,0xC6,0xC6,0xD6,0xD6,0x6C,0x00 }, /* ### Character 0x57, W */
	  { 0xC6,0x6C,0x38,0x38,0x6C,0xC6,0x00 }, /* ### Character 0x58, X */
	  { 0xCC,0xCC,0x78,0x30,0x30,0x30,0x00 }, /* ### Character 0x59, Y */
	  { 0xFE,0x0C,0x18,0x30,0x60,0xFE,0x00 }, /* ### Character 0x5a, Z */
	  { 0x1E,0x18,0x18,0x18,0x18,0x18,0x1E }, /* ### Character 0x5b, [ */
	  { 0x40,0x60,0x30,0x18,0x0C,0x06,0x02 }, /* ### Character 0x5c, \ */
	  { 0xF0,0x30,0x30,0x30,0x30,0x30,0xF0 }, /* ### Character 0x5d, ] */
	  { 0x30,0x78,0xCC,0x00,0x00,0x00,0x00 }, /* ### Character 0x5e, ^ */
	  { 0x00,0x00,0x00,0x00,0x00,0x00,0xFE }, /* ### Character 0x5f, _ */
	  { 0x30,0x0C,0x00,0x00,0x00,0x00,0x00 }, /* ### Character 0x60, ` */
	  { 0x00,0x7C,0x06,0x7E,0xC6,0x7E,0x00 }, /* ### Character 0x61, a */
	  { 0xC0,0xFC,0xC6,0xC6,0xC6,0xFC,0x00 }, /* ### Character 0x62, b */
	  { 0x00,0x7C,0xC0,0xC0,0xC0,0x7C,0x00 }, /* ### Character 0x63, c */
	  { 0x06,0x7E,0xC6,0xC6,0xC6,0x7E,0x00 }, /* ### Character 0x64, d */
	  { 0x00,0x7C,0xC6,0xFE,0xC0,0x7C,0x00 }, /* ### Character 0x65, e */
	  { 0x1C,0x36,0x30,0xFC,0x30,0x30,0x00 }, /* ### Character 0x66, f */
	  { 0x00,0x7E,0xC6,0xC6,0x7E,0x06,0x7C }, /* ### Character 0x67, g */
	  { 0xC0,0xFC,0xC6,0xC6,0xC6,0xC6,0x00 }, /* ### Character 0x68, h */
	  { 0x18,0x00,0x38,0x18,0x18,0x3C,0x00 }, /* ### Character 0x69, i */
	  { 0x0C,0x00,0x1C,0x0C,0x0C,0xCC,0x78 }, /* ### Character 0x6a, j */
	  { 0xC0,0xC0,0xCC,0xF8,0xCC,0xC6,0x00 }, /* ### Character 0x6b, k */
	  { 0x38,0x18,0x18,0x18,0x18,0x3C,0x00 }, /* ### Character 0x6c, l */
	  { 0x00,0xFC,0xD6,0xD6,0xD6,0xD6,0x00 }, /* ### Character 0x6d, m */
	  { 0x00,0xFC,0xC6,0xC6,0xC6,0xC6,0x00 }, /* ### Character 0x6e, n */
	  { 0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0x00 }, /* ### Character 0x6f, o */
	  { 0x00,0xFC,0xC6,0xC6,0xFC,0xC0,0xC0 }, /* ### Character 0x70, p */
	  { 0x00,0x7E,0xC6,0xC6,0x7E,0x06,0x06 }, /* ### Character 0x71, q */
	  { 0x00,0xBC,0xE6,0xC0,0xC0,0xC0,0x00 }, /* ### Character 0x72, r */
	  { 0x00,0x7C,0xC0,0x7C,0x06,0xFC,0x00 }, /* ### Character 0x73, s */
	  { 0x30,0xFC,0x30,0x30,0x36,0x1C,0x00 }, /* ### Character 0x74, t */
	  { 0x00,0xC6,0xC6,0xC6,0xC6,0x7E,0x00 }, /* ### Character 0x75, u */
	  { 0x00,0xC6,0xC6,0xC6,0x6C,0x38,0x00 }, /* ### Character 0x76, v */
	  { 0x00,0xC6,0xC6,0xD6,0xD6,0x6C,0x00 }, /* ### Character 0x77, w */
	  { 0x00,0xC6,0x7C,0x38,0x7C,0xC6,0x00 }, /* ### Character 0x78, x */
	  { 0x00,0xC6,0xC6,0xC6,0x7E,0x06,0x7C }, /* ### Character 0x79, y */
	  { 0x00,0xFC,0x18,0x30,0x60,0xFC,0x00 }, /* ### Character 0x7a, z */
	  { 0x0E,0x18,0x18,0x70,0x18,0x18,0x0E }, /* ### Character 0x7b, { */
	  { 0x30,0x30,0x30,0x30,0x30,0x30,0x30 }, /* ### Character 0x7c, | */
	  { 0xE0,0x30,0x30,0x1C,0x30,0x30,0xE0 }, /* ### Character 0x7d, } */
	  { 0x00,0x32,0xCC,0x00,0x00,0x00,0x00 }, /* ### Character 0x7e, ~ */
	  { 0x0E,0x18,0x18,0x18,0x18,0x70,0x00 }  /* ### Character 0x7f,   */
        };

