utils.c 4.13 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5 6 7
/*
 * Utility routines
 *
 * Copyright 1998 Bertho A. Stultiens
 *
 */

8 9
#include "config.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
10 11 12 13 14 15 16 17 18
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

#include "wrc.h"
#include "utils.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
19
#include "parser.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#define WANT_NEAR_INDICATION


#ifdef WANT_NEAR_INDICATION
void make_print(char *str)
{
	while(*str)
	{
		if(!isprint(*str))
			*str = ' ';
		str++;
	}
}
#endif

int yyerror(const char *s, ...)
{
	va_list ap;
	va_start(ap, s);
Alexandre Julliard's avatar
Alexandre Julliard committed
40
	fprintf(stderr, "Error %s: %d, %d: ", input_name ? input_name : "stdin", line_number, char_number);
Alexandre Julliard's avatar
Alexandre Julliard committed
41 42 43 44 45 46 47 48 49
	vfprintf(stderr, s, ap);
#ifdef WANT_NEAR_INDICATION
	{
		char *cpy = xstrdup(yytext);
		make_print(cpy);
		fprintf(stderr, " near '%s'\n", cpy);
		free(cpy);
	}
#else
Alexandre Julliard's avatar
Alexandre Julliard committed
50
	fprintf(stderr, "\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
51 52 53 54 55 56 57 58 59 60
#endif
	va_end(ap);
	exit(1);
	return 1;
}

int yywarning(const char *s, ...)
{
	va_list ap;
	va_start(ap, s);
Alexandre Julliard's avatar
Alexandre Julliard committed
61
	fprintf(stderr, "Warning %s: %d, %d: ", input_name ? input_name : "stdin", line_number, char_number);
Alexandre Julliard's avatar
Alexandre Julliard committed
62 63 64 65 66 67 68 69 70
	vfprintf(stderr, s, ap);
#ifdef WANT_NEAR_INDICATION
	{
		char *cpy = xstrdup(yytext);
		make_print(cpy);
		fprintf(stderr, " near '%s'\n", cpy);
		free(cpy);
	}
#else
Alexandre Julliard's avatar
Alexandre Julliard committed
71
	fprintf(stderr, "\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#endif
	va_end(ap);
	return 0;
}

void internal_error(const char *file, int line, const char *s, ...)
{
	va_list ap;
	va_start(ap, s);
	fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
	vfprintf(stderr, s, ap);
	fprintf(stderr, "\n");
	va_end(ap);
	exit(3);
}

void error(const char *s, ...)
{
	va_list ap;
	va_start(ap, s);
	fprintf(stderr, "Error: ");
	vfprintf(stderr, s, ap);
	fprintf(stderr, "\n");
	va_end(ap);
	exit(2);
}

void warning(const char *s, ...)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
101
	va_list ap;
Alexandre Julliard's avatar
Alexandre Julliard committed
102 103 104 105 106 107 108 109
	va_start(ap, s);
	fprintf(stderr, "Warning: ");
	vfprintf(stderr, s, ap);
	fprintf(stderr, "\n");
	va_end(ap);
}

void chat(const char *s, ...)
Alexandre Julliard's avatar
Alexandre Julliard committed
110
{
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	if(debuglevel & DEBUGLEVEL_CHAT)
	{
		va_list ap;
		va_start(ap, s);
		fprintf(stderr, "FYI: ");
		vfprintf(stderr, s, ap);
		fprintf(stderr, "\n");
		va_end(ap);
	}
}

char *dup_basename(const char *name, const char *ext)
{
	int namelen;
	int extlen = strlen(ext);
	char *base;
Alexandre Julliard's avatar
Alexandre Julliard committed
127
	char *slash;
Alexandre Julliard's avatar
Alexandre Julliard committed
128 129 130 131

	if(!name)
		name = "wrc.tab";

Alexandre Julliard's avatar
Alexandre Julliard committed
132 133 134 135
	slash = strrchr(name, '/');
	if (slash)
		name = slash + 1;

Alexandre Julliard's avatar
Alexandre Julliard committed
136 137 138 139 140
	namelen = strlen(name);

	/* +4 for later extension and +1 for '\0' */
	base = (char *)xmalloc(namelen +4 +1);
	strcpy(base, name);
141
	if(!strcasecmp(name + namelen-extlen, ext))
Alexandre Julliard's avatar
Alexandre Julliard committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	{
		base[namelen - extlen] = '\0';
	}
	return base;
}

void *xmalloc(size_t size)
{
    void *res;

    assert(size > 0);
    assert(size < 102400);
    res = malloc(size);
    if(res == NULL)
    {
	error("Virtual memory exhausted.\n");
    }
    memset(res, 0, size);
    return res;
}


void *xrealloc(void *p, size_t size)
{
    void *res;

    assert(size > 0);
    assert(size < 102400);
    res = realloc(p, size);
    if(res == NULL)
    {
	error("Virtual memory exhausted.\n");
    }
    return res;
}

char *xstrdup(const char *str)
{
	char *s = (char *)xmalloc(strlen(str)+1);
	return strcpy(s, str);
}

int string_compare(const string_t *s1, const string_t *s2)
{
	if(s1->type == str_char && s2->type == str_char)
	{
188
		return strcasecmp(s1->str.cstr, s2->str.cstr);
Alexandre Julliard's avatar
Alexandre Julliard committed
189 190 191
	}
	else
	{
Alexandre Julliard's avatar
Alexandre Julliard committed
192
		internal_error(__FILE__, __LINE__, "Cannot yet compare unicode strings");
Alexandre Julliard's avatar
Alexandre Julliard committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	}
	return 0;
}

int wstrlen(const short *s)
{
	int cnt = 0;
	while(*s++)
		cnt++;
	return cnt;
}

short *wstrcpy(short *dst, const short *src)
{
	short *d = dst;
	while(*src)
		*d++ = *src++;
	return dst;
}

int wstricmp(const short *s1, const short *s2)
{
	char *cs1 = dupwstr2cstr(s1);
	char *cs2 = dupwstr2cstr(s2);
217
	int retval = strcasecmp(cs1, cs2);
Alexandre Julliard's avatar
Alexandre Julliard committed
218 219
	free(cs1);
	free(cs2);
Alexandre Julliard's avatar
Alexandre Julliard committed
220
	warning("Comparing unicode strings without case -> converting to ascii");
Alexandre Julliard's avatar
Alexandre Julliard committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	return retval;;
}

short *dupcstr2wstr(const char *str)
{
	int len = strlen(str) + 1;
	short *ws = (short *)xmalloc(len*2);
	short *wptr;

	wptr = ws;
	/* FIXME: codepage translation */
	while(*str)
		*wptr++ = (short)(*str++ & 0xff);
	*wptr = 0;
	return ws;
}

char *dupwstr2cstr(const short *str)
{
	int len = wstrlen(str) + 1;
	char *cs = (char *)xmalloc(len);
	char *cptr;

	cptr = cs;
	/* FIXME: codepage translation */
	while(*str)
		*cptr++ = (char)*str++;
	*cptr = 0;
	return cs;
}