#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
#include "idn_cmd.h"
const char *gengetopt_args_info_purpose = "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use `--debug' to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with `-', for example `-foo', use `--'\nto signal the end of parameters, as in `idn --quiet -a -- -foo'.";
const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]... [STRINGS]...";
const char *gengetopt_args_info_help[] = {
" -h, --help Print help and exit",
" -V, --version Print version and exit",
" -s, --stringprep Prepare string according to nameprep profile",
" -d, --punycode-decode Decode Punycode",
" -e, --punycode-encode Encode Punycode",
" -a, --idna-to-ascii Convert to ACE according to IDNA (default)",
" -u, --idna-to-unicode Convert from ACE according to IDNA",
" --allow-unassigned Toggle IDNA AllowUnassigned flag (default=off)",
" --usestd3asciirules Toggle IDNA UseSTD3ASCIIRules flag (default=off)",
" -t, --tld Check string for TLD specific rules\n Only for --idna-to-ascii and --idna-to-unicode \n (default=on)",
" -p, --profile=STRING Use specified stringprep profile instead\n Valid stringprep profiles are `Nameprep', `iSCSI', \n `Nodeprep', `Resourceprep', `trace', and \n `SASLprep'.",
" --debug Print debugging information (default=off)",
" --quiet Silent operation (default=off)",
0
};
static
void clear_given (struct gengetopt_args_info *args_info);
static
void clear_args (struct gengetopt_args_info *args_info);
static int
cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
static char *
gengetopt_strdup (const char *s);
static
void clear_given (struct gengetopt_args_info *args_info)
{
args_info->help_given = 0 ;
args_info->version_given = 0 ;
args_info->stringprep_given = 0 ;
args_info->punycode_decode_given = 0 ;
args_info->punycode_encode_given = 0 ;
args_info->idna_to_ascii_given = 0 ;
args_info->idna_to_unicode_given = 0 ;
args_info->allow_unassigned_given = 0 ;
args_info->usestd3asciirules_given = 0 ;
args_info->tld_given = 0 ;
args_info->profile_given = 0 ;
args_info->debug_given = 0 ;
args_info->quiet_given = 0 ;
}
static
void clear_args (struct gengetopt_args_info *args_info)
{
args_info->allow_unassigned_flag = 0;
args_info->usestd3asciirules_flag = 0;
args_info->tld_flag = 1;
args_info->profile_arg = NULL;
args_info->profile_orig = NULL;
args_info->debug_flag = 0;
args_info->quiet_flag = 0;
}
static
void init_args_info(struct gengetopt_args_info *args_info)
{
args_info->help_help = gengetopt_args_info_help[0] ;
args_info->version_help = gengetopt_args_info_help[1] ;
args_info->stringprep_help = gengetopt_args_info_help[2] ;
args_info->punycode_decode_help = gengetopt_args_info_help[3] ;
args_info->punycode_encode_help = gengetopt_args_info_help[4] ;
args_info->idna_to_ascii_help = gengetopt_args_info_help[5] ;
args_info->idna_to_unicode_help = gengetopt_args_info_help[6] ;
args_info->allow_unassigned_help = gengetopt_args_info_help[7] ;
args_info->usestd3asciirules_help = gengetopt_args_info_help[8] ;
args_info->tld_help = gengetopt_args_info_help[9] ;
args_info->profile_help = gengetopt_args_info_help[10] ;
args_info->debug_help = gengetopt_args_info_help[11] ;
args_info->quiet_help = gengetopt_args_info_help[12] ;
}
void
cmdline_parser_print_version (void)
{
printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
}
void
cmdline_parser_print_help (void)
{
int i = 0;
cmdline_parser_print_version ();
if (strlen(gengetopt_args_info_purpose) > 0)
printf("\n%s\n", gengetopt_args_info_purpose);
printf("\n%s\n\n", gengetopt_args_info_usage);
while (gengetopt_args_info_help[i])
printf("%s\n", gengetopt_args_info_help[i++]);
}
void
cmdline_parser_init (struct gengetopt_args_info *args_info)
{
clear_given (args_info);
clear_args (args_info);
init_args_info (args_info);
args_info->inputs = NULL;
args_info->inputs_num = 0;
}
static void
cmdline_parser_release (struct gengetopt_args_info *args_info)
{
unsigned int i;
if (args_info->profile_arg)
{
free (args_info->profile_arg);
args_info->profile_arg = 0;
}
if (args_info->profile_orig)
{
free (args_info->profile_orig);
args_info->profile_orig = 0;
}
for (i = 0; i < args_info->inputs_num; ++i)
free (args_info->inputs [i]);
if (args_info->inputs_num)
free (args_info->inputs);
clear_given (args_info);
}
int
cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
{
FILE *outfile;
int i = 0;
outfile = fopen(filename, "w");
if (!outfile)
{
fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
return EXIT_FAILURE;
}
if (args_info->help_given) {
fprintf(outfile, "%s\n", "help");
}
if (args_info->version_given) {
fprintf(outfile, "%s\n", "version");
}
if (args_info->stringprep_given) {
fprintf(outfile, "%s\n", "stringprep");
}
if (args_info->punycode_decode_given) {
fprintf(outfile, "%s\n", "punycode-decode");
}
if (args_info->punycode_encode_given) {
fprintf(outfile, "%s\n", "punycode-encode");
}
if (args_info->idna_to_ascii_given) {
fprintf(outfile, "%s\n", "idna-to-ascii");
}
if (args_info->idna_to_unicode_given) {
fprintf(outfile, "%s\n", "idna-to-unicode");
}
if (args_info->allow_unassigned_given) {
fprintf(outfile, "%s\n", "allow-unassigned");
}
if (args_info->usestd3asciirules_given) {
fprintf(outfile, "%s\n", "usestd3asciirules");
}
if (args_info->tld_given) {
fprintf(outfile, "%s\n", "tld");
}
if (args_info->profile_given) {
if (args_info->profile_orig) {
fprintf(outfile, "%s=\"%s\"\n", "profile", args_info->profile_orig);
} else {
fprintf(outfile, "%s\n", "profile");
}
}
if (args_info->debug_given) {
fprintf(outfile, "%s\n", "debug");
}
if (args_info->quiet_given) {
fprintf(outfile, "%s\n", "quiet");
}
fclose (outfile);
i = EXIT_SUCCESS;
return i;
}
void
cmdline_parser_free (struct gengetopt_args_info *args_info)
{
cmdline_parser_release (args_info);
}
char *
gengetopt_strdup (const char *s)
{
char *result = NULL;
if (!s)
return result;
result = (char*)malloc(strlen(s) + 1);
if (result == (char*)0)
return (char*)0;
strcpy(result, s);
return result;
}
int
cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
{
return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
}
int
cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
{
int result;
result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
if (result == EXIT_FAILURE)
{
cmdline_parser_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
{
return EXIT_SUCCESS;
}
int
cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
{
int c;
int error = 0;
struct gengetopt_args_info local_args_info;
if (initialize)
cmdline_parser_init (args_info);
cmdline_parser_init (&local_args_info);
optarg = 0;
optind = 0;
opterr = 1;
optopt = '?';
while (1)
{
int option_index = 0;
char *stop_char;
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "stringprep", 0, NULL, 's' },
{ "punycode-decode", 0, NULL, 'd' },
{ "punycode-encode", 0, NULL, 'e' },
{ "idna-to-ascii", 0, NULL, 'a' },
{ "idna-to-unicode", 0, NULL, 'u' },
{ "allow-unassigned", 0, NULL, 0 },
{ "usestd3asciirules", 0, NULL, 0 },
{ "tld", 0, NULL, 't' },
{ "profile", 1, NULL, 'p' },
{ "debug", 0, NULL, 0 },
{ "quiet", 0, NULL, 0 },
{ NULL, 0, NULL, 0 }
};
stop_char = 0;
c = getopt_long (argc, argv, "hVsdeautp:", long_options, &option_index);
if (c == -1) break;
switch (c)
{
case 'h':
cmdline_parser_print_help ();
cmdline_parser_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'V':
cmdline_parser_print_version ();
cmdline_parser_free (&local_args_info);
exit (EXIT_SUCCESS);
case 's':
if (local_args_info.stringprep_given)
{
fprintf (stderr, "%s: `--stringprep' (`-s') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->stringprep_given && ! override)
continue;
local_args_info.stringprep_given = 1;
args_info->stringprep_given = 1;
break;
case 'd':
if (local_args_info.punycode_decode_given)
{
fprintf (stderr, "%s: `--punycode-decode' (`-d') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->punycode_decode_given && ! override)
continue;
local_args_info.punycode_decode_given = 1;
args_info->punycode_decode_given = 1;
break;
case 'e':
if (local_args_info.punycode_encode_given)
{
fprintf (stderr, "%s: `--punycode-encode' (`-e') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->punycode_encode_given && ! override)
continue;
local_args_info.punycode_encode_given = 1;
args_info->punycode_encode_given = 1;
break;
case 'a':
if (local_args_info.idna_to_ascii_given)
{
fprintf (stderr, "%s: `--idna-to-ascii' (`-a') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->idna_to_ascii_given && ! override)
continue;
local_args_info.idna_to_ascii_given = 1;
args_info->idna_to_ascii_given = 1;
break;
case 'u':
if (local_args_info.idna_to_unicode_given)
{
fprintf (stderr, "%s: `--idna-to-unicode' (`-u') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->idna_to_unicode_given && ! override)
continue;
local_args_info.idna_to_unicode_given = 1;
args_info->idna_to_unicode_given = 1;
break;
case 't':
if (local_args_info.tld_given)
{
fprintf (stderr, "%s: `--tld' (`-t') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->tld_given && ! override)
continue;
local_args_info.tld_given = 1;
args_info->tld_given = 1;
args_info->tld_flag = !(args_info->tld_flag);
break;
case 'p':
if (local_args_info.profile_given)
{
fprintf (stderr, "%s: `--profile' (`-p') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->profile_given && ! override)
continue;
local_args_info.profile_given = 1;
args_info->profile_given = 1;
if (args_info->profile_arg)
free (args_info->profile_arg);
args_info->profile_arg = gengetopt_strdup (optarg);
if (args_info->profile_orig)
free (args_info->profile_orig);
args_info->profile_orig = gengetopt_strdup (optarg);
break;
case 0:
if (strcmp (long_options[option_index].name, "allow-unassigned") == 0)
{
if (local_args_info.allow_unassigned_given)
{
fprintf (stderr, "%s: `--allow-unassigned' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->allow_unassigned_given && ! override)
continue;
local_args_info.allow_unassigned_given = 1;
args_info->allow_unassigned_given = 1;
args_info->allow_unassigned_flag = !(args_info->allow_unassigned_flag);
}
else if (strcmp (long_options[option_index].name, "usestd3asciirules") == 0)
{
if (local_args_info.usestd3asciirules_given)
{
fprintf (stderr, "%s: `--usestd3asciirules' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->usestd3asciirules_given && ! override)
continue;
local_args_info.usestd3asciirules_given = 1;
args_info->usestd3asciirules_given = 1;
args_info->usestd3asciirules_flag = !(args_info->usestd3asciirules_flag);
}
else if (strcmp (long_options[option_index].name, "debug") == 0)
{
if (local_args_info.debug_given)
{
fprintf (stderr, "%s: `--debug' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->debug_given && ! override)
continue;
local_args_info.debug_given = 1;
args_info->debug_given = 1;
args_info->debug_flag = !(args_info->debug_flag);
}
else if (strcmp (long_options[option_index].name, "quiet") == 0)
{
if (local_args_info.quiet_given)
{
fprintf (stderr, "%s: `--quiet' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->quiet_given && ! override)
continue;
local_args_info.quiet_given = 1;
args_info->quiet_given = 1;
args_info->quiet_flag = !(args_info->quiet_flag);
}
break;
case '?':
goto failure;
default:
fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
abort ();
}
}
cmdline_parser_release (&local_args_info);
if ( error )
return (EXIT_FAILURE);
if (optind < argc)
{
int i = 0 ;
int found_prog_name = 0;
i = optind;
while (i < argc)
if (argv[i++] == argv[0]) {
found_prog_name = 1;
break;
}
i = 0;
args_info->inputs_num = argc - optind - found_prog_name;
args_info->inputs =
(char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
while (optind < argc)
if (argv[optind++] != argv[0])
args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
}
return 0;
failure:
cmdline_parser_release (&local_args_info);
return (EXIT_FAILURE);
}