/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* CHARMAP file handling for iconv.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <alloca.h>
#include <stddef.h>
#include <unistd.h>
#include "charmap.h"
#include "parser.tab.h"
#include <assert.h>
typedef struct charmap {
const char *cm_name;
int cm_warned;
int cm_frmbs_len;
int cm_tombs_len;
} charmap_t;
/*
* Array of POSIX specific portable characters.
*/
static const struct {
char *name;
int ch;
} portable_chars[] = {
{ "NUL", '\0' },
{ "alert", '\a' },
{ "backspace", '\b' },
{ "tab", '\t' },
{ "carriage-return", '\r' },
{ "newline", '\n' },
{ "vertical-tab", '\v' },
{ "form-feed", '\f' },
{ "space", ' ' },
{ "exclamation-mark", '!' },
{ "quotation-mark", '"' },
{ "number-sign", '#' },
{ "dollar-sign", '$' },
{ "percent-sign", '%' },
{ "ampersand", '&' },
{ "apostrophe", '\'' },
{ "left-parenthesis", '(' },
{ "right-parenthesis", '(' },
{ "asterisk", '*' },
{ "plus-sign", '+' },
{ "comma", ','},
{ "hyphen-minus", '-' },
{ "hyphen", '-' },
{ "full-stop", '.' },
{ "period", '.' },
{ "slash", '/' },
{ "solidus", '/' },
{ "zero", '0' },
{ "one", '1' },
{ "two", '2' },
{ "three", '3' },
{ "four", '4' },
{ "five", '5' },
{ "six", '6' },
{ "seven", '7' },
{ "eight", '8' },
{ "nine", '9' },
{ "colon", ':' },
{ "semicolon", ';' },
{ "less-than-sign", '<' },
{ "equals-sign", '=' },
{ "greater-than-sign", '>' },
{ "question-mark", '?' },
{ "commercial-at", '@' },
{ "left-square-bracket", '[' },
{ "backslash", '\\' },
{ "reverse-solidus", '\\' },
{ "right-square-bracket", ']' },
{ "circumflex", '^' },
{ "circumflex-accent", '^' },
{ "low-line", '_' },
{ "underscore", '_' },
{ "grave-accent", '`' },
{ "left-brace", '{' },
{ "left-curly-bracket", '{' },
{ "vertical-line", '|' },
{ "right-brace", '}' },
{ "right-curly-bracket", '}' },
{ "tilde", '~' },
{ "A", 'A' },
{ "B", 'B' },
{ "C", 'C' },
{ "D", 'D' },
{ "E", 'E' },
{ "F", 'F' },
{ "G", 'G' },
{ "H", 'H' },
{ "I", 'I' },
{ "J", 'J' },
{ "K", 'K' },
{ "L", 'L' },
{ "M", 'M' },
{ "N", 'N' },
{ "O", 'O' },
{ "P", 'P' },
{ "Q", 'Q' },
{ "R", 'R' },
{ "S", 'S' },
{ "T", 'T' },
{ "U", 'U' },
{ "V", 'V' },
{ "W", 'W' },
{ "X", 'X' },
{ "Y", 'Y' },
{ "Z", 'Z' },
{ "a", 'a' },
{ "b", 'b' },
{ "c", 'c' },
{ "d", 'd' },
{ "e", 'e' },
{ "f", 'f' },
{ "g", 'g' },
{ "h", 'h' },
{ "i", 'i' },
{ "j", 'j' },
{ "k", 'k' },
{ "l", 'l' },
{ "m", 'm' },
{ "n", 'n' },
{ "o", 'o' },
{ "p", 'p' },
{ "q", 'q' },
{ "r", 'r' },
{ "s", 's' },
{ "t", 't' },
{ "u", 'u' },
{ "v", 'v' },
{ "w", 'w' },
{ "x", 'x' },
{ "y", 'y' },
{ "z", 'z' },
{ NULL, 0 }
};
static int
{
int rv;
}
/*
* In order for partial match searches to work,
* we need these sorted by mbs contents.
*/
static int
{
if (rv < 0)
return (-1);
if (rv > 0)
return (1);
/* they match through length */
return (-1);
return (1);
return (0);
}
void
{
(void) yyparse();
(void) yyparse();
}
void
{
int i;
(void) printf("\timbs=\"");
for (i = 0; i < cm->cm_frmbs_len; i++)
(void) printf("\"\n");
(void) printf("\tombs=\"");
for (i = 0; i < cm->cm_tombs_len; i++)
(void) printf("\"\n");
}
}
/*
* We parse two charmap files: First the "from" map, where we build
* cmap_mbs and cmap_sym which we'll later use to translate the input
* stream (mbs encodings) to symbols. Second, we parse the "to" map,
* where we fill in the tombs members of entries in cmap_sym, (which
* must alread exist) used later to write the output encoding.
*/
static void
{
/*
* While parsing both the "from" and "to" cmaps,
* require both the symbol and encoding.
*/
errf(_("invalid charmap entry"));
return;
}
switch (cmap_pass) {
case CMAP_PASS_FROM:
break;
case CMAP_PASS_TO:
break;
default:
abort();
break;
}
}
static void
{
charmap_t *m, *n, *s;
errf(_("out of memory"));
return;
}
n->cm_frmbs_len = mbs_len;
/*
* If we found the symbol, this is a dup.
*/
if (s != NULL) {
if (nodups) {
}
free(n);
return;
}
/*
* If we found the mbs, the new one is an alias,
* which we'll add _only_ to the symbol AVL.
*/
if (m != NULL) {
/* The new one is an alias of the original. */
n->cm_alias_of = m;
return;
}
}
static void
{
charmap_t *m;
if (m == NULL) {
if (sflag == 0)
return;
}
if (m->cm_alias_of != NULL) {
m = m->cm_alias_of;
/* don't warn for dups with aliases */
if (m->cm_tombs_len != 0)
return;
}
if (m->cm_tombs_len != 0) {
if (nodups) {
}
return;
}
m->cm_tombs_len = mbs_len;
}
void
{
/* mbs[0] is the length */
}
/*
* which are allocated in the scanner (T_SYMBOL) and free'd here.
*/
void
{
int si;
int i;
int mbs_len;
char *mb_last;
/* mbs[0] is the length */
errf(_("malformed charmap range"));
return;
}
char *nn;
errf(_("out of memory"));
return;
}
(*mb_last)++;
}
}
void
{
mbs[0] = c;
}
/*
* POSIX insists that certain entries be present, even when not in the
* orginal charmap file.
*/
void
add_charmap_posix(void)
{
int i;
for (i = 0; portable_chars[i].name; i++) {
}
}
/*
* This is called with a buffer of (typically) MB_LEN_MAX bytes,
* which is potentially a multi-byte symbol, but often contains
* extra bytes. Find and return the longest match in the charmap.
*/
static charmap_t *
{
while (len > 0) {
break;
len--;
}
return (cm);
}
/*
* Return true if this sequence matches the initial part
* of any sequence known in this charmap.
*/
static boolean_t
{
/* full match - not expected, but OK */
return (B_TRUE);
}
return (B_TRUE);
return (B_FALSE);
}
/*
* Do like iconv(3), but with charmaps.
*/
{
int mbs_len;
/* Ignore state reset requests. */
return (0);
if (*oleft < MB_LEN_MAX) {
return ((size_t)-1);
}
if (mbs_len < MB_LEN_MAX &&
/* incomplete sequence */
} else {
}
return ((size_t)-1);
}
if (cm->cm_tombs_len == 0) {
warn(_("To-map does not encode <%s>\n"),
}
if (cflag == 0) {
return ((size_t)-1);
}
/* just skip this input seq. */
continue;
}
}
return (0);
}