
/* 
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/
#ident "$Header: stack.c 1.1 90/01/19 $@(#)stack.c	1.1 89/12/01"
/*
 * Stack handling
 */
#include "xs.h"
#include "stack.h"

static struct st_buf {
  struct st_buf *st_next;
  int st_snelem;
} *tab_start, *st_start;


extern char *gettxt();
/*
 * Enlarge a stack
 */
static
st_enlarge(st)
  STACK *st;
{
  st->st_max += ST_INC;
  if (st->st_data == 0){
    if (tab_start != 0){
      st->st_data = (char **)tab_start;
      st->st_max = tab_start->st_snelem;
      tab_start = tab_start->st_next;
    } else {
      if ((st->st_data = (char **)malloc(sizeof(char *) * ST_INC)) == 0)
        fatal(gettxt("xs:68","Out of memory (stack.c (1))"));
    }
  } else {
    if ((st->st_data = (char **)realloc(st->st_data, sizeof(char *) * st->st_max)) == 0)
      fatal(gettxt("xs:69","Out of memory (stack.c (2))"));
  }
}

/*
 * Push an element on top of stack
 */
void
st_push(st, elem)
  STACK *st;
  char *elem;
{
  if (st->st_s_top + 1 == st->st_max)
    st_enlarge(st);
  st->st_data[++(st->st_s_top)] = elem;
}

/*
 * Pop an element from the stack
 * Return its value as a result
 */

/*
 * Return value of top element
 */

/*
 * Return value of bottom element
 */

/*
 * Return specified element from the stack
 */

/*
 * Modify a specified stack element
 */

/*
 * Create a stack
 */
STACK *
st_creat()
{
  STACK *newst;
  if (st_start != 0){
    newst = (STACK *)st_start;
    st_start = st_start->st_next;
  } else {
    if ((newst = (STACK *)malloc(sizeof(STACK))) == 0)
      fatal(gettxt("xs:70","Out of memory (stack.c (3))"));
  }
  newst->st_s_top = -1;
  newst->st_max = 0;
  newst->st_data = 0;
  return newst;
}

/*
 * Free a stack
 */
void
st_free(st)
  STACK *st;
{
  struct st_buf *ptr;
  if (st->st_data != 0){
    ptr = (struct st_buf *)st->st_data;
    ptr->st_snelem = st->st_max;
    ptr->st_next = tab_start;
    tab_start = ptr;
  }
  ptr = (struct st_buf *)st;
  ptr->st_next = st_start;
  st_start = ptr;
}
