/contrib/tcsh/tc.sched.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 258 lines · 184 code · 20 blank · 54 comment · 63 complexity · bc3c1a732a7213ee19c9ebe02561669b MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/tc.sched.c,v 3.25 2006/03/02 18:46:45 christos Exp $ */
  2. /*
  3. * tc.sched.c: Scheduled command execution
  4. *
  5. * Karl Kleinpaste: Computer Consoles Inc. 1984
  6. */
  7. /*-
  8. * Copyright (c) 1980, 1991 The Regents of the University of California.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. #include "sh.h"
  36. RCSID("$tcsh: tc.sched.c,v 3.25 2006/03/02 18:46:45 christos Exp $")
  37. #include "ed.h"
  38. #include "tw.h"
  39. #include "tc.h"
  40. extern int just_signaled;
  41. struct sched_event {
  42. struct sched_event *t_next;
  43. time_t t_when;
  44. Char **t_lex;
  45. };
  46. static struct sched_event *sched_ptr = NULL;
  47. time_t
  48. sched_next(void)
  49. {
  50. if (sched_ptr)
  51. return (sched_ptr->t_when);
  52. return ((time_t) - 1);
  53. }
  54. /*ARGSUSED*/
  55. void
  56. dosched(Char **v, struct command *c)
  57. {
  58. struct sched_event *tp, **pp;
  59. time_t cur_time;
  60. int count, hours, minutes, dif_hour, dif_min;
  61. Char *cp;
  62. int relative; /* time specified as +hh:mm */
  63. struct tm *ltp;
  64. USE(c);
  65. /* This is a major kludge because of a gcc linker */
  66. /* Problem. It may or may not be needed for you */
  67. #if defined(_MINIX) && !defined(_MINIX_VMD)
  68. char kludge[10];
  69. extern char *sprintf();
  70. sprintf(kludge, CGETS(24, 1, "kludge"));
  71. #endif /* _MINIX && !_MINIX_VMD */
  72. v++;
  73. cp = *v++;
  74. if (cp == NULL) {
  75. const Char *fmt;
  76. if ((fmt = varval(STRsched)) == STRNULL)
  77. fmt = str2short("%h\t%T\t%R\n");
  78. /* print list of scheduled events */
  79. for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
  80. Char *buf, *str;
  81. buf = blkexpand(tp->t_lex);
  82. cleanup_push(buf, xfree);
  83. str = tprintf(FMT_SCHED, fmt, short2str(buf), tp->t_when, &count);
  84. cleanup_until(buf);
  85. cleanup_push(str, xfree);
  86. for (cp = str; *cp;)
  87. xputwchar(*cp++);
  88. cleanup_until(str);
  89. }
  90. return;
  91. }
  92. if (*cp == '-') {
  93. /* remove item from list */
  94. if (!sched_ptr)
  95. stderror(ERR_NOSCHED);
  96. if (*v)
  97. stderror(ERR_SCHEDUSAGE);
  98. count = atoi(short2str(++cp));
  99. if (count <= 0)
  100. stderror(ERR_SCHEDUSAGE);
  101. pp = &sched_ptr;
  102. tp = sched_ptr;
  103. while (--count) {
  104. if (tp->t_next == 0)
  105. break;
  106. else {
  107. pp = &tp->t_next;
  108. tp = tp->t_next;
  109. }
  110. }
  111. if (count)
  112. stderror(ERR_SCHEDEV);
  113. *pp = tp->t_next;
  114. blkfree(tp->t_lex);
  115. xfree(tp);
  116. return;
  117. }
  118. /* else, add an item to the list */
  119. if (!*v)
  120. stderror(ERR_SCHEDCOM);
  121. relative = 0;
  122. if (!Isdigit(*cp)) { /* not abs. time */
  123. if (*cp != '+')
  124. stderror(ERR_SCHEDUSAGE);
  125. cp++, relative++;
  126. }
  127. minutes = 0;
  128. hours = atoi(short2str(cp));
  129. while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p')
  130. cp++;
  131. if (*cp && *cp == ':')
  132. minutes = atoi(short2str(++cp));
  133. if ((hours < 0) || (minutes < 0) ||
  134. (hours > 23) || (minutes > 59))
  135. stderror(ERR_SCHEDTIME);
  136. while (*cp && *cp != 'p' && *cp != 'a')
  137. cp++;
  138. if (*cp && relative)
  139. stderror(ERR_SCHEDREL);
  140. if (*cp == 'p')
  141. hours += 12;
  142. (void) time(&cur_time);
  143. ltp = localtime(&cur_time);
  144. if (relative) {
  145. dif_hour = hours;
  146. dif_min = minutes;
  147. }
  148. else {
  149. if ((dif_hour = hours - ltp->tm_hour) < 0)
  150. dif_hour += 24;
  151. if ((dif_min = minutes - ltp->tm_min) < 0) {
  152. dif_min += 60;
  153. if ((--dif_hour) < 0)
  154. dif_hour = 23;
  155. }
  156. }
  157. tp = xcalloc(1, sizeof *tp);
  158. #ifdef _SX
  159. tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60;
  160. #else /* _SX */
  161. tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
  162. #endif /* _SX */
  163. /* use of tm_sec: get to beginning of minute. */
  164. for (pp = &sched_ptr; *pp != NULL && tp->t_when >= (*pp)->t_when;
  165. pp = &(*pp)->t_next)
  166. ;
  167. tp->t_next = *pp;
  168. *pp = tp;
  169. tp->t_lex = saveblk(v);
  170. }
  171. /*
  172. * Execute scheduled events
  173. */
  174. void
  175. sched_run(void)
  176. {
  177. time_t cur_time;
  178. struct sched_event *tp;
  179. struct wordent cmd, *nextword, *lastword;
  180. struct command *t;
  181. Char **v, *cp;
  182. pintr_disabled++;
  183. cleanup_push(&pintr_disabled, disabled_cleanup);
  184. (void) time(&cur_time);
  185. /* bugfix by: Justin Bur at Universite de Montreal */
  186. /*
  187. * this test wouldn't be necessary if this routine were not called before
  188. * each prompt (in sh.c). But it is, to catch missed alarms. Someone
  189. * ought to fix it all up. -jbb
  190. */
  191. if (!(sched_ptr && sched_ptr->t_when < cur_time)) {
  192. cleanup_until(&pintr_disabled);
  193. return;
  194. }
  195. if (GettingInput)
  196. (void) Cookedmode();
  197. while ((tp = sched_ptr) != NULL && tp->t_when < cur_time) {
  198. if (seterr) {
  199. xfree(seterr);
  200. seterr = NULL;
  201. }
  202. cmd.word = STRNULL;
  203. lastword = &cmd;
  204. v = tp->t_lex;
  205. for (cp = *v; cp; cp = *++v) {
  206. nextword = xcalloc(1, sizeof cmd);
  207. nextword->word = Strsave(cp);
  208. lastword->next = nextword;
  209. nextword->prev = lastword;
  210. lastword = nextword;
  211. }
  212. lastword->next = &cmd;
  213. cmd.prev = lastword;
  214. sched_ptr = tp->t_next; /* looping termination cond: */
  215. blkfree(tp->t_lex); /* straighten out in case of */
  216. xfree(tp); /* command blow-up. */
  217. cleanup_push(&cmd, lex_cleanup);
  218. /* expand aliases like process() does. */
  219. alias(&cmd);
  220. /* build a syntax tree for the command. */
  221. t = syntax(cmd.next, &cmd, 0);
  222. cleanup_push(t, syntax_cleanup);
  223. if (seterr)
  224. stderror(ERR_OLD);
  225. /* execute the parse tree. */
  226. execute(t, -1, NULL, NULL, TRUE);
  227. /* done. free the lex list and parse tree. */
  228. cleanup_until(&cmd);
  229. }
  230. if (GettingInput && !just_signaled) { /* PWP */
  231. (void) Rawmode();
  232. ClearLines(); /* do a real refresh since something may */
  233. ClearDisp(); /* have printed to the screen */
  234. Refresh();
  235. }
  236. just_signaled = 0;
  237. cleanup_until(&pintr_disabled);
  238. }