9c4452fa6af4080057352f67f4a6354b91311955
[reactos.git] / reactos / 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 int 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 return 1;
80 }
81
82 void error_loc(const char *s, ...)
83 {
84 loc_info_t cur_loc = CURRENT_LOCATION;
85 va_list ap;
86 va_start(ap, s);
87 generic_msg(&cur_loc, s, "Error", ap);
88 va_end(ap);
89 exit(1);
90 }
91
92 void error_loc_info(const loc_info_t *loc_info, const char *s, ...)
93 {
94 va_list ap;
95 va_start(ap, s);
96 generic_msg(loc_info, s, "Error", ap);
97 va_end(ap);
98 exit(1);
99 }
100
101 int parser_warning(const char *s, ...)
102 {
103 loc_info_t cur_loc = CURRENT_LOCATION;
104 va_list ap;
105 va_start(ap, s);
106 generic_msg(&cur_loc, s, "Warning", ap);
107 va_end(ap);
108 return 0;
109 }
110
111 void error(const char *s, ...)
112 {
113 va_list ap;
114 va_start(ap, s);
115 fprintf(stderr, "error: ");
116 vfprintf(stderr, s, ap);
117 va_end(ap);
118 exit(2);
119 }
120
121 void warning(const char *s, ...)
122 {
123 va_list ap;
124 va_start(ap, s);
125 fprintf(stderr, "warning: ");
126 vfprintf(stderr, s, ap);
127 va_end(ap);
128 }
129
130 void warning_loc_info(const loc_info_t *loc_info, const char *s, ...)
131 {
132 va_list ap;
133 va_start(ap, s);
134 generic_msg(loc_info, s, "Warning", ap);
135 va_end(ap);
136 }
137
138 void chat(const char *s, ...)
139 {
140 if(debuglevel & DEBUGLEVEL_CHAT)
141 {
142 va_list ap;
143 va_start(ap, s);
144 fprintf(stderr, "chat: ");
145 vfprintf(stderr, s, ap);
146 va_end(ap);
147 }
148 }
149
150 char *dup_basename(const char *name, const char *ext)
151 {
152 int namelen;
153 int extlen = strlen(ext);
154 char *base;
155 char *slash;
156
157 if(!name)
158 name = "widl.tab";
159
160 slash = strrchr(name, '/');
161 if (!slash)
162 slash = strrchr(name, '\\');
163
164 if (slash)
165 name = slash + 1;
166
167 namelen = strlen(name);
168
169 /* +4 for later extension and +1 for '\0' */
170 base = xmalloc(namelen +4 +1);
171 strcpy(base, name);
172 if(!strcasecmp(name + namelen-extlen, ext))
173 {
174 base[namelen - extlen] = '\0';
175 }
176 return base;
177 }
178
179 size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
180 {
181 char *line = *linep;
182 size_t len = *lenp;
183 size_t n = 0;
184
185 if (!line)
186 {
187 len = 64;
188 line = xmalloc(len);
189 }
190
191 while (fgets(&line[n], len - n, fp))
192 {
193 n += strlen(&line[n]);
194 if (line[n - 1] == '\n')
195 break;
196 else if (n == len - 1)
197 {
198 len *= 2;
199 line = xrealloc(line, len);
200 }
201 }
202
203 *linep = line;
204 *lenp = len;
205 return n;
206 }
207
208 void *xmalloc(size_t size)
209 {
210 void *res;
211
212 assert(size > 0);
213 res = malloc(size);
214 if(res == NULL)
215 {
216 error("Virtual memory exhausted.\n");
217 }
218 memset(res, 0x55, size);
219 return res;
220 }
221
222
223 void *xrealloc(void *p, size_t size)
224 {
225 void *res;
226
227 assert(size > 0);
228 res = realloc(p, size);
229 if(res == NULL)
230 {
231 error("Virtual memory exhausted.\n");
232 }
233 return res;
234 }
235
236 char *xstrdup(const char *str)
237 {
238 char *s;
239
240 assert(str != NULL);
241 s = xmalloc(strlen(str)+1);
242 return strcpy(s, str);
243 }