home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / SHUTDOWN.CMD < prev    next >
OS/2 REXX Batch file  |  1995-01-02  |  8KB  |  247 lines

  1. @echo OFF
  2. REM *****************************************************
  3. REM *** ShutDown.cmd - Shutdown or reboot the system. ***
  4. REM *** ver.12                                        ***
  5. REM *****************************************************
  6.  
  7. CEnvi2 %0.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
  8. GOTO CENVI_EXIT
  9.  
  10. // Build the following list of processes that should not be killed
  11. IMMORTAL_PROCESSES = {
  12.       "PMSHELL",     // PM and WPS
  13.       "HARDERR",     // OS/2 nasty error manager
  14.       "FATMGR"       // stacker process
  15. };
  16.  
  17. #define BOOTDOS_FLAG_FILE
  18.  
  19. main(argc,argv)
  20. {
  21.    ShutProcedure = NULL;
  22.    ExtraArgs = "";
  23.    StartCommand = "START \"Automated CEnvi2 ShutDown\" /K /WIN";  // default method for starting
  24.    if ( argc == 2) {
  25.       if ( !strcmpi(argv[1],"OFF") ) {
  26.          ShutProcedure = "TurnOffSystem";
  27.          ExtraArgs = argv[0];
  28.       } else if ( !strcmpi(argv[1],"NICE") ) {
  29.          ShutProcedure = "WinShutdownSystem";
  30.          StartCommand = "CMD.EXE /C DETACH";
  31.       } else if ( !strcmpi(argv[1],"REBOOT") ) {
  32.          ShutProcedure = "RebootSystem";
  33.       } else if ( !strcmpi(argv[1],"BOOTDOS") ) {
  34.          ShutProcedure = "BootViaDualBoot";
  35.       }
  36.    } else if ( 2 < argc  &&  !strcmpi(argv[1],"SETBOOT") ) {
  37.       ShutProcedure = "BootByCommand";
  38.       // Build ExtraArgs to pass to SETBOOT
  39.       ExtraArgs = "SETBOOT";
  40.       for ( i = 2; i < argc; i++ ) {
  41.          strcat(ExtraArgs," ");
  42.          strcat(ExtraArgs,argv[i]);
  43.       }
  44.    }
  45.    if ( NULL == ShutProcedure )
  46.       Instructions();
  47.  
  48.    // turn off and flush lazy cache
  49.    system("cache /LAZY:OFF"); suspend(2000);
  50.  
  51.    // start the shutdown or reboot code detached. Extra CMD.EXE /C is
  52.    // added in case using 4OS2.
  53.    system("%s CEnvi2 #include '%s,,,/*RESIDENT*/,:CENVI_EXIT' %s(`%s`)",
  54.           StartCommand,argv[0],ShutProcedure,ExtraArgs);
  55.  
  56.    return EXIT_SUCCESS;
  57. }
  58.  
  59. Instructions()
  60. {
  61.    printf("\a\n")
  62.    puts(`ShutDown - Save desktop and shutdown or reboot`)
  63.    puts(``)
  64.    puts(`USAGE: ShutDown [ OFF | REBOOT | NICE | BOOTDOS | SETBOOT <SetbootArgs>]`)
  65.    puts(``)
  66.    puts(`Where: OFF: Force all windows and applications to close and shutdown for`)
  67.    puts(`            power to be turned off or to wait for ctrl-alt-del.`)
  68.    puts(`       REBOOT: Force all windows and applications to close and shutdown,`)
  69.    puts(`               and then automatically reboot the system.`)
  70.    puts(`       NICE: Standard shutdown, but do not force applications closed. You`)
  71.    puts(`             will be asked to shutdown running applications.`)
  72.    puts(`       BOOTDOS: Reboot via "BOOT /DOS" if using dual-boot`)
  73.    puts(`       SETBOOT <SetBootArgs>: Reboot via SETBOOT if you have boot manager.`)
  74.    puts(``)
  75.    puts(`Note: All options except NICE attempt to save all new settings and close`)
  76.    puts(`      windows or shut applications through various stages of severity.  These`)
  77.    puts(`      options will not, for example, select File/Save from an editors menus`)
  78.    puts(`      before terminating the editor.  If you have specific methods for closing`)
  79.    puts(`      applications before shutting down, then call those before SHUTDOWN.`)
  80.    puts(``)
  81.    puts(`Examples: ShutDown REBOOT`)
  82.    puts(`          ShutDown OFF`)
  83.    puts(`          ShutDown BOOTDOS`)
  84.    puts(`          ShutDown SETBOOT /IBA:DOS`)
  85.    exit(EXIT_FAILURE);
  86. }
  87.  
  88. /*RESIDENT*/
  89.  
  90. #include <WinMsg.lib>
  91. #include <WinTools.lib>
  92. #include <KeyPush.lib>
  93.  
  94. ForceEverythingToEnd()
  95. {
  96.    SaveAllWindows();    suspend(5000);
  97.    CloseAllWindows();   suspend(5000);
  98.    QuitAllWindows();    suspend(5000);
  99.    KillAllProcesses();  suspend(5000);
  100. }
  101.  
  102. PostToAllWindows(pMsg,pParam1,pParam2)
  103. {  // send to all windows except for this one
  104.    lEnum = WinBeginEnumWindows(HWND_DESKTOP);
  105.    while ( lChild = WinGetNextWindow(lEnum) ) {
  106.       if ( lChild != Info().WinHandle )
  107.          WinPostMsg(lChild,pMsg,pParam1,pParam2,False);
  108.    }
  109.    WinEndEnumWindows(lEnum);
  110. }
  111.  
  112. SaveAllWindows()  // Post WM_SAVEAPPLICATION to all windows
  113. {
  114.    printf("Send WM_SAVEAPPLICATION to all windows...");
  115.    #define WM_SAVEAPPLICATION 0x003e
  116.    PostToAllWindows(WM_SAVEAPPLICATION,0,0);
  117.    printf("\b\b\b\n");
  118. }
  119.  
  120. CloseAllWindows()  // Post WM_CLOSE to all windows
  121. {
  122.    printf("Send WM_CLOSE to all windows...");
  123.    #define WM_CLOSE  0x0029
  124.    PostToAllWindows(WM_CLOSE,0,0);
  125.    printf("\b\b\b\n");
  126. }
  127.  
  128. QuitAllWindows()  // Post WM_QUIT to any remaining windows
  129. {
  130.    printf("Send WM_QUIT to all windows...");
  131.    #define WM_QUIT   0x002a
  132.    PostToAllWindows(WM_QUIT,0,0);
  133.    printf("\b\b\b\n");
  134. }
  135.  
  136. KillAllProcesses()  // Kill any remaining processes, except for a
  137. {                 // select few (including this one)
  138.    ImmortalCount = 1 + GetArraySpan(IMMORTAL_PROCESSES);
  139.  
  140.    PList = ProcessList();
  141.    for ( i = 0; i < GetArraySpan(PList); i++ ) {
  142.       ProcessName = SplitFileName(PList[i].name).name;
  143.       printf("Process: %s - ",ProcessName);
  144.  
  145.       // don't kill if it is this process
  146.       if ( PList[i].id == Info().Process ) {
  147.          printf("don't kill myself\n");
  148.          continue;
  149.       }
  150.  
  151.       // don't kill if this is one of the IMMORTAL_PROCESSES
  152.       for ( j = 0; j < ImmortalCount; j++ ) {
  153.          ProcessName = SplitFileName(PList[i].name).name;
  154.          if ( !stricmp(ProcessName,IMMORTAL_PROCESSES[j]) )
  155.             break;
  156.       }
  157.       if ( j < ImmortalCount ) {
  158.          printf("don't kill immortal process\n");
  159.          continue;
  160.       }
  161.  
  162.       #define DKP_PROCESSTREE    0  // kill process and all descendents, if created by this process
  163.       #define DKP_PROCESS        1  // kill any process even if not created by this process
  164.       #define ORD_DOS32KILLPROCESS  235
  165.       DynamicLink("doscalls",ORD_DOS32KILLPROCESS,BIT32,CDECL,DKP_PROCESS,PList[i].id)
  166.       printf("killed\n");
  167.    }
  168. }
  169.  
  170. TurnOffSystem(ShutdownScriptName)   // brutal shutdown
  171. {
  172.    ForceEverythingToEnd();
  173.    printf("CMD.EXE /C DETACH CEnvi2 #include '%s,,,/*RESIDENT*/,:CENVI_EXIT' WinShutdownSystem()\n",
  174.           ShutdownScriptName);
  175.    system("CMD.EXE /C DETACH CEnvi2 #include '%s,,,/*RESIDENT*/,:CENVI_EXIT' WinShutdownSystem()",
  176.           ShutdownScriptName);
  177. }
  178.  
  179. WinShutdownSystem()  // Execute the WinShutdownSystem() function
  180. {
  181.    suspend(3000);
  182.    // Get Msg Queue of this window
  183.    #define ORD_WIN32QUERYWINDOWULONG   843
  184.    #define QWL_HMQ   (-4)
  185.    MsgQueue = DynamicLink("PMWIN",ORD_WIN32QUERYWINDOWULONG,BIT32,CDECL,
  186.                           Info().WinHandle,QWL_HMQ);
  187.  
  188.    // Call WinShutdownSystem()
  189.    #define  ORD_WIN32SHUTDOWNSYSTEM 149
  190.    DynamicLink("PMWP",ORD_WIN32SHUTDOWNSYSTEM,BIT32,CDECL,
  191.                Info().hab,MsgQueue);
  192. }
  193.  
  194. RebootSystem() // mimick ctrl-alt-del reboot
  195. {
  196.    ForceEverythingToEnd();
  197.    suspend(8000);
  198.    printf("Rebooting...");
  199.    #define ORD_DOS32OPEN   273
  200.    #define FILE_NORMAL     0x0000
  201.    #define FILE_OPEN       0x0001
  202.    #define OPEN_SHARE_DENYNONE      0x0040
  203.    #define OPEN_ACCESS_READWRITE    0x0002
  204.    if ( !DynamicLink("doscalls",ORD_DOS32OPEN,BIT32,CDECL,
  205.                      "\\DEV\\DOS$",FileHandle,ActionTaken,0,
  206.                      FILE_NORMAL,FILE_OPEN,
  207.                      OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,0) ) {
  208.  
  209.       // Send call to DosShutdown to clear all buffers
  210.       #define ORD_DOS32SHUTDOWN  415
  211.       //DynamicLink("doscalls",ORD_DOS32SHUTDOWN,BIT32,CDECL,0);
  212.  
  213.       // send reboot command to device
  214.       #define ORD_DOS32DEVIOCTL     284
  215.       #define CATEGORY_DOSSYS       0xD5
  216.       #define FUNCTION_REBOOT       0xAB
  217.       DynamicLink("doscalls",ORD_DOS32DEVIOCTL,BIT32,CDECL,
  218.                   FileHandle,CATEGORY_DOSSYS,FUNCTION_REBOOT,
  219.                   NULL,0,NULL,NULL,0,NULL);
  220.  
  221.       #define ORD_DOS32CLOSE  257
  222.       DynamicLink("doscalls",ORD_DOS32CLOSE,BIT32,CDECL,FileHandle);
  223.    }
  224. }
  225.  
  226. BootByCommand(BootCommand)
  227. {
  228.    ForceEverythingToEnd();
  229.    suspend(8000);
  230.    printf("%s\n",BootCommand);
  231.    system(BootCommand);
  232. }
  233.  
  234. BootViaDualBoot()
  235. {
  236.    ForceEverythingToEnd();
  237.    suspend(8000);
  238.    puts(`Start "BOOT /DOS"`);
  239.    system(`start "BOOT /DOS" /N /F /WIN BOOT /DOS`);
  240.    suspend(5000);
  241.    puts(`Send "Y" to "BOOT /DOS"`);
  242.    system(`TextWin "BOOT /DOS" Y\r`);
  243. }
  244.  
  245. :CENVI_EXIT
  246. REM IF NOT ERRORLEVEL 1 EXIT
  247.