home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 2 / amigaformatcd02.iso / comms / netsoftware / nethandler.lha / NetHandler / util / handd.c next >
Encoding:
C/C++ Source or Header  |  1989-09-17  |  4.1 KB  |  169 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/nodes.h>
  4. #include <exec/lists.h>
  5. #include <exec/ports.h>
  6. #include <libraries/dos.h>
  7. #include <libraries/dosextens.h>
  8. #include <libraries/filehandler.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <proto/exec.h>
  13. #include <proto/dos.h>
  14.  
  15. #undef GLOBAL
  16. /* my version of BADDR() has no problems with casting */
  17. #undef  BADDR
  18. #define BADDR(x)        ((APTR)((long)x << 2))
  19. #define MKBADDR(x)      ((BPTR)((long)x >> 2))
  20.  
  21. #define ACTION_HANDLER_DEBUG    2010L
  22. #define DEBUG_SPECIAL 0x40000000
  23. #define DEBUG_WAIT    0x40000001
  24. #define DEBUG_SERVER  0x20000000
  25. #define DEBUG_SERVWT  0x10000000
  26.  
  27. char windowname[50] = "CON:0/1/640/160/Debug ";
  28.  
  29. LONG sendpkt(struct MsgPort *, long, long*, long, long*);
  30.  
  31. void main(argc, argv)
  32. int argc;
  33. char **argv;
  34. {
  35.    struct MsgPort *proc;
  36.    long myargs[8];
  37.    long myres[2];
  38.    char *filename, *cmdname, *devname;
  39.    int freeflag, server, handler;
  40.  
  41.    cmdname = argv[0];
  42.    if(argc != 3 && argc != 4 && argc != 5)
  43.    {
  44. USAGE:
  45.       fprintf(stderr, 
  46.          "Usage: %s device <SERVER|BOTH> [OPEN|CLOSE|WAIT] <logfile>\n",
  47.          cmdname);
  48.       return;
  49.    }
  50.    devname = argv[1];
  51.  
  52.    if ((proc = (struct MsgPort *)DeviceProc(devname)) == NULL)
  53.    {
  54.       fprintf(stderr, "Unable to get a device proc for %s\n", devname);
  55.       goto USAGE;
  56.    }
  57.  
  58.    handler = 0;
  59.    if((server=(!stricmp(argv[2], "SERVER"))) ||
  60.       (server=handler=(!stricmp(argv[2], "BOTH"))))
  61.    {
  62.       if(argc < 4) goto USAGE;
  63.       argc--, argv++;
  64.    }
  65.    else
  66.       handler = 1;
  67.  
  68.    if(!stricmp(argv[2], "WAIT"))
  69.    {
  70.       freeflag = 0;
  71.       myargs[0] = DEBUG_WAIT;
  72.    }
  73.    else if(!stricmp(argv[2], "CLOSE"))
  74.    {
  75.       freeflag = 1;
  76.       myargs[0] = 0;
  77.    }
  78.    else if(!stricmp(argv[2], "OPEN"))
  79.    {
  80.       freeflag = 1;
  81.       myargs[0] = 1;
  82.       if(handler)
  83.       {
  84.          if(argc == 4) filename = argv[3];
  85.          else
  86.          {
  87.             strcat(windowname, devname);
  88.             strcat(windowname, "/a");
  89.             filename = windowname;
  90.          }
  91.          if(!(myargs[1] = (long)Open(filename, 1006)))
  92.          {
  93.             fprintf(stderr, "Can't open output %s '%s'\n",
  94.                (argc == 4 ? "file" : "window"), filename);
  95.             goto USAGE;
  96.          }
  97.       }
  98.    }
  99.    else
  100.    {
  101.       fprintf(stderr, "Unknown debugging command '%s'\n", argv[2]);
  102.       goto USAGE;
  103.    }
  104.  
  105.    if(handler)
  106.    {
  107.       sendpkt(proc,ACTION_HANDLER_DEBUG,myargs,2,myres);
  108.       if(freeflag && myres[0] != 0 && myres[1])
  109.          Close((BPTR)myres[1]);
  110.    }
  111.  
  112.    if(server)
  113.    {
  114.       myargs[0] |= (DEBUG_SERVER|DEBUG_SPECIAL);
  115.       sendpkt(proc,ACTION_HANDLER_DEBUG,myargs,2,myres);
  116.    }
  117.  
  118. }
  119.  
  120. LONG sendpkt(pid,action,args,nargs,res)
  121. struct MsgPort *pid;  /* process indentifier ... (handlers message port ) */
  122. LONG action,          /* packet type ... (what you want handler to do )   */
  123.      args[],          /* a pointer to a argument list           */
  124.      nargs,           /* number of arguments in list            */
  125.      res[];           /* pointer to 2 longs for result, or NULL */
  126.    {
  127.    struct MsgPort        *replyport;
  128.    struct StandardPacket *packet;
  129.  
  130.    LONG  count, lres, *pargs;
  131.  
  132.    replyport = (struct MsgPort *) CreatePort(NULL,0);
  133.    if(!replyport) return(0L);
  134.  
  135.    packet = (struct StandardPacket *) 
  136.       AllocMem((long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR);
  137.    if(!packet) 
  138.       {
  139.       DeletePort(replyport);
  140.       return(0L);
  141.       }
  142.  
  143.    packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
  144.    packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);
  145.    packet->sp_Pkt.dp_Port         = replyport;
  146.    packet->sp_Pkt.dp_Type         = action;
  147.  
  148.    /* copy the args into the packet */
  149.    pargs = &(packet->sp_Pkt.dp_Arg1);       /* address of first argument */
  150.    for(count=0;count < nargs;count++) 
  151.       pargs[count]=args[count];
  152.  
  153.    PutMsg(pid,(struct Message *)packet); /* send packet */
  154.  
  155.    WaitPort(replyport);
  156.    GetMsg(replyport); 
  157.  
  158.    if(res)
  159.    {
  160.       lres = res[0] = packet->sp_Pkt.dp_Res1;
  161.       res[1] = packet->sp_Pkt.dp_Res2;
  162.    }
  163.  
  164.    FreeMem((char *)packet,(long)sizeof(struct StandardPacket));
  165.    DeletePort(replyport); 
  166.  
  167.    return(lres);
  168. }
  169.