home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / lan / snuz.arj / PROTO.C < prev    next >
C/C++ Source or Header  |  1991-03-24  |  7KB  |  385 lines

  1. /*
  2.  * A note about include files. You will note that the Harvard PCIP that this
  3.  * runs with uses its own non-standard header files. Thus we have to include
  4.  * the prototypes for a number of functions that really should be in the
  5.  * standard headers. Also, PCIP is bad as far as type consistency is concerned.
  6.  * Since it is hopelessly tied to "small" memory model, we have just punted
  7.  * so far as prototypes for pointers to functions.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <types.h>
  12. #include <task.h>
  13. #include <q.h>
  14. #include <netq.h>
  15. #include <net.h>
  16. #include <custom.h>
  17. #include <netbuf.h>
  18. #include <icmp.h>
  19. #include <ip.h>
  20. #include <timer.h>
  21.  
  22. #define MAGIC 123
  23.  
  24. int             ngetc(void);
  25. int             ngetche(void);
  26. void            clearscreen(void);
  27. int             tcpwrite(int, char *, int);
  28. void            usleep(void);
  29. void            xabort(void);
  30. void            reversevideo(char *);
  31. void            normalvideo(void);
  32. int             tcpread(int, char *, int);
  33. int             tcpopen(char *, int);
  34. void            tcp_open(in_name *, int, int, unsigned, unsigned);
  35. int             twrite(char *, int);
  36. int             kbhit(void);
  37. void            exit(int);
  38. int             getche(void);
  39. int             write(int, char *, unsigned);
  40.  
  41. static task    *mtask;
  42.  
  43. static int      state;
  44. static int      first = 1;
  45.  
  46. void            us_open(), us_cls(), no_op();
  47. int             us_tmo(), us_yld();
  48. int             us_data();
  49.  
  50. void            tcpclose(int);
  51. in_name         resolve_name(char *);
  52. void            tcp_init();
  53.  
  54. int
  55. tcpopen(char *host, int port)
  56. {
  57. in_name         fhost;
  58.  
  59.     state = 0;
  60.     tcp_init(512, us_open, us_data, us_yld, us_cls, us_tmo, no_op, no_op);
  61.  
  62.     if ((fhost = resolve_name(host)) == 0L) {
  63.     fprintf(stderr, "Host %s unknown.\n", host);
  64.     return (-1);
  65.     }
  66.     if (fhost == 1L) {
  67.     fprintf(stderr, "Name servers not responding.\n");
  68.     return (-1);
  69.     }
  70.     mtask = tk_cur;
  71.     tcp_open(&fhost, port, 0, custom.c_telwin, custom.c_tellowwin);
  72.     while (!state)
  73.     tk_block();
  74.     if (first) {
  75.     first = 0;
  76.     onexit(tcpclose);
  77.     }
  78.     return (MAGIC);
  79. }
  80.  
  81. void
  82. tcpclose(int fd)
  83. {
  84.     if (state) {
  85.     tcp_close();
  86.     tcp_reset();
  87.     tk_yield();
  88.     }
  89. }
  90.  
  91. int
  92. tcpwrite(int fd, char *p, int cnt)
  93. {
  94.  
  95.     return (twrite(p, cnt));
  96. }
  97.  
  98. #define BSZ (8*1024)
  99. static char far buf[BSZ];
  100. char far       *head = buf;
  101. char far       *tail = buf;
  102. int             bufcnt;
  103.  
  104.  
  105. tcpread(int fd, char *p, int cnt)
  106. {
  107. register int    c;
  108.  
  109.     while (!bufcnt) {
  110.     if (!state)
  111.         return (-1);
  112.     tk_block();
  113.     }
  114.     if (cnt > bufcnt)
  115.     cnt = bufcnt;
  116.     c = cnt;
  117.     bufcnt -= c;
  118.     while (c--) {
  119.     *p++ = *tail++;
  120.     if (tail >= &buf[BSZ])
  121.         tail = buf;
  122.     }
  123.     return (cnt);
  124. }
  125.  
  126. us_data(p, cnt, urg)
  127.     register char  *p;
  128.     register int    cnt, urg;
  129. {
  130.     while (cnt--) {
  131.     while (bufcnt >= BSZ) {
  132.         tk_wake(mtask);
  133.         tk_yield();
  134.     }
  135.     *head++ = *p++;
  136.     if (head >= &buf[BSZ])
  137.         head = buf;
  138.     bufcnt++;
  139.     }
  140.     tk_wake(mtask);
  141. }
  142.  
  143. us_tmo()
  144. {
  145.     fprintf(stderr, "Host not responding\n");
  146.     tcp_close();
  147.     tcp_reset();
  148.     tk_yield();
  149.     tk_wake(mtask);
  150. }
  151.  
  152. void
  153. no_op()
  154. {
  155. }
  156.  
  157. void
  158. us_open()
  159. {
  160.     state = 1;
  161.     tk_wake(mtask);
  162. }
  163.  
  164. void
  165. us_cls()
  166. {
  167.  
  168.     state = 0;
  169.  
  170.     fflush(stdin);
  171.     printf("Closed: hit any key to quit ");
  172.     while (!kbhit());
  173.     fflush(stdin);
  174.     exit(3);
  175. }
  176.  
  177. int
  178. us_yld()
  179. {
  180.     return (1);
  181. }
  182. int
  183. ngetche(void)
  184. {
  185. int             i;
  186.     while (!kbhit())
  187.     tk_yield();
  188.  
  189.     i = getche();
  190.     if (!i) {
  191.     getche();
  192.     return 0;
  193.     }
  194.     return i;
  195.  
  196.  
  197. }
  198.  
  199. int
  200. ngetc(void)
  201. {
  202. register int    c;
  203.  
  204.     c = ngetche();
  205.     write(1, "\n", 1);
  206.     return (c);
  207. }
  208.  
  209. #include <dos.h>
  210. int             int86(int, union REGS *, union REGS *);
  211.  
  212.  
  213. /*
  214.  * The follwing routines all use BIOS calls (except xabort, which is the DOS
  215.  * terminate program call). Int 16 is video, 0x1a is the clock. They are of
  216.  * course rather arcane. If you don't understand them, I suggest the book
  217.  * "DOS Programmer's Reference" by Terry R. Dettmann, Que Corp., Carmel,
  218.  * Indiana. It is what I used to do the programming.
  219.  */
  220.  
  221.  
  222. void
  223. backline(void)
  224. {
  225.  
  226. union REGS      rg;
  227. int             c, r;
  228.  
  229.     rg.h.ah = 3;   /* get cursor position */
  230.     rg.h.bh = 0;
  231.     int86(16, &rg, &rg);
  232.     c = rg.h.dl;
  233.     r = rg.h.dh - 1;
  234.  
  235.     rg.h.ah = 2;   /* set cursor position */
  236.     rg.h.bh = 0;
  237.     rg.h.dl = 0;
  238.     rg.h.dh = r;
  239.     int86(16, &rg, &rg);
  240.     rg.h.ah = 9;   /* write character and attribute */
  241.     rg.h.al = ' ';
  242.     rg.h.bh = 0;
  243.     rg.h.bl = 7;   /* normal attribute */
  244.     rg.x.cx = 160;
  245.     int86(16, &rg, &rg);
  246.  
  247. }
  248.  
  249. unsigned        strlen(char *);
  250.  
  251. void
  252. reversevideo(char *buf)
  253. {
  254.  
  255. union REGS      rg;
  256. int             i, c, r;
  257.  
  258.     rg.h.ah = 3;   /* get cursor position */
  259.     rg.h.bh = 0;
  260.     int86(16, &rg, &rg);
  261.     c = rg.h.dl;
  262.     r = rg.h.dh;
  263.  
  264.     for (i = 0; i < strlen(buf); i++) {
  265.     rg.h.ah = 2;    /* set cursor position */
  266.     rg.h.bh = 0;
  267.     rg.h.dl = i;
  268.     rg.h.dh = r;
  269.     int86(16, &rg, &rg);
  270.     rg.h.ah = 9;    /* write character and attribute */
  271.     rg.h.al = buf[i];
  272.     rg.h.bh = 0;
  273.     rg.h.bl = 16 * 7;    /* reverse video */
  274.     rg.x.cx = 1;
  275.     int86(16, &rg, &rg);
  276.     }
  277. }
  278. void
  279. normalvideo(void)
  280. {
  281.  
  282. union REGS      rg;
  283. int             c, r;
  284.  
  285.     rg.h.ah = 3;   /* get cursor position */
  286.     rg.h.bh = 0;
  287.     int86(16, &rg, &rg);
  288.     c = rg.h.dl;
  289.     r = rg.h.dh;
  290.  
  291.     rg.h.ah = 2;   /* set cursor position */
  292.     rg.h.bh = 0;
  293.     rg.h.dl = 0;
  294.     rg.h.dh = r;
  295.     int86(16, &rg, &rg);
  296.     rg.h.ah = 9;   /* write character and attribute */
  297.     rg.h.al = ' ';
  298.     rg.h.bh = 0;
  299.     rg.h.bl = 7;   /* normal attribute */
  300.     rg.x.cx = 80;  /* number of characters */
  301.     int86(16, &rg, &rg);
  302.  
  303. }
  304.  
  305.  
  306. void
  307. clearscreen(void)
  308. {
  309. union REGS      rg;
  310.  
  311.     rg.h.ah = 2;   /* see above, etc */
  312.     rg.h.bh = 0;
  313.     rg.h.dl = 0;
  314.     rg.h.dh = 0;
  315.     int86(16, &rg, &rg);
  316.  
  317.     rg.h.ah = 9;
  318.     rg.h.al = ' ';
  319.     rg.h.bh = 0;
  320.     rg.h.bl = 16 * 7;
  321.     rg.x.cx = 240;
  322.     int86(16, &rg, &rg);
  323.  
  324.     rg.h.ah = 2;
  325.     rg.h.bh = 0;
  326.     rg.h.dl = 0;
  327.     rg.h.dh = 3;
  328.     int86(16, &rg, &rg);
  329.  
  330.     rg.h.ah = 9;
  331.     rg.h.al = ' ';
  332.     rg.h.bh = 0;
  333.     rg.h.bl = 7;
  334.     rg.x.cx = 22 * 80;
  335.     int86(16, &rg, &rg);
  336.     rg.h.ah = 2;
  337.     rg.h.bh = 0;
  338.     rg.h.dl = 0;
  339.     rg.h.dh = 0;
  340.     int86(16, &rg, &rg);
  341. }
  342.  
  343.  
  344. void
  345. usleep(void)
  346. {
  347. int             i;
  348. union REGS      rg;
  349. union {
  350.     unsigned int    i[2];
  351.     unsigned long   l;
  352. }               u;
  353. unsigned long   t;
  354.  
  355.     rg.h.ah = 0;   /* get current time */
  356.     int86(0x1a, &rg, &rg);
  357.     u.i[1] = rg.x.cx;
  358.     u.i[0] = rg.x.dx;
  359.  
  360.     t = u.l + 9;   /* add 9 ticks = 1/2 second */
  361.  
  362.     for (i = 0; i < 20000; i++) {    /* wait loop */
  363.     rg.h.ah = 0;
  364.     int86(0x1a, &rg, &rg);
  365.     u.i[1] = rg.x.cx;
  366.     u.i[0] = rg.x.dx;
  367.     if (u.l >= t)
  368.         break;
  369.     }
  370.  
  371. }
  372.  
  373.  
  374. void
  375. xabort()
  376. {
  377. union REGS      rg;
  378.  
  379.     /* this avoids onexit processing */
  380.     rg.h.ah = 0x4c;
  381.     rg.h.al = 0;
  382.     int86(0x21, &rg, &rg);
  383.     /* this interrupt never returns */
  384. }
  385.