tc.sched.c revision 59243
159243Sobrien/* $Header: /src/pub/tcsh/tc.sched.c,v 3.16 1998/10/25 15:10:40 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.
1959243Sobrien * 3. All advertising materials mentioning features or use of this software
2059243Sobrien *    must display the following acknowledgement:
2159243Sobrien *	This product includes software developed by the University of
2259243Sobrien *	California, Berkeley and its contributors.
2359243Sobrien * 4. Neither the name of the University nor the names of its contributors
2459243Sobrien *    may be used to endorse or promote products derived from this software
2559243Sobrien *    without specific prior written permission.
2659243Sobrien *
2759243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2859243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2959243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3059243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3159243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3259243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3359243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3459243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3559243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3659243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3759243Sobrien * SUCH DAMAGE.
3859243Sobrien */
3959243Sobrien#include "sh.h"
4059243Sobrien
4159243SobrienRCSID("$Id: tc.sched.c,v 3.16 1998/10/25 15:10:40 christos Exp $")
4259243Sobrien
4359243Sobrien#include "ed.h"
4459243Sobrien#include "tc.h"
4559243Sobrien
4659243Sobrienextern int just_signaled;
4759243Sobrien
4859243Sobrienstruct sched_event {
4959243Sobrien    struct sched_event *t_next;
5059243Sobrien    time_t t_when;
5159243Sobrien    Char  **t_lex;
5259243Sobrien};
5359243Sobrienstatic struct sched_event *sched_ptr = NULL;
5459243Sobrien
5559243Sobrien
5659243Sobrientime_t
5759243Sobriensched_next()
5859243Sobrien{
5959243Sobrien    if (sched_ptr)
6059243Sobrien	return (sched_ptr->t_when);
6159243Sobrien    return ((time_t) - 1);
6259243Sobrien}
6359243Sobrien
6459243Sobrien/*ARGSUSED*/
6559243Sobrienvoid
6659243Sobriendosched(v, c)
6759243Sobrien    register Char **v;
6859243Sobrien    struct command *c;
6959243Sobrien{
7059243Sobrien    register struct sched_event *tp, *tp1, *tp2;
7159243Sobrien    time_t  cur_time;
7259243Sobrien    int     count, hours, minutes, dif_hour, dif_min;
7359243Sobrien    Char   *cp;
7459243Sobrien    bool    relative;		/* time specified as +hh:mm */
7559243Sobrien    struct tm *ltp;
7659243Sobrien
7759243Sobrien    USE(c);
7859243Sobrien/* This is a major kludge because of a gcc linker  */
7959243Sobrien/* Problem.  It may or may not be needed for you   */
8059243Sobrien#ifdef _MINIX
8159243Sobrien    char kludge[10];
8259243Sobrien    extern char *sprintf();
8359243Sobrien    sprintf(kludge, CGETS(24, 1, "kludge"));
8459243Sobrien#endif /* _MINIX */
8559243Sobrien
8659243Sobrien    v++;
8759243Sobrien    cp = *v++;
8859243Sobrien    if (cp == NULL) {
8959243Sobrien	Char   *fmt;
9059243Sobrien	if ((fmt = varval(STRsched)) == STRNULL)
9159243Sobrien	    fmt = str2short("%h\t%T\t%R\n");
9259243Sobrien	/* print list of scheduled events */
9359243Sobrien	for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
9459243Sobrien	    Char buf[BUFSIZE], sbuf[BUFSIZE];
9559243Sobrien	    blkexpand(tp->t_lex, buf);
9659243Sobrien	    tprintf(FMT_SCHED, sbuf, fmt, sizeof(sbuf),
9759243Sobrien		    short2str(buf), tp->t_when, (ptr_t) &count);
9859243Sobrien	    for (cp = sbuf; *cp;)
9959243Sobrien		xputchar(*cp++);
10059243Sobrien	}
10159243Sobrien	return;
10259243Sobrien    }
10359243Sobrien
10459243Sobrien    if (*cp == '-') {
10559243Sobrien	/* remove item from list */
10659243Sobrien	if (!sched_ptr)
10759243Sobrien	    stderror(ERR_NOSCHED);
10859243Sobrien	if (*v)
10959243Sobrien	    stderror(ERR_SCHEDUSAGE);
11059243Sobrien	count = atoi(short2str(++cp));
11159243Sobrien	if (count <= 0)
11259243Sobrien	    stderror(ERR_SCHEDUSAGE);
11359243Sobrien	tp = sched_ptr;
11459243Sobrien	tp1 = 0;
11559243Sobrien	while (--count) {
11659243Sobrien	    if (tp->t_next == 0)
11759243Sobrien		break;
11859243Sobrien	    else {
11959243Sobrien		tp1 = tp;
12059243Sobrien		tp = tp->t_next;
12159243Sobrien	    }
12259243Sobrien	}
12359243Sobrien	if (count)
12459243Sobrien	    stderror(ERR_SCHEDEV);
12559243Sobrien	if (tp1 == 0)
12659243Sobrien	    sched_ptr = tp->t_next;
12759243Sobrien	else
12859243Sobrien	    tp1->t_next = tp->t_next;
12959243Sobrien	blkfree(tp->t_lex);
13059243Sobrien	xfree((ptr_t) tp);
13159243Sobrien	return;
13259243Sobrien    }
13359243Sobrien
13459243Sobrien    /* else, add an item to the list */
13559243Sobrien    if (!*v)
13659243Sobrien	stderror(ERR_SCHEDCOM);
13759243Sobrien    relative = 0;
13859243Sobrien    if (!Isdigit(*cp)) {	/* not abs. time */
13959243Sobrien	if (*cp != '+')
14059243Sobrien	    stderror(ERR_SCHEDUSAGE);
14159243Sobrien	cp++, relative++;
14259243Sobrien    }
14359243Sobrien    minutes = 0;
14459243Sobrien    hours = atoi(short2str(cp));
14559243Sobrien    while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p')
14659243Sobrien	cp++;
14759243Sobrien    if (*cp && *cp == ':')
14859243Sobrien	minutes = atoi(short2str(++cp));
14959243Sobrien    if ((hours < 0) || (minutes < 0) ||
15059243Sobrien	(hours > 23) || (minutes > 59))
15159243Sobrien	stderror(ERR_SCHEDTIME);
15259243Sobrien    while (*cp && *cp != 'p' && *cp != 'a')
15359243Sobrien	cp++;
15459243Sobrien    if (*cp && relative)
15559243Sobrien	stderror(ERR_SCHEDREL);
15659243Sobrien    if (*cp == 'p')
15759243Sobrien	hours += 12;
15859243Sobrien    (void) time(&cur_time);
15959243Sobrien    ltp = localtime(&cur_time);
16059243Sobrien    if (relative) {
16159243Sobrien	dif_hour = hours;
16259243Sobrien	dif_min = minutes;
16359243Sobrien    }
16459243Sobrien    else {
16559243Sobrien	if ((dif_hour = hours - ltp->tm_hour) < 0)
16659243Sobrien	    dif_hour += 24;
16759243Sobrien	if ((dif_min = minutes - ltp->tm_min) < 0) {
16859243Sobrien	    dif_min += 60;
16959243Sobrien	    if ((--dif_hour) < 0)
17059243Sobrien		dif_hour = 23;
17159243Sobrien	}
17259243Sobrien    }
17359243Sobrien    tp = (struct sched_event *) xcalloc(1, sizeof *tp);
17459243Sobrien#ifdef _SX
17559243Sobrien    tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60;
17659243Sobrien#else	/* _SX */
17759243Sobrien    tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
17859243Sobrien#endif /* _SX */
17959243Sobrien    /* use of tm_sec: get to beginning of minute. */
18059243Sobrien    if (!sched_ptr || tp->t_when < sched_ptr->t_when) {
18159243Sobrien	tp->t_next = sched_ptr;
18259243Sobrien	sched_ptr = tp;
18359243Sobrien    }
18459243Sobrien    else {
18559243Sobrien	tp1 = sched_ptr->t_next;
18659243Sobrien	tp2 = sched_ptr;
18759243Sobrien	while (tp1 && tp->t_when >= tp1->t_when) {
18859243Sobrien	    tp2 = tp1;
18959243Sobrien	    tp1 = tp1->t_next;
19059243Sobrien	}
19159243Sobrien	tp->t_next = tp1;
19259243Sobrien	tp2->t_next = tp;
19359243Sobrien    }
19459243Sobrien    tp->t_lex = saveblk(v);
19559243Sobrien}
19659243Sobrien
19759243Sobrien/*
19859243Sobrien * Execute scheduled events
19959243Sobrien */
20059243Sobrien/*ARGSUSED*/
20159243Sobrienvoid
20259243Sobriensched_run(n)
20359243Sobrien    int n;
20459243Sobrien{
20559243Sobrien    time_t   cur_time;
20659243Sobrien    register struct sched_event *tp, *tp1;
20759243Sobrien    struct wordent cmd, *nextword, *lastword;
20859243Sobrien    struct command *t;
20959243Sobrien    Char  **v, *cp;
21059243Sobrien    extern Char GettingInput;
21159243Sobrien#ifdef BSDSIGS
21259243Sobrien    sigmask_t omask;
21359243Sobrien
21459243Sobrien    omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
21559243Sobrien#else
21659243Sobrien    (void) sighold(SIGINT);
21759243Sobrien#endif
21859243Sobrien
21959243Sobrien    USE(n);
22059243Sobrien
22159243Sobrien    (void) time(&cur_time);
22259243Sobrien    tp = sched_ptr;
22359243Sobrien
22459243Sobrien    /* bugfix by: Justin Bur at Universite de Montreal */
22559243Sobrien    /*
22659243Sobrien     * this test wouldn't be necessary if this routine were not called before
22759243Sobrien     * each prompt (in sh.c).  But it is, to catch missed alarms.  Someone
22859243Sobrien     * ought to fix it all up.  -jbb
22959243Sobrien     */
23059243Sobrien    if (!(tp && tp->t_when < cur_time)) {
23159243Sobrien#ifdef BSDSIGS
23259243Sobrien	(void) sigsetmask(omask);
23359243Sobrien#else
23459243Sobrien	(void) sigrelse(SIGINT);
23559243Sobrien#endif
23659243Sobrien	return;
23759243Sobrien    }
23859243Sobrien
23959243Sobrien    if (GettingInput)
24059243Sobrien	(void) Cookedmode();
24159243Sobrien
24259243Sobrien    while (tp && tp->t_when < cur_time) {
24359243Sobrien	if (seterr) {
24459243Sobrien	    xfree((ptr_t) seterr);
24559243Sobrien	    seterr = NULL;
24659243Sobrien	}
24759243Sobrien	cmd.word = STRNULL;
24859243Sobrien	lastword = &cmd;
24959243Sobrien	v = tp->t_lex;
25059243Sobrien	for (cp = *v; cp; cp = *++v) {
25159243Sobrien	    nextword = (struct wordent *) xcalloc(1, sizeof cmd);
25259243Sobrien	    nextword->word = Strsave(cp);
25359243Sobrien	    lastword->next = nextword;
25459243Sobrien	    nextword->prev = lastword;
25559243Sobrien	    lastword = nextword;
25659243Sobrien	}
25759243Sobrien	lastword->next = &cmd;
25859243Sobrien	cmd.prev = lastword;
25959243Sobrien	tp1 = tp;
26059243Sobrien	sched_ptr = tp = tp1->t_next;	/* looping termination cond: */
26159243Sobrien	blkfree(tp1->t_lex);	/* straighten out in case of */
26259243Sobrien	xfree((ptr_t) tp1);	/* command blow-up. */
26359243Sobrien
26459243Sobrien	/* expand aliases like process() does. */
26559243Sobrien	alias(&cmd);
26659243Sobrien	/* build a syntax tree for the command. */
26759243Sobrien	t = syntax(cmd.next, &cmd, 0);
26859243Sobrien	if (seterr)
26959243Sobrien	    stderror(ERR_OLD);
27059243Sobrien	/* execute the parse tree. */
27159243Sobrien	execute(t, -1, NULL, NULL);
27259243Sobrien	/* done. free the lex list and parse tree. */
27359243Sobrien	freelex(&cmd), freesyn(t);
27459243Sobrien    }
27559243Sobrien    if (GettingInput && !just_signaled) {	/* PWP */
27659243Sobrien	(void) Rawmode();
27759243Sobrien	ClearLines();		/* do a real refresh since something may */
27859243Sobrien	ClearDisp();		/* have printed to the screen */
27959243Sobrien	Refresh();
28059243Sobrien    }
28159243Sobrien    just_signaled = 0;
28259243Sobrien
28359243Sobrien#ifdef BSDSIGS
28459243Sobrien    (void) sigsetmask(omask);
28559243Sobrien#else
28659243Sobrien    (void) sigrelse(SIGINT);
28759243Sobrien#endif
28859243Sobrien}
289