summaryrefslogtreecommitdiff
path: root/app/help/genmessages.c
blob: 349e81e8a45e62edfd140e16978679cacb6341ae (plain)
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
/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *						2007 Martin Fischer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef WINDOWS
	#if _MSC_VER >=1400
		#define strdup _strdup
	#endif
#endif

#define I18NHEADERFILE "i18n.h"

typedef struct helpMsg_t * helpMsg_p;
typedef struct helpMsg_t {
	char * key;
	char * title;
	char * help;
	} helpMsg_t;

helpMsg_t helpMsgs[200];
int helpMsgCnt = 0;

struct transTbl {
      char *inChar;
      char *outChar[];
};

/* ATTENTION: make sure that the characters are in the same order as the equivalent escape sequences below */

/* translation table for unicode sequences understood by Halibut */
struct transTbl toUnicode = {
       "\xB0\0",
      { "\\u00B0",
		  "\\0"  }
};

/* translation table for escape sequences understood by C compiler */

struct transTbl toC = {
      "\n\\\"\0", 
      { "\\n",		
		"\\\\",
      "\\\"",
		"\\0" }
};


char *
TranslateString( char *srcString, struct transTbl *trTbl )
{
	char *destString;
   char *cp;
   char *cp2;
   size_t bufLen = strlen( srcString ) + 1;
   char *idx;

   /* calculate the expected result length */
   for( cp = srcString; *cp; cp++ )
   	if( idx = strchr( trTbl->inChar, *cp ))         /* does character need translation ? */
      	bufLen += strlen( (trTbl->outChar)[idx - trTbl->inChar ] ) - 1; /* yes, extend buffer accordingly */

   /* allocate memory for result */
   destString = malloc( bufLen );

   if( destString ) {
		/* copy and translate characters as needed */
      cp2 = destString;
      for( cp = srcString; *cp; cp++ ) {
      	if( idx = strchr( trTbl->inChar, *cp )) {       /* does character need translation ? */
         	strcpy( cp2, (trTbl->outChar)[idx - trTbl->inChar ] );   /* yes, copy the escaped character sequence */
            cp2 += strlen((trTbl->outChar)[idx - trTbl->inChar ] );
         } else {
            *cp2++ = *cp;                       /* no, just copy the character */
         }
		}
   	/* terminate string */
   	*cp2 = '\0';
   } else {
   	/* memory allocation failed */
      exit(1);
   }

   return( destString );
}


int cmpHelpMsg( const void * a, const void * b )
{
	helpMsg_p aa = (helpMsg_p)a;
	helpMsg_p bb = (helpMsg_p)b;
	return strcmp( aa->title, bb->title );
}

void unescapeString( FILE * f, char * str ) 
{
	while (*str) {
		if (*str != '\\')
			fputc( *str, f );
		str++;
	}
}

/**
 * Generate the file in help source format ( ie. the BUT file )
 */

void dumpHelp( FILE *hlpsrcF )
{
	int inx;
	char *transStr;
	
	fputs( "\\#\n * DO NOT EDIT! This file has been automatically created by genmessages.\n * Changes to this file will be overwritten.\n", hlpsrcF );

	fprintf( hlpsrcF, "\n\n\\H{messageList} Message Explanations\n\n" );

	/* sort in alphabetical order */
	qsort( helpMsgs, helpMsgCnt, sizeof helpMsgs[0], cmpHelpMsg );

	/* now save all the help messages */
	for ( inx=0; inx<helpMsgCnt; inx++ ) {
	
		transStr = TranslateString( helpMsgs[inx].title, &toUnicode );
		fprintf( hlpsrcF, "\\S{%s} %s\n\n", helpMsgs[inx].key, transStr );
		free( transStr );
		
		transStr = TranslateString( helpMsgs[inx].help, &toUnicode );
		fprintf( hlpsrcF, "%s\n\n", transStr );
		free( transStr );			

		fprintf( hlpsrcF, "\n\n\\rule\n\n" );
	}
	
}


