home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / XTIME002.ZIP / XTIME.C < prev    next >
C/C++ Source or Header  |  1990-04-20  |  18KB  |  538 lines

  1. /*-----------------------------
  2.    XTIME.C -- Execute Command at a certain time. 
  3.   -----------------------------*/
  4. #define INCL_DOSSESMGR
  5. #define INCL_DOSFILEMGR
  6. #define INCL_WIN
  7. #define INCL_GPI
  8. #define INCL_DOS
  9. #include <os2.h>
  10. #include <string.h>
  11. #include <stddef.h>
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <stdlib.h>
  15.  
  16. #include "xtime.h"
  17.  
  18. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  19. VOID    SizeTheWindow (HWND) ;
  20. INT     Params (int, char **) ;
  21.  
  22. CHAR     Process_Executed  = FALSE ;
  23. CHAR     Process_Slayed = FALSE ;
  24. CHAR     ExitAfter = FALSE ;
  25.  
  26. TIMEINFO start_time ;
  27.  
  28. USHORT   idSession, id ;
  29.  
  30. STARTDATA scntl = {
  31.        50,                        /* Length               */
  32.        FALSE,                     /* Related              */
  33.        FALSE,                     /* FgBg                 */
  34.        FALSE,                     /* TraceOpt             */
  35.        "",                        /* PgmTitle             */
  36.        "",                        /* PgmName              */
  37.        "",                        /* PgmInputs            */
  38.        "",                        /* TermQ                */
  39.        0,                         /* Environment          */
  40.        FALSE,                     /* InheritOpt           */
  41.        0,                         /* SessionType          */
  42.        "",                        /* IconFile             */
  43.        0,                         /* PgmHandle            */
  44.        0x0000,                    /* PgmControl           */
  45.        0,0,                       /* InitXPos, InitYPos   */
  46.        0,0                        /* InitXSize, InitYSize */
  47.        };
  48.  
  49.  
  50. int main (int argc, char *argv[])
  51.      {
  52.      static CHAR  szClientClass[] = "XTIME" ;
  53.      static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU  |
  54.                                  FCF_BORDER   | FCF_TASKLIST ;
  55.      HAB          hab ;
  56.      HMQ          hmq ;
  57.      HWND         hwndFrame, hwndClient ;
  58.      QMSG         qmsg ;
  59.      int          exitrc ;
  60.      char         szErrorMsg[32];
  61.  
  62.      hab = WinInitialize (0) ;
  63.      hmq = WinCreateMsgQueue (hab, 0) ;
  64.  
  65.      WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0) ;
  66.  
  67.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  68.                                      &flFrameFlags, szClientClass, NULL,
  69.                                      0L, NULL, 0, &hwndClient) ;
  70.      SizeTheWindow (hwndFrame) ;
  71.  
  72.      if (WinStartTimer (hab, hwndClient, ID_TIMER, 1000))
  73.           {
  74.           if (exitrc = Params(argc, &argv[0]) == NO_ERROR)
  75.               while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  76.                     WinDispatchMsg (hab, &qmsg) ;
  77.           else {
  78.                if (exitrc == PROGRAM_NOT_FOUND)
  79.                   sprintf(szErrorMsg, "Unable to locate Program");
  80.  
  81.                if (exitrc == SEMANTIC_ERROR)
  82.                   sprintf (szErrorMsg, "Start Session Options invalid or program name not specified");
  83.  
  84.                WinMessageBox (HWND_DESKTOP, hwndClient, "Some Duffus Error", 
  85.                               szClientClass, 0, MB_OK | MB_ICONEXCLAMATION);
  86.                }
  87.           WinStopTimer (hab, hwndClient, ID_TIMER) ;
  88.           }
  89.      else
  90.           WinMessageBox (HWND_DESKTOP, hwndClient,
  91.                          "Too many clocks or timers",
  92.                          szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;
  93.  
  94.      WinDestroyWindow (hwndFrame) ;
  95.      WinDestroyMsgQueue (hmq) ;
  96.      WinTerminate (hab) ;
  97.      return 0 ;
  98.      }
  99.  
  100. VOID SizeTheWindow (HWND hwndFrame)
  101.      {
  102.      FONTMETRICS fm ;
  103.      HPS         hps ;
  104.      RECTL       rcl ;
  105.  
  106.      hps = WinGetPS (hwndFrame) ;
  107.      GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  108.      WinReleasePS (hps) ;
  109.  
  110.      rcl.yBottom = 0 ;
  111.      rcl.yTop    = 11 * fm.lMaxBaselineExt / 4 ;
  112.      rcl.xRight  = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN) ;
  113.      rcl.xLeft   = rcl.xRight - 20 * fm.lEmInc ;
  114.  
  115.      WinCalcFrameRect (hwndFrame, &rcl, FALSE) ;
  116.  
  117.      WinSetWindowPos (hwndFrame, NULL, (SHORT) rcl.xLeft, (SHORT) rcl.yBottom,
  118.                       (SHORT) (rcl.xRight - rcl.xLeft),
  119.                       (SHORT) (rcl.yTop - rcl.yBottom), SWP_SIZE | SWP_MOVE) ;
  120.      }
  121.  
  122.  
  123. int Params(int argc, char *argv[])
  124.      {
  125.      static CHAR PgmArgs[255]  = "",
  126.                  PgmDrive[6]   = "",
  127.                  PgmDir[255]   = "",
  128.                  PgmName[10]   = "",
  129.                  PgmExt[8]     = "",
  130.                  IconName[255] = "",
  131.                  PgmFName[255] = "",
  132.                  PgmTitle[32]  = "",
  133.                  PgmTime[32]   = "";
  134.  
  135.  
  136.      USHORT i,
  137.             hh, mm,
  138.             name_found,
  139.             time_found,
  140.             stype_found,
  141.             title_found,
  142.             exec_cmd,
  143.             retain_cmd,
  144.             SearchCode,
  145.             SearchCnt;
  146.  
  147.      CHAR SearchName[14],
  148.           SearchPath[255];
  149.  
  150.      HDIR        hdir = HDIR_SYSTEM;
  151.      FILEFINDBUF FFbufr;
  152.  
  153.      if (argc <= 1) {
  154.         return SEMANTIC_ERROR; 
  155.         }
  156.  
  157.      name_found = time_found = stype_found = title_found = exec_cmd = retain_cmd = FALSE;
  158.  
  159.      /* go through all arguments */
  160.   
  161.      for (i = 1; i < argc; i++) {
  162.  
  163.        /* if argument has a / or a - before it process here */
  164.        if (argv[i][0] == '/' || argv[i][0] == '-') {
  165.  
  166.           if (0 == strnicmp(_PARM(i), "TIT:", 4))
  167.               strncpy(PgmTitle, &argv[i][5], sizeof(PgmTitle));
  168.  
  169.           else if (0 == strnicmp(_PARM(i), "TIME:", 5)) {
  170.                   strncpy(PgmTime, &argv[i][6], sizeof(PgmTime));
  171.                   time_found = TRUE;
  172.                   }
  173.  
  174.           else if (0 == stricmp(_PARM(i), "BG"))
  175.                   scntl.FgBg = TRUE;
  176.  
  177.           else if (0 == stricmp(_PARM(i), "FS")) {
  178.                   if (stype_found)
  179.                      continue;
  180.                   scntl.SessionType = 1;
  181.                   stype_found = TRUE;
  182.                   }
  183.  
  184.           else if (0 == stricmp(_PARM(i), "WIN")) {
  185.                   if (stype_found)
  186.                      continue;
  187.                   scntl.SessionType = 2;
  188.                   stype_found = TRUE;
  189.                   }
  190.  
  191.           else if (0 == stricmp(_PARM(i), "PM")) {
  192.                   if (stype_found)
  193.                      continue;
  194.                   scntl.SessionType = 3;
  195.                   stype_found = TRUE;
  196.                   }
  197.                           
  198.           else if (0 == stricmp(_PARM(i), "MIN"))
  199.                   scntl.PgmControl = (scntl.PgmControl & 0x8008) | 0x0004;
  200.  
  201.           else if (0 == stricmp(_PARM(i), "MAX"))
  202.                   scntl.PgmControl = (scntl.PgmControl & 0x8008) | 0x0002;
  203.  
  204.           else if (0 == stricmp(_PARM(i), "NOC"))
  205.             scntl.PgmControl = scntl.PgmControl | 0x0008;
  206.  
  207.           else if (0 == stricmp(_PARM(i), "INV"))
  208.             scntl.PgmControl = 0x0001;
  209.  
  210.           else if (0 == stricmp(_PARM(i), "I"))
  211.             scntl.InheritOpt = TRUE;
  212.  
  213.           else if (0 == stricmp(_PARM(i), "CMD"))
  214.             exec_cmd = TRUE;
  215.  
  216.           else if (0 == stricmp(_PARM(i), "K"))
  217.             retain_cmd = TRUE;
  218.  
  219.           else if (0 == stricmp(_PARM(i), "EXIT"))
  220.             ExitAfter = TRUE;
  221.  
  222.           else if (0 == stricmp(_PARM(i), "TRACE"))
  223.             scntl.TraceOpt = TRUE;
  224.  
  225.           else  return SEMANTIC_ERROR;
  226.           
  227.        }
  228.        /* if no / or - before parameter it is the program name */
  229.        else {
  230.            _splitpath(argv[i], PgmDrive, PgmDir, PgmName, PgmExt);
  231.            name_found = TRUE;
  232.            break;
  233.            }
  234.      }
  235. /*end of command line parameters*/
  236.  
  237.      if (!name_found)
  238.         return SEMANTIC_ERROR;
  239.  
  240.      if (sscanf(PgmTime, "%u:%u", &hh, &mm) == 2)
  241.         if ((hh >= 0) && (mm >= 0) && (hh < 24) && (mm < 60))
  242.            {
  243.            start_time.hours = hh;
  244.            start_time.mins  = mm;
  245.            }
  246.         else return SEMANTIC_ERROR;
  247.      else return SEMANTIC_ERROR;
  248.  
  249.      /* Search for Program */
  250.      if (PgmDir[0]) {
  251.         strcpy(SearchPath, PgmDrive);
  252.         strcat(SearchPath, PgmDir);
  253.         SearchCode = SEARCH_PATH;
  254.         }
  255.  
  256.      else {
  257.  
  258.        strcpy(SearchPath, "PATH");
  259.        SearchCode = SEARCH_ENVIRONMENT + SEARCH_CUR_DIRECTORY;
  260.        }
  261.  
  262.      strcpy(SearchName, PgmName);
  263.  
  264.      if (PgmExt[0])
  265.         strcat(SearchName, PgmExt);
  266.      else
  267.         strcat(SearchName, ".*" );
  268.  
  269.      memset(PgmFName, '\0', sizeof(PgmFName));
  270.  
  271.      DosSearchPath(SearchCode,
  272.                    SearchPath,
  273.                    SearchName,
  274.                    PgmFName,
  275.                    sizeof(PgmFName));
  276.  
  277.      if (PgmFName[0]) {
  278.         if (!PgmExt[0]) {
  279.  
  280.            SearchCnt = 1;
  281.            _splitpath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  282.            strcpy(PgmExt, ".COM");
  283.            _makepath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  284.            hdir = HDIR_SYSTEM;
  285.  
  286.            if (DosFindFirst(PgmFName,
  287.                             &hdir,
  288.                             FILE_NORMAL + FILE_READONLY + FILE_ARCHIVED,
  289.                             &FFbufr,
  290.                             sizeof(FFbufr),
  291.                             &SearchCnt,
  292.                             0L)) {
  293.  
  294.               strcpy(PgmExt, ".CMD");
  295.               _makepath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  296.               SearchCnt = 1;
  297.               hdir = HDIR_SYSTEM;
  298.  
  299.               if (DosFindFirst(PgmFName,
  300.                                &hdir,
  301.                                FILE_NORMAL + FILE_READONLY + FILE_ARCHIVED,
  302.                                &FFbufr,
  303.                                sizeof(FFbufr),
  304.                                &SearchCnt,
  305.                                0L)) {
  306.   
  307.                  strcpy(PgmExt, ".EXE");
  308.                  _makepath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  309.                  SearchCnt = 1;
  310.                  hdir = HDIR_SYSTEM;
  311.  
  312.                  if (DosFindFirst(PgmFName,
  313.                                   &hdir,
  314.                                   FILE_NORMAL + FILE_READONLY + FILE_ARCHIVED,
  315.                                   &FFbufr,
  316.                                   sizeof(FFbufr),
  317.                                   &SearchCnt,
  318.                                   0L))
  319.                     return PROGRAM_NOT_FOUND;
  320.                  }
  321.               }
  322.            }
  323.         }
  324.         else if (exec_cmd) {
  325.                 strcpy(PgmName, argv[i]);
  326.                 strcpy(PgmFName, argv[i]);
  327.                 }
  328.              else 
  329.                 return PROGRAM_NOT_FOUND;
  330.  
  331.      if (strcmp(PgmExt, ".CMD") == 0 || exec_cmd) {
  332.  
  333.         if (scntl.SessionType == 3)
  334.             scntl.SessionType = 2;
  335.         scntl.PgmName = "CMD.EXE";
  336.         strcpy(PgmArgs, (retain_cmd ? "/K " : "/c "));
  337.         strcat(PgmArgs, PgmFName);
  338.         strcat(PgmArgs, " ");
  339.         }
  340.      else
  341.         scntl.PgmName = PgmFName;
  342.  
  343.      for (i++; i < argc; i++) {
  344.  
  345.          strcat(PgmArgs, argv[i]);
  346.          strcat(PgmArgs, " ");
  347.          }
  348.  
  349.      scntl.PgmInputs = PgmArgs;
  350.  
  351.      if (PgmTitle[0])
  352.         scntl.PgmTitle = PgmTitle;
  353.      else
  354.         scntl.PgmTitle = scntl.PgmName;
  355.  
  356.  
  357.      strcpy(SearchName, PgmName);
  358.      strcat(SearchName, ".ICO");
  359.  
  360.      memset(IconName, '\0', sizeof(IconName));
  361.  
  362.      DosSearchPath(SearchCode,
  363.                    SearchPath,
  364.                    SearchName,
  365.                    IconName,
  366.                    sizeof(IconName));
  367.            
  368.      scntl.IconFile = IconName;
  369.      
  370.      return 0;
  371.      }
  372.  
  373.  
  374.  
  375. VOID UpdateTime (HWND hwnd, HPS hps)
  376.      {
  377.      static BOOL        fHaveCtryInfo = FALSE ;
  378.      static CHAR        *szDayName [] = { "Sun", "Mon", "Tue", "Wed",
  379.                                           "Thu", "Fri", "Sat" } ;
  380.      static CHAR        szDateFormat [] = " %s  %d%s%02d%s%02d " ;
  381.      static COUNTRYCODE ctryc = { 0, 0 } ;
  382.      static COUNTRYINFO ctryi ;
  383.      int                erc ;
  384.      CHAR               szBuffer [20] ;
  385.      DATETIME           dt ;
  386.      RECTL              rcl ;
  387.      USHORT             usDataLength ;
  388.  
  389.                /*----------------------------------------
  390.                   Get Country Information, Date and Time
  391.                  ----------------------------------------*/
  392.  
  393.      if (!fHaveCtryInfo)
  394.           {
  395.           DosGetCtryInfo (sizeof ctryi, &ctryc, &ctryi, &usDataLength) ;
  396.           fHaveCtryInfo = TRUE ;
  397.           }
  398.      DosGetDateTime (&dt) ;
  399.      if ((dt.hours == (UCHAR)start_time.hours) &&
  400.         (dt.minutes == (UCHAR)start_time.mins))
  401.         {
  402.         if ( !Process_Executed ) {
  403.            Process_Executed = TRUE ;
  404.            GpiErase(hps) ;
  405.            sprintf(szBuffer, "Executing ....") ;
  406.            WinQueryWindowRect (hwnd, &rcl) ;
  407.            rcl.yBottom += 5 * rcl.yTop / 11 ;
  408.            WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  409.                         DT_CENTER | DT_VCENTER) ;
  410. /* Execute */
  411.  
  412.            if (erc = DosStartSession( &scntl, &idSession, &id) != NO_ERROR) {
  413.               sprintf(szBuffer, "Dos Start Session Ended with Error %d", erc);
  414.  
  415.               /* need to add for szClientClass */
  416.               WinMessageBox (HWND_DESKTOP, hwnd, szBuffer,
  417.                             "szClientClass", 0, MB_OK | MB_ICONEXCLAMATION) ;
  418.               }
  419.            }
  420.         }
  421.  
  422.      dt.year %= 100 ;
  423.  
  424.                /*------------- 
  425.                   Format Date
  426.                  -------------*/
  427.                                    /*-----------------
  428.                                       mm/dd/yy format
  429.                                      -----------------*/
  430.      if (ctryi.fsDateFmt == 0)
  431.  
  432.           sprintf (szBuffer, szDateFormat, szDayName [dt.weekday],
  433.                              dt.month, ctryi.szDateSeparator,
  434.                              dt.day,   ctryi.szDateSeparator, dt.year) ;
  435.  
  436.                                    /*-----------------
  437.                                       dd/mm/yy format
  438.                                      -----------------*/
  439.      else if (ctryi.fsDateFmt == 1)
  440.  
  441.           sprintf (szBuffer, szDateFormat, szDayName [dt.weekday],
  442.                              dt.day,   ctryi.szDateSeparator,
  443.                              dt.month, ctryi.szDateSeparator, dt.year) ;
  444.  
  445.                                    /*-----------------
  446.                                       yy/mm/dd format
  447.                                      -----------------*/
  448.      else
  449.           sprintf (szBuffer, szDateFormat, szDayName [dt.weekday],
  450.                              dt.year,  ctryi.szDateSeparator,
  451.                              dt.month, ctryi.szDateSeparator, dt.day) ;
  452.  
  453.                /*-------------- 
  454.                   Display Date
  455.                  --------------*/
  456.  
  457.      WinQueryWindowRect (hwnd, &rcl) ;
  458.      rcl.yBottom += 5 * rcl.yTop / 11 ;
  459. /*     WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  460.                   DT_CENTER | DT_VCENTER) ; */
  461.  
  462.                /*------------- 
  463.                   Format Time
  464.                  -------------*/
  465.                                    /*----------------
  466.                                       12-hour format
  467.                                      ----------------*/
  468.      if ((ctryi.fsTimeFmt & 1) == 0)
  469.  
  470.           sprintf (szBuffer, " %d%s%02d%s%02d %cm ",
  471.                              (dt.hours + 11) % 12 + 1, ctryi.szTimeSeparator,
  472.                              dt.minutes, ctryi.szTimeSeparator,
  473.                              dt.seconds, dt.hours / 12 ? 'p' : 'a') ;
  474.  
  475.                                    /*----------------
  476.                                       24-hour format
  477.                                      ----------------*/
  478.      else
  479.           sprintf (szBuffer, " %02d%s%02d%s%02d ",
  480.                              dt.hours,   ctryi.szTimeSeparator,
  481.                              dt.minutes, ctryi.szTimeSeparator, dt.seconds) ;
  482.  
  483.                /*-------------- 
  484.                   Display Time
  485.                  --------------*/
  486.  
  487.      WinQueryWindowRect (hwnd, &rcl) ;
  488.      rcl.yTop -= 5 * rcl.yTop / 11 ;
  489.      WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  490.                   DT_CENTER | DT_VCENTER) ;
  491.              
  492.                /*-------------------------
  493.                   Display Execute Program
  494.                  -------------------------*/
  495.      if (Process_Executed)
  496.         if (ExitAfter)
  497.            Exit(0);
  498.         else sprintf(szBuffer, "Program ID %u Executed.", &idSession ) ;
  499.      else
  500.         sprintf (szBuffer, "%s", scntl.PgmName) ;
  501.      WinQueryWindowRect (hwnd, &rcl) ;
  502.      rcl.yBottom += 5 * rcl.yTop /11 ;
  503.      WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  504.                   DT_CENTER | DT_VCENTER) ;
  505.  
  506.      
  507.      }
  508.  
  509. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  510.      {
  511.      HPS  hps;
  512.  
  513.      switch (msg)
  514.           {
  515.           case WM_CREATE:
  516.                return 0 ;
  517.  
  518.           case WM_TIMER:
  519.                hps = WinGetPS (hwnd) ;
  520.                GpiSetBackMix (hps, BM_OVERPAINT) ;
  521.  
  522.                UpdateTime (hwnd, hps) ;
  523.  
  524.                WinReleasePS (hps) ;
  525.                return 0 ;
  526.  
  527.           case WM_PAINT:
  528.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  529.                GpiErase (hps) ;
  530.  
  531.                UpdateTime (hwnd, hps) ;
  532.  
  533.                WinEndPaint (hps) ;
  534.                return 0 ;
  535.           }
  536.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  537.      }
  538.