home *** CD-ROM | disk | FTP | other *** search
- /*
- Module name.
- resclok2.c - display clock w/optional timer
-
- -) Functional description.
- This program will display a clock in the upper right corner
- of the screen and optionally accept a time and DOS command
- as an argument. At the specified time the command is passed
- to DOS and the TSR is terminated and freed from memory. The
- command length is limited to 15 characters, so batch files
- should be used to execute more complex command line
- requirments
-
- --) NOTICE: Copyright (C) 1990 South Mountain Software, Inc.
- */
-
- #include "tsr.h"
- #include "resproto.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "conio.h"
- #include "string.h"
- #include "process.h"
- #include "dos.h"
-
- #define TRUE 1
- #define FALSE 0
- #define SCAN_Q 16 /* scan code of Q key */
-
- #define ROW 0
- #define COL 65
- #define ATTR 0x70 /* black on white */
-
- /* int 16h unique function we use to see if loaded */
- unsigned int myfunc = 0x7274;
-
- int hr,min,sec,hd;
- int schr, schm; /* scheduled time vars */
- int cmdsize;
- char cmd[129] = "";
- unsigned char stop = 0;
-
- extern unsigned kb_in_progress; /* for use with kb_init */
- extern char *kb_str_ptr; /* for use with kb_init */
-
- void clock(void);
- void keystuff(char *str);
- void display(char *str);
- void isrpre(void);
- int tim_pars(char *str);
-
- void main(argc,argv)
- int argc;
- char **argv;
- {
- void clock();
- int add_arg; /* counter for command line arguments */
-
- /* process timed command request */
- if(argc > 2)
- {
- tim_pars(argv[1]); /* parse the time into schr & schm */
-
- add_arg = 2;
- while(argc-- > 2) { /* add all arguments to command line */
- strcat(cmd, argv[add_arg++]);
- strcat(cmd," ");
- }
- }
-
- if (tsrloaded(myfunc) == FALSE)
- {
- inittsr2(CTRL_KEY | ALT_KEY,SCAN_Q, clock, 30, myfunc, SIG,(void far *)NULL);
- }
- else
- {
- printf("RESCLOK2 Already Loaded!\n");
- exit(1);
- }
- }
-
-
- static int cnt = 99; /* initialize second comparison variable */
-
- void clock()
- {
- char curtime[15]; /* buffer to hold time */
- char temp_ptr[17]; /* temporary buffer for itoa() */
- union REGS regs; /* register structure for dos call */
- extern int _hotkey_hit; /* set to 1 if hotkey was hit */
-
-
- if(_hotkey_hit){
- if(freetsr() == 1){
- puts("Not Loaded Last! - TSR Will Still Be Loaded!");
- _hotkey_hit = 0; /* reset hotkey flag */
- }
- }
-
-
- regs.h.ah = 0x2c; /* get system time with DOS call */
- intdos(®s,®s);
- sec = regs.h.dh;
-
- /* we'll only update the clock display when sec changes */
- if(cnt != sec) {
- cnt = sec;
- hr = regs.h.ch;
- min = regs.h.cl;
-
- /* cannot use sprintf because it was compiled with
- stack checking on. */
- /* sprintf(curtime," %2d:%2d:%2d ",hr,min,sec); */
-
- /* do it the hard way with itoa */
- curtime[0] = ' ';
- curtime[1] = '\0';
- itoa(hr,temp_ptr,10);
- if(strlen(temp_ptr) == 1) /* force two digits for each number */
- strcat(curtime,"0");
- strcat(curtime,temp_ptr);
- strcat(curtime,":");
- itoa(min,temp_ptr,10);
- if(strlen(temp_ptr) == 1) /* force two digits for each number */
- strcat(curtime,"0");
- strcat(curtime,temp_ptr);
- strcat(curtime,":");
- itoa(sec,temp_ptr,10);
- if(strlen(temp_ptr) == 1) /* force two digits for each number */
- strcat(curtime,"0");
- strcat(curtime,temp_ptr);
- strcat(curtime," ");
-
- display(curtime); /* put the time on the screen */
- }
-
- /* process command if one was requested */
- if (!stop)
- if(cmdsize && hr == schr && min == schm)
- {
- if (if_last() == 0) /* free ISR when done */
- {
- stop = 1;
- puts("Not Loaded Last! - TSR Will Still Be Loaded!");
- }
- else
- if(cmdsize)
- {
- /* The following technique limits keyboard
- stuffing to 15 characters (the size of the
- keyboard buffer) so that the TSR can free
- itself before executing the command. If the
- TSR remained in memory, and it wanted to stuff
- more than 15 keys, it would have to return
- control to DOS to read in the keys. Otherwise,
- the while(kb_in_progress) would never end
- because no one would be reading in the stuffed
- keys.
- */
-
- freetsr();
- while(kbhit()) /* clear keyboard buffer */
- getch();
- kb_init();
- kb_str_ptr = cmd;
- kb_in_progress = cmdsize; /* start stuffing keyboard */
- while(kb_in_progress) ; /* wait till all keys stuffed */
- kb_close();
- return; /* return to DOS to execute */
- }
- }
- }
-
- /* Interleave atribute and display time */
- void display(str)
- char *str;
- {
- register char *curptr; /* used to steo thru buffer */
- char buf[31]; /* actual buffer to write to screen */
-
- curptr = buf;
-
- /* merge char and attribute for screen format */
- while(*str) {
- *curptr++ = *str++;
- *curptr++ = ATTR;
- }
-
- /* do the actual display */
- memtoscr(10, (ROW * 160) + (COL * 2), buf);
- return;
- }
-
- /*
- This function takes the time in HH:MM format and places it into
- two int variables for hour and minute.
- */
- int tim_pars(str)
- char *str;
- {
- char *ptr; /* used to step thru str */
-
- for(ptr = str; *ptr != ':' && *ptr; ptr++);
-
- *ptr++ = '\0'; /* split hour and point to minute */
- schr = atoi(str);
- schm = atoi(ptr);
-
- return(0);
- }
-
- void tsrpre()
- {
- printf("\nRESCLOK2 By South Mountain Software\n\n");
- if((cmdsize = strlen(cmd)) > 0) /* verify command and time */
- {
- if(cmdsize <= 13)
- {
- printf("At %02d:%02d Execute %s\n", schr, schm, cmd);
- /* add CR and LF to command, write over last blank in cmd */
- cmd[cmdsize] = 0xd;
- cmd[cmdsize+1] = 0xa;
- cmdsize += 1;
- }
- else
- {
- putch(7);
- printf("Command length exceeds max of 13 characters!\n");
- printf("Try using a batch file to work around this.\n");
- }
- }
- printf("\nYou can hit Ctrl-Alt-Q to unload the TSR\n");
- }