summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libuuconf/time.c
blob: db00d6a993e335039d6687d72ff70520b0e8b43b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* time.c
   Parse a time string into a uuconf_timespan structure.

   Copyright (C) 1992, 1993 Ian Lance Taylor

   This file is part of the Taylor UUCP uuconf library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucnfi.h"

#if USE_RCS_ID
const char _uuconf_time_rcsid[] = "$FreeBSD$";
#endif

#include <ctype.h>
#include <errno.h>

static int itadd_span P((struct sglobal *qglobal, int istart, int iend,
			 long ival, int cretry,
			 int (*picmp) P((long, long)),
			 struct uuconf_timespan **pqspan,
			 pointer pblock));
static int itnew P((struct sglobal *qglobal, struct uuconf_timespan **pqset,
		    struct uuconf_timespan *qnext, int istart, int iend,
		    long ival, int cretry, pointer pblock));

/* An array of weekday abbreviations.  The code below assumes that
   each one starts with a lower case letter.  */

static const struct
{
  const char *zname;
  int imin;
  int imax;
} asTdays[] =
{
  { "any", 0, 6 },
  { "wk", 1, 5 },
  { "su", 0, 0 },
  { "mo", 1, 1 },
  { "tu", 2, 2 },
  { "we", 3, 3 },
  { "th", 4, 4 },
  { "fr", 5, 5 },
  { "sa", 6, 6 },
  { "never", -1, -2 },
  { "none", -1, -2 },
  { NULL, 0, 0 }
};

/* Parse a time string and add it to a span list.  This function is
   given the value, the retry time, and the comparison function to
   use.  */

int
_uuconf_itime_parse (qglobal, ztime, ival, cretry, picmp, pqspan, pblock)
     struct sglobal *qglobal;
     char *ztime;
     long ival;
     int cretry;
     int (*picmp) P((long, long));
     struct uuconf_timespan **pqspan;
     pointer pblock;
{
  struct uuconf_timespan *qlist;
  char bfirst;
  const char *z;

  qlist = *pqspan;
  if (qlist == (struct uuconf_timespan *) &_uuconf_unset)
    qlist = NULL;

  /* Expand the string using a timetable.  Keep rechecking the string
     until it does not match.  */
  while (TRUE)
    {
      char **pz;
      char *zfound;

      bfirst = *ztime;
      if (isupper (BUCHAR (bfirst)))
	bfirst = tolower (BUCHAR (bfirst));

      zfound = NULL;
      pz = qglobal->qprocess->pztimetables;

      /* We want the last timetable to have been defined with this
	 name, so we always look through the entire table.  */
      while (*pz != NULL)
	{
	  if ((bfirst == (*pz)[0]
	       || (isupper (BUCHAR ((*pz)[0]))
		   && (int) bfirst == (int) tolower (BUCHAR ((*pz)[0]))))
	      && strcasecmp (ztime, *pz) == 0)
	    zfound = pz[1];
	  pz += 2;
	}
      if (zfound == NULL)
	break;
      ztime = zfound;
    }

  /* Look through the time string.  */
  z = ztime;
  while (*z != '\0')
    {
      int iday;
      boolean afday[7];
      int istart, iend;

      if (*z == ',' || *z == '|')
	++z;
      if (*z == '\0' || *z == ';')
	break;

      for (iday = 0; iday < 7; iday++)
	afday[iday] = FALSE;

      /* Get the days.  */
      do
	{
	  bfirst = *z;
	  if (isupper (BUCHAR (bfirst)))
	    bfirst = tolower (BUCHAR (bfirst));
	  for (iday = 0; asTdays[iday].zname != NULL; iday++)
	    {
	      size_t clen;

	      if (bfirst != asTdays[iday].zname[0])
		continue;

	      clen = strlen (asTdays[iday].zname);
	      if (strncasecmp (z, asTdays[iday].zname, clen) == 0)
		{
		  int iset;

		  for (iset = asTdays[iday].imin;
		       iset <= asTdays[iday].imax;
		       iset++)
		    afday[iset] = TRUE;
		  z += clen;
		  break;
		}
	    }
	  if (asTdays[iday].zname == NULL)
	    return UUCONF_SYNTAX_ERROR;
	}
      while (isalpha (BUCHAR (*z)));

      /* Get the hours.  */
      if (! isdigit (BUCHAR (*z)))
	{
	  istart = 0;
	  iend = 24 * 60;
	}
      else
	{
	  char *zendnum;

	  istart = (int) strtol ((char *) z, &zendnum, 10);
	  if (*zendnum != '-' || ! isdigit (BUCHAR (zendnum[1])))
	    return UUCONF_SYNTAX_ERROR;
	  z = zendnum + 1;
	  iend = (int) strtol ((char *) z, &zendnum, 10);
	  z = zendnum;

	  istart = (istart / 100) * 60 + istart % 100;
	  iend = (iend / 100) * 60 + iend % 100;
	}

      /* Add the times we've found onto the list.  */
      for (iday = 0; iday < 7; iday++)
	{
	  if (afday[iday])
	    {
	      int iminute, iret;

	      iminute = iday * 24 * 60;
	      if (istart < iend)
		iret = itadd_span (qglobal, iminute + istart,
				   iminute + iend, ival, cretry, picmp,
				   &qlist, pblock);
	      else
		{
		  /* Wrap around midnight.  */
		  iret = itadd_span (qglobal, iminute, iminute + iend,
				     ival, cretry, picmp, &qlist, pblock);
		  if (iret == UUCONF_SUCCESS)
		    iret = itadd_span (qglobal, iminute + istart,
				       iminute + 24 * 60, ival, cretry,
				       picmp, &qlist, pblock);
		}

	      if (iret != UUCONF_SUCCESS)
		return iret;
	    }
	}
    }

  *pqspan = qlist;

  return UUCONF_SUCCESS;
}

