msi/tests: Write-strings warnings fix.
[wine/wine-kai.git] / tools / widl / utils.c
blob9f82184cd94f0c0d5b90f38a7676f0a8fa858094
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 /* #define WANT_NEAR_INDICATION */
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str)
42 while(*str)
44 if(!isprint(*str))
45 *str = ' ';
46 str++;
49 #endif
51 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
53 fprintf(stderr, "%s:%d: %s: ", input_name ? input_name : "stdin", line_number, t);
54 vfprintf(stderr, s, ap);
55 #ifdef WANT_NEAR_INDICATION
57 char *cpy;
58 if(n)
60 cpy = xstrdup(n);
61 make_print(cpy);
62 fprintf(stderr, " near '%s'", cpy);
63 free(cpy);
66 #endif
67 fprintf(stderr, "\n");
71 int yyerror(const char *s, ...)
73 va_list ap;
74 va_start(ap, s);
75 generic_msg(s, "Error", yytext, ap);
76 va_end(ap);
77 exit(1);
78 return 1;
81 int yywarning(const char *s, ...)
83 va_list ap;
84 va_start(ap, s);
85 generic_msg(s, "Warning", yytext, ap);
86 va_end(ap);
87 return 0;
90 void internal_error(const char *file, int line, const char *s, ...)
92 va_list ap;
93 va_start(ap, s);
94 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
95 vfprintf(stderr, s, ap);
96 va_end(ap);
97 exit(3);
100 void error(const char *s, ...)
102 va_list ap;
103 va_start(ap, s);
104 fprintf(stderr, "error: ");
105 vfprintf(stderr, s, ap);
106 va_end(ap);
107 exit(2);
110 void warning(const char *s, ...)
112 va_list ap;
113 va_start(ap, s);
114 fprintf(stderr, "warning: ");
115 vfprintf(stderr, s, ap);
116 va_end(ap);
119 void chat(const char *s, ...)
121 if(debuglevel & DEBUGLEVEL_CHAT)
123 va_list ap;
124 va_start(ap, s);
125 fprintf(stderr, "chat: ");
126 vfprintf(stderr, s, ap);
127 va_end(ap);
131 char *dup_basename(const char *name, const char *ext)
133 int namelen;
134 int extlen = strlen(ext);
135 char *base;
136 char *slash;
138 if(!name)
139 name = "widl.tab";
141 slash = strrchr(name, '/');
142 if (slash)
143 name = slash + 1;
145 namelen = strlen(name);
147 /* +4 for later extension and +1 for '\0' */
148 base = (char *)xmalloc(namelen +4 +1);
149 strcpy(base, name);
150 if(!strcasecmp(name + namelen-extlen, ext))
152 base[namelen - extlen] = '\0';
154 return base;
157 void *xmalloc(size_t size)
159 void *res;
161 assert(size > 0);
162 res = malloc(size);
163 if(res == NULL)
165 error("Virtual memory exhausted.\n");
168 * We set it to 0.
169 * This is *paramount* because we depend on it
170 * just about everywhere in the rest of the code.
172 memset(res, 0, size);
173 return res;
177 void *xrealloc(void *p, size_t size)
179 void *res;
181 assert(size > 0);
182 res = realloc(p, size);
183 if(res == NULL)
185 error("Virtual memory exhausted.\n");
187 return res;
190 char *xstrdup(const char *str)
192 char *s;
194 assert(str != NULL);
195 s = (char *)xmalloc(strlen(str)+1);
196 return strcpy(s, str);