home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / AT.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  6KB  |  262 lines

  1. /*    Timed execution routine. Starts a timer and executes a sequence
  2.  *    of commands when expired.
  3.  *
  4.  *    Added by IW0CNB - Feb 1992
  5.  *  'at mm' format added by WG7J - 920805
  6.  */
  7.  
  8. /****************************************************************************
  9. *    $Id: at.c 1.2 93/07/16 11:42:14 ROOT_DOS Exp $
  10. *    15 Jul 93    1.2        GT    Fix warnings.                                    *
  11. ****************************************************************************/
  12.  
  13. #include <time.h>
  14. #include <dos.h>
  15. #include "global.h"
  16. #include "timer.h"
  17. #include "cmdparse.h"
  18. #include "socket.h"
  19.  
  20. #if    defined (time)
  21. #undef    time                                /* remove macro                    */
  22. #endif
  23.  
  24. void atcmd __ARGS((char *command));
  25.  
  26. /* List of events; We keep note of all timer processes generated by the 
  27.  * at command.
  28.  */
  29. struct at_list {
  30.     struct at_list *next;    /* Linked-list pointer */
  31.     struct timer *at_timer;
  32. };
  33.  
  34. #define    NULLATLIST    (struct at_list *)0
  35.  
  36. static struct at_list *Head_loe = NULLATLIST;    /* Head of List Of Events */
  37.  
  38. int
  39. doat(argc,argv,p)
  40. int argc;
  41. char *argv[];
  42. void *p;
  43. {
  44.     struct date *exp_date;
  45.     struct time *exp_time;
  46.     struct timer *t;
  47.     char *cp;
  48.     time_t nowtime;
  49.     unsigned long time1;
  50.     struct tm tm;
  51.     extern struct timer *Timers;
  52.     struct at_list *loe;    /* List of events */
  53.     char *Errmsg = "Usage:\nat yymmddhhmm <cmd>\nat hhmm <cmd>\nat mm <cmd>\nat now+hhmm <cmd>\n";
  54.  
  55.     if(argc < 2){        /* Print list of pending at commands */
  56.         tputs("List of events:\n");
  57.         for(t = Timers;t != NULLTIMER;t = t->next){
  58.             if(t->func == (void (*)())atcmd){
  59.             time(&nowtime);
  60.             nowtime = (time_t)(read_timer(t) / 1000L + (unsigned long)nowtime);
  61.             cp = ctime(&nowtime);
  62.             rip(cp);
  63.             tprintf("At: %s - Command: %s\n",cp,t->arg);
  64.            }
  65.         }
  66.         return 0;
  67.     }
  68.  
  69.     if(argc < 3){
  70.         tputs(Errmsg);
  71.         return 0;
  72.     }
  73.  
  74.     exp_date = (struct date *)mallocw(sizeof(struct date));
  75.     exp_time = (struct time *)mallocw(sizeof(struct time));
  76.  
  77.     cp=mallocw(5);
  78.  
  79.     switch(strlen(argv[1])){
  80.        case 10:    /* Full date and time given */
  81.         cp[0]=argv[1][0];
  82.         cp[1]=argv[1][1];
  83.         cp[2]='\0';
  84.     
  85.         exp_date->da_year = 1900 + atoi(cp);
  86.         if(exp_date->da_year > 1999) goto error;
  87.  
  88.         cp[0]=argv[1][2];
  89.         cp[1]=argv[1][3];
  90.         cp[2]='\0';
  91.  
  92.         exp_date->da_mon = (char)atoi(cp);
  93.         if(exp_date->da_mon > 12) goto error;
  94.  
  95.         cp[0]=argv[1][4];
  96.         cp[1]=argv[1][5];
  97.         cp[2]='\0';
  98.  
  99.         exp_date->da_day = (char)atoi(cp);
  100.         if(exp_date->da_day > 31) goto error;
  101.  
  102.         cp[0]=argv[1][6];
  103.         cp[1]=argv[1][7];
  104.         cp[2]='\0';
  105.  
  106.         exp_time->ti_hour = (char)atoi(cp);
  107.         if(exp_time->ti_hour > 23) goto error;
  108.  
  109.         cp[0]=argv[1][8];
  110.         cp[1]=argv[1][9];
  111.         cp[2]='\0';
  112.  
  113.         exp_time->ti_min = (char)atoi(cp);
  114.         if(exp_time->ti_min > 59) goto error;
  115.  
  116.         exp_time->ti_sec = 0;
  117.         exp_time->ti_hund = 0;
  118.  
  119.         time(&nowtime);
  120.         time1 = (unsigned long)dostounix(exp_date,exp_time);
  121.         if(time1 < (unsigned long)nowtime) goto error;
  122.  
  123.         break;
  124.  
  125.        case 4:  /* Only time given, so apply current date */
  126.         getdate(exp_date);
  127.         cp[0]=argv[1][0];
  128.         cp[1]=argv[1][1];
  129.         cp[2]='\0';
  130.  
  131.         exp_time->ti_hour = (char)atoi(cp);
  132.         if(exp_time->ti_hour > 23) goto error;
  133.  
  134.         cp[0]=argv[1][2];
  135.         cp[1]=argv[1][3];
  136.         cp[2]='\0';
  137.  
  138.         exp_time->ti_min = (char)atoi(cp);
  139.         if(exp_time->ti_min > 59) goto error;
  140.  
  141.         exp_time->ti_sec = 0;
  142.         exp_time->ti_hund = 0;
  143.  
  144.         time(&nowtime);
  145.         time1 = (unsigned long)dostounix(exp_date,exp_time);
  146.         if(time1 < (unsigned long)nowtime){    /* Requested time has passed */
  147.             time1 += 86400L;        /* So book him for tomorrow */
  148.         }
  149.         break;
  150.  
  151.  
  152.        case 2:  /* Only minutes given, so apply current time & date - WG7J */
  153.         tm.tm_min = (char)atoi(argv[1]);
  154.         if(tm.tm_min > 59) goto error;
  155.  
  156.         /* get today's date */
  157.         getdate(exp_date);
  158.         tm.tm_year = exp_date->da_year - 1900;
  159.         tm.tm_mday = exp_date->da_day;
  160.         tm.tm_mon = exp_date->da_mon - 1;
  161.  
  162.         /* get current time */
  163.         gettime(exp_time);
  164.         tm.tm_hour = exp_time->ti_hour;
  165.         /* if we're already past the minute, do it next hour ! */
  166.         if(exp_time->ti_min > tm.tm_min)
  167.             tm.tm_hour++;
  168.  
  169.         /* now adjust this for day boundaries, etc. */
  170.         tm.tm_sec = 0;
  171.         tm.tm_isdst = 0;
  172.         time1 = mktime(&tm);
  173.         time(&nowtime);
  174.         break;
  175.  
  176.        case 8:    /* now+hhmm given */
  177.         strncpy(cp,argv[1],4);
  178.         cp[4]='\0';
  179.         if(strcmp(cp,"now+") != 0) goto error;
  180.  
  181.         cp[0]=argv[1][4];
  182.         cp[1]=argv[1][5];
  183.         cp[2]='\0';
  184.  
  185.         time1=(unsigned long)atoi(cp)*3600L;
  186.  
  187.         cp[0]=argv[1][6];
  188.         cp[1]=argv[1][7];
  189.         cp[2]='\0';
  190.  
  191.         time1+=(unsigned long)atoi(cp)*60L;
  192.         time(&nowtime);
  193.         time1+=(unsigned long)nowtime;
  194.         break;
  195.  
  196.        default:
  197. error:        tprintf(Errmsg);
  198.         free(exp_date);
  199.         free(exp_time);
  200.         free(cp);
  201.         return 0;
  202.  
  203.     } /* switch */
  204.  
  205.     free(cp);
  206.     free(exp_time);
  207.     free(exp_date);
  208.  
  209.     t=(struct timer *)mallocw(sizeof(struct timer));
  210.  
  211.     set_timer(t,(time1 - (unsigned long)nowtime) * 1000L);
  212.     t->state=TIMER_RUN;
  213.     t->func=(void (*)())atcmd;
  214.     t->arg=(char *)mallocw(strlen(argv[2])+2);
  215.     strcpy(t->arg,argv[2]);
  216.     start_timer(t);
  217.  
  218.     /* Add the new timer to the head of List Of Events */
  219.     loe=(struct at_list *)mallocw(sizeof(struct at_list));
  220.     loe->at_timer=t;
  221.     loe->next=Head_loe;
  222.     Head_loe=loe;
  223.  
  224.     return 0;
  225. }
  226.  
  227. /* Function to be called on timer expiration to execute a command */
  228. void
  229. atcmd(command)
  230. char *command;
  231. {
  232.     extern struct cmds far Cmds[];
  233.     struct at_list *loe, *p;
  234.  
  235.     log(-1,"AT command: %s",command);
  236.  
  237.     /* Free up memory for expired at commands */
  238.     p=Head_loe;
  239.     loe=Head_loe;
  240.     while(loe != NULLATLIST){
  241.         if(loe->at_timer->state == TIMER_EXPIRE){
  242.             free(loe->at_timer);    /* Free timer */
  243.             if(loe == Head_loe){
  244.                 Head_loe=loe->next;
  245.                 p=loe->next;
  246.                 free(loe);
  247.                 loe=p;
  248.                 p=Head_loe;
  249.             } else {
  250.                 p->next=loe->next;
  251.                 free(loe);
  252.                 loe=p->next;
  253.             }
  254.         } else {    /* Not expired, go on */
  255.             if(loe != Head_loe) p=p->next;
  256.             loe=loe->next;
  257.         }
  258.     }
  259.     cmdparse(Cmds,command,NULL);    /* Go with requested command */
  260.     free(command);
  261. }
  262.