home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / ExecuteOnReset / ExecuteOnReset.c < prev    next >
C/C++ Source or Header  |  2000-08-13  |  7KB  |  288 lines

  1. /*******************************************************************************
  2. *
  3. *       Program:        ExecuteOnReset
  4. *       Filename:       ExecuteOnReset.c
  5. *
  6. *       Usage:          Run <>NIL: ExecuteOnReset [-p pri] string1 [string2] ...
  7. *                           -p pri    - priority (default: 0)
  8. *                           string1   - command line 1 to execute
  9. *                           string2   - optional command line 2 to execute
  10. *                           ...       - more command lines.
  11. *
  12. *       Contents:       Installes a reset handler which executes the command
  13. *                       lines on case of a reset (CTRL-Amiga-Amiga). After
  14. *                       execution of all commands the handler continues with
  15. *                       the reset (or other installed reset handlers)
  16. *                       imediately! So if you do some disk tasks add a
  17. *                       "Wait 3 secs" line as last command to avoid non
  18. *                       validated disks. You must be cautionous: The Amiga
  19. *                       allows a maximum of 10 seconds to do all reset handler
  20. *                       tasks. After this amount of time a hard reset occurs
  21. *                       regardless if a command has finished or not. If you
  22. *                       install more reset handlers you can choose the order
  23. *                       of execution by the priority argument.
  24. *                       The Run command is necessary to detach from the shell.
  25. *                       A CTRL-C signal removes the handler.
  26. *
  27. *       Language:       C
  28. *
  29. *       Link-instr.:    Objects:   c.o ExecuteOnReset.o keyhandler.o
  30. *                       Libraries: amiga.lib
  31. *                       Output:    ExecuteOnReset
  32. *
  33. *       Author:         Johannes R. Geiss
  34. *
  35. *       Copyright:      Amigavisions
  36. *
  37. *       History:        $HISTORY:
  38. *
  39. *                       08 Aug 2000 : 001.001 :  written jgeiss
  40. *
  41. *       Version:        $VER: ExecuteOnReset 1.001 (08 Aug 2000)
  42. *
  43. *******************************************************************************/
  44.  
  45.  
  46. /**************/
  47. /*- Includes -*/
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52.  
  53. #include <exec/types.h>
  54. #include <exec/interrupts.h>
  55. #include <dos/dos.h>
  56. #include <devices/keyboard.h>
  57. #include <clib/exec_protos.h>
  58. #include <clib/alib_protos.h>
  59.  
  60.  
  61. /*************/
  62. /*- Imports -*/
  63.  
  64. extern void ResetHandler();
  65.  
  66.  
  67. /*****************/
  68. /*- Definitions -*/
  69.  
  70. #define VER "1.1"
  71.  
  72. #define CSI "\x9b"
  73. #define COLORBOLD CSI "1;33m"
  74. #define COLOR CSI "0;33m"
  75. #define NORMAL CSI "0m"
  76.  
  77.  
  78. /*************/
  79. /*- Structs -*/
  80.  
  81. struct MyData
  82. {
  83.     struct Task *MyTask;
  84.     ULONG MySignal;
  85. };
  86.  
  87.  
  88. /**********/
  89. /*- main -*/
  90.  
  91. int main(int argc, char *argv[])
  92. {
  93.     BYTE pri = 0;
  94.     int i, start;
  95.     UWORD len;
  96.     char *command;
  97.     int rc = 0;
  98.     ULONG MySignal;
  99.     struct MyData MyDataStuff;
  100.     struct Interrupt *keyHandler;
  101.     struct IOStdReq *keyReqBlk;
  102.  
  103.  
  104.     /*
  105.      * handle arguments
  106.      */
  107.  
  108.     i = 1;
  109.  
  110.     if((argc > i) && (!strcmp(argv[i], "?"))) {
  111.     printf("\n"
  112.            COLORBOLD "ExecuteOnReset V" VER ":" COLOR " Copyright (C) 2000, Amigavisions" NORMAL "\n"
  113.            "Usage: Run <>NIL: %s [-p pri] string1 [string2] ...\n"
  114.            "   -p pri    - priority (default: 0)\n"
  115.            "   string1   - command line 1 to execute\n"
  116.            "   string2   - optional command line 2 to execute\n"
  117.            "   ...       - more command lines.\n"
  118.            "Installes a reset handler which executes the command lines on case of a reset\n"
  119.            "(CTRL-Amiga-Amiga). After execution of all commands the handler continues with\n"
  120.            "the reset (or other installed reset handlers) imediately! So if you do some disk\n"
  121.            "tasks add a \"Wait 3 secs\" line as last command to avoid non validated disks.\n"
  122.            "You must be cautionous: The Amiga allows a maximum of 10 seconds to do all reset\n"
  123.            "handler tasks. After this amount of time a hard reset occurs regardless if a\n"
  124.            "command has finished or not. If you install more reset handlers you can choose\n"
  125.            "the order of execution by the priority argument.\n"
  126.            "The Run command is necessary to detach from the shell. A CTRL-C signal removes\n"
  127.            "the handler.\n"
  128.            "\n", argv[0]);
  129.     return 5;
  130.     }
  131.  
  132.     if((argc > i) && (argv[i][0] == '-')) {
  133.  
  134.     if(argv[i][1] == 'p') {
  135.  
  136.         if(argv[i][2]) pri = atoi(&argv[i++][2]);
  137.         else if(argc > ++i) pri = atoi(argv[i++]);
  138.         else {
  139.         fprintf(stderr, "%s: missing priority\n", argv[0]);
  140.         return 10;
  141.         }
  142.  
  143.     }
  144.     else {
  145.         fprintf(stderr, "%s: unknown option: -%c\n", argv[0], argv[i][1]);
  146.         return 11;
  147.     }
  148.  
  149.     }
  150.  
  151.     start = i;
  152.     len = 0;
  153.  
  154.     while(i < argc) len += strlen(argv[i++]) + 1;
  155.  
  156.     if(len == 0) {
  157.     fprintf(stderr, "%s: missing command line\n", argv[0]);
  158.     return 12;
  159.     }
  160.  
  161.  
  162.     /*
  163.      * create command
  164.      */
  165.  
  166.     if(command = AllocMem(len, MEMF_PUBLIC|MEMF_CLEAR)) {
  167.     i = start;
  168.  
  169.     while(i < argc) {
  170.         strcat(command, argv[i++]);
  171.         strcat(command, "\n");
  172.     }
  173.  
  174.  
  175.         /*
  176.          * install reset handler
  177.          */
  178.  
  179.     if((MySignal = AllocSignal(-1l)) != -1) {
  180.         MyDataStuff.MyTask = FindTask(NULL);
  181.         MyDataStuff.MySignal = 1l << MySignal;
  182.  
  183.         if(keyHandler = AllocMem(sizeof(struct Interrupt),
  184.                      MEMF_PUBLIC|MEMF_CLEAR)) {
  185.  
  186.         if(keyReqBlk = (struct IOStdReq *)
  187.                    CreateExtIO(CreatePort(NULL, 0), sizeof(struct IOStdReq))) {
  188.  
  189.             if(!(OpenDevice("keyboard.device", NULL,
  190.                     (struct IORequest *)keyReqBlk, NULL))) {
  191.             keyHandler->is_Code = ResetHandler;
  192.             keyHandler->is_Data = (APTR)&MyDataStuff;
  193.             keyHandler->is_Node.ln_Pri = pri;
  194.             keyHandler->is_Node.ln_Name = argv[0];
  195.             keyReqBlk->io_Data = (APTR)keyHandler;
  196.             keyReqBlk->io_Command = KBD_ADDRESETHANDLER;
  197.             DoIO((struct IORequest *)keyReqBlk);
  198.  
  199.  
  200.                 /*
  201.                  * wait for reset or CTRL-C
  202.                  */
  203.  
  204.             if(Wait(MyDataStuff.MySignal|SIGBREAKF_CTRL_C)
  205.                & MyDataStuff.MySignal) {
  206.  
  207.  
  208.                 /*
  209.                  * captured a reset
  210.                  * now execute the command(s)
  211.                  */
  212.  
  213.                 system(command);
  214.  
  215.  
  216.                 /*
  217.                  * tell reset handler we are finished
  218.                  * so others can do their job
  219.                  */
  220.  
  221.                 keyReqBlk->io_Data = (APTR)keyHandler;
  222.                 keyReqBlk->io_Command = KBD_RESETHANDLERDONE;
  223.                 DoIO((struct IORequest *)keyReqBlk);
  224.  
  225.  
  226.                 /*
  227.                  * wait for reset
  228.                  */
  229.  
  230.                 Wait(0l);
  231.             }
  232.  
  233.  
  234.                 /*
  235.                  * got a CTRL-C
  236.                  * so we remove ourself
  237.                  */
  238.  
  239.             keyReqBlk->io_Data = (APTR)keyHandler;
  240.             keyReqBlk->io_Command = KBD_REMRESETHANDLER;
  241.             DoIO((struct IORequest *)keyReqBlk);
  242.  
  243.             CloseDevice((struct IORequest *)keyReqBlk);
  244.             }
  245.             else {
  246.             fprintf(stderr, "%s: couldn't open keyboard device\n", argv[0]);
  247.             rc = 24;
  248.             }
  249.  
  250.         }
  251.         else {
  252.             fprintf(stderr, "%s: couldn't create keyboard io\n", argv[0]);
  253.             rc = 23;
  254.         }
  255.  
  256.         FreeMem(keyHandler, sizeof(struct Interrupt));
  257.         }
  258.         else {
  259.         fprintf(stderr, "%s: couldn't allocate memory\n", argv[0]);
  260.         rc = 22;
  261.         }
  262.  
  263.         FreeSignal(MySignal);
  264.     }
  265.     else {
  266.         fprintf(stderr, "%s: couldn't allocate signal\n", argv[0]);
  267.         rc = 21;
  268.     }
  269.  
  270.     FreeMem(command, len);
  271.     }
  272.     else {
  273.     fprintf(stderr, "%s: couldn't allocate memory\n", argv[0]);
  274.     rc = 20;
  275.     }
  276.  
  277.  
  278.     /*
  279.      * exit
  280.      */
  281.  
  282.     return rc;
  283. }
  284.  
  285.  
  286. /** EOF **/
  287.  
  288.