File: gendata.c

package info (click to toggle)
libidn2 2.3.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,768 kB
  • sloc: ansic: 6,049; sh: 1,480; makefile: 499; xml: 50; perl: 15
file content (143 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (2)
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
/* gendata.c - generate idna_table
   Copyright (C) 2019 Orivej Desh
   Copyright (C) 2022-2025 Simon Josefsson

   Libidn2 is free software: you can redistribute it and/or modify it
   under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version.

   or

     * 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.

   or both in parallel, as here.

   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 copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

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

char line[1024];
int line_cnt = 0;

static int
next_line (void)
{
  line_cnt++;
  return fgets (line, sizeof line, stdin) != NULL;
}

static void
_ok (const char *fname, int fline, int rc)
{
  if (!rc)
    {
      fprintf (stderr, "%s:%d: unexpected failure at input line %d\n",
	       fname, fline, line_cnt);
      exit (EXIT_FAILURE);
    }
}

#define ok(rc) _ok (__FILE__, __LINE__, ((rc) != 0))

int
main (void)
{
  char *range_end;
  char *property;
  int idna_table_size = 0;
  enum
  { IANA, UTC } table_syntax;

  ok (next_line ());
  if (strstr (line, "Codepoint,Property,") == line)
    table_syntax = IANA;
  else if (strstr (line, "# Idna2008-") == line)
    table_syntax = UTC;
  else
    {
      fprintf (stderr, "gendata: unrecognized input\n");
      exit (EXIT_FAILURE);
    }

  puts ("/* This file is automatically generated.  DO NOT EDIT! */");
  puts ("");
  puts ("#include <config.h>");
  puts ("#include \"data.h\"");
  puts ("");
  puts ("const struct idna_table idna_table[] = {");

  while (next_line ())
    {
      if (table_syntax == IANA)
	{
	  ok (strtok (line, ","));
	  property = strtok (NULL, ",");
	  ok (property);

	  if (!strcmp (property, "UNASSIGNED"))
	    continue;

	  strtok (line, "-");
	  range_end = strtok (NULL, "");
	  if (!range_end)
	    range_end = line;

	  printf ("  {0x%s, 0x%s, %s},\n", line, range_end, property);
	  idna_table_size++;
	}
      else if (table_syntax == UTC)
	{
	  if (line[0] == '#')
	    continue;
	  if (line[0] == '\n')
	    continue;

	  ok (strtok (line, ";"));
	  property = strtok (NULL, "#");
	  ok (property);
	  ok (*property == ' ');
	  property++;

	  if (strstr (property, "UNASSIGNED") == property)
	    continue;

	  strtok (line, ".");
	  range_end = strtok (NULL, "");
	  if (range_end)
	    {
	      ok (*range_end == '.');
	      range_end++;
	    }
	  else
	    range_end = line;

	  strtok (line, " ");
	  strtok (range_end, " ");
	  strtok (property, " ");

	  printf ("  {0x%s, 0x%s, %s},\n", line, range_end, property);
	  idna_table_size++;
	}
    }

  puts ("};");
  printf ("const size_t idna_table_size = %d;\n", idna_table_size);
  return 0;
}