utils.c revision 3e6697d4
10e20ee16Smrg
20e20ee16Smrg  /*\
30e20ee16Smrg   * $Xorg: utils.c,v 1.5 2000/08/17 19:54:50 cpqbld Exp $
40e20ee16Smrg   *
50e20ee16Smrg   *		              COPYRIGHT 1990
60e20ee16Smrg   *		        DIGITAL EQUIPMENT CORPORATION
70e20ee16Smrg   *		           MAYNARD, MASSACHUSETTS
80e20ee16Smrg   *			    ALL RIGHTS RESERVED.
90e20ee16Smrg   *
100e20ee16Smrg   * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
110e20ee16Smrg   * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
120e20ee16Smrg   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
130e20ee16Smrg   * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
140e20ee16Smrg   * WARRANTY.
150e20ee16Smrg   *
160e20ee16Smrg   * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
170e20ee16Smrg   * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
180e20ee16Smrg   * ADDITION TO THAT SET FORTH ABOVE.
190e20ee16Smrg   *
200e20ee16Smrg   * Permission to use, copy, modify, and distribute this software and its
210e20ee16Smrg   * documentation for any purpose and without fee is hereby granted, provided
220e20ee16Smrg   * that the above copyright notice appear in all copies and that both that
230e20ee16Smrg   * copyright notice and this permission notice appear in supporting
240e20ee16Smrg   * documentation, and that the name of Digital Equipment Corporation not be
250e20ee16Smrg   * used in advertising or publicity pertaining to distribution of the
260e20ee16Smrg   * software without specific, written prior permission.
270e20ee16Smrg  \*/
280e20ee16Smrg/* $XFree86: xc/programs/xkbprint/utils.c,v 3.4 2001/01/17 23:46:11 dawes Exp $ */
290e20ee16Smrg
300e20ee16Smrg#include 	"utils.h"
310e20ee16Smrg#include	<ctype.h>
320e20ee16Smrg#include	<stdlib.h>
330e20ee16Smrg
343e6697d4Smrgunsigned int debugFlags;
353e6697d4Smrg
363e6697d4Smrg
370e20ee16Smrg/***====================================================================***/
380e20ee16Smrg
390e20ee16SmrgOpaque
403e6697d4SmrguAlloc(unsigned size)
410e20ee16Smrg{
420e20ee16Smrg    return((Opaque)malloc(size));
430e20ee16Smrg}
440e20ee16Smrg
450e20ee16Smrg/***====================================================================***/
460e20ee16Smrg
470e20ee16SmrgOpaque
483e6697d4SmrguCalloc(unsigned n, unsigned size)
490e20ee16Smrg{
500e20ee16Smrg    return((Opaque)calloc(n,size));
510e20ee16Smrg}
520e20ee16Smrg
530e20ee16Smrg/***====================================================================***/
540e20ee16Smrg
550e20ee16SmrgOpaque
563e6697d4SmrguRealloc(Opaque old, unsigned newSize)
570e20ee16Smrg{
580e20ee16Smrg    if (old==NULL)
590e20ee16Smrg	 return((Opaque)malloc(newSize));
600e20ee16Smrg    else return((Opaque)realloc((char *)old,newSize));
610e20ee16Smrg}
620e20ee16Smrg
630e20ee16Smrg/***====================================================================***/
640e20ee16Smrg
650e20ee16SmrgOpaque
663e6697d4SmrguRecalloc(Opaque old, unsigned nOld, unsigned nNew, unsigned itemSize)
670e20ee16Smrg{
680e20ee16Smrgchar *rtrn;
690e20ee16Smrg
700e20ee16Smrg    if (old==NULL)
710e20ee16Smrg	 rtrn= (char *)calloc(nNew,itemSize);
720e20ee16Smrg    else {
730e20ee16Smrg	rtrn= (char *)realloc((char *)old,nNew*itemSize);
740e20ee16Smrg   	if ((rtrn)&&(nNew>nOld)) {
750e20ee16Smrg	    bzero(&rtrn[nOld*itemSize],(nNew-nOld)*itemSize);
760e20ee16Smrg	}
770e20ee16Smrg    }
780e20ee16Smrg    return (Opaque)rtrn;
790e20ee16Smrg}
800e20ee16Smrg
810e20ee16Smrg/***====================================================================***/
820e20ee16Smrg
830e20ee16Smrgvoid
843e6697d4SmrguFree(Opaque ptr)
850e20ee16Smrg{
860e20ee16Smrg    if (ptr!=(Opaque)NULL)
870e20ee16Smrg	free((char *)ptr);
880e20ee16Smrg    return;
890e20ee16Smrg}
900e20ee16Smrg
910e20ee16Smrg/***====================================================================***/
920e20ee16Smrg/***                  FUNCTION ENTRY TRACKING                           ***/
930e20ee16Smrg/***====================================================================***/
940e20ee16Smrg
950e20ee16Smrgstatic	FILE	*entryFile=	NULL;
963e6697d4Smrgstatic	int	 uEntryLevel;
970e20ee16Smrg
980e20ee16SmrgBoolean
993e6697d4SmrguSetEntryFile(char *name)
1000e20ee16Smrg{
1010e20ee16Smrg    if ((entryFile!=NULL)&&(entryFile!=stderr)) {
1020e20ee16Smrg	fprintf(entryFile,"switching to %s\n",name?name:"stderr");
1030e20ee16Smrg	fclose(entryFile);
1040e20ee16Smrg    }
1050e20ee16Smrg    if (name!=NullString)	entryFile=	fopen(name,"w");
1060e20ee16Smrg    else			entryFile=	stderr;
1070e20ee16Smrg    if (entryFile==NULL) {
1080e20ee16Smrg	entryFile=	stderr;
1090e20ee16Smrg	return(False);
1100e20ee16Smrg    }
1110e20ee16Smrg    return(True);
1120e20ee16Smrg}
1130e20ee16Smrg
1140e20ee16Smrgvoid
1150e20ee16SmrguEntry(int l, char *s,...)
1160e20ee16Smrg{
1170e20ee16Smrgint	i;
1180e20ee16Smrgva_list ap;
1190e20ee16Smrg
1200e20ee16Smrg    va_start(ap, s);
1210e20ee16Smrg    for (i=0;i<uEntryLevel;i++) {
1220e20ee16Smrg	putc(' ',entryFile);
1230e20ee16Smrg    }
1240e20ee16Smrg    vfprintf(entryFile,s,ap);
1250e20ee16Smrg    uEntryLevel+= l;
1260e20ee16Smrg    va_end(ap);
1270e20ee16Smrg    return;
1280e20ee16Smrg}
1290e20ee16Smrg
1300e20ee16Smrgvoid
1313e6697d4SmrguExit(int l, char *rtVal)
1320e20ee16Smrg{
1330e20ee16Smrgint	i;
1340e20ee16Smrg
1350e20ee16Smrg    uEntryLevel-= l;
1360e20ee16Smrg    if (uEntryLevel<0)	uEntryLevel=	0;
1370e20ee16Smrg    for (i=0;i<uEntryLevel;i++) {
1380e20ee16Smrg	putc(' ',entryFile);
1390e20ee16Smrg    }
1400e20ee16Smrg    fprintf(entryFile,"---> 0x%p\n",rtVal);
1410e20ee16Smrg    return;
1420e20ee16Smrg}
1430e20ee16Smrg
1440e20ee16Smrg/***====================================================================***/
1450e20ee16Smrg/***			PRINT FUNCTIONS					***/
1460e20ee16Smrg/***====================================================================***/
1470e20ee16Smrg
1480e20ee16Smrg	FILE	*uDebugFile=		NULL;
1490e20ee16Smrg	int	 uDebugIndentLevel=	0;
1500e20ee16Smrg	int	 uDebugIndentSize=	4;
1510e20ee16Smrg
1520e20ee16SmrgBoolean
1533e6697d4SmrguSetDebugFile(char *name)
1540e20ee16Smrg{
1550e20ee16Smrg    if ((uDebugFile!=NULL)&&(uDebugFile!=stderr)) {
1560e20ee16Smrg	fprintf(uDebugFile,"switching to %s\n",name?name:"stderr");
1570e20ee16Smrg	fclose(uDebugFile);
1580e20ee16Smrg    }
1590e20ee16Smrg    if (name!=NullString)	uDebugFile=	fopen(name,"w");
1600e20ee16Smrg    else			uDebugFile=	stderr;
1610e20ee16Smrg    if (uDebugFile==NULL) {
1620e20ee16Smrg	uDebugFile=	stderr;
1630e20ee16Smrg	return(False);
1640e20ee16Smrg    }
1650e20ee16Smrg    return(True);
1660e20ee16Smrg}
1670e20ee16Smrg
1680e20ee16Smrgvoid
1690e20ee16SmrguDebug(char *s,...)
1700e20ee16Smrg{
1710e20ee16Smrgint	i;
1720e20ee16Smrgva_list ap;
1730e20ee16Smrg
1740e20ee16Smrg    va_start(ap, s);
1750e20ee16Smrg    for (i=(uDebugIndentLevel*uDebugIndentSize);i>0;i--) {
1760e20ee16Smrg	putc(' ',uDebugFile);
1770e20ee16Smrg    }
1780e20ee16Smrg    vfprintf(uDebugFile,s,ap);
1790e20ee16Smrg    fflush(uDebugFile);
1800e20ee16Smrg    va_end(ap);
1810e20ee16Smrg    return;
1820e20ee16Smrg}
1830e20ee16Smrg
1840e20ee16Smrgvoid
1850e20ee16SmrguDebugNOI(char *s,...)
1860e20ee16Smrg{
1870e20ee16Smrgva_list ap;
1880e20ee16Smrg
1890e20ee16Smrg    va_start(ap, s);
1900e20ee16Smrg    vfprintf(uDebugFile,s,ap);
1910e20ee16Smrg    fflush(uDebugFile);
1920e20ee16Smrg    va_end(ap);
1930e20ee16Smrg    return;
1940e20ee16Smrg}
1950e20ee16Smrg
1960e20ee16Smrg/***====================================================================***/
1970e20ee16Smrg
1980e20ee16Smrgstatic	FILE	*errorFile=	NULL;
1990e20ee16Smrg
2000e20ee16SmrgBoolean
2010e20ee16SmrguSetErrorFile(char *name)
2020e20ee16Smrg{
2030e20ee16Smrg    if ((errorFile!=NULL)&&(errorFile!=stderr)) {
2040e20ee16Smrg	fprintf(errorFile,"switching to %s\n",name?name:"stderr");
2050e20ee16Smrg	fclose(errorFile);
2060e20ee16Smrg    }
2070e20ee16Smrg    if (name!=NullString)	errorFile=	fopen(name,"w");
2080e20ee16Smrg    else			errorFile=	stderr;
2090e20ee16Smrg    if (errorFile==NULL) {
2100e20ee16Smrg	errorFile=	stderr;
2110e20ee16Smrg	return(False);
2120e20ee16Smrg    }
2130e20ee16Smrg    return(True);
2140e20ee16Smrg}
2150e20ee16Smrg
2160e20ee16Smrgvoid
2170e20ee16SmrguInformation(char *s,...)
2180e20ee16Smrg{
2190e20ee16Smrg    va_list ap;
2200e20ee16Smrg
2210e20ee16Smrg    va_start(ap, s);
2220e20ee16Smrg    vfprintf(errorFile,s,ap);
2230e20ee16Smrg    fflush(errorFile);
2240e20ee16Smrg    va_end(ap);
2250e20ee16Smrg    return;
2260e20ee16Smrg}
2270e20ee16Smrg
2280e20ee16Smrg/***====================================================================***/
2290e20ee16Smrg
2300e20ee16Smrgvoid
2310e20ee16SmrguAction(char *s,...)
2320e20ee16Smrg{
2330e20ee16Smrg    va_list ap;
2340e20ee16Smrg
2350e20ee16Smrg    va_start(ap, s);
2360e20ee16Smrg    fprintf(errorFile,"                  ");
2370e20ee16Smrg    vfprintf(errorFile,s,ap);
2380e20ee16Smrg    fflush(errorFile);
2390e20ee16Smrg    va_end(ap);
2400e20ee16Smrg    return;
2410e20ee16Smrg}
2420e20ee16Smrg
2430e20ee16Smrg/***====================================================================***/
2440e20ee16Smrg
2450e20ee16Smrgvoid
2460e20ee16SmrguWarning(char *s,...)
2470e20ee16Smrg{
2480e20ee16Smrg    va_list ap;
2490e20ee16Smrg
2500e20ee16Smrg    va_start(ap, s);
2510e20ee16Smrg    fprintf(errorFile,"Warning:          ");
2520e20ee16Smrg    vfprintf(errorFile,s,ap);
2530e20ee16Smrg    fflush(errorFile);
2540e20ee16Smrg    va_end(ap);
2550e20ee16Smrg    return;
2560e20ee16Smrg}
2570e20ee16Smrg
2580e20ee16Smrg/***====================================================================***/
2590e20ee16Smrg
2600e20ee16Smrgvoid
2610e20ee16SmrguError(char *s,...)
2620e20ee16Smrg{
2630e20ee16Smrg    va_list ap;
2640e20ee16Smrg
2650e20ee16Smrg    va_start(ap, s);
2660e20ee16Smrg    fprintf(errorFile,"Error:            ");
2670e20ee16Smrg    vfprintf(errorFile,s,ap);
2680e20ee16Smrg    fflush(errorFile);
2690e20ee16Smrg    va_end(ap);
2700e20ee16Smrg    return;
2710e20ee16Smrg}
2720e20ee16Smrg
2730e20ee16Smrg/***====================================================================***/
2740e20ee16Smrg
2750e20ee16Smrgvoid
2760e20ee16SmrguFatalError(char *s,...)
2770e20ee16Smrg{
2780e20ee16Smrg    va_list ap;
2790e20ee16Smrg
2800e20ee16Smrg    va_start(ap, s);
2810e20ee16Smrg    fprintf(errorFile,"Fatal Error:      ");
2820e20ee16Smrg    vfprintf(errorFile,s,ap);
2830e20ee16Smrg    fprintf(errorFile,"                  Exiting\n");
2840e20ee16Smrg    fflush(errorFile);
2850e20ee16Smrg    va_end(ap);
2860e20ee16Smrg    exit(1);
2870e20ee16Smrg    /* NOTREACHED */
2880e20ee16Smrg}
2890e20ee16Smrg
2900e20ee16Smrg/***====================================================================***/
2910e20ee16Smrg
2920e20ee16Smrgvoid
2930e20ee16SmrguInternalError(char *s,...)
2940e20ee16Smrg{
2950e20ee16Smrg    va_list ap;
2960e20ee16Smrg
2970e20ee16Smrg    va_start(ap, s);
2980e20ee16Smrg    fprintf(errorFile,"Internal error:   ");
2990e20ee16Smrg    vfprintf(errorFile,s,ap);
3000e20ee16Smrg    fflush(errorFile);
3010e20ee16Smrg    va_end(ap);
3020e20ee16Smrg    return;
3030e20ee16Smrg}
3040e20ee16Smrg
3050e20ee16Smrg/***====================================================================***/
3060e20ee16Smrg
3070e20ee16Smrg#ifndef HAVE_STRDUP
3080e20ee16Smrgchar *
3093e6697d4SmrguStringDup(char *str)
3100e20ee16Smrg{
3110e20ee16Smrgchar *rtrn;
3120e20ee16Smrg
3130e20ee16Smrg    if (str==NULL)
3140e20ee16Smrg	return NULL;
3150e20ee16Smrg    rtrn= (char *)uAlloc(strlen(str)+1);
3160e20ee16Smrg    strcpy(rtrn,str);
3170e20ee16Smrg    return rtrn;
3180e20ee16Smrg}
3190e20ee16Smrg#endif
3200e20ee16Smrg
3210e20ee16Smrg#ifndef HAVE_STRCASECMP
3220e20ee16Smrgint
3233e6697d4SmrguStrCaseCmp(char *str1, char *str2)
3240e20ee16Smrg{
3250e20ee16Smrg    char buf1[512],buf2[512];
3260e20ee16Smrg    char c, *s;
3270e20ee16Smrg    register int n;
3280e20ee16Smrg
3290e20ee16Smrg    for (n=0, s = buf1; (c = *str1++); n++) {
3300e20ee16Smrg	if (isupper(c))
3310e20ee16Smrg	    c = tolower(c);
3320e20ee16Smrg	if (n>510)
3330e20ee16Smrg	    break;
3340e20ee16Smrg	*s++ = c;
3350e20ee16Smrg    }
3360e20ee16Smrg    *s = '\0';
3370e20ee16Smrg    for (n=0, s = buf2; (c = *str2++); n++) {
3380e20ee16Smrg	if (isupper(c))
3390e20ee16Smrg	    c = tolower(c);
3400e20ee16Smrg	if (n>510)
3410e20ee16Smrg	    break;
3420e20ee16Smrg	*s++ = c;
3430e20ee16Smrg    }
3440e20ee16Smrg    *s = '\0';
3450e20ee16Smrg    return (strcmp(buf1, buf2));
3460e20ee16Smrg}
3470e20ee16Smrg
3480e20ee16Smrgint
3493e6697d4SmrguStrCasePrefix(char *prefix, char *str)
3500e20ee16Smrg{
3510e20ee16Smrg    char c1;
3520e20ee16Smrg    char c2;
3530e20ee16Smrg    while (((c1=*prefix)!='\0')&&((c2=*str)!='\0')) {
3540e20ee16Smrg	if (isupper(c1))	c1= tolower(c1);
3550e20ee16Smrg	if (isupper(c2))	c2= tolower(c2);
3560e20ee16Smrg	if (c1!=c2)
3570e20ee16Smrg	    return 0;
3580e20ee16Smrg	prefix++; str++;
3590e20ee16Smrg    }
3600e20ee16Smrg    if (c1!='\0')
3610e20ee16Smrg	return 0;
3620e20ee16Smrg    return 1;
3630e20ee16Smrg}
3640e20ee16Smrg
3650e20ee16Smrg#endif
366