home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / uuconf.subproj / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  10.8 KB  |  408 lines

  1. /* time.c
  2.    Parse a time string into a uuconf_timespan structure.
  3.  
  4.    Copyright (C) 1992, 1993 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP uuconf library.
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License
  10.    as published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucnfi.h"
  27.  
  28. #if USE_RCS_ID
  29. const char _uuconf_time_rcsid[] = "$Id: time.c,v 1.13 1995/06/21 19:24:39 ian Rel $";
  30. #endif
  31.  
  32. #include <ctype.h>
  33. #include <errno.h>
  34.  
  35. static int itadd_span P((struct sglobal *qglobal, int istart, int iend,
  36.              long ival, int cretry,
  37.              int (*picmp) P((long, long)),
  38.              struct uuconf_timespan **pqspan,
  39.              pointer pblock));
  40. static int itnew P((struct sglobal *qglobal, struct uuconf_timespan **pqset,
  41.             struct uuconf_timespan *qnext, int istart, int iend,
  42.             long ival, int cretry, pointer pblock));
  43.  
  44. /* An array of weekday abbreviations.  The code below assumes that
  45.    each one starts with a lower case letter.  */
  46.  
  47. static const struct
  48. {
  49.   const char *zname;
  50.   int imin;
  51.   int imax;
  52. } asTdays[] =
  53. {
  54.   { "any", 0, 6 },
  55.   { "wk", 1, 5 },
  56.   { "su", 0, 0 },
  57.   { "mo", 1, 1 },
  58.   { "tu", 2, 2 },
  59.   { "we", 3, 3 },
  60.   { "th", 4, 4 },
  61.   { "fr", 5, 5 },
  62.   { "sa", 6, 6 },
  63.   { "never", -1, -2 },
  64.   { "none", -1, -2 },
  65.   { NULL, 0, 0 }
  66. };
  67.  
  68. /* Parse a time string and add it to a span list.  This function is
  69.    given the value, the retry time, and the comparison function to
  70.    use.  */
  71.  
  72. int
  73. _uuconf_itime_parse (qglobal, ztime, ival, cretry, picmp, pqspan, pblock)
  74.      struct sglobal *qglobal;
  75.      char *ztime;
  76.      long ival;
  77.      int cretry;
  78.      int (*picmp) P((long, long));
  79.      struct uuconf_timespan **pqspan;
  80.      pointer pblock;
  81. {
  82.   struct uuconf_timespan *qlist;
  83.   char bfirst;
  84.   const char *z;
  85.  
  86.   qlist = *pqspan;
  87.   if (qlist == (struct uuconf_timespan *) &_uuconf_unset)
  88.     qlist = NULL;
  89.  
  90.   /* Expand the string using a timetable.  Keep rechecking the string
  91.      until it does not match.  */
  92.   while (TRUE)
  93.     {
  94.       char **pz;
  95.       char *zfound;
  96.  
  97.       bfirst = *ztime;
  98.       if (isupper (BUCHAR (bfirst)))
  99.     bfirst = tolower (BUCHAR (bfirst));
  100.  
  101.       zfound = NULL;
  102.       pz = qglobal->qprocess->pztimetables;
  103.  
  104.       /* We want the last timetable to have been defined with this
  105.      name, so we always look through the entire table.  */
  106.       while (*pz != NULL)
  107.     {
  108.       if ((bfirst == (*pz)[0]
  109.            || (isupper (BUCHAR ((*pz)[0]))
  110.            && (int) bfirst == (int) tolower (BUCHAR ((*pz)[0]))))
  111.           && strcasecmp (ztime, *pz) == 0)
  112.         zfound = pz[1];
  113.       pz += 2;
  114.     }
  115.       if (zfound == NULL)
  116.     break;
  117.       ztime = zfound;
  118.     }
  119.  
  120.   /* Look through the time string.  */
  121.   z = ztime;
  122.   while (*z != '\0')
  123.     {
  124.       int iday;
  125.       boolean afday[7];
  126.       int istart, iend;
  127.  
  128.       if (*z == ',' || *z == '|')
  129.     ++z;
  130.       if (*z == '\0' || *z == ';')
  131.     break;
  132.  
  133.       for (iday = 0; iday < 7; iday++)
  134.     afday[iday] = FALSE;
  135.  
  136.       /* Get the days.  */
  137.       do
  138.     {
  139.       bfirst = *z;
  140.       if (isupper (BUCHAR (bfirst)))
  141.         bfirst = tolower (BUCHAR (bfirst));
  142.       for (iday = 0; asTdays[iday].zname != NULL; iday++)
  143.         {
  144.           size_t clen;
  145.  
  146.           if (bfirst != asTdays[iday].zname[0])
  147.         continue;
  148.  
  149.           clen = strlen (asTdays[iday].zname);
  150.           if (strncasecmp (z, asTdays[iday].zname, clen) == 0)
  151.         {
  152.           int iset;
  153.  
  154.           for (iset = asTdays[iday].imin;
  155.                iset <= asTdays[iday].imax;
  156.                iset++)
  157.             afday[iset] = TRUE;
  158.           z += clen;
  159.           break;
  160.         }
  161.         }
  162.       if (asTdays[iday].zname == NULL)
  163.         return UUCONF_SYNTAX_ERROR;
  164.     }
  165.       while (isalpha (BUCHAR (*z)));
  166.  
  167.       /* Get the hours.  */
  168.       if (! isdigit (BUCHAR (*z)))
  169.     {
  170.       istart = 0;
  171.       iend = 24 * 60;
  172.     }
  173.       else
  174.     {
  175.       char *zendnum;
  176.  
  177.       istart = (int) strtol ((char *) z, &zendnum, 10);
  178.       if (*zendnum != '-' || ! isdigit (BUCHAR (zendnum[1])))
  179.         return UUCONF_SYNTAX_ERROR;
  180.       z = zendnum + 1;
  181.       iend = (int) strtol ((char *) z, &zendnum, 10);
  182.       z = zendnum;
  183.  
  184.       istart = (istart / 100) * 60 + istart % 100;
  185.       iend = (iend / 100) * 60 + iend % 100;
  186.     }
  187.  
  188.       /* Add the times we've found onto the list.  */
  189.       for (iday = 0; iday < 7; iday++)
  190.     {
  191.       if (afday[iday])
  192.         {
  193.           int iminute, iret;
  194.  
  195.           iminute = iday * 24 * 60;
  196.           if (istart < iend)
  197.         iret = itadd_span (qglobal, iminute + istart,
  198.                    iminute + iend, ival, cretry, picmp,
  199.                    &qlist, pblock);
  200.           else
  201.         {
  202.           /* Wrap around midnight.  */
  203.           iret = itadd_span (qglobal, iminute, iminute + iend,
  204.                      ival, cretry, picmp, &qlist, pblock);
  205.           if (iret == UUCONF_SUCCESS)
  206.             iret = itadd_span (qglobal, iminute + istart,
  207.                        iminute + 24 * 60, ival, cretry,
  208.                        picmp, &qlist, pblock);
  209.         }
  210.  
  211.           if (iret != UUCONF_SUCCESS)
  212.         return iret;
  213.         }
  214.     }
  215.     }
  216.  
  217.   *pqspan = qlist;
  218.  
  219.   return UUCONF_SUCCESS;
  220. }
  221.  
  222. /* Add a time span to an existing list of time spans.  We keep the
  223.    list sorted by time to make this operation easier.  This modifies
  224.    the existing list, and returns the modified version.  It takes a
  225.    comparison function which should return < 0 if the first argument
  226.    should take precedence over the second argument and == 0 if they
  227.    are the same (for grades this is igradecmp; for sizes it is minus
  228.    (the binary operator)).  */
  229.  
  230. static int
  231. itadd_span (qglobal, istart, iend, ival, cretry, picmp, pqspan, pblock)
  232.      struct sglobal *qglobal;
  233.      int istart;
  234.      int iend;
  235.      long ival;
  236.      int cretry;
  237.      int (*picmp) P((long, long));
  238.      struct uuconf_timespan **pqspan;
  239.      pointer pblock;
  240. {
  241.   struct uuconf_timespan **pq;
  242.   int iret;
  243.  
  244.   /* istart < iend  */
  245.   for (pq = pqspan; *pq != NULL; pq = &(*pq)->uuconf_qnext)
  246.     {
  247.       int icmp;
  248.  
  249.       /* Invariant: PREV (*pq) == NULL || PREV (*pq)->iend <= istart  */
  250.       /* istart < iend && (*pq)->istart < (*pq)->iend  */
  251.  
  252.       if (iend <= (*pq)->uuconf_istart)
  253.     {
  254.       /* istart < iend <= (*pq)->istart < (*pq)->iend  */
  255.       /* No overlap, and we're at the right spot.  See if we can
  256.          combine these spans.  */
  257.       if (iend == (*pq)->uuconf_istart
  258.           && cretry == (*pq)->uuconf_cretry
  259.           && (*picmp) (ival, (*pq)->uuconf_ival) == 0)
  260.         {
  261.           (*pq)->uuconf_istart = istart;
  262.           return UUCONF_SUCCESS;
  263.         }
  264.       /* We couldn't combine them.  */
  265.       break;
  266.     }
  267.  
  268.       if ((*pq)->uuconf_iend <= istart)
  269.     {
  270.       /* (*pq)->istart < (*pq)->iend <= istart < iend  */
  271.       /* No overlap.  Try attaching this span.  */
  272.       if ((*pq)->uuconf_iend == istart
  273.           && (*pq)->uuconf_cretry == cretry
  274.           && ((*pq)->uuconf_qnext == NULL
  275.           || iend <= (*pq)->uuconf_qnext->uuconf_istart)
  276.           && (*picmp) (ival, (*pq)->uuconf_ival) == 0)
  277.         {
  278.           (*pq)->uuconf_iend = iend;
  279.           return UUCONF_SUCCESS;
  280.         }
  281.       /* Couldn't attach; keep looking for the right spot.  We
  282.          might be able to combine part of the new span onto an
  283.          existing span, but it's probably not worth it.  */
  284.       continue;
  285.     }
  286.  
  287.       /* istart < iend
  288.      && (*pq)->istart < (*pq)->iend
  289.      && istart < (*pq)->iend
  290.      && (*pq)->istart < iend  */
  291.       /* Overlap.  */
  292.  
  293.       icmp = (*picmp) (ival, (*pq)->uuconf_ival);
  294.  
  295.       if (icmp == 0)
  296.     {
  297.       /* Just expand the old span to include the new span.  */
  298.       if (istart < (*pq)->uuconf_istart)
  299.         (*pq)->uuconf_istart = istart;
  300.       if ((*pq)->uuconf_iend >= iend)
  301.         return UUCONF_SUCCESS;
  302.       if ((*pq)->uuconf_qnext == NULL
  303.           || iend <= (*pq)->uuconf_qnext->uuconf_istart)
  304.         {
  305.           (*pq)->uuconf_iend = iend;
  306.           return UUCONF_SUCCESS;
  307.         }
  308.       /* The span we are adding overlaps the next span as well.
  309.          Expand the old span up to the next old span, and keep
  310.          trying to add the new span.  */
  311.       (*pq)->uuconf_iend = (*pq)->uuconf_qnext->uuconf_istart;
  312.       istart = (*pq)->uuconf_iend;
  313.     }
  314.       else if (icmp < 0)
  315.     {
  316.       /* Replace the old span with the new span.  */
  317.       if ((*pq)->uuconf_istart < istart)
  318.         {
  319.           /* Save the initial portion of the old span.  */
  320.           iret = itnew (qglobal, pq, *pq, (*pq)->uuconf_istart, istart,
  321.                 (*pq)->uuconf_ival, (*pq)->uuconf_cretry,
  322.                 pblock);
  323.           if (iret != UUCONF_SUCCESS)
  324.         return iret;
  325.           pq = &(*pq)->uuconf_qnext;
  326.         }
  327.       if (iend < (*pq)->uuconf_iend)
  328.         {
  329.           /* Save the final portion of the old span.  */
  330.           iret = itnew (qglobal, &(*pq)->uuconf_qnext,
  331.                 (*pq)->uuconf_qnext, iend, (*pq)->uuconf_iend,
  332.                 (*pq)->uuconf_ival, (*pq)->uuconf_cretry,
  333.                 pblock);
  334.           if (iret != UUCONF_SUCCESS)
  335.         return iret;
  336.         }
  337.       (*pq)->uuconf_ival = ival;
  338.       (*pq)->uuconf_istart = istart;
  339.       (*pq)->uuconf_cretry = cretry;
  340.       if ((*pq)->uuconf_qnext == NULL
  341.           || iend <= (*pq)->uuconf_qnext->uuconf_istart)
  342.         {
  343.           (*pq)->uuconf_iend = iend;
  344.           return UUCONF_SUCCESS;
  345.         }
  346.       /* Move this span up to the next one, and keep trying to add
  347.          the new span.  */
  348.       (*pq)->uuconf_iend = (*pq)->uuconf_qnext->uuconf_istart;
  349.       istart = (*pq)->uuconf_iend;
  350.     }
  351.       else
  352.     {
  353.       /* Leave the old span untouched.  */
  354.       if (istart < (*pq)->uuconf_istart)
  355.         {
  356.           /* Put in the initial portion of the new span.  */
  357.           iret = itnew (qglobal, pq, *pq, istart, (*pq)->uuconf_istart,
  358.                 ival, cretry, pblock);
  359.           if (iret != UUCONF_SUCCESS)
  360.         return iret;
  361.           pq = &(*pq)->uuconf_qnext;
  362.         }
  363.       if (iend <= (*pq)->uuconf_iend)
  364.         return UUCONF_SUCCESS;
  365.       /* Keep trying to add the new span.  */
  366.       istart = (*pq)->uuconf_iend;
  367.     }
  368.     }
  369.  
  370.   /* This is the spot for the new span, and there's no overlap.  */
  371.  
  372.   return itnew (qglobal, pq, *pq, istart, iend, ival, cretry, pblock);
  373. }
  374.  
  375. /* A utility function to create a new uuconf_timespan structure.  */
  376.  
  377. static int
  378. itnew (qglobal, pqset, qnext, istart, iend, ival, cretry, pblock)
  379.      struct sglobal *qglobal;
  380.      struct uuconf_timespan **pqset;
  381.      struct uuconf_timespan *qnext;
  382.      int istart;
  383.      int iend;
  384.      long ival;
  385.      int cretry;
  386.      pointer pblock;
  387. {
  388.   register struct uuconf_timespan *qnew;
  389.  
  390.   qnew = ((struct uuconf_timespan *)
  391.       uuconf_malloc (pblock, sizeof (struct uuconf_timespan)));
  392.   if (qnew == NULL)
  393.     {
  394.       qglobal->ierrno = errno;
  395.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  396.     }
  397.  
  398.   qnew->uuconf_qnext = qnext;
  399.   qnew->uuconf_istart = istart;
  400.   qnew->uuconf_iend = iend;
  401.   qnew->uuconf_ival = ival;
  402.   qnew->uuconf_cretry = cretry;
  403.  
  404.   *pqset = qnew;
  405.  
  406.   return UUCONF_SUCCESS;
  407. }
  408.