]> git.pond.sub.org Git - empserver/blob - src/lib/common/rdsched.c
Update known contributors comments
[empserver] / src / lib / common / rdsched.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  rdsched.c: Read update schedule
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2007-2010
31  */
32
33 #define _XOPEN_SOURCE 500
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <time.h>
43 #include "prototypes.h"
44
45 static int parse_schedule_line(char *, time_t[], int, time_t, time_t *,
46                                char *, int);
47 static int time_ok(time_t, char *, int);
48 static char *parse_time(time_t *, char *, time_t *);
49 static char *parse_every(time_t *, char *);
50 static char *parse_until(time_t *, char *, time_t *);
51 static char *parse_skip(time_t *, char *, time_t *);
52 static int insert_update(time_t, time_t[], int, time_t);
53 static int delete_update(time_t, time_t[], int);
54
55 /*
56  * Read update schedule from file FNAME.
57  * Put the first N-1 updates after T0 into SCHED[] in ascending order,
58  * terminated with a zero.
59  * Use ANCHOR as initial anchor for anchor-relative times.
60  * Return 0 on success, -1 on failure.
61  */
62 int
63 read_schedule(char *fname, time_t sched[], int n, time_t t0, time_t anchor)
64 {
65     FILE *fp;
66     int lno = 0;
67     char buf[1024];
68     char *endp;
69
70     if (fname) {
71         fp = fopen(fname, "r");
72         if (!fp) {
73             logerror("Can't open %s for reading (%s)\n",
74                      fname, strerror(errno));
75             return -1;
76         }
77     } else {
78         fp = stdin;
79         fname = "<stdin>";
80     }
81
82     sched[0] = 0;
83     while (fgets(buf, sizeof(buf), fp) != NULL) {
84         ++lno;
85         endp = strchr(buf, '#');
86         if (endp)
87             *endp = 0;
88         if (parse_schedule_line(buf, sched, n, t0, &anchor, fname, lno))
89             return -1;
90     }
91
92     fclose(fp);
93     return 0;
94 }
95
96 /*
97  * Parse an update schedule directive from LINE.
98  * Update SCHED[] and ANCHOR accordingly.
99  * SCHED[] holds the first N-1 updates after T0 in ascending order.
100  * FNAME and LNO file name and line number for reporting errors.
101  */
102 static int
103 parse_schedule_line(char *line, time_t sched[], int n,
104                     time_t t0, time_t *anchor,
105                     char *fname, int lno)
106 {
107     char *endp, *p;
108     int bol;
109     time_t t, delta, u;
110
111     if ((endp = parse_time(&t, line, anchor))) {
112         if (!time_ok(t, fname, lno))
113             return -1;
114         *anchor = t;
115         insert_update(t, sched, n, t0);
116     } else if ((endp = parse_every(&delta, line))) {
117         if ((p = parse_until(&u, endp, anchor))) {
118             endp = p;
119             if (!time_ok(u, fname, lno))
120                 return -1;
121         } else
122             u = (time_t)-1;
123         t = *anchor;
124         do {
125             t += delta;
126         } while ((u == (time_t)-1 || t <= u)
127                  && insert_update(t, sched, n, t0) < n - 1);
128     } else if ((endp = parse_skip(&t, line, anchor))) {
129         if (!time_ok(t, fname, lno))
130             return -1;
131         delete_update(t, sched, n);
132     } else
133         endp = line;
134
135     bol = endp == line;
136     while (isspace(*endp)) endp++;
137     if (*endp) {
138         if (bol)
139             logerror("%s:%d: unintelligible\n", fname, lno);
140         else
141             logerror("%s:%d: trailing junk\n", fname, lno);
142         return -1;
143     }
144
145     return 0;
146 }
147
148 /*
149  * Complain and return zero when T is bad, else return non-zero.
150  * FNAME and LNO file name and line number.
151  */
152 static int
153 time_ok(time_t t, char *fname, int lno)
154 {
155     if (t == (time_t)-1) {
156         logerror("%s:%d: time weird\n", fname, lno);
157         return 0;
158     }
159     return 1;
160 }
161
162 /*
163  * Parse a time from S into *T.
164  * *ANCHOR is the base for anchor-relative time.
165  * Return pointer to first character not parsed on success,
166  * null pointer on failure.
167  */
168 static char *
169 parse_time(time_t *t, char *s, time_t *anchor)
170 {
171     static char *fmt[] = {
172         /*
173          * Absolute time formats
174          * Must set tm_year, tm_mon, tm_mday, tm_hour, tm_min.
175          */
176         "%Y-%m-%d %H:%M ",      /* ISO 8601 */
177         "%b %d %H:%M %Y ",      /* like ctime(): Dec 22 15:35 2006 */
178         "%d %b %Y %H:%M ",      /* like RFC 2822: 22 Dec 2006 15:35 */
179         /*
180          * Relative to anchor formats
181          * Must set tm_wday, may set tm_hour and tm_min
182          */
183         "next %a %H:%M ",       /* next Fri 15:35 */
184         "next %a ",             /* next Fri */
185         NULL
186     };
187     char *p, *endp;
188     int i;
189     struct tm tm, nexttm;
190
191     for (p = s; isspace(*(unsigned char *)p); ++p) ;
192
193     for (i = 0; ; i++) {
194         if (!fmt[i])
195             return NULL;
196         /*
197          * Carefully initialize tm so we can tell what strptime()
198          * actually set.
199          *
200          * Beware: some losing implementations of strptime() happily
201          * succeed when they fully consumed the first argument,
202          * regardless of whether they matched the full second argument
203          * or not.  Observed on FreeBSD 6.2.
204          */
205         memset(&tm, 0, sizeof(tm));
206         tm.tm_hour = -1;        /* to recognize anchor-relat. w/o time */
207         tm.tm_year = INT_MIN;   /* strptime() lossage work-around */
208         tm.tm_mon = tm.tm_mday = tm.tm_min = tm.tm_wday = -1; /* ditto */
209         endp = strptime(p, fmt[i], &tm);
210         if (endp
211             /* strptime() lossage work-around: */
212             && ((tm.tm_year != INT_MIN && tm.tm_mon != -1
213                  && tm.tm_mday != -1 && tm.tm_hour != -1 && tm.tm_min != -1)
214                 || (tm.tm_wday != -1
215                     && (tm.tm_hour == -1 || tm.tm_min != -1))))
216             break;
217     }
218
219     if (tm.tm_mday == -1) {
220         /* relative to anchor */
221         nexttm = *localtime(anchor);
222         if (tm.tm_hour >= 0) {
223             /* got hour and minute */
224             nexttm.tm_hour = tm.tm_hour;
225             nexttm.tm_min = tm.tm_min;
226             nexttm.tm_sec = 0;
227         }
228         nexttm.tm_mday += tm.tm_wday - nexttm.tm_wday;
229         if (tm.tm_wday <= nexttm.tm_wday)
230             nexttm.tm_mday += 7;
231         tm = nexttm;
232     }
233
234     tm.tm_isdst = -1;
235     *t = mktime(&tm);
236     return endp;
237 }
238
239 /*
240  * Parse an every clause from S into *SECS.
241  * Return pointer to first character not parsed on success,
242  * null pointer on failure.
243  */
244 static char *
245 parse_every(time_t *secs, char *s)
246 {
247     int nch, delta;
248
249     nch = -1;
250     sscanf(s, " every %u hours%n", &delta, &nch);
251     if (nch >= 0)
252         delta *= 60;
253     else
254         sscanf(s, " every %u minutes%n", &delta, &nch);
255     if (nch < 0)
256         return NULL;
257     *secs = 60 * delta;
258     return s + nch;
259 }
260
261 /*
262  * Parse an until clause from S into *T.
263  * *ANCHOR is the base for anchor-relative time.
264  * Return pointer to first character not parsed on success,
265  * null pointer on failure.
266  */
267 static char *
268 parse_until(time_t *t, char *s, time_t *anchor)
269 {
270     int nch;
271
272     nch = -1;
273     sscanf(s, " until%n", &nch);
274     if (nch < 0)
275         return NULL;
276     return parse_time(t, s + nch, anchor);
277 }
278
279 /*
280  * Parse an skip clause from S into *T.
281  * *ANCHOR is the base for anchor-relative time.
282  * Return pointer to first character not parsed on success,
283  * null pointer on failure.
284  */
285 static char *
286 parse_skip(time_t *t, char *s, time_t *anchor)
287 {
288     int nch;
289
290     nch = -1;
291     sscanf(s, " skip%n", &nch);
292     if (nch < 0)
293         return NULL;
294     return parse_time(t, s + nch, anchor);
295 }
296
297 /*
298  * Return the index of the first update at or after T in SCHED[].
299  */
300 static int
301 find_update(time_t t, time_t sched[])
302 {
303     int i;
304
305     /* Could use binary search here, but it's hardly worth it */
306     for (i = 0; sched[i] && t > sched[i]; i++) ;
307     return i;
308 }
309
310 /*
311  * Insert update at T into SCHED[].
312  * SCHED[] holds the first N-1 updates after T0 in ascending order.
313  * If T is before T0 or outside game_days/game_hours, return -1.
314  * If there's no space for T in SCHED[], return N-1.
315  * Else insert T into SCHED[] and return its index in SCHED[].
316  */
317 static int
318 insert_update(time_t t, time_t sched[], int n, time_t t0)
319 {
320     int i;
321
322     if (t <= t0 || !gamehours(t))
323         return -1;
324
325     i = find_update(t, sched);
326     memmove(sched + i + 1, sched + i, (n - 1 - i) * sizeof(*sched));
327     sched[i] = t;
328     sched[n - 1] = 0;
329     return i;
330 }
331
332 /*
333  * Delete update T from SCHED[].
334  * SCHED[] holds N-1 updates in ascending order.
335  * Return the index of the first update after T in SCHED[].
336  */
337 static int
338 delete_update(time_t t, time_t sched[], int n)
339 {
340     int i = find_update(t, sched);
341     if (t == sched[i])
342         memmove(sched + i, sched + i + 1, (n - 1 - i) * sizeof(*sched));
343     return i;
344 }