Debian Codegraph @

msgcmp.c

raw
1 /*
2 * Compile text message files into binary files
3 *
4 * Expects any number of files specified on the command line
5 * and outputs an equivalent number of binary files with the
6 * same name (with .mtb extension) in the local directory.
7 *
8 * Format of input files:
9 * xxx,yyy,ttttttttttttttttttttttttt
10 * (type),pppppppppppppppppp
11 *
12 * where xxx is errno
13 * yyy is suberrno
14 * ttttt is error message text
15 * and (type) is error prefix type
16 * ppppp is error prefix string
17 *
18 * For each file:
19 * get file size
20 * allocate this much memory for text strings
21 * allocate 1000 structs for
22 * read all lines into memory counting number of rows and putting
23 * text lines into memory chunk, and mesage lengths into struct
24 * array, and offsets (0 being start of memory chunk)
25 * write out number of entries
26 * iterate through struct arrays, adding file offset of start of
27 * text messages based on n number of structs
28 * write out array of structs
29 * write out memory chunck of error texts
30 */
31
32 /*
33 * Bug in LCC complier wchar.h that incorrectly says it defines stat struct
34 * but doesn't
35 */
36 #if defined(__LCC__)
37 # include <sys/stat.h>
38 #endif
39
40 #include "rexx.h"
41
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
48 #endif
49
50 #include "rexxmsg.h"
51
52 /*
53 * Static pointers to language-specific error messages
54 * IF THIS EVER CHANGES, ALSO CHANGE THE SAME TABLE IN error.c
55 */
56 static const char *errlang[] =
57 {
58 "en", /* english */
59 "de", /* german */
60 "es", /* spanish */
61 "no", /* norwegian */
62 "pt", /* portuguese */
63 "pl", /* polish */
64 "sv", /* swedish */
65 "tr", /* turkish */
66 #if 0
67 "en","ca","cs","da","de","el","es","fi","fr","he","hu","is","it","ja","ko",
68 "lt","nl","no","pl","pt","ru","sk","sl","sv","th","zh",
69 #endif
70 } ;
71
72 unsigned int getlanguage( char *lang )
73 {
74 int i,size;
75
76 size = sizeof(errlang) / sizeof(errlang[0]);
77 for ( i = 0; i < size; i++)
78 {
79 if ( strcmp( errlang[i], lang ) == 0 )
80 return i;
81 }
82 return 255;
83 }
84
85 int main( int argc, char *argv[] )
86 {
87 struct stat statbuf;
88 FILE *infp, *outfp;
89 char line[512];
90 int i,j,len,end=0,start=0,text_size;
91 char *fn;
92 char *text;
93 struct textindex *pti,*ti;
94 unsigned int count,prefix_count,total_count,offset,lang;
95 int last_count=0,last_prefix_count=0;
96
97 ti = (struct textindex *)malloc( 500*sizeof(struct textindex) );
98 if ( ti == NULL )
99 {
100 fprintf( stderr, "Unable to allocate memory for 500 message structures.\n" );
101 exit(1);
102 }
103 for ( i = 1; i < argc; i++ )
104 {
105 fn = argv[i];
106 infp = fopen( fn, "r" );
107 if ( infp == NULL )
108 {
109 fprintf( stderr, "Unable to open %s for reading.\n", fn );
110 free(ti);
111 exit(1);
112 }
113 stat( fn, &statbuf );
114 text = (char *)malloc( statbuf.st_size );
115 if ( text == NULL )
116 {
117 fprintf( stderr, "Unable to allocate %d bytes of memory.\n", (int) statbuf.st_size );
118 free(ti);
119 exit(1);
120 }
121 /*
122 * Read each line from the file...
123 */
124 text_size = 0;
125 pti = ti;
126 for ( count = 0, prefix_count = 0; ; )
127 {
128 if ( fgets( line, 511, infp ) == NULL )
129 break;
130 if ( line[0] == '#' )
131 ;
132 else if ( line[0] == 'P' )
133 {
134 line[5] = '\0';
135 pti->errorno = 999;
136 pti->suberrorno = atol( line+2 );
137 pti->fileoffset = text_size;
138 pti->textlength = strlen( line+6)-1;
139 memcpy( text+text_size, line+6, pti->textlength);
140 text_size += pti->textlength;
141 prefix_count++;
142 pti++;
143 }
144 else
145 {
146 line[3] = '\0';
147 line[7] = '\0';
148 pti->errorno = atol( line );
149 pti->suberrorno = atol( line+4 );
150 pti->fileoffset = text_size;
151 pti->textlength = strlen( line+8)-1;
152 memcpy( text+text_size, line+8, pti->textlength);
153 text_size += pti->textlength;
154 count++;
155 pti++;
156 }
157 }
158 fclose( infp );
159 if ( last_count != 0 && count != (unsigned) last_count )
160 {
161 fprintf( stderr, "Inconsistent numbers of error messages (%d) between this file %s and the previous one (%d).\n", count, fn, last_count );
162 free(ti);
163 exit(1);
164 }
165 if ( last_prefix_count != 0 && prefix_count != (unsigned) last_prefix_count )
166 {
167 fprintf( stderr, "Inconsistent numbers of prefix messages (%d) between this file %s and the previous one (%d).\n", prefix_count, fn, last_prefix_count );
168 free(ti);
169 exit(1);
170 }
171 last_count = count;
172 last_prefix_count = prefix_count;
173 total_count = count + prefix_count;
174 /* generate output file name */
175 len = strlen( argv[i] );
176 for ( j = len-1; j >= 0; j-- )
177 {
178 if ( argv[i][j] == '.' )
179 end = j;
180 if ( argv[i][j] == '/' || argv[i][j] == '\\')
181 {
182 start = j;
183 break;
184 }
185 }
186 memcpy( line, fn+start+1, end-start );
187 line[end-start-1] = '\0';
188 if ( ( lang = getlanguage( line ) ) == 255 )
189 {
190 fprintf( stderr, "Unknown language file name %s.\n", line );
191 free(ti);
192 exit(1);
193 }
194 strcat( line, ".mtb" );
195 outfp = fopen( line, "wb" );
196 if ( outfp == NULL )
197 {
198 fprintf( stderr, "Unable to open %s for writing.\n", line );
199 free(ti);
200 exit(1);
201 }
202 if ( fwrite( (void *)&count, sizeof(unsigned int), 1, outfp ) != 1 )
203 {
204 fprintf( stderr, "Unable to write message count to %s.\n", line );
205 free(ti);
206 exit(1);
207 }
208 if ( fwrite( (void *)&lang, sizeof(unsigned int), 1, outfp ) != 1 )
209 {
210 fprintf( stderr, "Unable to write message lang to %s.\n", line );
211 free(ti);
212 exit(1);
213 }
214 if ( fwrite( (void *)&prefix_count, sizeof(unsigned int), 1, outfp ) != 1 )
215 {
216 fprintf( stderr, "Unable to prefix message count to %s.\n", line );
217 free(ti);
218 exit(1);
219 }
220 /* update the file offsets */
221 offset = total_count * sizeof( struct textindex ) + ( 3 * sizeof( unsigned int ) );
222 pti = ti;
223 for ( j = 0; (unsigned) j < total_count; j++ )
224 {
225 pti->fileoffset += offset;
226 pti++;
227 }
228 /* write the index structs */
229 pti = ti;
230 for ( j = 0; (unsigned) j < total_count; j++ )
231 {
232 if ( fwrite( (void *)pti, sizeof(struct textindex), 1, outfp ) != 1 )
233 {
234 fprintf( stderr, "Unable to write index struct %d to %s.\n", j, line );
235 free(ti);
236 exit(1);
237 }
238 pti++;
239 }
240 /* write the text messages */
241 if ( (int) fwrite( text, sizeof(char), text_size, outfp ) != text_size )
242 {
243 fprintf( stderr, "Unable to write text messages to %s.\n", line );
244 free(ti);
245 exit(1);
246 }
247 fclose( outfp );
248 free( text );
249 }
250 free(ti);
251 printf( "%d error messages and %d prefixes compiled\n", last_count, prefix_count );
252 return 0;
253 }