ed950f6095d3e1ed3586796586d5d828a6ac46ab
[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 static const int want_near_indication = 0;
37
38 static void make_print(char *str)
39 {
40 while(*str)
41 {
42 if(!isprint(*str))
43 *str = ' ';
44 str++;
45 }
46 }
47
48 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
49 {
50 fprintf(stderr, "%s:%d: %s: ", input_name ? input_name : "stdin", line_number, t);
51 vfprintf(stderr, s, ap);
52
53 if (want_near_indication)
54 {
55 char *cpy;
56 if(n)
57 {
58 cpy = xstrdup(n);
59 make_print(cpy);
60 fprintf(stderr, " near '%s'", cpy);
61 free(cpy);
62 }
63 }
64 }
65
66
67 /* yyerror: yacc assumes this is not newline terminated. */
68 int parser_error(const char *s, ...)
69 {
70 va_list ap;
71 va_start(ap, s);
72 generic_msg(s, "Error", parser_text, ap);
73 fprintf(stderr, "\n");
74 va_end(ap);
75 exit(1);
76 return 1;
77 }
78
79 void error_loc(const char *s, ...)
80 {
81 va_list ap;
82 va_start(ap, s);
83 generic_msg(s, "Error", parser_text, ap);
84 va_end(ap);
85 exit(1);
86 }
87
88 int parser_warning(const char *s, ...)
89 {
90 va_list ap;
91 va_start(ap, s);
92 generic_msg(s, "Warning", parser_text, ap);
93 va_end(ap);
94 return 0;
95 }
96
97 void error(const char *s, ...)
98 {
99 va_list ap;
100 va_start(ap, s);
101 fprintf(stderr, "error: ");
102 vfprintf(stderr, s, ap);
103 va_end(ap);
104 exit(2);
105 }
106
107 void warning(const char *s, ...)
108 {
109 va_list ap;
110 va_start(ap, s);
111 fprintf(stderr, "warning: ");
112 vfprintf(stderr, s, ap);
113 va_end(ap);
114 }
115
116 void chat(const char *s, ...)
117 {
118 if(debuglevel & DEBUGLEVEL_CHAT)
119 {
120 va_list ap;
121 va_start(ap, s);
122 fprintf(stderr, "chat: ");
123 vfprintf(stderr, s, ap);
124 va_end(ap);
125 }
126 }
127
128 char *dup_basename(const char *name, const char *ext)
129 {
130 int namelen;
131 int extlen = strlen(ext);
132 char *base;
133 char *slash;
134
135 if(!name)
136 name = "widl.tab";
137
138 slash = strrchr(name, '/');
139 if (!slash)
140 slash = strrchr(name, '\\');
141
142 if (slash)
143 name = slash + 1;
144
145 namelen = strlen(name);
146
147 /* +4 for later extension and +1 for '\0' */
148 base = xmalloc(namelen +4 +1);
149 strcpy(base, name);
150 if(!strcasecmp(name + namelen-extlen, ext))
151 {
152 base[namelen - extlen] = '\0';
153 }
154 return base;
155 }
156
157 size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
158 {
159 char *line = *linep;
160 size_t len = *lenp;
161 size_t n = 0;
162
163 if (!line)
164 {
165 len = 64;
166 line = xmalloc(len);
167 }
168
169 while (fgets(&line[n], len - n, fp))
170 {
171 n += strlen(&line[n]);
172 if (line[n - 1] == '\n')
173 break;
174 else if (n == len - 1)
175 {
176 len *= 2;
177 line = xrealloc(line, len);
178 }
179 }
180
181 *linep = line;
182 *lenp = len;
183 return n;
184 }
185
186 void *xmalloc(size_t size)
187 {
188 void *res;
189
190 assert(size > 0);
191 res = malloc(size);
192 if(res == NULL)
193 {
194 error("Virtual memory exhausted.\n");
195 }
196 memset(res, 0x55, size);
197 return res;
198 }
199
200
201 void *xrealloc(void *p, size_t size)
202 {
203 void *res;
204
205 assert(size > 0);
206 res = realloc(p, size);
207 if(res == NULL)
208 {
209 error("Virtual memory exhausted.\n");
210 }
211 return res;
212 }
213
214 char *xstrdup(const char *str)
215 {
216 char *s;
217
218 assert(str != NULL);
219 s = xmalloc(strlen(str)+1);
220 return strcpy(s, str);
221 }