/* trapdump                                                           */
/* Copyright (C) 1996 IBM Corporation                                 */
/* Copyright (C) 1996 Richard Moore                                   */
/* IBM Internal Use Only                                              */
/* Author: Richard Moore                      Version: 1.0            */
/* Command Interface to DosForceSystemDump                            */
/*                                                                    */
/* change log:                                                        */
/* 12 June 1996 Created                                               */

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

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


/* definitions for DosForceSystemDump */
#define FSD_TRAPDUMPOFF         0x8000
#define FSD_TRAPDUMPON          0x8001
#define FSD_TRAPDUMPR0          0x8002

APIRET APIENTRY DosForceSystemDump(ULONG reserved);

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

   union {
      ULONG flag;
      struct {
         USHORT dump_device;
         USHORT dump_on;
         } uls;
      } fl_u;

   APIRET rc;
   int i,x;
   BOOL bDumpOn=0, bDumpDev=0, bSysCheck=1;

   struct {
      ULONG Major;
      ULONG Minor;
      ULONG Revision;
      } MajMin;

   fl_u.flag=0;

   if (argc<2 || argc>4) {
      printf("Enable/Disable TRAPDUMP.\n");
      printf("Usage: TRAPDUMP [[ON] | [OFF] | [R0]] [x:] [/NOCHECK]\n");
      return 4;
   } /* endif */


   for (i=1;i<argc ;++i ) {


      argv[i]=strupr(argv[i]);


      if (strcmp(argv[i],"/NOCHECK")==0) bSysCheck=0;
      else {

      if (strcmp("OFF",argv[i])==0) {
         if (bDumpOn) {
            printf("Conflicting parameters specified\n");
            return 4;
         } /* endif */
         bDumpOn=1;
         fl_u.uls.dump_on=FSD_TRAPDUMPOFF;
      } else {

      if (strcmp("ON",argv[i])==0) {
         if (bDumpOn) {
            printf("Conflicting parameters specified\n");
            return 4;
         } /* endif */
         bDumpOn=1;
         fl_u.uls.dump_on=FSD_TRAPDUMPON;
      } else {

      if (strcmp("R0",argv[i])==0) {
         if (bDumpOn) {
            printf("Conflicting parameters specified\n");
            return 4;
         } /* endif */
         bDumpOn=1;
         fl_u.uls.dump_on=FSD_TRAPDUMPR0;
      } else {

      if ((strlen(argv[i])==2) && (* (argv[i]+1)==':')) {
         if (bDumpDev) {
            printf("Conflicting parameters specified\n");
            return 4;
         } /* endif */
         bDumpDev=1;
         fl_u.uls.dump_device=(int) * argv[i];

      } else {
         printf("Enable/Disable TRAPDUMP.\n");
         printf("Usage: TRAPDUMP [[ON] | [OFF] | [R0]] [x:] [/NOCHECK]\n");
         return 4;
      } /* endif */
      } /* endif */
      } /* endif */
      } /* endif */
      } /* endif */

   } /* endfor */

   if (fl_u.flag==0) return 9999; // sanity check - we don't want to force
                                  // a system dump through bugs in this module

   if (bSysCheck) {
      rc=DosQuerySysInfo(QSV_VERSION_MAJOR,QSV_VERSION_REVISION,(PVOID)&MajMin,12);
      if (rc!=0) {
         printf("DosQuerySysInfo returned rc=%u\n",rc);
         return 4;
      } /* endif */

      if ( (MajMin.Major <= 20L) && (MajMin.Minor < 40L)) {
         printf("This version of OS/2 may not support dynamic TRAPDUMP.\n");
         printf("System version: %u.%u.%u\n",MajMin.Major,MajMin.Minor,MajMin.Revision);
         printf("If you allow this command to continue a system dump\n");
         printf("may be forced. Do you wish to continue? (Y/N)\n");
         x=getc(stdin);
         if (!(x=='Y' || x=='y')) {
            return 0;
         } /* endif */
      } /* endif */
   } /* endif */

   rc=DosForceSystemDump(fl_u.flag);
   if (rc!=0) {
      printf("Invalid Parameters specified.\n");
     return rc;
   } /* endif */

   if (bDumpOn) {

      if (fl_u.uls.dump_on==FSD_TRAPDUMPOFF)
         printf("TRAPDUMP=OFF.\n");
      if (fl_u.uls.dump_on==FSD_TRAPDUMPON)
         printf("TRAPDUMP=ON.\n");
      if (fl_u.uls.dump_on==FSD_TRAPDUMPR0)
         printf("TRAPDUMP=R0.\n");

   } /* endif */

   if (bDumpDev) printf("Dump Drive=%c\n",fl_u.uls.dump_device);

   return 0;
}

