/*
 * This file is part of an snmp query application.
 * This file contains string utilities.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 15 April 1990
 *
 *    $Source: /afs/.net/tools/src/simon/RCS/str_utils.c,v $
 *    $Author: tom $
 *    $Locker: tom $
 *    $Log: str_utils.c,v $
 * Revision 1.2  1990/07/20  23:47:06  tom
 * compile warning
 *
 * Revision 1.1  90/07/20  21:17:45  tom
 * Initial revision
 * 
 *
 */

#ifndef lint
static char *rcsid = "$Header: /afs/.net/tools/src/simon/RCS/str_utils.c,v 1.2 1990/07/20 23:47:06 tom Exp tom $";
#endif

#include <stdio.h>
#include <sgtty.h>
#include <signal.h>
#include <ctype.h>
#include <simon.h>
#include <mit-copyright.h>

#ifdef POSIX
#else /* POSIX */
struct sgttyb ttyb;
#endif /* POSIX */

static char lbuf[BUF_SIZE];


/*
 * Function:    numeric_suffix() 
 * Decsription: decides what suffix best succeeds given number
 * Returns:     suffix
 */


char *
numeric_suffix(n)
     int n;
{
  while(n/10 > 0)
    n /= 10;

  switch(n)
    {
    case 1: 
      return("st");
    case 2:
      return("nd");
    case 3:
      return("rd");
    default:
      return("th");
    }
}



/*
 * Function:    article() 
 * Decsription: decides if "a" or "an" will best precede given word
 * Returns:     "a" or "an"
 */


char *
article(word)
     char *word;
{
  switch(*word)
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
      return("an");
    }

  switch(*word)
    {
    case 'l':
    case 'f':
    case 'm':
    case 'n':
    case 'r':
    case 's':
      switch(*(word+1))
        {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
          return("a");
        default:
          return("an");
        }
    default:
      return("a");
    }
}



/*
 * Function:    uncase() 
 * Decsription: converts a string to lower case letters. It also
 *              strips leading whitespace from the string.
 * Returns:     Nothing.
 */

void 
uncase(string)
    char *string;
{
  char *s1 = string;
  
  while (isspace (*string))
    string++;
  while (*string) 
    {
      *s1++ = isupper (*string) ? tolower (*string) : *string;
      string++;
    }
  *s1 = '\0';
}


/*
 * Function:    cap() 
 * Decsription: capitalizes first letter of the string.
 * Returns:     the string
 * Notes:
 */

char *
cap(string)
     char *string;
{
  static char buf[BUF_SIZE];
  char c;

  strncpy(buf, string, BUF_SIZE);
  c = buf[0];
  if(!isupper (c))
    buf[0] = toupper (c);
  return buf;
}



/*
 * Function:    isnumber() 
 * Decsription: checks to see if given string is a number
 * Returns:     SUCCESS/ERROR
 */

int 
isnumber(string)
     char *string;
{
  if (!string)
    return(TRUE);
  while (*string) 
    {
      if(!isdigit(*string))
	return(FALSE);
      ++string;
    }
  return(TRUE);
}



char *
format_number(num, ret)
     int num;
     char *ret;
{
  char buf[NAME_SIZE];
  char string[NAME_SIZE];
  int  i = 0;
  int  j = 0;
  int  len;
  int  pos = 0;

  (void) sprintf(buf, "%d", num);
  len = strlen(buf);
  len = len % 3;

  if(len == 2)
    pos = 1;
  if(len == 1)
    pos = 2;
  if(len == 0)
    pos = 0;

  while(buf[i] != '\0')
    {
      string[j] = buf[i];
      if((pos == 2) && (buf[i+1] != '\0'))
        {
          ++j;
          string[j] = ',';
          pos = -1;
        }
      ++j;
      ++i;
      ++pos;
    }
  string[j] = '\0';
  if(ret != (char *) NULL)
    (void) strcpy(ret, string);
  return(string);
}




/*
 * Function:    get_prompted_input() prompts the user for a command string.
 * Arguments:   prompt:         Prompt to use.
 *              buf:            Buffer to hold command line.
 *              len:            max length of buffer
 * Returns:     Nothing.
 * Notes:
 *      First, we print the prompt, then read a string using fgets().
 *      If a ^D is typed, we exit.
 */

int
get_prompted_input(prompt, buf, len)
     char *prompt;              /* Prompt to use. */
     char *buf;                 /* Input line buffer. */
     int  len;
{
  char *gets();                 /* Get a string from the stdin. */

  printf("%s", prompt);
  if (fgets(buf, len, stdin) == (char *) NULL)
    {
      printf("\n");
      return(ERROR);
    }
  buf[strlen(buf)-1] = '\0';
  return(SUCCESS);
}




void
raw_mode()
{
#ifdef POSIX
#else /* POSIX */
  ioctl(0, TIOCGETP, &ttyb);
  ttyb.sg_flags = ttyb.sg_flags & ~ECHO | CBREAK;
  ioctl(0, TIOCSETP, &ttyb);
#endif /* POSIX */
}



void
cooked_mode()
{
#ifdef POSIX 
#else /* POSIX */
  ioctl(0, TIOCGETP, &ttyb);
  ttyb.sg_flags = ttyb.sg_flags & ~CBREAK | ECHO;
  ioctl(0, TIOCSETP, &ttyb);
#endif /* POSIX */
}



int
get_prefixed_input(prompt, inbuf, len)
     char *prompt;
     char *inbuf;
     int len;
{
  char *p;
  char c;

  p = inbuf;
  if(prompt != (char *) NULL)
    write(1, prompt, sizeof(char) * strlen(prompt));

  if (inbuf != (char *) NULL)
    {
      write(1, inbuf, sizeof(char *) * strlen(inbuf));
      p += strlen(inbuf);
    }

  raw_mode();
  while (1)
    {
      c = getchar();		/* Get an input key... */

      if (c == '\n')		/* RETURN key, end input */
	break;
      if (iscntrl(c))
	{
#ifdef POSIX
#else /* POSIX */
	  if(c == ttyb.sg_erase) /* DELETE key */
#endif /* POSIX */
	    if (p > inbuf)	/* if not at beggining of inbuf, back */
	      {			/* up pointer, wipe out char, and delete */
		p--;		/* the character on the screen. */
		*p = '\0';
		write(1, "\b \b", 3);
	      }
	  continue;
	}
      if (p < &inbuf[len - 1])	/* Just print out all other characters. */
	{
	  write(1, &c, sizeof(c));
	  *p++ = c;
	  continue;
	}
      else
	write(1, "\007", sizeof(char));
    }
  *p = '\0';
  write(1, "\n", sizeof(char));
  cooked_mode();
  return(SUCCESS);
}




char *
prompt_string(prompt, prefix)
     char *prompt;
     char *prefix;
{
  char *str = (char *) NULL;
  
  bzero(lbuf, sizeof(lbuf));
  if(prefix)
    strcpy(lbuf, prefix);
  if((get_prefixed_input(prompt, lbuf, sizeof(lbuf)) != ERROR) &&
     (*lbuf != ('\0')))
    str = copy_string(lbuf);
  return(str);
}



char *
copy_string(string)
     char *string;
{
  char *s = (char *) NULL;

  if((s = (char *) malloc(sizeof(char) * (strlen(string)+1))) == (char *) NULL)
    fprintf(stderr, "copy_string: no memory\n");
  else
    strcpy(s, string);
  return(s);
}

