4 * Copyright 1998,2000 Bertho A. Stultiens
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.
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.
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
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define SUPPRESS_YACC_ERROR_MESSAGE
36 static void generic_msg(const char *s
, const char *t
, va_list ap
)
38 fprintf(stderr
, "%s:%d:%d: %s: ", input_name
? input_name
: "stdin", line_number
, char_number
, t
);
39 vfprintf(stderr
, s
, ap
);
43 * The yyerror routine should not exit because we use the error-token
44 * to determine the syntactic error in the source. However, YACC
45 * uses the same routine to print an error just before the error
47 * The extra routine 'xyyerror' is used to exit after giving a real
50 int mcy_error(const char *s
, ...)
52 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
55 generic_msg(s
, "Yacc error", ap
);
61 int xyyerror(const char *s
, ...)
65 generic_msg(s
, "Error", ap
);
71 int mcy_warning(const char *s
, ...)
75 generic_msg(s
, "Warning", ap
);
80 void internal_error(const char *file
, int line
, const char *s
, ...)
84 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
85 vfprintf(stderr
, s
, ap
);
90 void fatal_perror( const char *msg
, ... )
93 va_start( valist
, msg
);
94 fprintf(stderr
, "Error: ");
95 vfprintf( stderr
, msg
, valist
);
101 void error(const char *s
, ...)
105 fprintf(stderr
, "Error: ");
106 vfprintf(stderr
, s
, ap
);
111 void warning(const char *s
, ...)
115 fprintf(stderr
, "Warning: ");
116 vfprintf(stderr
, s
, ap
);
120 int unistrlen(const WCHAR
*s
)
123 for(n
= 0; *s
; n
++, s
++)
128 WCHAR
*unistrcpy(WCHAR
*dst
, const WCHAR
*src
)
137 WCHAR
*xunistrdup(const WCHAR
* str
)
142 s
= xmalloc((unistrlen(str
)+1) * sizeof(WCHAR
));
143 return unistrcpy(s
, str
);
146 int unistricmp(const WCHAR
*s1
, const WCHAR
*s2
)
150 static const char warn
[] = "Don't know the uppercase equivalent of non ascii characters;"
151 "comparison might yield wrong results";
154 if((*s1
& 0xffff) > 0x7f || (*s2
& 0xffff) > 0x7f)
164 i
= toupper(*s1
++) - toupper(*s2
++);
169 if((*s1
& 0xffff) > 0x7f || (*s2
& 0xffff) > 0x7f)
176 return toupper(*s1
) - toupper(*s2
);
179 int unistrcmp(const WCHAR
*s1
, const WCHAR
*s2
)
192 WCHAR
*utf8_to_unicode( const char *src
, int srclen
, int *dstlen
)
194 static const char utf8_length
[128] =
196 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
197 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
198 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
199 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
200 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
201 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
202 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
203 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
205 static const unsigned char utf8_mask
[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
207 const char *srcend
= src
+ srclen
;
211 dst
= ret
= xmalloc( (srclen
+ 1) * sizeof(WCHAR
) );
214 unsigned char ch
= *src
++;
215 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
220 len
= utf8_length
[ch
- 0x80];
221 if (len
&& src
+ len
<= srcend
)
223 res
= ch
& utf8_mask
[len
];
227 if ((ch
= *src
^ 0x80) >= 0x40) break;
228 res
= (res
<< 6) | ch
;
230 if (res
< 0x10) break;
232 if ((ch
= *src
^ 0x80) >= 0x40) break;
233 res
= (res
<< 6) | ch
;
234 if (res
>= 0x110000 >> 6) break;
236 if (res
< 0x20) break;
237 if (res
>= 0xd800 >> 6 && res
<= 0xdfff >> 6) break;
239 if ((ch
= *src
^ 0x80) >= 0x40) break;
240 res
= (res
<< 6) | ch
;
242 if (res
< 0x80) break;
243 if (res
<= 0xffff) *dst
++ = res
;
247 *dst
++ = 0xd800 | (res
>> 10);
248 *dst
++ = 0xdc00 | (res
& 0x3ff);
260 char *unicode_to_utf8( const WCHAR
*src
, int srclen
, int *dstlen
)
264 dst
= ret
= xmalloc( srclen
* 3 + 1 );
265 for ( ; srclen
; srclen
--, src
++)
267 unsigned int ch
= *src
;
269 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
274 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
276 dst
[1] = 0x80 | (ch
& 0x3f);
282 if (ch
>= 0xd800 && ch
<= 0xdbff && srclen
> 1 && src
[1] >= 0xdc00 && src
[1] <= 0xdfff)
284 /* 0x10000-0x10ffff: 4 bytes */
285 ch
= 0x10000 + ((ch
& 0x3ff) << 10) + (src
[1] & 0x3ff);
286 dst
[3] = 0x80 | (ch
& 0x3f);
288 dst
[2] = 0x80 | (ch
& 0x3f);
290 dst
[1] = 0x80 | (ch
& 0x3f);
298 if (ch
>= 0xd800 && ch
<= 0xdfff) ch
= 0xfffd; /* invalid surrogate pair */
300 /* 0x800-0xffff: 3 bytes */
301 dst
[2] = 0x80 | (ch
& 0x3f);
303 dst
[1] = 0x80 | (ch
& 0x3f);
315 int is_valid_codepage(int id
)
317 return IsValidCodePage( id
);
320 WCHAR
*codepage_to_unicode( int codepage
, const char *src
, int srclen
, int *dstlen
)
322 WCHAR
*dst
= xmalloc( (srclen
+ 1) * sizeof(WCHAR
) );
323 DWORD ret
= MultiByteToWideChar( codepage
, MB_ERR_INVALID_CHARS
, src
, srclen
, dst
, srclen
);
324 if (!ret
) return NULL
;
334 unsigned short codepage
;
335 unsigned short unidef
;
336 unsigned short trans_unidef
;
337 unsigned short *cp2uni
;
338 unsigned short *dbcs_offsets
;
341 static struct nls_info nlsinfo
[128];
343 static void init_nls_info( struct nls_info
*info
, unsigned short *ptr
)
345 unsigned short hdr_size
= ptr
[0];
347 info
->codepage
= ptr
[1];
348 info
->unidef
= ptr
[4];
349 info
->trans_unidef
= ptr
[6];
351 info
->cp2uni
= ++ptr
;
353 if (*ptr
++) ptr
+= 256; /* glyph table */
354 info
->dbcs_offsets
= *ptr
? ptr
+ 1 : NULL
;
357 static const struct nls_info
*get_nls_info( unsigned int codepage
)
359 unsigned short *data
;
364 for (i
= 0; i
< ARRAY_SIZE(nlsinfo
) && nlsinfo
[i
].codepage
; i
++)
365 if (nlsinfo
[i
].codepage
== codepage
) return &nlsinfo
[i
];
367 assert( i
< ARRAY_SIZE(nlsinfo
) );
369 for (i
= 0; nlsdirs
[i
]; i
++)
371 path
= strmake( "%s/c_%03u.nls", nlsdirs
[i
], codepage
);
372 if ((data
= read_file( path
, &size
)))
375 init_nls_info( &nlsinfo
[i
], data
);
383 int is_valid_codepage(int cp
)
385 return cp
== CP_UTF8
|| get_nls_info( cp
);
388 WCHAR
*codepage_to_unicode( int codepage
, const char *src
, int srclen
, int *dstlen
)
390 const struct nls_info
*info
= get_nls_info( codepage
);
392 WCHAR dbch
, *dst
= xmalloc( (srclen
+ 1) * sizeof(WCHAR
) );
394 if (!info
) error( "codepage %u not supported\n", codepage
);
396 if (info
->dbcs_offsets
)
398 for (i
= 0; srclen
; i
++, srclen
--, src
++)
400 unsigned short off
= info
->dbcs_offsets
[(unsigned char)*src
];
403 if (srclen
== 1) return NULL
;
404 dbch
= (src
[0] << 8) | (unsigned char)src
[1];
407 dst
[i
] = info
->dbcs_offsets
[off
+ (unsigned char)*src
];
408 if (dst
[i
] == info
->unidef
&& dbch
!= info
->trans_unidef
) return NULL
;
412 dst
[i
] = info
->cp2uni
[(unsigned char)*src
];
413 if (dst
[i
] == info
->unidef
&& *src
!= info
->trans_unidef
) return NULL
;
419 for (i
= 0; i
< srclen
; i
++)
421 dst
[i
] = info
->cp2uni
[(unsigned char)src
[i
]];
422 if (dst
[i
] == info
->unidef
&& src
[i
] != info
->trans_unidef
) return NULL
;
432 unsigned char *output_buffer
;
433 size_t output_buffer_pos
;
434 size_t output_buffer_size
;