Pristine Ack-5.5
[Ack-5.5.git] / mach / 6500 / dl / dl.c
1 /* $Id: dl.c,v 2.5 1994/06/24 12:54:46 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  *
6  */
7
8 #include        <stdio.h>
9 #include        <assert.h>
10 #include        <out.h>
11
12 #define DATTYPE         0
13 #define EOFTYPE         1
14 #define SEGTYPE         2
15 #define PCTYPE          3
16
17 #define MAXBYTE         0x18
18
19 int     check;
20 int     records;
21 int     echo;
22 int     bytecount;
23 int     ttyfd;
24
25 char    *progname;
26
27 char    hex[] = "0123456789ABCDEF";
28
29 struct outhead ohead;
30 struct outsect sect[MAXSECT];
31
32 main(argc,argv) char **argv; {
33         int i,nd,pc,first;
34         register char *s;
35
36
37         progname = argv[0];
38         if (argc > 3)
39                 fatal("usage: %s [object [tty]]\n",argv[0]);
40         s = "a.out";
41         if (argc >= 2)
42                 s = argv[1];
43         if (! rd_open(s)) {
44                 fprintf(stderr,"%s: can't open %s\n",progname,s);
45                 exit(-1);
46         }
47         rd_ohead(&ohead);
48         if (ohead.oh_flags & HF_LINK) {
49                 fprintf(stderr,"%s: %s contains unresolved references\n",progname,s);
50                 exit(-1);
51         }
52         rd_sect(sect, ohead.oh_nsect);
53         ttyfd = 1;
54         first = 1;
55         for (i = 0; i < ohead.oh_nsect; i++) {
56                 rd_outsect(i);
57                 pc = sect[i].os_base;
58                 while (sect[i].os_size) {
59                         unsigned int sz = 8096, fl;
60                         extern char *calloc();
61                         register char *buf;
62                         char *pbuf;
63
64                         if (sz > sect[i].os_size) sz = sect[i].os_size;
65                         sect[i].os_size -= sz;
66                         pbuf = buf = calloc(sz, 1);
67                         if (fl = sect[i].os_flen) {
68                                 if (fl > sz) fl = sz;
69                                 sect[i].os_flen -= fl;
70
71                                 rd_emit(buf, (long) fl);
72                         }
73                         while (sz >= MAXBYTE) {
74                                 data(MAXBYTE, (int) pc, buf);
75                                 sz -= MAXBYTE;
76                                 buf += MAXBYTE;
77                                 pc += MAXBYTE;
78                                 first = 0;
79                         }
80                         if (sz > 0) {
81                                 data(sz, (int) pc, buf);
82                                 first = 0;
83                         }
84                         free(pbuf);
85                 }
86         }
87         if (first == 0)
88                 eof();
89         if (echo)
90                 for (;;)
91                         reply();
92 }
93
94 data(nd,pc, buf)
95         register char *buf;
96 {
97
98         newline(nd,pc,DATTYPE);
99         do
100                 byte(*buf++);
101         while (--nd);
102         endline();
103 }
104
105 eof() {
106
107         newline(0,records,EOFTYPE);
108         endline();
109 }
110
111 newline(n,pc,typ) {
112
113         records++;
114         put(';');
115         byte(n);
116         check = 0;
117         bytecount = n+4;
118         word(pc);
119 }
120
121 endline() {
122
123         word(check);
124         put('\r');
125         put('\n');
126         assert(bytecount == 0);
127 put(0);
128 put(0);
129 put(0);
130 put(0);
131 put(0);
132 put(0);
133 }
134
135 word(w) {
136
137         byte(w>>8);
138         byte(w);
139 }
140
141 byte(b) {
142
143 b &= 0377;
144         check += b;
145         --bytecount;
146         put(hex[(b>>4) & 017]);
147         put(hex[b & 017]);
148 }
149
150 put(c)
151         char c;
152 {
153
154         write(ttyfd,&c,1);
155 }
156
157 reply() {
158         register i;
159         char c;
160
161         if (echo == 0)
162                 return;
163         i = read(ttyfd,&c,1);
164         assert(i > 0);
165         write(1,&c,1);
166 }
167
168 fatal(s,a) {
169
170         fprintf(stderr,"%s: ",progname);
171         fprintf(stderr,s,a);
172         fprintf(stderr,"\n");
173         exit(-1);
174 }
175
176 rd_fatal() { fatal("read error"); }