/*
* Copyright (C) 1999 Thomas Roessler <roessler@guug.de>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
* 02139, USA.
*/
/*
* This module deals with POSIX.2 character set definition files.
*/
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include "mutt.h"
#include "charset.h"
/* Define this if you want any dprint () statements in this code */
#undef CHARSET_DEBUG
#ifndef CHARSET_DEBUG
# undef dprint
# define dprint(a, b) (void) a
#endif
/* Module-global variables */
static HASH *Translations = NULL;
static HASH *Charsets = NULL;
static HASH *CharsetAliases = NULL;
/* Function Prototypes */
static CHARDESC *chardesc_new (void);
static CHARDESC *repr2descr (int repr, CHARSET * cs);
static CHARMAP *charmap_new (void);
static CHARMAP *parse_charmap_header (FILE * fp);
static CHARSET *charset_new (size_t hash_size);
static CHARSET_MAP *build_translation (CHARSET * from, CHARSET * to);
static char translate_character (CHARSET * to, const char *symbol);
static int load_charset (const char *filename, CHARSET ** csp, short multbyte);
static int parse_charmap_line (char *line, CHARMAP * m, CHARDESC ** descrp);
static int _cd_compar (const void *a, const void *b);
static void canonical_charset (char *dest, size_t dlen, const char *name);
static void chardesc_free (CHARDESC ** cdp);
static void charmap_free (CHARMAP ** cp);
static void charset_free (CHARSET ** csp);
static void fix_symbol (char *symbol, CHARMAP * m);
static void canonical_charset (char *dest, size_t dlen, const char *name)
{
size_t i;
if (!strncasecmp (name, "x-", 2))
name = name + 2;
for (i = 0; name[i] && i < dlen - 1; i++)
{
if (strchr ("_/. ", name[i]))
dest[i] = '-';
else
dest[i] = tolower (name[i]);
}
dest[i] = '\0';
}
static CHARSET *charset_new (size_t hash_size)
{
CHARSET *cp = safe_malloc (sizeof (CHARSET));
size_t i;
cp->n_symb = 256;
cp->u_symb = 0;
cp->multbyte = 1;
cp->symb_to_repr = hash_create (hash_size);
cp->description = safe_malloc (cp->n_symb * sizeof (CHARDESC *));
for (i = 0; i < cp->n_symb; i++)
cp->description[i]