tc.sched.c revision 145479
1145479Smp/* $Header: /src/pub/tcsh/tc.sched.c,v 3.21 2004/11/23 02:10:50 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
37145479SmpRCSID("$Id: tc.sched.c,v 3.21 2004/11/23 02:10:50 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
5459243Sobriensched_next()
5559243Sobrien{
5659243Sobrien    if (sched_ptr)
5759243Sobrien	return (sched_ptr->t_when);
5859243Sobrien    return ((time_t) - 1);
5959243Sobrien}
6059243Sobrien
6159243Sobrien/*ARGSUSED*/
6259243Sobrienvoid
6359243Sobriendosched(v, c)
64145479Smp    Char **v;
6559243Sobrien    struct command *c;
6659243Sobrien{
67145479Smp    struct sched_event *tp, *tp1, *tp2;
6859243Sobrien    time_t  cur_time;
6959243Sobrien    int     count, hours, minutes, dif_hour, dif_min;
7059243Sobrien    Char   *cp;
71145479Smp    int    relative;		/* time specified as +hh:mm */
7259243Sobrien    struct tm *ltp;
7359243Sobrien
7459243Sobrien    USE(c);
7559243Sobrien/* This is a major kludge because of a gcc linker  */
7659243Sobrien/* Problem.  It may or may not be needed for you   */
7769408Sache#if defined(_MINIX) && !defined(_MINIX_VMD)
7859243Sobrien    char kludge[10];
7959243Sobrien    extern char *sprintf();
8059243Sobrien    sprintf(kludge, CGETS(24, 1, "kludge"));
8169408Sache#endif /* _MINIX && !_MINIX_VMD */
8259243Sobrien
8359243Sobrien    v++;
8459243Sobrien    cp = *v++;
8559243Sobrien    if (cp == NULL) {
8659243Sobrien	Char   *fmt;
8759243Sobrien	if ((fmt = varval(STRsched)) == STRNULL)
8859243Sobrien	    fmt = str2short("%h\t%T\t%R\n");
8959243Sobrien	/* print list of scheduled events */
9059243Sobrien	for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
9159243Sobrien	    Char buf[BUFSIZE], sbuf[BUFSIZE];
9259243Sobrien	    blkexpand(tp->t_lex, buf);
9359243Sobrien	    tprintf(FMT_SCHED, sbuf, fmt, sizeof(sbuf),
9459243Sobrien		    short2str(buf), tp->t_when, (ptr_t) &count);
9559243Sobrien	    for (cp = sbuf; *cp;)
96145479Smp		xputwchar(*cp++);
9759243Sobrien	}
9859243Sobrien	return;
9959243Sobrien    }
10059243Sobrien
10159243Sobrien    if (*cp == '-') {
10259243Sobrien	/* remove item from list */
10359243Sobrien	if (!sched_ptr)
10459243Sobrien	    stderror(ERR_NOSCHED);
10559243Sobrien	if (*v)
10659243Sobrien	    stderror(ERR_SCHEDUSAGE);
10759243Sobrien	count = atoi(short2str(++cp));
10859243Sobrien	if (count <= 0)
10959243Sobrien	    stderror(ERR_SCHEDUSAGE);
11059243Sobrien	tp = sched_ptr;
11159243Sobrien	tp1 = 0;
11259243Sobrien	while (--count) {
11359243Sobrien	    if (tp->t_next == 0)
11459243Sobrien		break;
11559243Sobrien	    else {
11659243Sobrien		tp1 = tp;
11759243Sobrien		tp = tp->t_next;
11859243Sobrien	    }
11959243Sobrien	}
12059243Sobrien	if (count)
12159243Sobrien	    stderror(ERR_SCHEDEV);
12259243Sobrien	if (tp1 == 0)
12359243Sobrien	    sched_ptr = tp->t_next;
12459243Sobrien	else
12559243Sobrien	    tp1->t_next = tp->t_next;
12659243Sobrien	blkfree(tp->t_lex);
12759243Sobrien	xfree((ptr_t) tp);
12859243Sobrien	return;
12959243Sobrien    }
13059243Sobrien
13159243Sobrien    /* else, add an item to the list */
13259243Sobrien    if (!*v)
13359243Sobrien	stderror(ERR_SCHEDCOM);
13459243Sobrien    relative = 0;
13559243Sobrien    if (!Isdigit(*cp)) {	/* not abs. time */
13659243Sobrien	if (*cp != '+')
13759243Sobrien	    stderror(ERR_SCHEDUSAGE);
13859243Sobrien	cp++, relative++;
13959243Sobrien    }
14059243Sobrien    minutes = 0;
14159243Sobrien    hours = atoi(short2str(cp));
14259243Sobrien    while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p')
14359243Sobrien	cp++;
14459243Sobrien    if (*cp && *cp == ':')
14559243Sobrien	minutes = atoi(short2str(++cp));
14659243Sobrien    if ((hours < 0) || (minutes < 0) ||
14759243Sobrien	(hours > 23) || (minutes > 59))
14859243Sobrien	stderror(ERR_SCHEDTIME);
14959243Sobrien    while (*cp && *cp != 'p' && *cp != 'a')
15059243Sobrien	cp++;
15159243Sobrien    if (*cp && relative)
15259243Sobrien	stderror(ERR_SCHEDREL);
15359243Sobrien    if (*cp == 'p')
15459243Sobrien	hours += 12;
15559243Sobrien    (void) time(&cur_time);
15659243Sobrien    ltp = localtime(&cur_time);
15759243Sobrien    if (relative) {
15859243Sobrien	dif_hour = hours;
15959243Sobrien	dif_min = minutes;
16059243Sobrien    }
16159243Sobrien    else {
16259243Sobrien	if ((dif_hour = hours - ltp->tm_hour) < 0)
16359243Sobrien	    dif_hour += 24;
16459243Sobrien	if ((dif_min = minutes - ltp->tm_min) < 0) {
16559243Sobrien	    dif_min += 60;
16659243Sobrien	    if ((--dif_hour) < 0)
16759243Sobrien		dif_hour = 23;
16859243Sobrien	}
16959243Sobrien    }
17059243Sobrien    tp = (struct sched_event *) xcalloc(1, sizeof *tp);
17159243Sobrien#ifdef _SX
17259243Sobrien    tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60;
17359243Sobrien#else	/* _SX */
17459243Sobrien    tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
17559243Sobrien#endif /* _SX */
17659243Sobrien    /* use of tm_sec: get to beginning of minute. */
17759243Sobrien    if (!sched_ptr || tp->t_when < sched_ptr->t_when) {
17859243Sobrien	tp->t_next = sched_ptr;
17959243Sobrien	sched_ptr = tp;
18059243Sobrien    }
18159243Sobrien    else {
18259243Sobrien	tp1 = sched_ptr->t_next;
18359243Sobrien	tp2 = sched_ptr;
18459243Sobrien	while (tp1 && tp->t_when >= tp1->t_when) {
18559243Sobrien	    tp2 = tp1;
18659243Sobrien	    tp1 = tp1->t_next;
18759243Sobrien	}
18859243Sobrien	tp->t_next = tp1;
18959243Sobrien	tp2->t_next = tp;
19059243Sobrien    }
19159243Sobrien    tp->t_lex = saveblk(v);
19259243Sobrien}
19359243Sobrien
19459243Sobrien/*
19559243Sobrien * Execute scheduled events
19659243Sobrien */
19759243Sobrien/*ARGSUSED*/
19859243Sobrienvoid
19959243Sobriensched_run(n)
20059243Sobrien    int n;
20159243Sobrien{
20259243Sobrien    time_t   cur_time;
203145479Smp    struct sched_event *tp, *tp1;
20459243Sobrien    struct wordent cmd, *nextword, *lastword;
20559243Sobrien    struct command *t;
20659243Sobrien    Char  **v, *cp;
20759243Sobrien#ifdef BSDSIGS
20859243Sobrien    sigmask_t omask;
20959243Sobrien
21059243Sobrien    omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
21159243Sobrien#else
21259243Sobrien    (void) sighold(SIGINT);
21359243Sobrien#endif
21459243Sobrien
21559243Sobrien    USE(n);
21659243Sobrien
21759243Sobrien    (void) time(&cur_time);
21859243Sobrien    tp = sched_ptr;
21959243Sobrien
22059243Sobrien    /* bugfix by: Justin Bur at Universite de Montreal */
22159243Sobrien    /*
22259243Sobrien     * this test wouldn't be necessary if this routine were not called before
22359243Sobrien     * each prompt (in sh.c).  But it is, to catch missed alarms.  Someone
22459243Sobrien     * ought to fix it all up.  -jbb
22559243Sobrien     */
22659243Sobrien    if (!(tp && tp->t_when < cur_time)) {
22759243Sobrien#ifdef BSDSIGS
22859243Sobrien	(void) sigsetmask(omask);
22959243Sobrien#else
23059243Sobrien	(void) sigrelse(SIGINT);
23159243Sobrien#endif
23259243Sobrien	return;
23359243Sobrien    }
23459243Sobrien
23559243Sobrien    if (GettingInput)
23659243Sobrien	(void) Cookedmode();
23759243Sobrien
23859243Sobrien    while (tp && tp->t_when < cur_time) {
23959243Sobrien	if (seterr) {
24059243Sobrien	    xfree((ptr_t) seterr);
24159243Sobrien	    seterr = NULL;
24259243Sobrien	}
24359243Sobrien	cmd.word = STRNULL;
24459243Sobrien	lastword = &cmd;
24559243Sobrien	v = tp->t_lex;
24659243Sobrien	for (cp = *v; cp; cp = *++v) {
24759243Sobrien	    nextword = (struct wordent *) xcalloc(1, sizeof cmd);
24859243Sobrien	    nextword->word = Strsave(cp);
24959243Sobrien	    lastword->next = nextword;
25059243Sobrien	    nextword->prev = lastword;
25159243Sobrien	    lastword = nextword;
25259243Sobrien	}
25359243Sobrien	lastword->next = &cmd;
25459243Sobrien	cmd.prev = lastword;
25559243Sobrien	tp1 = tp;
25659243Sobrien	sched_ptr = tp = tp1->t_next;	/* looping termination cond: */
25759243Sobrien	blkfree(tp1->t_lex);	/* straighten out in case of */
25859243Sobrien	xfree((ptr_t) tp1);	/* command blow-up. */
25959243Sobrien
26059243Sobrien	/* expand aliases like process() does. */
26159243Sobrien	alias(&cmd);
26259243Sobrien	/* build a syntax tree for the command. */
26359243Sobrien	t = syntax(cmd.next, &cmd, 0);
26459243Sobrien	if (seterr)
26559243Sobrien	    stderror(ERR_OLD);
26659243Sobrien	/* execute the parse tree. */
267100616Smp	execute(t, -1, NULL, NULL, TRUE);
26859243Sobrien	/* done. free the lex list and parse tree. */
26959243Sobrien	freelex(&cmd), freesyn(t);
27059243Sobrien    }
27159243Sobrien    if (GettingInput && !just_signaled) {	/* PWP */
27259243Sobrien	(void) Rawmode();
27359243Sobrien	ClearLines();		/* do a real refresh since something may */
27459243Sobrien	ClearDisp();		/* have printed to the screen */
27559243Sobrien	Refresh();
27659243Sobrien    }
27759243Sobrien    just_signaled = 0;
27859243Sobrien
27959243Sobrien#ifdef BSDSIGS
28059243Sobrien    (void) sigsetmask(omask);
28159243Sobrien#else
28259243Sobrien    (void) sigrelse(SIGINT);
28359243Sobrien#endif
28459243Sobrien}
285