write.c 13.1 KB
Newer Older
1 2 3 4 5
/*
 * Wine Message Compiler output generation
 *
 * Copyright 2000 Bertho A. Stultiens (BS)
 *
6 7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

#include "wmc.h"
#include "utils.h"
#include "lang.h"
#include "write.h"

/*
 * The binary resource layout is as follows:
 *
 *         +===============+
 * Header  |    NBlocks    |
 *         +===============+
 * Block 0 |    Low ID     |
 *         +---------------+
 *         |    High ID    |
 *         +---------------+
 *         |    Offset     |---+
 *         +===============+   |
 * Block 1 |    Low ID     |   |
 *         +---------------+   |
 *         |    High ID    |   |
 *         +---------------+   |
 *         |    Offset     |------+
 *         +===============+   |  |
 *         |               |   |  |
 *        ...             ...  |  |
 *         |               |   |  |
 *         +===============+ <-+  |
 * B0 LoID |  Len  | Flags |      |
 *         +---+---+---+---+      |
 *         | b | l | a | b |      |
 *         +---+---+---+---+      |
 *         | l | a | \0| \0|      |
 *         +===============+      |
 *         |               |      |
 *        ...             ...     |
 *         |               |      |
 *         +===============+      |
 * B0 HiID |  Len  | Flags |      |
 *         +---+---+---+---+      |
 *         | M | o | r | e |      |
 *         +---+---+---+---+      |
 *         | b | l | a | \0|      |
 *         +===============+ <----+
 * B1 LoID |  Len  | Flags |
 *         +---+---+---+---+
 *         | J | u | n | k |
 *         +---+---+---+---+
 *         | \0| \0| \0| \0|
 *         +===============+
 *         |               |
 *        ...             ...
 *         |               |
 *         +===============+
 *
 * All Fields are aligned on their natural boundaries. The length
 * field (Len) covers both the length of the string and the header
 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
 * for normal character strings and 1 for unicode strings.
 */

static char str_header[] =
	"/* This file is generated with wmc version " WMC_FULLVERSION ". Do not edit! */\n"
	"/* Source : %s */\n"
	"/* Cmdline: %s */\n"
	"/* Date   : %s */\n"
	"\n"
        ;

static char *dup_u2c(int cp, const WCHAR *uc)
{
97 98
	int len;
	char *cptr;
99 100 101
	const union cptable *cpdef = find_codepage(cp);
	if(!cpdef)
		internal_error(__FILE__, __LINE__, "Codepage %d not found (vanished?)", cp);
102 103 104
	len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
	cptr = xmalloc(len);
	if((len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len, NULL, NULL)) < 0)
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
		internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", len);
	return cptr;
}

static void killnl(char *s, int ddd)
{
	char *tmp;
	tmp = strstr(s, "\r\n");
	if(tmp)
	{
		if(ddd && tmp - s > 3)
		{
			tmp[0] = tmp[1] = tmp[2] = '.';
			tmp[3] = '\0';
		}
		else
			*tmp = '\0';
	}
	tmp = strchr(s, '\n');
	if(tmp)
	{
		if(ddd && tmp - s > 3)
		{
			tmp[0] = tmp[1] = tmp[2] = '.';
			tmp[3] = '\0';
		}
		else
			*tmp = '\0';
	}
}

static int killcomment(char *s)
{
	char *tmp = s;
	int b = 0;
	while((tmp = strstr(tmp, "/*")))
	{
		tmp[1] = 'x';
		b++;
	}
	tmp = s;
	while((tmp = strstr(tmp, "*/")))
	{
		tmp[0] = 'x';
		b++;
	}
	return b;
}

void write_h_file(const char *fname)
{
	node_t *ndp;
	char *cptr;
	char *cast;
	FILE *fp;
	token_t *ttab;
	int ntab;
	int i;
	int once = 0;
	int idx_en = 0;

	fp = fopen(fname, "w");
	if(!fp)
	{
		perror(fname);
		exit(1);
	}
	cptr = ctime(&now);
	killnl(cptr, 0);
	fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);
