push 9ed7f32abbb93ea3a813fd6b1650e5bcfc506606
[wine/hacks.git] / tools / widl / utils.c
blob1382597dfea3ec305803b2ad33b5564d4081b8cd
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 static const int want_near_indication = 0;
39 static void make_print(char *str)
41 while(*str)
43 if(!isprint(*str))
44 *str = ' ';
45 str++;
49 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
51 fprintf(stderr, "%s:%d: %s: ", input_name ? input_name : "stdin", line_number, t);
52 vfprintf(stderr, s, ap);
54 if (want_near_indication)
56 char *cpy;
57 if(n)
59 cpy = xstrdup(n);
60 make_print(cpy);
61 fprintf(stderr, " near '%s'", cpy);
62 free(cpy);
68 int parser_error(const char *s, ...)
70 va_list ap;
71 va_start(ap, s);
72 generic_msg(s, "Error", parser_text, ap);
73 va_end(ap);
74 exit(1);
75 return 1;
78 int parser_warning(const char *s, ...)
80 va_list ap;
81 va_start(ap, s);
82 generic_msg(s, "Warning", parser_text, ap);
83 va_end(ap);
84 return 0;
87 void error(const char *s, ...)
89 va_list ap;
90 va_start(ap, s);
91 fprintf(stderr, "error: ");
92 vfprintf(stderr, s, ap);
93 va_end(ap);
94 exit(2);
97 void warning(const char *s, ...)
99 va_list ap;
100 va_start(ap, s);
101 fprintf(stderr, "warning: ");
102 vfprintf(stderr, s, ap);
103 va_end(ap);
106 void chat(const char *s, ...)
108 if(debuglevel & DEBUGLEVEL_CHAT)
110 va_list ap;
111 va_start(ap, s);
112 fprintf(stderr, "chat: ");
113 vfprintf(stderr, s, ap);
114 va_end(ap);
118 char *dup_basename(const char *name, const char *ext)
120 int namelen;
121 int extlen = strlen(ext);
122 char *base;
123 char *slash;
125 if(!name)
126 name = "widl.tab";
128 slash = strrchr(name, '/');
129 if (slash)
130 name = slash + 1;
132 namelen = strlen(name);
134 /* +4 for later extension and +1 for '\0' */
135 base = xmalloc(namelen +4 +1);
136 strcpy(base, name);
137 if(!strcasecmp(name + namelen-extlen, ext))
139 base[namelen - extlen] = '\0';
141 return base;
144 size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
146 char *line = *linep;
147 size_t len = *lenp;
148 size_t n = 0;
150 if (!line)
152 len = 64;
153 line = xmalloc(len);
156 while (fgets(&line[n], len - n, fp))
158 n += strlen(&line[n]);
159 if (line[n - 1] == '\n')
160 break;
161 else if (n == len - 1)
163 len *= 2;
164 line = xrealloc(line, len);
168 *linep = line;
169 *lenp = len;
170 return n;
173 void *xmalloc(size_t size)
175 void *res;
177 assert(size > 0);
178 res = malloc(size);
179 if(res == NULL)
181 error("Virtual memory exhausted.\n");
183 memset(res, 0x55, size);
184 return res;
188 void *xrealloc(void *p, size_t size)
190 void *res;
192 assert(size > 0);
193 res = realloc(p, size);
194 if(res == NULL)
196 error("Virtual memory exhausted.\n");
198 return res;
201 char *xstrdup(const char *str)
203 char *s;
205 assert(str != NULL);
206 s = xmalloc(strlen(str)+1);
207 return strcpy(s, str);