1 /* $OpenBSD: schedule.c,v 1.6 2009/10/27 23:59:27 deraadt Exp $ */ 2 /* $NetBSD: schedule.c,v 1.3 1995/04/22 10:59:23 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <stdio.h> 34 #include <math.h> 35 #include <err.h> 36 #include "trek.h" 37 38 /* 39 ** SCHEDULE AN EVENT 40 ** 41 ** An event of type 'type' is scheduled for time NOW + 'offset' 42 ** into the first available slot. 'x', 'y', and 'z' are 43 ** considered the attributes for this event. 44 ** 45 ** The address of the slot is returned. 46 */ 47 48 struct event * 49 schedule(type, offset, x, y, z) 50 int type; 51 double offset; 52 char x, y; 53 char z; 54 { 55 struct event *e; 56 int i; 57 double date; 58 59 date = Now.date + offset; 60 for (i = 0; i < MAXEVENTS; i++) 61 { 62 e = &Event[i]; 63 if (e->evcode) 64 continue; 65 /* got a slot */ 66 # ifdef xTRACE 67 if (Trace) 68 printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n", 69 type, date, i, x, y, z); 70 # endif 71 e->evcode = type; 72 e->date = date; 73 e->x = x; 74 e->y = y; 75 e->systemname = z; 76 Now.eventptr[type & E_EVENT] = e; 77 return (e); 78 } 79 errx(1, "Cannot schedule event %d parm %d %d %d", type, x, y, z); 80 } 81 82 83 /* 84 ** RESCHEDULE AN EVENT 85 ** 86 ** The event pointed to by 'e' is rescheduled to the current 87 ** time plus 'offset'. 88 */ 89 90 void 91 reschedule(e1, offset) 92 struct event *e1; 93 double offset; 94 { 95 double date; 96 struct event *e; 97 98 e = e1; 99 100 date = Now.date + offset; 101 e->date = date; 102 # ifdef xTRACE 103 if (Trace) 104 printf("reschedule: type %d parm %d %d %d @ %.2f\n", 105 e->evcode, e->x, e->y, e->systemname, date); 106 # endif 107 return; 108 } 109 110 111 /* 112 ** UNSCHEDULE AN EVENT 113 ** 114 ** The event at slot 'e' is deleted. 115 */ 116 117 void 118 unschedule(e1) 119 struct event *e1; 120 { 121 struct event *e; 122 123 e = e1; 124 125 # ifdef xTRACE 126 if (Trace) 127 printf("unschedule: type %d @ %.2f parm %d %d %d\n", 128 e->evcode, e->date, e->x, e->y, e->systemname); 129 # endif 130 Now.eventptr[e->evcode & E_EVENT] = 0; 131 e->date = 1e50; 132 e->evcode = 0; 133 return; 134 } 135 136 137 /* 138 ** Abreviated schedule routine 139 ** 140 ** Parameters are the event index and a factor for the time 141 ** figure. 142 */ 143 144 struct event * 145 xsched(ev1, factor, x, y, z) 146 int ev1; 147 int factor; 148 int x, y, z; 149 { 150 int ev; 151 152 ev = ev1; 153 return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z)); 154 } 155 156 157 /* 158 ** Simplified reschedule routine 159 ** 160 ** Parameters are the event index, the initial date, and the 161 ** division factor. Look at the code to see what really happens. 162 */ 163 164 void 165 xresched(e1, ev1, factor) 166 struct event *e1; 167 int ev1; 168 int factor; 169 { 170 int ev; 171 struct event *e; 172 173 ev = ev1; 174 e = e1; 175 reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor); 176 } 177