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

[3f615e]: gencat.c Maximize Restore History

Download this file

gencat.c    211 lines (181 with data), 5.3 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
/*
* gencat.c
*
* $Id$
*
* Copyright (C) 2006, 2007, Keith Marshall
*
* This file implements the `main' function for the `gencat' program.
*
* Written by Keith Marshall <keithmarshall@users.sourceforge.net>
* Last modification: 05-Mar-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.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.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;
int main( int argc, char **argv )
{
char *msgcat;
char *outfile = NULL;
int 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 );
cat_index.id = tag;
if( --argc > 1 )
{
/* Initialise the message list, to incorporate any messages which
* are already contained within the specified message catalogue.
*/
if( (cat = mc_load( msgcat = *++argv )) == 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_CATLOAD_FAILED ), progname, msgcat );
return EXIT_FAILURE;
}
/* Merge new or updated message definitions from input files.
*/
while( --argc )
cat = mc_merge( cat, mc_source( *++argv ));
msgcount = numsets = setnum = 0;
for( curr = cat; curr != NULL; curr = curr->link )
{
++msgcount;
if( curr->set > setnum )
{
++numsets;
setnum = curr->set;
}
}
dfprintf(( stderr, "%u messages in %u sets\n", msgcount, numsets ));
offset = ( numsets + msgcount ) * sizeof( struct key );
if( (set_index = mc_malloc( offset )) == NULL )
{
fprintf( errmsg( MSG_OUT_OF_MEMORY ), progname );
return( EXIT_FAILURE );
}
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;
}
write( mc, &cat_index, sizeof( MSGCAT ) );
offset = lseek( mc, offset, SEEK_CUR );
setnum = 0;
msg_index_offset = sizeof( MSGCAT ) + numsets * sizeof( struct key );
for( curr = cat; curr != NULL; curr = curr->link )
{
if( curr->set > setnum )
{
set_index->setnum = setnum = curr->set;
(set_index++)->offset = msg_index_offset;
}
msg_index_offset += sizeof( struct key );
msg_index->msgnum = curr->msg;
(msg_index++)->offset = offset;
offset += write( mc, curr->base + curr->loc, curr->len );
}
set_index = msg_index - numsets - msgcount;
lseek( mc, sizeof( MSGCAT ), SEEK_SET );
write( mc, set_index, (numsets + msgcount) * sizeof( struct key ) );
if( gencat_errno == 0 )
{
if( outfile == NULL )
outfile = msgcat;
if( strcmp( outfile, "-" ) == 0 )
{
int count;
char buf[ BUFSIZ ];
lseek( mc, (off_t)(0), SEEK_SET );
while( (count = read( mc, buf, BUFSIZ )) > 0 )
write( STDOUT_FILENO, buf, count );
close( mc );
remove( tmpcat );
}
else
{
close( mc );
rename( tmpcat, outfile );
chmod( outfile, 0644 );
}
}
else
{
close( mc );
remove( tmpcat );
}
}
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;
}
return EXIT_SUCCESS;
}
/* $RCSfile$Revision$: end of file */