175 176
	fprintf(fp, "#ifndef __WMCGENERATED_%08lx_H\n", (long)now);
	fprintf(fp, "#define __WMCGENERATED_%08lx_H\n", (long)now);
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
	fprintf(fp, "\n");

	/* Write severity and facility aliases */
	get_tokentable(&ttab, &ntab);
	fprintf(fp, "/* Severity codes */\n");
	for(i = 0; i < ntab; i++)
	{
		if(ttab[i].type == tok_severity && ttab[i].alias)
		{
			cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
			fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
			free(cptr);
		}
	}
	fprintf(fp, "\n");

	fprintf(fp, "/* Facility codes */\n");
	for(i = 0; i < ntab; i++)
	{
		if(ttab[i].type == tok_facility && ttab[i].alias)
		{
			cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
			fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
			free(cptr);
		}
	}
	fprintf(fp, "\n");

	/* Write the message codes */
	fprintf(fp, "/* Message definitions */\n");
	for(ndp = nodehead; ndp; ndp = ndp->next)
	{
		switch(ndp->type)
		{
		case nd_comment:
			cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
			killnl(cptr, 0);
			killcomment(cptr);
			if(*cptr)
				fprintf(fp, "/* %s */\n", cptr);
			else
				fprintf(fp, "\n");
			free(cptr);
			break;
		case nd_msg:
			if(!once)
			{
				/*
				 * Search for an english text.
				 * If not found, then use the first in the list
				 */
				once++;
				for(i = 0; i < ndp->u.msg->nmsgs; i++)
				{
					if(ndp->u.msg->msgs[i]->lan == 0x409)
					{
						idx_en = i;
						break;
					}
				}
				fprintf(fp, "\n");
			}
			fprintf(fp, "/* MessageId  : 0x%08x */\n", ndp->u.msg->realid);
			cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
			killnl(cptr, 0);
			killcomment(cptr);
			fprintf(fp, "/* Approx. msg: %s */\n", cptr);
			free(cptr);
			cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
			if(ndp->u.msg->cast)
				cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
			else
				cast = NULL;
			switch(ndp->u.msg->base)
			{
			case 8:
				if(cast)
					fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
				else
					fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
				break;
			case 10:
				if(cast)
					fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
				else
					fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
				break;
			case 16:
				if(cast)
					fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
				else
					fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
				break;
			default:
				internal_error(__FILE__, __LINE__, "Invalid base for number print");
			}
			free(cptr);
			if(cast)
				free(cast);
			break;
		default:
			internal_error(__FILE__, __LINE__, "Invalid node type %d", ndp->type);
		}
	}
	fprintf(fp, "\n#endif\n");
	fclose(fp);
}

static void write_rcbin(FILE *fp)
{
	lan_blk_t *lbp;
	token_t *ttab;
	int ntab;
	int i;

	get_tokentable(&ttab, &ntab);

	for(lbp = lanblockhead; lbp; lbp = lbp->next)
	{
		char *cptr = NULL;
		fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
		for(i = 0; i < ntab; i++)
		{
			if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
			{
				if(ttab[i].alias)
					cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
				break;
			}
		}
		if(!cptr)
			internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x", lbp->lan);
		fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
		free(cptr);
	}
}

