| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | msgcomp.cpp - T3 Message Compiler |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/14/00 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmerr.h" |
| 29 | #include "charmap.h" |
| 30 | #include "resload.h" |
| 31 | #include "vmregex.h" |
| 32 | #include "vmhash.h" |
| 33 | #include "vmimage.h" |
| 34 | |
| 35 | |
| 36 | /* ------------------------------------------------------------------------ */ |
| 37 | /* |
| 38 | * hash entry for a #define symbol |
| 39 | */ |
| 40 | class CVmHashEntryDef: public CVmHashEntryCS |
| 41 | { |
| 42 | public: |
| 43 | CVmHashEntryDef(const char *str, size_t len, int val) |
| 44 | : CVmHashEntryCS(str, len, TRUE) |
| 45 | { |
| 46 | val_ = val; |
| 47 | } |
| 48 | |
| 49 | /* the integer value of the #define symbol */ |
| 50 | int val_; |
| 51 | }; |
| 52 | |
| 53 | /* ------------------------------------------------------------------------ */ |
| 54 | /* |
| 55 | * read a header file |
| 56 | */ |
| 57 | static int read_header_file(int line_num, const char *fname, |
| 58 | CRegexSearcherSimple *searcher, |
| 59 | CVmHashTable *hashtab) |
| 60 | { |
| 61 | osfildef *fphdr; |
| 62 | |
| 63 | /* open the header file */ |
| 64 | fphdr = osfoprt(fname, OSFTTEXT); |
| 65 | if (fphdr == 0) |
| 66 | { |
| 67 | printf("line %d: unable to open header file \"%s\"\n", |
| 68 | line_num, fname); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | /* read the header file */ |
| 73 | for (;;) |
| 74 | { |
| 75 | static const char def_pat[] = "<space>*#<space>*define<space>+" |
| 76 | "((?:<alpha>|_)(?:<alphanum>|_)*)" |
| 77 | "<space>+" |
| 78 | "(<digit>+)"; |
| 79 | static size_t def_pat_len = sizeof(def_pat) - 1; |
| 80 | char buf[512]; |
| 81 | |
| 82 | /* read the next line */ |
| 83 | if (osfgets(buf, sizeof(buf), fphdr) == 0) |
| 84 | break; |
| 85 | |
| 86 | /* |
| 87 | * if the line is of the form "#define SYMBOL number", not it; |
| 88 | * otherwise, ignore it |
| 89 | */ |
| 90 | if (searcher->compile_and_match(def_pat, def_pat_len, |
| 91 | buf, buf, strlen(buf)) > 0) |
| 92 | { |
| 93 | const re_group_register *sym_reg; |
| 94 | const re_group_register *val_reg; |
| 95 | const char *sym; |
| 96 | size_t sym_len; |
| 97 | int val; |
| 98 | CVmHashEntryDef *entry; |
| 99 | |
| 100 | /* get the group register for the symbol being defined */ |
| 101 | sym_reg = searcher->get_group_reg(0); |
| 102 | sym = buf + sym_reg->start_ofs; |
| 103 | sym_len = sym_reg->end_ofs - sym_reg->start_ofs; |
| 104 | |
| 105 | /* get the group register for the value of the symbol */ |
| 106 | val_reg = searcher->get_group_reg(1); |
| 107 | val = atoi(buf + val_reg->start_ofs); |
| 108 | |
| 109 | /* add the symbol to our hash table */ |
| 110 | entry = new CVmHashEntryDef(sym, sym_len, val); |
| 111 | hashtab->add(entry); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* close the file */ |
| 116 | osfcls(fphdr); |
| 117 | |
| 118 | /* success */ |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* ------------------------------------------------------------------------ */ |
| 123 | /* |
| 124 | * Message structure |
| 125 | */ |
| 126 | struct msg_t |
| 127 | { |
| 128 | msg_t(int id, const char *short_msg, const char *long_msg) |
| 129 | { |
| 130 | /* remember the ID */ |
| 131 | id_ = id; |
| 132 | |
| 133 | /* remember the messages */ |
| 134 | short_msg_ = lib_copy_str(short_msg); |
| 135 | long_msg_ = lib_copy_str(long_msg); |
| 136 | |
| 137 | /* not in a list yet */ |
| 138 | nxt_ = 0; |
| 139 | } |
| 140 | |
| 141 | ~msg_t() |
| 142 | { |
| 143 | /* delete the message text buffers */ |
| 144 | lib_free_str(short_msg_); |
| 145 | lib_free_str(long_msg_); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * compare two messages to determine which comes first in message |
| 150 | * number order |
| 151 | */ |
| 152 | static int compare(const void *a0, const void *b0) |
| 153 | { |
| 154 | const msg_t *a = *(const msg_t **)a0; |
| 155 | const msg_t *b = *(const msg_t **)b0; |
| 156 | |
| 157 | /* compare the message numbers and return the result */ |
| 158 | if (a->id_ > b->id_) |
| 159 | return 1; |
| 160 | else if (a->id_ < b->id_) |
| 161 | return -1; |
| 162 | else |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* numeric ID of the message */ |
| 167 | int id_; |
| 168 | |
| 169 | /* short message text */ |
| 170 | char *short_msg_; |
| 171 | |
| 172 | /* long message text */ |
| 173 | char *long_msg_; |
| 174 | |
| 175 | /* next in list */ |
| 176 | msg_t *nxt_; |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | /* ------------------------------------------------------------------------ */ |
| 181 | /* |
| 182 | * Context structure for strict_check enumeration callback |
| 183 | */ |
| 184 | struct strict_check_ctx |
| 185 | { |
| 186 | /* the array of messages, sorted by message ID */ |
| 187 | msg_t **msg_array; |
| 188 | |
| 189 | /* the number of messages in the message array */ |
| 190 | int msg_count; |
| 191 | |
| 192 | /* number of symbols with no messages */ |
| 193 | int not_found_count; |
| 194 | }; |
| 195 | |
| 196 | /* |
| 197 | * Hash table symbol enumeration callback for strict checking. For each |
| 198 | * symbol, we verify that the symbol's message ID has a message defined |
| 199 | * in the message array. |
| 200 | */ |
| 201 | static void strict_check(void *ctx0, CVmHashEntry *entry0) |
| 202 | { |
| 203 | strict_check_ctx *ctx = (strict_check_ctx *)ctx0; |
| 204 | CVmHashEntryDef *entry = (CVmHashEntryDef *)entry0; |
| 205 | int hi, lo, cur; |
| 206 | |
| 207 | /* |
| 208 | * search for this message ID - the array is in sorted order, so we |
| 209 | * can perform a binary search to make the search reasonably quick |
| 210 | */ |
| 211 | lo = 0; |
| 212 | hi = ctx->msg_count - 1; |
| 213 | while (lo <= hi) |
| 214 | { |
| 215 | /* split the difference */ |
| 216 | cur = lo + (hi - lo)/2; |
| 217 | |
| 218 | /* is it a match? */ |
| 219 | if (entry->val_ == ctx->msg_array[cur]->id_) |
| 220 | { |
| 221 | /* |
| 222 | * found it - we don't need to warn about this symbol, so we |
| 223 | * can just return and get on with the rest of the |
| 224 | * enumeration |
| 225 | */ |
| 226 | return; |
| 227 | } |
| 228 | else if (entry->val_ > ctx->msg_array[cur]->id_) |
| 229 | { |
| 230 | /* we need to go higher */ |
| 231 | lo = (cur == lo ? cur + 1 : cur); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | /* we need to go lower */ |
| 236 | hi = (cur == hi ? hi - 1 : cur); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * We didn't find it - show this symbol. If this is the first one, |
| 242 | * also show the initial warning message, which prefixes the |
| 243 | * not-found symbol list. |
| 244 | */ |
| 245 | if (ctx->not_found_count == 0) |
| 246 | printf("warning: the following #define symbols have no " |
| 247 | "corresponding messages:\n"); |
| 248 | |
| 249 | /* show the symbol */ |
| 250 | printf(" %.*s (%d)\n", |
| 251 | (int)entry->getlen(), entry->getstr(), entry->val_); |
| 252 | |
| 253 | /* count it */ |
| 254 | ++(ctx->not_found_count); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /* ------------------------------------------------------------------------ */ |
| 259 | /* |
| 260 | * message compiler main entrypoint |
| 261 | */ |
| 262 | int main(int argc, char **argv) |
| 263 | { |
| 264 | char *fname_in; |
| 265 | char *fname_out; |
| 266 | osfildef *fpin = 0; |
| 267 | osfildef *fpout = 0; |
| 268 | CCharmapToUni *mapper = 0; |
| 269 | CResLoader *res_loader = 0; |
| 270 | CVmHashTable *hashtab = 0; |
| 271 | CRegexParser *regex = 0; |
| 272 | CRegexSearcherSimple *searcher = 0; |
| 273 | int stat; |
| 274 | int line_num; |
| 275 | int error_count; |
| 276 | msg_t *first_msg = 0; |
| 277 | msg_t *last_msg = 0; |
| 278 | int msg_count; |
| 279 | msg_t **msg_array = 0; |
| 280 | msg_t *cur_msg; |
| 281 | msg_t **arrp; |
| 282 | char writebuf[128]; |
| 283 | int i; |
| 284 | int strict = FALSE; |
| 285 | char exe_path[OSFNMAX]; |
| 286 | |
| 287 | /* presume failure */ |
| 288 | stat = OSEXFAIL; |
| 289 | |
| 290 | /* show the banner */ |
| 291 | printf("T3 Message Compiler v1.0.0 Copyright 2000, 2012 " |
| 292 | "Michael J. Roberts\n"); /* copyright-date-string */ |
| 293 | |
| 294 | /* check options */ |
| 295 | for (i = 1 ; i < argc && argv[i][0] == '-' ; ++i) |
| 296 | { |
| 297 | /* check which option we have */ |
| 298 | if (strcmp(argv[i], "-strict") == 0) |
| 299 | { |
| 300 | /* note STRICT mode */ |
| 301 | strict = TRUE; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | /* anything else is invalid */ |
| 306 | printf("invalid option \"%s\"\n", argv[i]); |
| 307 | return OSEXFAIL; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* check arguments */ |
| 312 | if (i + 2 != argc) |
| 313 | { |
| 314 | printf("usage: t3msgc [options] infile outfile\n" |
| 315 | "infile = your message text source file\n" |
| 316 | "outfile = the compiled binary output file to be created\n" |
| 317 | "options:\n" |
| 318 | " -strict warn for any #define symbols with no associated " |
| 319 | "message\n"); |
| 320 | return OSEXFAIL; |
| 321 | } |
| 322 | |
| 323 | /* create the resource loader and set the exe path if possible */ |
| 324 | res_loader = new CResLoader(); |
| 325 | if (os_get_exe_filename(exe_path, sizeof(exe_path), argv[0])) |
| 326 | res_loader->set_exe_filename(exe_path); |
| 327 | |
| 328 | /* get the arguments */ |
| 329 | fname_in = argv[i]; |
| 330 | fname_out = argv[i + 1]; |
| 331 | |
| 332 | /* create our hash table */ |
| 333 | hashtab = new CVmHashTable(256, new CVmHashFuncCS(), TRUE); |
| 334 | |
| 335 | /* create a regular expression parser */ |
| 336 | regex = new CRegexParser(); |
| 337 | searcher = new CRegexSearcherSimple(regex); |
| 338 | |
| 339 | /* open the input file */ |
| 340 | fpin = osfoprt(fname_in, OSFTTEXT); |
| 341 | if (fpin == 0) |
| 342 | { |
| 343 | printf("unable to open input file \"%s\"\n", fname_in); |
| 344 | goto done; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * read the source file |
| 349 | */ |
| 350 | for (line_num = 0, error_count = 0, msg_count = 0 ;; ) |
| 351 | { |
| 352 | char buf[512]; |
| 353 | size_t len; |
| 354 | char shortbuf[512]; |
| 355 | char longbuf[2048]; |
| 356 | size_t longbuf_len; |
| 357 | char *p; |
| 358 | char *idp; |
| 359 | int msg_id; |
| 360 | static const char charset_pat[] = "<space>*#<space>*charset<space>+" |
| 361 | "\"([^\"]*)\""; |
| 362 | static size_t charset_pat_len = sizeof(charset_pat) - 1; |
| 363 | static const char inc_pat[] = "<space>*#<space>*include<space>+" |
| 364 | "\"([^\"]*)\""; |
| 365 | static size_t inc_pat_len = sizeof(inc_pat) - 1; |
| 366 | static const char comment_pat[] = "<space>*(//|$)"; |
| 367 | static size_t comment_pat_len = sizeof(comment_pat) - 1; |
| 368 | |
| 369 | /* count the new line */ |
| 370 | ++line_num; |
| 371 | |
| 372 | /* read the next line - stop if we've reached the end of the file */ |
| 373 | if (osfgets(buf, sizeof(buf), fpin) == 0) |
| 374 | break; |
| 375 | |
| 376 | /* get the length of the line */ |
| 377 | len = strlen(buf); |
| 378 | |
| 379 | /* remove trailing newline characters */ |
| 380 | while (len != 0 && (buf[len-1] == '\r' || buf[len-1] == '\n')) |
| 381 | --len; |
| 382 | |
| 383 | /* add a null terminator replacing the last newline we found */ |
| 384 | buf[len] = '\0'; |
| 385 | |
| 386 | /* check for a comment or a blank line */ |
| 387 | if (searcher->compile_and_match(comment_pat, comment_pat_len, |
| 388 | buf, buf, len) >= 0) |
| 389 | { |
| 390 | /* |
| 391 | * it's a comment, so no further processing is required - |
| 392 | * move on to the next line |
| 393 | */ |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | /* check for a #charset directive */ |
| 398 | if (searcher->compile_and_match(charset_pat, charset_pat_len, |
| 399 | buf, buf, len) > 0) |
| 400 | { |
| 401 | char charset[128]; |
| 402 | const re_group_register *cs_reg; |
| 403 | size_t cs_len; |
| 404 | |
| 405 | /* if we already have a character mapping, it's an error */ |
| 406 | if (mapper != 0) |
| 407 | { |
| 408 | /* note the error */ |
| 409 | printf("line %d: error: redundant #charset directive " |
| 410 | "(only one is allowed in the file) - ignored\n", |
| 411 | line_num); |
| 412 | |
| 413 | /* count the error */ |
| 414 | ++error_count; |
| 415 | |
| 416 | /* simply ignore the redundant directive and keep going */ |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | /* get the group register for the character set name */ |
| 421 | cs_reg = searcher->get_group_reg(0); |
| 422 | |
| 423 | /* make sure it'll fit in our buffer */ |
| 424 | cs_len = cs_reg->end_ofs - cs_reg->start_ofs; |
| 425 | if (cs_len > sizeof(charset) - 1) |
| 426 | { |
| 427 | /* |
| 428 | * show an error and give up - failure to load a |
| 429 | * character mapping is fatal, since we can't interpret |
| 430 | * the remainder of the file without it |
| 431 | */ |
| 432 | printf("line %d: character set name \"%.*s\" is too long\n", |
| 433 | line_num, (int)cs_len, buf + cs_reg->start_ofs); |
| 434 | goto done; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * copy the character set name into our buffer and |
| 439 | * null-terminate it |
| 440 | */ |
| 441 | memcpy(charset, buf + cs_reg->start_ofs, cs_len); |
| 442 | charset[cs_len] = '\0'; |
| 443 | |
| 444 | /* create the mapper for the character set */ |
| 445 | mapper = CCharmapToUni::load(res_loader, charset); |
| 446 | if (mapper == 0) |
| 447 | { |
| 448 | /* show an error and abort */ |
| 449 | printf("line %d: unable to load character set " |
| 450 | "mapping for character set \"%s\"\n", |
| 451 | line_num, charset); |
| 452 | goto done; |
| 453 | } |
| 454 | |
| 455 | /* we're done processing this line - move on to the next */ |
| 456 | continue; |
| 457 | } |
| 458 | |
| 459 | /* check for a #include directive */ |
| 460 | if (searcher->compile_and_match( |
| 461 | inc_pat, inc_pat_len, buf, buf, len) > 0) |
| 462 | { |
| 463 | const re_group_register *fname_reg; |
| 464 | char fname[OSFNMAX]; |
| 465 | size_t fname_len; |
| 466 | |
| 467 | /* get the header file register from the regular expression */ |
| 468 | fname_reg = searcher->get_group_reg(0); |
| 469 | |
| 470 | /* note its length - make sure it's not too long for our buffer */ |
| 471 | fname_len = fname_reg->end_ofs - fname_reg->start_ofs; |
| 472 | if (fname_len > sizeof(fname) - 1) |
| 473 | { |
| 474 | /* it's too long - warn about it */ |
| 475 | printf("line %d: #include filename \"%.*s\" is too long - " |
| 476 | "#include directive ignored\n", |
| 477 | line_num, |
| 478 | (int)fname_len, buf + fname_reg->start_ofs); |
| 479 | |
| 480 | /* count the error */ |
| 481 | ++error_count; |
| 482 | |
| 483 | /* ignore this line */ |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | /* copy the name into our buffer and null-terminate it */ |
| 488 | memcpy(fname, buf + fname_reg->start_ofs, fname_len); |
| 489 | fname[fname_len] = '\0'; |
| 490 | |
| 491 | /* read the header file */ |
| 492 | if (read_header_file(line_num, fname, searcher, hashtab)) |
| 493 | { |
| 494 | /* an error occurred reading the file - abort */ |
| 495 | goto done; |
| 496 | } |
| 497 | |
| 498 | /* we're done with this line - proceed to the next one */ |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * We seem to have a message definition. We must have a |
| 504 | * character set defined at this point; if we don't, it's an |
| 505 | * error. |
| 506 | */ |
| 507 | if (mapper == 0) |
| 508 | { |
| 509 | printf("line %d: a character set must be defined with #charset " |
| 510 | "before the first message definition\n", line_num); |
| 511 | goto done; |
| 512 | } |
| 513 | |
| 514 | /* skip leading spaces */ |
| 515 | for (p = buf ; is_space(*p) ; ++p) ; |
| 516 | |
| 517 | /* find the end of the message identifier */ |
| 518 | for (idp = p ; *p != '\0' && !is_space(*p) ; ++p) ; |
| 519 | |
| 520 | /* determine if the message ID is numeric or symbolic */ |
| 521 | if (is_alpha(*idp) || *idp == '_') |
| 522 | { |
| 523 | CVmHashEntryDef *entry; |
| 524 | |
| 525 | /* look up the message ID in the hash table */ |
| 526 | entry = (CVmHashEntryDef *)hashtab->find(idp, p - idp); |
| 527 | |
| 528 | /* if we couldn't find the ID, it's an error */ |
| 529 | if (entry == 0) |
| 530 | { |
| 531 | /* note the error */ |
| 532 | printf("line %d: symbolic message identifier \"%.*s\" " |
| 533 | "not found\n", line_num, (int)(p - idp), idp); |
| 534 | |
| 535 | /* count the error */ |
| 536 | ++error_count; |
| 537 | |
| 538 | /* we don't know the message ID, so use zero */ |
| 539 | msg_id = 0; |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | /* get the message ID from the symbol table entry */ |
| 544 | msg_id = entry->val_; |
| 545 | } |
| 546 | } |
| 547 | else if (is_digit(*idp)) |
| 548 | { |
| 549 | /* get the numeric message ID */ |
| 550 | msg_id = atoi(idp); |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | /* note the error */ |
| 555 | printf("line %d: invalid message identifier \"%.*s\"\n", |
| 556 | line_num, (int)(idp - p), idp); |
| 557 | |
| 558 | /* count the error */ |
| 559 | ++error_count; |
| 560 | |
| 561 | /* we don't know the message ID, so use zero */ |
| 562 | msg_id = 0; |
| 563 | } |
| 564 | |
| 565 | /* skip spaces after the message ID */ |
| 566 | for ( ; is_space(*p) ; ++p) ; |
| 567 | |
| 568 | /* if there's nothing left, the short message is on a separate line */ |
| 569 | if (*p == '\0') |
| 570 | { |
| 571 | /* count the next line */ |
| 572 | ++line_num; |
| 573 | |
| 574 | /* read the next line */ |
| 575 | if (osfgets(buf, sizeof(buf), fpin) == 0) |
| 576 | { |
| 577 | printf("line %d: unexpected end of file before short message " |
| 578 | "for message number %d\n", line_num, msg_id); |
| 579 | goto done; |
| 580 | } |
| 581 | |
| 582 | /* get the length of the line */ |
| 583 | len = strlen(buf); |
| 584 | |
| 585 | /* remove trailing newline characters */ |
| 586 | while (len != 0 && (buf[len-1] == '\r' || buf[len-1] == '\n')) |
| 587 | --len; |
| 588 | |
| 589 | /* add a null terminator replacing the last newline we found */ |
| 590 | buf[len] = '\0'; |
| 591 | |
| 592 | /* use the whole line */ |
| 593 | p = buf; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * the remainder of the line is the short message - copy it into |
| 598 | * the short-message buffer, mapping it to utf-8 |
| 599 | */ |
| 600 | mapper->map_str(shortbuf, sizeof(shortbuf), p); |
| 601 | |
| 602 | /* |
| 603 | * subsequent lines up to the next blank line are the long |
| 604 | * message - read them and collect the entire message (with the |
| 605 | * newlines removed) into longbuf |
| 606 | */ |
| 607 | longbuf_len = 0; |
| 608 | for (;;) |
| 609 | { |
| 610 | size_t utf8_len; |
| 611 | size_t rem_len; |
| 612 | |
| 613 | /* count the next line */ |
| 614 | ++line_num; |
| 615 | |
| 616 | /* |
| 617 | * read the next line - if we reach the end of the file, |
| 618 | * that's fine, as long as the message isn't empty |
| 619 | */ |
| 620 | if (osfgets(buf, sizeof(buf), fpin) == 0) |
| 621 | { |
| 622 | /* make sure the message isn't empty */ |
| 623 | if (longbuf_len == 0) |
| 624 | { |
| 625 | printf("line %d: unexpected end of file in long message " |
| 626 | "for message number %d\n", line_num, msg_id); |
| 627 | goto done; |
| 628 | } |
| 629 | |
| 630 | /* that's clearly the end of this message */ |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | /* get the length of the line */ |
| 635 | len = strlen(buf); |
| 636 | |
| 637 | /* remove trailing newline characters */ |
| 638 | while (len != 0 && (buf[len-1] == '\r' || buf[len-1] == '\n')) |
| 639 | --len; |
| 640 | |
| 641 | /* add a null terminator replacing the last newline we found */ |
| 642 | buf[len] = '\0'; |
| 643 | |
| 644 | /* skip leading spaces and check for a blank line */ |
| 645 | for (p = buf ; is_space(*p) ; ++p) ; |
| 646 | if (*p == '\0') |
| 647 | break; |
| 648 | |
| 649 | /* calculate how much space we have left in our buffer */ |
| 650 | rem_len = sizeof(longbuf) - longbuf_len; |
| 651 | |
| 652 | /* |
| 653 | * append the line to the long message buffer, mapping it to |
| 654 | * utf-8 |
| 655 | */ |
| 656 | utf8_len = mapper->map_str(longbuf + longbuf_len, rem_len, buf); |
| 657 | |
| 658 | /* |
| 659 | * if that required more space than we had, it's an error |
| 660 | * (note that it's too much if we consumed exactly the |
| 661 | * amount we had left, because that means we didn't have |
| 662 | * room for the null terminator byte) |
| 663 | */ |
| 664 | if (utf8_len >= rem_len) |
| 665 | { |
| 666 | printf("line %d: long message for message ID %d is too " |
| 667 | "long\n", line_num, msg_id); |
| 668 | goto done; |
| 669 | } |
| 670 | |
| 671 | /* count this new section in the total message length so far */ |
| 672 | longbuf_len += utf8_len; |
| 673 | } |
| 674 | |
| 675 | /* create a new message structure for this message */ |
| 676 | cur_msg = new msg_t(msg_id, shortbuf, longbuf); |
| 677 | |
| 678 | /* link it in at the end of our list */ |
| 679 | if (last_msg != 0) |
| 680 | last_msg->nxt_ = cur_msg; |
| 681 | else |
| 682 | first_msg = cur_msg; |
| 683 | last_msg = cur_msg; |
| 684 | |
| 685 | /* count it */ |
| 686 | ++msg_count; |
| 687 | } |
| 688 | |
| 689 | /* make sure we have at least one message */ |
| 690 | if (msg_count == 0) |
| 691 | { |
| 692 | printf("source file contains no messages\n"); |
| 693 | goto done; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Next, we must put the messages into sorted order by message |
| 698 | * number. Allocate an array so we can perform the sorting. |
| 699 | */ |
| 700 | msg_array = (msg_t **)t3malloc(msg_count * sizeof(msg_array[0])); |
| 701 | |
| 702 | /* |
| 703 | * set up the array with pointers to the messages in source file |
| 704 | * order (i.e., in the order in which they appear in our linked list |
| 705 | * of messages) |
| 706 | */ |
| 707 | for (cur_msg = first_msg, arrp = msg_array ; cur_msg != 0 ; |
| 708 | cur_msg = cur_msg->nxt_, ++arrp) |
| 709 | *arrp = cur_msg; |
| 710 | |
| 711 | /* sort the array of messages by message number */ |
| 712 | qsort(msg_array, msg_count, sizeof(msg_array[0]), &msg_t::compare); |
| 713 | |
| 714 | /* open the output file */ |
| 715 | fpout = osfopwb(fname_out, OSFTERRS); |
| 716 | if (fpout == 0) |
| 717 | { |
| 718 | printf("unable to open output file \"%s\"\n", fname_out); |
| 719 | goto done; |
| 720 | } |
| 721 | |
| 722 | /* prepare the message count in portable format */ |
| 723 | oswp2(writebuf, msg_count); |
| 724 | |
| 725 | /* write the signature and message count */ |
| 726 | if (osfwb(fpout, VM_MESSAGE_FILE_SIGNATURE, |
| 727 | sizeof(VM_MESSAGE_FILE_SIGNATURE)) |
| 728 | || osfwb(fpout, writebuf, 2)) |
| 729 | { |
| 730 | printf("error writing message file header to output file\n"); |
| 731 | goto done; |
| 732 | } |
| 733 | |
| 734 | /* write the messages */ |
| 735 | for (arrp = msg_array, i = 0 ; i < msg_count ; ++i, ++arrp) |
| 736 | { |
| 737 | size_t short_len; |
| 738 | size_t long_len; |
| 739 | |
| 740 | /* get the lengths of the two messages */ |
| 741 | short_len = strlen((*arrp)->short_msg_); |
| 742 | long_len = strlen((*arrp)->long_msg_); |
| 743 | |
| 744 | /* |
| 745 | * prepare the message header: message ID, short message length, |
| 746 | * long message length |
| 747 | */ |
| 748 | oswp4(writebuf, (*arrp)->id_); |
| 749 | oswp2(writebuf + 4, short_len); |
| 750 | oswp2(writebuf + 6, long_len); |
| 751 | |
| 752 | /* |
| 753 | * write the message header, followed by the short message's |
| 754 | * text, followed by the long message's text |
| 755 | */ |
| 756 | if (osfwb(fpout, writebuf, 8) |
| 757 | || osfwb(fpout, (*arrp)->short_msg_, short_len) |
| 758 | || osfwb(fpout, (*arrp)->long_msg_, long_len)) |
| 759 | { |
| 760 | printf("error writing message to output file\n"); |
| 761 | goto done; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /* if we're in strict mode, check for symbols with no messages */ |
| 766 | if (strict) |
| 767 | { |
| 768 | strict_check_ctx ctx; |
| 769 | |
| 770 | /* build our strict-check-callback context */ |
| 771 | ctx.msg_array = msg_array; |
| 772 | ctx.msg_count = msg_count; |
| 773 | ctx.not_found_count = 0; |
| 774 | |
| 775 | /* enumerate all entries through our check callback */ |
| 776 | hashtab->enum_entries(strict_check, &ctx); |
| 777 | } |
| 778 | |
| 779 | /* show statistics */ |
| 780 | printf("success - %d messages\n", msg_count); |
| 781 | |
| 782 | /* success! */ |
| 783 | stat = OSEXSUCC; |
| 784 | |
| 785 | done: |
| 786 | /* delete all of the messages */ |
| 787 | while (first_msg != 0) |
| 788 | { |
| 789 | msg_t *nxt; |
| 790 | |
| 791 | /* remember the next one */ |
| 792 | nxt = first_msg->nxt_; |
| 793 | |
| 794 | /* delete this one */ |
| 795 | delete first_msg; |
| 796 | |
| 797 | /* move on to the next one */ |
| 798 | first_msg = nxt; |
| 799 | } |
| 800 | |
| 801 | /* close any files we opened */ |
| 802 | if (fpin != 0) |
| 803 | osfcls(fpin); |
| 804 | if (fpout != 0) |
| 805 | osfcls(fpout); |
| 806 | |
| 807 | /* delete any objects we created */ |
| 808 | if (mapper != 0) |
| 809 | mapper->release_ref(); |
| 810 | if (res_loader != 0) |
| 811 | delete res_loader; |
| 812 | if (searcher != 0) |
| 813 | delete searcher; |
| 814 | if (regex != 0) |
| 815 | delete regex; |
| 816 | if (hashtab != 0) |
| 817 | delete hashtab; |
| 818 | if (msg_array != 0) |
| 819 | t3free(msg_array); |
| 820 | |
| 821 | /* show any unfreed memory (if we're in a debug build) */ |
| 822 | t3_list_memory_blocks(0); |
| 823 | |
| 824 | /* terminate with failure status */ |
| 825 | return OSEXFAIL; |
| 826 | } |
| 827 |