/* Add a time span to an existing list of time spans.  We keep the
   list sorted by time to make this operation easier.  This modifies
   the existing list, and returns the modified version.  It takes a
   comparison function which should return < 0 if the first argument
   should take precedence over the second argument and == 0 if they
   are the same (for grades this is igradecmp; for sizes it is minus
   (the binary operator)).  */

static int
itadd_span (qglobal, istart, iend, ival, cretry, picmp, pqspan, pblock)
     struct sglobal *qglobal;
     int istart;
     int iend;
     long ival;
     int cretry;
     int (*picmp) P((long, long));
     struct uuconf_timespan **pqspan;
     pointer pblock;
{
  struct uuconf_timespan **pq;
  int iret;

  /* istart < iend  */
  for (pq = pqspan; *pq != NULL; pq = &(*pq)->uuconf_qnext)
    {
      int icmp;

      /* Invariant: PREV (*pq) == NULL || PREV (*pq)->iend <= istart  */
      /* istart < iend && (*pq)->istart < (*pq)->iend  */

      if (iend <= (*pq)->uuconf_istart)
	{
	  /* istart < iend <= (*pq)->istart < (*pq)->iend  */
	  /* No overlap, and we're at the right spot.  See if we can
	     combine these spans.  */
	  if (iend == (*pq)->uuconf_istart
	      && cretry == (*pq)->uuconf_cretry
	      && (*picmp) (ival, (*pq)->uuconf_ival) == 0)
	    {
	      (*pq)->uuconf_istart = istart;
	      return UUCONF_SUCCESS;
	    }
	  /* We couldn't combine them.  */
	  break;
	}

      if ((*pq)->uuconf_iend <= istart)
	{
	  /* (*pq)->istart < (*pq)->iend <= istart < iend  */
	  /* No overlap.  Try attaching this span.  */
	  if ((*pq)->uuconf_iend == istart
	      && (*pq)->uuconf_cretry == cretry
	      && ((*pq)->uuconf_qnext == NULL
		  || iend <= (*pq)->uuconf_qnext->uuconf_istart)
	      && (*picmp) (ival, (*pq)->uuconf_ival) == 0)
	    {
	      (*pq)->uuconf_iend = iend;
	      return UUCONF_SUCCESS;
	    }
	  /* Couldn't attach; keep looking for the right spot.  We
	     might be able to combine part of the new span onto an
	     existing span, but it's probably not worth it.  */
	  continue;
	}

      /* istart < iend
	 && (*pq)->istart < (*pq)->iend
	 && istart < (*pq)->iend
	 && (*pq)->istart < iend  */
      /* Overlap.  */

      icmp = (*picmp) (ival, (*pq)->uuconf_ival);

      if (icmp == 0)
	{
	  /* Just expand the old span to include the new span.  */
	  if (istart < (*pq)->uuconf_istart)
	    (*pq)->uuconf_istart = istart;
	  if ((*pq)->uuconf_iend >= iend)
	    return UUCONF_SUCCESS;
	  if ((*pq)->uuconf_qnext == NULL
	      || iend <= (*pq)->uuconf_qnext->uuconf_istart)
	    {
	      (*pq)->uuconf_iend = iend;
	      return UUCONF_SUCCESS;
	    }
	  /* The span we are adding overlaps the next span as well.
	     Expand the old span up to the next old span, and keep
	     trying to add the new span.  */
	  (*pq)->uuconf_iend = (*pq)->uuconf_qnext->uuconf_istart;
	  istart = (*pq)->uuconf_iend;
	}
      else if (icmp < 0)
	{
	  /* Replace the old span with the new span.  */
	  if ((*pq)->uuconf_istart < istart)
	    {
	      /* Save the initial portion of the old span.  */
	      iret = itnew (qglobal, pq, *pq, (*pq)->uuconf_istart, istart,
			    (*pq)->uuconf_ival, (*pq)->uuconf_cretry,
			    pblock);
	      if (iret != UUCONF_SUCCESS)
		return iret;
	      pq = &(*pq)->uuconf_qnext;
	    }
	  if (iend < (*pq)->uuconf_iend)
	    {
	      /* Save the final portion of the old span.  */
	      iret = itnew (qglobal, &(*pq)->uuconf_qnext,
			    (*pq)->uuconf_qnext, iend, (*pq)->uuconf_iend,
			    (*pq)->uuconf_ival, (*pq)->uuconf_cretry,
			    pblock);
	      if (iret != UUCONF_SUCCESS)
		return iret;
	    }
	  (*pq)->uuconf_ival = ival;
	  (*pq)->uuconf_istart = istart;
	  (*pq)->uuconf_cretry = cretry;
	  if ((*pq)->uuconf_qnext == NULL
	      || iend <= (*pq)->uuconf_qnext->uuconf_istart)
	    {
	      (*pq)->uuconf_iend = iend;
	      return UUCONF_SUCCESS;
	    }
	  /* Move this span up to the next one, and keep trying to add
	     the new span.  */
	  (*pq)->uuconf_iend = (*pq)->uuconf_qnext->uuconf_istart;
	  istart = (*pq)->uuconf_iend;
	}
      else
	{
	  /* Leave the old span untouched.  */
	  if (istart < (*pq)->uuconf_istart)
	    {
	      /* Put in the initial portion of the new span.  */
	      iret = itnew (qglobal, pq, *pq, istart, (*pq)->uuconf_istart,
			    ival, cretry, pblock);
	      if (iret != UUCONF_SUCCESS)
		return iret;
	      pq = &(*pq)->uuconf_qnext;
	    }
	  if (iend <= (*pq)->uuconf_iend)
	    return UUCONF_SUCCESS;
	  /* Keep trying to add the new span.  */
	  istart = (*pq)->uuconf_iend;
	}
    }

  /* This is the spot for the new span, and there's no overlap.  */

  return itnew (qglobal, pq, *pq, istart, iend, ival, cretry, pblock);
}

/* A utility function to create a new uuconf_timespan structure.  */

static int
itnew (qglobal, pqset, qnext, istart, iend, ival, cretry, pblock)
     struct sglobal *qglobal;
     struct uuconf_timespan **pqset;
     struct uuconf_timespan *qnext;
     int istart;
     int iend;
     long ival;
     int cretry;
     pointer pblock;
{
  register struct uuconf_timespan *qnew;

  qnew = ((struct uuconf_timespan *)
	  uuconf_malloc (pblock, sizeof (struct uuconf_timespan)));
  if (qnew == NULL)
    {
      qglobal->ierrno = errno;
      return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
    }

  qnew->uuconf_qnext = qnext;
  qnew->uuconf_istart = istart;
  qnew->uuconf_iend = iend;
  qnew->uuconf_ival = ival;
  qnew->uuconf_cretry = cretry;

  *pqset = qnew;

  return UUCONF_SUCCESS;
}
OpenPOWER on IntegriCloud