File: idn_cmd.c

package info (click to toggle)
libidn 0.6.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,520 kB
  • ctags: 1,515
  • sloc: ansic: 31,525; java: 13,494; cs: 12,385; sh: 9,482; xml: 2,283; perl: 1,868; makefile: 316; lisp: 231; php: 214; sed: 16; python: 9
file content (488 lines) | stat: -rw-r--r-- 16,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
  File autogenerated by gengetopt version 2.16
  generated with the following command:
  gengetopt --unamed-opts --input idn.ggo --file-name idn_cmd 

  The developers of gengetopt consider the fixed text that goes in all
  gengetopt output files to be in the public domain:
  we make no copyright claims on it.
*/

/* If we use autoconf.  */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "getopt.h"

#include "idn_cmd.h"

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;
  
}

void
cmdline_parser_print_version (void)
{
  printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
}

void
cmdline_parser_print_help (void)
{
  cmdline_parser_print_version ();
  printf("\n%s\n", "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'.");
  printf("\nUsage: %s [OPTIONS]... [STRINGS]...\n\n", CMDLINE_PARSER_PACKAGE);
  printf("%s\n","  -h, --help               Print help and exit");
  printf("%s\n","  -V, --version            Print version and exit");
  printf("%s\n","  -s, --stringprep         Prepare string according to nameprep profile");
  printf("%s\n","  -d, --punycode-decode    Decode Punycode");
  printf("%s\n","  -e, --punycode-encode    Encode Punycode");
  printf("%s\n","  -a, --idna-to-ascii      Convert to ACE according to IDNA (default)");
  printf("%s\n","  -u, --idna-to-unicode    Convert from ACE according to IDNA");
  printf("%s\n","      --allow-unassigned   Toggle IDNA AllowUnassigned flag  (default=off)");
  printf("%s\n","      --usestd3asciirules  Toggle IDNA UseSTD3ASCIIRules flag  (default=off)");
  printf("%s\n","  -t, --tld                Check string for TLD specific rules\n                             Only for --idna-to-ascii and --idna-to-unicode  \n                             (default=on)");
  printf("%s\n","  -p, --profile=STRING     Use specified stringprep profile instead\n                             Valid stringprep profiles are `Nameprep', `iSCSI', \n                             `Nodeprep', `Resourceprep', `trace', and \n                             `SASLprep'.");
  printf("%s\n","      --debug              Print debugging information  (default=off)");
  printf("%s\n","      --quiet              Silent operation  (default=off)");
  
}

void
cmdline_parser_init (struct gengetopt_args_info *args_info)
{
  clear_given (args_info);
  clear_args (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); /* free previous argument */
      args_info->profile_arg = 0;
    }
  if (args_info->profile_orig)
    {
      free (args_info->profile_orig); /* free previous argument */
      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);
}


/* gengetopt_strdup() */
/* strdup.c replacement of strdup, which is not standard */
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;	/* Character of the parsed option.  */

  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;	/* Exit from `while (1)' loop.  */

      switch (c)
        {
        case 'h':	/* Print help and exit.  */
          cmdline_parser_print_help ();
          cmdline_parser_free (&local_args_info);
          exit (EXIT_SUCCESS);

        case 'V':	/* Print version and exit.  */
          cmdline_parser_print_version ();
          cmdline_parser_free (&local_args_info);
          exit (EXIT_SUCCESS);

        case 's':	/* Prepare string according to nameprep profile.  */
          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':	/* Decode Punycode.  */
          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':	/* Encode Punycode.  */
          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':	/* Convert to ACE according to IDNA (default).  */
          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':	/* Convert from ACE according to IDNA.  */
          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':	/* Check string for TLD specific rules\nOnly for --idna-to-ascii and --idna-to-unicode.  */
          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':	/* Use specified stringprep profile instead\nValid stringprep profiles are `Nameprep', `iSCSI', `Nodeprep', `Resourceprep', `trace', and `SASLprep'..  */
          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); /* free previous string */
          args_info->profile_arg = gengetopt_strdup (optarg);
          if (args_info->profile_orig)
            free (args_info->profile_orig); /* free previous string */
          args_info->profile_orig = gengetopt_strdup (optarg);
          break;


        case 0:	/* Long option with no short option */
          /* Toggle IDNA AllowUnassigned flag.  */
          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);
          }
          /* Toggle IDNA UseSTD3ASCIIRules 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);
          }
          /* Print debugging information.  */
          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);
          }
          /* Silent operation.  */
          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 '?':	/* Invalid option.  */
          /* `getopt_long' already printed an error message.  */
          goto failure;

        default:	/* bug: option not considered.  */
          fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
          abort ();
        } /* switch */
    } /* while */




  cmdline_parser_release (&local_args_info);

  if ( error )
    return (EXIT_FAILURE);

  if (optind < argc)
    {
      int i = 0 ;

      args_info->inputs_num = argc - optind ;
      args_info->inputs =
        (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
      while (optind < argc)
        args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
    }

  return 0;

failure:
  
  cmdline_parser_release (&local_args_info);
  return (EXIT_FAILURE);
}