dl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <sgtty.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. struct sgttyb tty;
  11. #define DATTYPE 0
  12. #define EOFTYPE 1
  13. #define SEGTYPE 2
  14. #define PCTYPE 3
  15. #define MAXBYTE 32
  16. int check;
  17. int echo;
  18. int istty;
  19. int bytecount;
  20. int ttyfd;
  21. char *progname;
  22. char hex[] = "0123456789ABCDEF";
  23. main(argc,argv) char **argv; {
  24. register nd,pc,sg,osg,first;
  25. register char *s;
  26. int uid;
  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 (freopen(s,"r",stdin) == NULL)
  34. fatal("can't open %s",s);
  35. s = "/dev/tty05";
  36. if (argc >= 3)
  37. s = argv[2];
  38. if ((ttyfd = open(s,2)) < 0)
  39. if ((ttyfd = creat(s,0666)) < 0)
  40. fatal("can't open %s",s);
  41. if (gtty(ttyfd,&tty) == 0) {
  42. echo++;
  43. istty++;
  44. tty.sg_ispeed = tty.sg_ospeed = B2400;
  45. tty.sg_flags = RAW;
  46. stty(ttyfd,&tty);
  47. } else {
  48. freopen(s,"w",stdout);
  49. }
  50. first = 1; osg = 0;
  51. /* uid = getuid(); */
  52. /* lock(1); */
  53. for (;;) {
  54. pc = get2c(stdin);
  55. if (feof(stdin))
  56. break;
  57. sg = get2c(stdin);
  58. nd = get2c(stdin);
  59. if (first) {
  60. put('L'); reply();
  61. put('S'); reply();
  62. first = 0;
  63. }
  64. if (sg != osg) {
  65. segment(sg);
  66. osg = sg;
  67. }
  68. while (nd > MAXBYTE) {
  69. data(MAXBYTE,pc);
  70. nd -= MAXBYTE;
  71. pc += MAXBYTE;
  72. }
  73. if (nd > 0)
  74. data(nd,pc);
  75. assert(feof(stdin) == 0);
  76. }
  77. if (first == 0)
  78. eof();
  79. /* lock(0); */
  80. /* setuid(uid); */
  81. /* if (echo) */
  82. /* for (;;) */
  83. /* reply(); */
  84. }
  85. segment(sg) {
  86. newline(2,0,SEGTYPE);
  87. word(sg);
  88. endline();
  89. }
  90. startad(pc) {
  91. newline(4,0,PCTYPE);
  92. word(0);
  93. word(pc);
  94. endline();
  95. }
  96. data(nd,pc) {
  97. newline(nd,pc,DATTYPE);
  98. do
  99. byte(getc(stdin));
  100. while (--nd);
  101. endline();
  102. }
  103. eof() {
  104. newline(0,0,EOFTYPE);
  105. byte(0xFF);
  106. put('\n');
  107. }
  108. newline(n,pc,typ) {
  109. check = 0;
  110. bytecount = n+5;
  111. put('\n'); /* added instruction */
  112. put(':');
  113. byte(n);
  114. word(pc);
  115. byte(typ);
  116. }
  117. endline() {
  118. byte(-check);
  119. assert(bytecount == 0);
  120. assert(check == 0);
  121. }
  122. word(w) {
  123. byte(w>>8);
  124. byte(w);
  125. }
  126. byte(b) {
  127. check += b;
  128. --bytecount;
  129. put(hex[(b>>4) & 017]);
  130. put(hex[b & 017]);
  131. }
  132. put(c) {
  133. if (istty)
  134. write(ttyfd,&c,1);
  135. else
  136. putchar(c);
  137. }
  138. reply() {
  139. register i;
  140. int c;
  141. if (echo == 0)
  142. return;
  143. i = read(ttyfd,&c,1);
  144. assert(i > 0);
  145. write(1,&c,1);
  146. }
  147. get2c(f) FILE *f; {
  148. register c;
  149. c = getc(f);
  150. return((getc(f) << 8) | c);
  151. }
  152. fatal(s,a) {
  153. fprintf(stderr,"%s: ",progname);
  154. fprintf(stderr,s,a);
  155. fprintf(stderr,"\n");
  156. exit(-1);
  157. }