home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 267.img / RESIDNTC.ZIP / DEMOS.ZIP / RESCLOK2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-13  |  6.8 KB  |  234 lines

  1. /*
  2.       Module name.
  3.         resclok2.c - display clock w/optional timer
  4.   
  5. -)    Functional description.
  6.          This program will display a clock in the upper right corner
  7.          of the screen and optionally accept a time and DOS command
  8.          as an argument.  At the specified time the command is passed
  9.          to DOS and the TSR is terminated and freed from memory.  The
  10.          command length is limited to 15 characters, so batch files
  11.          should be used to execute more complex command line
  12.          requirments
  13.   
  14. --)   NOTICE:  Copyright (C) 1990 South Mountain Software, Inc.
  15. */
  16.   
  17. #include "tsr.h"
  18. #include "resproto.h"
  19. #include "stdio.h"
  20. #include "stdlib.h"
  21. #include "conio.h"
  22. #include "string.h"
  23. #include "process.h"
  24. #include "dos.h"
  25.   
  26. #define  TRUE  1
  27. #define  FALSE 0
  28. #define SCAN_Q 16   /* scan code of Q key */
  29.   
  30. #define  ROW   0
  31. #define  COL   65
  32. #define  ATTR  0x70   /* black on white */
  33.   
  34. /* int 16h unique function we use to see if loaded */
  35. unsigned int myfunc = 0x7274;
  36.   
  37. int hr,min,sec,hd;
  38. int schr, schm;       /* scheduled time vars */
  39. int  cmdsize;
  40. char cmd[129] = "";
  41. unsigned char stop = 0;
  42.   
  43. extern unsigned kb_in_progress; /* for use with kb_init */
  44. extern char *kb_str_ptr;            /* for use with kb_init */
  45.   
  46. void clock(void);
  47. void keystuff(char *str);
  48. void display(char *str);
  49. void isrpre(void);
  50. int tim_pars(char *str);
  51.   
  52. void main(argc,argv)
  53. int  argc;
  54. char **argv;
  55. {
  56. void clock();
  57. int  add_arg;      /* counter for command line arguments */
  58.   
  59.    /* process timed command request */
  60. if(argc > 2)
  61.     {
  62.     tim_pars(argv[1]);         /* parse the time into schr & schm */
  63.   
  64.     add_arg = 2;
  65.         while(argc-- > 2) {       /* add all arguments to command line */
  66.         strcat(cmd, argv[add_arg++]);
  67.         strcat(cmd," ");
  68.         }
  69.     }
  70.   
  71. if (tsrloaded(myfunc) == FALSE)
  72.     {
  73.     inittsr2(CTRL_KEY | ALT_KEY,SCAN_Q, clock, 30, myfunc, SIG,(void far *)NULL);
  74.     }
  75. else
  76.     {
  77.     printf("RESCLOK2 Already Loaded!\n");
  78.     exit(1);
  79.     }
  80. }
  81.   
  82.   
  83. static int cnt = 99;    /*   initialize second comparison variable */
  84.   
  85. void clock()
  86. {
  87. char curtime[15];          /* buffer to hold time */
  88. char temp_ptr[17];          /* temporary buffer for itoa() */
  89. union REGS regs;                /* register structure for dos call */
  90. extern int _hotkey_hit;     /* set to 1 if hotkey was hit */
  91.   
  92.   
  93.     if(_hotkey_hit){
  94.         if(freetsr() == 1){
  95.         puts("Not Loaded Last! - TSR Will Still Be Loaded!");
  96.         _hotkey_hit = 0;        /* reset hotkey flag */
  97.         }
  98.     }
  99.   
  100.   
  101. regs.h.ah = 0x2c;       /* get system time with DOS call */
  102. intdos(®s,®s);
  103. sec = regs.h.dh;
  104.   
  105.    /* we'll only update the clock display when sec changes */
  106.     if(cnt != sec) {
  107.     cnt = sec;
  108.     hr = regs.h.ch;
  109.     min = regs.h.cl;
  110.   
  111.  /*  cannot use sprintf because it was compiled with
  112.      stack checking on.                                */
  113.  /*     sprintf(curtime," %2d:%2d:%2d ",hr,min,sec); */
  114.   
  115.         /* do it the hard way with itoa */
  116.     curtime[0] = ' ';
  117.     curtime[1] = '\0';
  118.     itoa(hr,temp_ptr,10);
  119.     if(strlen(temp_ptr) == 1)   /* force two digits for each number */
  120.         strcat(curtime,"0");
  121.     strcat(curtime,temp_ptr);
  122.     strcat(curtime,":");
  123.     itoa(min,temp_ptr,10);
  124.     if(strlen(temp_ptr) == 1)   /* force two digits for each number */
  125.         strcat(curtime,"0");
  126.     strcat(curtime,temp_ptr);
  127.     strcat(curtime,":");
  128.     itoa(sec,temp_ptr,10);
  129.     if(strlen(temp_ptr) == 1)   /* force two digits for each number */
  130.         strcat(curtime,"0");
  131.     strcat(curtime,temp_ptr);
  132.     strcat(curtime," ");
  133.   
  134.     display(curtime);                /* put the time on the screen */
  135.     }
  136.   
  137.    /* process command if one was requested */
  138. if (!stop)
  139.     if(cmdsize && hr == schr && min == schm)
  140.         {
  141.         if (if_last() == 0)           /* free ISR when done */
  142.             {
  143.             stop = 1;
  144.             puts("Not Loaded Last! - TSR Will Still Be Loaded!");
  145.             }
  146.         else
  147.             if(cmdsize)
  148.                 {
  149.                                     /* The following technique limits keyboard
  150.                                         stuffing to 15 characters (the size of the
  151.                                         keyboard buffer) so that the TSR can free
  152.                                         itself before executing the command.  If the
  153.                                         TSR remained in memory, and it wanted to stuff
  154.                                         more than 15 keys, it would have to return
  155.                                         control to DOS to read in the keys.  Otherwise,
  156.                                         the while(kb_in_progress) would never end
  157.                                         because no one would be reading in the stuffed
  158.                                         keys.
  159.                                     */
  160.   
  161.                 freetsr();
  162.                 while(kbhit())  /* clear keyboard buffer */
  163.                     getch();
  164.                 kb_init();
  165.                 kb_str_ptr = cmd;
  166.                 kb_in_progress = cmdsize;   /* start stuffing keyboard */
  167.                 while(kb_in_progress) ;    /* wait till all keys stuffed */
  168.                 kb_close();
  169.                 return;                         /* return to DOS to execute */
  170.                 }
  171.         }
  172. }
  173.   
  174. /* Interleave atribute and display time */
  175. void display(str)
  176. char *str;
  177. {
  178. register char *curptr;   /* used to steo thru buffer */
  179. char buf[31];            /* actual buffer to write to screen */
  180.   
  181. curptr = buf;
  182.   
  183.    /* merge char and attribute for screen format */
  184.     while(*str) {
  185.     *curptr++ = *str++;
  186.     *curptr++ = ATTR;
  187.     }
  188.   
  189.    /* do the actual display */
  190. memtoscr(10, (ROW * 160) + (COL * 2), buf);
  191. return;
  192. }
  193.   
  194. /*
  195.    This function takes the time in HH:MM format and places it into
  196.    two int variables for hour and minute.
  197. */
  198. int tim_pars(str)
  199. char *str;
  200. {
  201. char *ptr;   /* used to step thru str */
  202.   
  203. for(ptr = str; *ptr != ':' && *ptr; ptr++);
  204.   
  205. *ptr++ = '\0';                      /* split hour and point to minute */
  206. schr = atoi(str);
  207. schm = atoi(ptr);
  208.   
  209. return(0);
  210. }
  211.   
  212. void tsrpre()
  213. {
  214. printf("\nRESCLOK2  By South Mountain Software\n\n");
  215. if((cmdsize = strlen(cmd)) > 0)      /* verify command and time */
  216.     {
  217.     if(cmdsize <= 13)
  218.         {
  219.         printf("At %02d:%02d Execute %s\n", schr, schm, cmd);
  220.                 /* add CR and LF to command, write over last blank in cmd */
  221.         cmd[cmdsize] = 0xd;
  222.         cmd[cmdsize+1] = 0xa;
  223.         cmdsize += 1;
  224.         }
  225.     else
  226.         {
  227.         putch(7);
  228.         printf("Command length exceeds max of 13 characters!\n");
  229.         printf("Try using a batch file to work around this.\n");
  230.         }
  231.     }
  232. printf("\nYou can hit Ctrl-Alt-Q to unload the TSR\n");
  233. }
  234.