widl: Make sure format strings for structure and union types are only output once.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35
36 #define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text }
37
38 static const int want_near_indication = 0;
39
40 static void make_print(char *str)
41 {
42         while(*str)
43         {
44                 if(!isprint(*str))
45                         *str = ' ';
46                 str++;
47         }
48 }
49
50 static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
51 {
52         fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t);
53         vfprintf(stderr, s, ap);
54
55         if (want_near_indication)
56         {
57                 char *cpy;
58                 if(loc_info->near_text)
59                 {
60                         cpy = xstrdup(loc_info->near_text);
61                         make_print(cpy);
62                         fprintf(stderr, " near '%s'", cpy);
63                         free(cpy);
64                 }
65         }
66 }
67
68
69 /* yyerror:  yacc assumes this is not newline terminated.  */
70 void parser_error(const char *s, ...)
71 {
72         loc_info_t cur_location = CURRENT_LOCATION;
73         va_list ap;
74         va_start(ap, s);
75         generic_msg(&cur_location, s, "Error", ap);
76         fprintf(stderr, "\n");
77         va_end(ap);
78         exit(1);
79 }
80
81 void error_loc(const char *s, ...)
82 {
83         loc_info_t cur_loc = CURRENT_LOCATION;
84         va_list ap;
85         va_start(ap, s);
86         generic_msg(&cur_loc, s, "Error", ap);
87         va_end(ap);
88         exit(1);
89 }
90
91 void error_loc_info(const loc_info_t *loc_info, const char *s, ...)
92 {
93         va_list ap;
94         va_start(ap, s);
95         generic_msg(loc_info, s, "Error", ap);
96         va_end(ap);
97         exit(1);
98 }
99
100 int parser_warning(const char *s, ...)
101 {
102         loc_info_t cur_loc = CURRENT_LOCATION;
103         va_list ap;
104         va_start(ap, s);
105         generic_msg(&cur_loc, s, "Warning", ap);
106         va_end(ap);
107         return 0;
108 }
109
110 void error(const char *s, ...)
111 {
112         va_list ap;
113         va_start(ap, s);
114         fprintf(stderr, "error: ");
115         vfprintf(stderr, s, ap);
116         va_end(ap);
117         exit(2);
118 }
119
120 void warning(const char *s, ...)
121 {
122         va_list ap;
123         va_start(ap, s);
124         fprintf(stderr, "warning: ");
125         vfprintf(stderr, s, ap);
126         va_end(ap);
127 }
128
129 void warning_loc_info(const loc_info_t *loc_info, const char *s, ...)
130 {
131         va_list ap;
132         va_start(ap, s);
133         generic_msg(loc_info, s, "Warning", ap);
134         va_end(ap);
135 }
136
137 void chat(const char *s, ...)
138 {
139         if(debuglevel & DEBUGLEVEL_CHAT)
140         {
141                 va_list ap;
142                 va_start(ap, s);
143                 fprintf(stderr, "chat: ");
144                 vfprintf(stderr, s, ap);
145                 va_end(ap);
146         }
147 }
148
149 char *dup_basename(const char *name, const char *ext)
150 {
151         int namelen;
152         int extlen = strlen(ext);
153         char *base;
154         char *slash;
155
156         if(!name)
157                 name = "widl.tab";
158
159         slash = strrchr(name, '/');
160         if (!slash)
161                 slash = strrchr(name, '\\');
162
163         if (slash)
164                 name = slash + 1;
165
166         namelen = strlen(name);
167
168         /* +4 for later extension and +1 for '\0' */
169         base = xmalloc(namelen +4 +1);
170         strcpy(base, name);
171         if(!strcasecmp(name + namelen-extlen, ext))
172         {
173                 base[namelen - extlen] = '\0';
174         }
175         return base;
176 }
177
178 size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
179 {
180     char *line = *linep;
181     size_t len = *lenp;
182     size_t n = 0;
183
184     if (!line)
185     {
186         len = 64;
187         line = xmalloc(len);
188     }
189
190     while (fgets(&line[n], len - n, fp))
191     {
192         n += strlen(&line[n]);
193         if (line[n - 1] == '\n')
194             break;
195         else if (n == len - 1)
196         {
197             len *= 2;
198             line = xrealloc(line, len);
199         }
200     }
201
202     *linep = line;
203     *lenp = len;
204     return n;
205 }
206
207 void *xmalloc(size_t size)
208 {
209     void *res;
210
211     assert(size > 0);
212     res = malloc(size);
213     if(res == NULL)
214     {
215         error("Virtual memory exhausted.\n");
216     }
217     memset(res, 0x55, size);
218     return res;
219 }
220
221
222 void *xrealloc(void *p, size_t size)
223 {
224     void *res;
225
226     assert(size > 0);
227     res = realloc(p, size);
228     if(res == NULL)
229     {
230         error("Virtual memory exhausted.\n");
231     }
232     return res;
233 }
234
235 char *xstrdup(const char *str)
236 {
237         char *s;
238
239         assert(str != NULL);
240         s = xmalloc(strlen(str)+1);
241         return strcpy(s, str);
242 }