mdnconv.c revision 04bdb234571448ed6194e1d4048e6512f2446f1c
803b50652fd6ad81d04d18fc04332c8a94f8fe9aAndreas Gustafsson#ifndef lint
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updaterstatic char *rcsid = "$Id: mdnconv.c,v 1.19 2000/09/13 05:55:55 ishisone Exp $";
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater/*
803b50652fd6ad81d04d18fc04332c8a94f8fe9aAndreas Gustafsson * Copyright (c) 2000 Japan Network Information Center. All rights reserved.
803b50652fd6ad81d04d18fc04332c8a94f8fe9aAndreas Gustafsson *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * By using this file, you agree to the terms and conditions set forth bellow.
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews *
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews * LICENSE TERMS AND CONDITIONS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews * The following License Terms and Conditions apply, unless a different
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews * license is obtained from Japan Network Information Center ("JPNIC"),
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews * a Japanese association, Fuundo Bldg., 1-2 Kanda Ogawamachi, Chiyoda-ku,
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews * Tokyo, Japan.
803b50652fd6ad81d04d18fc04332c8a94f8fe9aAndreas Gustafsson *
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater * 1. Use, Modification and Redistribution (including distribution of any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * modified or derived work) in source and/or binary forms is permitted
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * under this License Terms and Conditions.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 2. Redistribution of source code must retain the copyright notices as they
852ccdd42a71550c974111b49415204ffeca6573Automatic Updater * appear in each source code file, this License Terms and Conditions.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 3. Redistribution in binary form must reproduce the Copyright Notice,
0c6ada0a814f3c5417daa1654129bc2af56ed504Automatic Updater * this License Terms and Conditions, in the documentation and/or other
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * materials provided with the distribution. For the purposes of binary
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * distribution the "Copyright Notice" refers to the following language:
0c6ada0a814f3c5417daa1654129bc2af56ed504Automatic Updater * "Copyright (c) Japan Network Information Center. All rights reserved."
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 4. Neither the name of JPNIC may be used to endorse or promote products
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * derived from this Software without specific prior written approval of
163af735c2082a024167be111d27bd5b5ff4f462Automatic Updater * JPNIC.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 6. Indemnification by Licensee
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Any person or entities using and/or redistributing this Software under
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * this License Terms and Conditions shall defend indemnify and hold
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * harmless JPNIC from and against any and all judgements damages,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * expenses, settlement liabilities, cost and other liabilities of any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * kind as a result of use and redistribution of this Software or any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * claim, suite, action, litigation or proceeding by any third party
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * arising out of or relates to this License Terms and Conditions.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 7. Governing Law, Jurisdiction and Venue
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * This License Terms and Conditions shall be governed by and and
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * construed in accordance with the law of Japan. Any person or entities
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * using and/or redistributing this Software under this License Terms and
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Conditions hereby agrees and consent to the personal and exclusive
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * jurisdiction and venue of Tokyo District Court of Japan.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein/*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * mdnconv -- Codeset converter for named.conf and zone files
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <config.h>
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater#include <stdio.h>
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater#include <stddef.h>
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater#include <stdlib.h>
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt#include <string.h>
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt#include <stdarg.h>
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt#include <errno.h>
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt#ifdef HAVE_LOCALE_H
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <locale.h>
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#endif
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater#include <mdn/result.h>
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater#include <mdn/converter.h>
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#include <mdn/normalizer.h>
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#include <mdn/localencoding.h>
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#include <mdn/utf8.h>
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#include <mdn/resconf.h>
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#include "util.h"
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater/* Maxmum number of normalizers */
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#define MAX_NORMALIZER 10
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#ifndef MDN_UTF8_ENCODING_NAME
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#define MDN_UTF8_ENCODING_NAME "UTF-8"
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#endif
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater#ifndef MDN_RACE_ENCODING_NAME
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define MDN_RACE_ENCODING_NAME "RACE"
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater#endif
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater#ifndef MDN_RACE_PREFIX
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater#define MDN_RACE_PREFIX "bq--"
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater#endif
d060d8669f5558690e7faf4a1c12fe5c02a7c60dAutomatic Updater
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinint line_number; /* current input file line number */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinint flush_every_line = 0; /* pretty obvious */
7329012471d165cd3dc4180ad2a0a43de91e7f01Mark Andrewsmdn_converter_t conv_in_ctx; /* input converter */
7329012471d165cd3dc4180ad2a0a43de91e7f01Mark Andrewsmdn_converter_t conv_out_ctx; /* output converter */
7329012471d165cd3dc4180ad2a0a43de91e7f01Mark Andrewsmdn_normalizer_t norm_ctx; /* normalizer */
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt
351b62535d4c4f89883bfdba025999dd32490266Evan Huntvoid errormsg(const char *fmt, ...);
351b62535d4c4f89883bfdba025999dd32490266Evan Huntstatic int convert_file(FILE *fp, char *zld, int auto_zld,
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt int selective);
351b62535d4c4f89883bfdba025999dd32490266Evan Huntstatic void usage(char *cmd);
7329012471d165cd3dc4180ad2a0a43de91e7f01Mark Andrews
7329012471d165cd3dc4180ad2a0a43de91e7f01Mark Andrewsint
351b62535d4c4f89883bfdba025999dd32490266Evan Huntmain(int ac, char **av) {
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt char *cmd = *av;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt char *normalizer[MAX_NORMALIZER];
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt int nnormalizer = 0;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt const char *in_code = NULL;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt const char *out_code = NULL;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein char *resconf_file = NULL;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein int no_resconf = 0;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt char zld[256 + 1];
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt int zld_specified = 0;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt int auto_zld = 0;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt char *encoding_alias = NULL;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt int selective = 1;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt FILE *fp;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt mdn_resconf_t resconf;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt#ifdef HAVE_SETLOCALE
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt (void)setlocale(LC_ALL, "");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein zld[0] = '\0';
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater ac--;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt av++;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt while (ac > 0 && **av == '-') {
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt#define MUST_HAVE_ARG if (ac < 2) usage(cmd)
90c38ab4e6904126bec2f2f57f60cd834ce759cbAutomatic Updater if (strcmp(*av, "-in") == 0) {
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt MUST_HAVE_ARG;
3cddb2c552ee6582e8db0849c28747f6b6ca57feAutomatic Updater in_code = av[1];
3cddb2c552ee6582e8db0849c28747f6b6ca57feAutomatic Updater ac--;
3cddb2c552ee6582e8db0849c28747f6b6ca57feAutomatic Updater av++;
ed4475f3f583f6137b4ff7fea775c5363a4fdb29Automatic Updater } else if (strcmp(*av, "-out") == 0) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein MUST_HAVE_ARG;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt out_code = av[1];
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ac--;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein av++;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-conf") == 0) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein MUST_HAVE_ARG;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein resconf_file = av[1];
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ac--;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein av++;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-noconf") == 0) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein no_resconf = 1;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt } else if (strcmp(*av, "-zld") == 0) {
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt MUST_HAVE_ARG;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt canonical_zld(zld, av[1]);
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt zld_specified = 1;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ac--;
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews av++;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-auto") == 0) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein auto_zld = 1;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-normalize") == 0) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein MUST_HAVE_ARG;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (nnormalizer >= MAX_NORMALIZER) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein errormsg("too many normalizers\n");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein exit(1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews normalizer[nnormalizer++] = av[1];
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ac--;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein av++;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-alias") == 0) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein MUST_HAVE_ARG;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein encoding_alias = *av;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-flush") == 0) {
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews flush_every_line = 1;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else if (strcmp(*av, "-whole") == 0) {
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews selective = 0;
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews } else {
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews usage(cmd);
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews }
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews#undef MUST_HAVE_ARG
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews ac--;
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews av++;
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews }
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews
f02216f5b390ff0a589fa080f29350fd7794bf5cMark Andrews if (ac > 1)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein usage(cmd);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Load configuration file.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein resconf = NULL;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (!no_resconf) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein mdn_result_t r;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein r = mdn_resconf_initialize();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (r == mdn_success)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein r = mdn_resconf_create(&resconf);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (r == mdn_success)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein r = mdn_resconf_loadfile(resconf, resconf_file);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (r != mdn_success) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein errormsg("error reading configuration file: %s\n",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein mdn_result_tostring(r));
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Get default input/output code.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (in_code == NULL)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein in_code = mdn_localencoding_name();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (out_code == NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein mdn_converter_t c;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (resconf != NULL &&
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (c = mdn_resconf_serverconverter(resconf)) != NULL)
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews out_code = mdn_converter_localencoding(c);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews }
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews if (in_code == NULL) {
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews errormsg("input codeset must be specified\n");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (out_code == NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein errormsg("output codeset must be specified\n");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Initialize codeset converter.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (!initialize_converter(in_code, out_code, encoding_alias))
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Initialize normalizer.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (nnormalizer == 0 && resconf != NULL)
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews norm_ctx = mdn_resconf_normalizer(resconf);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews if (norm_ctx == NULL &&
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews !initialize_normalizer(normalizer, nnormalizer))
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews return (1);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Default ZLD.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
61e1dc26d62c2a0059e3ca7efe2ad0f4a5b8df92Mark Andrews if (!zld_specified && resconf != NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein const char *conf_zld = mdn_resconf_zld(resconf);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (conf_zld != NULL)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein canonical_zld(zld, conf_zld);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Open input file.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (ac > 0) {
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews if ((fp = fopen(av[0], "r")) == NULL) {
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews errormsg("cannot open file %s: %s\n",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews av[0], strerror(errno));
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews return (1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein fp = stdin;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Do the conversion.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return convert_file(fp, zld, auto_zld, selective);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrewsstatic int
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrewsconvert_file(FILE *fp, char *zld, int auto_zld, int selective) {
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews mdn_result_t r;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein char line1[1024];
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein char line2[1024];
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews int nl_trimmed;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein int race_hack;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (strcmp(mdn_converter_localencoding(conv_in_ctx),
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater MDN_RACE_ENCODING_NAME) == 0)
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater race_hack = 1;
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater else
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater race_hack = 0;
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater line_number = 1;
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater while (fgets(line1, sizeof(line1), fp) != NULL) {
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater /*
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater * Trim newline at the end. This is needed for
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews * those ascii-comatible encodings such as UTF-5 or RACE
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews * not to try converting newlines, which will result
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews * in `invalid encoding' error.
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews */
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews if (line1[strlen(line1) - 1] == '\n') {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein line1[strlen(line1) - 1] = '\0';
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews nl_trimmed = 1;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein nl_trimmed = 0;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater * Convert input line to UTF-8.
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater */
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater if (race_hack) {
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater /*
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater * Find portions encoded in RACE using RACE's prefix
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater * "bq--", and selectively decode those portions.
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater */
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater r = selective_decode(MDN_RACE_PREFIX,
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater line1, line2, 1024);
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater } else {
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater r = mdn_converter_localtoutf8(conv_in_ctx,
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater line1, line2, 1024);
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater }
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater if (r != mdn_success) {
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater errormsg("conversion failed at line %d: %s\n",
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater line_number,
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater mdn_result_tostring(r));
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater return (1);
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater }
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater if (!mdn_utf8_isvalidstring(line2)) {
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater errormsg("conversion to utf-8 failed at line %d\n",
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater line_number);
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater return (1);
7c6b9b263898daf28d657f65dbd75c330ca4aa13Automatic Updater }
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews /*
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews * Normalize and convert to the output codeset.
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (selective) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein r = selective_encode(line2, line1, sizeof(line1),
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein zld, auto_zld);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } else {
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews r = encode_region(line2, line1, sizeof(line1),
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews zld, auto_zld);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews }
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews if (r != mdn_success)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein fputs(line1, stdout);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (nl_trimmed)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein putc('\n', stdout);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews if (flush_every_line)
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews fflush(stdout);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews line_number++;
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (0);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinvoid
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinerrormsg(const char *fmt, ...) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein va_list args;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein va_start(args, fmt);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein vfprintf(stderr, fmt, args);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein va_end(args);
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews}
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrewsstatic void
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrewsusage(char *cmd) {
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews int i;
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews static char *options[] = {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-in input-codeset : specifies input codeset name.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-out output-codeset : specifies output codeset name.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-normalize scheme : specifies normalization scheme.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein " this option can be specified",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein " multiple times.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-zld zld : specifies ZLD to use.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-auto : automatically appends ZLD where",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein " seemed appropriate.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-alias alias-file : specifies codeset alias file.",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein "-conf conf-file : specifies pathname of MDN resolver",
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein " configuration file.",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews "-noconf : do not load MDN resolver configuration",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews " file.",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews "-flush : line-buffering mode.",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews "-whole : convert the whole region instead of",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews " regions containing non-ascii characters",
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews NULL,
69f175fc57a578dd85c1548ed3f34284321f9d3aMark Andrews };
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein fprintf(stderr, "Usage: %s [options..] [file]\n", cmd);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein for (i = 0; options[i] != NULL; i++)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein fprintf(stderr, "\t%s\n", options[i]);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein exit(1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein