dl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* $Header$ */
  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 <stdio.h>
  8. #include <assert.h>
  9. #include <out.h>
  10. #define DATTYPE 0
  11. #define EOFTYPE 1
  12. #define SEGTYPE 2
  13. #define PCTYPE 3
  14. #define MAXBYTE 0x18
  15. int check;
  16. int records;
  17. int echo;
  18. int bytecount;
  19. int ttyfd;
  20. char *progname;
  21. char hex[] = "0123456789ABCDEF";
  22. struct outhead ohead;
  23. struct outsect sect[MAXSECT];
  24. main(argc,argv) char **argv; {
  25. int i,nd,pc,first;
  26. register char *s;
  27. progname = argv[0];
  28. if (argc > 3)
  29. fatal("usage: %s [object [tty]]\n",argv[0]);
  30. s = "a.out";
  31. if (argc >= 2)
  32. s = argv[1];
  33. if (! rd_open(s)) {
  34. fprintf(stderr,"%s: can't open %s\n",progname,s);
  35. exit(-1);
  36. }
  37. rd_ohead(&ohead);
  38. if (ohead.oh_flags & HF_LINK) {
  39. fprintf(stderr,"%s: %s contains unresolved references\n",progname,s);
  40. exit(-1);
  41. }
  42. rd_sect(sect, ohead.oh_nsect);
  43. ttyfd = 1;
  44. first = 1;
  45. for (i = 0; i < ohead.oh_nsect; i++) {
  46. rd_outsect(i);
  47. pc = sect[i].os_base;
  48. while (sect[i].os_size) {
  49. unsigned int sz = 8096, fl;
  50. extern char *calloc();
  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. fprintf(stderr,"%s: ",progname);
  141. fprintf(stderr,s,a);
  142. fprintf(stderr,"\n");
  143. exit(-1);
  144. }
  145. rd_fatal() { fatal("read error"); }