static char *make_string(WCHAR *uc, int len, int codepage)
{
	char *str = xmalloc(7*len + 1);
	char *cptr = str;
	int i;
	int b;

	if(!codepage)
	{
		*cptr++ = ' ';
		*cptr++ = 'L';
		*cptr++ = '"';
		for(i = b = 0; i < len; i++, uc++)
		{
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
                        switch(*uc)
                        {
                        case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
                        case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
                        case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
                        case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
                        case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
                        case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
                        case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
                        case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
                        case '"':  *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
                        default:
                            if (*uc < 0x100 && isprint(*uc))
                            {
                                *cptr++ = *uc;
                                b++;
                            }
                            else
                            {
                                int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
                                cptr += n;
                                b += n;
                            }
                            break;
                        }
353 354 355 356 357 358 359 360 361 362 363
			if(i < len-1 && b >= 72)
			{
				*cptr++ = '"';
				*cptr++ = ',';
				*cptr++ = '\n';
				*cptr++ = ' ';
				*cptr++ = 'L';
				*cptr++ = '"';
				b = 0;
			}
		}
364 365 366 367
		if (unicodeout)
		    len = (len + 1) & ~1;
		else
		    len = (len + 3) & ~3;
368 369 370 371 372 373 374 375 376 377 378 379 380 381
		for(; i < len; i++)
		{
			*cptr++ = '\\';
			*cptr++ = 'x';
			*cptr++ = '0';
			*cptr++ = '0';
			*cptr++ = '0';
			*cptr++ = '0';
		}
		*cptr++ = '"';
		*cptr = '\0';
	}
	else
	{
382 383
		char *tmp, *cc;
		int mlen;
384 385 386
		const union cptable *cpdef = find_codepage(codepage);

		assert(cpdef != NULL);
387 388 389
		mlen = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
		cc = tmp = xmalloc(mlen);
		if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, mlen, NULL, NULL)) < 0)
390 391 392 393 394
			internal_error(__FILE__, __LINE__, "Buffer overflow? code %d.", i);
		*cptr++ = ' ';
		*cptr++ = '"';
		for(i = b = 0; i < len; i++, cc++)
		{
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
                        switch(*cc)
                        {
                        case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
                        case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
                        case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
                        case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
                        case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
                        case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
                        case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
                        case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
                        case '"':  *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
                        default:
                            if(isprint(*cc))
                            {
                                *cptr++ = *cc;
                                b++;
                            }
                            else
                            {
                                int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
                                cptr += n;
                                b += n;
                            }
                            break;
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
			}
			if(i < len-1 && b >= 72)
			{
				*cptr++ = '"';
				*cptr++ = ',';
				*cptr++ = '\n';
				*cptr++ = ' ';
				*cptr++ = '"';
				b = 0;
			}
		}
		len = (len + 3) & ~3;
		for(; i < len; i++)
		{
			*cptr++ = '\\';
			*cptr++ = 'x';
			*cptr++ = '0';
			*cptr++ = '0';
		}
		*cptr++ = '"';
		*cptr = '\0';
		free(tmp);
	}
	return str;
}

static void write_rcinline(FILE *fp)
{
	lan_blk_t *lbp;
	int i;
	int j;

	for(lbp = lanblockhead; lbp; lbp = lbp->next)
	{
453
		unsigned offs = 4 * (lbp->nblk * 3 + 1);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
		fprintf(fp, "\n1 MESSAGETABLE\n");
		fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
		fprintf(fp, "{\n");
		fprintf(fp, " /* NBlocks    */ 0x%08xL,\n", lbp->nblk);
		for(i = 0; i < lbp->nblk; i++)
		{
			fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
					lbp->blks[i].idlo,
					lbp->blks[i].idhi,
					offs);
			offs += lbp->blks[i].size;
		}
		for(i = 0; i < lbp->nblk; i++)
		{
			block_t *blk = &lbp->blks[i];
			for(j = 0; j < blk->nmsg; j++)
			{
				char *cptr;
				int l = blk->msgs[j]->len;
				char *comma = j == blk->nmsg-1  && i == lbp->nblk-1 ? "" : ",";
				cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
				fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
					blk->idlo + j,
477
					(unicodeout ? (l*2+3)&~3 : (l+3)&~3) + 4,
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
					unicodeout ? '1' : '0');
				fprintf(fp, "%s%s\n", cptr, comma);
				free(cptr);
			}
		}
		fprintf(fp, "}\n");
	}
}

void write_rc_file(const char *fname)
{
	FILE *fp;
	char *cptr;

	fp = fopen(fname, "w");
	if(!fp)
	{
		perror(fname);
		exit(1);
	}
	cptr = ctime(&now);
	killnl(cptr, 0);
	fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline, cptr);

	if(rcinline)
		write_rcinline(fp);
	else
		write_rcbin(fp);
	fclose(fp);
}

void write_bin_files(void)
{
	assert(rcinline == 0);
}