Debian Codegraph @

terps/tads/tads2/tadsrsc.c

raw symbols SCIP
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 * Copyright (c) 1992, 1998 by 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 tadsrsc.c - TADS v2 resource manager
15 Function
16 resource manager for TADS v2
17 Notes
18 Somewhat improved over TADS v1 resource manager thanks to new file
19 format. This version can list, add, replace, and remove resources.
20 Modified
21 04/30/92 MJRoberts - creation
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 /* printf */
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; /* next operation in list */
86 char *opres; /* resource name */
87 char *opfile; /* file to add */
88 int opflag; /* indication of what to do with resource */
89 int oprestype; /* resource type */
90 # define OPFADD 0x01 /* add resource */
91 # define OPFDEL 0x02 /* delete resource */
92 # define OPFDONE 0x04 /* this entry has been processed */
93 # define OPFADDDIR 0x08 /* add an entire directory */
94 };
95
96 /* resource types */
97 #define RESTYPE_DFLT 0x00 /* default - infer type from filename suffix */
98 #define RESTYPE_XFCN 0x01
99 #define RESTYPE_HTML 0x02
100
101
102 /* copy a block of bytes from the input file to the output file */
103 char copybuf[16 * 1024];
104 static void copybytes(osfildef *fpin, osfildef *fpout, ulong siz)
105 {
106 uint cursiz;
107
108 /* copy bytes until we run out */
109 while (siz != 0)
110 {
111 /* we can copy up to one full buffer at a time */
112 cursiz = (siz > sizeof(copybuf) ? sizeof(copybuf) : siz);
113
114 /* deduct the amount we're copying from the total */
115 siz -= cursiz;
116
117 /* read from input, copy to output */
118 if (osfrb(fpin, copybuf, cursiz)
119 || osfwb(fpout, copybuf, cursiz))
120 {
121 /* error - close files and display an error */
122 osfcls(fpin);
123 osfcls(fpout);
124 errexit("error copying resource", 1);
125 }
126 }
127 }
128
129 /* copy a resource from the input file to the output file */
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 /* note the starting position */
138 startpos = osfpos(fpout);
139
140 /* copy the bytes of the resource */
141 copybytes(fpin, fpout, siz);
142
143 /* note the ending position */
144 endpos = osfpos(fpout);
145
146 /* write the ending position at the appropriate point */
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 /* seek back to the end of the output file */
152 osfseek(fpout, endpos, OSFSK_SET);
153 }
154
155 /* process an operation */
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 /* remember location of first resource if necessary */
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 /* get file size */
176 osfseek(fpin, 0L, OSFSK_END);
177 fsiz = (uint)osfpos(fpin);
178 osfseek(fpin, 0L, OSFSK_SET);
179
180 /* set up the resource type part of the header */
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 /* set up the rest of the header */
195 if (osfwb(fpout, buf, buf[0] + 1)) errexit("error writing resource", 1);
196 sizpos = osfpos(fpout); /* remember where size field goes */
197 oswp4(buf, 0);
198 if (osfwb(fpout, buf, 4)) errexit("error writing resource", 1);
199
200 /* set up the header */
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 /* copy the resource to the output */
208 copybytes(fpin, fpout, fsiz);
209
210 /* write end position in the resource header */
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 /* seek back to the end of the resource in the output file */
217 osfseek(fpout, endpos, OSFSK_SET);
218
219 /* done with the input file */
220 osfcls(fpin);
221 }
222
223 /* display information on an operation */
224 static void show_op(const char *desc, const char *resname, size_t resnamelen,
225 int restype)
226 {
227 const char *restypename;
228
229 /* figure the type name */
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 /* display the message */
246 rscptf(". %s %.*s (%s)\n", desc, resnamelen, resname, restypename);
247 }
248
249 /* display a list item */
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 /* if we haven't displayed a header yet, do so now */
254 if (!*showed_heading)
255 {
256 /* show the header */
257 rscptf("Type Size Name\n"
258 "----------------------------------------------------------\n");
259
260 /* note that we've displayed the header now */
261 *showed_heading = TRUE;
262 }
263
264 /* show this item */
265 rscptf("%.4s %8lu %.*s\n", type_name, res_size,
266 (int)name_len, res_name);
267 }
268
269 /* HTMLRES index entry */
270 struct idx_t
271 {
272 /* source of the resource */
273 opdef *src_op;
274 struct idx_t *src_idx;
275
276 /* information on the resource in the file */
277 ulong ofs;
278 ulong siz;
279 ushort namlen;
280 struct idx_t *nxt;
281 char nam[1];
282 };
283
284 /*
285 * Allocate an index entry
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 /* allocate a new entry */
294 entry = (struct idx_t *)malloc(sizeof(struct idx_t) + namlen - 1);
295
296 /* fill it in */
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 /* return the new entry */
305 return entry;
306 }
307
308 /*
309 * Add an item to an index list
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 /* count its statistics */
319 ++(*out_res_cnt);
320 *out_total_name_len += namlen;
321
322 /* allocate a new entry */
323 entry = alloc_idx_entry(ofs, siz, nam, namlen, src_op, src_idx);
324
325 /* link it into the list */
326 entry->nxt = *list;
327 *list = entry;
328 }
329
330 /*
331 * Process an HTML resource list. If 'old_htmlres' is true, it
332 * indicates that the input file is pointing to an old resource map;
333 * otherwise, we need to construct a brand new one.
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 * Scan the oplist for an HTML resource. If there aren't any, we
358 * don't need to modify the HTMLRES list, so tell the caller to copy
359 * this resource unchanged.
360 */
361 for (op = oplist, found = FALSE ; op != 0 ; op = op->opnxt)
362 {
363 /* if this is an HTML resource, note it and stop looking */
364 if (op->oprestype == RESTYPE_HTML)
365 {
366 found = TRUE;
367 break;
368 }
369 }
370
371 /*
372 * If we didn't find any operations on this resource, and we're not
373 * simply listing resources or we don't have an old resource to
374 * list, tell the caller to copy it unchanged.
375 */
376 if (!found && (fpout != 0 || !old_htmlres))
377 {
378 *copyrsc = TRUE;
379 return oplist;
380 }
381
382 /* we'll be handling the resource - tell the caller not to copy it */
383 *copyrsc = FALSE;
384
385 /* if there's an old HTMLRES resource, read it */
386 if (old_htmlres)
387 {
388 /* read the index entry count and size */
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 /* allocate space for pointers to all of the entries */
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 /* read the index table entries */
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 /* read the entry information */
411 if (osfrb(fp, buf, 10))
412 listexit(fp,
413 "unable to read HTMLRES index table entry (prefix)");
414
415 /* get the resource size */
416 res_ofs = osrp4(buf);
417 res_siz = osrp4(buf + 4);
418
419 /* read the name */
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 /* build this entry */
428 *p = alloc_idx_entry(res_ofs, res_siz, buf, name_siz, 0, 0);
429 }
430
431 /* if we don't have an output file, list the HTMLRES contents */
432 if (fpout == 0)
433 {
434 /* display all of the entries */
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 /* there's no more processing to do */
440 goto done;
441 }
442
443 /*
444 * The resources start at the end of the index table - note the
445 * location of the end of the input table, since it's the base
446 * address relative to which the resource offsets are stated.
447 */
448 in_res_base = osfpos(fp);
449
450 /*
451 * Go through the resource table in the input file. Find each
452 * one in the op list. If it's not in the op list, we'll copy
453 * it to the output file.
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 /* see if we can find this entry in the op list */
461 for (prev_op = 0, op = oplist ; op != 0 ;
462 prev_op = op, op = op->opnxt)
463 {
464 /* if this one matches, note it */
465 if (op->oprestype == RESTYPE_HTML
466 && strlen(op->opres) == (*p)->namlen
467 && !memicmp(op->opres, (*p)->nam, (*p)->namlen))
468 {
469 /*
470 * if we're adding this resource (not replacing it),
471 * warn that it's already in the file, and ignore
472 * this op; if we're removing it or replacing it,
473 * simply delete this entry from the input list so
474 * it doesn't get copied to the output.
475 */
476 if (!(op->opflag & OPFDEL))
477 {
478 /* warn that the old one will stay */
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 /* remove it from the processing list */
485 remove_res = TRUE;
486 }
487 else
488 {
489 /* we are deleting it; see if we're also adding it */
490 if (op->opflag & OPFADD)
491 {
492 /*
493 * we're replacing this resource - take this
494 * op out of the main list and put it into
495 * the add list
496 */
497 remove_res = TRUE;
498 add_res = TRUE;
499
500 /* note the addition */
501 show_op("replacing", op->opres, strlen(op->opres),
502 op->oprestype);
503 }
504 else
505 {
506 /* note the deletion */
507 show_op("deleting", op->opres, strlen(op->opres),
508 op->oprestype);
509
510 /* just remove it */
511 remove_res = TRUE;
512 }
513
514 /* get rid of this item from the input list */
515 free(*p);
516 *p = 0;
517 }
518
519 /* no need to look further in the operations list */
520 break;
521 }
522 }
523
524 /*
525 * If desired, remove this resource from the main list, and
526 * add it into the list of resources to add.
527 */
528 if (remove_res)
529 {
530 /* unlink it from the main list */
531 if (prev_op == 0)
532 oplist = op->opnxt;
533 else
534 prev_op->opnxt = op->opnxt;
535
536 /* if desired, add it to the additions list */
537 if (add_res)
538 {
539 /* we're adding it - put it in the additions list */
540 op->opnxt = add_list;
541 add_list = op;
542 }
543 else
544 {
545 /* this item has been processed - delete it */
546 free(op);
547 }
548 }
549 }
550 }
551 else
552 {
553 /* there are no entries in the input file */
554 in_entry_cnt = 0;
555 in_table_siz = 0;
556 }
557
558 /*
559 * Move all of the HTML resources marked as additions in the main
560 * operations list into the additions list.
561 */
562 for (prev_op = 0, op = oplist ; op != 0 ; op = next_op)
563 {
564 /* note the next op, in case we move this one to the other list */
565 next_op = op->opnxt;
566
567 /*
568 * if it's an HTML resource to be added, move it to the
569 * additions list
570 */
571 if (op->oprestype == RESTYPE_HTML && (op->opflag & OPFADD) != 0)
572 {
573 /* show what we're doing */
574 show_op("adding", op->opres, strlen(op->opres), op->oprestype);
575
576 /* unlink it from the main list */
577 if (prev_op == 0)
578 oplist = op->opnxt;
579 else
580 prev_op->opnxt = op->opnxt;
581
582 /* add it to the additions list */
583 op->opnxt = add_list;
584 add_list = op;
585
586 /*
587 * note that we don't want to advance the 'prev_op' pointer,
588 * since we just removed this item - the previous item is
589 * still the same as it was on the last iteration
590 */
591 }
592 else
593 {
594 /*
595 * we're leaving this op in the original list - it's now the
596 * previous op in the main list
597 */
598 prev_op = op;
599 }
600 }
601
602 /*
603 * Figure out what we'll be putting in the HTMLRES list: we'll add
604 * each surviving entry from the input file, plus all of the items
605 * in the add list, plus all of the HTML items in the main list that
606 * are marked for addition.
607 */
608 out_res_cnt = 0;
609 out_total_name_len = 0;
610
611 /* count input file entries that we're going to copy */
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 * Count items in the additions list. Note that every HTML resource
621 * marked for addition is in the additions list, since we moved all
622 * such resources out of the main list and into the additions list
623 * earlier.
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 /* write the resource header */
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 * Reserve space in the output file for the index table. We need
636 * eight bytes for the index table prefix, then ten bytes per entry
637 * plus the name sizes.
638 */
639 out_hdr_siz = 8 + (10 * out_res_cnt) + out_total_name_len;
640
641 /* write the index table prefix */
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 * Reserve space for the headers. Don't actually write them yet,
649 * since we don't know the actual locations and sizes of the
650 * entries; for now, simply reserve the space, so that we can come
651 * back here later and write the actual headers. Note that we
652 * deduct the eight bytes we've already written from the amount of
653 * filler to put in.
654 */
655 for (rem = out_hdr_siz - 8 ; rem != 0 ; )
656 {
657 ulong amt;
658
659 /* write out a buffer full */
660 amt = (rem > sizeof(buf) ? sizeof(buf) : rem);
661 if (osfwb(fpout, buf, amt))
662 listexit(fp, "unable to write HTMLRES header");
663
664 /* deduct the amount we wrote from the remainder */
665 rem -= amt;
666 }
667
668 /*
669 * note the current position in the output file - this is the base
670 * address of the resources
671 */
672 out_res_base = osfpos(fpout);
673
674 /*
675 * Write the resources.
676 */
677 for (cur = out_list ; cur != 0 ; cur = cur->nxt)
678 {
679 /*
680 * note the current file position as an offset from the resource
681 * base in the output file - this is the offset that we need to
682 * store in the index entry for this object
683 */
684 cur->ofs = osfpos(fpout) - out_res_base;
685
686 /*
687 * Copy the resource to the output. If it comes from the input
688 * file, copy from there, otherwise go out and find the external
689 * file and copy its contents.
690 */
691 if (cur->src_op != 0)
692 {
693 osfildef *fpext;
694 ulong fsiz;
695
696 /* it comes from an external file - open the file */
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 /* figure the size of the file */
705 osfseek(fpext, 0L, OSFSK_END);
706 fsiz = osfpos(fpext);
707 osfseek(fpext, 0L, OSFSK_SET);
708
709 /* copy the contents of the external file to the output */
710 copybytes(fpext, fpout, fsiz);
711
712 /* the size is the same as the external file's size */
713 cur->siz = fsiz;
714
715 /* done with the file */
716 osfcls(fpext);
717 }
718 else
719 {
720 /*
721 * it comes from the input resource file - seek to the start
722 * of the resource in the input file, and copy the data to
723 * the output file
724 */
725 osfseek(fp, in_res_base + cur->src_idx->ofs, OSFSK_SET);
726 copybytes(fp, fpout, cur->src_idx->siz);
727
728 /* the size is the same as in the input file */
729 cur->siz = cur->src_idx->siz;
730 }
731 }
732
733 /* note the current output position - this is the end of the resource */
734 out_endpos = osfpos(fpout);
735
736 /*
737 * Now that we've written all of the resources and know their actual
738 * layout in the file, we can go back and write the index table.
739 */
740 osfseek(fpout, out_hdr_pos + 8, OSFSK_SET);
741 for (cur = out_list ; cur != 0 ; cur = cur->nxt)
742 {
743 /* build this object's index table entry */
744 oswp4(buf, cur->ofs);
745 oswp4(buf + 4, cur->siz);
746 oswp2(buf + 8, cur->namlen);
747
748 /* write the entry */
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 * We're done building the resource; now all we need to do is go
756 * back and write the ending position of the resource in the
757 * resource header.
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 /* seek back to the end of the resource in the output file */
765 osfseek(fpout, out_endpos, OSFSK_SET);
766
767 done:
768 /* if we have an input list, free it */
769 if (in_list != 0)
770 {
771 /* delete all of the entries in the input table */
772 for (i = 0, p = in_list ; i < in_entry_cnt ; ++i, ++p)
773 {
774 /* delete this entry if we haven't already done so */
775 if (*p != 0)
776 free(*p);
777 }
778
779 /* delete the input pointer list itself */
780 free(in_list);
781 }
782
783 /*
784 * delete everything in the additions list, since we're done with
785 * them now
786 */
787 for (op = add_list ; op != 0 ; op = next_op)
788 {
789 /* note the next entry in the list */
790 next_op = op->opnxt;
791
792 /* delete this entry */
793 free(op);
794 }
795
796 /* return the op list in its current form */
797 return oplist;
798 }
799
800
801 /* process changes to resource file (or just list it) */
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 * if we're reading an existing file, check it header; otherwise,
823 * write out a brand new file header
824 */
825 if (fp != 0)
826 {
827 /*
828 * the input file exists -- check file and version headers, and
829 * get flags and timestamp
830 */
831 if (osfrb(fp, buf, (int)(sizeof(FIOFILHDR) + sizeof(FIOVSNHDR) + 2)))
832 listexit(fp, "error reading file header");
833
834 /* check the file type */
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 /* check the version header */
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 /* get the timestamp */
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 /* construct a new file header */
859 memcpy(buf, FIOFILHDRRSC, sizeof(FIOFILHDRRSC));
860 memcpy(buf + sizeof(FIOFILHDR), FIOVSNHDR, sizeof(FIOVSNHDR));
861
862 /* clear the flags */
863 oswp2(buf + sizeof(FIOFILHDR) + sizeof(FIOVSNHDR), 0);
864
865 /* construct the timestamp */
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 /* if listing, show file creation timestamp */
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 * Process the input file, if there is one
887 */
888 for ( ; fp != 0 ; )
889 {
890 /* assume this resource will be copied to the output */
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 /* see what kind of resource we have, and do the right thing */
906 if (!strcmp((char *)nambuf, "$EOF"))
907 {
908 /* end of file marker - quit here */
909 break;
910 }
911 else if (!strcmp((char *)nambuf, "HTMLRES"))
912 {
913 /* if we've already found an HTMLRES list, it's an error */
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 /* go process it */
923 oplist = prochtmlres(fp, fpout, oplist, &copyrsc,
924 &showed_heading, TRUE);
925 }
926
927 /* note that we've found a resource */
928 found_user_rsc = TRUE;
929 found_htmlres = TRUE;
930 }
931 else if (!strcmp((char *)nambuf, "EXTCNT"))
932 {
933 /* place to write start of XFCN's? */
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 /* see if this resource is in the list */
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 /* note that this resource has now been processed */
954 op->opflag |= OPFDONE;
955
956 /*
957 * if it's already here, and we're not deleting
958 * it, warn that the old one will stay around
959 */
960 if (!(op->opflag & OPFDEL))
961 {
962 /* warn that we're going to ignore it */
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 /* don't add it */
969 op->opflag &= ~OPFADD;
970 }
971 else
972 {
973 /*
974 * we're deleting this resource; if adding
975 * it back in (i.e., replacing it), process
976 * the add operation now
977 */
978 if (op->opflag & OPFADD)
979 {
980 /* show what we're doing */
981 show_op("replacing", op->opres,
982 strlen(op->opres), op->oprestype);
983
984 /*
985 * add the external file, replacing the
986 * one in the input file
987 */
988 procop(fpout, op, &first_xfcn);
989 }
990 else
991 {
992 /* note that we're deleting it */
993 show_op("deleting", op->opres,
994 strlen(op->opres), op->oprestype);
995 }
996
997 /* don't copy the one out of the file */
998 copyrsc = FALSE;
999 }
1000 break;
1001 }
1002 }
1003 }
1004 else
1005 {
1006 /* no output file - just list the resource */
1007 show_list_item(&showed_heading, "XFCN",
1008 (ulong)rsiz, buf + 3, (size_t)buf[2]);
1009 }
1010
1011 /* note that we've found a user resource */
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 /* skip to the next resource */
1022 osfseek(fp, endpos, OSFSK_SET);
1023 }
1024
1025 /* add the HTML resources if we haven't already */
1026 if (!found_htmlres)
1027 oplist = prochtmlres(fp, fpout, oplist, &copyrsc,
1028 &showed_heading, FALSE);
1029
1030 /* now go through what's left, and add new non-HTML resources */
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 /* show what we're doing */
1039 show_op("adding", op->opres,
1040 strlen(op->opres), op->oprestype);
1041
1042 /* add the file */
1043 procop(fpout, op, &first_xfcn);
1044
1045 /* mark the operation as completed */
1046 op->opflag |= OPFDONE;
1047 }
1048 }
1049 }
1050
1051 /* if just listing, and we didn't find anything, tell the user so */
1052 if (fpout == 0 && !found_user_rsc)
1053 rscptf("No user resources found.\n");
1054
1055 /* done reading the file - finish it up */
1056 if (fpout != 0)
1057 {
1058 /* write EOF resource */
1059 if (osfwb(fpout, "\004$EOF\0\0\0\0", 9))
1060 errexit("error writing resource", 1);
1061
1062 /* write first_xfcn value to EXTCNT resource */
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 * return the final oplist (it may have been changed during
1073 * processing)
1074 */
1075 return oplist;
1076 }
1077
1078 /*
1079 * Operation parsing context. Certain options affect subsquent
1080 * operations; we use this structure to keep track of the current
1081 * settings from past arguments.
1082 */
1083 typedef struct
1084 {
1085 /*
1086 * Type to use for resources. If this is RESTYPE_DFLT, it means
1087 * that we need to infer the type from the filename suffix.
1088 */
1089 int restype;
1090
1091 /*
1092 * operations to perform - a combination of OPFADD and OPFDEL
1093 */
1094 int flag;
1095
1096 /*
1097 * context flag: if true, it means that we just parsed a -type
1098 * option, so the next argument is the type name
1099 */
1100 int doing_type;
1101 } opctxdef;
1102
1103 /*
1104 * Get the resource type of a file. If an explicit resource type is
1105 * specified in the resource type argument, we'll use that; otherwise
1106 * (i.e., the resource type argument is RESTYPE_DFLT), we'll look at the
1107 * filename suffix to determine the type.
1108 */
1109 static int get_file_restype(int restype, char *fname)
1110 {
1111 /* see if we have a specified type */
1112 if (restype == RESTYPE_DFLT)
1113 {
1114 char *p;
1115 char *lastdot;
1116
1117 /*
1118 * No specified type - infer type from the filename. If the
1119 * filename ends in .bin, .com, or has no '.', we'll assume it's
1120 * an XFCN; otherwise, it's an HTML resource.
1121 */
1122 for (p = fname, lastdot = 0 ; *p ; ++p)
1123 {
1124 /* if we're at a dot, note it, but keep looking */
1125 if (*p == '.')
1126 lastdot = p;
1127 }
1128
1129 /* check our suffix */
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 * an explicit resource type has been specified - use it without
1140 * regard to the filename
1141 */
1142 return restype;
1143 }
1144 }
1145
1146
1147 /*
1148 * Process a directory, adding an operation for each file in the
1149 * directory
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 /* HTML-ize the directory prefix */
1158 os_cvt_dir_url(dir_prefix, sizeof(dir_prefix) - 1, nam);
1159
1160 /* add a '/' if the name isn't empty */
1161 if (dir_prefix[0] != '\0')
1162 strcat(dir_prefix, "/");
1163
1164 /* search the directory */
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 /* build the full name */
1175 os_build_full_path(fullname, sizeof(fullname), nam, fname);
1176
1177 /*
1178 * If it's a file, process it; ignore subdirectories. Also
1179 * skip hidden and system files. $$$ We could easily recurse
1180 * here to process subdirectories, but it's unclear how we
1181 * should name resources found in subdirectories, so we'll just
1182 * ignore them entirely for now.
1183 */
1184 if (osfmode(fullname, TRUE, &fmode, &fattr)
1185 && (fmode & OSFMODE_DIR) == 0
1186 && (fattr & (OSFATTR_HIDDEN | OSFATTR_SYSTEM)) == 0)
1187 {
1188 /* build the full name of the resource, using the URL prefix */
1189 char fullurl[OSFNMAX];
1190 sprintf(fullurl, "%s%s", dir_prefix, fname);
1191
1192 /* build a new node and link it into the list */
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 /* it's the new head of the list */
1204 cur = newop;
1205 }
1206 }
1207
1208 /* done with the directory scan */
1209 os_close_dir(dirhdl);
1210 }
1211
1212 /* done - return the most recent head node */
1213 return cur;
1214 }
1215
1216
1217 /* get a resource type given a resource name */
1218 static int parse_res_type(char *p)
1219 {
1220 /* see what we have */
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 * Add an operation from a command line argument
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 /* see if we're parsing a -type argument */
1245 if (opctx->doing_type)
1246 {
1247 /*
1248 * parse the type name, and store it as the type for following
1249 * resources
1250 */
1251 opctx->restype = parse_res_type(nam);
1252
1253 /* we're done parsing the -type argument */
1254 opctx->doing_type = FALSE;
1255
1256 /*
1257 * we're done parsing this argument - we haven't added any
1258 * operations, so return old list head
1259 */
1260 return cur;
1261 }
1262
1263 /* see if we have an option */
1264 if (*nam == '-')
1265 {
1266 /* see what we have */
1267 if (!stricmp(nam, "-type"))
1268 {
1269 /*
1270 * note that we're doing a type, so we parse it on the next
1271 * argument
1272 */
1273 opctx->doing_type = TRUE;
1274 }
1275 else if (!stricmp(nam, "-replace"))
1276 {
1277 /* set current flags to replace */
1278 opctx->flag = OPFADD | OPFDEL;
1279 }
1280 else if (!stricmp(nam, "-delete"))
1281 {
1282 /* set current flags to delete */
1283 opctx->flag = OPFDEL;
1284 }
1285 else if (!stricmp(nam, "-add"))
1286 {
1287 /* set current flags to add */
1288 opctx->flag = OPFADD;
1289 }
1290 else
1291 {
1292 /* invalid argument */
1293 rscptf("invalid option: %s", nam);
1294 errexit("", 1);
1295 }
1296
1297 /*
1298 * done parsing this option - we didn't add a new operation, so
1299 * return the current list head
1300 */
1301 return cur;
1302 }
1303
1304 /* look for '=' */
1305 for (p = nam ; *p && *p != '=' ; ++p);
1306 if (*p == '=')
1307 {
1308 /*
1309 * We found an '=', so an explicit resource name follows - use
1310 * the given string as the resource name rather than basing the
1311 * resource name on the filename. First, overwrite the '=' with
1312 * a null byte so that the filename string is terminated.
1313 */
1314 *p = '\0';
1315
1316 /* skip the '=' (now the null byte, of course) */
1317 ++p;
1318
1319 /*
1320 * skip any spaces after the '='; leave p pointing to the start
1321 * of the resource name
1322 */
1323 while (t_isspace(*p))
1324 ++p;
1325 }
1326 else
1327 {
1328 /*
1329 * A resource name wasn't specified - synthesize a resource name
1330 * based on the filename by converting from the local file system
1331 * name to a relative URL
1332 */
1333 os_cvt_dir_url(resname, sizeof(resname), nam);
1334
1335 /* point p to the synthesized resource name */
1336 p = resname;
1337 }
1338
1339 /*
1340 * If we're adding a directory, rather than returning a single op
1341 * for the directory, expand the directory into ops for for all of
1342 * the files in the directory.
1343 */
1344 if (osfmode(nam, TRUE, &fmode, &fattr)
1345 && (fmode & OSFMODE_DIR) != 0)
1346 return addopdir(cur, nam, opctx);
1347
1348 /* allocate space and set up new op */
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 /* print main banner */
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 /* set default parsing options */
1384 opctx.restype = RESTYPE_DFLT;
1385 opctx.flag = OPFADD | OPFDEL;
1386 opctx.doing_type = FALSE;
1387
1388 /* scan file options (these come before the filename) */
1389 for (curarg = 1 ; curarg < argc ; ++curarg)
1390 {
1391 /* check if it's an option - if not, stop looking */
1392 if (argv[curarg][0] != '-')
1393 break;
1394
1395 /* check the option */
1396 if (!stricmp(argv[curarg], "-create"))
1397 {
1398 /* note that we want to create the file */
1399 do_create = TRUE;
1400 }
1401 else
1402 {
1403 rscptf("unrecognized file option \"%s\"", argv[curarg]);
1404 errexit("", 1);
1405 }
1406 }
1407
1408 /* get the file name */
1409 infile = argv[curarg++];
1410 strcpy(inbuf, infile);
1411 os_defext(inbuf, "gam");
1412
1413 /* open the file for reading, unless we're creating a new file */
1414 if (do_create)
1415 {
1416 /* creating - we have no input file */
1417 fpin = 0;
1418 }
1419 else if ((fpin = osfoprb(inbuf, OSFTGAME)) == 0)
1420 {
1421 /*
1422 * not creating, so the file must already exist - it doesn't, so
1423 * issue an error and quit
1424 */
1425 errexit("unable to open resource file", 1);
1426 }
1427
1428 /*
1429 * if no operations are desired, and we're not creating a new file,
1430 * just list the existing file's contents and quit
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 * Create an output file. If we're creating a new file, create the
1441 * file named on the command line; otherwise, create a temporary
1442 * file that we'll write to while working and then rename to the
1443 * original input filename after we've finished with the original
1444 * input file.
1445 */
1446 if (do_create)
1447 {
1448 /* create the new file */
1449 if ((fpout = osfopwb(inbuf, OSFTGAME)) == 0)
1450 errexit("unable to create file", 1);
1451
1452 /* report the creation */
1453 rscptf("\nFile created.\n");
1454 }
1455 else
1456 {
1457 /* generate a temporary filename */
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 /* open the temporary file */
1464 if ((fpout = osfopwb(tmpfile, OSFTGAME)) == 0)
1465 errexit("unable to create temporary file", 1);
1466 }
1467
1468 /* see if we need to read a response file */
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 /* process the resources */
1496 oplist = rscproc(fpin, fpout, oplist);
1497
1498 /* make sure they all got processed */
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 /* close files */
1506 if (fpin != 0)
1507 osfcls(fpin);
1508 if (fpout != 0)
1509 osfcls(fpout);
1510
1511 /*
1512 * if we didn't create a new file, remove the original input file
1513 * and rename the temp file to the original file name
1514 */
1515 if (!do_create)
1516 {
1517 /* remove the original input file */
1518 if (remove(inbuf))
1519 errexit("error deleting input file", 1);
1520
1521 /* rename the temp file to the output file */
1522 if (rename(tmpfile, inbuf))
1523 errexit("error renaming temporary file", 1);
1524 }
1525
1526 /* success */
1527 os_term(OSEXSUCC);
1528 return OSEXSUCC;
1529 }
1530