home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2111 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  8.2 KB

  1. From: dfs@doe.carleton.ca (David F. Skoll)
  2. Newsgroups: alt.sources
  3. Subject: REMIND 2.2 05/05
  4. Message-ID: <dfs.658781337@yar>
  5. Date: 16 Nov 90 18:48:57 GMT
  6.  
  7.  
  8. #!/bin/sh
  9. # This is part 05 of Remind-2.2
  10. if touch 2>&1 | fgrep 'amc' > /dev/null
  11.  then TOUCH=touch
  12.  else TOUCH=true
  13. fi
  14. # ============= timed.c ==============
  15. if test X"$1" != X"-c" -a -f 'timed.c'; then
  16.     echo "File already exists: skipping 'timed.c'"
  17. else
  18. echo "x - extracting timed.c (Text)"
  19. sed 's/^X//' << 'SHAR_EOF' > timed.c &&
  20. X#include <stdio.h>
  21. X#include <signal.h>
  22. X#include <malloc.h>
  23. X#include "defines.h"
  24. X#include "globals.h"
  25. X#include "protos.h"
  26. X
  27. X/***************************************************************/
  28. X/*                                                             */
  29. X/*  TIMED.C                                                    */
  30. X/*                                                             */
  31. X/*  Contains routines for triggering timed reminders           */
  32. X/*                                                             */
  33. X/*  By David Skoll - 12 Nov 1990                               */
  34. X/*                                                             */
  35. X/*  (C) 1990 by David Skoll                                    */
  36. X/*                                                             */
  37. X/***************************************************************/
  38. X
  39. X/* Global pointer to AT reminders */
  40. Xstatic AtEntry AtQueue =
  41. X{
  42. X   0, 0, 0, 0, 0, NULL, NULL
  43. X};
  44. X
  45. X/***************************************************************/
  46. X/*                                                             */
  47. X/*  AddEntry                                                   */
  48. X/*                                                             */
  49. X/*  Add an entry to the AT queue, keeping things sorted by     */
  50. X/*  trigger time.                                              */
  51. X/*                                                             */
  52. X/*  Returns 0 for success, nonzero for failure                 */
  53. X/*                                                             */
  54. X/***************************************************************/
  55. Xint AddEntry(e)
  56. XAtEntry *e;
  57. X{
  58. X   AtEntry *current, *prev;
  59. X   prev = &AtQueue;
  60. X   current = prev->next;
  61. X   while (current) {
  62. X      if (e->firsttime < current->firsttime) {
  63. X         prev->next = e;
  64. X         e->next = current;
  65. X         break;
  66. X      } else {
  67. X         prev = current;
  68. X         current = prev->next;
  69. X      }
  70. X   }
  71. X   if (!current) {
  72. X      prev->next = e;
  73. X      e->next = NULL;
  74. X   }
  75. X}
  76. X
  77. X/***************************************************************/
  78. X/*                                                             */
  79. X/* DoAt                                                        */
  80. X/* Creates an entry for an At reminder, puts it on the queue   */
  81. X/* Updates the global variable NumAtsQueued                    */
  82. X/*                                                             */
  83. X/***************************************************************/
  84. X#ifndef UNIX
  85. Xint DoAt(int tim, int tdelta, int trep, char *body, enum Token_t type)
  86. X#else
  87. Xint DoAt(tim, tdelta, trep, body, type)
  88. Xint tim, tdelta, trep;
  89. Xchar *body;
  90. Xenum Token_t type;
  91. X#endif
  92. X{
  93. X   AtEntry *e;
  94. X   int curtime;
  95. X
  96. X   curtime = (int) (SystemTime() / 60);
  97. X   
  98. X   /* If the trigger time is in the past, don't add to the at queue */     
  99. X   if (tim < curtime) return 0;
  100. X
  101. X   /* Allocate space for the entry */
  102. X   e = (AtEntry *) malloc(sizeof(AtEntry));
  103. X   if (e == (AtEntry *) NULL) {
  104. X      Eprint("Can't malloc memory for AT!\n");
  105. X      return 1;
  106. X   }
  107. X   e->text = malloc(strlen(body)+1);
  108. X   if (e->text == NULL) {
  109. X      Eprint("Can't malloc memory for body of AT!\n");
  110. X      return 1;
  111. X   }
  112. X   strcpy(e->text, body);
  113. X
  114. X   /* Find out the next trigger time */
  115. X   e->firsttime = FindNextTriggerTime(tim, trep, tdelta, curtime);
  116. X   e->repeat    = trep;
  117. X   e->type      = type;
  118. X   e->time      = tim;
  119. X   e->delta     = tdelta;
  120. X   AddEntry(e);
  121. X   NumAtsQueued++;
  122. X   return 0;
  123. X}
  124. X
  125. X/***************************************************************/
  126. X/*                                                             */
  127. X/* int FindNextTriggerTime                                     */
  128. X/*                                                             */
  129. X/* Returns the next time a queued AT should be triggered.      */
  130. X/* Returns -1 if the AT has expired.                           */
  131. X/*                                                             */
  132. X/***************************************************************/
  133. X#ifndef UNIX
  134. Xint FindNextTriggerTime(int tim, int rep, int delta, int curtime)
  135. X#else
  136. Xint FindNextTriggerTime(tim, rep, delta, curtime)
  137. Xint tim, rep, delta, curtime;
  138. X#endif
  139. X{
  140. X   int trigger = tim;
  141. X   
  142. X   if (delta <= 0)
  143. X      if (trigger < curtime) return -1; else return trigger;
  144. X   
  145. X   trigger -= delta;
  146. X   if (rep == -1) rep = delta;
  147. X
  148. X   if (trigger < curtime) trigger += ((curtime - trigger) / rep) * rep;
  149. X   if (trigger < curtime) trigger += rep;
  150. X   if (trigger > tim) trigger = tim;
  151. X   if (trigger < curtime) return -1; else return trigger;
  152. X}
  153. X
  154. X/***************************************************************/
  155. X/*                                                             */
  156. X/* HandleQueuedAts                                             */
  157. X/*                                                             */
  158. X/* Handles all the queued AT reminders.  Sits in a sleep loop  */
  159. X/* to trigger reminders.                                       */
  160. X/*                                                             */
  161. X/***************************************************************/
  162. Xvoid HandleQueuedAts()
  163. X{
  164. X   AtEntry *e;
  165. X   long TimeToSleep;
  166. X   unsigned SleepTime;
  167. X   long now;
  168. X
  169. X   signal(SIGHUP, SigHupHandler);
  170. X
  171. X   while (e = AtQueue.next) {
  172. X      /* Check if the user has logged off.  If so, die gracefully. */
  173. X      if (!isatty(1)) {
  174. X        exit(0);
  175. X      }
  176. X      now = SystemTime();
  177. X      TimeToSleep = (long) e->firsttime * 60L - now;
  178. X      if (TimeToSleep < 0L) TimeToSleep = 0L;
  179. X
  180. X      /* Be paranoid and assume that unsigneds are only two bytes long -
  181. X         therefore, do the sleeping in 30000-second chunks. */  
  182. X     
  183. X      while (TimeToSleep) {
  184. X         if (TimeToSleep > 30000L) SleepTime = 30000;
  185. X                              else SleepTime = (unsigned) TimeToSleep;
  186. X         sleep(SleepTime);
  187. X         TimeToSleep -= (long) SleepTime;
  188. X      }
  189. X
  190. X      /* Over here, we trigger the reminder */
  191. X      DoSubst(e->text, WorkBuf, CurDay, CurMon, CurYear, JulianToday, e->type, e->time);
  192. X      if (e->type == Run_t) system(WorkBuf);
  193. X      else printf("%s\n", WorkBuf);
  194. X
  195. X      /* Remove the entry from the queue */
  196. X      AtQueue.next = e->next;
  197. X
  198. X      /* Check if this reminder should be re-triggered */
  199. X      e->firsttime = FindNextTriggerTime(e->time, e->repeat, 
  200. X                                        e->delta, e->firsttime + 1);
  201. X
  202. X      if (e->firsttime != -1) AddEntry(e);
  203. X      else {
  204. X      /* Not to be added - free the memory */
  205. X         free(e->text);
  206. X         free(e);
  207. X      }
  208. X   }
  209. X}
  210. X
  211. X/***************************************************************/
  212. X/*                                                             */
  213. X/* SigHupHandler                                               */
  214. X/*                                                             */
  215. X/* For debugging purposes, when sent a HUP, we print the       */
  216. X/* contents of the queue.                                      */
  217. X/*                                                             */
  218. X/***************************************************************/
  219. Xvoid SigHupHandler()
  220. X{
  221. X   AtEntry *e;
  222. X
  223. X   printf("Contents of AT queue:\n");
  224. X
  225. X   e = AtQueue.next;
  226. X   while (e) {
  227. X      printf("Trigger: %02d:%02d  Activate: %02d:%02d  Rep: %d  Delta: %d\n",
  228. X              e->time / 60, e->time % 60, e->firsttime / 60, e->firsttime % 60,
  229. X              e->repeat, e->delta);
  230. X      printf("Text: %s %s\n\n", ((e->type == Msg_t) ? "MSG" : "RUN"), e->text);
  231. X      e = e->next;
  232. X   }
  233. X   printf("\n");
  234. X}
  235. SHAR_EOF
  236. $TOUCH -am 1115093590 timed.c &&
  237. chmod 0600 timed.c ||
  238. echo "restore of timed.c failed"
  239. set `wc -c timed.c`;Wc_c=$1
  240. if test "$Wc_c" != "7436"; then
  241.     echo original size 7436, current size $Wc_c
  242. fi
  243. fi
  244. exit 0
  245.