home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
pc
/
source
/
remind23.lzh
/
remind.3
/
timed.c
< prev
Wrap
C/C++ Source or Header
|
1991-02-24
|
7KB
|
224 lines
#include <stdio.h>
#include <signal.h>
#include <string.h>
#ifndef NO_MALLOC_H
#include <malloc.h>
#endif
#include "defines.h"
#include "globals.h"
#include "protos.h"
/***************************************************************/
/* */
/* TIMED.C */
/* */
/* Contains routines for triggering timed reminders */
/* */
/* By David Skoll - 12 Nov 1990 */
/* */
/* (C) 1990 by David Skoll */
/* */
/***************************************************************/
/* Global pointer to AT reminders */
static AtEntry AtQueue =
{
0, 0, 0, 0, Unknown_t, NULL, NULL
};
/***************************************************************/
/* */
/* AddEntry */
/* */
/* Add an entry to the AT queue, keeping things sorted by */
/* trigger time. */
/* */
/* Returns 0 for success, nonzero for failure */
/* */
/***************************************************************/
#ifdef __STDC__
int AddEntry(AtEntry *e)
#else
int AddEntry(e)
AtEntry *e;
#endif
{
AtEntry *current, *prev;
prev = &AtQueue;
current = prev->next;
while (current) {
if (e->firsttime < current->firsttime) {
prev->next = e;
e->next = current;
break;
} else {
prev = current;
current = prev->next;
}
}
if (!current) {
prev->next = e;
e->next = NULL;
}
}
/***************************************************************/
/* */
/* DoAt */
/* Creates an entry for an At reminder, puts it on the queue */
/* Updates the global variable NumAtsQueued */
/* */
/***************************************************************/
#ifdef __STDC__
int DoAt(int tim, int tdelta, int trep, char *body, enum Token_t type)
#else
int DoAt(tim, tdelta, trep, body, type)
int tim, tdelta, trep;
char *body;
enum Token_t type;
#endif
{
AtEntry *e;
int curtime;
curtime = (int) (SystemTime() / 60);
/* If the trigger time is in the past, don't add to the at queue */
if (tim < curtime) return 0;
/* Allocate space for the entry */
e = (AtEntry *) malloc(sizeof(AtEntry));
if (e == (AtEntry *) NULL) {
Eprint("Can't malloc memory for AT!\n");
return 1;
}
e->text = malloc(strlen(body)+1);
if (e->text == NULL) {
Eprint("Can't malloc memory for body of AT!\n");
return 1;
}
strcpy(e->text, body);
/* Find out the next trigger time */
e->firsttime = FindNextTriggerTime(tim, trep, tdelta, curtime);
e->repeat = trep;
e->type = type;
e->time = tim;
e->delta = tdelta;
AddEntry(e);
NumAtsQueued++;
return 0;
}
/***************************************************************/
/* */
/* int FindNextTriggerTime */
/* */
/* Returns the next time a queued AT should be triggered. */
/* Returns -1 if the AT has expired. */
/* */
/***************************************************************/
#ifdef __STDC__
int FindNextTriggerTime(int tim, int rep, int delta, int curtime)
#else
int FindNextTriggerTime(tim, rep, delta, curtime)
int tim, rep, delta, curtime;
#endif
{
int trigger = tim;
if (delta <= 0)
if (trigger < curtime) return -1; else return trigger;
trigger -= delta;
if (rep == -1) rep = delta;
if (trigger < curtime) trigger += ((curtime - trigger) / rep) * rep;
if (trigger < curtime) trigger += rep;
if (trigger > tim) trigger = tim;
if (trigger < curtime) return -1; else return trigger;
}
/***************************************************************/
/* */
/* HandleQueuedAts */
/* */
/* Handles all the queued AT reminders. Sits in a sleep loop */
/* to trigger reminders. */
/* */
/***************************************************************/
#ifdef __STDC__
void HandleQueuedAts(void)
#else
void HandleQueuedAts()
#endif
{
AtEntry *e;
long TimeToSleep;
unsigned SleepTime;
long now;
signal(SIGINT, SigIntHandler);
while (e = AtQueue.next) {
now = SystemTime();
TimeToSleep = (long) e->firsttime * 60L - now;
if (TimeToSleep < 0L) TimeToSleep = 0L;
/* Be paranoid and assume that unsigneds are only two bytes long -
therefore, do the sleeping in 30000-second chunks. */
while (TimeToSleep) {
if (TimeToSleep > 30000L) SleepTime = 30000;
else SleepTime = (unsigned) TimeToSleep;
sleep(SleepTime);
TimeToSleep -= (long) SleepTime;
}
/* Over here, we trigger the reminder */
DoSubst(e->text, WorkBuf, CurDay, CurMon, CurYear, JulianToday, e->type, e->time, 0);
if (e->type == Run_t) system(WorkBuf);
else printf("%s\n", WorkBuf);
/* Remove the entry from the queue */
AtQueue.next = e->next;
/* Check if this reminder should be re-triggered */
e->firsttime = FindNextTriggerTime(e->time, e->repeat,
e->delta, e->firsttime + 1);
if (e->firsttime != -1) AddEntry(e);
else {
/* Not to be added - free the memory */
free(e->text);
free(e);
}
}
}
/***************************************************************/
/* */
/* SigIntHandler */
/* */
/* For debugging purposes, when sent a SIGINT, we print the */
/* contents of the queue. */
/* */
/***************************************************************/
void SigIntHandler()
{
AtEntry *e;
printf("Contents of AT queue:\n");
e = AtQueue.next;
while (e) {
printf("Trigger: %02d:%02d Activate: %02d:%02d Rep: %d Delta: %d\n",
e->time / 60, e->time % 60, e->firsttime / 60, e->firsttime % 60,
e->repeat, e->delta);
printf("Text: %s %s\n\n", ((e->type == Msg_t) ? "MSG" : "RUN"), e->text);
e = e->next;
}
printf("\n");
}