]> git.lyx.org Git - lyx.git/blob - intl/loadmsgcat.c
new siamltex class
[lyx.git] / intl / loadmsgcat.c
1 /* Load needed message catalogs.
2    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25
26 #if defined STDC_HEADERS || defined _LIBC
27 # include <stdlib.h>
28 #endif
29
30 #if defined HAVE_UNISTD_H || defined _LIBC
31 # include <unistd.h>
32 #endif
33
34 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
35 # include <sys/mman.h>
36 #endif
37
38 #include "gettext.h"
39 #include "gettextP.h"
40
41 /* @@ end of prolog @@ */
42
43 #ifdef _LIBC
44 /* Rename the non ISO C functions.  This is required by the standard
45    because some ISO C functions will require linking with this object
46    file and the name space must not be polluted.  */
47 # define open   __open
48 # define close  __close
49 # define read   __read
50 # define mmap   __mmap
51 # define munmap __munmap
52 #endif
53
54 /* We need a sign, whether a new catalog was loaded, which can be associated
55    with all translations.  This is important if the translations are
56    cached by one of GCC's features.  */
57 int _nl_msg_cat_cntr = 0;
58
59
60 /* Load the message catalogs specified by FILENAME.  If it is no valid
61    message catalog do nothing.  */
62 void
63 internal_function
64 _nl_load_domain (domain_file)
65      struct loaded_l10nfile *domain_file;
66 {
67   int fd;
68   size_t size;
69   struct stat st;
70   struct mo_file_header *data = (struct mo_file_header *) -1;
71 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
72     || defined _LIBC
73   int use_mmap = 0;
74 #endif
75   struct loaded_domain *domain;
76
77   domain_file->decided = 1;
78   domain_file->data = NULL;
79
80   /* If the record does not represent a valid locale the FILENAME
81      might be NULL.  This can happen when according to the given
82      specification the locale file name is different for XPG and CEN
83      syntax.  */
84   if (domain_file->filename == NULL)
85     return;
86
87   /* Try to open the addressed file.  */
88 #ifdef CYGWIN32
89   fd = open (domain_file->filename, O_RDONLY | O_BINARY);
90 #else
91   fd = open (domain_file->filename, O_RDONLY);
92 #endif
93   if (fd == -1)
94     return;
95
96   /* We must know about the size of the file.  */
97   if (fstat (fd, &st) != 0
98       || (size = (size_t) st.st_size) != st.st_size
99       || size < sizeof (struct mo_file_header))
100     {
101       /* Something went wrong.  */
102       close (fd);
103       return;
104     }
105
106 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
107     || defined _LIBC
108   /* Now we are ready to load the file.  If mmap() is available we try
109      this first.  If not available or it failed we try to load it.  */
110   data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
111                                          MAP_PRIVATE, fd, 0);
112
113   if (data != (struct mo_file_header *) -1)
114     {
115       /* mmap() call was successful.  */
116       close (fd);
117       use_mmap = 1;
118     }
119 #endif
120
121   /* If the data is not yet available (i.e. mmap'ed) we try to load
122      it manually.  */
123   if (data == (struct mo_file_header *) -1)
124     {
125       size_t to_read;
126       char *read_ptr;
127
128       data = (struct mo_file_header *) malloc (size);
129       if (data == NULL)
130         return;
131
132       to_read = size;
133       read_ptr = (char *) data;
134       do
135         {
136           long int nb = (long int) read (fd, read_ptr, to_read);
137           if (nb == -1)
138             {
139               close (fd);
140               return;
141             }
142
143           read_ptr += nb;
144           to_read -= nb;
145         }
146       while (to_read > 0);
147
148       close (fd);
149     }
150
151   /* Using the magic number we can test whether it really is a message
152      catalog file.  */
153   if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
154     {
155       /* The magic number is wrong: not a message catalog file.  */
156 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
157     || defined _LIBC
158       if (use_mmap)
159         munmap ((caddr_t) data, size);
160       else
161 #endif
162         free (data);
163       return;
164     }
165
166   domain_file->data
167     = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
168   if (domain_file->data == NULL)
169     return;
170
171   domain = (struct loaded_domain *) domain_file->data;
172   domain->data = (char *) data;
173 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
174     || defined _LIBC
175   domain->use_mmap = use_mmap;
176 #endif
177   domain->mmap_size = size;
178   domain->must_swap = data->magic != _MAGIC;
179
180   /* Fill in the information about the available tables.  */
181   switch (W (domain->must_swap, data->revision))
182     {
183     case 0:
184       domain->nstrings = W (domain->must_swap, data->nstrings);
185       domain->orig_tab = (struct string_desc *)
186         ((char *) data + W (domain->must_swap, data->orig_tab_offset));
187       domain->trans_tab = (struct string_desc *)
188         ((char *) data + W (domain->must_swap, data->trans_tab_offset));
189       domain->hash_size = W (domain->must_swap, data->hash_tab_size);
190       domain->hash_tab = (nls_uint32 *)
191         ((char *) data + W (domain->must_swap, data->hash_tab_offset));
192       break;
193     default:
194       /* This is an illegal revision.  */
195 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
196     || defined _LIBC
197       if (use_mmap)
198         munmap ((caddr_t) data, size);
199       else
200 #endif
201         free (data);
202       free (domain);
203       domain_file->data = NULL;
204       return;
205     }
206
207   /* Show that one domain is changed.  This might make some cached
208      translations invalid.  */
209   ++_nl_msg_cat_cntr;
210 }
211
212
213 #ifdef _LIBC
214 void
215 internal_function
216 _nl_unload_domain (domain)
217      struct loaded_domain *domain;
218 {
219   if (domain->use_mmap)
220     munmap ((caddr_t) domain->data, domain->mmap_size);
221   else
222     free ((void *) domain->data);
223
224   free (domain);
225 }
226 #endif