home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 073.lha / CommandRequester / commandreq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-20  |  6.7 KB  |  241 lines

  1. /***************************************************************************
  2. ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  3.                    CommandReq -- Command File Requester
  4.                  Copyright 1987 Jay Johnson and Sam Morse
  5. ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  6.                    Written by Jay Johnson and Sam Morse
  7.                       Version 1.0 July 17th 1987
  8. ***************************************************************************
  9. This program is public domain. The authors assume no liability for missuse
  10. of this program and are not liable for any damage incurred during use. Use
  11. at your own risk.
  12.  
  13. This program may be used in commercial software ONLY with the written consent
  14. of the original authors.
  15.  
  16. Please send modifications of this code to the below address so that we may
  17. better control the evalution and insure that the best revisions are included
  18. in updates.
  19. ****************************************************************************/
  20.  
  21. #include <intuition/intuitionbase.h> 
  22. #include <graphics/display.h> 
  23. #include <graphics/text.h> 
  24. #include <ctype.h>
  25.  
  26. #define SEL_TIMEOUT 5
  27.  
  28. extern char toupper();
  29.  
  30. char def_font[] ="topaz.font";
  31.  
  32. struct MsgPort *myTimerPort;
  33. struct IOStdReq *myTimerMsg;
  34.  
  35. struct TextAttr TxtAt_Plain = {   (UBYTE *)def_font, 8,
  36.         FS_NORMAL, FPF_ROMFONT};
  37.  
  38. USHORT midwb_img_dat[]=  {
  39.   0xffff,
  40.   0xffff,  0xffff,  0xffff,  0xf800,  0x8000,  0x0000,  0x0000,  0x0000,
  41.   0x0800,  0x8000,  0x0000,  0x0000,  0x0000,  0x0800,  0x8000,  0x0000,
  42.   0x0000,  0x0000,  0x0800,  0x8000,  0x0000,  0x0000,  0x0000,  0x0800,
  43.   0x8000,  0x0000,  0x0000,  0x0000,  0x0800,  0x8000,  0x0000,  0x0000,
  44.   0x0000,  0x0800,  0x8000,  0x0000,  0x0000,  0x0000,  0x0800,  0x8000,
  45.   0x0000,  0x0000,  0x0000,  0x083c,  0x8000,  0x0000,  0x0000,  0x0000,
  46.   0x0800,  0x8000,  0x0000,  0x0000,  0x0000,  0x0a00,  0x8000,  0x0000,
  47.   0x0000,  0x0000,  0x0800,  0x8000,  0x0000,  0x0000,  0x0000,  0x0800,
  48.   0x8000,  0x0000,  0x0000,  0x0000,  0x0800,  0x8000,  0x0000,  0x0000,
  49.   0x0000,  0x0800,  0xffff,  0xffff,  0xffff,  0xffff,  0xf800,  0x0000,
  50.   0x0000,  0x0000,  0x0000,  0x0400,  0x3fff,  0xffff,  0xffff,  0xffff,
  51.   0xf400,  0x3fff,  0xffff,  0xffff,  0xffff,  0xf466,  0x3fff,  0xffff,
  52.   0xffff,  0xffff,  0xf760,  0x3fff,  0xffff,  0xffff,  0xffff,  0xf600,
  53.   0x3fff,  0xffff,  0xffff,  0xffff,  0xf400,  0x3fff,  0xffff,  0xffff,
  54.   0xffff,  0xf400,  0x3fff,  0xffff,  0xffff,  0xffff,  0xf400,  0x3fff,
  55.   0xffff,  0xffff,  0xffff,  0xf400,  0x3fff,  0xffff,  0xffff,  0xffff,
  56.   0xf400,  0x3fff,  0xffff,  0xffff,  0xffff,  0xf4e3,  0x3fff,  0xffff,
  57.   0xffff,  0xffff,  0xf400,  0x3fff,  0xffff,  0xffff,  0xffff,  0xf618,
  58.   0x3fff,  0xffff,  0xffff,  0xffff,  0xf400,  0x3fff,  0xffff,  0xffff,
  59.   0xffff,  0xf400,  0x0000,  0x0000,  0x0000,  0x0000,  0x0400,  0x0001,
  60.  
  61.      };
  62.  
  63.  
  64.  
  65. struct Image midwb_img =  {
  66.        0, 0,70, 16,2,(USHORT *)&midwb_img_dat,0xff,0x00, NULL};
  67.  
  68.  
  69. #include "commandreq.h"
  70.  
  71. struct IntuitionBase *IntuitionBase;
  72. struct GfxBase *GfxBase;
  73. struct Window   *cw;
  74.  
  75. reqcommand(default_value)
  76.   int default_value;
  77. {
  78.  
  79.   ULONG MessageClass,window_sigbit,timer_sigbit,actual_sigbit;
  80.   USHORT code;
  81.   SHORT gadgetID;
  82.   APTR address;
  83.   struct IntuiMessage *cwmess;
  84.   BOOL go = TRUE;
  85.   int retcode;
  86.  
  87.  
  88.   if (!(cw = (struct Window *)OpenWindow(&commandreqwin)))
  89.     exit(20);
  90.  
  91.   Request(&commandreq,cw);
  92.  
  93.   window_sigbit = (1 << (cw->UserPort->mp_SigBit));
  94.   timer_sigbit = 0;
  95.   if (default_value) {
  96.     timer_sigbit = (1 << (myTimerPort->mp_SigBit));
  97.     myTimerMsg->io_Command = TR_ADDREQUEST;
  98.     myTimerMsg->io_Actual = default_value;
  99.     myTimerMsg->io_Length = 0;
  100.     SendIO(myTimerMsg);
  101.   }
  102.  
  103.   do
  104.   {
  105.     actual_sigbit = Wait(window_sigbit | timer_sigbit);
  106.     if (actual_sigbit & timer_sigbit) {
  107.       GetMsg(myTimerPort);
  108.       retcode = SEL_TIMEOUT;
  109.       go = FALSE;
  110.     }
  111.     if (actual_sigbit & window_sigbit) {
  112.       cwmess = (struct IntuiMessage *)GetMsg(cw->UserPort);
  113.  
  114.       MessageClass = cwmess->Class;
  115.       code = cwmess->Code;
  116.       address = cwmess->IAddress;
  117.       ReplyMsg(cwmess);
  118.  
  119.       switch (MessageClass)
  120.       {
  121.         case GADGETUP:
  122.           gadgetID = (((struct Gadget *) address)->GadgetID);
  123.           switch (gadgetID)
  124.           {
  125.             case YES:
  126.             case STOP:
  127.             case NO:
  128.             case FUNCT:
  129.               retcode = GadgetID;
  130.               go = FALSE;
  131.             break;
  132.             default:break;
  133.           };
  134.         break;
  135.         default: break;
  136.       };
  137.     }
  138.   } while(go);
  139.   if (default_value)
  140.     AbortIO(myTimerMsg);
  141.   EndRequest(&commandreq,cw);
  142.   CloseWindow(cw);
  143.  
  144.   return(retcode);
  145.  
  146. }
  147.  
  148. main(argc,argv)
  149.   LONG argc;
  150.   char *argv[];
  151. {
  152.   int returncode,default_time,default_cmd,cmd_value;
  153.  
  154.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  155.   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  156.   if (IntuitionBase == NULL || GfxBase == NULL) {
  157.     printf("Can't open a Library!\n");
  158.     exit(20);
  159.   }
  160.  
  161.   if ((argc < 2) || (argc > 4)) {
  162.     printf("Format: ExeReq 'Command' [default(yes/no/stop)] [seconds]\n");
  163.     cl_libs();
  164.     exit(20);
  165.   }
  166.  
  167.   default_time = 0;
  168.   default_cmd  = 0;
  169.   if (argc > 2) {
  170.     default_time = 10;
  171.     if (strcmpU(argv[2],"YES") == 0) default_cmd = YES;
  172.     if (strcmpU(argv[2],"NO") == 0) default_cmd = NO;
  173.     if (strcmpU(argv[2],"STOP") == 0) default_cmd = STOP;
  174.     if (default_cmd == 0) {
  175.       printf("Default Command Paramater Error.\n");
  176.       cl_libs();
  177.       exit(20);
  178.     }
  179.   }
  180.   if (argc > 3) {
  181.     default_time = atoi(argv[3]);
  182.     if (default_time < 1 || default_time > 600) {
  183.       printf("Default Timeout Paramater Error.\n");
  184.       cl_libs();
  185.       exit(20);
  186.     }
  187.   }
  188.   if (default_time) {
  189.     myTimerPort = CreatePort(0,0);
  190.     if (myTimerPort == 0) {
  191.       cl_libs();
  192.       exit(20);
  193.     }
  194.     myTimerMsg = CreateStdIO(myTimerPort);
  195.     if (myTimerMsg == 0) {
  196.       DeletePort(myTimerPort);
  197.       cl_libs();
  198.       exit(20);
  199.     }
  200.     if (OpenDevice(TIMERNAME,UNIT_VBLANK,myTimerMsg,0) != 0) {
  201.       DeleteStdIO(myTimerMsg);
  202.       DeletePort(myTimerPort);
  203.       cl_libs();
  204.       exit(20);
  205.     }
  206.   }
  207.   strcpy(command_sbuf_1,argv[1]);
  208.   returncode = reqcommand(default_time);
  209.  
  210.   if (returncode == SEL_TIMEOUT) returncode = default_cmd;
  211.   cmd_value = TRUE;
  212.   if (returncode == YES || returncode == FUNCT)
  213.     cmd_value = Execute(command_sbuf_1,0,0);
  214.   if (default_time) {
  215.     CloseDevice(myTimerMsg);
  216.     DeleteStdIO(myTimerMsg);
  217.     DeletePort(myTimerPort);
  218.   }
  219.   cl_libs();
  220.   if (returncode == STOP || cmd_value == FALSE) exit(20);
  221.   if (returncode == NO) exit(0);
  222. }
  223.  
  224. cl_libs()
  225. {
  226.   CloseLibrary(GfxBase);
  227.   CloseLibrary(IntuitionBase);
  228. }
  229. strcmpU(s,t)
  230.   char s[], t[];
  231. {
  232.   int i;
  233.  
  234.   i = 0;
  235.   while(toupper(s[i]) == toupper(t[i])) {
  236.     if (s[i++] == '\0') return 0;
  237.   }
  238.   return toupper(s[i]) - toupper(t[i]);
  239. }
  240.  
  241.