/***********************************************************************
|
/***********************************************************************
|
|
|
util.c - memory allocation, error reporting, and other mundane stuff
|
util.c - memory allocation, error reporting, and other mundane stuff
|
|
|
Copyright (C) 1991 Dean Rubine
|
Copyright (C) 1991 Dean Rubine
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License. See ../COPYING for
|
it under the terms of the GNU General Public License. See ../COPYING for
|
the full agreement.
|
the full agreement.
|
|
|
**********************************************************************/
|
**********************************************************************/
|
/*
|
/*
|
* Mundane utility routines
|
* Mundane utility routines
|
* see util.h
|
* see util.h
|
*/
|
*/
|
|
|
/*LINTLIBRARY*/
|
/*LINTLIBRARY*/
|
|
|
#include "util.h"
|
#include "util.h"
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <ctype.h>
|
#include <ctype.h>
|
#include <setjmp.h>
|
#include <setjmp.h>
|
/* ari -- for strlen */
|
/* ari -- for strlen */
|
#include <string.h>
|
#include <string.h>
|
|
|
extern char* li_err_msg;
|
extern char* li_err_msg;
|
static char err_msg[BUFSIZ];
|
static char err_msg[BUFSIZ];
|
|
|
/*
|
/*
|
* Function used by allocation macro
|
* Function used by allocation macro
|
*/
|
*/
|
|
|
char *
|
char *
|
myalloc(nitems, itemsize, typename)
|
myalloc(nitems, itemsize, typename)
|
char *typename;
|
char *typename;
|
{
|
{
|
register unsigned int bytes = nitems * itemsize;
|
register unsigned int bytes = nitems * itemsize;
|
register char *p = malloc(bytes);
|
register char *p = malloc(bytes);
|
if(p == NULL)
|
if(p == NULL)
|
error("Can't get mem for %d %s's (each %d bytes, %d total bytes)",
|
error("Can't get mem for %d %s's (each %d bytes, %d total bytes)",
|
nitems, typename, itemsize, bytes);
|
nitems, typename, itemsize, bytes);
|
return p;
|
return p;
|
}
|
}
|
|
|
/*
|
/*
|
* Return a copy of a string
|
* Return a copy of a string
|
*/
|
*/
|
|
|
char *
|
char *
|
scopy(s)
|
scopy(s)
|
char *s;
|
char *s;
|
{
|
{
|
register char *p = allocate(strlen(s) + 1, char);
|
register char *p = allocate(strlen(s) + 1, char);
|
(void) strcpy(p, s);
|
(void) strcpy(p, s);
|
return p;
|
return p;
|
}
|
}
|
|
|
/*
|
/*
|
* Save error message, then return to recognition manager.
|
* Save error message, then return to recognition manager.
|
*/
|
*/
|
|
|
/*VARARGS1*/
|
/*VARARGS1*/
|
void
|
void
|
error(a, b, c, d, e, f, g, h, i, j)
|
error(a, b, c, d, e, f, g, h, i, j)
|
char *a;
|
char *a;
|
{
|
{
|
sprintf(err_msg, a, b, c, d, e, f, g, h, i, j);
|
sprintf(err_msg, a, b, c, d, e, f, g, h, i, j);
|
li_err_msg = err_msg;
|
li_err_msg = err_msg;
|
}
|
}
|
|
|
/*
|
/*
|
* Print error message, exit.
|
* Print error message, exit.
|
*/
|
*/
|
|
|
|
|
/*VARARGS1*/
|
/*VARARGS1*/
|
void
|
void
|
exit_error(a, b, c, d, e, f, g, h, i, j)
|
exit_error(a, b, c, d, e, f, g, h, i, j)
|
char *a;
|
char *a;
|
{
|
{
|
fprintf(stderr, a, b, c, d, e, f, g, h, i, j);
|
fprintf(stderr, a, b, c, d, e, f, g, h, i, j);
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
|
|
/*
|
/*
|
* print a message if DebugFlag is non-zero
|
* print a message if DebugFlag is non-zero
|
*/
|
*/
|
|
|
int DebugFlag = 1;
|
int DebugFlag = 1;
|
|
|
void
|
void
|
debug(a, b, c, d, e, f, g)
|
debug(a, b, c, d, e, f, g)
|
char *a;
|
char *a;
|
{
|
{
|
if(DebugFlag)
|
if(DebugFlag)
|
fprintf(stderr, a, b, c, d, e, f, g);
|
fprintf(stderr, a, b, c, d, e, f, g);
|
}
|
}
|
|
|
#define upper(c) (islower(c) ? toupper(c) : (c))
|
#define upper(c) (islower(c) ? toupper(c) : (c))
|
|
|
int
|
int
|
ucstrcmp(s1, s2)
|
ucstrcmp(s1, s2)
|
register char *s1, *s2;
|
register char *s1, *s2;
|
{
|
{
|
register int i;
|
register int i;
|
|
|
for(; *s1 && *s2; s1++, s2++)
|
for(; *s1 && *s2; s1++, s2++)
|
if( (i = (upper(*s1) - upper(*s2))) != 0)
|
if( (i = (upper(*s1) - upper(*s2))) != 0)
|
return i;
|
return i;
|
return (upper(*s1) - upper(*s2));
|
return (upper(*s1) - upper(*s2));
|
}
|
}
|
|
|
#define NSTRINGS 3
|
#define NSTRINGS 3
|
|
|
char *
|
char *
|
tempstring()
|
tempstring()
|
{
|
{
|
static char strings[NSTRINGS][100];
|
static char strings[NSTRINGS][100];
|
static int index;
|
static int index;
|
if(index >= NSTRINGS) index = 0;
|
if(index >= NSTRINGS) index = 0;
|
return strings[index++];
|
return strings[index++];
|
}
|
}
|
|
|