| 1 |
#ifdef RCSID |
| 2 |
static char RCSid[] = |
| 3 |
"$Header: d:/cvsroot/tads/TADS2/TADSRSC.C,v 1.2 1999/05/17 02:52:13 MJRoberts Exp $"; |
| 4 |
#endif |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <time.h> |
| 28 |
|
| 29 |
#include "os.h" |
| 30 |
#include "std.h" |
| 31 |
#include "fio.h" |
| 32 |
|
| 33 |
|
| 34 |
#define rscptf printf |
| 35 |
|
| 36 |
static void usage(void) |
| 37 |
{ |
| 38 |
rscptf("usage: tadsrsc [fileoptions] <resfile> [operations]\n" |
| 39 |
" or: tadsrsc [fileoptions] <resfile> @<opfile>" |
| 40 |
" - read ops from <opfile>\n" |
| 41 |
"If no operations are given, <resfile> contents are listed.\n" |
| 42 |
"File options:\n" |
| 43 |
" -create - create a new resource file\n" |
| 44 |
"Operations:\n" |
| 45 |
" -type <type> - use <type> as the type for following " |
| 46 |
"resources\n" |
| 47 |
" -replace - add or replace resources that follow\n" |
| 48 |
" -delete - remove following resources from resfile\n" |
| 49 |
" -add - add following resources, but do not replace if " |
| 50 |
"present\n" |
| 51 |
" <file> - add/delete/replace resource, using filename " |
| 52 |
"as resource name\n" |
| 53 |
" <dir> - add/delete/replace all files in directory\n" |
| 54 |
" <file>=<res> - add/delete/replace resource, using <res> as " |
| 55 |
"resource name\n" |
| 56 |
"-replace is assumed if no conflicting option is specified\n" |
| 57 |
"If no -type option is specified, types are inferred from " |
| 58 |
"filename suffix:\n" |
| 59 |
".bin, .com, and no '.' are assumed to be XFCN, all others " |
| 60 |
"HTML.\n" |
| 61 |
"Valid resource types:\n" |
| 62 |
" XFCN - external function (executable code)\n" |
| 63 |
" HTML - graphic/sound resource for HTML TADS (JPEG, etc)\n"); |
| 64 |
os_term(OSEXFAIL); |
| 65 |
} |
| 66 |
|
| 67 |
static void errexit(char *msg, long arg) |
| 68 |
{ |
| 69 |
rscptf(msg, arg); |
| 70 |
rscptf("\n"); |
| 71 |
os_term(OSEXFAIL); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static void listexit(osfildef *fp, char *msg) |
| 76 |
{ |
| 77 |
if (fp != 0) |
| 78 |
osfcls(fp); |
| 79 |
errexit(msg, 1); |
| 80 |
} |
| 81 |
|
| 82 |
typedef struct opdef opdef; |
| 83 |
struct opdef |
| 84 |
{ |
| 85 |
opdef *opnxt; |
| 86 |
char *opres; |
| 87 |
char *opfile; |
| 88 |
int opflag; |
| 89 |
int oprestype; |
| 90 |
# define OPFADD 0x01 |
| 91 |
# define OPFDEL 0x02 |
| 92 |
# define OPFDONE 0x04 |
| 93 |
# define OPFADDDIR 0x08 |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
#define RESTYPE_DFLT 0x00 |
| 98 |
#define RESTYPE_XFCN 0x01 |
| 99 |
#define RESTYPE_HTML 0x02 |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
char copybuf[16 * 1024]; |
| 104 |
static void copybytes(osfildef *fpin, osfildef *fpout, ulong siz) |
| 105 |
{ |
| 106 |
uint cursiz; |
| 107 |
|
| 108 |
|
| 109 |
while (siz != 0) |
| 110 |
{ |
| 111 |
|
| 112 |
cursiz = (siz > sizeof(copybuf) ? sizeof(copybuf) : siz); |
| 113 |
|
| 114 |
|
| 115 |
siz -= cursiz; |
| 116 |
|
| 117 |
|
| 118 |
if (osfrb(fpin, copybuf, cursiz) |
| 119 |
|| osfwb(fpout, copybuf, cursiz)) |
| 120 |
{ |
| 121 |
|
| 122 |
osfcls(fpin); |
| 123 |
osfcls(fpout); |
| 124 |
errexit("error copying resource", 1); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static void copyres(osfildef *fpin, osfildef *fpout, ulong siz, |
| 131 |
uint endpos_ofs) |
| 132 |
{ |
| 133 |
ulong startpos; |
| 134 |
ulong endpos; |
| 135 |
uchar buf[4]; |
| 136 |
|
| 137 |
|
| 138 |
startpos = osfpos(fpout); |
| 139 |
|
| 140 |
|
| 141 |
copybytes(fpin, fpout, siz); |
| 142 |
|
| 143 |
|
| 144 |
endpos = osfpos(fpout); |
| 145 |
|
| 146 |
|
| 147 |
osfseek(fpout, (ulong)(startpos + endpos_ofs), OSFSK_SET); |
| 148 |
oswp4(buf, endpos); |
| 149 |
if (osfwb(fpout, buf, 4)) errexit("error writing resource", 1); |
| 150 |
|
| 151 |
|
| 152 |
osfseek(fpout, endpos, OSFSK_SET); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static void procop(osfildef *fpout, opdef *op, ulong *first_xfcn) |
| 157 |
{ |
| 158 |
osfildef *fpin; |
| 159 |
char buf[128]; |
| 160 |
uint fsiz; |
| 161 |
ulong sizpos; |
| 162 |
ulong endpos; |
| 163 |
|
| 164 |
|
| 165 |
if (fpout && *first_xfcn == 0) |
| 166 |
*first_xfcn = osfpos(fpout); |
| 167 |
|
| 168 |
fpin = osfoprb(op->opfile, OSFTGAME); |
| 169 |
if (fpin == 0) |
| 170 |
{ |
| 171 |
rscptf("%s: ", op->opfile); |
| 172 |
errexit("unable to open file", 1); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
osfseek(fpin, 0L, OSFSK_END); |
| 177 |
fsiz = (uint)osfpos(fpin); |
| 178 |
osfseek(fpin, 0L, OSFSK_SET); |
| 179 |
|
| 180 |
|
| 181 |
switch(op->oprestype) |
| 182 |
{ |
| 183 |
case RESTYPE_XFCN: |
| 184 |
buf[0] = 4; |
| 185 |
memcpy(buf + 1, "XFCN", 4); |
| 186 |
break; |
| 187 |
|
| 188 |
case RESTYPE_HTML: |
| 189 |
buf[0] = 4; |
| 190 |
memcpy(buf + 1, "HTML", 3); |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if (osfwb(fpout, buf, buf[0] + 1)) errexit("error writing resource", 1); |
| 196 |
sizpos = osfpos(fpout); |
| 197 |
oswp4(buf, 0); |
| 198 |
if (osfwb(fpout, buf, 4)) errexit("error writing resource", 1); |
| 199 |
|
| 200 |
|
| 201 |
oswp2(buf, fsiz); |
| 202 |
buf[2] = strlen(op->opres); |
| 203 |
strcpy(buf + 3, op->opres); |
| 204 |
if (osfwb(fpout, buf, (uint)(buf[2] + 3))) |
| 205 |
errexit("error writing resource", 1); |
| 206 |
|
| 207 |
|
| 208 |
copybytes(fpin, fpout, fsiz); |
| 209 |
|
| 210 |
|
| 211 |
endpos = osfpos(fpout); |
| 212 |
oswp4(buf, endpos); |
| 213 |
osfseek(fpout, sizpos, OSFSK_SET); |
| 214 |
if (osfwb(fpout, buf, 4)) errexit("error writing resource", 1); |
| 215 |
|
| 216 |
|
| 217 |
osfseek(fpout, endpos, OSFSK_SET); |
| 218 |
|
| 219 |
|
| 220 |
osfcls(fpin); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
static void show_op(const char *desc, const char *resname, size_t resnamelen, |
| 225 |
int restype) |
| 226 |
{ |
| 227 |
const char *restypename; |
| 228 |
|
| 229 |
|
| 230 |
switch(restype) |
| 231 |
{ |
| 232 |
case RESTYPE_XFCN: |
| 233 |
restypename = "XFCN"; |
| 234 |
break; |
| 235 |
|
| 236 |
case RESTYPE_HTML: |
| 237 |
restypename = "HTML"; |
| 238 |
break; |
| 239 |
|
| 240 |
default: |
| 241 |
restypename = "????"; |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
rscptf(". %s %.*s (%s)\n", desc, resnamelen, resname, restypename); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static void show_list_item(int *showed_heading, char *type_name, |
| 251 |
ulong res_size, char *res_name, size_t name_len) |
| 252 |
{ |
| 253 |
|
| 254 |
if (!*showed_heading) |
| 255 |
{ |
| 256 |
|
| 257 |
rscptf("Type Size Name\n" |
| 258 |
"----------------------------------------------------------\n"); |
| 259 |
|
| 260 |
|
| 261 |
*showed_heading = TRUE; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
rscptf("%.4s %8lu %.*s\n", type_name, res_size, |
| 266 |
(int)name_len, res_name); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
struct idx_t |
| 271 |
{ |
| 272 |
|
| 273 |
opdef *src_op; |
| 274 |
struct idx_t *src_idx; |
| 275 |
|
| 276 |
|
| 277 |
ulong ofs; |
| 278 |
ulong siz; |
| 279 |
ushort namlen; |
| 280 |
struct idx_t *nxt; |
| 281 |
char nam[1]; |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
static struct idx_t *alloc_idx_entry(ulong ofs, ulong siz, |
| 288 |
char *nam, ushort namlen, |
| 289 |
opdef *src_op, struct idx_t *src_idx) |
| 290 |
{ |
| 291 |
struct idx_t *entry; |
| 292 |
|
| 293 |
|
| 294 |
entry = (struct idx_t *)malloc(sizeof(struct idx_t) + namlen - 1); |
| 295 |
|
| 296 |
|
| 297 |
entry->src_op = src_op; |
| 298 |
entry->src_idx = src_idx; |
| 299 |
entry->ofs = ofs; |
| 300 |
entry->siz = siz; |
| 301 |
entry->namlen = namlen; |
| 302 |
memcpy(entry->nam, nam, namlen); |
| 303 |
|
| 304 |
|
| 305 |
return entry; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
static void add_idx_entry(struct idx_t **list, ulong *out_res_cnt, |
| 312 |
ulong *out_total_name_len, ulong ofs, ulong siz, |
| 313 |
char *nam, ushort namlen, |
| 314 |
opdef *src_op, struct idx_t *src_idx) |
| 315 |
{ |
| 316 |
struct idx_t *entry; |
| 317 |
|
| 318 |
|
| 319 |
++(*out_res_cnt); |
| 320 |
*out_total_name_len += namlen; |
| 321 |
|
| 322 |
|
| 323 |
entry = alloc_idx_entry(ofs, siz, nam, namlen, src_op, src_idx); |
| 324 |
|
| 325 |
|
| 326 |
entry->nxt = *list; |
| 327 |
*list = entry; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
static opdef *prochtmlres(osfildef *fp, osfildef *fpout, opdef *oplist, |
| 336 |
int *copyrsc, int *showed_heading, int old_htmlres) |
| 337 |
{ |
| 338 |
opdef *op; |
| 339 |
opdef *add_list = 0; |
| 340 |
opdef *prev_op; |
| 341 |
opdef *next_op; |
| 342 |
int found; |
| 343 |
char buf[512]; |
| 344 |
ulong in_entry_cnt; |
| 345 |
ulong in_table_siz; |
| 346 |
ulong out_hdr_siz; |
| 347 |
ulong out_hdr_pos; |
| 348 |
ulong i; |
| 349 |
ulong out_res_cnt; |
| 350 |
ulong out_total_name_len; |
| 351 |
ulong rem; |
| 352 |
struct idx_t **in_list = 0, *out_list = 0, **p, *cur; |
| 353 |
ulong in_res_base, out_res_base; |
| 354 |
ulong out_endpos; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
for (op = oplist, found = FALSE ; op != 0 ; op = op->opnxt) |
| 362 |
{ |
| 363 |
|
| 364 |
if (op->oprestype == RESTYPE_HTML) |
| 365 |
{ |
| 366 |
found = TRUE; |
| 367 |
break; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
if (!found && (fpout != 0 || !old_htmlres)) |
| 377 |
{ |
| 378 |
*copyrsc = TRUE; |
| 379 |
return oplist; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
*copyrsc = FALSE; |
| 384 |
|
| 385 |
|
| 386 |
if (old_htmlres) |
| 387 |
{ |
| 388 |
|
| 389 |
if (osfrb(fp, buf, 8)) |
| 390 |
listexit(fp, "unable to read HTMLRES header"); |
| 391 |
in_entry_cnt = osrp4(buf); |
| 392 |
in_table_siz = osrp4(buf + 4); |
| 393 |
|
| 394 |
|
| 395 |
if (in_entry_cnt != 0) |
| 396 |
{ |
| 397 |
in_list = (struct idx_t **) |
| 398 |
malloc(in_entry_cnt * sizeof(struct idx_t *)); |
| 399 |
if (in_list == 0) |
| 400 |
listexit(fp, "unable to allocate space for HTMLRES entries"); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
for (i = 0, p = in_list ; i < in_entry_cnt ; ++i, ++p) |
| 405 |
{ |
| 406 |
ushort name_siz; |
| 407 |
ulong res_siz; |
| 408 |
ulong res_ofs; |
| 409 |
|
| 410 |
|
| 411 |
if (osfrb(fp, buf, 10)) |
| 412 |
listexit(fp, |
| 413 |
"unable to read HTMLRES index table entry (prefix)"); |
| 414 |
|
| 415 |
|
| 416 |
res_ofs = osrp4(buf); |
| 417 |
res_siz = osrp4(buf + 4); |
| 418 |
|
| 419 |
|
| 420 |
name_siz = osrp2(buf + 8); |
| 421 |
if (name_siz > sizeof(buf)) |
| 422 |
listexit(fp, "name too large in HTMLRES index table entry"); |
| 423 |
if (osfrb(fp, buf, name_siz)) |
| 424 |
listexit(fp, |
| 425 |
"unable to read HTMLRES index table entry (name)"); |
| 426 |
|
| 427 |
|
| 428 |
*p = alloc_idx_entry(res_ofs, res_siz, buf, name_siz, 0, 0); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
if (fpout == 0) |
| 433 |
{ |
| 434 |
|
| 435 |
for (i = 0, p = in_list ; i < in_entry_cnt ; ++i, ++p) |
| 436 |
show_list_item(showed_heading, "HTML", |
| 437 |
(*p)->siz, (*p)->nam, (*p)->namlen); |
| 438 |
|
| 439 |
|
| 440 |
goto done; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
in_res_base = osfpos(fp); |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
for (i = 0, p = in_list ; i < in_entry_cnt ; ++i, ++p) |
| 456 |
{ |
| 457 |
int remove_res = FALSE; |
| 458 |
int add_res = FALSE; |
| 459 |
|
| 460 |
|
| 461 |
for (prev_op = 0, op = oplist ; op != 0 ; |
| 462 |
prev_op = op, op = op->opnxt) |
| 463 |
{ |
| 464 |
|
| 465 |
if (op->oprestype == RESTYPE_HTML |
| 466 |
&& strlen(op->opres) == (*p)->namlen |
| 467 |
&& !memicmp(op->opres, (*p)->nam, (*p)->namlen)) |
| 468 |
{ |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
if (!(op->opflag & OPFDEL)) |
| 477 |
{ |
| 478 |
|
| 479 |
rscptf("warning: HTML resource \"%s\" already " |
| 480 |
"present\n" |
| 481 |
" -- old version will be kept (use -replace " |
| 482 |
"to replace it)\n", op->opres); |
| 483 |
|
| 484 |
|
| 485 |
remove_res = TRUE; |
| 486 |
} |
| 487 |
else |
| 488 |
{ |
| 489 |
|
| 490 |
if (op->opflag & OPFADD) |
| 491 |
{ |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
remove_res = TRUE; |
| 498 |
add_res = TRUE; |
| 499 |
|
| 500 |
|
| 501 |
show_op("replacing", op->opres, strlen(op->opres), |
| 502 |
op->oprestype); |
| 503 |
} |
| 504 |
else |
| 505 |
{ |
| 506 |
|
| 507 |
show_op("deleting", op->opres, strlen(op->opres), |
| 508 |
op->oprestype); |
| 509 |
|
| 510 |
|
| 511 |
remove_res = TRUE; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
free(*p); |
| 516 |
*p = 0; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
break; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
if (remove_res) |
| 529 |
{ |
| 530 |
|
| 531 |
if (prev_op == 0) |
| 532 |
oplist = op->opnxt; |
| 533 |
else |
| 534 |
prev_op->opnxt = op->opnxt; |
| 535 |
|
| 536 |
|
| 537 |
if (add_res) |
| 538 |
{ |
| 539 |
|
| 540 |
op->opnxt = add_list; |
| 541 |
add_list = op; |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
|
| 546 |
free(op); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
|
| 554 |
in_entry_cnt = 0; |
| 555 |
in_table_siz = 0; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
for (prev_op = 0, op = oplist ; op != 0 ; op = next_op) |
| 563 |
{ |
| 564 |
|
| 565 |
next_op = op->opnxt; |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
if (op->oprestype == RESTYPE_HTML && (op->opflag & OPFADD) != 0) |
| 572 |
{ |
| 573 |
|
| 574 |
show_op("adding", op->opres, strlen(op->opres), op->oprestype); |
| 575 |
|
| 576 |
|
| 577 |
if (prev_op == 0) |
| 578 |
oplist = op->opnxt; |
| 579 |
else |
| 580 |
prev_op->opnxt = op->opnxt; |
| 581 |
|
| 582 |
|
| 583 |
op->opnxt = add_list; |
| 584 |
add_list = op; |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
} |
| 592 |
else |
| 593 |
{ |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
prev_op = op; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
out_res_cnt = 0; |
| 609 |
out_total_name_len = 0; |
| 610 |
|
| 611 |
|
| 612 |
for (i = 0, p = in_list ; i < in_entry_cnt ; ++i, ++p) |
| 613 |
{ |
| 614 |
if (*p != 0) |
| 615 |
add_idx_entry(&out_list, &out_res_cnt, &out_total_name_len, |
| 616 |
0, 0, (*p)->nam, (*p)->namlen, 0, *p); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
for (op = add_list ; op != 0 ; op = op->opnxt) |
| 626 |
add_idx_entry(&out_list, &out_res_cnt, &out_total_name_len, |
| 627 |
0, 0, op->opres, (ushort)strlen(op->opres), op, 0); |
| 628 |
|
| 629 |
|
| 630 |
if (osfwb(fpout, "\007HTMLRES\0\0\0\0", 12)) |
| 631 |
listexit(fp, "unable to write HTMLRES type header"); |
| 632 |
out_hdr_pos = osfpos(fpout); |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
out_hdr_siz = 8 + (10 * out_res_cnt) + out_total_name_len; |
| 640 |
|
| 641 |
|
| 642 |
oswp4(buf, out_res_cnt); |
| 643 |
oswp4(buf + 4, out_hdr_siz); |
| 644 |
if (osfwb(fpout, buf, 8)) |
| 645 |
listexit(fp, "unable to write HTMLRES prefix"); |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
for (rem = out_hdr_siz - 8 ; rem != 0 ; ) |
| 656 |
{ |
| 657 |
ulong amt; |
| 658 |
|
| 659 |
|
| 660 |
amt = (rem > sizeof(buf) ? sizeof(buf) : rem); |
| 661 |
if (osfwb(fpout, buf, amt)) |
| 662 |
listexit(fp, "unable to write HTMLRES header"); |
| 663 |
|
| 664 |
|
| 665 |
rem -= amt; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
out_res_base = osfpos(fpout); |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
for (cur = out_list ; cur != 0 ; cur = cur->nxt) |
| 678 |
{ |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
cur->ofs = osfpos(fpout) - out_res_base; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
if (cur->src_op != 0) |
| 692 |
{ |
| 693 |
osfildef *fpext; |
| 694 |
ulong fsiz; |
| 695 |
|
| 696 |
|
| 697 |
fpext = osfoprb(cur->src_op->opfile, OSFTGAME); |
| 698 |
if (fpext == 0) |
| 699 |
{ |
| 700 |
rscptf("%s: ", cur->src_op->opfile); |
| 701 |
errexit("unable to open file", 1); |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
osfseek(fpext, 0L, OSFSK_END); |
| 706 |
fsiz = osfpos(fpext); |
| 707 |
osfseek(fpext, 0L, OSFSK_SET); |
| 708 |
|
| 709 |
|
| 710 |
copybytes(fpext, fpout, fsiz); |
| 711 |
|
| 712 |
|
| 713 |
cur->siz = fsiz; |
| 714 |
|
| 715 |
|
| 716 |
osfcls(fpext); |
| 717 |
} |
| 718 |
else |
| 719 |
{ |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
osfseek(fp, in_res_base + cur->src_idx->ofs, OSFSK_SET); |
| 726 |
copybytes(fp, fpout, cur->src_idx->siz); |
| 727 |
|
| 728 |
|
| 729 |
cur->siz = cur->src_idx->siz; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
out_endpos = osfpos(fpout); |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
osfseek(fpout, out_hdr_pos + 8, OSFSK_SET); |
| 741 |
for (cur = out_list ; cur != 0 ; cur = cur->nxt) |
| 742 |
{ |
| 743 |
|
| 744 |
oswp4(buf, cur->ofs); |
| 745 |
oswp4(buf + 4, cur->siz); |
| 746 |
oswp2(buf + 8, cur->namlen); |
| 747 |
|
| 748 |
|
| 749 |
if (osfwb(fpout, buf, 10) |
| 750 |
|| osfwb(fpout, cur->nam, cur->namlen)) |
| 751 |
listexit(fp, "unable to write HTML index table entry"); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
osfseek(fpout, out_hdr_pos - 4, OSFSK_SET); |
| 760 |
oswp4(buf, out_endpos); |
| 761 |
if (osfwb(fpout, buf, 4)) |
| 762 |
errexit("error writing resource", 1); |
| 763 |
|
| 764 |
|
| 765 |
osfseek(fpout, out_endpos, OSFSK_SET); |
| 766 |
|
| 767 |
done: |
| 768 |
|
| 769 |
if (in_list != 0) |
| 770 |
{ |
| 771 |
|
| 772 |
for (i = 0, p = in_list ; i < in_entry_cnt ; ++i, ++p) |
| 773 |
{ |
| 774 |
|
| 775 |
if (*p != 0) |
| 776 |
free(*p); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
free(in_list); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
for (op = add_list ; op != 0 ; op = next_op) |
| 788 |
{ |
| 789 |
|
| 790 |
next_op = op->opnxt; |
| 791 |
|
| 792 |
|
| 793 |
free(op); |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
return oplist; |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
static opdef *rscproc(osfildef *fp, osfildef *fpout, opdef *oplist) |
| 803 |
{ |
| 804 |
char buf[128]; |
| 805 |
ulong siz; |
| 806 |
char datebuf[27]; |
| 807 |
ulong endpos; |
| 808 |
uchar nambuf[40]; |
| 809 |
uint rsiz; |
| 810 |
opdef *op; |
| 811 |
int copyrsc; |
| 812 |
ulong startpos; |
| 813 |
uint endpos_ofs; |
| 814 |
ulong first_xfcn = 0; |
| 815 |
ulong extcnt_pos = 0; |
| 816 |
int found_user_rsc = FALSE; |
| 817 |
int showed_heading = FALSE; |
| 818 |
int found_htmlres = FALSE; |
| 819 |
char *file_type; |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
if (fp != 0) |
| 826 |
{ |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if (osfrb(fp, buf, (int)(sizeof(FIOFILHDR) + sizeof(FIOVSNHDR) + 2))) |
| 832 |
listexit(fp, "error reading file header"); |
| 833 |
|
| 834 |
|
| 835 |
if (memcmp(buf, FIOFILHDR, sizeof(FIOFILHDR)) == 0) |
| 836 |
file_type = "game"; |
| 837 |
else if (memcmp(buf, FIOFILHDRRSC, sizeof(FIOFILHDRRSC)) == 0) |
| 838 |
file_type = "resource"; |
| 839 |
else |
| 840 |
listexit(fp, "invalid resource file header"); |
| 841 |
|
| 842 |
|
| 843 |
if (memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR, sizeof(FIOVSNHDR)) |
| 844 |
&& memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR2, sizeof(FIOVSNHDR2)) |
| 845 |
&& memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR3, sizeof(FIOVSNHDR3))) |
| 846 |
listexit(fp, "incompatible resource file version"); |
| 847 |
|
| 848 |
|
| 849 |
if (osfrb(fp, datebuf, (size_t)26)) |
| 850 |
listexit(fp, "error reading file"); |
| 851 |
datebuf[26] = '\0'; |
| 852 |
} |
| 853 |
else |
| 854 |
{ |
| 855 |
struct tm *tblock; |
| 856 |
time_t timer; |
| 857 |
|
| 858 |
|
| 859 |
memcpy(buf, FIOFILHDRRSC, sizeof(FIOFILHDRRSC)); |
| 860 |
memcpy(buf + sizeof(FIOFILHDR), FIOVSNHDR, sizeof(FIOVSNHDR)); |
| 861 |
|
| 862 |
|
| 863 |
oswp2(buf + sizeof(FIOFILHDR) + sizeof(FIOVSNHDR), 0); |
| 864 |
|
| 865 |
|
| 866 |
timer = time(0); |
| 867 |
tblock = localtime(&timer); |
| 868 |
strcpy(datebuf, asctime(tblock)); |
| 869 |
} |
| 870 |
|
| 871 |
if (fpout) |
| 872 |
{ |
| 873 |
if (osfwb(fpout, buf, |
| 874 |
(int)(sizeof(FIOFILHDR) + sizeof(FIOVSNHDR) + 2)) |
| 875 |
|| osfwb(fpout, datebuf, (size_t)26)) |
| 876 |
listexit(fp, "error writing header"); |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
if (fpout == 0) |
| 881 |
rscptf("\n" |
| 882 |
"File type: TADS %s file\n" |
| 883 |
"Date compiled: %s\n", file_type, datebuf); |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
for ( ; fp != 0 ; ) |
| 889 |
{ |
| 890 |
|
| 891 |
copyrsc = TRUE; |
| 892 |
|
| 893 |
startpos = osfpos(fp); |
| 894 |
if (osfrb(fp, buf, 1) |
| 895 |
|| osfrb(fp, buf + 1, (int)(buf[0] + 4))) |
| 896 |
listexit(fp, "error reading file"); |
| 897 |
|
| 898 |
memcpy(nambuf, buf + 1, (size_t)buf[0]); |
| 899 |
nambuf[buf[0]] = '\0'; |
| 900 |
|
| 901 |
endpos_ofs = 1 + buf[0]; |
| 902 |
endpos = osrp4(buf + endpos_ofs); |
| 903 |
siz = endpos - startpos; |
| 904 |
|
| 905 |
|
| 906 |
if (!strcmp((char *)nambuf, "$EOF")) |
| 907 |
{ |
| 908 |
|
| 909 |
break; |
| 910 |
} |
| 911 |
else if (!strcmp((char *)nambuf, "HTMLRES")) |
| 912 |
{ |
| 913 |
|
| 914 |
if (found_htmlres) |
| 915 |
{ |
| 916 |
rscptf("error: multiple HTMLRES maps found in file\n" |
| 917 |
" -- redundant entries have been deleted.\n"); |
| 918 |
copyrsc = FALSE; |
| 919 |
} |
| 920 |
else |
| 921 |
{ |
| 922 |
|
| 923 |
oplist = prochtmlres(fp, fpout, oplist, ©rsc, |
| 924 |
&showed_heading, TRUE); |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
found_user_rsc = TRUE; |
| 929 |
found_htmlres = TRUE; |
| 930 |
} |
| 931 |
else if (!strcmp((char *)nambuf, "EXTCNT")) |
| 932 |
{ |
| 933 |
|
| 934 |
if (siz >= 17 && fpout) |
| 935 |
extcnt_pos = osfpos(fpout); |
| 936 |
} |
| 937 |
else if (!strcmp((char *)nambuf, "XFCN")) |
| 938 |
{ |
| 939 |
if (osfrb(fp, buf, 3) || osfrb(fp, buf + 3, (int)buf[2])) |
| 940 |
listexit(fp, "error reading file"); |
| 941 |
rsiz = osrp2(buf); |
| 942 |
buf[3 + buf[2]] = '\0'; |
| 943 |
|
| 944 |
if (fpout) |
| 945 |
{ |
| 946 |
|
| 947 |
for (op = oplist ; op ; op = op->opnxt) |
| 948 |
{ |
| 949 |
if (!(op->opflag & OPFDONE) |
| 950 |
&& op->oprestype == RESTYPE_XFCN |
| 951 |
&& !stricmp((char *)buf + 3, op->opres)) |
| 952 |
{ |
| 953 |
|
| 954 |
op->opflag |= OPFDONE; |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
if (!(op->opflag & OPFDEL)) |
| 961 |
{ |
| 962 |
|
| 963 |
rscptf("warning: XFCN resource \"%s\" already in " |
| 964 |
"file\n -- the old resource will be kept " |
| 965 |
"(use -replace to replace it)\n", |
| 966 |
op->opres); |
| 967 |
|
| 968 |
|
| 969 |
op->opflag &= ~OPFADD; |
| 970 |
} |
| 971 |
else |
| 972 |
{ |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
if (op->opflag & OPFADD) |
| 979 |
{ |
| 980 |
|
| 981 |
show_op("replacing", op->opres, |
| 982 |
strlen(op->opres), op->oprestype); |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
procop(fpout, op, &first_xfcn); |
| 989 |
} |
| 990 |
else |
| 991 |
{ |
| 992 |
|
| 993 |
show_op("deleting", op->opres, |
| 994 |
strlen(op->opres), op->oprestype); |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
copyrsc = FALSE; |
| 999 |
} |
| 1000 |
break; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
else |
| 1005 |
{ |
| 1006 |
|
| 1007 |
show_list_item(&showed_heading, "XFCN", |
| 1008 |
(ulong)rsiz, buf + 3, (size_t)buf[2]); |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
found_user_rsc = TRUE; |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (fpout != 0 && copyrsc) |
| 1016 |
{ |
| 1017 |
osfseek(fp, startpos, OSFSK_SET); |
| 1018 |
copyres(fp, fpout, siz, endpos_ofs); |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
osfseek(fp, endpos, OSFSK_SET); |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
if (!found_htmlres) |
| 1027 |
oplist = prochtmlres(fp, fpout, oplist, ©rsc, |
| 1028 |
&showed_heading, FALSE); |
| 1029 |
|
| 1030 |
|
| 1031 |
if (fpout != 0) |
| 1032 |
{ |
| 1033 |
for (op = oplist ; op != 0 ; op = op->opnxt) |
| 1034 |
{ |
| 1035 |
if (!(op->opflag & OPFDONE) && (op->opflag & OPFADD) |
| 1036 |
&& op->oprestype != RESTYPE_HTML) |
| 1037 |
{ |
| 1038 |
|
| 1039 |
show_op("adding", op->opres, |
| 1040 |
strlen(op->opres), op->oprestype); |
| 1041 |
|
| 1042 |
|
| 1043 |
procop(fpout, op, &first_xfcn); |
| 1044 |
|
| 1045 |
|
| 1046 |
op->opflag |= OPFDONE; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
if (fpout == 0 && !found_user_rsc) |
| 1053 |
rscptf("No user resources found.\n"); |
| 1054 |
|
| 1055 |
|
| 1056 |
if (fpout != 0) |
| 1057 |
{ |
| 1058 |
|
| 1059 |
if (osfwb(fpout, "\004$EOF\0\0\0\0", 9)) |
| 1060 |
errexit("error writing resource", 1); |
| 1061 |
|
| 1062 |
|
| 1063 |
if (extcnt_pos) |
| 1064 |
{ |
| 1065 |
osfseek(fpout, extcnt_pos + 13, OSFSK_SET); |
| 1066 |
oswp4(buf, first_xfcn); |
| 1067 |
osfwb(fpout, buf, 4); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
return oplist; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
typedef struct |
| 1084 |
{ |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
int restype; |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
int flag; |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
int doing_type; |
| 1101 |
} opctxdef; |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
static int get_file_restype(int restype, char *fname) |
| 1110 |
{ |
| 1111 |
|
| 1112 |
if (restype == RESTYPE_DFLT) |
| 1113 |
{ |
| 1114 |
char *p; |
| 1115 |
char *lastdot; |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
for (p = fname, lastdot = 0 ; *p ; ++p) |
| 1123 |
{ |
| 1124 |
|
| 1125 |
if (*p == '.') |
| 1126 |
lastdot = p; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
if (lastdot == 0 |
| 1131 |
|| !stricmp(lastdot, ".bin") || !stricmp(lastdot, ".com")) |
| 1132 |
return RESTYPE_XFCN; |
| 1133 |
else |
| 1134 |
return RESTYPE_HTML; |
| 1135 |
} |
| 1136 |
else |
| 1137 |
{ |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
return restype; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
static opdef *addopdir(opdef *cur, char *nam, opctxdef *opctx) |
| 1152 |
{ |
| 1153 |
opdef *newop; |
| 1154 |
char dir_prefix[OSFNMAX]; |
| 1155 |
osdirhdl_t dirhdl; |
| 1156 |
|
| 1157 |
|
| 1158 |
os_cvt_dir_url(dir_prefix, sizeof(dir_prefix) - 1, nam); |
| 1159 |
|
| 1160 |
|
| 1161 |
if (dir_prefix[0] != '\0') |
| 1162 |
strcat(dir_prefix, "/"); |
| 1163 |
|
| 1164 |
|
| 1165 |
if (os_open_dir(nam, &dirhdl)) |
| 1166 |
{ |
| 1167 |
char fname[OSFNMAX]; |
| 1168 |
while (os_read_dir(dirhdl, fname, sizeof(fname))) |
| 1169 |
{ |
| 1170 |
char fullname[OSFNMAX]; |
| 1171 |
unsigned long fmode; |
| 1172 |
unsigned long fattr; |
| 1173 |
|
| 1174 |
|
| 1175 |
os_build_full_path(fullname, sizeof(fullname), nam, fname); |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
if (osfmode(fullname, TRUE, &fmode, &fattr) |
| 1185 |
&& (fmode & OSFMODE_DIR) == 0 |
| 1186 |
&& (fattr & (OSFATTR_HIDDEN | OSFATTR_SYSTEM)) == 0) |
| 1187 |
{ |
| 1188 |
|
| 1189 |
char fullurl[OSFNMAX]; |
| 1190 |
sprintf(fullurl, "%s%s", dir_prefix, fname); |
| 1191 |
|
| 1192 |
|
| 1193 |
newop = (opdef *)malloc(sizeof(opdef) + strlen(fullurl) |
| 1194 |
+ strlen(fullname) + 2); |
| 1195 |
newop->opnxt = cur; |
| 1196 |
newop->opflag = opctx->flag; |
| 1197 |
newop->oprestype = get_file_restype(opctx->restype, fname); |
| 1198 |
newop->opres = (char *)(newop + 1); |
| 1199 |
strcpy(newop->opres, fullurl); |
| 1200 |
newop->opfile = newop->opres + strlen(newop->opres) + 1; |
| 1201 |
strcpy(newop->opfile, fullname); |
| 1202 |
|
| 1203 |
|
| 1204 |
cur = newop; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
os_close_dir(dirhdl); |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
return cur; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
static int parse_res_type(char *p) |
| 1219 |
{ |
| 1220 |
|
| 1221 |
if (!stricmp(p, "xfcn")) |
| 1222 |
return RESTYPE_XFCN; |
| 1223 |
else if (!stricmp(p, "html")) |
| 1224 |
return RESTYPE_HTML; |
| 1225 |
else |
| 1226 |
{ |
| 1227 |
rscptf("invalid resource type specified: %s", p); |
| 1228 |
errexit("", 1); |
| 1229 |
return 0; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
static opdef *addop(opdef *cur, char *nam, opctxdef *opctx) |
| 1237 |
{ |
| 1238 |
char *p; |
| 1239 |
opdef *newop; |
| 1240 |
char resname[OSFNMAX]; |
| 1241 |
unsigned long fmode; |
| 1242 |
unsigned long fattr; |
| 1243 |
|
| 1244 |
|
| 1245 |
if (opctx->doing_type) |
| 1246 |
{ |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
opctx->restype = parse_res_type(nam); |
| 1252 |
|
| 1253 |
|
| 1254 |
opctx->doing_type = FALSE; |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
return cur; |
| 1261 |
} |
| 1262 |
|
| 1263 |
|
| 1264 |
if (*nam == '-') |
| 1265 |
{ |
| 1266 |
|
| 1267 |
if (!stricmp(nam, "-type")) |
| 1268 |
{ |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
opctx->doing_type = TRUE; |
| 1274 |
} |
| 1275 |
else if (!stricmp(nam, "-replace")) |
| 1276 |
{ |
| 1277 |
|
| 1278 |
opctx->flag = OPFADD | OPFDEL; |
| 1279 |
} |
| 1280 |
else if (!stricmp(nam, "-delete")) |
| 1281 |
{ |
| 1282 |
|
| 1283 |
opctx->flag = OPFDEL; |
| 1284 |
} |
| 1285 |
else if (!stricmp(nam, "-add")) |
| 1286 |
{ |
| 1287 |
|
| 1288 |
opctx->flag = OPFADD; |
| 1289 |
} |
| 1290 |
else |
| 1291 |
{ |
| 1292 |
|
| 1293 |
rscptf("invalid option: %s", nam); |
| 1294 |
errexit("", 1); |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
return cur; |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
for (p = nam ; *p && *p != '=' ; ++p); |
| 1306 |
if (*p == '=') |
| 1307 |
{ |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
*p = '\0'; |
| 1315 |
|
| 1316 |
|
| 1317 |
++p; |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
while (t_isspace(*p)) |
| 1324 |
++p; |
| 1325 |
} |
| 1326 |
else |
| 1327 |
{ |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
os_cvt_dir_url(resname, sizeof(resname), nam); |
| 1334 |
|
| 1335 |
|
| 1336 |
p = resname; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
if (osfmode(nam, TRUE, &fmode, &fattr) |
| 1345 |
&& (fmode & OSFMODE_DIR) != 0) |
| 1346 |
return addopdir(cur, nam, opctx); |
| 1347 |
|
| 1348 |
|
| 1349 |
newop = (opdef *)malloc(sizeof(opdef) + strlen(p) + strlen(nam) + 2); |
| 1350 |
|
| 1351 |
newop->opnxt = cur; |
| 1352 |
newop->opflag = opctx->flag; |
| 1353 |
newop->opres = (char *)(newop + 1); |
| 1354 |
newop->oprestype = get_file_restype(opctx->restype, nam); |
| 1355 |
strcpy(newop->opres, p); |
| 1356 |
newop->opfile = newop->opres + strlen(newop->opres) + 1; |
| 1357 |
strcpy(newop->opfile, nam); |
| 1358 |
|
| 1359 |
return(newop); |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
int main(int argc, char **argv) |
| 1364 |
{ |
| 1365 |
int curarg; |
| 1366 |
osfildef *fpin; |
| 1367 |
osfildef *fpout; |
| 1368 |
char tmpfile[OSFNMAX + 1]; |
| 1369 |
char inbuf[OSFNMAX + 1]; |
| 1370 |
char *p; |
| 1371 |
char *infile; |
| 1372 |
char buf[128]; |
| 1373 |
opdef *oplist = (opdef *)0; |
| 1374 |
opctxdef opctx; |
| 1375 |
int do_create = FALSE; |
| 1376 |
|
| 1377 |
|
| 1378 |
rscptf("TADS Resource Manager version 2.2.4\n"); |
| 1379 |
rscptf("Copyright (c) 1992, 1999 by Michael J. Roberts. "); |
| 1380 |
rscptf("All Rights Reserved.\n"); |
| 1381 |
if (argc < 2) usage(); |
| 1382 |
|
| 1383 |
|
| 1384 |
opctx.restype = RESTYPE_DFLT; |
| 1385 |
opctx.flag = OPFADD | OPFDEL; |
| 1386 |
opctx.doing_type = FALSE; |
| 1387 |
|
| 1388 |
|
| 1389 |
for (curarg = 1 ; curarg < argc ; ++curarg) |
| 1390 |
{ |
| 1391 |
|
| 1392 |
if (argv[curarg][0] != '-') |
| 1393 |
break; |
| 1394 |
|
| 1395 |
|
| 1396 |
if (!stricmp(argv[curarg], "-create")) |
| 1397 |
{ |
| 1398 |
|
| 1399 |
do_create = TRUE; |
| 1400 |
} |
| 1401 |
else |
| 1402 |
{ |
| 1403 |
rscptf("unrecognized file option \"%s\"", argv[curarg]); |
| 1404 |
errexit("", 1); |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
infile = argv[curarg++]; |
| 1410 |
strcpy(inbuf, infile); |
| 1411 |
os_defext(inbuf, "gam"); |
| 1412 |
|
| 1413 |
|
| 1414 |
if (do_create) |
| 1415 |
{ |
| 1416 |
|
| 1417 |
fpin = 0; |
| 1418 |
} |
| 1419 |
else if ((fpin = osfoprb(inbuf, OSFTGAME)) == 0) |
| 1420 |
{ |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
errexit("unable to open resource file", 1); |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
if (curarg == argc && fpin != 0) |
| 1433 |
{ |
| 1434 |
rscproc(fpin, (osfildef *)0, 0); |
| 1435 |
osfcls(fpin); |
| 1436 |
os_term(OSEXSUCC); |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
if (do_create) |
| 1447 |
{ |
| 1448 |
|
| 1449 |
if ((fpout = osfopwb(inbuf, OSFTGAME)) == 0) |
| 1450 |
errexit("unable to create file", 1); |
| 1451 |
|
| 1452 |
|
| 1453 |
rscptf("\nFile created.\n"); |
| 1454 |
} |
| 1455 |
else |
| 1456 |
{ |
| 1457 |
|
| 1458 |
strcpy(tmpfile, inbuf); |
| 1459 |
for (p = tmpfile + strlen(tmpfile) ; p > tmpfile && |
| 1460 |
*(p-1) != ':' && *(p-1) != '\\' && *(p-1) != '/' ; --p); |
| 1461 |
strcpy(p, "$TADSRSC.TMP"); |
| 1462 |
|
| 1463 |
|
| 1464 |
if ((fpout = osfopwb(tmpfile, OSFTGAME)) == 0) |
| 1465 |
errexit("unable to create temporary file", 1); |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
if (curarg < argc && argv[curarg][0] == '@') |
| 1470 |
{ |
| 1471 |
osfildef *argfp; |
| 1472 |
int l; |
| 1473 |
char *p; |
| 1474 |
|
| 1475 |
if (!(argfp = osfoprt(argv[curarg]+1, OSFTTEXT))) |
| 1476 |
errexit("unable to open response file", 1); |
| 1477 |
|
| 1478 |
for (;;) |
| 1479 |
{ |
| 1480 |
if (!osfgets(buf, sizeof(buf), argfp)) break; |
| 1481 |
l = strlen(buf); |
| 1482 |
if (l && buf[l-1] == '\n') buf[--l] = '\0'; |
| 1483 |
for (p = buf ; t_isspace(*p) ; ++p); |
| 1484 |
if (!*p) continue; |
| 1485 |
oplist = addop(oplist, p, &opctx); |
| 1486 |
} |
| 1487 |
osfcls(argfp); |
| 1488 |
} |
| 1489 |
else |
| 1490 |
{ |
| 1491 |
for ( ; curarg < argc ; ++curarg) |
| 1492 |
oplist = addop(oplist, argv[curarg], &opctx); |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
oplist = rscproc(fpin, fpout, oplist); |
| 1497 |
|
| 1498 |
|
| 1499 |
for ( ; oplist != 0 ; oplist = oplist->opnxt) |
| 1500 |
{ |
| 1501 |
if (!(oplist->opflag & OPFDONE)) |
| 1502 |
rscptf("warning: resource \"%s\" not found\n", oplist->opres); |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
if (fpin != 0) |
| 1507 |
osfcls(fpin); |
| 1508 |
if (fpout != 0) |
| 1509 |
osfcls(fpout); |
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
if (!do_create) |
| 1516 |
{ |
| 1517 |
|
| 1518 |
if (remove(inbuf)) |
| 1519 |
errexit("error deleting input file", 1); |
| 1520 |
|
| 1521 |
|
| 1522 |
if (rename(tmpfile, inbuf)) |
| 1523 |
errexit("error renaming temporary file", 1); |
| 1524 |
} |
| 1525 |
|
| 1526 |
|
| 1527 |
os_term(OSEXSUCC); |
| 1528 |
return OSEXSUCC; |
| 1529 |
} |
| 1530 |
|