Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[2b7377]: catopen.c Maximize Restore History

Download this file

catopen.c    500 lines (456 with data), 15.4 kB

  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
489
490
491
492
493
494
495
496
497
498
499
/*
* catopen.c
*
* $Id$
*
* Copyright (C) 2006, Keith Marshall
*
* This file implements the `catopen' function, required to support
* POSIX compatible national language message catalogues in MinGW.
*
* Written by Keith Marshall <keithmarshall@users.sourceforge.net>
* Last modification: 22-Sept-2007
*
*
* This is free software. It is provided AS IS, in the hope that it may
* be useful, but WITHOUT WARRANTY OF ANY KIND, not even an IMPLIED WARRANTY
* of MERCHANTABILITY, nor of FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted to redistribute this software, either "as is" or
* in modified form, under the terms of the GNU General Public License, as
* published by the Free Software Foundation; either version 2, or (at your
* option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to the
* Free Software Foundation, 51 Franklin St - Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#define WIN32_LEAN_AND_MEAN
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <locale.h>
#include <nl_types.h>
#include <nlspath.h>
#include <msgcat.h>
#include <mctab.h>
#ifndef LC_MESSAGES
/*
* On Win32 platforms, we don't expect LC_MESSAGES to be defined.
* For this, and any others which don't define it, substitute LC_CTYPE.
*/
# define LC_MESSAGES LC_CTYPE
#endif
#include <platform.h>
#ifdef _WIN32
/*
* For MSVCRT based Win32 platforms,
* we need to provide the malloc/realloc wrapper function,
* which is prototyped in platform.h
*/
#include <errno.h>
void *mc_realloc( void *ptr, unsigned size )
{
void *retptr;
if( (retptr = realloc( ptr, size )) == NULL )
errno = ENOMEM;
return retptr;
}
#endif
/* Now, we define the helper functions, used to implement `catopen'.
*/
int mc_validate( __const char *name )
{
int fd;
/* Attempt to open the message catalogue specified by `name',
* on file descriptor `fd'; if successful, confirm that the file
* has the correct magic number, and an issue (version) number
* we can handle.
*/
if( (fd = open( name, O_RDONLY | O_BINARY )) >= 0 )
{
MSGCAT test;
struct ident supported = { mk_msgcat_tag( MINGW32_CATGETS ) };
if( (read( fd, &test, sizeof( supported )) == sizeof( supported ))
&& (test.mc.magic_number == ((MSGCAT) supported).mc.magic_number)
&& (test.mc.version >= ((MSGCAT) supported).mc.version)
&& (lseek( fd, (off_t)(0), SEEK_SET ) == (off_t)(0)) )
/*
* Message catalogue passed validation;
* leave the file open, and return the descriptor.
*/
return fd;
/* If we get to here,
* message catalogue validation was unsuccessful,
* so release the file descriptor...
*/
close( fd );
}
/* ...and fall through, returning an invalid descriptor.
*/
return (int)(-1);
}
static
int mc_pop_locale( int LC_TYPE, char *working_locale, int retval )
{
/* This convenience function is used by `mc_nlspath_open', (below),
* to restore the application's working locale, before returning the
* status code specified by `retval'.
*/
setlocale( LC_TYPE, working_locale );
free( working_locale );
return retval;
}
static
int mc_check_break_code( wchar_t chk, wchar_t *break_code )
{
/* Helper function, called by `mc_nlspath_open;
* it checks a given character against each of the
* contextually valid delimiters, to establish where
* to split the LC_MESSAGES string into components.
*/
if( chk )
{
/* If we haven't run out of characters to parse...
*/
while( *break_code )
{
/* Check the current character against each valid delimiter,
* and return the character code if a match is found.
*/
if( chk == *break_code++ )
return (int)(chk);
}
}
/* Fall through on no match,
* returning zero, to tell `mc_nlspath_open' to keep parsing.
*/
return 0;
}
static
char *mc_nl_cat_locale_getenv( int flags )
{
char *retptr;
/* Retrieve any appropriate LC_MESSAGES or LANG setting string
* from the process environment, for use in interpreting NLSPATH.
*/
if( flags & NL_CAT_LOCALE )
{
/* When this flag is set, then we use the LC_MESSAGES setting,
* if any, noting that this may be overridden by LC_ALL.
*/
if( (((retptr = getenv( "LC_ALL" )) != NULL) && (*retptr != '\0'))
|| (((retptr = getenv( "LC_MESSAGES" )) != NULL) && (*retptr != '\0')) )
return retptr;
}
/* When NL_CAT_LOCALE is not set, or if LC_MESSAGES is not defined,
* fall back to using the LANG specification.
*/
if( ((retptr = getenv( "LANG" )) != NULL) && (*retptr != '\0') )
return retptr;
/* Fall through, if there is no appropriate specification
* in the process environment, returning NULL.
*/
return NULL;
}
static
int mc_nlspath_open( __const char *msgcat, unsigned flags )
{
# define mc_select(PREF, ALT) (mblen((PREF), MB_CUR_MAX) > 0) ? (PREF) : (ALT)
int fd, step, copy_index, headroom;
wchar_t chk; __const char *subst, *nlspath, *chkptr;
char nlsname[PATH_MAX + MB_LEN_MAX], *nlscopy;
char *nls_locale = NULL;
/* Save the application's working locale, so we can restore it later;
* ( we need to change it temporarily, because...
*/
char *saved_locale = strdup( setlocale( LC_CTYPE, NULL ) );
/* ...we must parse message catalogue and NLSPATH specifiers in the
* wchar_t domain, while using the system locale, to correctly handle
* Win32 multibyte character path names ).
*/
setlocale( LC_CTYPE, "" );
if( (step = mbtowc( &chk, (chkptr = msgcat), MB_CUR_MAX )) > 0 )
{
/* First, check if the given `msgcat' spec begins with a pair of
* characters which appear to represent a Win32 drive specifier.
*/
if( (chk != L'/') && (chk != L'\\') )
{
/* We assume that it is, when the first character is *not* a
* directory name separator, and the second *is* a colon...
*/
chkptr += step;
if( ((step = mbtowc( &chk, chkptr, MB_CUR_MAX )) > 0) && (chk == L':') )
/*
* ...and we flag this condition, simply by pretending that
* we found a normal directory name separator, (slash).
*/
chk = L'/';
}
/* Scan the given `msgcat' spec, checking for directory name separators...
*/
while( step > 0 )
{
if( (chk == L'/') || (chk == L'\\') )
{
/* The `msgcat' spec includes at least one directory name separator,
* so pass it back to `catopen', as an exact catalogue file reference.
*/
return mc_pop_locale( LC_CTYPE, saved_locale, mc_validate( msgcat ) );
}
step = mbtowc( &chk, (chkptr += step), MB_CUR_MAX );
}
/* The `msgcat' argument did not specify an explicit path name,
* so we must search the NLSPATH, to locate a suitable candidate;
* first check the environment for the user's NLSPATH specification,
* falling back to the built in default, if none is defined.
*/
if( (nlspath = getenv( "NLSPATH" )) == NULL )
nlspath = NLSPATH_DEFAULT;
/* Parse the NLSPATH, constructing prototypes for message catalogue paths
* from each component path template in turn; try each generated prototype
* in turn, for a valid message catalogue match; select and open the first
* catalogue successfully matched, or bail out if no match found.
*/
nlscopy = nlsname;
headroom = sizeof( nlsname );
do { step = mbtowc( &chk, nlspath, MB_CUR_MAX );
switch( chk )
{
case L'\0':
case NLSPATH_SEPARATOR_CHAR:
/*
* We reached the end of the current NLSPATH template component;
* add a terminator, and attempt to validate a matching message
* catalogue; return it immediately if successful.
*/
if( headroom >= MB_CUR_MAX )
{
wctomb( nlscopy, L'\0' );
if( (fd = mc_validate( mc_select( nlsname, msgcat ))) >= 0 )
return fd;
}
/* Couldn't find a valid message catalogue to match the current
* NLSPATH prototype; move on to the next template, if any, and
* try again.
*/
nlspath += step;
nlscopy = nlsname;
headroom = sizeof( nlsname );
break;
case L'%':
/*
* Found a substitution meta-character; need to interpret it.
*/
nlspath += step;
nlspath += (step = mbtowc( &chk, nlspath, MB_CUR_MAX ));
switch( chk )
{
case L'%':
/*
* It's a literal `%' character; check we have sufficient space,
* then append it to the prototype for the message catalogue name
* which we are currently constructing.
*/
if( headroom >= MB_CUR_MAX )
{
copy_index = wctomb( nlscopy, L'%' );
headroom -= copy_index;
nlscopy += copy_index;
}
else headroom = 0;
break;
case L'N':
/*
* This is a request to substitute the `msgcat' name, passed to
* this `catopen' request, into the assembled name prototype;
* again, check we have sufficient space, before proceeding.
*/
subst = msgcat;
while( (copy_index = mbtowc( &chk, subst, MB_CUR_MAX )) > 0 )
{
if( headroom > copy_index )
{
headroom -= copy_index;
while( copy_index-- )
*nlscopy++ = *subst++;
}
else headroom = 0;
}
break;
case L'l': /* the `language' specified for the locale */
case L't': /* the associated `territory' designation */
case L'c': /* and its `codeset' */
case L'L': /* the entire locale specification */
/*
* These are requests to substitute the specified components
* of the current locale specification, either as specified by
* `LC_MESSAGES' or `LANG' definitions in the environment, if
* present, otherwise for the system locale.
*/
if( (nls_locale != NULL)
|| ((nls_locale = mc_nl_cat_locale_getenv( flags )) != NULL)
|| ((nls_locale = setlocale( LC_MESSAGES, NULL )) != NULL) )
{
wchar_t *break_code = L"_.@";
subst = nls_locale;
if( chk == L'L' )
{
break_code += 3;
}
else if( (chk == L't') || (chk == L'c') )
{
if( chk == L'c' )
++break_code;
do subst += (copy_index = mbtowc( &chk, subst, MB_CUR_MAX ));
while( (copy_index > 0) && (chk != *break_code) );
if( *++break_code == L'@' )
++break_code;
}
while( ((copy_index = mbtowc( &chk, subst, MB_CUR_MAX )) > 0)
&& ! mc_check_break_code( chk, break_code ) )
{
if( headroom > copy_index )
{
headroom -= copy_index;
while( copy_index-- )
*nlscopy++ = *subst++;
}
else headroom = 0;
}
}
break;
}
break;
default:
/*
* Any regular character is simply copied to the constructed path,
* provided there is sufficient space available.
*/
if( headroom >= step )
{
headroom -= step;
for( copy_index = 0; copy_index < step; copy_index++ )
*nlscopy++ = *nlspath++;
}
else headroom = 0;
}
}
while( step > 0 );
}
/* We fall through to here, if the `msgcat' spec was empty, or NULL;
* so just return it as is, and leave `catopen' to clean up.
*/
return mc_pop_locale( LC_CTYPE, saved_locale, (int)(-1) );
}
static
void *mc_open( struct mc_tab *cdt, va_list argv )
{
/* This call-back function provides the actual `catopen' implementation.
* It is invoked via `_mctab_', with `argv' specifying the actual path name
* for the required message catalogue file, followed by the `catopen' flags.
*
* It is responsible for assigning a message catalogue descriptor, allocating
* space in the descriptor table if necessary. It then opens the requested
* catalogue file, validating it as a valid message catalogue, allocates
* memory, and loads the entire catalogue into the allocated space.
*/
char *catname = va_arg( argv, char * );
unsigned tab_increment, flags = va_arg( argv, unsigned );
int retval = 0;
/* First, check the `catopen' flags, to determine how many new descriptor
* table slots to create, if we need to expand the table.
*/
if( (tab_increment = flags & NL_CATD_BLKSIZ_MAX) > 0 )
cdt->grow_size = tab_increment;
/* Find the first free slot, if any, in the descriptor table.
*/
while( (retval < cdt->curr_size) && (cdt->tab[ retval ].fd >= 0) )
++retval;
/* Got an empty slot?
*/
if( retval == cdt->curr_size )
{
/* No: we need to expand the table; this requires us to reallocate the
* memory block, in which the table is stored, expanding it by the current
* table increment size, bailing out on failure.
*/
struct mc_descriptor *tmp = cdt->tab;
int new_size = retval + cdt->grow_size;
if( (tmp = mc_realloc( tmp, new_size * sizeof( struct mc_descriptor ))) == NULL )
return (void *)(-1);
/* After successfully resizing the message catalogue descriptor table,
* initialise each new slot...
*/
cdt->tab = tmp;
while( retval < new_size )
{
/* Marking it as unused...
*/
tmp[ retval ].fd = -1;
/* and having no associated message data buffer...
*/
tmp[ retval++ ].data = NULL;
}
/* then fix up the reference for the slot we will use now,
* and adjust the record of the current table size.
*/
retval = cdt->curr_size;
cdt->curr_size = new_size;
}
/* Now, we have a message catalogue descriptor we can use,
* so open the specified file, recording the file descriptor
* within the message catalogue descriptor.
*/
if( (cdt->tab[ retval ].fd = mc_nlspath_open( catname, flags )) >= 0 )
{
/* After successfully opening the message catalogue file,
* we allocate the required memory for the message catalogue data buffer,
* and load the entire contents of the file into this buffer.
*/
struct stat catinfo;
struct mc_descriptor *ref = cdt->tab + retval;
if( (fstat( ref->fd, &catinfo ) != 0)
|| ((ref->data = mc_malloc( catinfo.st_size )) == NULL)
|| ((read( ref->fd, ref->data, catinfo.st_size )) < catinfo.st_size) )
{
/* If we get to here, we failed to load the message catalogue,
* so mark the descriptor as unused, free any resources we allocated,
* and fall through to bail out.
*/
_mc_free_( ref );
}
else
/*
* We should now have a valid, and open, message catalogue descriptor;
* it is actually of `nl_catd' type, but `_mctab_' requires us to return
* a generic `void *' pointer; we do that, leaving `catopen' to cast it
* to the ultimately expected `nl_catd' type.
*/
return (void *) retval;
}
/* If we fall through to here, then something went wrong;
* there should be nothing to clean up, so just return a `failed' status.
*/
return (void *)(-1);
}
/* The actual `catopen' implementation is trivial...
*/
nl_catd catopen( __const char *name, int flags )
{
/* ...with all the hard work done by the `mc_open' call-back from `_mctab_'.
*/
return (nl_catd)_mctab_( mc_open, name, flags );
}
/* $RCSfile$Revision: 1.4 $: end of file */