home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / feel0_89.lha / Feel / AddOns / packet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-13  |  40.5 KB  |  1,402 lines

  1. #ifndef lint
  2. static char *RcsId
  3.     = "$Id: packet.c,v 1.1 1993/01/18 12:57:24 pab Exp $";
  4. #endif
  5.  
  6. /* Packets for YY-protocol
  7.  * This file is part of YY-server of YYonX (1.3 Distribution)
  8.  * $Id: packet.c,v 1.1 1993/01/18 12:57:24 pab Exp $
  9.  */
  10.  
  11. /****************************************************************************
  12. ;;;
  13. ;;;  Copyright (C) 1989,1990,1991 Aoyama Gakuin University
  14. ;;;
  15. ;;;        All Rights Reserved
  16. ;;;
  17. ;;; This software is developed for the YY project of Aoyama Gakuin University.
  18. ;;; Permission to use, copy, modify, and distribute this software
  19. ;;; and its documentation for any purpose and without fee is hereby granted,
  20. ;;; provided that the above copyright notices appear in all copies and that
  21. ;;; both that copyright notice and this permission notice appear in 
  22. ;;; supporting documentation, and that the name of Aoyama Gakuin
  23. ;;; not be used in advertising or publicity pertaining to distribution of
  24. ;;; the software without specific, written prior permission.
  25. ;;;
  26. ;;; This software is made available AS IS, and Aoyama Gakuin makes no
  27. ;;; warranty about the software, its performance or its conformity to
  28. ;;; any specification. 
  29. ;;;
  30. ;;; To make a contact: Send E-mail to ida@csrl.aoyama.ac.jp for overall
  31. ;;; issues. To ask specific questions, send to the individual authors at
  32. ;;; csrl.aoyama.ac.jp. To request a mailing list, send E-mail to 
  33. ;;; yyonx-request@csrl.aoyama.ac.jp.
  34. ;;;
  35. ;;; Authors:
  36. ;;;   Version 1.0 90/02/26 by Keisuke 'Keiko' Tanaka
  37. ;;;                (keisuke@csrl.aoyama.ac.jp)
  38. ;;;   Version 2.0 90/08/27 by Keisuke 'Keiko' Tanaka
  39. ;;;            Page Mode Territory is supported
  40. ;;;   Version 2.1 90/11/05 by Keisuke 'Keiko' Tanaka
  41. ;;;            Copyright Notice is rewritten
  42. ;;;
  43. ****************************************************************************/
  44.  
  45. /****************************************************************************
  46.   $Revision: 1.1 $ Written by Keisuke 'Keiko' Tanaka
  47.   $Date: 1993/01/18 12:57:24 $
  48. ****************************************************************************/
  49.  
  50. #include <stdio.h>
  51. #include <sys/types.h>
  52. #include "yydefs.h"
  53. #include "yypacket.h"
  54.  
  55.  
  56. #ifdef EULISP
  57. #include "feel_malloc.h"
  58. #endif
  59.  
  60. /*
  61.  * Packet consists ..
  62.  */
  63.  
  64. /*
  65.  *
  66.  *    1 2 3 4 5 6 7 8  1 2 3 4 5 6 7 8  1 2 3 4 5 6 7 8  1 2 3 4 5 6 7 8
  67.  *   +----------------+----------------+----------------+----------------+
  68.  * 0 | Command        |      Packet Length                               |
  69.  *   +----------------+----------------+----------------+----------------+
  70.  * 1 | -------------- | -------------- | -------------- | Packet Type    |
  71.  *   +----------------+----------------+----------------+----------------+
  72.  * 2 |    Data ...                                                       |
  73.  *
  74.  */
  75.  
  76. /*
  77.  * Packet Type:
  78.  *  1 2 3 4 5 6 7 8
  79.  *  X X X X X X      - Data Class
  80.  *  0 0 0 0 0 0      - ignore data class
  81.  *  0 0 0 0 0 1      - Command
  82.  *  0 0 0 0 1 0      - Syncronous Packet
  83.  *  0 0 0 1 0 0      - Reply for Command (ACK)
  84.  *  0 0 0 1 1 1      - Reply for Command (NACK)
  85.  *  0 0 1 0 0 0      - Event on Server
  86.  *  1 0 0 0 0 0      - Error on Server
  87.  *              X X  - Type
  88.  *              0 0  - Only this one packet
  89.  *              0 1  - this is a first packet of sequence
  90.  *              1 0  - this is a middle packet of sequence
  91.  *              1 1  - this is a last packet of sequence
  92.  */
  93.  
  94. extern int YYPacketBlockSize;
  95.  
  96. #define YYPACKET_LENGTH(pkt)    ((pkt)->pktLength)
  97.  
  98. #define YYPACKET_READ_PTR(pkt)    ((pkt)->pktRWPtr)
  99. #define YYPACKET_WRITE_PTR(pkt)    ((pkt)->pktRWPtr)
  100. #define YYPACKET_CUR_BLOCK(pkt)    ((pkt)->pktRWBlock)
  101. #define YYPACKET_NEXT_BLOCK(pkt)    ((pkt)->pktRWBlock->pbNextBlock)
  102.  
  103. #define YYPACKET_LENGTH_INBLOCK(pkt) ((pkt)->pktRWBlock->pbLength)
  104. #define YYPACKET_BLOCKSIZE(pkt)    ((pkt)->pktRWBlock->pbBlockSize)
  105. #define YYPACKET_FILLEDSIZE_INBLOCK(pkt)    \
  106.     ((pkt)->pktRWBlock->pbBlockSize - (pkt)->pktRWBlock->pbFreeSize)
  107. #define YYPACKET_FREESIZE_INBLOCK(pkt) ((pkt)->pktRWBlock->pbFreeSize)
  108. #define YYPACKET_UNREADLENGTH_INBLOCK(pkt)    \
  109.     ((pkt)->pktRWBlock->pbUnreadLength)
  110. #define YYPACKET_READLENGTH_INBLOCK(pkt)    \
  111.     ((pkt)->pktRWBlock->pbLength - (pkt)->pktRWBlock->pbUnreadLength)
  112.  
  113. static void alloc_new_packet_block();
  114. static int goto_next_packet_block();
  115.  
  116.  
  117. /***************************************************************************
  118.  * Packet Queue Control
  119.  ***************************************************************************/
  120.  
  121. static YYPKTQUEENT *FreeQueueEntList
  122.     = (YYPKTQUEENT *)NULL;
  123.  
  124. /*
  125.  * YYPKTQUEENT *alloc_packet_queue_ent()
  126.  *
  127.  * $B?7$7$$%-%e!<%(%s%H%j$r3d$jEv$F$k(B
  128.  */
  129. static YYPKTQUEENT *alloc_packet_queue_ent()
  130. {
  131.     register YYPKTQUEENT *qent;
  132.     debug_setfunc("packet", "alloc_packet_queue_ent");
  133.     if (FreeQueueEntList != (YYPKTQUEENT *)NULL) {
  134.     debug_print(5, "Get Entry from Free-List\n");
  135.     qent = FreeQueueEntList;
  136.     FreeQueueEntList = FreeQueueEntList->pqNextPacket;
  137.     } else {
  138.     debug_print(5, "Allocate New Area\n");
  139.     qent = (YYPKTQUEENT *)memALLOC(sizeof(YYPKTQUEENT));
  140.     }
  141.     bzero((char *)qent, sizeof(YYPKTQUEENT));
  142.     debug_endfunc("alloc_packet_queue_ent");
  143.     return qent;
  144. }
  145.  
  146. /*
  147.  * void free_packet_queue_ent(qent)
  148.  *
  149.  * $B%-%e!<%(%s%H%j(B qent $B$r(B Free-List $B$K2C$(!$:FMxMQ$G$-$k$h$&$K$9$k(B
  150.  */
  151. static void free_packet_queue_ent(qent)
  152.     register YYPKTQUEENT *qent;
  153. {
  154.     qent->pqNextPacket = FreeQueueEntList;
  155.     FreeQueueEntList = qent;
  156. }
  157.  
  158. /*
  159.  * YYPKTQUEENT *append_queent_from_packet_queue(top, qent)
  160.  *
  161.  * $B%-%e!<%(%s%H%j(B qent $B$r(B top $B$r@hF,$H$9$k%-%e!<%(%s%H%j%j%9%H$N(B
  162.  * $B:G8eHx$KDI2C$9$k(B
  163.  * top $B$,(B NULL $B$G$"$l$P!$(Bqent $B$,%j%9%H$N@hF,$K$J$k(B
  164.  *
  165.  * Return Value:
  166.  *  $B%-%e!<%j%9%H$N@hF,%"%I%l%9(B
  167.  */
  168. static YYPKTQUEENT *append_queent_from_packet_queue(top, qent)
  169.     YYPKTQUEENT *top;
  170.     register YYPKTQUEENT *qent;
  171. {
  172.     register YYPKTQUEENT *last = top;
  173.     debug_setfunc("packet", "append_queent_from_packet_queue");
  174.     qent->pqNextPacket = (YYPKTQUEENT *)NULL;
  175.     if (last == (YYPKTQUEENT *)NULL) {
  176.     top = qent;
  177.     } else {
  178.     while (last->pqNextPacket != (YYPKTQUEENT *)NULL)
  179.         last = last->pqNextPacket;
  180.     last->pqNextPacket = qent;
  181.     }
  182.     qent->pqPrevPacket = last;
  183.     debug_endfunc("append_queent_from_packet_queue");
  184.     return top;
  185. }
  186.  
  187. /*
  188.  * YYPKTQUEENT *pick_queent_from_packet_queue(top, qent)
  189.  *
  190.  * $B%-%e!<%(%s%H%j(B qent $B$r(B top $B$r@hF,$H$9$k%-%e!<%(%s%H%j%j%9%H$N(B
  191.  * $BCf$+$i<h$j=P$9(B
  192.  *
  193.  * Return Value:
  194.  *  $B%-%e!<%j%9%H$N@hF,%"%I%l%9(B
  195.  */
  196. static YYPKTQUEENT *pick_queent_from_packet_queue(top, qent)
  197.     YYPKTQUEENT *top;
  198.     YYPKTQUEENT *qent;
  199. {
  200.     if (qent->pqPrevPacket != (YYPKTQUEENT *)NULL)
  201.     qent->pqPrevPacket->pqNextPacket = qent->pqNextPacket;
  202.     else
  203.     top = qent->pqNextPacket;
  204.     if (qent->pqNextPacket != (YYPKTQUEENT *)NULL)
  205.     qent->pqNextPacket->pqPrevPacket = qent->pqPrevPacket;
  206.     qent->pqNextPacket = qent->pqPrevPacket = (YYPKTQUEENT *)NULL;
  207.     return top;
  208. }
  209.  
  210. /***************************************************************************
  211.  * Packet Data Area Control
  212.  *
  213.  * $B%Q%1%C%H%G!<%?%(%j%"$O%Q%1%C%H>e$N<B:]$N%G!<%?$r<}G<$9$kNN0h$N(B
  214.  * $B$3$H$G$"$k(B
  215.  ***************************************************************************/
  216.  
  217. struct _yy_packet_data_area_ent_ {
  218.     byte *Ptr;
  219.     struct _yy_packet_data_area_ent_ *Next;
  220. } ;
  221. typedef struct _yy_packet_data_area_ent_ YYPKTDAREAENT;
  222.  
  223. /* FreePktDataAreaList $B$OL$;HMQ$N%Q%1%C%H%G!<%?%(%j%"$N%j%9%H$G$"$k(B
  224.  * UnusedPktDAreaEntList $B$O6u$-%Q%1%C%H%G!<%?%(%j%"$rJ];}$9$k$?$a$N(B
  225.  * $B%G!<%?%(%j%"$N6u$-%j%9%H$G$"$k(B
  226.  */
  227. static YYPKTDAREAENT *FreePktDataAreaList
  228.     = (YYPKTDAREAENT *)NULL;
  229. static YYPKTDAREAENT *UnusedPktDAreaEntList
  230.     = (YYPKTDAREAENT *)NULL;
  231.  
  232. /*
  233.  * byte *alloc_packet_data_area()
  234.  *
  235.  * $B?7$7$$%Q%1%C%H%G!<%?NN0h$r3d$jEv$F$k(B
  236.  */
  237. byte *alloc_packet_data_area()
  238. {
  239.     register byte *ptr;
  240.     debug_setfunc("packet", "alloc_packet_data_area");
  241.     if (FreePktDataAreaList != (YYPKTDAREAENT *)NULL) {
  242.     register YYPKTDAREAENT *dent;
  243.     debug_print(9, "Get %dbytes area from free-list\n", YYPacketBlockSize);
  244.     ptr = FreePktDataAreaList->Ptr;
  245.     /* $B%(%s%H%j$r6u%(%s%H%j$N%j%9%H$X(B */
  246.     dent = FreePktDataAreaList;
  247.     FreePktDataAreaList = FreePktDataAreaList->Next;
  248.     dent->Next = UnusedPktDAreaEntList;
  249.     UnusedPktDAreaEntList = dent;
  250.     } else {
  251.     debug_print(9, "Allocate %dbytes area\n", YYPacketBlockSize);
  252.     ptr = (byte *)memALLOC(YYPacketBlockSize);
  253.     }
  254.     debug_print(9, "ADDRESS for PacketDataArea:0x%x\n", ptr);
  255.     debug_endfunc("alloc_packet_data_area");
  256.     return ptr;
  257. }
  258.  
  259. /*
  260.  * void free_packet_data_area(ptr)
  261.  *
  262.  * $B%Q%1%C%H%G!<%?NN0h(B ptr $B$rL$;HMQ%G!<%?NN0h$N%j%9%H$K2C$($k(B
  263.  */
  264. void free_packet_data_area(ptr)
  265.     register byte *ptr;
  266. {
  267.     register YYPKTDAREAENT *dent;
  268.     debug_setfunc("packet", "free_packet_data_area");
  269.     debug_print(9, "ADDRESS for PacketDataArea:0x%x\n", ptr);
  270.     if (UnusedPktDAreaEntList != (YYPKTDAREAENT *)NULL) {
  271.     dent = UnusedPktDAreaEntList;
  272.     UnusedPktDAreaEntList = UnusedPktDAreaEntList->Next;
  273.     } else {
  274.     debug_print(9, "Allocate %dbytes area\n", sizeof(YYPKTDAREAENT));
  275.     dent = (YYPKTDAREAENT *)memALLOC(sizeof(YYPKTDAREAENT));
  276.     }
  277.     dent->Ptr = ptr;
  278.     dent->Next = FreePktDataAreaList;
  279.     /* right ??? */
  280.     FreePktDataAreaList = dent;
  281.     debug_endfunc("free_packet_data_area");
  282.     /*NoReturnValue*/
  283. }
  284.  
  285.  
  286.  
  287.  
  288.  
  289. /***************************************************************************
  290.  *
  291.  ***************************************************************************/
  292.  
  293.  
  294.  
  295. /*
  296.  * int fix_yy_packet(pkt)
  297.  *
  298.  * $B%Q%1%C%H%5%$%:!$3F%V%m%C%/$NM-8zD9$r(B fix
  299.  * $B%X%C%@>pJs$r<}G<(B
  300.  */
  301.  
  302. fix_yy_packet(pkt)
  303.     register yy_packet *pkt;
  304. {
  305.     register yy_packet_block *pb = &pkt->pktFirstBlock;
  306.     register yy_packet_header *hp;
  307.     register bool num = 0;
  308.  
  309.     debug_setfunc("packet", "fix_yy_packet");
  310.     debug_print(1, "Command: %d\n", pkt->pktCommand);
  311.     pkt->pktLength = 0;
  312.     for (pb = &pkt->pktFirstBlock;
  313.      pb != (yy_packet_block *)NULL; pb = pb->pbNextBlock, num++) {
  314.     int leng;
  315.     /* Fix Length of Block */
  316.     leng = pb->pbLength = pb->pbBlockSize - pb->pbFreeSize;
  317.     pkt->pktLength += leng;
  318.     /* Fix Header */
  319.     hp = (yy_packet_header *)pb->pbBuf;
  320.     hp->hdCommand = pkt->pktCommand;
  321.     leng >>= 2;
  322.     bcopy(((byte *)&leng)+1, hp->hdPacketLength, 3);
  323.     hp->hdPacketType = pkt->pktType;
  324.     hp->hdBlockType = (num > 0? YYPACKET_BLOCKTYPE_CONTINUE:
  325.                YYPACKET_BLOCKTYPE_FIRSTBLOCK);
  326.     }
  327.     hp->hdBlockType = (num > 1? YYPACKET_BLOCKTYPE_LASTBLOCK:
  328.                YYPACKET_BLOCKTYPE_ONLYONE);
  329.     YYPACKET_CUR_BLOCK(pkt) = &pkt->pktFirstBlock;
  330.     debug_endfunc("fix_yy_packet");
  331. }
  332.  
  333. /*
  334.  * yy_packet *alloc_new_yy_packet(command, type, priority, block, leng)
  335.  *
  336.  * $B?7$7$$%Q%1%C%HNN0h$r3NJ]$9$k(B
  337.  * command, type, priority $B$O$=$NDL$j$N$b$N$,@_Dj$5$l$k(B
  338.  * block $B$,(B NULL $B$G$J$1$l$P!$$=$l$,:G=i$N%V%m%C%/$NFbMF$H$7$F:NMQ$5$l$k(B
  339.  * block $B$,(B NULL $B$J$i$P(B YYPacketBlockSize $B$NNN0h$,3NJ]$5$l$k(B
  340.  */
  341. yy_packet *alloc_new_yy_packet(command, type, priority, block, leng)
  342.     int command;
  343.     int type;
  344.     int priority;
  345.     byte *block; int leng;
  346. {
  347.     register yy_packet *pkt = (yy_packet *)memALLOC(sizeof(yy_packet));
  348.     debug_setfunc("packet", "alloc_new_yy_packet");
  349.  
  350.     debug_print(1, "Command: %d(%x)\n", command, command);
  351.     if (pkt == (yy_packet *)NULL)
  352.     return (yy_packet *)NULL;
  353.     pkt->pktCommand = (command & 0377);
  354.     pkt->pktType = type;
  355.     pkt->pktPriority = priority;
  356.     pkt->pktLength = 0;
  357.     pkt->pktReadFlag = FALSE;
  358.     pkt->pktRWBlock = &pkt->pktFirstBlock;
  359.     if (block != (byte *)NULL) {
  360.     pkt->pktFirstBlock.pbBuf = block;
  361.     pkt->pktFirstBlock.pbBlockSize = leng;
  362.     pkt->pktFirstBlock.pbFreeSize = sizeof(yy_packet_header);
  363.     pkt->pktFirstBlock.pbLength = leng;
  364.     } else {
  365.     pkt->pktFirstBlock.pbBuf = alloc_packet_data_area();
  366.     pkt->pktFirstBlock.pbBlockSize = YYPacketBlockSize;
  367.     pkt->pktFirstBlock.pbFreeSize = YYPacketBlockSize;
  368.     pkt->pktFirstBlock.pbLength = 0;
  369.     }
  370.     pkt->pktFirstBlock.pbUnreadLength = 0;
  371.     pkt->pktFirstBlock.pbNextBlock = (yy_packet_block *)NULL;
  372.     pkt->pktFirstBlock.pbPrevBlock = (yy_packet_block *)NULL;
  373.     /* Skip Header Area */
  374.     pkt->pktRWPtr = pkt->pktFirstBlock.pbBuf + sizeof(yy_packet_header);
  375.     pkt->pktFirstBlock.pbFreeSize -= sizeof(yy_packet_header);
  376.     debug_endfunc("alloc_new_yy_packet");
  377.     return pkt;
  378. }
  379.  
  380.  
  381.  
  382. void remove_yy_packet(pkt)
  383.     yy_packet *pkt;
  384. {
  385.     register yy_packet_block *pb = &pkt->pktFirstBlock;
  386.     register yy_packet_block *next;
  387.     debug_setfunc("packet", "remove_yy_packet");
  388.     free_packet_data_area(pb->pbBuf);
  389.     for (pb = pb->pbNextBlock; pb != (yy_packet_block *)NULL; pb = next) {
  390.     next = pb->pbNextBlock;
  391.     free_packet_data_area(pb->pbBuf);
  392.     free((char *)pb);
  393.     }
  394.     (void)xfree((char *)pkt);
  395.     debug_endfunc("remove_yy_packet");
  396. }
  397.  
  398. /*
  399.  * append_packet_entry_integer(pkt, value)
  400.  *
  401.  * pkt $B$G<($5$l$k%Q%1%C%H$K@0?tCM(B value $B$rDI2C$9$k(B
  402.  * $BDI2C0LCV$r<($9%]%$%s%?$OJQ99$5$l$k(B
  403.  * $BDI2C0LCV$,(B4$B$N4p=`0LCV$G$J$$>l9g$O$9$9$a$k(B
  404.  */
  405. void append_packet_entry_integer(pkt, value)
  406.     yy_packet *pkt;
  407.     int value;
  408. {
  409. #ifdef FOURBYTESINT
  410.     register int l;
  411.     debug_setfunc("packet", "append_packet_entry_interger");
  412.     debug_print(1, "Block Length = %d/%d\n",
  413.         YYPACKET_FILLEDSIZE_INBLOCK(pkt), YYPACKET_BLOCKSIZE(pkt));
  414.     if ((l = (YYPACKET_FILLEDSIZE_INBLOCK(pkt)&03)) > 0) {
  415.     l = 4 - l;
  416.     YYPACKET_FREESIZE_INBLOCK(pkt) -= l;
  417.     while (l-- > 0)
  418.         *YYPACKET_WRITE_PTR(pkt)++ = (byte)EOS;
  419.     }
  420.     if (YYPACKET_FREESIZE_INBLOCK(pkt) <= 0)
  421.     alloc_new_packet_block(pkt, (byte *)NULL);
  422.     (void) bcopy((byte *)&value, YYPACKET_WRITE_PTR(pkt), 4);
  423.     YYPACKET_WRITE_PTR(pkt) += 4;
  424.     YYPACKET_FREESIZE_INBLOCK(pkt) -= 4;
  425.     debug_endfunc("append_packet_entry_interger");
  426. #else
  427.     NOT YET;
  428. #endif
  429. }
  430.  
  431. /*
  432.  * append_packet_entry_color(pkt, value)
  433.  *
  434.  * pkt $B$G<($5$l$k%Q%1%C%H$K(B X $B$N%+%i!<CM(B value $B$rDI2C$9$k(B
  435.  * $BDI2C0LCV$r<($9%]%$%s%?$OJQ99$5$l$k(B
  436.  * $BDI2C0LCV$,(B4$B$N4p=`0LCV$G$J$$>l9g$O$9$9$a$k(B
  437.  *
  438.  * $B8=CJ3,$G$O(B integer $B$H$J$C$F$$$k!%(B
  439.  *
  440.  */
  441. void append_packet_entry_color(pkt, color)
  442.     yy_packet *pkt;
  443.     x_color color;
  444. {
  445. #ifdef FOURBYTESINT
  446.     register int l;
  447.     debug_setfunc("packet", "append_packet_entry_color");
  448.     append_packet_entry_integer(pkt, color);
  449.     debug_endfunc("append_packet_entry_color");
  450. #else
  451.     NOT YET;
  452. #endif
  453. }
  454.  
  455. /*
  456.  * append_packet_entry_bytes(pkt, value, count)
  457.  *
  458.  * pkt $B$G<($5$l$k%Q%1%C%H$K@0?tCM(B value $B$N2<0L(B count $B%P%$%H$rDI2C$9$k(B
  459.  * $BDI2C0LCV$r<($9%]%$%s%?$OJQ99$5$l$k(B
  460.  * $BDI2C0LCV$NJd@5$O9T$J$o$J$$(B
  461.  * count $B$N@5Ev@-$O%A%'%C%/$7$J$$(B
  462.  */
  463. void append_packet_entry_bytes(pkt, value, count)
  464.     yy_packet *pkt;
  465.     int value;
  466.     int count;
  467. {
  468. #ifdef FOURBYTESINT
  469.     byte *vp = (byte *)&value;
  470.     vp += (4-count);
  471.     while (count-- > 0) {
  472.     if (YYPACKET_FREESIZE_INBLOCK(pkt) <= 0)
  473.         alloc_new_packet_block(pkt, (byte *)NULL);
  474.     *YYPACKET_WRITE_PTR(pkt)++ = *vp++;
  475.     YYPACKET_FREESIZE_INBLOCK(pkt)--;
  476.     }
  477. #else
  478.     NOT YET;
  479. #endif
  480. }
  481.  
  482. /*
  483.  * append_packet_entry_onebyte(pkt, value)
  484.  *
  485.  * pkt $B$G<($5$l$k%Q%1%C%H$K@0?tCM(B value $B$rDI2C$9$k(B
  486.  * $BDI2C0LCV$r<($9%]%$%s%?$OJQ99$5$l$k(B
  487.  * $BDI2C0LCV$NJd@5$O9T$J$o$J$$(B
  488.  */
  489. void append_packet_entry_onebyte(pkt, value)
  490.     yy_packet *pkt;
  491.     byte value;
  492. {
  493. #ifdef FOURBYTESINT
  494.     if (YYPACKET_FREESIZE_INBLOCK(pkt) <= 0)
  495.     alloc_new_packet_block(pkt, (byte *)NULL);
  496.     *YYPACKET_WRITE_PTR(pkt)++ = value;
  497.     YYPACKET_FREESIZE_INBLOCK(pkt)--;
  498. #else
  499.     NOT YET;
  500. #endif
  501. }
  502.  
  503. /*
  504.  * append_packet_entry_string(pkt, size, string)
  505.  *
  506.  * pkt $B$G<($5$l$k%Q%1%C%H$KJ8;zNs(B str $B$rDI2C$9$k(B
  507.  * str $BCf$NM-8zJ8;zNs?t$O(B size $B$,J];}$7$F$$$k(B
  508.  * $BDI2C0LCV$r<($9%]%$%s%?$OJQ99$5$l$k(B
  509.  * $BDI2C0LCV$,(B4$B$N4p=`0LCV$G$J$$>l9g$O$9$9$a$k(B
  510.  *    (append_packet_entry_integer $B$K$h$k(B)
  511.  */
  512. void append_packet_entry_string_with_length(pkt, size, str)
  513.     yy_packet *pkt;
  514.     int size;
  515.     byte *str;
  516. {
  517. #ifdef FOURBYTESINT
  518.     append_packet_entry_integer(pkt, size);
  519.     while (size > 3) {
  520.     size -= 4;
  521.     if (YYPACKET_FREESIZE_INBLOCK(pkt) <= 0)
  522.         alloc_new_packet_block(pkt, (byte *)NULL);
  523.     bcopy(str, YYPACKET_WRITE_PTR(pkt), 4);
  524.     str += 4;
  525.     YYPACKET_WRITE_PTR(pkt) += 4;
  526.     YYPACKET_FREESIZE_INBLOCK(pkt) -= 4;
  527.     }
  528.     if (size > 0) {
  529.     int rest = 4 - size;
  530.     if (YYPACKET_FREESIZE_INBLOCK(pkt) <= 0)
  531.         alloc_new_packet_block(pkt, (byte *)NULL);
  532.     while (size-- > 0)
  533.         *YYPACKET_WRITE_PTR(pkt)++ = *str++;
  534.     while (rest-- > 0)
  535.         *YYPACKET_WRITE_PTR(pkt)++ = (byte)EOS;
  536.     YYPACKET_FREESIZE_INBLOCK(pkt) -= 4;
  537.     }
  538. #else
  539.     NOT YET;
  540. #endif
  541. }
  542.  
  543. /*
  544.  * void alloc_new_packet_block(pkt, block, leng)
  545.  *
  546.  * pkt $B$N8=:_$N%5%$%:$r(B fix $B$7$F(B
  547.  * pkt $B$K?7$?$J%V%m%C%/$rDI2C$9$k(B
  548.  * block $B$,(B NULL $B$G$J$1$l$P%V%m%C%/FbMF$H$7$F;H$&(B
  549.  * block $B$,(B NULL $B$J$i?7$?$KNN0h$r3NJ](B
  550.  */
  551. static void alloc_new_packet_block(pkt, block, leng)
  552.     yy_packet *pkt;
  553.     byte *block;
  554.     int leng;
  555. {
  556.     register yy_packet_block *last;
  557.     register yy_packet_block *new;
  558.     debug_setfunc("packet", "alloc_new_packet_block");
  559.     new = (yy_packet_block *)memALLOC(sizeof(yy_packet_block));
  560.     for (last = &pkt->pktFirstBlock;
  561.      last->pbNextBlock != (yy_packet_block *)NULL;
  562.      last = last->pbNextBlock)
  563.     ;
  564.     debug_print(5, "Command on Last Block %d(0x%x) -- %x\n",
  565.         last->pbBuf, last->pbBuf, *((int *)last->pbBuf));
  566.     last->pbNextBlock = new;
  567.     new->pbPrevBlock = last;
  568.     new->pbNextBlock = (yy_packet_block *)NULL;
  569.     if (block != (byte *)NULL) {
  570.     new->pbLength = leng;
  571.     new->pbBuf = block;
  572.     new->pbBlockSize = leng;
  573.     new->pbFreeSize = 0;
  574.     } else {
  575.     new->pbLength = 0;
  576.     new->pbBuf = alloc_packet_data_area();
  577.     new->pbBlockSize = YYPacketBlockSize;
  578.     new->pbFreeSize = new->pbBlockSize - sizeof(yy_packet_header);
  579.     }
  580.     /* fix ptr - Skip Header */
  581.     YYPACKET_CUR_BLOCK(pkt) = new;
  582.     YYPACKET_WRITE_PTR(pkt) = new->pbBuf + sizeof(yy_packet_header);
  583.     /* Skip Header */
  584.     debug_endfunc("alloc_new_packet_block");
  585. }
  586.  
  587. /*********************************************************************
  588.  * Read Packet Entry..
  589.  *
  590.  * read_packet_entry_ $B%7%j!<%:$O$9$Y$FFI$_$@$70LCV$N0\F0$r9T$J$&(B
  591.  *********************************************************************/
  592.  
  593. /*
  594.  * reset_packet_read_ptr(pkt)
  595.  */
  596. void reset_packet_read_ptr(pkt)
  597.     yy_packet *pkt;
  598. {
  599.     yy_packet_block *pb;
  600.     yy_packet_header *hp;
  601.     int leng;
  602.     int num;
  603.     debug_setfunc("packet", "reset_packet_read_ptr");
  604.     pb = &pkt->pktFirstBlock;
  605.     pkt->pktRWBlock = pb;
  606.     pkt->pktRWPtr = pb->pbBuf + sizeof(yy_packet_header);
  607.     pb->pbUnreadLength = pb->pbLength - sizeof(yy_packet_header);
  608.     /* Get Header Info. */
  609.     hp = (yy_packet_header *)(pb->pbBuf);
  610.     /*pkt->pktCommand = (int)(hp->hdCommand);*/
  611.     debug_print(1, "Command:%d(0x%x)\n", pkt->pktCommand, pkt->pktCommand);
  612.     pkt->pktType = hp->hdPacketType;
  613.     pkt->pktLength = 0;
  614.     for (num = 0; pb != (yy_packet_block *)NULL; pb = pb->pbNextBlock, num++) {
  615.     pkt->pktLength += pb->pbLength;
  616.     debug_print(1, "PKT#%d <Length=%d>\n", num, pb->pbLength);
  617.     }
  618.     debug_print(1, "Packet Length = %d\n", pkt->pktLength);
  619.     debug_endfunc("reset_packet_read_ptr");
  620. }
  621.  
  622. /*
  623.  * int read_packet_entry_integer(pkt)
  624.  *
  625.  * pkt $B$+$i@0?tCM$r0l$DFI$_9~$`(B
  626.  * $BFI$_9~$`0LCV$,(B4$B$N4p=`0LCV$G$J$$>l9g$O$9$9$a$k(B
  627.  */
  628.  
  629. int read_packet_entry_integer(pkt)
  630.     yy_packet *pkt;
  631. {
  632. #ifdef FOURBYTESINT
  633.     int val, l;
  634.     if ((l = (YYPACKET_READLENGTH_INBLOCK(pkt)&3)) > 0) {
  635.     l = 4 - l;
  636.     YYPACKET_READ_PTR(pkt) += l;
  637.     YYPACKET_UNREADLENGTH_INBLOCK(pkt) -= l;
  638.     }
  639.     if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) <= 0)
  640.     if (goto_next_packet_block(pkt) < 0)
  641.         return -1;
  642.     (void) bcopy(YYPACKET_READ_PTR(pkt), (byte *)&val, 4);
  643.     YYPACKET_READ_PTR(pkt) += 4;
  644.     YYPACKET_UNREADLENGTH_INBLOCK(pkt) -= 4;
  645.     return val;
  646. #else
  647.     NOT YET;
  648. #endif
  649. }
  650.  
  651. /*
  652.  * u_long read_packet_entry_color(pkt)
  653.  *
  654.  * pkt $B$+$i(B X $B$N%+%i!<CM$r0l$DFI$_9~$`(B
  655.  * $BFI$_9~$`0LCV$,(B4$B$N4p=`0LCV$G$J$$>l9g$O$9$9$a$k(B
  656.  *
  657.  */
  658.  
  659. #ifdef COLORNAME
  660. char *read_packet_entry_color(pkt)
  661. #else
  662. x_color read_packet_entry_color(pkt)
  663. #endif
  664.     yy_packet *pkt;
  665. {
  666. #ifdef FOURBYTESINT
  667. #ifdef COLORNAME
  668.     int l;
  669.     static char name[256];
  670.     l = read_packet_entry_integer(pkt);
  671.     read_packet_entry_string(pkt, l, name);
  672.     return name;
  673. #else /* !COLORNAME */
  674.     x_color color;
  675.     color = read_packet_entry_integer(pkt);
  676.     return color;
  677. #endif
  678. #else
  679.     NOT YET;
  680. #endif
  681. }
  682.  
  683. /*
  684.  * int read_packet_entry_bytes(pkt, count)
  685.  *
  686.  * pkt $B$G<($5$l$k%Q%1%C%H$+$i(B count $B%P%$%H$r<h$j=P$9(B
  687.  * $BFI$_$@$73+;O0LCV$NJd@5$O9T$J$o$J$$(B
  688.  * count $B$N@5Ev@-$O%A%'%C%/$7$J$$(B
  689.  */
  690.  
  691. int read_packet_entry_bytes(pkt, count)
  692.     yy_packet *pkt;
  693.     int count;
  694. {
  695. #ifdef FOURBYTESINT
  696.     int val = 0;
  697.     register byte *vp = (byte *)&val;
  698.     vp += (4-count);
  699.     while (count-- > 0) {
  700.         if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) <= 0)
  701.             if (goto_next_packet_block(pkt) < 0)
  702.                 return -1;
  703.         *vp++ = *YYPACKET_READ_PTR(pkt)++;
  704.         YYPACKET_UNREADLENGTH_INBLOCK(pkt)--;
  705.     }
  706.     return val;
  707. #else
  708.     NOT YET;
  709. #endif
  710. }
  711.  
  712. /*
  713.  * int read_packet_entry_onebyte(pkt)
  714.  *
  715.  * pkt $B$G<($5$l$k%Q%1%C%H$+$i(B 1$B%P%$%H$r<h$j=P$9(B
  716.  * $BFI$_$@$73+;O0LCV$NJd@5$O9T$J$o$J$$(B
  717.  */
  718.  
  719. int read_packet_entry_onebyte(pkt)
  720.     yy_packet *pkt;
  721. {
  722. #ifdef FOURBYTESINT
  723.     register int val;
  724.     if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) <= 0)
  725.         if (goto_next_packet_block(pkt) < 0)
  726.             return -1;
  727.     val = *YYPACKET_READ_PTR(pkt)++;
  728.     YYPACKET_UNREADLENGTH_INBLOCK(pkt)--;
  729.     return val;
  730. #else
  731.     NOT YET;
  732. #endif
  733. }
  734.  
  735. /*
  736.  * int read_packet_entry_string(pkt, size, buf)
  737.  *
  738.  * pkt $B$G<($5$l$k%Q%1%C%H$+$i(B size $B%P%$%H$NJ8;zNs$r<h$j=P$9(B
  739.  * buf $B$O(B size+1 $B0J>e$NBg$-$5$r;}$D$H2>Dj$9$k(B
  740.  * $BFI$_$@$73+;O0LCV$NJd@5$O9T$J$&(B
  741.  */
  742.  
  743. int read_packet_entry_string(pkt, size, buf)
  744.     yy_packet *pkt;
  745.     register int size;
  746.     register byte *buf;
  747. {
  748. #ifdef FOURBYTESINT
  749.     int val, l, rest;
  750.     if ((l = (YYPACKET_READLENGTH_INBLOCK(pkt)&3)) > 0) {
  751.         l = 4 - l;
  752.         YYPACKET_READ_PTR(pkt) += l;
  753.         YYPACKET_UNREADLENGTH_INBLOCK(pkt) -= l;
  754.     }
  755.     for (rest = (size & 3), size -= 4; size >= 0; size -= 4) {
  756.         if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) <= 0)
  757.             if (goto_next_packet_block(pkt) < 0)
  758.                 return -1;
  759.         (void) bcopy(YYPACKET_READ_PTR(pkt), buf, 4);
  760.         YYPACKET_READ_PTR(pkt) += 4;
  761.         buf += 4;
  762.         YYPACKET_UNREADLENGTH_INBLOCK(pkt) -= 4;
  763.     }
  764.     if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) <= 0)
  765.         if (goto_next_packet_block(pkt) < 0)
  766.             return -1;
  767.     (void) bcopy(YYPACKET_READ_PTR(pkt), buf, rest);
  768.     YYPACKET_READ_PTR(pkt) += rest;
  769.     buf += rest;
  770.     YYPACKET_UNREADLENGTH_INBLOCK(pkt) -= rest;
  771.     *buf = (byte)EOS;
  772.     return 0;
  773. #else
  774.     NOT YET;
  775. #endif
  776. }
  777.  
  778. /*
  779.  * int goto_next_packet_block(pkt)
  780.  *
  781.  * pkt $B$K$*$$$F<!$N%V%m%C%/$rFI$_=P$9=`Hw$r$9$k(B
  782.  */
  783.  
  784. static int goto_next_packet_block(pkt)
  785.     yy_packet *pkt;
  786. {
  787.     register yy_packet_block *new = YYPACKET_NEXT_BLOCK(pkt);
  788.     debug_setfunc("packet", "goto_next_packet_block");
  789.     if (new == (yy_packet_block *)NULL)
  790.     return -1;
  791.     YYPACKET_CUR_BLOCK(pkt) = new;
  792.     /* Skip Header */
  793.     YYPACKET_READ_PTR(pkt) = new->pbBuf + sizeof(yy_packet_header);
  794.     new->pbUnreadLength = new->pbLength - sizeof(yy_packet_header);
  795.     debug_endfunc("goto_next_packet_block");
  796.     return 0;
  797. }
  798.  
  799. /************************************************************************
  800.  * Queue
  801.  ***********************************************************************/
  802.  
  803. /*
  804.  * yy_packet *get_packet_from_readq(queue)
  805.  *
  806.  * ReadQueue $B$+$i%Q%1%C%H$r0l$D<h$j=P$9(B
  807.  * $B<h$j=P$7$?%Q%1%C%H$O(B queue $B$+$i$O$O$:$l!$FI$_=P$7MQ$K@_Dj$5$l$k(B
  808.  */
  809.  
  810. yy_packet *get_packet_from_readq(queue)
  811.     yy_packet_system_queue *queue;
  812. {
  813.     YYPKTQUEENT *oldq;
  814.     yy_packet *pkt;
  815.     debug_setfunc("packet", "get_packet_from_readq");
  816.     if ((oldq = queue->sysqRead) == (YYPKTQUEENT *)NULL)
  817.     return (yy_packet *)NULL;
  818.     pkt = oldq->pqPacketBody;
  819.     if ((queue->sysqRead = oldq->pqNextPacket) != (YYPKTQUEENT *)NULL)
  820.     queue->sysqRead->pqPrevPacket = (YYPKTQUEENT *)NULL;
  821.     reset_packet_read_ptr(pkt);
  822.     if(debug_on(8))
  823.     dump_yy_packet(pkt);
  824.     free_packet_queue_ent(oldq);
  825.     debug_endfunc("get_packet_from_readq");
  826.     return pkt;
  827. }
  828.  
  829. /*
  830.  * void put_block_on_recvq(queue, block)
  831.  *
  832.  * block $B$r(B recv_queue $B$KEPO?$9$k(B
  833.  * $B$=$NEPO?$K$h$j%Q%1%C%H$,$=$m$C$?$i<+F0E*$K(B
  834.  * recv_queue $B$+$i(B read_queue $B$K$^$o$9(B
  835.  */
  836.  
  837. void put_block_on_recvq(queue, block, leng)
  838.     yy_packet_system_queue *queue;
  839.     byte *block;
  840.     int leng;
  841. {
  842.     YYPKTQUEENT *qent, *last_qent;
  843.     int command;
  844.     yy_packet_header *hp = (yy_packet_header *)block;
  845.     debug_setfunc("packet", "put_block_on_recvq");
  846.     if (block != (byte *)NULL) {
  847.     command = (int)(hp->hdCommand);
  848.     } else
  849.     command = -1;
  850.     debug_print(5, "Command:%d(0x%x), Block Length = %d\n",
  851.         command, command, leng);
  852.  
  853.     /* Search Packet Entry and Register this block */
  854.     for (qent = queue->sysqRecv, last_qent = (YYPKTQUEENT *)NULL;
  855.      qent != (YYPKTQUEENT *)NULL; qent = (last_qent = qent)->pqNextPacket)
  856.     if (qent->pqPacketBody->pktCommand == command)
  857.         break;
  858.     if (qent != (YYPKTQUEENT *)NULL) {
  859.     alloc_new_packet_block(qent->pqPacketBody, block, leng);
  860.     } else {
  861.     qent = alloc_packet_queue_ent();
  862.     qent->pqPacketBody
  863.         = alloc_new_yy_packet(command, 0, 0, block, leng);
  864.     qent->pqNextPacket = (YYPKTQUEENT *)NULL;
  865.     qent->pqPrevPacket = last_qent;
  866.     if (last_qent != (YYPKTQUEENT *)NULL)
  867.         last_qent->pqNextPacket = qent;
  868.     else
  869.         queue->sysqRecv = qent;
  870.     }
  871.  
  872.     /* If this packet has been completed, move it into Read Queue */
  873.     if (hp->hdBlockType == YYPACKET_BLOCKTYPE_ONLYONE
  874.     || hp->hdBlockType == YYPACKET_BLOCKTYPE_LASTBLOCK) {
  875.     debug_print(8, " Move Packet from recvq to readq\n");
  876.     /* remove this packet from RecvQueue */
  877.     if (qent->pqNextPacket != (YYPKTQUEENT *)NULL)
  878.         qent->pqNextPacket->pqPrevPacket = qent->pqPrevPacket;
  879.     if (qent->pqPrevPacket != (YYPKTQUEENT *)NULL)
  880.         qent->pqPrevPacket->pqNextPacket = qent->pqNextPacket;
  881.     else
  882.         queue->sysqRecv = qent->pqNextPacket;
  883.     /* Register it on Readq */
  884.     if (queue->sysqRead == (YYPKTQUEENT *)NULL) {
  885.         queue->sysqRead = qent;
  886.         qent->pqPrevPacket = (YYPKTQUEENT *)NULL;
  887.         qent->pqNextPacket = (YYPKTQUEENT *)NULL;
  888.     } else {
  889.         register YYPKTQUEENT *rq = queue->sysqRead;
  890.         while (rq->pqNextPacket != (YYPKTQUEENT *)NULL)
  891.         rq = rq->pqNextPacket;
  892.         debug_print(8, " Put Packet on tail of readq\n");
  893.         rq->pqNextPacket = qent;
  894.         qent->pqPrevPacket = rq;
  895.         qent->pqNextPacket = (YYPKTQUEENT *)NULL;
  896.     }
  897.     }
  898.     debug_endfunc("put_block_on_recvq");
  899. }
  900.  
  901. int get_block_from_sendq(queue, bpp, lengp)
  902.     yy_packet_system_queue *queue;
  903.     byte **bpp;
  904.     int *lengp;
  905. {
  906.     yy_packet *pkt;
  907.     debug_setfunc("packet", "get_block_from_sendq");
  908.     if (queue->sysqSend == (YYPKTQUEENT *)NULL) {
  909.         debug_print(5, "SendQ has no packet\n");
  910.         debug_endfunc("get_block_from_sendq");
  911.         return 0;
  912.     }
  913.     pkt = queue->sysqSend->pqPacketBody;
  914.     debug_print(3, "Take One Block from Packet\n");
  915.     debug_print(5, "Command Number#%d, Packet Type 0%o\n",
  916.             pkt->pktCommand, pkt->pktType);
  917.     if (YYPACKET_CUR_BLOCK(pkt) == (yy_packet_block *)NULL) {
  918.         /* free area for this packet */
  919.         YYPKTQUEENT *old = queue->sysqSend;
  920.         YYPKTQUEENT *new = queue->sysqSend->pqNextPacket;
  921.         debug_print(3, "Aha! This Packet has no more block..\n");
  922.         remove_yy_packet(pkt);
  923.         free_packet_queue_ent(old);
  924.         if ((queue->sysqSend = new) == (YYPKTQUEENT *)NULL) {
  925.             debug_print(5, "SendQ has no packet\n");
  926.             debug_endfunc("get_block_from_sendq");
  927.             return 0;
  928.         }
  929.         new->pqPrevPacket = (YYPKTQUEENT *)NULL;
  930.         pkt = new->pqPacketBody;
  931.         debug_print(3, "Take One Block from Packet\n");
  932.         debug_print(5, "Command Number#%d, Packet Type 0%o\n",
  933.                 pkt->pktCommand, pkt->pktType);
  934.     }
  935.     *lengp = YYPACKET_LENGTH_INBLOCK(pkt);
  936.     *bpp = YYPACKET_CUR_BLOCK(pkt)->pbBuf;
  937.     YYPACKET_CUR_BLOCK(pkt) = YYPACKET_NEXT_BLOCK(pkt);
  938.     debug_endfunc("get_block_from_sendq");
  939.     return *lengp;
  940. }
  941.  
  942. int get_block_from_eventq(queue, bpp, lengp)
  943.     yy_packet_system_queue *queue;
  944.     byte **bpp;
  945.     int *lengp;
  946. {
  947.     yy_packet *pkt;
  948.     debug_setfunc("packet", "get_block_from_eventq");
  949.     if (queue->sysqEvent == (YYPKTQUEENT *)NULL) {
  950.         debug_print(5, "EventQ has no packet\n");
  951.         debug_endfunc("get_block_from_eventq");
  952.         return 0;
  953.     }
  954.     pkt = queue->sysqEvent->pqPacketBody;
  955.     debug_print(3, "Take One Block from Packet\n");
  956.     debug_print(5, "Command Number#%d, Packet Type 0%o\n",
  957.             pkt->pktCommand, pkt->pktType);
  958.     if (YYPACKET_CUR_BLOCK(pkt) == (yy_packet_block *)NULL) {
  959.         /* free area for this packet */
  960.         YYPKTQUEENT *old = queue->sysqEvent;
  961.         YYPKTQUEENT *new = queue->sysqEvent->pqNextPacket;
  962.         debug_print(3, "Aha! This Packet has no more block..\n");
  963.         remove_yy_packet(pkt);
  964.         free_packet_queue_ent(old);
  965.         if ((queue->sysqEvent = new) == (YYPKTQUEENT *)NULL) {
  966.             debug_print(5, "EventQ has no packet\n");
  967.             debug_endfunc("get_block_from_eventq");
  968.             return 0;
  969.         }
  970.         new->pqPrevPacket = (YYPKTQUEENT *)NULL;
  971.         pkt = new->pqPacketBody;
  972.         debug_print(3, "Take One Block from Packet\n");
  973.         debug_print(5, "Command Number#%d, Packet Type 0%o\n",
  974.                 pkt->pktCommand, pkt->pktType);
  975.     }
  976.     *lengp = YYPACKET_LENGTH_INBLOCK(pkt);
  977.     *bpp = YYPACKET_CUR_BLOCK(pkt)->pbBuf;
  978.     YYPACKET_CUR_BLOCK(pkt) = YYPACKET_NEXT_BLOCK(pkt);
  979.     debug_endfunc("get_block_from_eventq");
  980.     return *lengp;
  981. }
  982.  
  983. void put_packet_on_sendq(queue, pkt)
  984.     yy_packet_system_queue *queue;
  985.     yy_packet *pkt;
  986. {
  987.     register YYPKTQUEENT *cur;
  988.     register YYPKTQUEENT **qp;
  989.  
  990.     debug_setfunc("packet", "put_packet_on_sendq");
  991.     fix_yy_packet(pkt);
  992.     if (debug_on(8))
  993.     dump_yy_packet(pkt);
  994.     qp = ((pkt->pktType == YYPACKETTYPE_EVENT)?
  995.       &queue->sysqEvent: &queue->sysqSend);
  996.     if ((cur = *qp) == (YYPKTQUEENT *)NULL) {
  997.     cur = *qp = alloc_packet_queue_ent();
  998.     cur->pqPacketBody = pkt;
  999.     cur->pqNextPacket = (YYPKTQUEENT *)NULL;
  1000.     cur->pqPrevPacket = (YYPKTQUEENT *)NULL;
  1001.     } else {
  1002.     register YYPKTQUEENT *new = alloc_packet_queue_ent();
  1003.     while (cur->pqNextPacket != (YYPKTQUEENT *)NULL)
  1004.         cur = cur->pqNextPacket;
  1005.     cur->pqNextPacket = new;
  1006.     new->pqPacketBody = pkt;
  1007.     new->pqPrevPacket = cur;
  1008.     new->pqNextPacket = (YYPKTQUEENT *)NULL;
  1009.     }
  1010.     debug_endfunc("put_packet_on_sendq");
  1011. }
  1012.  
  1013. void dump_yy_packet(pkt)
  1014.     yy_packet *pkt;
  1015. {
  1016.     int num = 1;
  1017.     yy_packet_block *pb;
  1018.     debug_setfunc("packet", "dump_yy_packet");
  1019.     debug_print(1, "PacketLength: %d\n", YYPACKET_LENGTH(pkt));
  1020.     pb = &pkt->pktFirstBlock;
  1021.     for ( ; pb != (yy_packet_block *)NULL; pb = pb->pbNextBlock, num++) {
  1022.     byte *vp;
  1023.     int line, size;
  1024.     size = pb->pbBlockSize - pb->pbFreeSize;
  1025.     debug_print(2, "Block #%d - Block Size: %d, Length: %d\n",
  1026.             num, pb->pbBlockSize, size);
  1027.     debug_print(3, "     0  1  2  3  4  5  6  7\n");
  1028.     vp = pb->pbBuf;
  1029.     line = 0;
  1030.     while (size > 0) {
  1031.         byte *p;
  1032.         int col, len;
  1033.         debug_print(3, "%3d", line++);
  1034.         for (p = vp, col = 8, len = size;
  1035.          len > 0 && col > 0; len--, col--, p++)
  1036.         debug_print(3, " %02x", (int)*p);
  1037.         while (col-- > 0)
  1038.         debug_print(3, "   ");
  1039.         debug_print(3, "   ");
  1040.         for (p = vp, col = 8, len = size;
  1041.          len > 0 && col > 0; len--, col--, p++)
  1042.         debug_print(3, "%c",
  1043.                 (*p > ' ' && *p < 0177? *p: ' '));
  1044.         vp = p;
  1045.         size = len;
  1046.         debug_print(3, "\n");
  1047.     }
  1048.     }
  1049.     debug_endfunc("dump_yy_packet");
  1050. }
  1051.  
  1052. #ifdef notdef
  1053.  
  1054. typedef struct _yy_packet_block_ YYPACKETBLOCK;
  1055. struct _yy_packet_block_ {
  1056.     int pbSize;        /* (Max) Size of Data Area */
  1057.     int pbLeng;        /* Length of Active Data */
  1058.     byte *pbBuf;    /* Data Area */
  1059.     struct _yy_packet_block_ *pbNextBlock;
  1060.     struct _yy_packet_block_ *pbPrevBlock;
  1061. } ;
  1062.  
  1063. #define YYPKTOP_NONE        0    /* $B2?$b$7$J$$(B ($B%(%i!<(B) */
  1064. #define YYPKTOP_READ        1    /* $B%Q%1%C%H9=B$$+$i%G!<%?$NFI$_$@$7(B */
  1065. #define YYPKTOP_WRITE        2    /* $B%Q%1%C%H9=B$$X$N%G!<%?$N=q$-9~$_(B */
  1066. #define YYPKTOP_RECV        3    /* $B%Q%1%C%H%G!<%?$N<u?.(B */
  1067. #define YYPKTOP_SEND        4    /* $B%Q%1%C%H%G!<%?$NAw?.(B */
  1068. #define YYPKTOP_ALLOC        5    /* $BNN0h$N3NJ](B */
  1069. #define YYPKTOP_FREE        6    /* $BNN0h$N3+J|(B */
  1070.  
  1071. struct _yy_packet_ {
  1072.     /* Packet Control Information */
  1073.     int pktOPType;            /* Operation Type */
  1074.     int pktPriority;            /* Priority */
  1075.     /* Packet Header Information */
  1076.     int pktYYOpe;            /* YY-Operation Number */
  1077.     int pktType;            /* Type of Packet */
  1078.     int pktLeng;            /* Total Size of this packet */
  1079.     YYPACKETBLOCK pktFirstBlock;    /* First Block of this packet */
  1080.     /* Packet Read/Write Control */
  1081.     YYPACKETBLOCK *pktCurBlock;        /* Current Block */
  1082.     union {
  1083.     byte *rwRead;            /* Read PTR */
  1084.     byte *rwWrite;            /* Write PTR */
  1085.     } pktRWPtrEnt;
  1086.     int pktActiveInBlock;        /* Length of Written Data in Block */
  1087.     int pktFreeInBlock;            /* Length of Room in Block */
  1088.     int pktReadInBlock;            /* Read Data in Block */
  1089.     int pktUnreadInBlock;        /* Unread Data in Block */
  1090. } ;
  1091. #define pktReadPtr    pktRWPtrEnt.rwRead
  1092. #define pktWritePtr    pktRWPtrEnt.rwWrite
  1093.  
  1094.  
  1095. /*
  1096.  * void goto_next_read_packet(pkt)
  1097.  *
  1098.  * YY $B$K$*$1$k%Q%1%C%H(B pkt $B$K$*$$$F!$8=:_$N%V%m%C%/$N(B
  1099.  * $B<!$N%V%m%C%/$rFI$_=P$9$h$&$K@_Dj$9$k(B
  1100.  */
  1101. static void goto_next_read_packet(pkt)
  1102.     register YYPACKET *pkt;
  1103. {
  1104.     register YYPACKETBLOCK *next;
  1105. #ifdef DEBUG_PRINT
  1106.     debug_setfunc("packet", "goto_next_read_packet");
  1107. #endif /*DEBUG_PRINT*/
  1108.     /* fix length of unread data in Packet */
  1109.     pkt->pktUnreadLength -= YYPACKET_CURBLOCK(pkt)->pbUnreadLength;
  1110.     YYPACKET_CURBLOCK(pkt)->pbUnreadLength = 0;
  1111.     next = YYPACKET_CURBLOCK(pkt)->pbNextBlock;
  1112.     if (next != (YYPACKETBLOCK *)NULL) {
  1113.     next->pbReadPtr = next->pbBuf + sizeof(YYPACKETHEADER);
  1114.     next->pbUnreadLength = next->pbLength - sizeof(YYPACKETHEADER);
  1115.     YYPACKET_CURBLOCK(pkt) = next;
  1116.     } else {
  1117.     pkt->pktUnreadLength = 0;
  1118.     }
  1119. #ifdef DEBUG_PRINT
  1120.     debug_endfunc("goto_next_read_packet");
  1121. #endif /*DEBUG_PRINT*/
  1122.     /*NoReturnValue*/
  1123. }
  1124.  
  1125. /*
  1126.  * void skip_packet_read_ptr(pkt, bytes)
  1127.  *
  1128.  * $B%Q%1%C%H(B pkt $B$NFI$_$@$70LCV$r(B bytes $B$@$1$9$9$a$k(B
  1129.  * bytes $B?J$a$k4V$K%V%m%C%/$NNN0h$,=*$o$C$F$7$^$C$?$i(B
  1130.  * $B<!$N%V%m%C%/$K?J$`(B
  1131.  */
  1132. static void skip_packet_read_ptr(pkt, bytes)
  1133.     register YYPACKET *pkt;
  1134.     register int bytes;
  1135. {
  1136. #ifdef DEBUG_PRINT
  1137.     debug_setfunc("packet", "skip_packet_read_ptr");
  1138. #endif /*DEBUG_PRINT*/
  1139.     if (YYPACKET_CURBLOCK(pkt)->pbUnreadLength < bytes) {
  1140.     bytes -= YYPACKET_CURBLOCK(pkt)->pbUnreadLength;
  1141.     goto_next_read_packet(pkt);
  1142.     }
  1143.     if (YYPACKET_CURBLOCK(pkt)->pbUnreadLength >= bytes) {
  1144.     YYPACKET_READ_PTR(pkt) += bytes;
  1145.     pkt->pktUnreadLength -= bytes;
  1146.     YYPACKET_CURBLOCK(pkt)->pbnreadLength -= bytes;
  1147.     } else {
  1148.     pkt->pktUnreadLength = 0;
  1149.     YYPACKET_CURBLOCK(pkt)->pbnreadLength = 0;
  1150.     }
  1151. #ifdef DEBUG_PRINT
  1152.     debug_endfunc("skip_packet_read_ptr");
  1153. #endif /*DEBUG_PRINT*/
  1154.     /*NoReturnValue*/
  1155. }
  1156.  
  1157. /*
  1158.  * void fix_packet_read_ptr(pkt, fix_boundary, room)
  1159.  *
  1160.  * $B%Q%1%C%H(B pkt $B$NFI$_$@$70LCV$r9g$o$;$k(B
  1161.  * fix_boundary $B$,(B TRUE $B$G$"$l$P!$(B4$B%P%$%H$N6-3&$K(B
  1162.  * $B9g$o$;$k$h$&$K$9$k(B
  1163.  * $B8=:_$N%V%m%C%/Cf$K(B room $B%P%$%H$NL$FI$NNN0h$,;D$C$F$$$J$1$l$P(B
  1164.  * $B<!$N%V%m%C%/$rFI$_=P$9$h$&$K@_Dj$9$k(B
  1165.  *   ($B$3$N%V%m%C%/$N;D$jItJ,$O<N$F$i$l$k(B)
  1166.  */
  1167. static void fix_packet_read_ptr(pkt, fix_boundary, room)
  1168.     register YYPACKET *pkt;
  1169.     register bool fix_boundary;
  1170.     register int room;
  1171. {
  1172.     register int bound;
  1173. #ifdef DEBUG_PRINT
  1174.     debug_setfunc("packet", "fix_packet_read_ptr");
  1175. #endif /*DEBUG_PRINT*/
  1176.     if (fix_boundary &&
  1177.     (bound = ((YYPACKET_CURBLOCK(pkt)->pbUnreadLength)&3)) > 0) {
  1178. #ifdef DEBUG_PRINT
  1179.     debug_print(9, "Fix Boundary\n");
  1180. #endif /*DEBUG_PRINT*/
  1181.     (void)skip_packet_read_ptr(pkt, (4 - bound));
  1182.     }
  1183.     if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) < room)
  1184.     goto_next_read_packet(pkt);
  1185. #ifdef DEBUG_PRINT
  1186.     debug_endfunc("fix_packet_read_ptr");
  1187. #endif /*DEBUG_PRINT*/
  1188. }
  1189.  
  1190. /***************************************************************************
  1191.  *****  WritePtr
  1192.  ***************************************************************************/
  1193.  
  1194. /*
  1195.  * void goto_next_write_packet(pkt)
  1196.  *
  1197.  * YY $B$K$*$1$k%Q%1%C%H(B pkt $B$K$*$$$F!$8=:_$N%V%m%C%/$N(B
  1198.  * $B<!$N%V%m%C%/$K=q$-9~$_$,$G$-$k$h$&$K@_Dj$9$k(B
  1199.  */
  1200. static void goto_next_write_packet(pkt)
  1201.     register YYPACKET *pkt;
  1202. {
  1203.     register YYPACKETBLOCK *next;
  1204. #ifdef DEBUG_PRINT
  1205.     debug_setfunc("packet", "goto_next_write_packet");
  1206. #endif /*DEBUG_PRINT*/
  1207.     next = YYPACKET_CURBLOCK(pkt)->pbNextBlock;
  1208.     if (next == (YYPACKETBLOCK *)NULL) {
  1209. #ifdef DEBUG_PRINT
  1210.     debug_print(9, "Allocate New Packet Block\n");
  1211. #endif /*DEBUG_PRINT*/
  1212.     next = alloc_packet_block(pkt, (bytes *)NULL);
  1213.     }
  1214.     next->pbWritePtr = next->pbBuf + sizeof(YYPACKETHEADER);
  1215.     next->pbFreeLength = next->pbSize - sizeof(YYPACKETHEADER);
  1216.     YYPACKET_CURBLOCK(pkt) = next;
  1217. #ifdef DEBUG_PRINT
  1218.     debug_endfunc("goto_next_write_packet");
  1219. #endif /*DEBUG_PRINT*/
  1220.     /*NoReturnValue*/
  1221. }
  1222.  
  1223. /*
  1224.  * void skip_packet_write_ptr(pkt, bytes)
  1225.  *
  1226.  * $B%Q%1%C%H(B pkt $B$N=q$-9~$_0LCV$r(B bytes $B$@$1$9$9$a$k(B
  1227.  * bytes $B?J$a$k4V$K%V%m%C%/$NNN0h$,=*$o$C$F$7$^$C$?$i(B
  1228.  * $B<!$N%V%m%C%/$K?J$`(B
  1229.  */
  1230. static void skip_packet_write_ptr(pkt, bytes)
  1231.     register YYPACKET *pkt;
  1232.     register int bytes;
  1233. {
  1234. #ifdef DEBUG_PRINT
  1235.     debug_setfunc("packet", "skip_packet_write_ptr");
  1236. #endif /*DEBUG_PRINT*/
  1237.     if (YYPACKET_CURBLOCK(pkt)->pbFreeLength < bytes) {
  1238.     bytes -= YYPACKET_CURBLOCK(pkt)->pbFreeLength;
  1239.     goto_next_read_packet(pkt);
  1240.     }
  1241.     if (YYPACKET_CURBLOCK(pkt)->pbFreeLength >= bytes) {
  1242.     YYPACKET_WRITE_PTR(pkt) += bytes;
  1243.     YYPACKET_CURBLOCK(pkt)->pbFreeLength -= bytes;
  1244.     pkt->pktLength += bytes;
  1245.     } else {
  1246.     YYPACKET_CURBLOCK(pkt)->pbFreeLength = 0;
  1247.     }
  1248. #ifdef DEBUG_PRINT
  1249.     debug_endfunc("skip_packet_write_ptr");
  1250. #endif /*DEBUG_PRINT*/
  1251.     /*NoReturnValue*/
  1252. }
  1253.  
  1254.  
  1255. /*
  1256.  * void fix_packet_write_ptr(pkt, fix_boundary, room)
  1257.  *
  1258.  * $B%Q%1%C%H(B pkt $B$N=q$-9~$_0LCV$r9g$o$;$k(B
  1259.  * fix_boundary $B$,(B TRUE $B$G$"$l$P!$(B4$B%P%$%H$N6-3&$K(B
  1260.  * $B9g$o$;$k$h$&$K$9$k(B
  1261.  * $B8=:_$N%V%m%C%/Cf$K(B room $B%P%$%H$N=q$-9~$_NN0h$,;D$C$F$$$J$1$l$P(B
  1262.  * $B<!$N%V%m%C%/$r=q$-9~$`$h$&$K@_Dj$9$k(B
  1263.  *   ($B$3$N%V%m%C%/$N;D$jItJ,$OITDj>uBV$H$J$k(B)
  1264.  */
  1265. static void fix_packet_write_ptr(pkt, fix_boundary, room)
  1266.     register YYPACKET *pkt;
  1267.     register bool fix_boundary;
  1268.     register int room;
  1269. {
  1270.     register int bound;
  1271. #ifdef DEBUG_PRINT
  1272.     debug_setfunc("packet", "fix_packet_write_ptr");
  1273. #endif /*DEBUG_PRINT*/
  1274.     if (fix_boundary &&
  1275.     (bound = ((YYPACKET_CURBLOCK(pkt)->pbLength)&3)) > 0) {
  1276. #ifdef DEBUG_PRINT
  1277.     debug_print(9, "Fix Boundary\n");
  1278. #endif /*DEBUG_PRINT*/
  1279.     (void)skip_packet_read_ptr(pkt, (4 - bound));
  1280.     }
  1281.     if (YYPACKET_UNREADLENGTH_INBLOCK(pkt) < room)
  1282.     goto_next_read_packet(pkt);
  1283. #ifdef DEBUG_PRINT
  1284.     debug_endfunc("fix_packet_write_ptr");
  1285. #endif /*DEBUG_PRINT*/
  1286. }
  1287.  
  1288. /***************************************************************************
  1289.  *****
  1290.  ***************************************************************************/
  1291.  
  1292. bool pkt_entry_integer(pkt, dp)
  1293.     register YYPACKET *pkt;
  1294.     int *dp;
  1295. {
  1296.     register bool status = TRUE;
  1297. #ifdef DEBUG_PRINT
  1298.     debug_setfunc("packet", "pkt_entry_integer");
  1299. #endif /*DEBUG_PRINT*/
  1300.     switch (pkt->pktOperation) {
  1301.     case YYPKT_READ:
  1302.     /* Get One Interger Value from Packet */
  1303.     fix_packet_read_ptr(pkt, YYPKT_FIX_BOUNDARY, 4);
  1304.     if (YYPACKET_UNREAD_LENGTH(pkt) > 3) {
  1305. #ifdef FOURBYTESINT
  1306.         (void)bcopy(YYPACKET_READ_PTR(pkt), dp, 4);
  1307. #else /*!FOURBYTESINT*/
  1308.         if (sizeof(int) < 4) {
  1309.         (void)bcopy(YYPACKET_READ_PTR(pkt), dp, 4);
  1310.         } else {
  1311.         *dp = 0;
  1312.         (void)bcopy(YYPACKET_READ_PTR(pkt), dp+(sizeof(int)-4), 4);
  1313.         }
  1314. #endif /*!FOURBYTESINT*/
  1315.         skip_packet_read_ptr(pkt, 4);
  1316.     } else {
  1317.         *dp = 0; status = FALSE;
  1318.     }
  1319.     break;
  1320.     case YYPKT_WRITE:
  1321.     /* Put One Interger Value on Packet */
  1322.     fix_packet_write_ptr(pkt, YYPKT_FIX_BOUNDARY, 4);
  1323. #ifdef FOURBYTESINT
  1324.     (void)bcopy(dp, YYPACKET_WRITE_PTR(pkt), 4);
  1325. #else /*!FOURBYTESINT*/
  1326.     if (sizeof(int) < 4) {
  1327.         (void)bzero(YYPACKET_WRITE_PTR(pkt), 4);
  1328.         (void)bcopy(dp, YYPACKET_WRITE_PTR(pkt)+(4-sizeof(int)),
  1329.             sizeof(int));
  1330.     } else {
  1331.         (void)bcopy(dp+(sizeof(int)-4), YYPACKET_WRITE_PTR(pkt), 4);
  1332.     }
  1333. #endif /*!FOURBYTESINT*/
  1334.     skip_packet_write_ptr(pkt, 4);
  1335.     break;
  1336.     case YYPKT_ALLOC:
  1337.     case YYPKT_FREE:
  1338.     /*Do Nothing*/
  1339.     break;
  1340.     }
  1341. #ifdef DEBUG_PRINT
  1342.     debug_endfunc("pkt_entry_integer");
  1343. #endif /*DEBUG_PRINT*/
  1344.     return TRUE;
  1345. }
  1346.  
  1347. typedef struct {
  1348.     int Leng;
  1349.     char *Ptr;
  1350. } pktstring;
  1351.  
  1352. bool pkt_entry_string(pkt, dp)
  1353.     register YYPACKET *pkt;
  1354.     pkt_string *dp;
  1355. {
  1356.     register bool status = TRUE;
  1357. #ifdef DEBUG_PRINT
  1358.     debug_setfunc("packet", "pkt_entry_string");
  1359. #endif /*DEBUG_PRINT*/
  1360.     switch (pkt->pktOperation) {
  1361.     case YYPKT_READ:
  1362.     /* Get String from Packet */
  1363.     pkt_entry_integer(pkt, dp->leng);
  1364.     if (dp->leng > 0) {
  1365.         dp->ptr = (char *)memALLOC(dp->leng);
  1366.     } else {
  1367.         int dummy;
  1368.         pkt_entry_integer(pkt, &dummy);
  1369.     }
  1370.     break;
  1371.     case YYPKT_WRITE:
  1372.     /* Put One Interger Value on Packet */
  1373.     fix_packet_write_ptr(pkt, YYPKT_FIX_BOUNDARY, 4);
  1374. #ifdef FOURBYTESINT
  1375.     (void)bcopy(dp, YYPACKET_WRITE_PTR(pkt), 4);
  1376. #else /*!FOURBYTESINT*/
  1377.     if (sizeof(int) < 4) {
  1378.         (void)bzero(YYPACKET_WRITE_PTR(pkt), 4);
  1379.         (void)bcopy(dp, YYPACKET_WRITE_PTR(pkt)+(4-sizeof(int)),
  1380.             sizeof(int));
  1381.     } else {
  1382.         (void)bcopy(dp+(sizeof(int)-4), YYPACKET_WRITE_PTR(pkt), 4);
  1383.     }
  1384. #endif /*!FOURBYTESINT*/
  1385.     skip_packet_write_ptr(pkt, 4);
  1386.     break;
  1387.     case YYPKT_ALLOC:
  1388.     case YYPKT_FREE:
  1389.     /*Do Nothing*/
  1390.     break;
  1391.     }
  1392. #ifdef DEBUG_PRINT
  1393.     debug_endfunc("pkt_entry_integer");
  1394. #endif /*DEBUG_PRINT*/
  1395.     return TRUE;
  1396. }
  1397.  
  1398. #endif
  1399.  
  1400. /* $RCSFile:$
  1401.  */
  1402.