cabinet: FCICreate: Initialize oldCCAB.
[wine.git] / tools / widl / utils.c
blob7dafc10635d247a1e73629e17afe9f01e7834a39
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 /* yyerror: yacc assumes this is not newline terminated. */
69 int parser_error(const char *s, ...)
71 va_list ap;
72 va_start(ap, s);
73 generic_msg(s, "Error", parser_text, ap);
74 fprintf(stderr, "\n");
75 va_end(ap);
76 exit(1);
77 return 1;
80 void error_loc(const char *s, ...)
82 va_list ap;
83 va_start(ap, s);
84 generic_msg(s, "Error", parser_text, ap);
85 va_end(ap);
86 exit(1);
89 int parser_warning(const char *s, ...)
91 va_list ap;
92 va_start(ap, s);
93 generic_msg(s, "Warning", parser_text, ap);
94 va_end(ap);
95 return 0;
98 void error(const char *s, ...)
100 va_list ap;
101 va_start(ap, s);
102 fprintf(stderr, "error: ");
103 vfprintf(stderr, s, ap);
104 va_end(ap);
105 exit(2);
108 void warning(const char *s, ...)
110 va_list ap;
111 va_start(ap, s);
112 fprintf(stderr, "warning: ");
113 vfprintf(stderr, s, ap);
114 va_end(ap);
117 void chat(const char *s, ...)
119 if(debuglevel & DEBUGLEVEL_CHAT)
121 va_list ap;
122 va_start(ap, s);
123 fprintf(stderr, "chat: ");
124 vfprintf(stderr, s, ap);
125 va_end(ap);
129 char *dup_basename(const char *name, const char *ext)
131 int namelen;
132 int extlen = strlen(ext);
133 char *base;
134 char *slash;
136 if(!name)
137 name = "widl.tab";
139 slash = strrchr(name, '/');
140 if (slash)
141 name = slash + 1;
143 namelen = strlen(name);
145 /* +4 for later extension and +1 for '\0' */
146 base = xmalloc(namelen +4 +1);
147 strcpy(base, name);
148 if(!strcasecmp(name + namelen-extlen, ext))
150 base[namelen - extlen] = '\0';
152 return base;
155 size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
157 char *line = *linep;
158 size_t len = *lenp;
159 size_t n = 0;
161 if (!line)
163 len = 64;
164 line = xmalloc(len);
167 while (fgets(&line[n], len - n, fp))
169 n += strlen(&line[n]);
170 if (line[n - 1] == '\n')
171 break;
172 else if (n == len - 1)
174 len *= 2;
175 line = xrealloc(line, len);
179 *linep = line;
180 *lenp = len;
181 return n;
184 void *xmalloc(size_t size)
186 void *res;
188 assert(size > 0);
189 res = malloc(size);
190 if(res == NULL)
192 error("Virtual memory exhausted.\n");
194 memset(res, 0x55, size);
195 return res;
199 void *xrealloc(void *p, size_t size)
201 void *res;
203 assert(size > 0);
204 res = realloc(p, size);
205 if(res == NULL)
207 error("Virtual memory exhausted.\n");
209 return res;
212 char *xstrdup(const char *str)
214 char *s;
216 assert(str != NULL);
217 s = xmalloc(strlen(str)+1);
218 return strcpy(s, str);