#include <string.h>
#include "util.h"

/*

18.70.0.213 - - [10/Nov/1996:00:00:09 -0500] "GET /gif/Icon_SERV.GIF \
HTTP/1.0" 200 648 http://www.mit.edu/ [Mozilla/1.22 (compatible; MSIE 2.0; \
Windows 95)]

 */

int
parse_entry (char *buf, int *match_start, int *match_end)
{
  char *p, *q, *r;
  int i;

  /* find IP address (\d+\.\d+\.\d+\.\d+) (until first space) */
  match_start[0] = 0;
  p = strchr(buf, ' ');
  if (!p)
    return 1;
  match_end[0] = p - buf;

  /* skip to (] "[A-Z]) and take previous (:(\d\d?):[^:]*:) as hour */
  do
  {
    p = strchr(buf, ']');
    if (!p)
      return 1;
  } while (memcmp(p, "] \"", 3));
  if (!(q = memlchr(buf, p, '[')))
    return 1;
  match_start[7] = q-buf;
  match_end[7] = p-buf+1;
  if (!(q = memlchr(buf, p, ':')) ||
      !(q = memlchr(buf, q-1, ':')))
    return 1;
  match_end[1] = q - buf;
  if (*(q-1) < '0' && *(q-1) > '9')
    return 1;
  else if (*(q-2) < '0' && *(q-2) > '9')
  {
    if (*(q-2) == ':')
      match_start[1] = q - buf - 1;
    else
      return 1;
  }
  else if (*(q-3) == ':')
    match_start[1] = q - buf - 2;
  else
    return 1;

  /* skip method to find beginning of url */
  /* find end of quoted string, check for HTTP/1.0, get end of url */
  if (!(q = strchr(p+3, ' ')))
    return 1;
  if (*(q-1) == '"' && q > p + 3)
    q = p + 2;
  match_start[2] = q - buf + 1;
  p = q;
  do
  {
    if (!(q = strchr(q+1, '"')))
      return 1;
  } while (q[1] != ' ' ||
	   (q[2] < '0' && q[2] > '9') || (q[3] < '0' && q[3] > '9') ||
           (q[4] < '0' && q[4] > '9') || q[5] != ' ');
  if ((r = memlchr(buf, q, ' ')) > p)
    match_end[2] = r - buf;
  else
    match_end[2] = q - buf;

  /* get status, size */
  match_start[3] = q - buf + 2;
  match_end[3] = q - buf + 5;
  p = q + 6;
  match_start[4] = p - buf;
  if (p[0] == '-' && p[1] == ' ')
  {
    match_end[4] = p - buf + 1;
    p += 2;
  }
  else
  {
    q = p;
    while (*q >= '0' && *q <= '9')
      q++;
    if (q == p || *q != ' ')
      return 1;
    match_end[4] = q - buf;
    p = q + 1;
  }

  /* find final ], backtrack to matching [, get browser */
  i = 1;
  q = p + strlen(p) - 1;
  if (*q != ']')
    return 1;
  match_end[6] = q - buf;
  q--;
  while (q > p && i)
  {
    if (*q == '[')
    {
      if (i != 1 || *(q-1) == ' ')
	i--;
    }
    else if (*q == ']')
      i++;
    q--;
  }
  if (i || *q != ' ')
  {
    /* that didn't work; try to skip the referrer by going to the next space */
    q = strchr(p, ' ');
    if (!q || q[1] != '[')
      return 1;
  }
  match_start[6] = q - buf + 2;

  /* get referer out of what's left */
  match_start[5] = p - buf;
  match_end[5] = q - buf;

  return(0);
}
