home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d079 / monproc.lha / MonProc / printpkt.c < prev   
Encoding:
C/C++ Source or Header  |  1987-06-04  |  13.9 KB  |  639 lines

  1. /*
  2.  * PrintPkt.C - This doesn't do much, it takes a packet type (number) and
  3.  *     gives you something more readable on stdout.
  4.  *
  5.  *       Phillip Lindsay (c) 1987 Commodore-Amiga Inc. 
  6.  * You may use this source as long as the copyright notice is left intact.
  7.  * 
  8.  * Modified to print details of packet contents
  9.  *    by Davide P. Cervone, 4/25/87
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <libraries/dosextens.h>
  14.  
  15. /*
  16.  *  packet types
  17.  */
  18.  
  19. #define _ACTION_NIL                0L
  20. #define _ACTION_GET_BLOCK          2L
  21. #define _ACTION_SET_MAP            4L
  22. #define _ACTION_DIE                5L
  23. #define _ACTION_EVENT              6L
  24. #define _ACTION_CURRENT_VOLUME     7L
  25. #define _ACTION_LOCATE_OBJECT      8L
  26. #define _ACTION_RENAME_DISK        9L
  27. #define _ACTION_FREE_LOCK         15L
  28. #define _ACTION_DELETE_OBJECT     16L
  29. #define _ACTION_RENAME_OBJECT     17L
  30. #define _ACTION_MORE_CACHE        18L
  31. #define _ACTION_COPY_DIR          19L
  32. #define _ACTION_WAIT_CHAR         20L
  33. #define _ACTION_SET_PROTECT       21L
  34. #define _ACTION_CREATE_DIR        22L
  35. #define _ACTION_EXAMINE_OBJECT    23L
  36. #define _ACTION_EXAMINE_NEXT      24L
  37. #define _ACTION_DISK_INFO         25L
  38. #define _ACTION_INFO              26L
  39. #define _ACTION_FLUSH             27L
  40. #define _ACTION_SET_COMMENT       28L
  41. #define _ACTION_PARENT            29L
  42.  
  43. /*
  44.  *  This is normally a returning timer device request. (internal)
  45.  */
  46. #define _ACTION_TIMER             30L
  47.  
  48. #define _ACTION_INHIBIT           31L
  49. #define _ACTION_DISK_TYPE         32L
  50. #define _ACTION_DISK_CHANGE       33L
  51. #define _ACTION_SET_FILE_DATE     34L
  52. #define _ACTION_READ              82L
  53. #define _ACTION_WRITE             87L
  54. #define _ACTION_SET_SCREEN_MODE  994L
  55.  
  56. /*
  57.  * When a handler internally sends a device i/o request they are sent using
  58.  * their process port and in the form of a "packet" the packet types below:
  59.  * (ACTION_TIMER above also falls into this catagory)
  60.  */
  61. #define _ACTION_READ_INTERNAL   1001L   
  62. #define _ACTION_WRITE_INTERNAL  1002L   
  63.  
  64. #define _ACTION_FIND_INPUT      1005L
  65. #define _ACTION_FIND_OUTPUT     1006L
  66. #define _ACTION_CLOSE           1007L
  67. #define _ACTION_SEEK            1008L
  68.  
  69.  
  70. /*
  71.  *  Short-hand for the parts of the DosPacket
  72.  */
  73. #define ARG1    pkt->dp_Arg1
  74. #define ARG2    pkt->dp_Arg2
  75. #define ARG3    pkt->dp_Arg3
  76. #define ARG4    pkt->dp_Arg4
  77. #define RES1    pkt->dp_Res1
  78.  
  79. #define BCPL(s,p)   ((struct s *)(((ULONG)p)<<2))
  80.  
  81.  
  82.  
  83. int Dump_OK = TRUE;    /* it's OK to perform HEX dumps for long data buffers */
  84. int Lock_OK = TRUE;    /* it's OK to print LOCKs */
  85.  
  86. /*
  87.  *  printall()
  88.  *
  89.  *  Prints all the important parts of a DosPacket when we don't know
  90.  *  what they are.
  91.  */
  92.  
  93. void printall(s,pkt)
  94. char *s;
  95. struct DosPacket *pkt;
  96. {
  97.    printf("%s (%X,%X,%X) Returning: %X\n",s,ARG1,ARG2,ARG3,RES1);
  98. }
  99.  
  100.  
  101. /*
  102.  *  printINT()
  103.  *
  104.  *  Prints a label and an integer in decimal notation.
  105.  */
  106.  
  107. void printINT(s,i)
  108. char *s;
  109. int i;
  110. {
  111.    printf("   %s: %d",s,i);
  112. }
  113.  
  114.  
  115. /*
  116.  *  printBOOL()
  117.  *
  118.  *  Prints return values as OK or FALSE
  119.  */
  120.  
  121. void printBOOL(b)
  122. int b;
  123. {
  124.    printf("   Return: %s\n",(b)? "OK": "FALSE");
  125. }
  126.  
  127.  
  128. /*
  129.  *  printX()
  130.  *
  131.  *  Prints a label and HEX value
  132.  */
  133.  
  134. void printX(s,x)
  135. char *s;
  136. unsigned int x;
  137. {
  138.    printf("   %s: %X",s,x);
  139. }
  140.  
  141.  
  142. /*
  143.  *  printQuoted()
  144.  *
  145.  *  Prints a data buffer as a quoted string.  Special characters are shown
  146.  *  as '\n','\r','\t', '\001', etc.
  147.  */
  148.  
  149. void printQuoted(buf,length)
  150. unsigned char *buf;
  151. int length;
  152. {
  153.    short i;
  154.    unsigned char c, c7;
  155.    char line[200];
  156.    char *s = &line[0];
  157.  
  158.    for (i=0; i<length; i++)
  159.    {
  160.       c = *(buf++); c7 = c & 0x7F;
  161.       if (c7 < ' ' || c7 > 0x7E)
  162.       {
  163.          switch(c)
  164.          {
  165.             case '\b':
  166.                *s++ = '\\';
  167.                *s++ = 'b';
  168.                break;
  169.  
  170.             case '\t':
  171.                *s++ = '\\';
  172.                *s++ = 't';
  173.                break;
  174.  
  175.             case '\r':
  176.                *s++ = '\\';
  177.                *s++ = 'r';
  178.                break;
  179.  
  180.             case '\n':
  181.                *s++ = '\\';
  182.                *s++ = 'n';
  183.                break;
  184.  
  185.             default:
  186.                sprintf(s,"\\%03o",c);
  187.                s += 4;
  188.                break;
  189.          }
  190.       } else {
  191.          if (c == '"' || c == '\\') *s++ = '\\';
  192.          *s++ = c;
  193.       }
  194.    }
  195.    *s = '\0';
  196.    printf("\"%s\"",line);
  197. }
  198.  
  199.  
  200. /*
  201.  *  printDump()
  202.  *
  203.  *  Prints a data buffer in HEX dump format with ASCII equivalents.
  204.  *  Non-printing characters are shown as '.'  Only the first 80 characters 
  205.  *  are displayed.
  206.  */
  207.  
  208. void printDump(buf,length)
  209. unsigned char *buf;
  210. unsigned int length;
  211. {
  212.    short i,j,count;
  213.    int l = (length > 80)? 80: length;
  214.    unsigned char c, c7;
  215.    char line[40];
  216.  
  217.    line[39] = '\0';
  218.    count = 0;
  219.    printf("\n");
  220.    for (i=l/8; i>=0; i--)
  221.    {
  222.       for (j=0; j<39; j++) line[j] = ' ';
  223.       for (j=0; j<8 & count<l; j++,count++)
  224.       {
  225.          c = *(buf++); c7 = c & 0x7F;
  226.          sprintf(&line[j*3],"%02X ",c);
  227.          sprintf(&line[j+26],"%c ",(c7 < ' ' || c7 > 0x7E)? '.': c);
  228.       }
  229.       line[24] = ' ';
  230.       printf("\n         %s",line);
  231.    }
  232.    if (l < length) printf("\n                 [more]\n"); else printf("\n");
  233. }
  234.  
  235.  
  236. /*
  237.  *  printBUF()
  238.  *
  239.  *  Prints a data buffer as a quoted string (if it is small enough)
  240.  *  or as a HEX dump (if it is big).
  241.  */
  242.  
  243. void printBUF(s,buf,length)
  244. char *s;
  245. unsigned char *buf;
  246. unsigned int length;
  247. {
  248.    printf("   %s: ",s);
  249.    if (length <= 50)
  250.       printQuoted(buf,length);
  251.      else
  252.       if (Dump_OK) printDump(buf,length);
  253. }
  254.  
  255.  
  256. /*
  257.  *  printBSTR()
  258.  *
  259.  *  Prints the contents of a BCPL string buffer
  260.  */
  261.  
  262. void printBSTR(s1,s2)
  263. char *s1,*s2; 
  264. {
  265.    printBUF(s1,BADDR(s2)+1,(long)(*((char *)BADDR(s2))));
  266. }
  267.  
  268. /*
  269.  *  printBPTR()
  270.  *
  271.  *  Prints the contents of a buffer pointed to by a BPTR
  272.  */
  273.  
  274. #define printBPTR(s,b,l)    printBUF(s,BADDR(b),l)
  275.  
  276.  
  277. /*
  278.  *  printLOCK()
  279.  *
  280.  *  Prints the name of the file locked by a FileLock.  DON'T ATTEMPT TO
  281.  *  MONITOR LOCKS FROIM FILE HANDLERS!  It will put you into a deadlock 
  282.  *  situation:  the handler is waiting for the returned packet from 
  283.  *  PacketWait, and we are waiting for the handler to process our DupLock 
  284.  *  in GetPathFromLock.  Use MONPROC NOLOCK when you want to monitor file
  285.  *  handlers.
  286.  *  Notice that we expect a BPTR to a Lock.
  287.  */
  288.  
  289. void printLOCK(s,FL)
  290. char *s;
  291. BPTR FL;
  292. {
  293.    struct FileLock *fl = BCPL(FileLock,FL);
  294.    char name[60];
  295.  
  296.    if (Lock_OK)
  297.    {
  298.       name[0] = '\0';
  299.       if (FL) GetPathFromLock(name,FL);
  300.       printBUF(s,name,strlen(name));
  301.    } else {
  302.       if (fl)
  303.       {
  304.          printBSTR("Lock Volume",BCPL(DeviceList,fl->fl_Volume)->dl_Name);
  305.          printINT("Lock Access",fl->fl_Access);
  306.       } else {
  307.          printf("   Volume: \"\"");
  308.       }
  309.    }
  310.  
  311. /*
  312.    printX(s,fl);
  313.    if (fl)
  314.    {
  315.       printINT("Key",fl->fl_Key);
  316.       printINT("Access",fl->fl_Access);
  317.       printX("Task",fl->fl_Task);
  318.       printf("\n");
  319.       printBSTR("Volume",BCPL(DeviceList,fl->fl_Volume)->dl_Name);
  320.    }
  321. */
  322. }
  323.  
  324.  
  325. /*
  326.  *  printFH()
  327.  *
  328.  *  Should print the contents of a FileHandle.  Does anyone have a good idea
  329.  *  about what's useful in a Filehandle?
  330.  *  Note that we expect a BPTR to a FileHandle.
  331.  */
  332.  
  333. void printFH(s,FH)
  334. char *s;
  335. BPTR FH;
  336. {
  337. /*
  338.    struct FileHandle *fh = BCPL(FileHandle,FH);
  339.  
  340.    if (fh)
  341.    {
  342.       printX("FH Port",fh->fh_Port);
  343.       printX("FH Type",fh->fh_Type);
  344.    }
  345. */
  346. }
  347.  
  348.  
  349. /*
  350.  *  printFIB()
  351.  *
  352.  *  Prints the fib_FileName field of a FileInfoBlock.
  353.  *  Note that we expect a BPTR to a FileInfoBlock.
  354.  */
  355.  
  356. void printFIB(FIB)
  357. BPTR FIB;
  358. {
  359.    struct FileInfoBlock *fib = BCPL(FileInfoBlock,FIB);
  360.  
  361.    if (fib)
  362.       printBUF("File Name",&(fib->fib_FileName[1]),fib->fib_FileName[0]);
  363.      else
  364.       printf("   File Name: \"\"");
  365. }
  366.  
  367.  
  368. /*
  369.  *  printfINFO()
  370.  *
  371.  *  Prints the volume name from an InfoData structure.
  372.  *  Note that we expect a BPTR to InfoData.
  373.  */
  374.  
  375. void printINFO(INFO)
  376. BPTR INFO;
  377. {
  378.    struct InfoData *info = BCPL(InfoData,INFO);
  379.    char name[60];
  380.  
  381.    name[0] = '\0';
  382.    if (info) GetInfoVolume(name,info);
  383.    printBUF("Volume",name,strlen(name));
  384. }
  385.  
  386.  
  387. /*
  388.  *  PrintPkt()
  389.  *
  390.  *  Prints the type of packet and the information appropriate to
  391.  *  that packet type (if known).  Packet types and contents are described
  392.  *  in the AmigaDOS Technical Reference Manual, secion 3.8.1.
  393.  */
  394.  
  395. void PrintPkt(pkt)
  396. struct DosPacket *pkt;
  397. {
  398.    switch(pkt->dp_Type)
  399.    {
  400.       case _ACTION_NIL:
  401.          printall("ACTION_NIL",pkt);
  402.          break;
  403.  
  404.       case _ACTION_GET_BLOCK:
  405.          printall("ACTION_GET_BLOCK",pkt);
  406.          break;
  407.  
  408.       case _ACTION_SET_MAP:
  409.          printall("ACTION_SET_MAP",pkt);
  410.          break;
  411.  
  412.       case _ACTION_DIE:
  413.          printall("ACTION_DIE",pkt);
  414.          break;
  415.  
  416.       case _ACTION_EVENT:
  417.          printall("ACTION_EVENT",pkt);
  418.          break;
  419.  
  420.       case _ACTION_CURRENT_VOLUME:
  421.          printall("ACTION_CURRENT_VOLUME",pkt);
  422.          break;
  423.  
  424.       case _ACTION_LOCATE_OBJECT:
  425.          printf("ACTION_LOCATE_OBJECT (Lock)\n");
  426.          printLOCK("Lock",ARG1);
  427.          printBSTR("Name",ARG2);
  428.          printINT("Mode",ARG3);
  429.          printf("\n");
  430.          printLOCK("NewLock",RES1);
  431.          printf("\n");
  432.          break;
  433.  
  434.       case _ACTION_RENAME_DISK:
  435.          printf("ACTION_RENAME_DISK\n");
  436.          printBSTR("New Name",ARG1);
  437.          printBOOL(RES1);
  438.          break;
  439.  
  440.       case _ACTION_FREE_LOCK:
  441.          printf("ACTION_FREE_LOCK\n");
  442.          printLOCK("Lock",ARG1);
  443.          printBOOL(RES1);
  444.          break;
  445.  
  446.       case _ACTION_DELETE_OBJECT:
  447.          printf("ACTION_DELETE_OBJECT\n");
  448.          printLOCK("Dir Lock",ARG1);
  449.          printBSTR("Name",ARG2);
  450.          printf("\n");
  451.          printBOOL(RES1);
  452.          break;
  453.  
  454.       case _ACTION_RENAME_OBJECT:
  455.          printf("ACTION_RENAME_OBJECT\n");
  456.          printLOCK("From Lock",ARG1);
  457.          printBSTR("From Name",ARG2);
  458.          printf("\n");
  459.          printLOCK("To Lock",ARG3);
  460.          printBSTR("To Name",ARG4);
  461.          printf("\n");
  462.          printBOOL(RES1);
  463.          break;
  464.  
  465.       case _ACTION_MORE_CACHE:
  466.          printall("ACTION_MORE_CACHE",pkt);
  467.          break;
  468.  
  469.       case _ACTION_COPY_DIR:
  470.          printf("ACTION_COPY_DIR (DupLock)\n");
  471.          printLOCK("Lock",ARG1);
  472.          printLOCK("NewLock",RES1);
  473.          printf("\n");
  474.          break;
  475.  
  476.       case _ACTION_WAIT_CHAR:
  477.          printf("ACTION_WAIT_CHAR\n");
  478.          printINT("Timeout",ARG1);
  479.          printBOOL(RES1);
  480.          break;
  481.  
  482.       case _ACTION_SET_PROTECT:
  483.          printf("ACTION_SET_PROTECT\n");
  484.          printLOCK("Dir Lock",ARG2);
  485.          printBSTR("Name",ARG3);
  486.          printf("\n");
  487.          printX("Mask",ARG4);
  488.          printBOOL(RES1);
  489.          break;
  490.  
  491.       case _ACTION_CREATE_DIR:
  492.          printf("ACTION_CREATE_DIR\n");
  493.          printLOCK("Dir Lock",ARG1);
  494.          printBSTR("Name",ARG2);
  495.          printf("\n");
  496.          printLOCK("New Lock",RES1);
  497.          printf("\n");
  498.          break;
  499.  
  500.       case _ACTION_EXAMINE_OBJECT:
  501.          printf("ACTION_EXAMINE_OBJECT\n");
  502.          printLOCK("Lock",ARG1);
  503.          printFIB(ARG2);
  504.          printBOOL(RES1);
  505.          break;
  506.  
  507.       case _ACTION_EXAMINE_NEXT:
  508.          printf("ACTION_EXAMINE_NEXT\n");
  509.          printLOCK("Dir Lock",ARG1);
  510.          printFIB(ARG2);
  511.          printBOOL(RES1);
  512.          break;
  513.  
  514.       case _ACTION_DISK_INFO:
  515.          printf("ACTION_DISK_INFO\n");
  516.          printINFO(ARG1);
  517.          printBOOL(RES1);
  518.          break;
  519.  
  520.       case _ACTION_INFO:
  521.          printall("ACTION_INFO",pkt);
  522.          break;
  523.  
  524.       case _ACTION_FLUSH:
  525.          printall("ACTION_FLUSH",pkt);
  526.          break;
  527.  
  528.       case _ACTION_SET_COMMENT:
  529.          printf("ACTION_SET_COMMENT\n");
  530.          printLOCK("Lock",ARG2);
  531.          printBSTR("Name",ARG3);
  532.          printf("\n");
  533.          printBSTR("Comment",ARG4);
  534.          printBOOL(RES1);
  535.          break;
  536.  
  537.       case _ACTION_PARENT:
  538.          printf("ACTION_PARENT\n");
  539.          printLOCK("Dir Lock",ARG1);
  540.          printLOCK("Parent",RES1);
  541.          printf("\n");
  542.          break;
  543.  
  544.       case _ACTION_TIMER:
  545.          printall("ACTION_TIMER",pkt);
  546.          break;
  547.  
  548.       case _ACTION_INHIBIT:
  549.          printf("ACTION_INHIBIT\n");
  550.          printINT("On/Off",ARG1);
  551.          printBOOL(RES1);
  552.          break;
  553.  
  554.       case _ACTION_DISK_TYPE:
  555.          printall("ACTION_DISK_TYPE",pkt);
  556.          break;
  557.  
  558.       case _ACTION_DISK_CHANGE:
  559.          printall("ACTION_DISK_CHANGE",pkt);
  560.          break;
  561.  
  562.       case _ACTION_SET_FILE_DATE:
  563.          printall("ACTION_SET_FILE_DATE",pkt);
  564.          break;
  565.  
  566.       case _ACTION_READ:
  567.          printf("ACTION_READ\n");
  568.          printX("Arg1",ARG1);
  569.          printX("Buffer",ARG2);
  570.          printINT("Length",ARG3);
  571.          printINT("Actual Length",RES1);
  572.          printf("\n");
  573.          printBUF("Data",ARG2,RES1);
  574.          printf("\n");
  575.          break;
  576.  
  577.       case _ACTION_WRITE:
  578.          printf("ACTION_WRITE\n");
  579.          printX("Arg1",ARG1);
  580.          printX("Buffer",ARG2);
  581.          printINT("Length",ARG3);
  582.          printINT("Actual Length",RES1);
  583.          printf("\n");
  584.          printBUF("Data",ARG2,RES1);
  585.          printf("\n");
  586.          break;
  587.  
  588.       case _ACTION_SET_SCREEN_MODE:
  589.          printall("ACTION_SET_SCREEN_MODE",pkt);
  590.          break;
  591.  
  592.       case _ACTION_READ_INTERNAL:
  593.          printall("ACTION_READ_INTERNAL",pkt);
  594.          break;
  595.  
  596.       case _ACTION_WRITE_INTERNAL:
  597.          printall("ACTION_WRITE_INTERNAL",pkt);
  598.          break;
  599.  
  600.       case _ACTION_FIND_INPUT:
  601.          printf("ACTION_FIND_INPUT\n");
  602.          printFH("File Handle",ARG1);
  603.          printLOCK("Dir Lock",ARG2);
  604.          printBSTR("Name",ARG3);
  605.          printf("\n");
  606.          printBOOL(RES1);
  607.          break;
  608.  
  609.       case _ACTION_FIND_OUTPUT:
  610.          printf("ACTION_FIND_OUTPUT\n");
  611.          printFH("File Handle",ARG1);
  612.          printLOCK("Lock",ARG2);
  613.          printBSTR("Name",ARG3);
  614.          printf("\n");
  615.          printBOOL(RES1);
  616.          break;
  617.  
  618.       case _ACTION_CLOSE:
  619.          printf("ACTION_CLOSE\n");
  620.          printX("Arg1",ARG1);
  621.          printBOOL(RES1);
  622.          break;
  623.  
  624.       case _ACTION_SEEK:
  625.          printf("ACTION_SEEK\n");
  626.          printFH("File Handle",ARG1);
  627.          printINT("Position",ARG2);
  628.          printINT("Mode",ARG3);
  629.          printINT("Old Pos",RES1);
  630.          printf("\n");
  631.          break;
  632.  
  633.       default:
  634.          printf("Unknown packet: %ld",pkt->dp_Type);
  635.          printall("",pkt);
  636.          break;
  637.   }
  638. }
  639.