vdmdbg: Add a stub implementation of VDMEnumTaskWOW.
[wine/multimedia.git] / tools / widl / utils.c
blob007d1cb0311d68ac9c4cdf552e88e0bc2a641ea0
1 /*
2 * Utility routines
4 * Copyright 1998 Bertho A. Stultiens
5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <ctype.h>
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
37 static const int want_near_indication = 0;
39 static void make_print(char *str)
41 while(*str)
43 if(!isprint(*str))
44 *str = ' ';
45 str++;
49 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
51 fprintf(stderr, "%s:%d: %s: ", input_name ? input_name : "stdin", line_number, t);
52 vfprintf(stderr, s, ap);
54 if (want_near_indication)
56 char *cpy;
57 if(n)
59 cpy = xstrdup(n);
60 make_print(cpy);
61 fprintf(stderr, " near '%s'", cpy);
62 free(cpy);
66 fprintf(stderr, "\n");
70 int parser_error(const char *s, ...)
72 va_list ap;
73 va_start(ap, s);
74 generic_msg(s, "Error", parser_text, ap);
75 va_end(ap);
76 exit(1);
77 return 1;
80 int parser_warning(const char *s, ...)
82 va_list ap;
83 va_start(ap, s);
84 generic_msg(s, "Warning", parser_text, ap);
85 va_end(ap);
86 return 0;
89 void internal_error(const char *file, int line, const char *s, ...)
91 va_list ap;
92 va_start(ap, s);
93 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
94 vfprintf(stderr, s, ap);
95 va_end(ap);
96 exit(3);
99 void error(const char *s, ...)
101 va_list ap;
102 va_start(ap, s);
103 fprintf(stderr, "error: ");
104 vfprintf(stderr, s, ap);
105 va_end(ap);
106 exit(2);
109 void warning(const char *s, ...)
111 va_list ap;
112 va_start(ap, s);
113 fprintf(stderr, "warning: ");
114 vfprintf(stderr, s, ap);
115 va_end(ap);
118 void chat(const char *s, ...)
120 if(debuglevel & DEBUGLEVEL_CHAT)
122 va_list ap;
123 va_start(ap, s);
124 fprintf(stderr, "chat: ");
125 vfprintf(stderr, s, ap);
126 va_end(ap);
130 char *dup_basename(const char *name, const char *ext)
132 int namelen;
133 int extlen = strlen(ext);
134 char *base;
135 char *slash;
137 if(!name)
138 name = "widl.tab";
140 slash = strrchr(name, '/');
141 if (slash)
142 name = slash + 1;
144 namelen = strlen(name);
146 /* +4 for later extension and +1 for '\0' */
147 base = xmalloc(namelen +4 +1);
148 strcpy(base, name);
149 if(!strcasecmp(name + namelen-extlen, ext))
151 base[namelen - extlen] = '\0';
153 return base;
156 void *xmalloc(size_t size)
158 void *res;
160 assert(size > 0);
161 res = malloc(size);
162 if(res == NULL)
164 error("Virtual memory exhausted.\n");
166 memset(res, 0x55, size);
167 return res;
171 void *xrealloc(void *p, size_t size)
173 void *res;
175 assert(size > 0);
176 res = realloc(p, size);
177 if(res == NULL)
179 error("Virtual memory exhausted.\n");
181 return res;
184 char *xstrdup(const char *str)
186 char *s;
188 assert(str != NULL);
189 s = xmalloc(strlen(str)+1);
190 return strcpy(s, str);