home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / ALARM.C next >
C/C++ Source or Header  |  1992-08-27  |  12KB  |  449 lines

  1. /*
  2. * ALARM.C - Show a alarm message at a specified time.
  3. *
  4. *
  5. * PROGRAMMER:        Martti Ylikoski
  6. * CREATED:        16.3.1992
  7. */
  8. static char *VERSION="Version  1.0. Copyright(c) Martti Ylikoski, 1992. " ;
  9. /*
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "list.h"
  16. #include <param.h>
  17. #include <paramstd.h>
  18. ParamEntry pentry[12] = {
  19.      "P", &ParamSetPause, 0,
  20.      "F", &ParamSetFold, 0,
  21.      "V", &ParamSetVerbose, 0,
  22.      "R", &ParamSetReport, 0,
  23.      "S", &ParamSetSubDirs, 0,
  24.      "?", &ParamSetHelp, 1,
  25.      "H", &ParamSetHelp, 1,
  26.      "NOD", &ParamSetNoDefault, 1,
  27.      "TEST", &ParamSetTest, 0,
  28.      "Y", &ParamSetYes, 0,
  29.      "N", &ParamSetTest, 0,
  30.      "\0", NULL, 0
  31. } ;
  32.  
  33. ParamBlock params = {
  34.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  35.     pentry
  36. } ;
  37. extern unsigned long pflags ; /* needed because of paramstd */
  38.  
  39.  
  40. /* from wndbox.h */
  41.  
  42. #define SNGL_TOPLEFT  0
  43. #define SNGL_TOPRIGHT 1
  44. #define SNGL_LINE     2
  45. #define SNGL_BAR      3
  46. #define SNGL_BOTLEFT  4
  47. #define SNGL_BOTRIGHT 5
  48.  
  49. char sngl[6] = {
  50.    218,
  51.    191,
  52.    196,
  53.    179,
  54.    192,
  55.    217
  56. } ;
  57.  
  58. #define INCL_VIO
  59. #define INCL_DOS
  60. #define INCL_KBD
  61. #include <os2.h>
  62.  
  63. static char *progname ;
  64. static char *deffile = ".\\alarm.dat" ;
  65. /* local prototypes */
  66. static void DrawBox(int TopRow, int TopCol, int BotRow, int BotCol) ;
  67. static void DrawData(HNDLIST *lhandle, int TopRow, int TopCol, int BotRow, int BotCol) ;
  68. static void AddToDisplay(HNDLIST *lhandle, char *fbuff, int *DataLineCount, int *MaxLineLen) ;
  69. int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds) ;
  70. void TimeWaitUntil(int hours, int minutes, int seconds) ;
  71. void AlarmShow(HNDLIST *lhandle, int MaxLineLen, int DataLineCount) ;
  72. void MessageExpand(HNDLIST *lhandle, char *tmp2, int *MaxLineLen, int *DataLineCount ) ;
  73. int main(int argc, char *argv[])
  74. {
  75. int  MaxLineLen, DataLineCount, c ;
  76. FILE *fp ;
  77. HNDLIST *lhandle ;
  78. char  fbuff[125], *tmp, *tmp2 ;
  79. UCHAR hours, minutes, seconds ;
  80.  
  81.  
  82.    progname = argv[0] ;
  83.    ParamHandle(¶ms, &argc, argv) ;
  84.  
  85.    if ((lhandle = ListInit()) == NULL)
  86.    {
  87.       printf("Error in ListInit\nExit\n") ;
  88.       return( 1 ) ;
  89.    }
  90.  
  91.    
  92.  
  93.    DataLineCount = 0 ;
  94.    MaxLineLen = 0 ;
  95.  
  96.    if (pflags & PA_HELP)
  97.    {
  98.       sprintf(fbuff,"%s - Show a alarm message at a specified time to the user.\n", progname) ;
  99.       AddToDisplay(lhandle, fbuff, &DataLineCount, &MaxLineLen) ;
  100.       AddToDisplay(lhandle,VERSION, &DataLineCount, &MaxLineLen) ;
  101.       AddToDisplay(lhandle,"Usage: alarm  [time message] | [/F alarmfile] | [/? | /H] [/NOD]", &DataLineCount, &MaxLineLen) ;
  102.       AddToDisplay(lhandle,"Where,", &DataLineCount, &MaxLineLen) ;
  103.       AddToDisplay(lhandle,"  message        = Displayed message with \\n between different lines.", &DataLineCount, &MaxLineLen) ;
  104.       AddToDisplay(lhandle,"  /F alarmfile = Alarm times and messages in a file", &DataLineCount, &MaxLineLen) ;
  105.       AddToDisplay(lhandle,"  /NOD = No default. Automatically added two last lines are not displayed.", &DataLineCount, &MaxLineLen) ;
  106.       AddToDisplay(lhandle,"  /H,/? = Help", &DataLineCount, &MaxLineLen) ;
  107.       AlarmShow(lhandle, MaxLineLen, DataLineCount) ;
  108.       return( 0 ) ; 
  109.    }
  110.  
  111.    if (argc == 3 && (strcmpi(argv[1], "-F") != 0 && strcmpi(argv[1], "/f") !=0) )
  112.    {
  113.       StrExpandTime(argv[1], ":", &hours, &minutes, &seconds) ;         
  114.       MessageExpand(lhandle, argv[2], &MaxLineLen, &DataLineCount ) ;
  115.       TimeWaitUntil(hours, minutes, seconds) ;
  116.  
  117.    }
  118.    else 
  119.    {
  120.  
  121.       if (argc >2 && ( strcmpi(argv[1], "/f") == 0 || strcmpi(argv[1], "-f")==0))
  122.          deffile = argv[2] ;
  123.       
  124.       if ((fp = fopen (deffile, "r")) == NULL)
  125.       {
  126.          sprintf(fbuff,"%s : error opening messagefile %s.", progname, deffile) ;
  127.          AddToDisplay(lhandle, fbuff, &DataLineCount, &MaxLineLen) ;
  128.          AddToDisplay(lhandle, " ", &DataLineCount, &MaxLineLen) ;
  129.       }
  130.       else
  131.       {
  132.          while (fgets(fbuff, sizeof(fbuff), fp ) != NULL)
  133.          {
  134.             char t[11] ;
  135.  
  136.             if (strlen(fbuff) == 0 || fbuff[0] == '\n')
  137.                continue ;
  138.             
  139.             if (fbuff[0] == '#' || fbuff[0] == ';') /* handle comment lines */
  140.                continue ;
  141.  
  142.             if (strnicmp(fbuff, "CONTINUE",8) == 0)
  143.             {
  144.                rewind(fp) ;
  145.                continue ;
  146.             }
  147.  
  148.             if (strnicmp(fbuff, "BREAK",5) == 0 || strnicmp(fbuff, "QUIT",4) == 0)
  149.             {
  150.                fclose(fp) ;
  151.                return( 0 )  ;
  152.            }
  153.  
  154.             tmp = strtok(fbuff, " ") ;
  155.             strcpy(t, tmp) ;
  156.             StrExpandTime(t, ":", &hours, &minutes, &seconds) ;         
  157.             tmp += strlen(tmp)+1 ;
  158.             MessageExpand(lhandle, tmp, &MaxLineLen, &DataLineCount ) ;
  159.             TimeWaitUntil(hours, minutes, seconds) ;
  160.             AlarmShow(lhandle, MaxLineLen, DataLineCount) ;
  161.             DataLineCount = 0 ;
  162.             MaxLineLen = 0 ;
  163.             ListExit(lhandle) ; /* missing function ListClear */
  164.             if ((lhandle = ListInit()) == NULL)
  165.             {
  166.                printf("Error in ListInit\nExit\n") ;
  167.                return( 1 ) ;
  168.             }
  169.           }
  170.  
  171.          fclose (fp) ;
  172.          return( 0 ) ;
  173.       }
  174.    }
  175.  
  176.    AlarmShow(lhandle, MaxLineLen, DataLineCount) ;  
  177.    return( 0 ) ;
  178. }
  179.  
  180. void MessageExpand(HNDLIST *lhandle, char *tmp2, int *MaxLineLen, int *DataLineCount )
  181. {
  182. char *tmp ;
  183. //      tmp2 = argv[2] ;
  184.       while (  1 ) 
  185.       {
  186.          
  187.          if ( (tmp =strchr(tmp2, (int) '\\')) != NULL && tmp[1] == 'n' )
  188.          {
  189.             tmp[0] = '\0' ;
  190.             tmp[1] = '\0' ;
  191.             *DataLineCount += 1 ;
  192.  
  193. //            if (fbuff[strlen(tmp2)-1] == '\n')
  194. //               fbuff[strlen(tmp2)-1] = '\0' ; /* remove trailing newline */
  195.  
  196.             if (strlen(tmp2) > *MaxLineLen)
  197.                *MaxLineLen = strlen(tmp2) ;
  198.             ListAdd(lhandle, tmp2, strlen(tmp2)+1) ; /* +1 because we store also \0 */
  199.             tmp2 = tmp+2 ;
  200.             if (tmp2[0] == '\0')
  201.                break ;
  202.          }
  203.          else
  204.          if (tmp2[0] == '\0' || tmp2[0] == '\n')
  205.             break ;
  206.          else
  207.          {
  208.             *DataLineCount += 1 ;
  209.             if (strlen(tmp2) > *MaxLineLen)
  210.                *MaxLineLen = strlen(tmp2) ;
  211.             ListAdd(lhandle, tmp2, strlen(tmp2)+1) ; /* +1 because we store also \0 */
  212.             break ;
  213.  
  214.          }
  215.       }
  216. }
  217.  
  218. void AlarmShow(HNDLIST *lhandle, int MaxLineLen, int DataLineCount)
  219. {
  220. USHORT fWait = VP_WAIT | VP_OPAQUE ;
  221. int  TopRow, TopCol, BotRow, BotCol, MaxCol, MaxRow;
  222. KBDKEYINFO kbdi ;
  223. char  fbuff[125] ;
  224.  
  225.    VioPopUp(&fWait, 0) ;
  226.  
  227.    MaxCol = 79 ;
  228.    MaxRow = 24 ;
  229.  
  230.    if (pflags & PA_NODEFAULT)
  231.       ;
  232.    else
  233.    {
  234.       if (MaxLineLen <28)
  235.          MaxLineLen = 28 ;
  236.       memset(fbuff, sngl[SNGL_LINE], (size_t) MaxLineLen) ;
  237.       ListAdd(lhandle, fbuff, strlen(fbuff)+1) ; /* +1 because we store also \0 */
  238.       strcpy(fbuff, "Press ESC to cancel message.") ;
  239.       ListAdd(lhandle, fbuff, strlen(fbuff)+1) ; /* +1 because we store also \0 */
  240.       if (strlen(fbuff) > MaxLineLen)
  241.          MaxLineLen = strlen(fbuff) ;
  242.  
  243.       DataLineCount += 2 ;
  244.    }
  245.  
  246.    TopRow = (MaxRow - DataLineCount)/2 -1  ;
  247.    if (TopRow <0)
  248.       TopRow = 0 ;
  249.  
  250.    TopCol = (MaxCol - MaxLineLen)/2 - 1 ;
  251.    if (TopCol <0)
  252.       TopCol = 0 ;   
  253.  
  254.    BotRow = 24 -(MaxRow - DataLineCount)/2 +1 ;
  255.    BotCol = 79 - (MaxCol - MaxLineLen)/2 + 1 ;
  256.  
  257.    DrawBox(TopRow, TopCol, BotRow, BotCol)  ;
  258.  
  259.  
  260.    DrawData(lhandle, TopRow, TopCol, BotRow, BotCol) ;
  261.    do
  262.    {
  263.       KbdCharIn(&kbdi,  IO_WAIT, 0) ;
  264.    } while (kbdi.chChar != 27) ;
  265.  
  266.    VioEndPopUp( 0 ) ;
  267.  
  268. }
  269. static void DrawBox(int TopRow, int TopCol, int BotRow, int BotCol)
  270. {
  271. int i ;
  272. BYTE WndCol = 0x17 ;
  273.  
  274.    VioWrtCharStrAtt(&sngl[SNGL_TOPLEFT],1,TopRow, TopCol, &WndCol, 0) ;
  275.  
  276.    for (i = 1 ; i < BotCol - TopCol ; i++)
  277.       VioWrtCharStrAtt(&sngl[SNGL_LINE],1,TopRow, TopCol+i, &WndCol, 0) ;
  278.  
  279.    VioWrtCharStrAtt(&sngl[SNGL_TOPRIGHT],1,TopRow, BotCol, &WndCol, 0) ;
  280.  
  281.    for (i = 1 ; i < BotRow - TopRow; i++)
  282.    {
  283.       VioWrtCharStrAtt(&sngl[SNGL_BAR],1, TopRow+i, TopCol, &WndCol, 0) ;
  284.       VioWrtCharStrAtt(&sngl[SNGL_BAR],1, TopRow+i, BotCol, &WndCol, 0) ;
  285.    }
  286.    VioWrtCharStrAtt( &sngl[SNGL_BOTLEFT], 1, BotRow, TopCol, &WndCol, 0) ;
  287.    for (i = 1 ; i < BotCol - TopCol ; i++)
  288.       VioWrtCharStrAtt(&sngl[SNGL_LINE],1,BotRow, TopCol+i, &WndCol, 0) ;
  289.    VioWrtCharStrAtt(&sngl[SNGL_BOTRIGHT],1,BotRow, BotCol, &WndCol, 0) ;
  290.  
  291.  
  292. }
  293.  
  294. static void DrawData(HNDLIST *lhandle, int TopRow, int TopCol, int BotRow, int BotCol)
  295. {
  296. unsigned short width, height, drawlen ;
  297. LIST_ENTRY *templist ;
  298. long templ, templ2 ;
  299. int actioncode ;
  300. char *buf ;
  301. unsigned long buflen ;
  302. int i ;
  303. unsigned char attrib ;
  304. unsigned char cell[2] ;
  305.    cell[0] = ' ' ;
  306.    cell[1] =  0x17; // wnd->WndCol ;
  307.  
  308.    height = BotRow - TopRow ;  /* calculate the height of the window */
  309.    width = BotCol - TopCol  ;  /* calculate the width of the window */
  310.    attrib = 0x17 ; //WndCol ;
  311.  
  312.    ListFirst(lhandle) ;
  313.  
  314.    /* Note different drawing limits for windows with/without borders */
  315.    for (i = 1 ; i <= height - 1 ; i++)
  316.    {   
  317.       ListReturn(lhandle, &buf, &buflen) ;
  318.       VioWrtCharStrAtt( buf, buflen , TopRow+i, TopCol+1, &attrib, 0) ;
  319.       VioWrtNCell(cell, width-1 - buflen, TopRow+i, TopCol+1+buflen, 0) ;
  320.  
  321.       if ( ListNext(lhandle) != LIST_OK)
  322.          break ;
  323.    }
  324.    
  325.    /* Note different drawing limits for windows with/without borders */
  326.    for ( ++i ; i <= height - 1 ; i++)
  327.    {   
  328.       VioWrtNCell(cell, width-1 , TopRow+i, TopCol+1, 0) ;
  329.    }
  330.  
  331. }
  332.  
  333.  
  334. static void AddToDisplay(HNDLIST *lhandle, char *fbuff, int *DataLineCount, int *MaxLineLen) 
  335. {
  336.          *DataLineCount += 1 ;
  337.  
  338.          if (fbuff[strlen(fbuff)-1] == '\n')
  339.             fbuff[strlen(fbuff)-1] = '\0' ; /* remove trailing newline */
  340.  
  341.          if (strlen(fbuff) > *MaxLineLen)
  342.             *MaxLineLen = strlen(fbuff) ;
  343.  
  344.          ListAdd(lhandle, fbuff, strlen(fbuff)+1) ; /* +1 because we store also \0 */
  345.  
  346. }
  347.  
  348. void TimeWaitUntil(int hours, int minutes, int seconds)
  349. {
  350. DATETIME dt ;
  351. LONG sleepval ;
  352. USHORT tomorrow = FALSE ;
  353.  
  354.  
  355.    DosGetDateTime(&dt) ;
  356.    
  357.    if (dt.hours > hours)  /* schedule for tomorrow */
  358.       tomorrow = TRUE ;
  359.  
  360.    do
  361.    {
  362.       DosGetDateTime(&dt) ;
  363.  
  364.       if(dt.hours == 0)
  365.          tomorrow = FALSE ;
  366.          
  367.       sleepval = ((hours - dt.hours)*3600 + (minutes-dt.minutes)*60 +
  368.                  seconds-dt.seconds)/2 ;
  369.  
  370.       if (sleepval < 0 && tomorrow == FALSE)
  371.          break ;
  372.  
  373.       if (sleepval == 0)
  374.         DosSleep(320L) ;
  375.       else
  376.          DosSleep(sleepval*1000L) ;
  377.  
  378.    } while ( hours > dt.hours ||
  379.      (hours ==dt.hours && minutes > dt.minutes) ||
  380.      (hours ==dt.hours  && minutes == dt.minutes && seconds > dt.seconds) ||
  381.      tomorrow == TRUE ) ;
  382. }
  383.  
  384. int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds)
  385. {
  386. int add = FALSE ;
  387. DATETIME dt ;
  388. char *tmp ;
  389.    /* set default values for time */
  390.    DosGetDateTime(&dt) ;
  391.    *hours = dt.hours ;
  392.    *minutes = dt.minutes ;
  393.    *seconds = dt.seconds ;
  394.  
  395.    if (str[0] == '+')
  396.       add = TRUE ;
  397.  
  398.    if ( (tmp=strtok(str, tsepa)) == NULL)
  399.       return( 1 ) ;
  400.  
  401.     if (add==TRUE)
  402.        *hours += atoi(tmp) ;
  403.     else
  404.        *hours=atoi(tmp) ;
  405.  
  406.    if (*hours >24)
  407.    {
  408.       *hours -= 24 ;
  409.    }
  410.    
  411.    /* new default values */
  412.    if (add == FALSE)
  413.    {
  414.       *minutes = '0' ;
  415.       *seconds = '0' ;
  416.    }
  417.  
  418.    if ( (tmp=strtok(NULL, tsepa)) == NULL)
  419.       return( 0 ) ;
  420.  
  421.     if (add==TRUE)
  422.        *minutes += atoi(tmp) ;
  423.     else
  424.        *minutes = atoi(tmp) ;
  425.    
  426.    if (*minutes >60)
  427.    {
  428.       *minutes -= 60 ;
  429.       *hours += 1 ;
  430.    }
  431.  
  432.    if ( (tmp=strtok(NULL, tsepa)) == NULL)
  433.       return( 0 ) ;
  434.  
  435.     if (add==TRUE)
  436.        *seconds += atoi(tmp) ;
  437.     else
  438.        *seconds = atoi(tmp) ;
  439.  
  440.    if (*seconds >60)
  441.    {
  442.       *seconds -=60 ;
  443.       *minutes += 1 ;
  444.    }
  445.    
  446.    return( 0 ) ;
  447. }
  448.  
  449.