4 * Copyright 1998 Bertho A. Stultiens
22 /* #define WANT_NEAR_INDICATION */
25 #ifdef WANT_NEAR_INDICATION
26 void make_print(char *str)
37 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
39 fprintf(stderr, "%s %s: %d, %d: ", t, input_name ? input_name : "stdin", line_number, char_number);
40 vfprintf(stderr, s, ap);
41 #ifdef WANT_NEAR_INDICATION
48 fprintf(stderr, " near '%s'", cpy);
53 fprintf(stderr, "\n");
57 int yyerror(const char *s, ...)
61 generic_msg(s, "Error", yytext, ap);
67 int yywarning(const char *s, ...)
71 generic_msg(s, "Warning", yytext, ap);
76 int pperror(const char *s, ...)
80 generic_msg(s, "Error", pptext, ap);
86 int ppwarning(const char *s, ...)
90 generic_msg(s, "Warning", pptext, ap);
96 void internal_error(const char *file, int line, const char *s, ...)
100 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
101 vfprintf(stderr, s, ap);
102 fprintf(stderr, "\n");
107 void error(const char *s, ...)
111 fprintf(stderr, "Error: ");
112 vfprintf(stderr, s, ap);
113 fprintf(stderr, "\n");
118 void warning(const char *s, ...)
122 fprintf(stderr, "Warning: ");
123 vfprintf(stderr, s, ap);
124 fprintf(stderr, "\n");
128 void chat(const char *s, ...)
130 if(debuglevel & DEBUGLEVEL_CHAT)
134 fprintf(stderr, "FYI: ");
135 vfprintf(stderr, s, ap);
136 fprintf(stderr, "\n");
141 char *dup_basename(const char *name, const char *ext)
144 int extlen = strlen(ext);
151 slash = strrchr(name, '/');
155 namelen = strlen(name);
157 /* +4 for later extension and +1 for '\0' */
158 base = (char *)xmalloc(namelen +4 +1);
160 if(!strcasecmp(name + namelen-extlen, ext))
162 base[namelen - extlen] = '\0';
167 void *xmalloc(size_t size)
172 assert(size < 102400);
176 error("Virtual memory exhausted.\n");
180 * This is *paramount* because we depend on it
181 * just about everywhere in the rest of the code.
183 memset(res, 0, size);
188 void *xrealloc(void *p, size_t size)
193 assert(size < 102400);
194 res = realloc(p, size);
197 error("Virtual memory exhausted.\n");
202 char *xstrdup(const char *str)
207 s = (char *)xmalloc(strlen(str)+1);
208 return strcpy(s, str);
211 int string_compare(const string_t *s1, const string_t *s2)
213 if(s1->type == str_char && s2->type == str_char)
215 return strcasecmp(s1->str.cstr, s2->str.cstr);
219 internal_error(__FILE__, __LINE__, "Cannot yet compare unicode strings");
224 int wstrlen(const short *s)
232 short *wstrcpy(short *dst, const short *src)
240 int wstricmp(const short *s1, const short *s2)
242 char *cs1 = dupwstr2cstr(s1);
243 char *cs2 = dupwstr2cstr(s2);
244 int retval = strcasecmp(cs1, cs2);
247 warning("Comparing unicode strings without case -> converting to ascii");
251 short *dupcstr2wstr(const char *str)
253 int len = strlen(str) + 1;
254 short *ws = (short *)xmalloc(len*2);
258 /* FIXME: codepage translation */
260 *wptr++ = (short)(*str++ & 0xff);
265 char *dupwstr2cstr(const short *str)
267 int len = wstrlen(str) + 1;
268 char *cs = (char *)xmalloc(len);
272 /* FIXME: codepage translation */
274 *cptr++ = (char)*str++;
280 *****************************************************************************
281 * Function : compare_name_id
282 * Syntax : int compare_name_id(name_id_t *n1, name_id_t *n2)
287 *****************************************************************************
289 int compare_name_id(name_id_t *n1, name_id_t *n2)
291 if(n1->type == name_ord && n2->type == name_ord)
293 return n1->name.i_name - n2->name.i_name;
295 else if(n1->type == name_str && n2->type == name_str)
297 if(n1->name.s_name->type == str_char
298 && n2->name.s_name->type == str_char)
300 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
302 else if(n1->name.s_name->type == str_unicode
303 && n2->name.s_name->type == str_unicode)
305 return wstricmp(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
309 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
312 else if(n1->type == name_ord && n2->type == name_str)
314 else if(n1->type == name_str && n2->type == name_ord)
317 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
320 return 0; /* Keep the compiler happy */