/* chario.c   September 14, 1982  9:22 PM   */

#include "env.h"

/* runtime */
extern SSReturn();
extern IsDigit();
extern PutCRChar();
extern GetChar();
extern LC();

/* user interface data */
char leftover;
int any;
int mlerr[3];

int eGetChar()  /* GetChar with echo */
  {
  leftover = GetChar() & 0177;
  PutCRChar(leftover);
  if (leftover == 0177) SSReturn(mlerr, 1);
  return(leftover);
  };

int GetHex()
  {
  return(GetNum(16));
  };

int GetDecimal()
  {
  return(GetNum(10));
  };

/* get string, upto l characters returns number gotten */
int ReadID(s, l)
  char *s;
  int l;
  {
  char c;
  int w;
  int first;
  first = true;
  w = 0;
  for (;;) {
    if (w == l) {
      s[0] = 0;
      break;
      };
    if (s[w] == 0) break;
    w += 1;
    };
  w = 0;
  for (;;) {
    c = eGetChar();
    if (first && (c == '\033')) {
      wf1("%s", s);
      while (s[w] != 0) w += 1;
      first = false;
      continue;
      };
    first = false;
    switch (c) {
      case '\010': {
        if (w > 0) w -= 1;
        putchar(' ');
        putchar('\010');
        s[w] = 0;
        break;
        };
      case '\t':
      case ' ':
      case '\n':
      case '\r': return(w);
      default: {
        s[w] = c;
        w += 1;
        s[w] = 0;
        if (w == (l-1)) return(w-1);
        break;
        };
      };
    };
  };

int GetNum(radix)
  int radix;
  {
  int v, sbit;
  v = 0;
  sbit = false;
  any = 0;
  for (;;) {
    eGetChar();
    if (IsDigit(leftover)) {
      v = v * radix;
      v += ((int) leftover-'0');
      any = 1;
      continue;
      };
    leftover = LC(leftover);
    if (leftover>='a' && leftover<='f') {
      v = v * radix;
      v += ((int) leftover-'a'+10);
      any = 1;
      continue;
      };
    if (leftover == '-') {
      sbit = true;
      continue;
      };
    break;
    };
  if (sbit) v = -v;
  return(v);
  };