Do not define __WINESRC__ when building tools and miscemu.
[wine] / tools / widl / utils.c
1 /*
2  * Utility routines
3  *
4  * Copyright 1998 Bertho A. Stultiens
5  * Copyright 2002 Ove Kaaven
6  *
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.
11  *
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.
16  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <ctype.h>
31
32 #include "wine/unicode.h"
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
36
37 /* #define WANT_NEAR_INDICATION */
38
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str)
41 {
42         while(*str)
43         {
44                 if(!isprint(*str))
45                         *str = ' ';
46                 str++;
47         }
48 }
49 #endif
50
51 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
52 {
53         fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
54         vfprintf(stderr, s, ap);
55 #ifdef WANT_NEAR_INDICATION
56         {
57                 char *cpy;
58                 if(n)
59                 {
60                         cpy = xstrdup(n);
61                         make_print(cpy);
62                         fprintf(stderr, " near '%s'", cpy);
63                         free(cpy);
64                 }
65         }
66 #endif
67         fprintf(stderr, "\n");
68 }
69
70
71 int yyerror(const char *s, ...)
72 {
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;
79 }
80
81 int yywarning(const char *s, ...)
82 {
83         va_list ap;
84         va_start(ap, s);
85         generic_msg(s, "Warning", yytext, ap);
86         va_end(ap);
87         return 0;
88 }
89
90 void internal_error(const char *file, int line, const char *s, ...)
91 {
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         fprintf(stderr, "\n");
97         va_end(ap);
98         exit(3);
99 }
100
101 void error(const char *s, ...)
102 {
103         va_list ap;
104         va_start(ap, s);
105         fprintf(stderr, "Error: ");
106         vfprintf(stderr, s, ap);
107         fprintf(stderr, "\n");
108         va_end(ap);
109         exit(2);
110 }
111
112 void warning(const char *s, ...)
113 {
114         va_list ap;
115         va_start(ap, s);
116         fprintf(stderr, "Warning: ");
117         vfprintf(stderr, s, ap);
118         fprintf(stderr, "\n");
119         va_end(ap);
120 }
121
122 void chat(const char *s, ...)
123 {
124         if(debuglevel & DEBUGLEVEL_CHAT)
125         {
126                 va_list ap;
127                 va_start(ap, s);
128                 fprintf(stderr, "FYI: ");
129                 vfprintf(stderr, s, ap);
130                 fprintf(stderr, "\n");
131                 va_end(ap);
132         }
133 }
134
135 char *dup_basename(const char *name, const char *ext)
136 {
137         int namelen;
138         int extlen = strlen(ext);
139         char *base;
140         char *slash;
141
142         if(!name)
143                 name = "widl.tab";
144
145         slash = strrchr(name, '/');
146         if (slash)
147                 name = slash + 1;
148
149         namelen = strlen(name);
150
151         /* +4 for later extension and +1 for '\0' */
152         base = (char *)xmalloc(namelen +4 +1);
153         strcpy(base, name);
154         if(!strcasecmp(name + namelen-extlen, ext))
155         {
156                 base[namelen - extlen] = '\0';
157         }
158         return base;
159 }
160
161 void *xmalloc(size_t size)
162 {
163     void *res;
164
165     assert(size > 0);
166     res = malloc(size);
167     if(res == NULL)
168     {
169         error("Virtual memory exhausted.\n");
170     }
171     /*
172      * We set it to 0.
173      * This is *paramount* because we depend on it
174      * just about everywhere in the rest of the code.
175      */
176     memset(res, 0, size);
177     return res;
178 }
179
180
181 void *xrealloc(void *p, size_t size)
182 {
183     void *res;
184
185     assert(size > 0);
186     res = realloc(p, size);
187     if(res == NULL)
188     {
189         error("Virtual memory exhausted.\n");
190     }
191     return res;
192 }
193
194 char *xstrdup(const char *str)
195 {
196         char *s;
197
198         assert(str != NULL);
199         s = (char *)xmalloc(strlen(str)+1);
200         return strcpy(s, str);
201 }