Pristine Ack-5.5
[Ack-5.5.git] / util / led / write.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 #ifndef lint
6 static char rcsid[] = "$Id: write.c,v 3.6 1994/06/24 10:35:26 ceriel Exp $";
7 #endif
8
9 #include <out.h>
10 #include <stdio.h>
11 #include "const.h"
12 #include "assert.h"
13 #include "memory.h"
14
15 extern struct outhead   outhead;
16 extern struct outsect   outsect[];
17 extern int              flagword;
18 extern bool             incore;
19
20 wr_fatal()
21 {
22         fatal("write error");
23 }
24
25 static long             off_char;
26
27 /*
28  * Open the output file according to the chosen strategy.
29  * Write away the header and section table: they will not change anymore.
30  */
31 begin_write()
32 {
33         register struct outhead *hd = &outhead;
34
35         assert(! incore);
36         wr_ohead(hd);
37         wr_sect(outsect, hd->oh_nsect);
38         off_char = OFF_CHAR(*hd);
39 }
40
41 static struct outname *
42 sectname(sectindex)
43         int                     sectindex;
44 {
45         static struct outname   namebuf;
46
47         namebuf.on_foff = (long)0;      /* No string name. */
48         namebuf.on_type = (S_MIN + sectindex) | S_SCT;
49         namebuf.on_desc = 0;
50         namebuf.on_valu = outsect[sectindex].os_base;
51
52         return &namebuf;
53 }
54
55 /*
56  * Write out the symbol table and the section names.
57  */
58 end_write()
59 {
60         register struct outname *name;
61         register int            sectindex;
62         extern unsigned short   NGlobals;
63         extern long             NGChars;
64
65         assert(!incore);
66         assert(!(flagword & SFLAG));
67         name = (struct outname *)address(ALLOGLOB, (ind_t)0);
68         namecpy(name, NGlobals, off_char);
69         wr_name(name, NGlobals);
70         wr_string(mems[ALLOGCHR].mem_base+1, NGChars);
71         off_char += NGChars;
72
73         for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++)
74                 wrt_name(sectname(sectindex), 1);
75 }
76         
77 wrt_emit(emit, sectindex, cnt)
78         char            *emit;
79         int             sectindex;
80         long            cnt;
81 {
82
83         wr_outsect(sectindex);
84         wr_emit(emit, cnt);
85 }
86
87 wrt_nulls(sectindex, cnt)
88         register long cnt;
89 {
90         static char nullbuf[BUFSIZ];
91
92         wr_outsect(sectindex);
93         while (cnt) {
94                 register int n = cnt >= BUFSIZ ? BUFSIZ : cnt;
95                 wr_emit(nullbuf, (long)n);
96                 cnt -= n;
97         }
98 }
99
100 wrt_name(name, writename)
101         register struct outname *name;
102 {
103         assert(!incore);
104         assert(!(flagword & SFLAG));
105         if (name->on_mptr != (char *)0) {
106                 register long   len = strlen(name->on_mptr) + 1;
107
108                 wr_string(name->on_mptr, len);
109                 name->on_foff = off_char;
110                 off_char += len;
111         } else {
112                 name->on_foff = (long)0;
113         }
114         if (writename) wr_name(name, 1);
115 }