Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Utility routines |
| 3 | * |
| 4 | * Copyright 1998,2000 Bertho A. Stultiens |
| 5 | * |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <string.h> |
| 28 | #include <assert.h> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | #include "wmctypes.h" |
| 32 | #include "utils.h" |
| 33 | #include "wmc.h" |
| 34 | |
| 35 | #define SUPPRESS_YACC_ERROR_MESSAGE |
| 36 | |
| 37 | static void generic_msg(const char *s, const char *t, va_list ap) |
| 38 | { |
Alexandre Julliard | 04ac4be | 2007-01-16 16:00:55 +0100 | [diff] [blame] | 39 | fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 40 | vfprintf(stderr, s, ap); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
| 44 | * The yyerror routine should not exit because we use the error-token |
| 45 | * to determine the syntactic error in the source. However, YACC |
| 46 | * uses the same routine to print an error just before the error |
| 47 | * token is reduced. |
| 48 | * The extra routine 'xyyerror' is used to exit after giving a real |
| 49 | * message. |
| 50 | */ |
Alexandre Julliard | 4f0fa26 | 2006-09-12 09:05:15 +0200 | [diff] [blame] | 51 | int mcy_error(const char *s, ...) |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 52 | { |
| 53 | #ifndef SUPPRESS_YACC_ERROR_MESSAGE |
| 54 | va_list ap; |
| 55 | va_start(ap, s); |
| 56 | generic_msg(s, "Yacc error", ap); |
| 57 | va_end(ap); |
| 58 | #endif |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | int xyyerror(const char *s, ...) |
| 63 | { |
| 64 | va_list ap; |
| 65 | va_start(ap, s); |
| 66 | generic_msg(s, "Error", ap); |
| 67 | va_end(ap); |
| 68 | exit(1); |
| 69 | return 1; |
| 70 | } |
| 71 | |
Alexandre Julliard | 4f0fa26 | 2006-09-12 09:05:15 +0200 | [diff] [blame] | 72 | int mcy_warning(const char *s, ...) |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 73 | { |
| 74 | va_list ap; |
| 75 | va_start(ap, s); |
| 76 | generic_msg(s, "Warning", ap); |
| 77 | va_end(ap); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | void internal_error(const char *file, int line, const char *s, ...) |
| 82 | { |
| 83 | va_list ap; |
| 84 | va_start(ap, s); |
| 85 | fprintf(stderr, "Internal error (please report) %s %d: ", file, line); |
| 86 | vfprintf(stderr, s, ap); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 87 | va_end(ap); |
| 88 | exit(3); |
| 89 | } |
| 90 | |
| 91 | void error(const char *s, ...) |
| 92 | { |
| 93 | va_list ap; |
| 94 | va_start(ap, s); |
| 95 | fprintf(stderr, "Error: "); |
| 96 | vfprintf(stderr, s, ap); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 97 | va_end(ap); |
| 98 | exit(2); |
| 99 | } |
| 100 | |
| 101 | void warning(const char *s, ...) |
| 102 | { |
| 103 | va_list ap; |
| 104 | va_start(ap, s); |
| 105 | fprintf(stderr, "Warning: "); |
| 106 | vfprintf(stderr, s, ap); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 107 | va_end(ap); |
| 108 | } |
| 109 | |
| 110 | char *dup_basename(const char *name, const char *ext) |
| 111 | { |
| 112 | int namelen; |
| 113 | int extlen = strlen(ext); |
| 114 | char *base; |
| 115 | char *slash; |
| 116 | |
| 117 | if(!name) |
| 118 | name = "wmc.tab"; |
| 119 | |
| 120 | slash = strrchr(name, '/'); |
| 121 | if (slash) |
| 122 | name = slash + 1; |
| 123 | |
| 124 | namelen = strlen(name); |
| 125 | |
| 126 | /* +4 for later extension and +1 for '\0' */ |
Alexandre Julliard | 1916cb1 | 2006-08-26 21:35:32 +0200 | [diff] [blame] | 127 | base = xmalloc(namelen +4 +1); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 128 | strcpy(base, name); |
| 129 | if(!strcasecmp(name + namelen-extlen, ext)) |
| 130 | { |
| 131 | base[namelen - extlen] = '\0'; |
| 132 | } |
| 133 | return base; |
| 134 | } |
| 135 | |
| 136 | void *xmalloc(size_t size) |
| 137 | { |
| 138 | void *res; |
| 139 | |
| 140 | assert(size > 0); |
| 141 | assert(size < 102400); |
| 142 | res = malloc(size); |
| 143 | if(res == NULL) |
| 144 | { |
| 145 | error("Virtual memory exhausted.\n"); |
| 146 | } |
Alexandre Julliard | 1659f53 | 2006-08-26 21:29:13 +0200 | [diff] [blame] | 147 | memset(res, 0x55, size); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 148 | return res; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void *xrealloc(void *p, size_t size) |
| 153 | { |
| 154 | void *res; |
| 155 | |
| 156 | assert(size > 0); |
| 157 | assert(size < 102400); |
| 158 | res = realloc(p, size); |
| 159 | if(res == NULL) |
| 160 | { |
| 161 | error("Virtual memory exhausted.\n"); |
| 162 | } |
| 163 | return res; |
| 164 | } |
| 165 | |
| 166 | char *xstrdup(const char *str) |
| 167 | { |
| 168 | char *s; |
| 169 | |
| 170 | assert(str != NULL); |
Alexandre Julliard | 1916cb1 | 2006-08-26 21:35:32 +0200 | [diff] [blame] | 171 | s = xmalloc(strlen(str)+1); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 172 | return strcpy(s, str); |
| 173 | } |
| 174 | |
| 175 | int unistrlen(const WCHAR *s) |
| 176 | { |
| 177 | int n; |
| 178 | for(n = 0; *s; n++, s++) |
| 179 | ; |
| 180 | return n; |
| 181 | } |
| 182 | |
| 183 | WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src) |
| 184 | { |
| 185 | WCHAR *t = dst; |
| 186 | while(*src) |
| 187 | *t++ = *src++; |
| 188 | *t = 0; |
| 189 | return dst; |
| 190 | } |
| 191 | |
| 192 | WCHAR *xunistrdup(const WCHAR * str) |
| 193 | { |
| 194 | WCHAR *s; |
| 195 | |
| 196 | assert(str != NULL); |
Alexandre Julliard | 1916cb1 | 2006-08-26 21:35:32 +0200 | [diff] [blame] | 197 | s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR)); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 198 | return unistrcpy(s, str); |
| 199 | } |
| 200 | |
| 201 | int unistricmp(const WCHAR *s1, const WCHAR *s2) |
| 202 | { |
| 203 | int i; |
| 204 | int once = 0; |
Francois Gouget | cfc3943 | 2004-05-04 04:13:05 +0000 | [diff] [blame] | 205 | static const char warn[] = "Don't know the uppercase equivalent of non acsii characters;" |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 206 | "comparison might yield wrong results"; |
| 207 | while(*s1 && *s2) |
| 208 | { |
| 209 | if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f) |
| 210 | { |
| 211 | if(!once) |
| 212 | { |
| 213 | once++; |
Alexandre Julliard | 4f0fa26 | 2006-09-12 09:05:15 +0200 | [diff] [blame] | 214 | mcy_warning(warn); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 215 | } |
| 216 | i = *s1++ - *s2++; |
| 217 | } |
| 218 | else |
| 219 | i = toupper(*s1++) - toupper(*s2++); |
| 220 | if(i) |
| 221 | return i; |
| 222 | } |
| 223 | |
| 224 | if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f) |
| 225 | { |
| 226 | if(!once) |
Alexandre Julliard | 4f0fa26 | 2006-09-12 09:05:15 +0200 | [diff] [blame] | 227 | mcy_warning(warn); |
Bertho Stultiens | 3085591 | 2000-06-13 04:34:41 +0000 | [diff] [blame] | 228 | return *s1 - *s2; |
| 229 | } |
| 230 | else |
| 231 | return toupper(*s1) - toupper(*s2); |
| 232 | } |
| 233 | |
| 234 | int unistrcmp(const WCHAR *s1, const WCHAR *s2) |
| 235 | { |
| 236 | int i; |
| 237 | while(*s1 && *s2) |
| 238 | { |
| 239 | i = *s1++ - *s2++; |
| 240 | if(i) |
| 241 | return i; |
| 242 | } |
| 243 | |
| 244 | return *s1 - *s2; |
| 245 | } |