Debian Codegraph @

libhylafax/AtSyntax.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1993-1996 Sam Leffler
4 * Copyright (c) 1993-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "Str.h"
27
28 #include <stdlib.h>
29 #include <time.h>
30 #include <ctype.h>
31
32 #include "NLS.h"
33
34 #define TM_YEAR_BASE 1900 // tm times all start here...
35 #define EPOCH_YEAR 1970
36 #define EPOCH_WDAY 4 // January 1, 1970 was a Thursday
37 #define DAYSPERWEEK 7
38
39 #define HOUR 60 // minutes per hour
40 #define HALFDAY (12 * HOUR) // half a day (12 hours)
41 #define FULLDAY (24 * HOUR) // a full day (24 hours)
42
43 static int
44 isLeapYear(const tm& at) {
45 int y = at.tm_year + TM_YEAR_BASE;
46 return ((y % 4) == 0 && (y % 100) != 0 || (y % 400) == 0);
47 }
48
49 #ifdef streq
50 #undef streq
51 #endif
52 #define streq(a,b,n) (strncasecmp(a,b,n) == 0)
53
54 static bool checkDay(const char*& cp, int& day);
55 static void adjustDay(struct tm& at, int day, const struct tm&);
56 static bool checkMonth(const char*& cp, int& month);
57 static bool parseMonthAndYear(const char*&, const struct tm& ref,
58 struct tm& at, fxStr& emsg);
59 static bool parseMultiplier(const char* cp, struct tm& at, fxStr& emsg);
60 static const char* whitespace(const char* cp);
61 static void fixup(struct tm& at);
62
63 static void _atSyntax(fxStr& emsg, const char* fmt, ...);
64 static void _atError(fxStr& emsg, const char* fmt, ...);
65
66 static int
67 operator<(const struct tm& a, const struct tm& b)
68 {
69 return (
70 (a.tm_year < b.tm_year)
71 || (a.tm_year == b.tm_year && a.tm_yday < b.tm_yday)
72 || (a.tm_year == b.tm_year && a.tm_yday == b.tm_yday &&
73 a.tm_hour < b.tm_hour)
74 || (a.tm_year == b.tm_year && a.tm_yday == b.tm_yday &&
75 a.tm_hour == b.tm_hour && a.tm_min < b.tm_min)
76 );
77 }
78
79 /*
80 * Parse at-style time [date] [+increment] syntax
81 * and return the resultant time, relative to the
82 * specified reference time.
83 */
84 int
85 parseAtSyntax(const char* s, const struct tm& ref, struct tm& at0, fxStr& emsg)
86 {
87 struct tm at = ref;
88
89 /*
90 * Time specification.
91 */
92 const char* cp = s = whitespace(s);
93 int v = 0;
94 if (isdigit(cp[0])) {
95 do
96 v = v*10 + (*cp - '0');
97 while (isdigit(*++cp));
98 if (cp-s < 3) // only hours specified
99 v *= HOUR;
100 else
101 v = (v/100)*HOUR + v%100;
102 if (cp[0] == ':') { // HH::MM notation
103 if (isdigit(cp[1]) && isdigit(cp[2])) {
104 int min = 10*(cp[1]-'0') + (cp[2]-'0');
105 if (min >= 60) {
106 _atError(emsg, NLS::TEXT("Illegal minutes value %u"), min);
107 return (false);
108 }
109 v += min;
110 cp += 3;
111 } else {
112 //translator: HH:MM represents (each two digits) hour:minutes
113 _atSyntax(emsg, NLS::TEXT("expecting HH:MM"));
114 return (false);
115 }
116 }
117 cp = whitespace(cp);
118 if (streq(cp, "am", 2)) {
119 if (v >= HALFDAY+HOUR) {
120 _atError(emsg, NLS::TEXT("%u:%02u is not an AM value"), v/HOUR, v%HOUR);
121 return (false);
122 }
123 if (HALFDAY <= v && v < HALFDAY+HOUR)
124 v -= HALFDAY;
125 cp += 2;
126 } else if (streq(cp, "pm", 2)) {
127 if (v >= HALFDAY+HOUR) {
128 _atError(emsg, NLS::TEXT("%u:%02u is not a PM value"), v/HOUR, v%HOUR);
129 return (false);
130 }
131 if (v < HALFDAY)
132 v += HALFDAY;
133 cp += 2;
134 }
135 } else {
136 if (streq(cp, "noon", 4)) {
137 v = HALFDAY;
138 cp += 4;
139 } else if (streq(cp, "midnight", 8)) {
140 v = 0;
141 cp += 8;
142 } else if (streq(cp, "now", 3)) {
143 v = at.tm_hour*HOUR + at.tm_min;
144 cp += 3;
145 } else if (streq(cp, "next", 4)) {
146 v = at.tm_hour*HOUR + at.tm_min;
147 cp += 4;
148 } else {
149 _atSyntax(emsg, NLS::TEXT("unrecognized symbolic time \"%s\""), cp);
150 return (false);
151 }
152 }
153 if ((unsigned) v >= FULLDAY) {
154 _atError(emsg, NLS::TEXT("Illegal time value; out of range"));
155 return (false);
156 }
157 at.tm_hour = v/HOUR;
158 at.tm_min = v%HOUR;
159 at.tm_sec = 0; // NB: no way to specify seconds
160
161 /*
162 * Check for optional date.
163 */
164 cp = whitespace(cp);
165 if (checkMonth(cp, v)) {
166 at.tm_mon = v;
167 if (!parseMonthAndYear(cp, ref, at, emsg))
168 return (false);
169 } else if (checkDay(cp, v)) {
170 adjustDay(at, v, ref);
171 } else {
172 if (streq(cp, "today", 5)) {
173 cp += 5;
174 } else if (streq(cp, "tomorrow", 8)) {
175 at.tm_yday++;
176 cp += 8;
177 } else if (cp[0] != '\0' && cp[0] != '+') {
178 _atSyntax(emsg, NLS::TEXT("expecting \"+\" after time"));
179 return (false);
180 }
181 /*
182 * Adjust the date according to whether it is before ``now''.
183 */
184 if (at < ref)
185 at.tm_yday++;
186 }
187 /*
188 * Process any increment expression.
189 */
190 if (cp[0] == '+' && !parseMultiplier(++cp, at, emsg))
191 return (false);
192 fixup(at);
193 if (at < ref) {
194 _atError(emsg, NLS::TEXT("Invalid date/time; time must be in the future"));
195 return (false);
196 }
197 at0 = at;
198 return (true);
199 }
200
201 /*
202 * Return a pointer to the next non-ws
203 * item in the character string.
204 */
205 static const char*
206 whitespace(const char* cp)
207 {
208 while (isspace(*cp))
209 cp++;
210 return (cp);
211 }
212
213 #define N(a) (sizeof (a) / sizeof (a[0]))
214
215 /*
216 * Check for a day argument and, if found,
217 * return the day number [0..6] and update
218 * the character pointer.
219 */
220 static bool
221 checkDay(const char*& cp, int& day)
222 {
223 static const char* days[] = {
224 "sunday", "monday", "tuesday", "wednesday",
225 "thursday", "friday", "saturday"
226 };
227 for (u_int i = 0; i < N(days); i++) {
228 u_int len = strlen(days[i]);
229 again:
230 if (streq(cp, days[i], len)) {
231 cp += len;
232 day = i;
233 return (true);
234 }
235 if (len != 3) { // an Algol-style for loop...
236 len = 3;
237 goto again;
238 }
239 }
240 return (false);
241 }
242
243 /*
244 * Adjust tm_yday according to whether or not the
245 * specified day [0...6] is before or after the
246 * current week day. Note that if they are the
247 * same day, then the specified day is assumed to
248 * in the next week. This routine assumes that
249 * tm_wday is correct; which is only true when
250 * setup from ``now''.
251 */
252 static void
253 adjustDay(struct tm& at, int day, const struct tm& ref)
254 {
255 if (at.tm_wday < day)
256 at.tm_yday += day - at.tm_wday;
257 else if (at.tm_wday > day)
258 at.tm_yday += (DAYSPERWEEK - at.tm_wday) + day;
259 else if (at < ref)
260 at.tm_yday += DAYSPERWEEK;
261 }
262
263 static const char* months[] = {
264 "January", "February", "March", "April", "May", "June", "July",
265 "August", "September", "October", "November", "December"
266 };
267
268 /*
269 * Check for a month parameter and if found,
270 * return the month index [0..11] and update
271 * the character pointer.
272 */
273 static bool
274 checkMonth(const char*& cp, int& month)
275 {
276 for (u_int i = 0; i < N(months); i++) {
277 u_int len = strlen(months[i]);
278 again:
279 if (streq(cp, months[i], len)) {
280 cp += len;
281 month = i;
282 return (true);
283 }
284 if (len != 3) { // an Algol-style for loop...
285 len = 3;
286 goto again;
287 }
288 }
289 return (false);
290 }
291
292 /*
293 * The number of days in each month of the year.
294 */
295 static const int nonLeapYear[12] =
296 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
297 static const int leapYear[12] =
298 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
299 static const int* daysInMonth[2] = { nonLeapYear, leapYear };
300
301 static void
302 adjustYDay(struct tm& t)
303 {
304 // adjust year day according to month
305 const int* days = daysInMonth[isLeapYear(t)];
306 t.tm_yday = t.tm_mday;
307 for (int i = 0; i < t.tm_mon; i++)
308 t.tm_yday += days[i];
309 }
310
311 /*
312 * Parse an expected day [, year] expression.
313 */
314 static bool
315 parseMonthAndYear(const char*& cp, const struct tm& ref, struct tm& at, fxStr& emsg)
316 {
317 char* tp;
318 at.tm_mday = (u_int) strtoul(cp, &tp, 10);
319 if (tp == cp) {
320 _atSyntax(emsg, NLS::TEXT("missing or invalid day of month"));
321 return (false);
322 }
323 at.tm_mday--; // tm_mday is [0..31]
324 tp = (char*) whitespace(tp);
325 if (*tp == ',') { // ,[ ]*YYYY
326 char* xp;
327 u_int year = (u_int) strtoul(++tp, &xp, 10);
328 if (tp == xp) {
329 _atSyntax(emsg, NLS::TEXT("missing year"));
330 return (false);
331 }
332 if (year < TM_YEAR_BASE) {
333 _atError(emsg, NLS::TEXT("Sorry, cannot handle dates before %u"),
334 TM_YEAR_BASE);
335 return (false);
336 }
337 at.tm_year = year - TM_YEAR_BASE;
338 adjustYDay(at);
339 tp = xp;
340 } else { // implicit year
341 /*
342 * If the specified date is before ``now'', then it
343 * must be for next year. Note that we have to do
344 * this in two steps since yday can vary because of
345 * leap years.
346 */
347 adjustYDay(at);
348 if (at.tm_yday < ref.tm_yday ||
349 (at.tm_yday == ref.tm_yday && at.tm_hour < ref.tm_hour)) {
350 at.tm_year++;
351 adjustYDay(at);
352 }
353 }
354 const int* days = daysInMonth[isLeapYear(at)];
355 if (at.tm_mday > days[at.tm_mon]) {
356 _atError(emsg, NLS::TEXT("Invalid day of month, %s has only %u days"),
357 months[at.tm_mon], days[at.tm_mon]);
358 return (false);
359 }
360 cp = tp;
361 return (true);
362 }
363
364 /*
365 * Parse an expected multiplier expression.
366 */
367 static bool
368 parseMultiplier(const char* cp, struct tm& at, fxStr& emsg)
369 {
370 cp = whitespace(cp);
371 if (!isdigit(cp[0])) {
372 _atSyntax(emsg, NLS::TEXT("expecting number after \"+\""));
373 return (false);
374 }
375 int v = 0;
376 for (; isdigit(*cp); cp++)
377 v = v*10 + (*cp - '0');
378 cp = whitespace(cp);
379 if (*cp == '\0') {
380 _atSyntax(emsg, NLS::TEXT("\"+%u\" without unit"), v);
381 return (false);
382 }
383 if (streq(cp, "minute", 6))
384 at.tm_min += v;
385 else if (streq(cp, "hour", 4))
386 at.tm_hour += v;
387 else if (streq(cp, "day", 3))
388 at.tm_yday += v;
389 else if (streq(cp, "week", 4))
390 at.tm_yday += DAYSPERWEEK*v;
391 else if (streq(cp, "month", 5)) {
392 at.tm_mon += v;
393 while (at.tm_mon >= 12)
394 at.tm_mon -= 12, at.tm_year++;
395 adjustYDay(at);
396 } else if (streq(cp, "year", 4))
397 at.tm_year += v;
398 else {
399 _atError(emsg, NLS::TEXT("Unknown increment unit \"%s\""), cp);
400 return (false);
401 }
402 return (true);
403 }
404
405 /*
406 * Correct things in case we've slopped over as a result
407 * of an increment expression or similar calculation.
408 * Note that this routine ``believes'' the minutes, hours,
409 * year, and yday values and recalculates month, mday,
410 * and wday values based on the believed values.
411 */
412 static void
413 fixup(struct tm& at)
414 {
415 while (at.tm_min >= HOUR)
416 at.tm_hour++, at.tm_min -= HOUR;
417 while (at.tm_hour >= FULLDAY)
418 at.tm_yday++, at.tm_hour -= FULLDAY;
419 bool leap;
420 int daysinyear;
421 for (;;) {
422 leap = isLeapYear(at);
423 daysinyear = leap ? 366 : 365;
424 if (at.tm_yday < daysinyear)
425 break;
426 at.tm_yday -= daysinyear, at.tm_year++;
427 }
428 /*
429 * Now recalculate derivative values
430 * to insure everything is consistent.
431 */
432 const int* days = daysInMonth[leap];
433 at.tm_mday = at.tm_yday;
434 for (at.tm_mon = 0; at.tm_mday >= days[at.tm_mon]; at.tm_mon++)
435 at.tm_mday -= days[at.tm_mon];
436 at.tm_mday++; // NB: [1..31]
437
438 int eday = at.tm_yday;
439 tm temp;
440 temp.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
441 while (temp.tm_year < at.tm_year) {
442 eday += isLeapYear(temp) ? 366 : 365;
443 temp.tm_year++;
444 }
445 at.tm_wday = (EPOCH_WDAY + eday) % DAYSPERWEEK;
446 }
447
448 static void
449 _atSyntax(fxStr& emsg, const char* fmt, ...)
450 {
451 va_list ap;
452 va_start(ap, fmt);
453 emsg = NLS::TEXT("Syntax error, ") | fxStr::vformat(fmt, ap);
454 va_end(ap);
455 }
456
457 static void
458 _atError(fxStr& emsg, const char* fmt, ...)
459 {
460 va_list ap;
461 va_start(ap, fmt);
462 emsg = fxStr::vformat(fmt, ap);
463 va_end(ap);
464 }