/* procdump                                                           */
/* Copyright (C) 1994 IBM Corporation                                 */
/* Copyright (C) 1994 Richard Moore                                   */
/* IBM Internal Use Only                                              */
/* Author: Richard Moore                      Version: 2.0            */
/* Command Interface to DosDumpProcess API                            */
/*                                                                    */
/* change log:                                                        */
/* 06/03/94 Created                                                   */
/* 15 June 1996 2.0 created for Merlin Functionality                  */

#define INCL_NOPMAPI
#define INCL_DOSMISC
#include <os2.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


/* definitions for DosDumpProcess */
#define DDP_DISABLEPROCDUMP     0x00000000L     /* disable process dumps */
#define DDP_ENABLEPROCDUMP      0x00000001L     /* enable process dumps */
#define DDP_PERFORMPROCDUMP     0x00000002L     /* perform process dump */

APIRET  APIENTRY        DosDumpProcess(ULONG Flag, ULONG Drive, PID Pid);

int main (int argc, char *argv[], char *envp[])
{

   ULONG flag;
   ULONG drive;
   PID pid=0;
   APIRET rc;
   char pidchar[4];
   int i,x,c;
   char * s;
   int trhex[2][10]={
       {0,1,2,3,4,5,6,7,8,9},
       {0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0},
       };

   if (argc!=2) {
      printf("Enable/Disable Process Dump.\n");
      printf("Usage: PROCDUMP [x:] | [OFF] | [PID:pid]\n");
      return 4;
   } /* endif */

   argv[1]=strupr(argv[1]);
   if (strcmp("OFF",argv[1])==0) {
      flag=DDP_DISABLEPROCDUMP;
      drive=0;
      pid=0;
   } else {

   if (memcmp("PID:",argv[1],4)==0) {
      s=strcpy(pidchar,argv[1]+4);
      for (i=0;i<strlen(pidchar);++i) {
         c=pidchar[i];
         x=trhex[c>>6][c & 0x0f];
         pid=(pid<<4)|x;
         };
      if (pid==0L) {
          printf("Dumping current process.\n");
      } else {
         printf("Dumping process for pid:0x%lx.\n",pid);
      } /* endif */
      flag=DDP_PERFORMPROCDUMP;
      drive=0;
   } else {

   if ((strlen(argv[1])==2) && (* (argv[1]+1)==':')) {
      flag=DDP_ENABLEPROCDUMP;
      drive=(int) * argv[1];
      pid=0;
   } else {
      printf("Enable/Disable Process Dump.\n");
      printf("Usage: PROCDUMP [x:] | [OFF] | [PID:pid]\n");
      return 4;
   } /* endif */
   } /* endif */
   } /* endif */

   rc=DosDumpProcess(flag,drive,pid);
   if (rc!=0) {
      printf("DosDumpProcess returned:%u\n",rc);
      return rc;
   } /* endif */

   if (flag==DDP_ENABLEPROCDUMP) 
      printf("Process Dump Enabled. Drive=%s\n",argv[1]);

   if (flag==DDP_DISABLEPROCDUMP) 
      printf("Process Dump Disabled.\n");

   if (flag==DDP_PERFORMPROCDUMP)
      printf("Process Dump Complete.\n");

   return 0;

}

