#include "make.h"

/*----------------------------------------------------------------*/
char *gmem( numbytes )
int numbytes;
   {
   char   *p, *getmem();

   if ( !( p = getmem(numbytes) ))
      err("Out of memory");

   setmem(p, numbytes, 0);
   return (p);
   }

/*----------------------------------------------------------------*/
msg( str, p)
char *str;
char *p;
   {
   fprintf( stderr, (Inputline) ? "Make (%s line %ld): "
                       : "Make: ",
           MAKEFILE, Inputline);
   fprintf( stderr, str, p);
   }

/*----------------------------------------------------------------*/
/* Print error msg & exit */
err( str, param )
char  *str;
   {
   msg( str, param);
   exit(20);
   }

/*----------------------------------------------------------------*/
char *find( key, root )
char *key;
TREENODE *root;
   {
   register int notequal;

   if ( !root )
      return( NULL );

   if( !(notequal = strcmp(root->name,key)) )
      return( (char *)root->inode );

   return( find( key, (notequal > 0) ? root->lnode
                                     : root->rnode ) );
   }

/*----------------------------------------------------------------*/
tree( name, inode, rootp )
char  *name;
char  *inode;
TREENODE **rootp;
   {
   register int notequal;

   if (*rootp == NULL)
      {
      *rootp = (TREENODE *)gmem( sizeof(TREENODE) );
      (*rootp)->lnode = NULL;
      (*rootp)->rnode = NULL;
      (*rootp)->name  = name;
      (*rootp)->inode = inode;
      }

   else
      if ( (notequal = strcmp( (*rootp)->name, name) ) )
         tree( name, inode, (notequal > 0) ? &(*rootp)->lnode
                                           : &(*rootp)->rnode );
      else
         (*rootp)->inode = inode;
   }

/*----------------------------------------------------------------*/
char *copystr( str )
char *str;
{
   char *p;

   p = (char *)gmem( strlen(str)+1 );
   strcpy(p, str);
   return(p);
}

STRNODE *makestr( name )
char *name;
   {
   STRNODE *result;

   result = (STRNODE *)gmem( sizeof(STRNODE) );

   result->string = name;
   result->next   = NULL;

   return(result);
   }

/*----------------------------------------------------------------*/
#define SIGS (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D)
tstbreak()
   {
   LONG SetSignal();

   if ( SetSignal(0,SIGS) & SIGS )
      {
      fprintf(stderr, "***BREAK\n");
      exit(20);  /* break key hit */
      }
   }
