/*
 * This file is part of an snmpquery application.
 * This file contains routines that manage the variable lists.
 *
 * 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.mit.edu/tools/src/simon/RCS/variable.c,v $
 *    $Author: tom $
 *    $Locker:  $
 *    $Log:	variable.c,v $
 * Revision 1.3  91/02/21  13:52:57  tom
 * changed array to link list
 * 
 * Revision 1.2  90/07/25  10:33:48  tom
 * checks for numeric specification of variable and frees this ptr
 * if assigned
 * 
 * Revision 1.1  90/07/20  21:17:56  tom
 * Initial revision
 * 
 *
 */

#ifndef lint
static char *rcsid = "$Header: /afs/net.mit.edu/tools/src/simon/RCS/variable.c,v 1.3 91/02/21 13:52:57 tom Exp $";
#endif


#include <simon.h>
#include <mit-copyright.h>


/*
 * callbacks
 */

/*
 * Function:    cmd_add_variable()
 * Description: add variabel to class
 */

void 
cmd_add_variable(argc, argv)
     int argc;
     char *argv[];
{
  VARIABLE v;
  CLASS *class;
  char *c = (char *) NULL;

  pname = argv[0];
  bzero(&v, sizeof(v));
  bzero(cmdbuf, sizeof(cmdbuf));
  class = stuff.class;
  
  while (*++argv != (char *) NULL)
    {
      if(**argv != '-')
        {
          if(c == (char *) NULL)
            {
              c = *argv;
              continue;
            }
          else
            fprintf(stderr, "Too many classes specified on command line.\n");
        }
      else
        if(strcmp(*argv, "-help") != 0)
          fprintf(stderr, "Unknown option \"%s\".\n", *argv);

      printf("usage: %s [<class>]\n", pname);
      return;
    }

  if(!ClassList)
    {
      printf("...but there are no classes defined!\n");
      return;
    }

  if((class == (CLASS *) NULL) || (c != (char *) NULL))
    if((class = prompt_for_class("in class: ", c)) == (CLASS *) NULL)
      {
	printf("Request cancelled.\n");
	return;
      }

  while(make_variable(class, &v) == SUCCESS)
    bzero(&v, sizeof(v));
  return;
}






/*
 * Function:    cmd_delete_variable()
 * Description: add variable to class
 */

void 
cmd_delete_variable(argc, argv)
     int argc;
     char *argv[];
{
  printf("not implemented\n");
  return;
}




/*
 * utilities
 */


int
make_variable(class, var)
     CLASS *class;
     VARIABLE *var;
{
  VARIABLE *v;

  if(class == (CLASS *) NULL)
    return(ERROR);

  if(var == (VARIABLE *) NULL)
    return(ERROR);

  if((v = (VARIABLE *) malloc(sizeof(VARIABLE))) == (VARIABLE *) NULL)
    {
      fprintf(stderr, "Unable to allocated variable space.\n");
      return(ERROR);
    }
  bzero(v, sizeof(VARIABLE));

  if(var->variable == (char *) NULL)
    {
      if((v->variable = prompt_string("variable: ", 
				      stuff.dispmode & SYMBOLIC ? 
				      stuff.prefix : (char *) NULL)) == 
	 (char *) NULL)
	return(ERROR);
      if(index(v->variable, '_') == (char *) NULL)
	{
	  v->nvariable = v->variable;
	  v->variable = (char *) NULL;
	}
      if(v->variable != (char *) NULL)
	if(strcmp(stuff.prefix, v->variable) == 0)
	  return(ERROR);      
    }
  else
    {
      if((stuff.prefix != (char *) NULL) && (stuff.dispmode & SYMBOLIC))
	{
	  v->variable = (char *) malloc(sizeof(char) * 
			       (1 + strlen(var->variable) + 
				strlen(stuff.prefix)));
	  strcpy(v->variable, stuff.prefix);
	  strcat(v->variable, var->variable);
	}
      else
	v->variable = copy_string(var->variable);
      
      if(index(v->variable, '_') == (char *) NULL)
	{
	  v->nvariable = v->variable;
	  v->variable = (char *) NULL;
	}
    }

  if(v->variable != (char *) NULL)
    if(v->variable[strlen(v->variable)-1] == '*')
      {
	v->variable[strlen(v->variable)-1] = '\0';
	v->mquery = TRUE;
      }

  if(var->abbrev != (char *) NULL)
  /*  v->abbrev = prompt_string("abbrev: ", ""); 
  else */
    v->abbrev = copy_string(var->abbrev);

#ifdef notdef
  if(var->label == (char *) NULL)
    v->label = prompt_string("label: ", "");
  else
    v->label = copy_string(var->variable);
#endif 

  return(add_variable(class, v));
}



int 
add_variable(class, var)
     CLASS *class;
     VARIABLE *var;
{
  VARIABLE *v, *prev, *save;

  if(!class->vars)
    {
      class->vars = var;
      return(SUCCESS);
    }

  save = prev = (VARIABLE *) NULL;  
  v = class->vars;
  while(v)
    {
      if(var->variable && v->variable)
	if(strcmp(v->variable, var->variable) == 0)
	  {
	    save = v->next;
	    free_variable(v);
	    break;
	  }
      prev = v;
      v = v->next;
    } 
  
  if(prev)
    prev->next = var;
  var->next = save;
  return(SUCCESS);
}



char *
find_abbrev_variable(abbrev)
     char *abbrev;
{
  CLASS    *c;
  VARIABLE *v;
  
  if(!abbrev)
    return((char *) NULL);

  c = ClassList;
  while(c)
    {
      v = c->vars;
      while(v)
	{
	  if(v->abbrev)
	    if(strcmp(abbrev, v->abbrev) == 0)
	      return(v->variable ? v->variable : v->nvariable);
	      		
	  v = v->next;
	}
      c = c->next;
    }
  return((char *) NULL);
}




VARIABLE *
get_variable(s)
     char *s;
{
  CLASS    *c;
  VARIABLE *v;
  
  if(!s)
    return((VARIABLE *) NULL);

  c = ClassList;
  while(c)
    {
      v = c->vars;
      while(v)
	{
	  if(!v->variable && !v->checked)
	    {
	      v->variable = copy_string(numstrtosym(v->nvariable));
	      v->checked = TRUE;
	    }

	  if(v->variable)
	    if(strncmp(s, v->variable, strlen(v->variable)) == 0)
	      return(v);
	  if(v->nvariable)
	    if(strncmp(s, v->nvariable, strlen(v->nvariable)) == 0)
	      return(v);
	  
	  v = v->next;
	}
      c = c->next;
    }
  return((VARIABLE *) NULL);
}



void
free_variable(v)
     VARIABLE *v;
{
  if(v == (VARIABLE *) NULL)
    return;

  if(v->variable != (char *) NULL)
    free(v->variable);

  if(v->nvariable != (char *) NULL)
    free(v->nvariable);

  if(v->abbrev != (char *) NULL)
    free(v->abbrev);

  if(v->label != (char *) NULL)
    free(v->label);
  
  free(v);
  return;
}

  
