home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************/
- /* */
- /* SORT.C */
- /* */
- /* Routines for sorting reminders by trigger date */
- /* */
- /* This file is part of REMIND. */
- /* Copyright (C) 1992, 1993 by David F. Skoll. */
- /* */
- /***************************************************************/
- #include "config.h"
-
- #ifdef HAVE_STDLIB_H
- #include <stdlib.h>
- #endif
-
- #ifdef HAVE_MALLOC_H
- #include <malloc.h>
- #endif
-
- #include <stdio.h>
- #include <string.h>
- #include "types.h"
- #include "protos.h"
- #include "expr.h"
- #include "globals.h"
- #include "err.h"
-
- /* The structure of a sorted entry */
- typedef struct sortrem {
- struct sortrem *next;
- char *text;
- int trigdate;
- int trigtime;
- int typ;
- } Sortrem;
-
- /* The sorted reminder queue */
- static Sortrem *SortedQueue = (Sortrem *) NULL;
-
- PRIVATE Sortrem *MakeSortRem ARGS ((int jul, int tim, char *body, int typ));
- PRIVATE void IssueSortBanner ARGS ((int jul));
-
- /***************************************************************/
- /* */
- /* MakeSortRem */
- /* */
- /* Create a new Sortrem entry - return NULL on failure. */
- /* */
- /***************************************************************/
- #ifdef HAVE_PROTOS
- PRIVATE Sortrem *MakeSortRem(int jul, int tim, char *body, int typ)
- #else
- static Sortrem *MakeSortRem(jul, tim, body, typ)
- int jul, tim;
- char *body;
- int typ;
- #endif
- {
- Sortrem *new = NEW(Sortrem);
- if (!new) return NULL;
-
- new->text = StrDup(body);
- if (!new->text) {
- free(new);
- return NULL;
- }
-
- new->trigdate = jul;
- new->trigtime = tim;
- new->typ = typ;
- new->next = NULL;
- return new;
- }
-
- /***************************************************************/
- /* */
- /* InsertIntoSortBuffer */
- /* */
- /* Insert a reminder into the sort buffer */
- /* */
- /***************************************************************/
- #ifdef HAVE_PROTOS
- PUBLIC int InsertIntoSortBuffer(int jul, int tim, char *body, int typ)
- #else
- int InsertIntoSortBuffer(jul, tim, body, typ)
- int jul;
- int tim;
- char *body;
- int typ;
- #endif
- {
- Sortrem *new = MakeSortRem(jul, tim, body, typ);
- Sortrem *cur = SortedQueue, *prev = NULL;
- int ShouldGoAfter;
-
- if (!new) {
- Eprint("Out of memory for sorting.");
- IssueSortedReminders();
- SortByDate = 0;
- SortByTime = 0;
- return E_NO_MEM;
- }
-
- /* Find the correct place in the sorted list */
- if (!SortedQueue) {
- SortedQueue = new;
- return OK;
- }
- while (cur) {
- if (cur->trigdate == new->trigdate) {
- ShouldGoAfter =
- (SortByTime == SORT_ASCEND) ?
- (new->trigtime >= cur->trigtime) :
- ((new->trigtime == NO_TIME) || (new->trigtime <= cur->trigtime));
- } else {
- ShouldGoAfter =
- (SortByDate == SORT_ASCEND) ?
- (new->trigdate >= cur->trigdate) :
- (new->trigdate <= cur->trigdate);
- }
-
- if (ShouldGoAfter) {
- prev = cur;
- cur = cur->next;
- } else {
- if (prev) {
- prev->next = new;
- new->next = cur;
- } else {
- SortedQueue = new;
- new->next = cur;
- }
- return OK;
- }
-
- }
- prev->next = new;
- new->next = cur; /* For safety - actually redundant */
- return OK;
- }
-
-
- /***************************************************************/
- /* */
- /* IssueSortedReminders */
- /* */
- /* Issue all of the sorted reminders and free memory. */
- /* */
- /***************************************************************/
- #ifdef HAVE_PROTOS
- PUBLIC void IssueSortedReminders(void)
- #else
- void IssueSortedReminders()
- #endif
- {
- Sortrem *cur = SortedQueue;
- Sortrem *next;
- int olddate = NO_DATE;
-
- while (cur) {
- next = cur->next;
- if (cur->typ == MSG_TYPE) {
- if (!MsgCommand) {
- if (cur->trigdate != olddate) {
- IssueSortBanner(cur->trigdate);
- olddate = cur->trigdate;
- }
- printf("%s\n", cur->text);
- } else {
- char buf[LINELEN+TOKSIZE];
- sprintf(buf, MsgCommand, cur->text);
- system(buf);
- }
- } else { /* Must be RUN_TYPE */
- system(cur->text);
- }
- free(cur->text);
- free(cur);
- cur = next;
- }
- }
- /***************************************************************/
- /* */
- /* IssueSortBanner */
- /* */
- /* Issue a daily banner if the function sortbanner() is */
- /* defined to take one argument. */
- /* */
- /***************************************************************/
- #ifdef HAVE_PROTOS
- PRIVATE void IssueSortBanner(int jul)
- #else
- static void IssueSortBanner(jul)
- int jul;
- #endif
- {
- char BanExpr[25];
- int y, m, d;
- Value v;
- char *s = BanExpr;
-
- if (UserFuncExists("sortbanner") != 1) return;
-
- FromJulian(jul, &y, &m, &d);
- sprintf(BanExpr, "sortbanner('%04d/%02d/%02d')", y, m+1, d);
- y = EvalExpr(&s, &v);
- if (y) return;
- if (DoCoerce(STR_TYPE, &v)) return;
- if (!DoSubstFromString(v.v.str, SubstBuffer, jul, NO_TIME))
- if (*SubstBuffer) printf("%s\n", SubstBuffer);
- DestroyValue(&v);
- }
-