dl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. *
  4. * This product is part of the Amsterdam Compiler Kit.
  5. *
  6. * Permission to use, sell, duplicate or disclose this software must be
  7. * obtained in writing. Requests for such permissions may be sent to
  8. *
  9. * Dr. Andrew S. Tanenbaum
  10. * Wiskundig Seminarium
  11. * Vrije Universiteit
  12. * Postbox 7161
  13. * 1007 MC Amsterdam
  14. * The Netherlands
  15. *
  16. */
  17. #include <sgtty.h>
  18. #include <stdio.h>
  19. #include <assert.h>
  20. struct sgttyb tty;
  21. #define DATTYPE 0
  22. #define EOFTYPE 1
  23. #define SEGTYPE 2
  24. #define PCTYPE 3
  25. #define MAXBYTE 32
  26. int check;
  27. int echo;
  28. int istty;
  29. int bytecount;
  30. int ttyfd;
  31. char *progname;
  32. char hex[] = "0123456789ABCDEF";
  33. main(argc,argv) char **argv; {
  34. register nd,pc,sg,osg,first;
  35. register char *s;
  36. int uid;
  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 (freopen(s,"r",stdin) == NULL)
  44. fatal("can't open %s",s);
  45. s = "/dev/tty05";
  46. if (argc >= 3)
  47. s = argv[2];
  48. if ((ttyfd = open(s,2)) < 0)
  49. if ((ttyfd = creat(s,0666)) < 0)
  50. fatal("can't open %s",s);
  51. if (gtty(ttyfd,&tty) == 0) {
  52. echo++;
  53. istty++;
  54. tty.sg_ispeed = tty.sg_ospeed = B2400;
  55. tty.sg_flags = RAW;
  56. stty(ttyfd,&tty);
  57. } else {
  58. freopen(s,"w",stdout);
  59. }
  60. first = 1; osg = 0;
  61. uid = getuid();
  62. lock(1);
  63. for (;;) {
  64. pc = get2c(stdin);
  65. if (feof(stdin))
  66. break;
  67. sg = get2c(stdin);
  68. nd = get2c(stdin);
  69. if (first) {
  70. put('L'); reply();
  71. put('S'); reply();
  72. first = 0;
  73. }
  74. if (sg != osg) {
  75. segment(sg);
  76. osg = sg;
  77. }
  78. while (nd > MAXBYTE) {
  79. data(MAXBYTE,pc);
  80. nd -= MAXBYTE;
  81. pc += MAXBYTE;
  82. }
  83. if (nd > 0)
  84. data(nd,pc);
  85. assert(feof(stdin) == 0);
  86. }
  87. if (first == 0)
  88. eof();
  89. /* lock(0); */
  90. /* setuid(uid); */
  91. /* if (echo) */
  92. /* for (;;) */
  93. /* reply(); */
  94. }
  95. segment(sg) {
  96. newline(2,0,SEGTYPE);
  97. word(sg);
  98. endline();
  99. }
  100. startad(pc) {
  101. newline(4,0,PCTYPE);
  102. word(0);
  103. word(pc);
  104. endline();
  105. }
  106. data(nd,pc) {
  107. newline(nd,pc,DATTYPE);
  108. do
  109. byte(getc(stdin));
  110. while (--nd);
  111. endline();
  112. }
  113. eof() {
  114. newline(0,0,EOFTYPE);
  115. byte(0xFF);
  116. put('\n');
  117. }
  118. newline(n,pc,typ) {
  119. check = 0;
  120. bytecount = n+5;
  121. put('\n'); /* added instruction */
  122. put(':');
  123. byte(n);
  124. word(pc);
  125. byte(typ);
  126. }
  127. endline() {
  128. byte(-check);
  129. assert(bytecount == 0);
  130. assert(check == 0);
  131. }
  132. word(w) {
  133. byte(w>>8);
  134. byte(w);
  135. }
  136. byte(b) {
  137. check += b;
  138. --bytecount;
  139. put(hex[(b>>4) & 017]);
  140. put(hex[b & 017]);
  141. }
  142. put(c) {
  143. if (istty)
  144. write(ttyfd,&c,1);
  145. else
  146. putchar(c);
  147. }
  148. reply() {
  149. register i;
  150. int c;
  151. if (echo == 0)
  152. return;
  153. i = read(ttyfd,&c,1);
  154. assert(i > 0);
  155. write(1,&c,1);
  156. }
  157. get2c(f) FILE *f; {
  158. register c;
  159. c = getc(f);
  160. return((getc(f) << 8) | c);
  161. }
  162. fatal(s,a) {
  163. fprintf(stderr,"%s: ",progname);
  164. fprintf(stderr,s,a);
  165. fprintf(stderr,"\n");
  166. exit(-1);
  167. }