dl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* $Id$ */
  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. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "out.h"
  11. #define DATTYPE 0
  12. #define EOFTYPE 1
  13. #define SEGTYPE 2
  14. #define PCTYPE 3
  15. #define MAXBYTE 0x18
  16. int check;
  17. int records;
  18. int echo;
  19. int bytecount;
  20. int ttyfd;
  21. char *progname;
  22. char hex[] = "0123456789ABCDEF";
  23. struct outhead ohead;
  24. struct outsect sect[MAXSECT];
  25. main(argc,argv) char **argv; {
  26. int i,nd,pc,first;
  27. register char *s;
  28. progname = argv[0];
  29. if (argc > 3)
  30. fatal("usage: %s [object [tty]]\n",argv[0]);
  31. s = "a.out";
  32. if (argc >= 2)
  33. s = argv[1];
  34. if (! rd_open(s)) {
  35. fprintf(stderr,"%s: can't open %s\n",progname,s);
  36. exit(-1);
  37. }
  38. rd_ohead(&ohead);
  39. if (ohead.oh_flags & HF_LINK) {
  40. fprintf(stderr,"%s: %s contains unresolved references\n",progname,s);
  41. exit(-1);
  42. }
  43. rd_sect(sect, ohead.oh_nsect);
  44. ttyfd = 1;
  45. first = 1;
  46. for (i = 0; i < ohead.oh_nsect; i++) {
  47. rd_outsect(i);
  48. pc = sect[i].os_base;
  49. while (sect[i].os_size) {
  50. unsigned int sz = 8096, fl;
  51. register char *buf;
  52. char *pbuf;
  53. if (sz > sect[i].os_size) sz = sect[i].os_size;
  54. sect[i].os_size -= sz;
  55. pbuf = buf = calloc(sz, 1);
  56. if (fl = sect[i].os_flen) {
  57. if (fl > sz) fl = sz;
  58. sect[i].os_flen -= fl;
  59. rd_emit(buf, (long) fl);
  60. }
  61. while (sz >= MAXBYTE) {
  62. data(MAXBYTE, (int) pc, buf);
  63. sz -= MAXBYTE;
  64. buf += MAXBYTE;
  65. pc += MAXBYTE;
  66. first = 0;
  67. }
  68. if (sz > 0) {
  69. data(sz, (int) pc, buf);
  70. first = 0;
  71. }
  72. free(pbuf);
  73. }
  74. }
  75. if (first == 0)
  76. eof();
  77. if (echo)
  78. for (;;)
  79. reply();
  80. }
  81. data(nd,pc, buf)
  82. register char *buf;
  83. {
  84. newline(nd,pc,DATTYPE);
  85. do
  86. byte(*buf++);
  87. while (--nd);
  88. endline();
  89. }
  90. eof() {
  91. newline(0,records,EOFTYPE);
  92. endline();
  93. }
  94. newline(n,pc,typ) {
  95. records++;
  96. put(';');
  97. byte(n);
  98. check = 0;
  99. bytecount = n+4;
  100. word(pc);
  101. }
  102. endline() {
  103. word(check);
  104. put('\r');
  105. put('\n');
  106. assert(bytecount == 0);
  107. put(0);
  108. put(0);
  109. put(0);
  110. put(0);
  111. put(0);
  112. put(0);
  113. }
  114. word(w) {
  115. byte(w>>8);
  116. byte(w);
  117. }
  118. byte(b) {
  119. b &= 0377;
  120. check += b;
  121. --bytecount;
  122. put(hex[(b>>4) & 017]);
  123. put(hex[b & 017]);
  124. }
  125. put(c)
  126. char c;
  127. {
  128. write(ttyfd,&c,1);
  129. }
  130. reply() {
  131. register i;
  132. char c;
  133. if (echo == 0)
  134. return;
  135. i = read(ttyfd,&c,1);
  136. assert(i > 0);
  137. write(1,&c,1);
  138. }
  139. fatal(s,a)
  140. const char* s;
  141. const char* a;
  142. {
  143. fprintf(stderr,"%s: ",progname);
  144. fprintf(stderr,s,a);
  145. fprintf(stderr,"\n");
  146. exit(-1);
  147. }
  148. rd_fatal() { fatal("read error"); }