[bfb799]: gencat.c Maximize Restore History

Download this file

gencat.c    397 lines (352 with data), 11.7 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
/*
* gencat.c
*
* $Id$
*
* Copyright (C) 2006, 2007, 2008, Keith Marshall
*
* This file implements the `main' function for the `gencat' program.
*
* Written by Keith Marshall <keithmarshall@users.sourceforge.net>
* Last modification: 09-Jan-2008
*
*
* 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 PROGRAM_IDENTITY progname, PACKAGE_VERSION
#define AUTHOR_IDENTITY "Keith Marshall"
#define AUTHOR_ATTRIBUTION AUTHOR_IDENTITY" for the MinGW Project"
#define COPYRIGHT_YEARS "2006, 2007, 2008"
#define COPYRIGHT_HOLDER COPYRIGHT_YEARS, AUTHOR_IDENTITY
#define COPYRIGHT_NOTICE MSG_COPYRIGHT_NOTICE, COPYRIGHT_HOLDER
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <errno.h>
char *progname, *invoked;
#include <gencat.h>
#include <gcmsgs.h>
#include <msgcat.h>
#include <mctab.h>
#include <platform.h>
#include <debug.h>
int gencat_errno = 0;
nl_catd gencat_messages;
/* Option control flags, for printing of program identification banners,
* as required for `help', `version' and copyright notifications.
*/
#define print_top_banner 0x08
#define print_nl_duplicate 0x04
/* Standard arguments, to include in a program identification banner.
*/
#define top_banner_text PROGRAM_IDENTITY, AUTHOR_ATTRIBUTION
static
void banner_printf( int opts, int set, int msg, const char *fmt, ... )
{
/* A helper function, for printing banner notifications.
*/
va_list args;
const char *nl_fmt = catgets( gencat_messages, set, msg, NULL );
if( opts & print_top_banner )
printf( catgets( gencat_messages, MSG_PROGRAM_BANNER ), top_banner_text );
va_start( args, fmt );
if( (nl_fmt == NULL) || ((opts & print_nl_duplicate) != 0) )
/*
* A national language translation has been provided, but the
* English version must also be displayed, or there is no available
* translation, so we just print the English text anyway.
*/
vprintf( fmt, args );
if( nl_fmt != NULL )
/*
* When we found a national language translation, we print it.
*/
vprintf( nl_fmt, args );
va_end( args );
}
int main( int argc, char **argv )
{
struct option options[] =
{
/* The options understood by this version of `gencat';
* all are long form, to be evaluated by getopt_long_only().
*/
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
char *msgcat;
char *outfile = NULL;
int opt, mc, numsets, setnum, msgcount;
off_t offset, msg_index_offset;
char tmpcat[] = "mcXXXXXX";
struct ident tag = { mk_msgcat_tag( MINGW32_CATGETS )};
MSGCAT cat_index; struct key *set_index, *msg_index;
struct msgdict *cat, *curr;
# ifdef _O_BINARY
/*
* This is specific to the Win32 platform;
* we must ensure that all file I/O operations are performed
* in BINARY translation mode.
*
*/
_fmode = _O_BINARY;
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
# endif
progname = basename( invoked = *argv );
gencat_messages = catopen( "gencat", NL_CAT_LOCALE );
if( (*argv = strdup( progname )) != NULL )
{
/* Set up the form of the program name to display in diagnostics;
* exclude the path component, and discard any `.exe' suffix.
*/
int ext = strlen( *argv ) - 4;
if( (ext > 0) && (strcasecmp( *argv + ext, ".exe" ) == 0) )
*(*argv + ext) = '\0';
}
else
/* Something went wrong...
* retain the basename from the original program path name,
* as the effective program name.
*/
*argv = progname;
/* Evaluate any options, specified on the command line.
*/
while( (opt = getopt_long_only( argc, argv, "vh", options, NULL )) != -1 )
switch( opt )
{
case 'h':
/*
* This is a request to display a help message.
*/
banner_printf( print_top_banner, MSG_GENCAT_SYNOPSIS, *argv );
exit( EXIT_SUCCESS );
case 'v':
/*
* And this is for display of version and copyright info.
*/
banner_printf( print_top_banner + print_nl_duplicate, COPYRIGHT_NOTICE );
exit( EXIT_SUCCESS );
}
progname = *argv;
cat_index.id = tag;
if( (argc -= optind) > 1 )
{
/* Establish the message catalogue name, recognising `/dev/stdout'
* as an alias for `-', representing the standard output stream.
*/
if( strcasecmp( (msgcat = *(argv += optind)), "/dev/stdout" ) == 0 )
msgcat = "-";
/* Initialise the message list, to incorporate any messages which
* are already contained within the specified message catalogue.
*/
if( (cat = mc_load( msgcat )) == NULL )
switch( errno )
{
case ENOENT:
/*
* Not an error; the target catalogue simply doesn't yet exist.
*/
dfprintf(( stderr, "ignoring non-existent catalogue %s; errno = %d, ENOENT = %d\n", msgcat, errno, ENOENT ));
break;
default:
fprintf( errmsg( MSG_BAD_CATALOGUE ), progname, msgcat );
return EXIT_FAILURE;
}
/* Merge new or updated message definitions from input files.
*/
while( --argc )
cat = mc_merge( cat, mc_source( *++argv ));
/* Walk the resultant in-memory linked message list, counting...
*/
msgcount = numsets = setnum = 0;
for( curr = cat; curr != NULL; curr = curr->link )
{
/* the number of individual messages defined...
*/
++msgcount;
if( curr->set > setnum )
{
/* and the number of distinct message sets,
* to which they are allocated.
*/
++numsets;
setnum = curr->set;
}
}
dfprintf(( stderr, "%u messages in %u sets\n", msgcount, numsets ));
/* Compute the required image size for the message catalogue index,
* and allocate memory in which to construct it...
*/
offset = ( numsets + msgcount ) * sizeof( struct key );
if( (set_index = mc_malloc( offset )) == NULL )
{
/* bailing out, if insufficient memory.
*/
fprintf( errmsg( MSG_OUT_OF_MEMORY ), progname );
return( EXIT_FAILURE );
}
/* Locate the start of the message index entries,
* within the composite set and message index image.
*/
msg_index = set_index + numsets;
/* Create a temporary output file,
* which will eventually become the generated message catalogue,
* or will be copied to stdout, as appropriate...
*/
if( (mc = mkstemp( tmpcat )) < 0 )
{
/* ...diagnosing, and bailing out if we can't. */
fprintf( errmsg( MSG_OUTPUT_NOFILE ), progname, tmpcat );
return EXIT_FAILURE;
}
/* Seek forward in the temporary output file,
* to reserve space into which we will later write the message
* catalogue header record, and the message index.
*/
offset = lseek( mc, sizeof( MSGCAT ) + offset, SEEK_SET );
/* Compute the offset, ON DISK, for the start of the message
* index, within the composite set and message index image, in
* terms of its physical byte count from start of file.
*/
msg_index_offset = sizeof( MSGCAT ) + numsets * sizeof( struct key );
/* Forcing a set number transition into the first message set...
*/
setnum = 0;
/*
* Walk the in-memory linked message list again...
*/
for( curr = cat; curr != NULL; curr = curr->link )
{
/* adding a new set index entry, on each set number transition...
*/
if( curr->set > setnum )
{
/* incorporating the applicable set number,
* and the ON-DISK byte offset for the index entry
* associated with its first included message.
*/
set_index->setnum = setnum = curr->set;
(set_index++)->offset = msg_index_offset;
}
/* Adjust the cumulative computed value of the ON-DISK
* message index offset, to account for each message traversed...
*/
msg_index_offset += sizeof( struct key );
/*
* while incorporating the appropriate message number,
* and message data offset, into the in-memory image of
* the message index.
*/
msg_index->msgnum = curr->msg;
(msg_index++)->offset = offset;
/*
* and write out the message data, to the catalogue file,
* updating `offset' to track where the data for the NEXT message,
* if any, is to be written.
*/
offset += write( mc, curr->base + curr->loc, curr->len );
}
/* Store the effective data size of the catalogue into the header.
*/
cat_index.mc.extent = offset;
/* Rewind, and write out the message catalogue header record,
* at the start of the file space reserved previously.
*/
lseek( mc, 0L, SEEK_SET );
write( mc, &cat_index, sizeof( MSGCAT ) );
/* Locate the start of the in-memory image of the index, once more,
* and copy it into the remaining reserved space in the output file.
*/
set_index = msg_index - numsets - msgcount;
write( mc, set_index, (numsets + msgcount) * sizeof( struct key ) );
/* Check message catalogue generation status...
*/
if( gencat_errno == 0 )
{
/* Completed without error...
*/
if( outfile == NULL )
/*
* Always true, at present...
* (this is here to accommodate a possible future implementation
* of an `--output-file=NAME' option, to facilitate including an
* existing message catalogue into a new, and differently named,
* derivative message catalogue).
*/
outfile = msgcat;
if( strcmp( outfile, "-" ) == 0 )
{
/* This is emitting the message catalogue content to the standard
* output stream; rewind the temporary catalogue file, read back,
* and then rewrite its content to the stdout stream.
*/
int count;
char buf[ BUFSIZ ];
lseek( mc, (off_t)(0), SEEK_SET );
while( (count = read( mc, buf, BUFSIZ )) > 0 )
write( STDOUT_FILENO, buf, count );
/* When done, close and delete the temporary file.
*/
close( mc );
remove( tmpcat );
}
else
{
/* This is saving the message catalogue as a permanent disk file,
* overwriting any previously existing version; this is achieved by
* simply renaming the temporary file, and setting its attributes
* appropriately.
*/
close( mc );
rename( tmpcat, outfile );
chmod( outfile, 0644 );
}
}
else
{ /* An error occurred...
* The temporary file is likely to be corrupt,
* so simply discard it, and report failure.
*/
close( mc );
remove( tmpcat );
return EXIT_FAILURE;
}
}
else
{ /* User specified insufficient command line arguments.
* Diagnose, and bail out.
*/
fprintf( errmsg( MSG_MISSING_ARGS ), progname );
fprintf( errmsg( MSG_GENCAT_USAGE ), progname );
return EXIT_FAILURE;
}
/* On successful completion, report it.
*/
return EXIT_SUCCESS;
}
/* $RCSfile$Revision: 1.5 $: end of file */