4 * Copyright 1998 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
36 /* #define WANT_NEAR_INDICATION */
38 #ifdef WANT_NEAR_INDICATION
39 void make_print(char *str
)
50 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
52 fprintf(stderr
, "%s:%d:%d: %s: ", input_name
? input_name
: "stdin", line_number
, char_number
, t
);
53 vfprintf(stderr
, s
, ap
);
54 #ifdef WANT_NEAR_INDICATION
61 fprintf(stderr
, " near '%s'", cpy
);
69 int parser_error(const char *s
, ...)
73 generic_msg(s
, "Error", parser_text
, ap
);
74 fputc( '\n', stderr
);
80 int parser_warning(const char *s
, ...)
84 generic_msg(s
, "Warning", parser_text
, ap
);
89 void fatal_perror( const char *msg
, ... )
92 va_start( valist
, msg
);
93 fprintf(stderr
, "Error: ");
94 vfprintf( stderr
, msg
, valist
);
100 void error(const char *s
, ...)
104 fprintf(stderr
, "Error: ");
105 vfprintf(stderr
, s
, ap
);
110 void warning(const char *s
, ...)
114 fprintf(stderr
, "Warning: ");
115 vfprintf(stderr
, s
, ap
);
119 void chat(const char *s
, ...)
121 if(debuglevel
& DEBUGLEVEL_CHAT
)
125 fprintf(stderr
, "FYI: ");
126 vfprintf(stderr
, s
, ap
);
131 int compare_striA( const char *str1
, const char *str2
)
135 /* only the A-Z range is case-insensitive */
136 char ch1
= (*str1
>= 'a' && *str1
<= 'z') ? *str1
+ 'A' - 'a' : *str1
;
137 char ch2
= (*str2
>= 'a' && *str2
<= 'z') ? *str2
+ 'A' - 'a' : *str2
;
138 if (!ch1
|| ch1
!= ch2
) return ch1
- ch2
;
144 int compare_striW( const WCHAR
*str1
, const WCHAR
*str2
)
148 /* only the A-Z range is case-insensitive */
149 WCHAR ch1
= (*str1
>= 'a' && *str1
<= 'z') ? *str1
+ 'A' - 'a' : *str1
;
150 WCHAR ch2
= (*str2
>= 'a' && *str2
<= 'z') ? *str2
+ 'A' - 'a' : *str2
;
151 if (!ch1
|| ch1
!= ch2
) return ch1
- ch2
;
157 int compare_striAW( const char *str1
, const WCHAR
*str2
)
161 /* only the A-Z range is case-insensitive */
162 WCHAR ch1
= (*str1
>= 'a' && *str1
<= 'z') ? *str1
+ 'A' - 'a' : (unsigned char)*str1
;
163 WCHAR ch2
= (*str2
>= 'a' && *str2
<= 'z') ? *str2
+ 'A' - 'a' : *str2
;
164 if (!ch1
|| ch1
!= ch2
) return ch1
- ch2
;
171 *****************************************************************************
172 * Function : compare_name_id
173 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
178 *****************************************************************************
180 int compare_name_id(const name_id_t
*n1
, const name_id_t
*n2
)
182 if (n1
->type
!= n2
->type
) return n1
->type
== name_ord
? 1 : -1;
183 if (n1
->type
== name_ord
) return n1
->name
.i_name
- n2
->name
.i_name
;
185 if (n1
->name
.s_name
->type
== str_char
)
187 if (n2
->name
.s_name
->type
== str_char
)
188 return compare_striA(n1
->name
.s_name
->str
.cstr
, n2
->name
.s_name
->str
.cstr
);
189 return compare_striAW(n1
->name
.s_name
->str
.cstr
, n2
->name
.s_name
->str
.wstr
);
193 if (n2
->name
.s_name
->type
== str_char
)
194 return -compare_striAW(n2
->name
.s_name
->str
.cstr
, n1
->name
.s_name
->str
.wstr
);
195 return compare_striW(n1
->name
.s_name
->str
.wstr
, n2
->name
.s_name
->str
.wstr
);
201 int is_valid_codepage(int id
)
203 return IsValidCodePage( id
);
206 static WCHAR
*codepage_to_unicode( int codepage
, const char *src
, int srclen
, int *dstlen
)
208 WCHAR
*dst
= xmalloc( (srclen
+ 1) * sizeof(WCHAR
) );
209 DWORD ret
= MultiByteToWideChar( codepage
, MB_ERR_INVALID_CHARS
, src
, srclen
, dst
, srclen
);
210 if (!ret
) return NULL
;
216 int get_language_codepage( language_t lang
)
220 if (!lang
) return 1252;
221 if (!GetLocaleInfoW( lang
, LOCALE_IDEFAULTANSICODEPAGE
| LOCALE_RETURN_NUMBER
,
222 (WCHAR
*)&codepage
, sizeof(codepage
)/sizeof(WCHAR
) )) return -1;
230 unsigned short codepage
;
231 unsigned short unidef
;
232 unsigned short trans_unidef
;
233 unsigned short *cp2uni
;
234 unsigned short *dbcs_offsets
;
237 static struct nls_info nlsinfo
[128];
239 static void init_nls_info( struct nls_info
*info
, unsigned short *ptr
)
241 unsigned short hdr_size
= ptr
[0];
243 info
->codepage
= ptr
[1];
244 info
->unidef
= ptr
[4];
245 info
->trans_unidef
= ptr
[6];
247 info
->cp2uni
= ++ptr
;
249 if (*ptr
++) ptr
+= 256; /* glyph table */
250 info
->dbcs_offsets
= *ptr
? ptr
+ 1 : NULL
;
253 static const struct nls_info
*get_nls_info( unsigned int codepage
)
255 unsigned short *data
;
260 for (i
= 0; i
< ARRAY_SIZE(nlsinfo
) && nlsinfo
[i
].codepage
; i
++)
261 if (nlsinfo
[i
].codepage
== codepage
) return &nlsinfo
[i
];
263 assert( i
< ARRAY_SIZE(nlsinfo
) );
265 for (i
= 0; nlsdirs
[i
]; i
++)
267 path
= strmake( "%s/c_%03u.nls", nlsdirs
[i
], codepage
);
268 if ((data
= read_file( path
, &size
)))
271 init_nls_info( &nlsinfo
[i
], data
);
279 int is_valid_codepage(int cp
)
281 return cp
== CP_UTF8
|| get_nls_info( cp
);
284 static WCHAR
*codepage_to_unicode( int codepage
, const char *src
, int srclen
, int *dstlen
)
286 const struct nls_info
*info
= get_nls_info( codepage
);
288 WCHAR dbch
, *dst
= xmalloc( (srclen
+ 1) * sizeof(WCHAR
) );
290 if (!info
) error( "codepage %u not supported\n", codepage
);
292 if (info
->dbcs_offsets
)
294 for (i
= 0; srclen
; i
++, srclen
--, src
++)
296 unsigned short off
= info
->dbcs_offsets
[(unsigned char)*src
];
299 if (srclen
== 1) return NULL
;
300 dbch
= (src
[0] << 8) | (unsigned char)src
[1];
303 dst
[i
] = info
->dbcs_offsets
[off
+ (unsigned char)*src
];
304 if (dst
[i
] == info
->unidef
&& dbch
!= info
->trans_unidef
) return NULL
;
308 dst
[i
] = info
->cp2uni
[(unsigned char)*src
];
309 if (dst
[i
] == info
->unidef
&& *src
!= info
->trans_unidef
) return NULL
;
315 for (i
= 0; i
< srclen
; i
++)
317 dst
[i
] = info
->cp2uni
[(unsigned char)src
[i
]];
318 if (dst
[i
] == info
->unidef
&& src
[i
] != info
->trans_unidef
) return NULL
;
326 static const NLS_LOCALE_LCID_INDEX
*lcids_index
;
327 static const NLS_LOCALE_HEADER
*locale_table
;
328 static const NLS_LOCALE_LCNAME_INDEX
*lcnames_index
;
329 static const WCHAR
*locale_strings
;
331 static void load_locale_nls(void)
336 unsigned int unknown1
;
337 unsigned int unknown2
;
338 unsigned int unknown3
;
339 unsigned int locales
;
340 unsigned int charmaps
;
342 unsigned int scripts
;
348 for (i
= 0; nlsdirs
[i
]; i
++)
350 path
= strmake( "%s/locale.nls", nlsdirs
[i
] );
351 header
= read_file( path
, &size
);
353 if (!header
) continue;
354 locale_table
= (const NLS_LOCALE_HEADER
*)((char *)header
+ header
->locales
);
355 lcids_index
= (const NLS_LOCALE_LCID_INDEX
*)((char *)locale_table
+ locale_table
->lcids_offset
);
356 lcnames_index
= (const NLS_LOCALE_LCNAME_INDEX
*)((char *)locale_table
+ locale_table
->lcnames_offset
);
357 locale_strings
= (const WCHAR
*)((char *)locale_table
+ locale_table
->strings_offset
);
360 error( "unable to load locale.nls\n" );
363 static const NLS_LOCALE_LCID_INDEX
*find_lcid_entry( LCID lcid
)
365 int min
= 0, max
= locale_table
->nb_lcids
- 1;
369 int pos
= (min
+ max
) / 2;
370 if (lcid
< lcids_index
[pos
].id
) max
= pos
- 1;
371 else if (lcid
> lcids_index
[pos
].id
) min
= pos
+ 1;
372 else return &lcids_index
[pos
];
377 static const NLS_LOCALE_DATA
*get_locale_data( UINT idx
)
379 ULONG offset
= locale_table
->locales_offset
+ idx
* locale_table
->locale_size
;
380 return (const NLS_LOCALE_DATA
*)((const char *)locale_table
+ offset
);
383 int get_language_codepage( language_t lang
)
385 const NLS_LOCALE_LCID_INDEX
*entry
;
387 if (!lang
) return 1252;
388 if (lang
== MAKELANGID( LANG_ENGLISH
, SUBLANG_DEFAULT
)) return 1252;
389 if (!locale_table
) load_locale_nls();
390 if (!(entry
= find_lcid_entry( lang
))) return -1;
391 return get_locale_data( entry
->idx
)->idefaultansicodepage
;
396 static WCHAR
*utf8_to_unicode( const char *src
, int srclen
, int *dstlen
)
398 static const char utf8_length
[128] =
400 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
401 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
402 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
403 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
404 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
405 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
406 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
407 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
409 static const unsigned char utf8_mask
[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
411 const char *srcend
= src
+ srclen
;
415 dst
= ret
= xmalloc( (srclen
+ 1) * sizeof(WCHAR
) );
418 unsigned char ch
= *src
++;
419 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
424 len
= utf8_length
[ch
- 0x80];
425 if (len
&& src
+ len
<= srcend
)
427 res
= ch
& utf8_mask
[len
];
431 if ((ch
= *src
^ 0x80) >= 0x40) break;
432 res
= (res
<< 6) | ch
;
434 if (res
< 0x10) break;
436 if ((ch
= *src
^ 0x80) >= 0x40) break;
437 res
= (res
<< 6) | ch
;
438 if (res
>= 0x110000 >> 6) break;
440 if (res
< 0x20) break;
441 if (res
>= 0xd800 >> 6 && res
<= 0xdfff >> 6) break;
443 if ((ch
= *src
^ 0x80) >= 0x40) break;
444 res
= (res
<< 6) | ch
;
446 if (res
< 0x80) break;
447 if (res
<= 0xffff) *dst
++ = res
;
451 *dst
++ = 0xd800 | (res
>> 10);
452 *dst
++ = 0xdc00 | (res
& 0x3ff);
464 static char *unicode_to_utf8( const WCHAR
*src
, int srclen
, int *dstlen
)
468 dst
= ret
= xmalloc( srclen
* 3 + 1 );
469 for ( ; srclen
; srclen
--, src
++)
471 unsigned int ch
= *src
;
473 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
478 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
480 dst
[1] = 0x80 | (ch
& 0x3f);
486 if (ch
>= 0xd800 && ch
<= 0xdbff && srclen
> 1 && src
[1] >= 0xdc00 && src
[1] <= 0xdfff)
488 /* 0x10000-0x10ffff: 4 bytes */
489 ch
= 0x10000 + ((ch
& 0x3ff) << 10) + (src
[1] & 0x3ff);
490 dst
[3] = 0x80 | (ch
& 0x3f);
492 dst
[2] = 0x80 | (ch
& 0x3f);
494 dst
[1] = 0x80 | (ch
& 0x3f);
502 if (ch
>= 0xd800 && ch
<= 0xdfff) ch
= 0xfffd; /* invalid surrogate pair */
504 /* 0x800-0xffff: 3 bytes */
505 dst
[2] = 0x80 | (ch
& 0x3f);
507 dst
[1] = 0x80 | (ch
& 0x3f);
517 string_t
*convert_string_unicode( const string_t
*str
, int codepage
)
519 string_t
*ret
= xmalloc(sizeof(*ret
));
521 ret
->type
= str_unicode
;
524 if (str
->type
== str_char
)
526 if (!codepage
) parser_error( "Current language is Unicode only, cannot convert string" );
528 if (codepage
== CP_UTF8
)
529 ret
->str
.wstr
= utf8_to_unicode( str
->str
.cstr
, str
->size
, &ret
->size
);
531 ret
->str
.wstr
= codepage_to_unicode( codepage
, str
->str
.cstr
, str
->size
, &ret
->size
);
532 if (!ret
->str
.wstr
) parser_error( "Invalid character in string '%.*s' for codepage %u",
533 str
->size
, str
->str
.cstr
, codepage
);
537 ret
->size
= str
->size
;
538 ret
->str
.wstr
= xmalloc(sizeof(WCHAR
)*(ret
->size
+1));
539 memcpy( ret
->str
.wstr
, str
->str
.wstr
, ret
->size
* sizeof(WCHAR
) );
540 ret
->str
.wstr
[ret
->size
] = 0;
545 char *convert_string_utf8( const string_t
*str
, int codepage
)
548 string_t
*wstr
= convert_string_unicode( str
, codepage
);
549 char *ret
= unicode_to_utf8( wstr
->str
.wstr
, wstr
->size
, &len
);
554 void free_string(string_t
*str
)
556 if (str
->type
== str_unicode
) free( str
->str
.wstr
);
557 else free( str
->str
.cstr
);
561 /* check if the string is valid utf8 despite a different codepage being in use */
562 int check_valid_utf8( const string_t
*str
, int codepage
)
567 if (!check_utf8
) return 0;
568 if (!codepage
) return 0;
569 if (codepage
== CP_UTF8
) return 0;
570 if (!is_valid_codepage( codepage
)) return 0;
572 for (i
= count
= 0; i
< str
->size
; i
++)
574 if ((unsigned char)str
->str
.cstr
[i
] >= 0xf5) goto done
;
575 if ((unsigned char)str
->str
.cstr
[i
] >= 0xc2) { count
++; continue; }
576 if ((unsigned char)str
->str
.cstr
[i
] >= 0x80) goto done
;
578 if (!count
) return 0; /* no 8-bit chars at all */
580 wstr
= utf8_to_unicode( str
->str
.cstr
, str
->size
, &count
);
581 for (i
= 0; i
< count
; i
++) if (wstr
[i
] == 0xfffd) break;
586 check_utf8
= 0; /* at least one 8-bit non-utf8 string found, stop checking */
590 const char *get_nameid_str(const name_id_t
*n
)
594 if (!n
) return "<none>";
595 if (n
->type
== name_ord
) return strmake( "%u", n
->name
.i_name
);
596 if (n
->name
.s_name
->type
== str_char
) return n
->name
.s_name
->str
.cstr
;
597 return unicode_to_utf8( n
->name
.s_name
->str
.wstr
, n
->name
.s_name
->size
, &len
);