]> git.pond.sub.org Git - empserver/blob - src/lib/common/rdsched.c
New update scheduler:
[empserver] / src / lib / common / rdsched.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  rdsched.c: Read update schedule
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2007
32  */
33
34 #define _XOPEN_SOURCE 500
35
36 #include <config.h>
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 #include "prototypes.h"
45
46 static int parse_schedule_line(char *, time_t[], int, time_t, time_t *,
47                                char *, int);
48 static int time_ok(time_t, char *, int);
49 static char *parse_time(time_t *, char *, time_t *);
50 static char *parse_every(time_t *, char *);
51 static char *parse_until(time_t *, char *, time_t *);
52 static char *parse_skip(time_t *, char *, time_t *);
53 static int insert_update(time_t, time_t[], int, time_t);
54 static int delete_update(time_t, time_t[], int);
55
56 /*
57  * Read update schedule from file FNAME.
58  * Put the first N-1 updates after T0 into SCHED[] in ascending order,
59  * terminated with a zero.
60  * Use ANCHOR as initial anchor for anchor-relative times. 
61  * Return 0 on success, -1 on failure.
62  */
63 int
64 read_schedule(char *fname, time_t sched[], int n, time_t t0, time_t anchor)
65 {
66     FILE *fp;
67     int lno = 0;
68     char buf[1024];
69     char *endp;
70
71     if (fname) {
72         fp = fopen(fname, "r");
73         if (!fp) {
74             logerror("Can't open %s for reading (%s)\n",
75                      fname, strerror(errno));
76             return -1;
77         }
78     } else {
79         fp = stdin;
80         fname = "<stdin>";
81     }
82
83     sched[0] = 0;
84     while (fgets(buf, sizeof(buf), fp) != NULL) {
85         ++lno;
86         endp = strchr(buf, '#');
87         if (endp)
88             *endp = 0;
89         if (parse_schedule_line(buf, sched, n, t0, &anchor, fname, lno))
90             return -1;
91     }
92
93     fclose(fp);
94     return 0;
95 }
96
97 /*
98  * Parse an update schedule directive from LINE.
99  * Update SCHED[] and ANCHOR accordingly.
100  * SCHED[] holds the first N-1 updates after T0 in ascending order.
101  * FNAME and LNO file name and line number for reporting errors.
102  */
103 static int
104 parse_schedule_line(char *line, time_t sched[], int n,
105                     time_t t0, time_t *anchor,
106                     char *fname, int lno)
107 {
108     char *endp, *p;
109     int bol;
110     time_t t, delta, u;
111
112     if ((endp = parse_time(&t, line, anchor))) {
113         if (!time_ok(t, fname, lno))
114             return -1;
115         *anchor = t;
116         insert_update(t, sched, n, t0);
117     } else if ((endp = parse_every(&delta, line))) {
118         if ((p = parse_until(&u, endp, anchor))) {
119             endp = p;
120             if (!time_ok(u, fname, lno))
121                 return -1;
122         } else
123             u = (time_t)-1;
124         t = *anchor;
125         do {
126             t += delta;
127         } while ((u == (time_t)-1 || t <= u)
128                  && insert_update(t, sched, n, t0) < n - 1);
129     } else if ((endp = parse_skip(&t, line, anchor))) {
130         if (!time_ok(t, fname, lno))
131             return -1;
132         delete_update(t, sched, n);
133     } else
134         endp = line;
135
136     bol = endp == line;
137     while (isspace(*endp)) endp++;
138     if (*endp) {
139         if (bol)
140             logerror("%s:%d: unintelligible\n", fname, lno);
141         else
142             logerror("%s:%d: trailing junk\n", fname, lno);
143         return -1;
144     }
145
146     return 0;
147 }
148
149 /*
150  * Complain and return zero when T is bad, else return non-zero.
151  * FNAME and LNO file name and line number.
152  */
153 static int
154 time_ok(time_t t, char *fname, int lno)
155 {
156     if (t == (time_t)-1) {
157         logerror("%s:%d: time weird\n", fname, lno);
158         return 0;
159     }
160     return 1;
161 }
162
163 /*
164  * Parse a time from S into *T.
165  * *ANCHOR is the base for anchor-relative time.
166  * Return pointer to first character not parsed on success,
167  * null pointer on failure.
168  */
169 static char *
170 parse_time(time_t *t, char *s, time_t *anchor)
171 {
172     static char *fmt[] = {
173         "%Y-%m-%d %H:%M ",      /* ISO 8601 */
174         "%b %d %H:%M %Y ",      /* like ctime(): Dec 22 15:35 2006 */
175         "%d %b %Y %H:%M ",      /* 22 Dec 2006 15:35 */
176         "next %a %H:%M ",       /* next Fri 15:35 */
177         "next %a ",             /* next Fri */
178         NULL
179     };
180     char *p, *endp;
181     int i;
182     struct tm tm, nexttm;
183
184     for (p = s; isspace(*(unsigned char *)p); ++p) ;
185
186     for (i = 0; ; i++) {
187         if (!fmt[i])
188             return NULL;
189         memset(&tm, 0, sizeof(tm));
190         tm.tm_hour = -1;
191         endp = strptime(p, fmt[i], &tm);
192         if (endp)
193             break;
194     }
195
196     if (tm.tm_mday == 0) {
197         /* relative to anchor */
198         nexttm = *localtime(anchor);
199         if (tm.tm_hour >= 0) {
200             /* got hour and minute */
201             nexttm.tm_hour = tm.tm_hour;
202             nexttm.tm_min = tm.tm_min;
203             nexttm.tm_sec = 0;
204         }
205         nexttm.tm_mday += tm.tm_wday - nexttm.tm_wday;
206         if (tm.tm_wday <= nexttm.tm_wday)
207             nexttm.tm_mday += 7;
208         tm = nexttm;
209     }
210
211     *t = mktime(&tm);
212     return endp;
213 }
214
215 /*
216  * Parse an every clause from S into *SECS.
217  * Return pointer to first character not parsed on success,
218  * null pointer on failure.
219  */
220 static char *
221 parse_every(time_t *secs, char *s)
222 {
223     int nch, delta;
224
225     nch = -1;
226     sscanf(s, " every %u hours%n", &delta, &nch);
227     if (nch >= 0)
228         delta *= 60;
229     else
230         sscanf(s, " every %u minutes%n", &delta, &nch);
231     if (nch < 0)
232         return NULL;    *secs = 60 * delta;
233     return s + nch;
234 }
235
236 /*
237  * Parse an until clause from S into *T.
238  * *ANCHOR is the base for anchor-relative time.
239  * Return pointer to first character not parsed on success,
240  * null pointer on failure.
241  */
242 static char *
243 parse_until(time_t *t, char *s, time_t *anchor)
244 {
245     int nch;
246
247     nch = -1;
248     sscanf(s, " until%n", &nch);
249     if (nch < 0)
250         return NULL;
251     return parse_time(t, s + nch, anchor);
252 }
253
254 /*
255  * Parse an skip clause from S into *T.
256  * *ANCHOR is the base for anchor-relative time.
257  * Return pointer to first character not parsed on success,
258  * null pointer on failure.
259  */
260 static char *
261 parse_skip(time_t *t, char *s, time_t *anchor)
262 {
263     int nch;
264
265     nch = -1;
266     sscanf(s, " skip%n", &nch);
267     if (nch < 0)
268         return NULL;
269     return parse_time(t, s + nch, anchor);
270 }
271
272 /*
273  * Return the index of the first update at or after T in SCHED[].
274  */
275 static int
276 find_update(time_t t, time_t sched[])
277 {
278     int i;
279
280     /* Could use binary search here, but it's hardly worth it */
281     for (i = 0; sched[i] && t > sched[i]; i++) ;
282     return i;
283 }
284
285 /*
286  * Insert update at T into SCHED[].
287  * SCHED[] holds the first N-1 updates after T0 in ascending order.
288  * If T is before T0 or outside game_days/game_hours, return -1.
289  * If there's no space for T in SCHED[], return N-1.
290  * Else insert T into SCHED[] and return its index in SCHED[].
291  */
292 static int
293 insert_update(time_t t, time_t sched[], int n, time_t t0)
294 {
295     int i;
296
297     if (t <= t0 || !gamehours(t))
298         return -1;
299
300     i = find_update(t, sched);
301     memmove(sched + i + 1, sched + i, (n - 1 - i) * sizeof(*sched));
302     sched[i] = t;
303     sched[n - 1] = 0;
304     return i;
305 }
306
307 /*
308  * Delete update T from SCHED[].
309  * SCHED[] holds N-1 updates in ascending order.
310  * Return the index of the first update after T in SCHED[].
311  */
312 static int
313 delete_update(time_t t, time_t sched[], int n)
314 {
315     int i = find_update(t, sched);
316     if (t == sched[i])
317         memmove(sched + i, sched + i + 1,
318                 (n - 1 - i) * sizeof(*sched));
319     return i;
320 }