Imported Upstream version 1.0.8
[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 HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34
35 #ifdef STDC_HEADERS
36 # include <stdlib.h>
37 # include <stddef.h>
38 #else
39 # if HAVE_STDLIB_H
40 #  include <stdlib.h>
41 # endif
42 #endif
43 #ifdef HAVE_STRING_H
44 # if !STDC_HEADERS && HAVE_MEMORY_H
45 #  include <memory.h>
46 # endif
47 # include <string.h>
48 #endif
49 #ifdef HAVE_STRINGS_H
50 # include <strings.h>
51 #endif
52
53 #include "packtab.h"
54
55 #define appname "gen-unicode-version"
56 #define outputname "fribidi-unicode-version.h"
57
58 static void
59 die (
60   const char *msg
61 )
62 {
63   fprintf (stderr, appname ": %s\n", msg);
64   exit (1);
65 }
66
67 static void
68 die2 (
69   const char *fmt,
70   const char *p
71 )
72 {
73   fprintf (stderr, appname ": ");
74   fprintf (stderr, fmt, p);
75   fprintf (stderr, "\n");
76   exit (1);
77 }
78
79 int version_major, version_minor, version_micro;
80 char unicode_version[100];
81 char buf[4000];
82
83 static void
84 init (
85   void
86 )
87 {
88   version_major = version_minor = version_micro = 0;
89   strcpy (unicode_version, "(unknown)");
90 }
91
92 #define READ_VERSION(prefix) ((where = strstr(buf, prefix)) && \
93                               (3 == sscanf (where, \
94                                             prefix"%d.%d.%d", &version_major, &version_minor, &version_micro)))
95
96 static int
97 read_file (
98   FILE *f
99 )
100 {
101   int found = 0;
102
103   version_micro = 0;
104
105   while (fgets (buf, sizeof buf, f))
106     {
107       const char *where;
108
109       if (READ_VERSION ("Version ") || READ_VERSION ("Unicode "))
110         {
111           found = 1;
112           break;
113         }
114     }
115
116   if (!found)
117     return 0;
118
119   sprintf (unicode_version, "%d.%d.%d", version_major, version_minor,
120            version_micro);
121   return 1;
122 }
123
124 static int
125 read_data (
126   const char *data_file_type,
127   const char *data_file_name
128 )
129 {
130   FILE *f;
131   int status;
132
133   if (!(f = fopen (data_file_name, "rt")))
134     die2 ("error: cannot open `%s' for reading", data_file_name);
135
136   status = read_file (f);
137
138   fclose (f);
139
140   return status;
141 }
142
143 static void
144 gen_unicode_version (
145   const char *data_file_type
146 )
147 {
148   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
149           FRIBIDI_VERSION ")\n" " * from the file %s */\n\n", data_file_type);
150
151   printf ("#define FRIBIDI_UNICODE_VERSION \"%s\"\n"
152           "#define FRIBIDI_UNICODE_MAJOR_VERSION %d\n"
153           "#define FRIBIDI_UNICODE_MINOR_VERSION %d\n"
154           "#define FRIBIDI_UNICODE_MICRO_VERSION %d\n"
155           "\n",
156           unicode_version,
157           version_major, version_minor, version_micro);
158
159   printf ("/* End of generated " outputname " */\n");
160 }
161
162 int
163 main (
164   int argc,
165   const char **argv
166 )
167 {
168   const char *data_file_type;
169   int i;
170   int found = 0;
171
172   if (argc < 2)
173     die ("usage:\n  " appname
174          " /path/to/a/UCD/file [/path/to/more/UCD/files ...]");
175
176   {
177     const char *data_file_name;
178
179     init ();
180     for (i = 1; i < argc && !found; i++)
181       {
182         data_file_name = argv[i];
183         data_file_type = strrchr (data_file_name, '/');
184         if (data_file_type)
185           data_file_type++;
186         else
187           data_file_type = data_file_name;
188         found = read_data (data_file_type, data_file_name);
189       }
190     if (!found)
191       die ("error: version not found");
192     gen_unicode_version (data_file_type);
193   }
194
195   return 0;
196 }