1167465Smp/* $Header: /p/tcsh/cvsroot/tcsh/tc.sched.c,v 3.25 2006/03/02 18:46:45 christos Exp $ */ 259243Sobrien/* 359243Sobrien * tc.sched.c: Scheduled command execution 459243Sobrien * 559243Sobrien * Karl Kleinpaste: Computer Consoles Inc. 1984 659243Sobrien */ 759243Sobrien/*- 859243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California. 959243Sobrien * All rights reserved. 1059243Sobrien * 1159243Sobrien * Redistribution and use in source and binary forms, with or without 1259243Sobrien * modification, are permitted provided that the following conditions 1359243Sobrien * are met: 1459243Sobrien * 1. Redistributions of source code must retain the above copyright 1559243Sobrien * notice, this list of conditions and the following disclaimer. 1659243Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1759243Sobrien * notice, this list of conditions and the following disclaimer in the 1859243Sobrien * documentation and/or other materials provided with the distribution. 19100616Smp * 3. Neither the name of the University nor the names of its contributors 2059243Sobrien * may be used to endorse or promote products derived from this software 2159243Sobrien * without specific prior written permission. 2259243Sobrien * 2359243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2459243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2559243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2659243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2759243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2859243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2959243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3059243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3159243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3259243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3359243Sobrien * SUCH DAMAGE. 3459243Sobrien */ 3559243Sobrien#include "sh.h" 3659243Sobrien 37167465SmpRCSID("$tcsh: tc.sched.c,v 3.25 2006/03/02 18:46:45 christos Exp $") 3859243Sobrien 3959243Sobrien#include "ed.h" 40100616Smp#include "tw.h" 4159243Sobrien#include "tc.h" 4259243Sobrien 4359243Sobrienextern int just_signaled; 4459243Sobrien 4559243Sobrienstruct sched_event { 4659243Sobrien struct sched_event *t_next; 4759243Sobrien time_t t_when; 4859243Sobrien Char **t_lex; 4959243Sobrien}; 5059243Sobrienstatic struct sched_event *sched_ptr = NULL; 5159243Sobrien 5259243Sobrien 5359243Sobrientime_t 54167465Smpsched_next(void) 5559243Sobrien{ 5659243Sobrien if (sched_ptr) 5759243Sobrien return (sched_ptr->t_when); 5859243Sobrien return ((time_t) - 1); 5959243Sobrien} 6059243Sobrien 6159243Sobrien/*ARGSUSED*/ 6259243Sobrienvoid 63167465Smpdosched(Char **v, struct command *c) 6459243Sobrien{ 65167465Smp struct sched_event *tp, **pp; 6659243Sobrien time_t cur_time; 6759243Sobrien int count, hours, minutes, dif_hour, dif_min; 6859243Sobrien Char *cp; 69145479Smp int relative; /* time specified as +hh:mm */ 7059243Sobrien struct tm *ltp; 7159243Sobrien 7259243Sobrien USE(c); 7359243Sobrien/* This is a major kludge because of a gcc linker */ 7459243Sobrien/* Problem. It may or may not be needed for you */ 7569408Sache#if defined(_MINIX) && !defined(_MINIX_VMD) 7659243Sobrien char kludge[10]; 7759243Sobrien extern char *sprintf(); 7859243Sobrien sprintf(kludge, CGETS(24, 1, "kludge")); 7969408Sache#endif /* _MINIX && !_MINIX_VMD */ 8059243Sobrien 8159243Sobrien v++; 8259243Sobrien cp = *v++; 8359243Sobrien if (cp == NULL) { 84167465Smp const Char *fmt; 8559243Sobrien if ((fmt = varval(STRsched)) == STRNULL) 8659243Sobrien fmt = str2short("%h\t%T\t%R\n"); 8759243Sobrien /* print list of scheduled events */ 8859243Sobrien for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) { 89167465Smp Char *buf, *str; 90167465Smp 91167465Smp buf = blkexpand(tp->t_lex); 92167465Smp cleanup_push(buf, xfree); 93167465Smp str = tprintf(FMT_SCHED, fmt, short2str(buf), tp->t_when, &count); 94167465Smp cleanup_until(buf); 95167465Smp cleanup_push(str, xfree); 96167465Smp for (cp = str; *cp;) 97145479Smp xputwchar(*cp++); 98167465Smp cleanup_until(str); 9959243Sobrien } 10059243Sobrien return; 10159243Sobrien } 10259243Sobrien 10359243Sobrien if (*cp == '-') { 10459243Sobrien /* remove item from list */ 10559243Sobrien if (!sched_ptr) 10659243Sobrien stderror(ERR_NOSCHED); 10759243Sobrien if (*v) 10859243Sobrien stderror(ERR_SCHEDUSAGE); 10959243Sobrien count = atoi(short2str(++cp)); 11059243Sobrien if (count <= 0) 11159243Sobrien stderror(ERR_SCHEDUSAGE); 112167465Smp pp = &sched_ptr; 11359243Sobrien tp = sched_ptr; 11459243Sobrien while (--count) { 11559243Sobrien if (tp->t_next == 0) 11659243Sobrien break; 11759243Sobrien else { 118167465Smp pp = &tp->t_next; 11959243Sobrien tp = tp->t_next; 12059243Sobrien } 12159243Sobrien } 12259243Sobrien if (count) 12359243Sobrien stderror(ERR_SCHEDEV); 124167465Smp *pp = tp->t_next; 12559243Sobrien blkfree(tp->t_lex); 126167465Smp xfree(tp); 12759243Sobrien return; 12859243Sobrien } 12959243Sobrien 13059243Sobrien /* else, add an item to the list */ 13159243Sobrien if (!*v) 13259243Sobrien stderror(ERR_SCHEDCOM); 13359243Sobrien relative = 0; 13459243Sobrien if (!Isdigit(*cp)) { /* not abs. time */ 13559243Sobrien if (*cp != '+') 13659243Sobrien stderror(ERR_SCHEDUSAGE); 13759243Sobrien cp++, relative++; 13859243Sobrien } 13959243Sobrien minutes = 0; 14059243Sobrien hours = atoi(short2str(cp)); 14159243Sobrien while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p') 14259243Sobrien cp++; 14359243Sobrien if (*cp && *cp == ':') 14459243Sobrien minutes = atoi(short2str(++cp)); 14559243Sobrien if ((hours < 0) || (minutes < 0) || 14659243Sobrien (hours > 23) || (minutes > 59)) 14759243Sobrien stderror(ERR_SCHEDTIME); 14859243Sobrien while (*cp && *cp != 'p' && *cp != 'a') 14959243Sobrien cp++; 15059243Sobrien if (*cp && relative) 15159243Sobrien stderror(ERR_SCHEDREL); 15259243Sobrien if (*cp == 'p') 15359243Sobrien hours += 12; 15459243Sobrien (void) time(&cur_time); 15559243Sobrien ltp = localtime(&cur_time); 15659243Sobrien if (relative) { 15759243Sobrien dif_hour = hours; 15859243Sobrien dif_min = minutes; 15959243Sobrien } 16059243Sobrien else { 16159243Sobrien if ((dif_hour = hours - ltp->tm_hour) < 0) 16259243Sobrien dif_hour += 24; 16359243Sobrien if ((dif_min = minutes - ltp->tm_min) < 0) { 16459243Sobrien dif_min += 60; 16559243Sobrien if ((--dif_hour) < 0) 16659243Sobrien dif_hour = 23; 16759243Sobrien } 16859243Sobrien } 169167465Smp tp = xcalloc(1, sizeof *tp); 17059243Sobrien#ifdef _SX 17159243Sobrien tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60; 172167465Smp#else /* _SX */ 17359243Sobrien tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L; 17459243Sobrien#endif /* _SX */ 17559243Sobrien /* use of tm_sec: get to beginning of minute. */ 176167465Smp for (pp = &sched_ptr; *pp != NULL && tp->t_when >= (*pp)->t_when; 177167465Smp pp = &(*pp)->t_next) 178167465Smp ; 179167465Smp tp->t_next = *pp; 180167465Smp *pp = tp; 18159243Sobrien tp->t_lex = saveblk(v); 18259243Sobrien} 18359243Sobrien 18459243Sobrien/* 18559243Sobrien * Execute scheduled events 18659243Sobrien */ 18759243Sobrienvoid 188167465Smpsched_run(void) 18959243Sobrien{ 19059243Sobrien time_t cur_time; 191167465Smp struct sched_event *tp; 19259243Sobrien struct wordent cmd, *nextword, *lastword; 19359243Sobrien struct command *t; 19459243Sobrien Char **v, *cp; 19559243Sobrien 196167465Smp pintr_disabled++; 197167465Smp cleanup_push(&pintr_disabled, disabled_cleanup); 19859243Sobrien 19959243Sobrien (void) time(&cur_time); 20059243Sobrien 20159243Sobrien /* bugfix by: Justin Bur at Universite de Montreal */ 20259243Sobrien /* 20359243Sobrien * this test wouldn't be necessary if this routine were not called before 20459243Sobrien * each prompt (in sh.c). But it is, to catch missed alarms. Someone 20559243Sobrien * ought to fix it all up. -jbb 20659243Sobrien */ 207167465Smp if (!(sched_ptr && sched_ptr->t_when < cur_time)) { 208167465Smp cleanup_until(&pintr_disabled); 20959243Sobrien return; 21059243Sobrien } 21159243Sobrien 21259243Sobrien if (GettingInput) 21359243Sobrien (void) Cookedmode(); 21459243Sobrien 215167465Smp while ((tp = sched_ptr) != NULL && tp->t_when < cur_time) { 21659243Sobrien if (seterr) { 217167465Smp xfree(seterr); 21859243Sobrien seterr = NULL; 21959243Sobrien } 22059243Sobrien cmd.word = STRNULL; 22159243Sobrien lastword = &cmd; 22259243Sobrien v = tp->t_lex; 22359243Sobrien for (cp = *v; cp; cp = *++v) { 224167465Smp nextword = xcalloc(1, sizeof cmd); 22559243Sobrien nextword->word = Strsave(cp); 22659243Sobrien lastword->next = nextword; 22759243Sobrien nextword->prev = lastword; 22859243Sobrien lastword = nextword; 22959243Sobrien } 23059243Sobrien lastword->next = &cmd; 23159243Sobrien cmd.prev = lastword; 232167465Smp sched_ptr = tp->t_next; /* looping termination cond: */ 233167465Smp blkfree(tp->t_lex); /* straighten out in case of */ 234167465Smp xfree(tp); /* command blow-up. */ 23559243Sobrien 236167465Smp cleanup_push(&cmd, lex_cleanup); 23759243Sobrien /* expand aliases like process() does. */ 23859243Sobrien alias(&cmd); 23959243Sobrien /* build a syntax tree for the command. */ 24059243Sobrien t = syntax(cmd.next, &cmd, 0); 241167465Smp cleanup_push(t, syntax_cleanup); 24259243Sobrien if (seterr) 24359243Sobrien stderror(ERR_OLD); 24459243Sobrien /* execute the parse tree. */ 245100616Smp execute(t, -1, NULL, NULL, TRUE); 24659243Sobrien /* done. free the lex list and parse tree. */ 247167465Smp cleanup_until(&cmd); 24859243Sobrien } 24959243Sobrien if (GettingInput && !just_signaled) { /* PWP */ 25059243Sobrien (void) Rawmode(); 25159243Sobrien ClearLines(); /* do a real refresh since something may */ 25259243Sobrien ClearDisp(); /* have printed to the screen */ 25359243Sobrien Refresh(); 25459243Sobrien } 25559243Sobrien just_signaled = 0; 25659243Sobrien 257167465Smp cleanup_until(&pintr_disabled); 25859243Sobrien} 259