int main( int argc, char * argv[] )
{
	FILE * hdrF;
	FILE *inF;
	FILE *outF;

	char buff[ 4096 ];
	char * cp;
	int inFileIdx;
	enum {m_init, m_title, m_alt, m_help } mode = m_init;
	char msgName[256];
	char msgAlt[256];
	char msgTitle[1024];
	char msgTitle1[1024];
	char msgHelp[4096];
	char *tName, *tAlt, *tTitle;
#ifndef FLAGS	
	int flags; 
#endif
	int i18n = 0;

	/* check argument count */
	if ( argc < 3 || argc > 4 ) {
		fprintf( stderr, "Usage: %s [-i18n] INFILE OUTFILE\n\n", argv[0] );
		fprintf( stderr, "       -i18n is used to generate a include file with gettext support.\n\n" );
		exit(1);
	}

	/* check options */
	if( argc == 4 ) {
		if( !strcmp(argv[ 1 ], "-i18n")){
			i18n = 1;
			inFileIdx = 2;	/* second argument is input file */
		}
		/* inFileIdx = 2;  skip over option argument */
	} else {
		inFileIdx = 1;	/* first argument is input file */
	}	

	/* open the file for reading */
	inF = fopen( argv[ inFileIdx ], "r" );
	if( !inF ) {
			fprintf( stderr, "Could not open %s for reading!\n", argv[ inFileIdx ] );
			exit( 1 );
	}		
			
	/* open the include file to generate */		
	hdrF = fopen( "messages.h", "w" );
	if( !hdrF ) {
			fprintf( stderr, "Could not open messages.h for writing!\n" );
			exit( 1 );
	}		

	fputs( "/*\n * DO NOT EDIT! This file has been automatically created by genmessages.\n * Changes to this file will be overwritten.\n */\n", hdrF );
	
	/* open the help file to generate */
	outF = fopen( argv[ inFileIdx + 1 ], "w" );
	if( !inF ) {
			fprintf( stderr, "Could not open %s for writing!\n", argv[ inFileIdx ] );
			exit( 1 );
	}		

	/* Include i18n header, if needed */
	if (i18n)
		fprintf( hdrF, "#include \"" I18NHEADERFILE "\"\n\n" );

	while ( fgets( buff, sizeof buff, inF ) ) {
	
		/* skip comment lines */
		if ( buff[0] == '#' )
			continue;

		/* remove trailing whitespaces */	
		cp = buff+strlen(buff)-1;
		while( cp >= buff && isspace(*cp)) {
			*cp = '\0';
			cp--;
		}

		if ( strncmp( buff, "MESSAGE ", 8 ) == 0 ) {

			/* skip any spaces */
			cp = strchr( buff+8, ' ' );
			if (cp)
				while (*cp == ' ') *cp++ = 0;
#ifndef FLAGS		
			if ( cp && *cp ) {
				flags = atoi(cp);
			} 
#endif			
			/* save the name of the message */
			strcpy( msgName, buff + 8 );
			msgAlt[0] = 0;
			msgTitle[0] = 0;
			msgTitle1[0] = 0;
			msgHelp[0] = 0;
			mode = m_title;
		} else if ( strncmp( buff, "ALT", 3 ) == 0 ) {
			mode = m_alt;
			msgAlt[0] = 0;			
		} else if ( strncmp( buff, "HELP", 4 ) == 0 ) {
			mode = m_help;
		} else if ( strncmp( buff, "END", 3 ) == 0 ) {
			/* the whole message has been read */

			/* create escape sequences */
			tName = TranslateString( msgName, &toC );
			tTitle = TranslateString( msgTitle, &toC );	
			tAlt = TranslateString( msgAlt, &toC );
			
			if (msgHelp[0]==0) {
				/* no help text is included */
				if (i18n)
					fprintf( hdrF, "#define %s N_(\"%s\")\n", tName, tTitle );
				else
					fprintf( hdrF, "#define %s \"%s\"\n", tName, tTitle );
			} else if (msgAlt[0]) {
				/* a help text and an alternate description are included */
				if (i18n)
					fprintf( hdrF, "#define %s N_(\"%s\\t%s\\t%s\")\n", tName, tName, tAlt, tTitle );
				else
					fprintf( hdrF, "#define %s \"%s\\t%s\\t%s\"\n", tName, tName, tAlt, tTitle );
			} else {
				/* a help text but no alternate description are included */
				if (i18n)
					fprintf( hdrF, "#define %s N_(\"%s\\t%s\")\n", tName, tName, tTitle );
				else
					fprintf( hdrF, "#define %s \"%s\\t%s\"\n", tName, tName, tTitle );
			}
			
			/*free temp stzrings */
			free( tName );
			free( tTitle );
			free( tAlt );
			
			/* save the help text for later use */
			if (msgHelp[0]) {
				helpMsgs[helpMsgCnt].key = strdup(msgName);
				if ( msgAlt[0] )
				    helpMsgs[helpMsgCnt].title = strdup(msgAlt);
				else
				    helpMsgs[helpMsgCnt].title = strdup(msgTitle);
				helpMsgs[helpMsgCnt].help = strdup(msgHelp);
				helpMsgCnt++;
			}
			mode = 0;
		} else {
			/* are we currently reading the message text? */
			if (mode == m_title) {
				/* yes, is the message text split over two lines ? */
				if (msgTitle[0]) {
					/* if yes, keep the first part as the short text */			
					if (msgAlt[0] == 0) {
						strcpy( msgAlt, msgTitle );
						strcat( msgAlt, "..." );
					}
					/* add a newline to the first part */
					strcat( msgTitle, "\n" );
				}
				/* now save the buffer into the message title */
				strcat( msgTitle, buff );
			} else if (mode == m_alt) {
				/* an alternate text was explicitly specified, save */
				if( msgAlt[ 0 ] ) {
					strcat( msgAlt, " " );
					strcat( msgAlt, buff );
				} else {					
					strcpy( msgAlt, buff );
				}	
			} else if (mode == m_help) {
				/* we are reading the help text, save in buffer */
				strcat( msgHelp, buff );
				strcat( msgHelp, "\n" );
			}
		}
	}
	dumpHelp( outF );

	fclose( hdrF );
	fclose( inF );
	fclose( outF );
	
	printf( "%d messages\n", helpMsgCnt );
	return 0;
}