utils.c revision 76910425
176910425Smrg
276910425Smrg  /*\
376910425Smrg   * $XFree86: xc/programs/xkbevd/utils.c,v 3.5 2001/07/25 15:05:25 dawes Exp $
476910425Smrg   *
576910425Smrg   *		              COPYRIGHT 1990
676910425Smrg   *		        DIGITAL EQUIPMENT CORPORATION
776910425Smrg   *		           MAYNARD, MASSACHUSETTS
876910425Smrg   *			    ALL RIGHTS RESERVED.
976910425Smrg   *
1076910425Smrg   * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
1176910425Smrg   * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
1276910425Smrg   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
1376910425Smrg   * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
1476910425Smrg   * WARRANTY.
1576910425Smrg   *
1676910425Smrg   * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
1776910425Smrg   * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
1876910425Smrg   * ADDITION TO THAT SET FORTH ABOVE.
1976910425Smrg   *
2076910425Smrg   * Permission to use, copy, modify, and distribute this software and its
2176910425Smrg   * documentation for any purpose and without fee is hereby granted, provided
2276910425Smrg   * that the above copyright notice appear in all copies and that both that
2376910425Smrg   * copyright notice and this permission notice appear in supporting
2476910425Smrg   * documentation, and that the name of Digital Equipment Corporation not be
2576910425Smrg   * used in advertising or publicity pertaining to distribution of the
2676910425Smrg   * software without specific, written prior permission.
2776910425Smrg  \*/
2876910425Smrg/* $Xorg: utils.c,v 1.5 2000/08/17 19:54:49 cpqbld Exp $ */
2976910425Smrg
3076910425Smrg#define DEBUG_VAR_LOCAL
3176910425Smrg#include 	"utils.h"
3276910425Smrg#include	<ctype.h>
3376910425Smrg#include	<stdlib.h>
3476910425Smrg
3576910425Smrg/***====================================================================***/
3676910425Smrg
3776910425SmrgOpaque
3876910425SmrguAlloc(size)
3976910425Smrg    unsigned	size;
4076910425Smrg{
4176910425Smrg    return((Opaque)malloc(size));
4276910425Smrg}
4376910425Smrg
4476910425Smrg/***====================================================================***/
4576910425Smrg
4676910425SmrgOpaque
4776910425SmrguCalloc(n,size)
4876910425Smrg    unsigned	n;
4976910425Smrg    unsigned	size;
5076910425Smrg{
5176910425Smrg    return((Opaque)calloc(n,size));
5276910425Smrg}
5376910425Smrg
5476910425Smrg/***====================================================================***/
5576910425Smrg
5676910425SmrgOpaque
5776910425SmrguRealloc(old,newSize)
5876910425Smrg    Opaque	old;
5976910425Smrg    unsigned	newSize;
6076910425Smrg{
6176910425Smrg    if (old==NULL)
6276910425Smrg	 return((Opaque)malloc(newSize));
6376910425Smrg    else return((Opaque)realloc((char *)old,newSize));
6476910425Smrg}
6576910425Smrg
6676910425Smrg/***====================================================================***/
6776910425Smrg
6876910425SmrgOpaque
6976910425SmrguRecalloc(old,nOld,nNew,itemSize)
7076910425Smrg    Opaque	old;
7176910425Smrg    unsigned	nOld;
7276910425Smrg    unsigned	nNew;
7376910425Smrg    unsigned	itemSize;
7476910425Smrg{
7576910425Smrgchar *rtrn;
7676910425Smrg
7776910425Smrg    if (old==NULL)
7876910425Smrg	 rtrn= (char *)calloc(nNew,itemSize);
7976910425Smrg    else {
8076910425Smrg	rtrn= (char *)realloc((char *)old,nNew*itemSize);
8176910425Smrg   	if ((rtrn)&&(nNew>nOld)) {
8276910425Smrg	    bzero(&rtrn[nOld*itemSize],(nNew-nOld)*itemSize);
8376910425Smrg	}
8476910425Smrg    }
8576910425Smrg    return (Opaque)rtrn;
8676910425Smrg}
8776910425Smrg
8876910425Smrg/***====================================================================***/
8976910425Smrg
9076910425Smrgvoid
9176910425SmrguFree(ptr)
9276910425Smrg    Opaque ptr;
9376910425Smrg{
9476910425Smrg    if (ptr!=(Opaque)NULL)
9576910425Smrg	free((char *)ptr);
9676910425Smrg    return;
9776910425Smrg}
9876910425Smrg
9976910425Smrg/***====================================================================***/
10076910425Smrg/***                  FUNCTION ENTRY TRACKING                           ***/
10176910425Smrg/***====================================================================***/
10276910425Smrg
10376910425Smrgstatic	FILE	*entryFile=	NULL;
10476910425Smrg	int	 uEntryLevel;
10576910425Smrg
10676910425SmrgBoolean
10776910425SmrguSetEntryFile(name)
10876910425Smrg    char *name;
10976910425Smrg{
11076910425Smrg    if ((entryFile!=NULL)&&(entryFile!=stderr)) {
11176910425Smrg	fprintf(entryFile,"switching to %s\n",name?name:"stderr");
11276910425Smrg	fclose(entryFile);
11376910425Smrg    }
11476910425Smrg    if (name!=NullString)	entryFile=	fopen(name,"w");
11576910425Smrg    else			entryFile=	stderr;
11676910425Smrg    if (entryFile==NULL) {
11776910425Smrg	entryFile=	stderr;
11876910425Smrg	return(False);
11976910425Smrg    }
12076910425Smrg    return(True);
12176910425Smrg}
12276910425Smrg
12376910425Smrgvoid
12476910425SmrguEntry(int l, char *s,...)
12576910425Smrg{
12676910425Smrgint	i;
12776910425Smrgva_list ap;
12876910425Smrg
12976910425Smrg    va_start(ap, s);
13076910425Smrg    for (i=0;i<uEntryLevel;i++) {
13176910425Smrg	putc(' ',entryFile);
13276910425Smrg    }
13376910425Smrg    vfprintf(entryFile,s,ap);
13476910425Smrg    uEntryLevel+= l;
13576910425Smrg    va_end(ap);
13676910425Smrg    return;
13776910425Smrg}
13876910425Smrg
13976910425Smrgvoid
14076910425SmrguExit(l,rtVal)
14176910425Smrg    int		l;
14276910425Smrg    char *	rtVal;
14376910425Smrg{
14476910425Smrgint	i;
14576910425Smrg
14676910425Smrg    uEntryLevel-= l;
14776910425Smrg    if (uEntryLevel<0)	uEntryLevel=	0;
14876910425Smrg    for (i=0;i<uEntryLevel;i++) {
14976910425Smrg	putc(' ',entryFile);
15076910425Smrg    }
15176910425Smrg    fprintf(entryFile,"---> 0x%p\n",rtVal);
15276910425Smrg    return;
15376910425Smrg}
15476910425Smrg
15576910425Smrg/***====================================================================***/
15676910425Smrg/***			PRINT FUNCTIONS					***/
15776910425Smrg/***====================================================================***/
15876910425Smrg
15976910425Smrg	FILE	*uDebugFile=		NULL;
16076910425Smrg	int	 uDebugIndentLevel=	0;
16176910425Smrg	int	 uDebugIndentSize=	4;
16276910425Smrg
16376910425SmrgBoolean
16476910425SmrguSetDebugFile(name)
16576910425Smrg    char *name;
16676910425Smrg{
16776910425Smrg    if ((uDebugFile!=NULL)&&(uDebugFile!=stderr)) {
16876910425Smrg	fprintf(uDebugFile,"switching to %s\n",name?name:"stderr");
16976910425Smrg	fclose(uDebugFile);
17076910425Smrg    }
17176910425Smrg    if (name!=NullString)	uDebugFile=	fopen(name,"w");
17276910425Smrg    else			uDebugFile=	stderr;
17376910425Smrg    if (uDebugFile==NULL) {
17476910425Smrg	uDebugFile=	stderr;
17576910425Smrg	return(False);
17676910425Smrg    }
17776910425Smrg    return(True);
17876910425Smrg}
17976910425Smrg
18076910425Smrgvoid
18176910425SmrguDebug(char *s,...)
18276910425Smrg{
18376910425Smrgint	i;
18476910425Smrgva_list ap;
18576910425Smrg
18676910425Smrg    va_start(ap, s);
18776910425Smrg    for (i=(uDebugIndentLevel*uDebugIndentSize);i>0;i--) {
18876910425Smrg	putc(' ',uDebugFile);
18976910425Smrg    }
19076910425Smrg    vfprintf(uDebugFile,s,ap);
19176910425Smrg    fflush(uDebugFile);
19276910425Smrg    va_end(ap);
19376910425Smrg    return;
19476910425Smrg}
19576910425Smrg
19676910425Smrgvoid
19776910425SmrguDebugNOI(char *s,...)
19876910425Smrg{
19976910425Smrgva_list ap;
20076910425Smrg
20176910425Smrg    va_start(ap, s);
20276910425Smrg    vfprintf(uDebugFile,s,ap);
20376910425Smrg    fflush(uDebugFile);
20476910425Smrg    va_end(ap);
20576910425Smrg    return;
20676910425Smrg}
20776910425Smrg
20876910425Smrg/***====================================================================***/
20976910425Smrg
21076910425Smrgstatic	FILE	*errorFile=	NULL;
21176910425Smrg
21276910425SmrgBoolean
21376910425SmrguSetErrorFile(char *name)
21476910425Smrg{
21576910425Smrg    if ((errorFile!=NULL)&&(errorFile!=stderr)) {
21676910425Smrg	fprintf(errorFile,"switching to %s\n",name?name:"stderr");
21776910425Smrg	fclose(errorFile);
21876910425Smrg    }
21976910425Smrg    if (name!=NullString)	errorFile=	fopen(name,"w");
22076910425Smrg    else			errorFile=	stderr;
22176910425Smrg    if (errorFile==NULL) {
22276910425Smrg	errorFile=	stderr;
22376910425Smrg	return(False);
22476910425Smrg    }
22576910425Smrg    return(True);
22676910425Smrg}
22776910425Smrg
22876910425Smrgvoid
22976910425SmrguInformation(char *s,...)
23076910425Smrg{
23176910425Smrg    va_list ap;
23276910425Smrg
23376910425Smrg    va_start(ap, s);
23476910425Smrg    vfprintf(errorFile,s,ap);
23576910425Smrg    fflush(errorFile);
23676910425Smrg    va_end(ap);
23776910425Smrg    return;
23876910425Smrg}
23976910425Smrg
24076910425Smrg/***====================================================================***/
24176910425Smrg
24276910425Smrgvoid
24376910425SmrguAction(char *s,...)
24476910425Smrg{
24576910425Smrg    va_list ap;
24676910425Smrg
24776910425Smrg    va_start(ap, s);
24876910425Smrg    fprintf(errorFile,"                  ");
24976910425Smrg    vfprintf(errorFile,s,ap);
25076910425Smrg    fflush(errorFile);
25176910425Smrg    va_end(ap);
25276910425Smrg    return;
25376910425Smrg}
25476910425Smrg
25576910425Smrg/***====================================================================***/
25676910425Smrg
25776910425Smrgvoid
25876910425SmrguWarning(char *s,...)
25976910425Smrg{
26076910425Smrg    va_list ap;
26176910425Smrg
26276910425Smrg    va_start(ap, s);
26376910425Smrg    fprintf(errorFile,"Warning:          ");
26476910425Smrg    vfprintf(errorFile,s,ap);
26576910425Smrg    fflush(errorFile);
26676910425Smrg    va_end(ap);
26776910425Smrg    return;
26876910425Smrg}
26976910425Smrg
27076910425Smrg/***====================================================================***/
27176910425Smrg
27276910425Smrgvoid
27376910425SmrguError(char *s,...)
27476910425Smrg{
27576910425Smrg    va_list ap;
27676910425Smrg
27776910425Smrg    va_start(ap, s);
27876910425Smrg    fprintf(errorFile,"Error:            ");
27976910425Smrg    vfprintf(errorFile,s,ap);
28076910425Smrg    fflush(errorFile);
28176910425Smrg    va_end(ap);
28276910425Smrg    return;
28376910425Smrg}
28476910425Smrg
28576910425Smrg/***====================================================================***/
28676910425Smrg
28776910425Smrgvoid
28876910425SmrguFatalError(char *s,...)
28976910425Smrg{
29076910425Smrg    va_list ap;
29176910425Smrg
29276910425Smrg    va_start(ap, s);
29376910425Smrg    fprintf(errorFile,"Fatal Error:      ");
29476910425Smrg    vfprintf(errorFile,s,ap);
29576910425Smrg    fprintf(errorFile,"                  Exiting\n");
29676910425Smrg    fflush(errorFile);
29776910425Smrg    va_end(ap);
29876910425Smrg    exit(1);
29976910425Smrg    /* NOTREACHED */
30076910425Smrg}
30176910425Smrg
30276910425Smrg/***====================================================================***/
30376910425Smrg
30476910425Smrgvoid
30576910425SmrguInternalError(char *s,...)
30676910425Smrg{
30776910425Smrg    va_list ap;
30876910425Smrg
30976910425Smrg    va_start(ap, s);
31076910425Smrg    fprintf(errorFile,"Internal error:   ");
31176910425Smrg    vfprintf(errorFile,s,ap);
31276910425Smrg    fflush(errorFile);
31376910425Smrg    va_end(ap);
31476910425Smrg    return;
31576910425Smrg}
31676910425Smrg
31776910425Smrg/***====================================================================***/
31876910425Smrg
31976910425Smrg#ifndef HAVE_STRDUP
32076910425Smrgchar *
32176910425SmrguStringDup(str)
32276910425Smrg    char *str;
32376910425Smrg{
32476910425Smrgchar *rtrn;
32576910425Smrg
32676910425Smrg    if (str==NULL)
32776910425Smrg	return NULL;
32876910425Smrg    rtrn= (char *)uAlloc(strlen(str)+1);
32976910425Smrg    strcpy(rtrn,str);
33076910425Smrg    return rtrn;
33176910425Smrg}
33276910425Smrg#endif
33376910425Smrg
33476910425Smrg#ifndef HAVE_STRCASECMP
33576910425Smrgint
33676910425SmrguStrCaseCmp(str1, str2)
33776910425Smrg    char *str1, *str2;
33876910425Smrg{
33976910425Smrg    char buf1[512],buf2[512];
34076910425Smrg    char c, *s;
34176910425Smrg    register int n;
34276910425Smrg
34376910425Smrg    for (n=0, s = buf1; (c = *str1++); n++) {
34476910425Smrg	if (isupper(c))
34576910425Smrg	    c = tolower(c);
34676910425Smrg	if (n>510)
34776910425Smrg	    break;
34876910425Smrg	*s++ = c;
34976910425Smrg    }
35076910425Smrg    *s = '\0';
35176910425Smrg    for (n=0, s = buf2; (c = *str2++); n++) {
35276910425Smrg	if (isupper(c))
35376910425Smrg	    c = tolower(c);
35476910425Smrg	if (n>510)
35576910425Smrg	    break;
35676910425Smrg	*s++ = c;
35776910425Smrg    }
35876910425Smrg    *s = '\0';
35976910425Smrg    return (strcmp(buf1, buf2));
36076910425Smrg}
36176910425Smrg
36276910425Smrgint
36376910425SmrguStrCasePrefix(prefix, str)
36476910425Smrg    char *prefix, *str;
36576910425Smrg{
36676910425Smrg    char c1;
36776910425Smrg    char c2;
36876910425Smrg    while (((c1=*prefix)!='\0')&&((c2=*str)!='\0')) {
36976910425Smrg	if (isupper(c1))	c1= tolower(c1);
37076910425Smrg	if (isupper(c2))	c2= tolower(c2);
37176910425Smrg	if (c1!=c2)
37276910425Smrg	    return 0;
37376910425Smrg	prefix++; str++;
37476910425Smrg    }
37576910425Smrg    if (c1!='\0')
37676910425Smrg	return 0;
37776910425Smrg    return 1;
37876910425Smrg}
37976910425Smrg
38076910425Smrg#endif
381