159243Sobrien/* 259243Sobrien * tc.sched.c: Scheduled command execution 359243Sobrien * 459243Sobrien * Karl Kleinpaste: Computer Consoles Inc. 1984 559243Sobrien */ 659243Sobrien/*- 759243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California. 859243Sobrien * All rights reserved. 959243Sobrien * 1059243Sobrien * Redistribution and use in source and binary forms, with or without 1159243Sobrien * modification, are permitted provided that the following conditions 1259243Sobrien * are met: 1359243Sobrien * 1. Redistributions of source code must retain the above copyright 1459243Sobrien * notice, this list of conditions and the following disclaimer. 1559243Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1659243Sobrien * notice, this list of conditions and the following disclaimer in the 1759243Sobrien * documentation and/or other materials provided with the distribution. 18100616Smp * 3. Neither the name of the University nor the names of its contributors 1959243Sobrien * may be used to endorse or promote products derived from this software 2059243Sobrien * without specific prior written permission. 2159243Sobrien * 2259243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2359243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2459243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2559243Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2659243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2759243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2859243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2959243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3059243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3159243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3259243Sobrien * SUCH DAMAGE. 3359243Sobrien */ 3459243Sobrien#include "sh.h" 3559243Sobrien#include "ed.h" 36100616Smp#include "tw.h" 3759243Sobrien#include "tc.h" 3859243Sobrien 3959243Sobrienextern int just_signaled; 4059243Sobrien 4159243Sobrienstruct sched_event { 4259243Sobrien struct sched_event *t_next; 4359243Sobrien time_t t_when; 4459243Sobrien Char **t_lex; 4559243Sobrien}; 4659243Sobrienstatic struct sched_event *sched_ptr = NULL; 4759243Sobrien 4859243Sobrien 4959243Sobrientime_t 50167465Smpsched_next(void) 5159243Sobrien{ 5259243Sobrien if (sched_ptr) 5359243Sobrien return (sched_ptr->t_when); 5459243Sobrien return ((time_t) - 1); 5559243Sobrien} 5659243Sobrien 5759243Sobrien/*ARGSUSED*/ 5859243Sobrienvoid 59167465Smpdosched(Char **v, struct command *c) 6059243Sobrien{ 61167465Smp struct sched_event *tp, **pp; 6259243Sobrien time_t cur_time; 6359243Sobrien int count, hours, minutes, dif_hour, dif_min; 6459243Sobrien Char *cp; 65145479Smp int relative; /* time specified as +hh:mm */ 6659243Sobrien struct tm *ltp; 6759243Sobrien 6859243Sobrien USE(c); 6959243Sobrien/* This is a major kludge because of a gcc linker */ 7059243Sobrien/* Problem. It may or may not be needed for you */ 7169408Sache#if defined(_MINIX) && !defined(_MINIX_VMD) 7259243Sobrien char kludge[10]; 7359243Sobrien extern char *sprintf(); 7459243Sobrien sprintf(kludge, CGETS(24, 1, "kludge")); 7569408Sache#endif /* _MINIX && !_MINIX_VMD */ 7659243Sobrien 7759243Sobrien v++; 7859243Sobrien cp = *v++; 7959243Sobrien if (cp == NULL) { 80167465Smp const Char *fmt; 8159243Sobrien if ((fmt = varval(STRsched)) == STRNULL) 8259243Sobrien fmt = str2short("%h\t%T\t%R\n"); 8359243Sobrien /* print list of scheduled events */ 8459243Sobrien for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) { 85167465Smp Char *buf, *str; 86167465Smp 87167465Smp buf = blkexpand(tp->t_lex); 88167465Smp cleanup_push(buf, xfree); 89167465Smp str = tprintf(FMT_SCHED, fmt, short2str(buf), tp->t_when, &count); 90167465Smp cleanup_until(buf); 91167465Smp cleanup_push(str, xfree); 92167465Smp for (cp = str; *cp;) 93145479Smp xputwchar(*cp++); 94167465Smp cleanup_until(str); 9559243Sobrien } 9659243Sobrien return; 9759243Sobrien } 9859243Sobrien 9959243Sobrien if (*cp == '-') { 10059243Sobrien /* remove item from list */ 10159243Sobrien if (!sched_ptr) 10259243Sobrien stderror(ERR_NOSCHED); 10359243Sobrien if (*v) 10459243Sobrien stderror(ERR_SCHEDUSAGE); 10559243Sobrien count = atoi(short2str(++cp)); 10659243Sobrien if (count <= 0) 10759243Sobrien stderror(ERR_SCHEDUSAGE); 108167465Smp pp = &sched_ptr; 10959243Sobrien tp = sched_ptr; 11059243Sobrien while (--count) { 11159243Sobrien if (tp->t_next == 0) 11259243Sobrien break; 11359243Sobrien else { 114167465Smp pp = &tp->t_next; 11559243Sobrien tp = tp->t_next; 11659243Sobrien } 11759243Sobrien } 11859243Sobrien if (count) 11959243Sobrien stderror(ERR_SCHEDEV); 120167465Smp *pp = tp->t_next; 12159243Sobrien blkfree(tp->t_lex); 122167465Smp xfree(tp); 12359243Sobrien return; 12459243Sobrien } 12559243Sobrien 12659243Sobrien /* else, add an item to the list */ 12759243Sobrien if (!*v) 12859243Sobrien stderror(ERR_SCHEDCOM); 12959243Sobrien relative = 0; 13059243Sobrien if (!Isdigit(*cp)) { /* not abs. time */ 13159243Sobrien if (*cp != '+') 13259243Sobrien stderror(ERR_SCHEDUSAGE); 13359243Sobrien cp++, relative++; 13459243Sobrien } 13559243Sobrien minutes = 0; 13659243Sobrien hours = atoi(short2str(cp)); 13759243Sobrien while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p') 13859243Sobrien cp++; 13959243Sobrien if (*cp && *cp == ':') 14059243Sobrien minutes = atoi(short2str(++cp)); 14159243Sobrien if ((hours < 0) || (minutes < 0) || 14259243Sobrien (hours > 23) || (minutes > 59)) 14359243Sobrien stderror(ERR_SCHEDTIME); 14459243Sobrien while (*cp && *cp != 'p' && *cp != 'a') 14559243Sobrien cp++; 14659243Sobrien if (*cp && relative) 14759243Sobrien stderror(ERR_SCHEDREL); 14859243Sobrien if (*cp == 'p') 14959243Sobrien hours += 12; 15059243Sobrien (void) time(&cur_time); 15159243Sobrien ltp = localtime(&cur_time); 15259243Sobrien if (relative) { 15359243Sobrien dif_hour = hours; 15459243Sobrien dif_min = minutes; 15559243Sobrien } 15659243Sobrien else { 15759243Sobrien if ((dif_hour = hours - ltp->tm_hour) < 0) 15859243Sobrien dif_hour += 24; 15959243Sobrien if ((dif_min = minutes - ltp->tm_min) < 0) { 16059243Sobrien dif_min += 60; 16159243Sobrien if ((--dif_hour) < 0) 16259243Sobrien dif_hour = 23; 16359243Sobrien } 16459243Sobrien } 165167465Smp tp = xcalloc(1, sizeof *tp); 16659243Sobrien#ifdef _SX 16759243Sobrien tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60; 168167465Smp#else /* _SX */ 16959243Sobrien tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L; 17059243Sobrien#endif /* _SX */ 17159243Sobrien /* use of tm_sec: get to beginning of minute. */ 172167465Smp for (pp = &sched_ptr; *pp != NULL && tp->t_when >= (*pp)->t_when; 173167465Smp pp = &(*pp)->t_next) 174167465Smp ; 175167465Smp tp->t_next = *pp; 176167465Smp *pp = tp; 17759243Sobrien tp->t_lex = saveblk(v); 17859243Sobrien} 17959243Sobrien 18059243Sobrien/* 18159243Sobrien * Execute scheduled events 18259243Sobrien */ 18359243Sobrienvoid 184167465Smpsched_run(void) 18559243Sobrien{ 18659243Sobrien time_t cur_time; 187167465Smp struct sched_event *tp; 18859243Sobrien struct wordent cmd, *nextword, *lastword; 18959243Sobrien struct command *t; 19059243Sobrien Char **v, *cp; 19159243Sobrien 192167465Smp pintr_disabled++; 193167465Smp cleanup_push(&pintr_disabled, disabled_cleanup); 19459243Sobrien 19559243Sobrien (void) time(&cur_time); 19659243Sobrien 19759243Sobrien /* bugfix by: Justin Bur at Universite de Montreal */ 19859243Sobrien /* 19959243Sobrien * this test wouldn't be necessary if this routine were not called before 20059243Sobrien * each prompt (in sh.c). But it is, to catch missed alarms. Someone 20159243Sobrien * ought to fix it all up. -jbb 20259243Sobrien */ 203167465Smp if (!(sched_ptr && sched_ptr->t_when < cur_time)) { 204167465Smp cleanup_until(&pintr_disabled); 20559243Sobrien return; 20659243Sobrien } 20759243Sobrien 20859243Sobrien if (GettingInput) 20959243Sobrien (void) Cookedmode(); 21059243Sobrien 211167465Smp while ((tp = sched_ptr) != NULL && tp->t_when < cur_time) { 21259243Sobrien if (seterr) { 213167465Smp xfree(seterr); 21459243Sobrien seterr = NULL; 21559243Sobrien } 21659243Sobrien cmd.word = STRNULL; 21759243Sobrien lastword = &cmd; 21859243Sobrien v = tp->t_lex; 21959243Sobrien for (cp = *v; cp; cp = *++v) { 220167465Smp nextword = xcalloc(1, sizeof cmd); 22159243Sobrien nextword->word = Strsave(cp); 22259243Sobrien lastword->next = nextword; 22359243Sobrien nextword->prev = lastword; 22459243Sobrien lastword = nextword; 22559243Sobrien } 22659243Sobrien lastword->next = &cmd; 22759243Sobrien cmd.prev = lastword; 228167465Smp sched_ptr = tp->t_next; /* looping termination cond: */ 229167465Smp blkfree(tp->t_lex); /* straighten out in case of */ 230167465Smp xfree(tp); /* command blow-up. */ 23159243Sobrien 232167465Smp cleanup_push(&cmd, lex_cleanup); 23359243Sobrien /* expand aliases like process() does. */ 23459243Sobrien alias(&cmd); 23559243Sobrien /* build a syntax tree for the command. */ 23659243Sobrien t = syntax(cmd.next, &cmd, 0); 237167465Smp cleanup_push(t, syntax_cleanup); 23859243Sobrien if (seterr) 23959243Sobrien stderror(ERR_OLD); 24059243Sobrien /* execute the parse tree. */ 241100616Smp execute(t, -1, NULL, NULL, TRUE); 24259243Sobrien /* done. free the lex list and parse tree. */ 243167465Smp cleanup_until(&cmd); 24459243Sobrien } 24559243Sobrien if (GettingInput && !just_signaled) { /* PWP */ 24659243Sobrien (void) Rawmode(); 24759243Sobrien ClearLines(); /* do a real refresh since something may */ 24859243Sobrien ClearDisp(); /* have printed to the screen */ 24959243Sobrien Refresh(); 25059243Sobrien } 25159243Sobrien just_signaled = 0; 25259243Sobrien 253167465Smp cleanup_until(&pintr_disabled); 25459243Sobrien} 255