Imported Upstream version 1.0.2
[platform/upstream/fribidi.git] / gen.tab / gen-unicode-version.c
1 /* FriBidi
2  * gen-unicode-version.c - generate fribidi-unicode-version.h
3  *
4  * Author:
5  *   Behdad Esfahbod, 2001, 2002, 2004
6  *
7  * Copyright (C) 2004 Sharif FarsiWeb, Inc
8  * Copyright (C) 2004 Behdad Esfahbod
9  * 
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  * 
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library, in a file named COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA
24  * 
25  * For licensing issues, contact <fribidi.license@gmail.com>.
26  */
27
28 #include <common.h>
29
30 #include <stdio.h>
31 #ifdef STDC_HEADERS
32 # include <stdlib.h>
33 # include <stddef.h>
34 #else
35 # if HAVE_STDLIB_H
36 #  include <stdlib.h>
37 # endif
38 #endif
39 #ifdef HAVE_STRING_H
40 # if !STDC_HEADERS && HAVE_MEMORY_H
41 #  include <memory.h>
42 # endif
43 # include <string.h>
44 #endif
45 #ifdef HAVE_STRINGS_H
46 # include <strings.h>
47 #endif
48
49 #include "packtab.h"
50
51 #define appname "gen-unicode-version"
52 #define outputname "fribidi-unicode-version.h"
53
54 static void
55 die (
56   const char *msg
57 )
58 {
59   fprintf (stderr, appname ": %s\n", msg);
60   exit (1);
61 }
62
63 static void
64 die2 (
65   const char *fmt,
66   const char *p
67 )
68 {
69   fprintf (stderr, appname ": ");
70   fprintf (stderr, fmt, p);
71   fprintf (stderr, "\n");
72   exit (1);
73 }
74
75 int version_major, version_minor, version_micro;
76 char unicode_version[100];
77 char buf[4000];
78
79 static void
80 init (
81   void
82 )
83 {
84   version_major = version_minor = version_micro = 0;
85   strcpy (unicode_version, "(unknown)");
86 }
87
88 #define READ_VERSION(prefix) ((where = strstr(buf, prefix)) && \
89                               (3 == sscanf (where, \
90                                             prefix"%d.%d.%d", &version_major, &version_minor, &version_micro)))
91
92 static int
93 read_file (
94   FILE *f
95 )
96 {
97   int found = 0;
98
99   version_micro = 0;
100
101   while (fgets (buf, sizeof buf, f))
102     {
103       const char *where;
104
105       if (READ_VERSION ("Version ") || READ_VERSION ("Unicode "))
106         {
107           found = 1;
108           break;
109         }
110     }
111
112   if (!found)
113     return 0;
114
115   sprintf (unicode_version, "%d.%d.%d", version_major, version_minor,
116            version_micro);
117   return 1;
118 }
119
120 static int
121 read_data (
122   const char *data_file_type,
123   const char *data_file_name
124 )
125 {
126   FILE *f;
127   int status;
128
129   if (!(f = fopen (data_file_name, "rt")))
130     die2 ("error: cannot open `%s' for reading", data_file_name);
131
132   status = read_file (f);
133
134   fclose (f);
135
136   return status;
137 }
138
139 static void
140 gen_unicode_version (
141   const char *data_file_type
142 )
143 {
144   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
145           FRIBIDI_VERSION ")\n" " * from the file %s */\n\n", data_file_type);
146
147   printf ("#define FRIBIDI_UNICODE_VERSION \"%s\"\n"
148           "#define FRIBIDI_UNICODE_MAJOR_VERSION %d\n"
149           "#define FRIBIDI_UNICODE_MINOR_VERSION %d\n"
150           "#define FRIBIDI_UNICODE_MICRO_VERSION %d\n"
151           "\n",
152           unicode_version,
153           version_major, version_minor, version_micro);
154
155   printf ("/* End of generated " outputname " */\n");
156 }
157
158 int
159 main (
160   int argc,
161   const char **argv
162 )
163 {
164   const char *data_file_type;
165   int i;
166   int found = 0;
167
168   if (argc < 2)
169     die ("usage:\n  " appname
170          " /path/to/a/UCD/file [/path/to/more/UCD/files ...]");
171
172   {
173     const char *data_file_name;
174
175     init ();
176     for (i = 1; i < argc && !found; i++)
177       {
178         data_file_name = argv[i];
179         data_file_type = strrchr (data_file_name, '/');
180         if (data_file_type)
181           data_file_type++;
182         else
183           data_file_type = data_file_name;
184         found = read_data (data_file_type, data_file_name);
185       }
186     if (!found)
187       die ("error: version not found");
188     gen_unicode_version (data_file_type);
189   }
190
191   return 0;
192 }